terminal.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch.
  2. Commands are spawned as separate processes for preemptive multitasking. */
  3. #include "io.h"
  4. #include "dbg.h"
  5. #include "kexec.h"
  6. #include "kmalloc.h"
  7. #include "mm.h"
  8. #include "process.h"
  9. #include "terminal.h"
  10. #include "timer.h"
  11. extern void program_reboot_main(void);
  12. extern void program_poweroff_main(void);
  13. extern void program_run_main(const char *filename);
  14. extern void program_display_main(void);
  15. extern void program_gfxbench_main(void);
  16. extern void program_serial_loop_main(const char *arg);
  17. extern void program_serial_recv_main(const char *arg);
  18. extern void program_run_serial_main(const char *arg);
  19. extern void program_run_embedded_main(const char *name);
  20. extern void program_lsusb_main(void);
  21. extern void program_format_main(const char *arg);
  22. extern void program_lspci_main(const char *filter);
  23. extern void program_peek_main(const char *arg);
  24. extern void program_poke_main(const char *arg);
  25. static inline unsigned int term_irq_save_disable(void)
  26. {
  27. unsigned int flags;
  28. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  29. return flags;
  30. }
  31. static inline void term_irq_restore(unsigned int flags)
  32. {
  33. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  34. }
  35. /* Print the prompt (with the current directory) and set line_start. */
  36. static void term_print_prompt(struct term *t, int first)
  37. {
  38. if (!first)
  39. io_putchar('\n');
  40. io_putchar('[');
  41. io_putstr(t->cwd);
  42. io_putstr("] > ");
  43. t->line_start = io_get_cursor();
  44. }
  45. /* ---- Command registry ----
  46. *
  47. * One table drives command dispatch, `help` and Tab completion. Most commands
  48. * are spawned as separate kernel processes; the CK_CUSTOM handlers are the
  49. * shell builtins, the commands that must run inline (ps/kill/kexec/...) and
  50. * `run`, the one command still unavailable in a windowed terminal. */
  51. enum cmd_kind {
  52. CK_SPAWN = 0, /* spawn, no arguments */
  53. CK_SPAWN_ARG, /* spawn with the raw remainder of the line */
  54. CK_CUSTOM /* handled by a cmd_*() handler below */
  55. };
  56. #define CMD_REQARG 0x01 /* the argument must be non-empty */
  57. #define CMD_BUILTIN 0x04 /* handled inline, before the PATH lookup */
  58. struct kcmd {
  59. const char *name; /* dispatch token */
  60. const char *usage; /* `help` line (and completion name) */
  61. unsigned char kind;
  62. unsigned char flags;
  63. void (*fn)(void); /* spawned entry, no argument */
  64. void (*fn_arg)(const char *); /* spawned entry with an argument */
  65. const char *fixed; /* CK_SPAWN: fixed argument, if any */
  66. int (*custom)(struct term *t, const char *args);
  67. };
  68. /* The terminal's spawn/path helpers are defined later in this file. */
  69. static void term_cd(struct term *t, const char *arg);
  70. static void term_spawn(struct term *t, const char *name, void (*entry)(void));
  71. static void term_spawn_arg(struct term *t, const char *name,
  72. void (*entry)(const char *), const char *arg);
  73. static int term_path_spawn(struct term *t, const char *word, const char *args);
  74. static int cmd_cd(struct term *t, const char *args);
  75. static int cmd_clear(struct term *t, const char *args);
  76. static int cmd_dbg(struct term *t, const char *args);
  77. static int cmd_format(struct term *t, const char *args);
  78. static int cmd_help(struct term *t, const char *args);
  79. static int cmd_path(struct term *t, const char *args);
  80. static int cmd_kexec(struct term *t, const char *args);
  81. static int cmd_kill(struct term *t, const char *args);
  82. static int cmd_poweroff(struct term *t, const char *args);
  83. static int cmd_ps(struct term *t, const char *args);
  84. static int cmd_renice(struct term *t, const char *args);
  85. static int cmd_sched(struct term *t, const char *args);
  86. static int cmd_reboot(struct term *t, const char *args);
  87. static int cmd_run(struct term *t, const char *args);
  88. static int cmd_serial(struct term *t, const char *args);
  89. /* Table order defines the `help` layout (printed as two columns). Ring-3
  90. * commands are not listed here: they are files on the PATH (see cmd_path). */
  91. static const struct kcmd cmds[] = {
  92. { "cd", "cd [path]", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_cd },
  93. { "path", "path [dir[:dir...]]", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_path },
  94. { "help", "help", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_help },
  95. { "clear", "clear", CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_clear },
  96. { "run", "run <name>|<file>", CK_CUSTOM, 0, 0, 0, 0, cmd_run },
  97. { "space", "space", CK_SPAWN, 0, 0, program_run_embedded_main, "space", 0 },
  98. { "prog", "prog", CK_SPAWN, 0, 0, program_run_embedded_main, "prog", 0 },
  99. { "reboot", "reboot", CK_CUSTOM, 0, 0, 0, 0, cmd_reboot },
  100. { "poweroff", "poweroff", CK_CUSTOM, 0, 0, 0, 0, cmd_poweroff },
  101. { "display", "display", CK_SPAWN, 0, program_display_main, 0, 0, 0 },
  102. { "gfxbench", "gfxbench", CK_SPAWN, 0, program_gfxbench_main, 0, 0, 0 },
  103. { "lsusb", "lsusb", CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
  104. { "format", "format <device>", CK_CUSTOM, 0, 0, 0, 0, cmd_format },
  105. { "lspci", "lspci [id]", CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
  106. { "peek", "peek <addr> [len]", CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
  107. { "poke", "poke <addr> <val>", CK_SPAWN_ARG, CMD_REQARG, 0, program_poke_main, 0, 0 },
  108. { "ps", "ps", CK_CUSTOM, 0, 0, 0, 0, cmd_ps },
  109. { "sched", "sched", CK_CUSTOM, 0, 0, 0, 0, cmd_sched },
  110. { "renice", "renice <pid> <weight>", CK_CUSTOM, CMD_REQARG, 0, 0, 0, cmd_renice },
  111. { "kill", "kill <pid>", CK_CUSTOM, 0, 0, 0, 0, cmd_kill },
  112. { "kexec", "kexec", CK_CUSTOM, 0, 0, 0, 0, cmd_kexec },
  113. { "dbg", "dbg", CK_CUSTOM, 0, 0, 0, 0, cmd_dbg },
  114. { "serial", "serial test|recv", CK_CUSTOM, 0, 0, 0, 0, cmd_serial },
  115. };
  116. #define NCMDS (sizeof(cmds) / sizeof(cmds[0]))
  117. /* Find the registry entry whose name matches the first word of the line. */
  118. static const struct kcmd *cmd_lookup(const char *line, unsigned int line_len)
  119. {
  120. unsigned int i = 0, j, k, m;
  121. while (i < line_len && line[i] == ' ')
  122. i++;
  123. for (j = 0; i + j < line_len && line[i + j] != ' '; j++)
  124. ;
  125. if (j == 0)
  126. return 0;
  127. for (k = 0; k < NCMDS; k++) {
  128. const char *n = cmds[k].name;
  129. for (m = 0; m < j && n[m]; m++)
  130. if (line[i + m] != n[m])
  131. break;
  132. if (m == j && n[m] == '\0')
  133. return &cmds[k];
  134. }
  135. return 0;
  136. }
  137. static void cmd_usage(const struct kcmd *c)
  138. {
  139. io_putstr(c->name);
  140. io_putstr(": usage ");
  141. io_putstr(c->usage);
  142. io_putchar('\n');
  143. }
  144. /* Split the line into the command word and the trimmed remainder. */
  145. static void term_split_line(struct term *t, char *word, unsigned int wsize,
  146. char *args, unsigned int asize)
  147. {
  148. unsigned int i = 0, n = 0;
  149. while (i < t->line_len && t->line[i] == ' ')
  150. i++;
  151. while (i < t->line_len && t->line[i] != ' ' && n + 1 < wsize)
  152. word[n++] = t->line[i++];
  153. word[n] = '\0';
  154. while (i < t->line_len && t->line[i] == ' ')
  155. i++;
  156. n = 0;
  157. while (i < t->line_len && n + 1 < asize)
  158. args[n++] = t->line[i++];
  159. while (n > 0 && args[n - 1] == ' ')
  160. n--;
  161. args[n] = '\0';
  162. }
  163. /* Dispatch one registry entry. Returns 1 when the handler already printed the
  164. * next prompt (clear), 0 otherwise. */
  165. static int term_run_command(struct term *t, const struct kcmd *c,
  166. const char *args)
  167. {
  168. if ((c->flags & CMD_REQARG) && args[0] == '\0') {
  169. cmd_usage(c);
  170. return 0;
  171. }
  172. switch (c->kind) {
  173. case CK_SPAWN:
  174. if (c->fixed)
  175. term_spawn_arg(t, c->name, c->fn_arg, c->fixed);
  176. else
  177. term_spawn(t, c->name, c->fn);
  178. break;
  179. case CK_SPAWN_ARG:
  180. term_spawn_arg(t, c->name, c->fn_arg, args);
  181. break;
  182. case CK_CUSTOM:
  183. return c->custom(t, args);
  184. }
  185. return 0;
  186. }
  187. /* `help`: printed as two 32-column columns so it fits the 64-column terminal
  188. * without scrolling. */
  189. static void help_item_pad(const char *s)
  190. {
  191. unsigned int n = 0;
  192. while (s[n]) {
  193. io_putchar(s[n]);
  194. n++;
  195. }
  196. while (n < 32) {
  197. io_putchar(' ');
  198. n++;
  199. }
  200. }
  201. static void help_print(void)
  202. {
  203. unsigned int nitems = NCMDS;
  204. unsigned int half = (nitems + 1) / 2;
  205. unsigned int k;
  206. for (k = 0; k < half; k++) {
  207. if (k + half < nitems) {
  208. help_item_pad(cmds[k].usage);
  209. io_putstr(cmds[k + half].usage);
  210. } else {
  211. io_putstr(cmds[k].usage);
  212. }
  213. io_putchar('\n');
  214. }
  215. }
  216. /* ---- Command handlers that run inline in the terminal ---- */
  217. static int cmd_cd(struct term *t, const char *args)
  218. {
  219. term_cd(t, args);
  220. return 0;
  221. }
  222. static int cmd_clear(struct term *t, const char *args)
  223. {
  224. (void)args;
  225. io_clear_screen();
  226. term_print_prompt(t, 1);
  227. return 1; /* prompt already printed */
  228. }
  229. static int cmd_dbg(struct term *t, const char *args)
  230. {
  231. (void)t;
  232. (void)args;
  233. program_dbg_main();
  234. return 0;
  235. }
  236. static int cmd_format(struct term *t, const char *args)
  237. {
  238. char dev[16];
  239. unsigned int n = 0;
  240. const char *p;
  241. (void)t;
  242. while (args[n] && args[n] != ' ' && n + 1 < sizeof(dev))
  243. dev[n] = args[n], n++;
  244. dev[n] = '\0';
  245. p = args + n;
  246. while (*p == ' ')
  247. p++;
  248. if (n == 0 || *p) {
  249. io_putstr("format: usage format <device>\n");
  250. return 0;
  251. }
  252. program_format_main(dev);
  253. return 0;
  254. }
  255. /* `path` builtin: show or set the command search path. */
  256. static int path_entry_ok(const char *entry)
  257. {
  258. unsigned int size = 0;
  259. int is_dir = 0;
  260. if (entry[0] != '/')
  261. return 0;
  262. return vfs_stat(entry, &size, &is_dir) == 0 && is_dir;
  263. }
  264. static int cmd_path(struct term *t, const char *args)
  265. {
  266. if (args[0] == '\0') {
  267. io_putstr("path=");
  268. io_putstr(t->path);
  269. io_putchar('\n');
  270. return 0;
  271. }
  272. /* Validate every colon-separated entry (absolute, existing dir) and
  273. * rebuild the path without stray spaces. */
  274. {
  275. char entry[VFS_PATH_MAX];
  276. char normalized[VFS_PATH_MAX];
  277. unsigned int i = 0, n = 0, o = 0;
  278. for (;;) {
  279. char c = args[i];
  280. if (c == ':' || c == '\0') {
  281. entry[n] = '\0';
  282. if (n > 0) {
  283. unsigned int k;
  284. if (!path_entry_ok(entry)) {
  285. io_putstr("path: not a directory: ");
  286. io_putstr(entry);
  287. io_putchar('\n');
  288. return 0;
  289. }
  290. if (o > 0) {
  291. if (o + 1 >= sizeof(normalized)) {
  292. io_putstr("path: too long\n");
  293. return 0;
  294. }
  295. normalized[o++] = ':';
  296. }
  297. for (k = 0; entry[k]; k++) {
  298. if (o + 1 >= sizeof(normalized)) {
  299. io_putstr("path: too long\n");
  300. return 0;
  301. }
  302. normalized[o++] = entry[k];
  303. }
  304. }
  305. n = 0;
  306. if (c == '\0')
  307. break;
  308. i++;
  309. continue;
  310. }
  311. if (!(c == ' ' && n == 0)) {
  312. if (n + 1 >= sizeof(entry)) {
  313. io_putstr("path: entry too long\n");
  314. return 0;
  315. }
  316. entry[n++] = c;
  317. }
  318. i++;
  319. }
  320. if (o == 0) {
  321. io_putstr("path: empty\n");
  322. return 0;
  323. }
  324. normalized[o] = '\0';
  325. for (i = 0; i + 1 < sizeof(t->path) && normalized[i]; i++)
  326. t->path[i] = normalized[i];
  327. t->path[i] = '\0';
  328. }
  329. io_putstr("path set to ");
  330. io_putstr(t->path);
  331. io_putchar('\n');
  332. return 0;
  333. }
  334. /* List the command files found on the search path. */
  335. static void help_print_path(struct term *t)
  336. {
  337. const char *p = t->path;
  338. while (*p) {
  339. char dir[VFS_PATH_MAX];
  340. unsigned int n = 0, col = 0, i;
  341. while (*p && *p != ':' && n + 1 < sizeof(dir))
  342. dir[n++] = *p++;
  343. dir[n] = '\0';
  344. if (*p == ':')
  345. p++;
  346. if (dir[0] == '\0')
  347. continue;
  348. io_putstr(dir);
  349. io_putstr(":\n");
  350. for (i = 0; ; i++) {
  351. struct vfs_dirent d;
  352. unsigned int len = 0;
  353. int r = vfs_list_dir(dir, i, &d);
  354. if (r != 1)
  355. break;
  356. if (d.is_dir)
  357. continue;
  358. while (d.name[len])
  359. len++;
  360. if (col > 0 && col + 2 + len > t->screen.cols) {
  361. io_putchar('\n');
  362. col = 0;
  363. }
  364. if (col > 0) {
  365. io_putstr(" ");
  366. col += 2;
  367. }
  368. io_putstr(d.name);
  369. col += len;
  370. }
  371. io_putchar('\n');
  372. }
  373. }
  374. static int cmd_help(struct term *t, const char *args)
  375. {
  376. (void)args;
  377. help_print();
  378. help_print_path(t);
  379. return 0;
  380. }
  381. static int cmd_kexec(struct term *t, const char *args)
  382. {
  383. (void)t;
  384. (void)args;
  385. if (kexec_busy) {
  386. io_putstr("kexec: already receiving\n");
  387. return 0;
  388. }
  389. kexec_busy = 1;
  390. if (process_create("kexec", program_kexec_main) < 0) {
  391. kexec_busy = 0;
  392. io_putstr("error: too many processes\n");
  393. }
  394. return 0;
  395. }
  396. static int cmd_kill(struct term *t, const char *args)
  397. {
  398. unsigned int pid = 0;
  399. int has_digit = 0;
  400. (void)t;
  401. while (*args >= '0' && *args <= '9') {
  402. pid = pid * 10 + (unsigned int)(*args - '0');
  403. args++;
  404. has_digit = 1;
  405. }
  406. while (*args == ' ')
  407. args++;
  408. if (!has_digit || *args) {
  409. io_putstr("kill: usage kill <pid>\n");
  410. return 0;
  411. }
  412. if (process_kill(pid) < 0)
  413. io_putstr("kill: no such process\n");
  414. else
  415. io_putstr("killed\n");
  416. return 0;
  417. }
  418. static int cmd_poweroff(struct term *t, const char *args)
  419. {
  420. (void)t;
  421. (void)args;
  422. program_poweroff_main();
  423. return 0;
  424. }
  425. static int cmd_ps(struct term *t, const char *args)
  426. {
  427. (void)t;
  428. (void)args;
  429. process_ps();
  430. return 0;
  431. }
  432. static int cmd_sched(struct term *t, const char *args)
  433. {
  434. (void)t;
  435. (void)args;
  436. sched_print();
  437. return 0;
  438. }
  439. static int cmd_renice(struct term *t, const char *args)
  440. {
  441. unsigned int pid = 0, weight = 0;
  442. int have_pid = 0, have_weight = 0;
  443. (void)t;
  444. while (*args >= '0' && *args <= '9') {
  445. pid = pid * 10 + (unsigned int)(*args - '0');
  446. args++;
  447. have_pid = 1;
  448. }
  449. while (*args == ' ')
  450. args++;
  451. while (*args >= '0' && *args <= '9') {
  452. weight = weight * 10 + (unsigned int)(*args - '0');
  453. args++;
  454. have_weight = 1;
  455. }
  456. while (*args == ' ')
  457. args++;
  458. if (!have_pid || !have_weight || *args ||
  459. weight < 1 || weight > SCHED_WEIGHT_MAX) {
  460. io_putstr("renice: usage renice <pid> <1..16>\n");
  461. return 0;
  462. }
  463. if (process_set_weight(pid, weight) < 0)
  464. io_putstr("renice: no such process\n");
  465. else
  466. io_putstr("weight changed\n");
  467. return 0;
  468. }
  469. static int cmd_reboot(struct term *t, const char *args)
  470. {
  471. (void)t;
  472. (void)args;
  473. program_reboot_main();
  474. return 0;
  475. }
  476. static int cmd_run(struct term *t, const char *args)
  477. {
  478. (void)t;
  479. if (args[0] == 's' && args[1] == 'e' && args[2] == 'r' &&
  480. args[3] == 'i' && args[4] == 'a' && args[5] == 'l' &&
  481. args[6] == '\0') {
  482. if (process_create_arg("run_serial", program_run_serial_main, "serial") < 0)
  483. io_putstr("error: too many processes\n");
  484. } else {
  485. io_putstr("run: not available in windowed mode yet\n");
  486. }
  487. return 0;
  488. }
  489. static int cmd_serial(struct term *t, const char *args)
  490. {
  491. (void)t;
  492. if (args[0] == 't' && args[1] == 'e' && args[2] == 's' && args[3] == 't' &&
  493. args[4] == '\0') {
  494. if (process_create_arg("serial_loop", program_serial_loop_main, "test") < 0)
  495. io_putstr("error: too many processes\n");
  496. } else if (args[0] == 'r' && args[1] == 'e' && args[2] == 'c' &&
  497. args[3] == 'v' && args[4] == '\0') {
  498. program_serial_recv_main("recv");
  499. } else {
  500. io_putstr("serial: usage serial test|recv\n");
  501. }
  502. return 0;
  503. }
  504. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  505. static void redraw_line_from(const char *line, unsigned int line_len,
  506. unsigned int line_pos, unsigned int line_start, unsigned int from)
  507. {
  508. unsigned int i;
  509. io_set_cursor(line_start + from);
  510. for (i = from; i < line_len; i++)
  511. io_putchar(line[i]);
  512. io_putchar(' ');
  513. io_set_cursor(line_start + line_pos);
  514. }
  515. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  516. static void redraw_line_clear(const char *line, unsigned int line_len,
  517. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  518. {
  519. unsigned int i;
  520. io_set_cursor(line_start);
  521. for (i = 0; i < line_len; i++)
  522. io_putchar(line[i]);
  523. for (i = line_len; i <= clear_through; i++)
  524. io_putchar(' ');
  525. io_set_cursor(line_start + line_pos);
  526. }
  527. /* Copy history entry (1 = newest) into t->line, redraw from start. */
  528. static void history_load_into_line(struct term *t, unsigned int browse)
  529. {
  530. unsigned int slot, len, old_len;
  531. const char *src;
  532. if (browse < 1 || browse > t->history_count)
  533. return;
  534. old_len = t->line_len;
  535. slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  536. src = t->history[slot];
  537. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  538. t->line[len] = src[len];
  539. t->line[len] = '\0';
  540. t->line_len = len;
  541. t->line_pos = len;
  542. redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
  543. }
  544. /* ---- Tab completion ---- */
  545. static char lower_ch(char c)
  546. {
  547. return (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
  548. }
  549. static int prefix_ci(const char *s, const char *prefix, unsigned int plen)
  550. {
  551. unsigned int i;
  552. for (i = 0; i < plen; i++)
  553. if (lower_ch(s[i]) != lower_ch(prefix[i]))
  554. return 0;
  555. return 1;
  556. }
  557. /* "HELLO TXT" -> "hello.txt" (a completion-ready 8.3 display name). */
  558. static void tab_display_name(const char *n, char *out)
  559. {
  560. unsigned int bl = 0, el = 0, i, j = 0;
  561. for (i = 0; i < 8 && n[i] && n[i] != ' '; i++)
  562. bl = i + 1;
  563. for (i = 8; i < 11 && n[i] && n[i] != ' '; i++)
  564. el = i - 8 + 1;
  565. for (i = 0; i < bl; i++)
  566. out[j++] = lower_ch(n[i]);
  567. if (el) {
  568. out[j++] = '.';
  569. for (i = 0; i < el; i++)
  570. out[j++] = lower_ch(n[8 + i]);
  571. }
  572. out[j] = '\0';
  573. }
  574. /* Shared completion state (fat_list_dir takes no context pointer). */
  575. static char tab_prefix[16];
  576. static unsigned int tab_prefix_len;
  577. static unsigned int tab_count;
  578. static unsigned int tab_lcp_len;
  579. static char tab_lcp[16];
  580. static int tab_single_dir;
  581. static int tab_raw_83; /* current dir holds raw 8.3 names */
  582. static char tab_scan_path[VFS_PATH_MAX]; /* dir of the last tab_scan_dir() */
  583. static unsigned int tab_col;
  584. static unsigned int tab_width;
  585. /* Display form of a name: FAT's raw 8.3 gets "NAME EXT" -> "name.ext",
  586. * everything else (ramfs) is just lowercased. */
  587. static void tab_disp_name(const char *name, char *out)
  588. {
  589. unsigned int i;
  590. if (tab_raw_83) {
  591. tab_display_name(name, out);
  592. return;
  593. }
  594. for (i = 0; name[i] && i < 15; i++)
  595. out[i] = lower_ch(name[i]);
  596. out[i] = '\0';
  597. }
  598. static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
  599. {
  600. char disp[16];
  601. unsigned int i, n;
  602. (void)size;
  603. if (name[0] == '.') /* "." / ".." */
  604. return;
  605. tab_disp_name(name, disp);
  606. if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
  607. return;
  608. n = 0;
  609. while (disp[n])
  610. n++;
  611. if (tab_count == 0) {
  612. for (i = 0; i < n; i++)
  613. tab_lcp[i] = disp[i];
  614. tab_lcp_len = n;
  615. tab_single_dir = is_dir;
  616. } else {
  617. i = 0;
  618. while (i < tab_lcp_len && i < n && tab_lcp[i] == disp[i])
  619. i++;
  620. tab_lcp_len = i;
  621. }
  622. tab_count++;
  623. }
  624. static void tab_print_one(const char *name, int is_dir)
  625. {
  626. unsigned int n = 0, w;
  627. while (name[n])
  628. n++;
  629. w = n + (is_dir ? 1 : 0);
  630. if (tab_col > 0 && tab_col + 2 + w > tab_width) {
  631. io_putchar('\n');
  632. tab_col = 0;
  633. }
  634. if (tab_col > 0) {
  635. io_putstr(" ");
  636. tab_col += 2;
  637. }
  638. io_putstr(name);
  639. if (is_dir)
  640. io_putchar('/');
  641. tab_col += w;
  642. }
  643. static void tab_print_cb(const char *name, int is_dir, unsigned int size)
  644. {
  645. char disp[16];
  646. (void)size;
  647. if (name[0] == '.')
  648. return;
  649. tab_disp_name(name, disp);
  650. if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
  651. return;
  652. tab_print_one(disp, is_dir);
  653. }
  654. static int tab_scan_dir(const char *abs)
  655. {
  656. struct vfs_mount *m;
  657. char rel[VFS_PATH_MAX];
  658. unsigned int i;
  659. if (vfs_resolve(abs, &m, rel, sizeof(rel)) != 0)
  660. return 0;
  661. tab_raw_83 = m->ops->raw_83;
  662. {
  663. unsigned int j;
  664. for (j = 0; abs[j] && j + 1 < sizeof(tab_scan_path); j++)
  665. tab_scan_path[j] = abs[j];
  666. tab_scan_path[j] = '\0';
  667. }
  668. tab_count = 0;
  669. tab_lcp_len = 0;
  670. for (i = 0; ; i++) {
  671. struct vfs_dirent d;
  672. int r = m->ops->list_dir(m, rel, i, &d);
  673. if (r != 1)
  674. break;
  675. tab_collect_cb(d.name, (int)d.is_dir, d.size);
  676. }
  677. return (int)tab_count;
  678. }
  679. static int tab_scan_mounts(void)
  680. {
  681. unsigned int i;
  682. tab_count = 0;
  683. tab_lcp_len = 0;
  684. for (i = 0; i < vfs_mount_count; i++) {
  685. const char *name = vfs_mounts[i].name;
  686. unsigned int n = 0, j;
  687. while (name[n])
  688. n++;
  689. if (!prefix_ci(name, tab_prefix, tab_prefix_len))
  690. continue;
  691. if (tab_count == 0) {
  692. for (j = 0; j < n; j++)
  693. tab_lcp[j] = name[j];
  694. tab_lcp_len = n;
  695. } else {
  696. j = 0;
  697. while (j < tab_lcp_len && j < n && tab_lcp[j] == name[j])
  698. j++;
  699. tab_lcp_len = j;
  700. }
  701. tab_count++;
  702. }
  703. return (int)tab_count;
  704. }
  705. /* Replace line[start,end) with ins, keep the tail and redraw. */
  706. static void term_replace(struct term *t, unsigned int start, unsigned int end,
  707. const char *ins)
  708. {
  709. unsigned int inslen = 0, tail, i, old_len = t->line_len;
  710. while (ins[inslen])
  711. inslen++;
  712. tail = old_len - end;
  713. if (start + inslen + tail >= LINE_MAX)
  714. return;
  715. for (i = 0; i < tail; i++)
  716. t->line[start + inslen + i] = t->line[end + i];
  717. for (i = 0; i < inslen; i++)
  718. t->line[start + i] = ins[i];
  719. t->line_len = start + inslen + tail;
  720. t->line[t->line_len] = '\0';
  721. t->line_pos = start + inslen;
  722. redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start,
  723. old_len > t->line_len ? old_len : t->line_len);
  724. }
  725. /* List the ambiguous matches below the line, then redraw prompt + line. */
  726. static void term_tab_ambiguous(struct term *t, int mounts)
  727. {
  728. unsigned int i;
  729. io_putchar('\n');
  730. tab_col = 0;
  731. tab_width = t->screen.cols;
  732. if (mounts) {
  733. for (i = 0; i < vfs_mount_count; i++) {
  734. const char *name = vfs_mounts[i].name;
  735. if (prefix_ci(name, tab_prefix, tab_prefix_len))
  736. tab_print_one(name, 1);
  737. }
  738. } else {
  739. for (i = 0; ; i++) {
  740. struct vfs_dirent d;
  741. int r = vfs_list_dir(tab_scan_path, i, &d);
  742. if (r != 1)
  743. break;
  744. tab_print_cb(d.name, (int)d.is_dir, d.size);
  745. }
  746. }
  747. io_putchar('\n');
  748. term_print_prompt(t, 1);
  749. redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start, t->line_len);
  750. }
  751. /* Command-name completion state: fed from the registry rows and the PATH. */
  752. struct compl_state {
  753. const char *token;
  754. unsigned int tl;
  755. unsigned int count;
  756. unsigned int lcp_len;
  757. char lcp[16];
  758. char best[16];
  759. };
  760. static void compl_add(struct compl_state *st, const char *name)
  761. {
  762. unsigned int nl = 0, j;
  763. while (name[nl])
  764. nl++;
  765. if (nl < st->tl || !prefix_ci(name, st->token, st->tl))
  766. return;
  767. if (st->count == 0) {
  768. for (j = 0; j < nl && j < sizeof(st->lcp) - 1; j++)
  769. st->lcp[j] = name[j];
  770. st->lcp[j] = '\0';
  771. st->lcp_len = j;
  772. for (j = 0; j < nl && j < sizeof(st->best) - 1; j++)
  773. st->best[j] = name[j];
  774. st->best[j] = '\0';
  775. } else {
  776. for (j = 0; j < st->lcp_len && j < nl && st->lcp[j] == name[j]; j++)
  777. ;
  778. st->lcp_len = j;
  779. }
  780. st->count++;
  781. }
  782. static void compl_print_one(const char *name, unsigned int tl, const char *token)
  783. {
  784. char buf[16];
  785. unsigned int nl = 0, j;
  786. while (name[nl])
  787. nl++;
  788. if (nl < tl || !prefix_ci(name, token, tl))
  789. return;
  790. for (j = 0; j < nl && j < 15; j++)
  791. buf[j] = name[j];
  792. buf[j] = '\0';
  793. tab_print_one(buf, 0);
  794. }
  795. static void compl_consider(struct compl_state *st, const char *name, int print)
  796. {
  797. if (print)
  798. compl_print_one(name, st->tl, st->token);
  799. else
  800. compl_add(st, name);
  801. }
  802. /* Visit every command-name candidate: registry rows + files on the PATH. */
  803. static void compl_visit(struct term *t, struct compl_state *st, int print)
  804. {
  805. const char *p;
  806. unsigned int i;
  807. for (i = 0; i < NCMDS; i++)
  808. compl_consider(st, cmds[i].name, print);
  809. p = t->path;
  810. while (*p) {
  811. char dir[VFS_PATH_MAX];
  812. unsigned int n = 0;
  813. while (*p && *p != ':' && n + 1 < sizeof(dir))
  814. dir[n++] = *p++;
  815. dir[n] = '\0';
  816. if (*p == ':')
  817. p++;
  818. if (dir[0] == '\0')
  819. continue;
  820. for (i = 0; ; i++) {
  821. struct vfs_dirent d;
  822. int r = vfs_list_dir(dir, i, &d);
  823. if (r != 1)
  824. break;
  825. if (!d.is_dir)
  826. compl_consider(st, d.name, print);
  827. }
  828. }
  829. }
  830. /* Complete the token at the cursor: command name in the first token, a VFS
  831. * path afterwards. One match completes, several extend to the common
  832. * prefix or are listed below the line. */
  833. static void term_complete(struct term *t)
  834. {
  835. char token[LINE_MAX + 1];
  836. char ins[LINE_MAX + 1];
  837. char dir[VFS_PATH_MAX];
  838. unsigned int start = t->line_pos, end, tl, i;
  839. while (start > 0 && t->line[start - 1] != ' ')
  840. start--;
  841. end = t->line_pos;
  842. while (end < t->line_len && t->line[end] != ' ')
  843. end++;
  844. tl = end - start;
  845. for (i = 0; i < tl; i++)
  846. token[i] = t->line[start + i];
  847. token[tl] = '\0';
  848. /* First token -> command name. */
  849. {
  850. int first = 1;
  851. for (i = 0; i < start; i++)
  852. if (t->line[i] != ' ') {
  853. first = 0;
  854. break;
  855. }
  856. if (first) {
  857. struct compl_state st;
  858. unsigned int j;
  859. st.token = token;
  860. st.tl = tl;
  861. st.count = 0;
  862. st.lcp_len = 0;
  863. st.lcp[0] = '\0';
  864. st.best[0] = '\0';
  865. compl_visit(t, &st, 0);
  866. if (st.count == 0)
  867. return;
  868. if (st.count == 1) {
  869. unsigned int nl = 0;
  870. while (st.best[nl])
  871. nl++;
  872. for (j = 0; j < nl && j < LINE_MAX - 1; j++)
  873. ins[j] = st.best[j];
  874. ins[j] = ' ';
  875. ins[j + 1] = '\0';
  876. term_replace(t, start, end, ins);
  877. return;
  878. }
  879. if (st.lcp_len > tl) {
  880. for (j = 0; j < st.lcp_len; j++)
  881. ins[j] = st.lcp[j];
  882. ins[st.lcp_len] = '\0';
  883. term_replace(t, start, end, ins);
  884. return;
  885. }
  886. io_putchar('\n');
  887. tab_col = 0;
  888. tab_width = t->screen.cols;
  889. compl_visit(t, &st, 1);
  890. io_putchar('\n');
  891. term_print_prompt(t, 1);
  892. redraw_line_clear(t->line, t->line_len, t->line_pos,
  893. t->line_start, t->line_len);
  894. return;
  895. }
  896. }
  897. /* Argument token -> VFS path. */
  898. {
  899. char dirpart[LINE_MAX + 1];
  900. const char *last = 0, *p;
  901. unsigned int dp, n;
  902. int mounts = 0;
  903. for (p = token; *p; p++)
  904. if (*p == '/')
  905. last = p;
  906. if (last) {
  907. dp = (unsigned int)(last - token) + 1;
  908. for (i = 0; i < dp; i++)
  909. dirpart[i] = token[i];
  910. dirpart[dp] = '\0';
  911. p = last + 1;
  912. } else {
  913. dp = 0;
  914. dirpart[0] = '\0';
  915. p = token;
  916. }
  917. {
  918. unsigned int k = 0;
  919. while (*p && k < 15)
  920. tab_prefix[k++] = *p++;
  921. tab_prefix[k] = '\0';
  922. tab_prefix_len = k;
  923. }
  924. if (dirpart[0] == '/' && dirpart[1] == '\0')
  925. mounts = 1;
  926. if (dirpart[0] == '/') {
  927. if (dp >= VFS_PATH_MAX)
  928. return;
  929. for (i = 0; dirpart[i]; i++)
  930. dir[i] = dirpart[i];
  931. dir[i] = '\0';
  932. } else {
  933. if (vfs_join_path(t->cwd, dirpart, dir, sizeof(dir)) != 0)
  934. return;
  935. }
  936. n = mounts ? (unsigned int)tab_scan_mounts()
  937. : (unsigned int)tab_scan_dir(dir);
  938. if (n == 0)
  939. return;
  940. if (dp + tab_lcp_len + 2 > LINE_MAX)
  941. return;
  942. if (n == 1) {
  943. for (i = 0; i < dp; i++)
  944. ins[i] = dirpart[i];
  945. for (i = 0; i < tab_lcp_len; i++)
  946. ins[dp + i] = tab_lcp[i];
  947. ins[dp + tab_lcp_len] = (mounts || tab_single_dir) ? '/' : ' ';
  948. ins[dp + tab_lcp_len + 1] = '\0';
  949. term_replace(t, start, end, ins);
  950. return;
  951. }
  952. if (tab_lcp_len > tab_prefix_len) {
  953. for (i = 0; i < dp; i++)
  954. ins[i] = dirpart[i];
  955. for (i = 0; i < tab_lcp_len; i++)
  956. ins[dp + i] = tab_lcp[i];
  957. ins[dp + tab_lcp_len] = '\0';
  958. term_replace(t, start, end, ins);
  959. return;
  960. }
  961. term_tab_ambiguous(t, mounts);
  962. }
  963. }
  964. /* Push current line to history if non-empty (and not duplicate of newest). */
  965. static void history_push(struct term *t, const char *line, unsigned int line_len)
  966. {
  967. unsigned int i, slot;
  968. int all_spaces = 1;
  969. if (line_len == 0)
  970. return;
  971. for (i = 0; i < line_len; i++) {
  972. if (line[i] != ' ') {
  973. all_spaces = 0;
  974. break;
  975. }
  976. }
  977. if (all_spaces)
  978. return;
  979. if (t->history_count > 0) {
  980. slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  981. for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
  982. if (t->history[slot][i] != line[i])
  983. break;
  984. }
  985. if (i == line_len && t->history[slot][i] == '\0')
  986. return; /* duplicate of newest */
  987. }
  988. slot = t->history_next;
  989. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  990. t->history[slot][i] = line[i];
  991. t->history[slot][i] = '\0';
  992. t->history_next = (t->history_next + 1) % HISTORY_SIZE;
  993. if (t->history_count < HISTORY_SIZE)
  994. t->history_count++;
  995. }
  996. /* Change directory: resolve `arg` (relative to t->cwd, or absolute) and
  997. * validate it is an existing directory. Updates t->cwd on success. */
  998. static void term_cd(struct term *t, const char *arg)
  999. {
  1000. char abs[VFS_PATH_MAX];
  1001. unsigned int size = 0;
  1002. int is_dir = 0;
  1003. int r;
  1004. unsigned int i;
  1005. if (arg[0] == '\0') {
  1006. t->cwd[0] = '/';
  1007. t->cwd[1] = '\0';
  1008. return;
  1009. }
  1010. if (vfs_join_path(t->cwd, arg, abs, sizeof(abs)) != 0) {
  1011. io_putstr("cd: path too long\n");
  1012. return;
  1013. }
  1014. if (abs[0] == '/' && abs[1] == '\0') {
  1015. t->cwd[0] = '/';
  1016. t->cwd[1] = '\0';
  1017. return;
  1018. }
  1019. r = vfs_stat(abs, &size, &is_dir);
  1020. if (r == VFS_EIO) {
  1021. io_putstr("cd: disk error\n");
  1022. return;
  1023. }
  1024. if (r != 0 || !is_dir) {
  1025. io_putstr("cd: no such directory\n");
  1026. return;
  1027. }
  1028. for (i = 0; abs[i] && i < sizeof(t->cwd) - 1; i++)
  1029. t->cwd[i] = abs[i];
  1030. t->cwd[i] = '\0';
  1031. }
  1032. /* Spawn a foreground command process; the caller (term_run's wait loop)
  1033. * waits for it to finish and handles Ctrl+C. */
  1034. static void term_spawn(struct term *t, const char *name, void (*entry)(void))
  1035. {
  1036. int pid = process_create(name, entry);
  1037. if (pid < 0)
  1038. io_putstr("error: too many processes\n");
  1039. else
  1040. t->fg_pid = pid;
  1041. }
  1042. static void term_spawn_arg(struct term *t, const char *name,
  1043. void (*entry)(const char *), const char *arg)
  1044. {
  1045. int pid = process_create_arg(name, entry, arg);
  1046. if (pid < 0)
  1047. io_putstr("error: too many processes\n");
  1048. else
  1049. t->fg_pid = pid;
  1050. }
  1051. /* PATH search: try each directory in t->path for `word`; the first hit is
  1052. * loaded and spawned as a ring-3 process. Returns 1 when the command was
  1053. * found (spawned, or reported as bad), 0 when no directory has it. */
  1054. static int term_path_spawn(struct term *t, const char *word, const char *args)
  1055. {
  1056. const char *p = t->path;
  1057. while (*p) {
  1058. char dir[VFS_PATH_MAX];
  1059. char full[VFS_PATH_MAX + LINE_MAX];
  1060. struct vfs_file f;
  1061. struct vfs_mount *m = 0;
  1062. unsigned int n = 0;
  1063. int r, pid;
  1064. while (*p && *p != ':' && n + 1 < sizeof(dir))
  1065. dir[n++] = *p++;
  1066. dir[n] = '\0';
  1067. if (*p == ':')
  1068. p++;
  1069. if (dir[0] == '\0')
  1070. continue;
  1071. if (vfs_join_path(dir, word, full, sizeof(full)) != 0)
  1072. continue;
  1073. r = vfs_load_file(full, &f, &m);
  1074. if (r != 0)
  1075. continue;
  1076. pid = process_create_user(word, f.data, f.size, args, t->cwd);
  1077. vfs_release_file(m, &f);
  1078. if (pid == -2) {
  1079. io_putstr(word);
  1080. io_putstr(": not an executable\n");
  1081. } else if (pid < 0) {
  1082. io_putstr("error: too many processes\n");
  1083. } else {
  1084. t->fg_pid = pid;
  1085. }
  1086. return 1;
  1087. }
  1088. return 0;
  1089. }
  1090. void term_run(struct term *t)
  1091. {
  1092. t->line_len = 0;
  1093. t->line_pos = 0;
  1094. t->line[0] = '\0';
  1095. t->history_browse = 0;
  1096. t->fg_pid = -1;
  1097. if (t->path[0] == '\0') {
  1098. const char *def = "/ramfs/bin";
  1099. unsigned int i;
  1100. for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
  1101. t->path[i] = def[i];
  1102. t->path[i] = '\0';
  1103. }
  1104. io_clear_screen();
  1105. term_print_prompt(t, 1);
  1106. for (;;) {
  1107. if (t->fg_pid >= 0) {
  1108. /* Waiting on a foreground child: poll keys for Ctrl+C
  1109. * and watch for the child to exit. */
  1110. int k = term_key_pop(t);
  1111. if (k == IO_KEY_CTRL_C) {
  1112. int killed = (process_kill((unsigned int)t->fg_pid) == 0);
  1113. t->fg_pid = -1;
  1114. if (killed)
  1115. io_putstr("^C\n");
  1116. term_print_prompt(t, 0);
  1117. } else if (!process_is_alive((unsigned int)t->fg_pid)) {
  1118. t->fg_pid = -1;
  1119. term_print_prompt(t, 0);
  1120. } else {
  1121. process_sleep_ms(10);
  1122. }
  1123. continue;
  1124. }
  1125. {
  1126. unsigned int flags = term_irq_save_disable();
  1127. int k = term_key_pop(t);
  1128. if (!k) {
  1129. sched_block_on_locked(&t->key_wq);
  1130. term_irq_restore(flags);
  1131. sched_yield();
  1132. continue;
  1133. }
  1134. term_irq_restore(flags);
  1135. if (k == IO_KEY_CTRL_C) {
  1136. /* Ctrl+C at the prompt: discard the line. */
  1137. io_putstr("^C\n");
  1138. t->line_len = 0;
  1139. t->line_pos = 0;
  1140. t->line[0] = '\0';
  1141. term_print_prompt(t, 0);
  1142. } else if (k == IO_KEY_LEFT) {
  1143. if (t->line_pos > 0) {
  1144. t->line_pos--;
  1145. io_set_cursor(t->line_start + t->line_pos);
  1146. }
  1147. } else if (k == IO_KEY_RIGHT) {
  1148. if (t->line_pos < t->line_len) {
  1149. t->line_pos++;
  1150. io_set_cursor(t->line_start + t->line_pos);
  1151. }
  1152. } else if (k == IO_KEY_UP) {
  1153. if (t->history_count > 0) {
  1154. if (t->history_browse < t->history_count)
  1155. t->history_browse++;
  1156. history_load_into_line(t, t->history_browse);
  1157. }
  1158. } else if (k == IO_KEY_DOWN) {
  1159. if (t->history_browse > 0) {
  1160. t->history_browse--;
  1161. if (t->history_browse == 0) {
  1162. unsigned int clear_len = t->line_len;
  1163. t->line_len = 0;
  1164. t->line_pos = 0;
  1165. t->line[0] = '\0';
  1166. redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
  1167. } else {
  1168. history_load_into_line(t, t->history_browse);
  1169. }
  1170. }
  1171. } else if (k == '\b') {
  1172. if (t->line_pos > 0) {
  1173. unsigned int i;
  1174. t->line_pos--;
  1175. for (i = t->line_pos; i < t->line_len; i++)
  1176. t->line[i] = t->line[i + 1];
  1177. t->line_len--;
  1178. t->line[t->line_len] = '\0';
  1179. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
  1180. }
  1181. } else if (k == '\t') {
  1182. term_complete(t);
  1183. } else if (k == '\n') {
  1184. int skip_prompt = 0;
  1185. io_putchar('\n');
  1186. history_push(t, t->line, t->line_len);
  1187. t->history_browse = 0;
  1188. {
  1189. char word[LINE_MAX + 1];
  1190. char args[LINE_MAX + 1];
  1191. const struct kcmd *c;
  1192. term_split_line(t, word, sizeof(word),
  1193. args, sizeof(args));
  1194. c = cmd_lookup(t->line, t->line_len);
  1195. if (c && (c->flags & CMD_BUILTIN)) {
  1196. skip_prompt = term_run_command(t, c, args);
  1197. } else if (word[0] != '\0' &&
  1198. term_path_spawn(t, word, args)) {
  1199. /* spawned from the search path */
  1200. } else if (c) {
  1201. skip_prompt = term_run_command(t, c, args);
  1202. } else if (word[0] != '\0') {
  1203. io_putstr("command not found\n");
  1204. }
  1205. }
  1206. t->line_len = 0;
  1207. t->line_pos = 0;
  1208. t->line[0] = '\0';
  1209. if (!skip_prompt && t->fg_pid < 0) {
  1210. term_print_prompt(t, 0);
  1211. }
  1212. } else {
  1213. unsigned char u = (unsigned char)k;
  1214. if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
  1215. unsigned int i;
  1216. t->history_browse = 0;
  1217. for (i = t->line_len; i > t->line_pos; i--)
  1218. t->line[i] = t->line[i - 1];
  1219. t->line[t->line_pos] = (char)k;
  1220. t->line_len++;
  1221. t->line[t->line_len] = '\0';
  1222. t->line_pos++;
  1223. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
  1224. }
  1225. }
  1226. }
  1227. }
  1228. }
  1229. struct kcmd_ctx {
  1230. char name[16];
  1231. char args[VFS_PATH_MAX];
  1232. };
  1233. static const struct kcmd *kcmd_find(const char *name)
  1234. {
  1235. unsigned int i;
  1236. for (i = 0; i < NCMDS; i++)
  1237. if (cmds[i].name[0] == name[0]) {
  1238. unsigned int j = 0;
  1239. while (cmds[i].name[j] && name[j] && cmds[i].name[j] == name[j])
  1240. j++;
  1241. if (!cmds[i].name[j] && !name[j])
  1242. return &cmds[i];
  1243. }
  1244. return 0;
  1245. }
  1246. static void kcmd_entry(void *arg)
  1247. {
  1248. struct kcmd_ctx *ctx = (struct kcmd_ctx *)arg;
  1249. const struct kcmd *c = kcmd_find(ctx->name);
  1250. if (c) {
  1251. if (c->kind == CK_CUSTOM) {
  1252. struct term *t = (struct term *)mm_alloc_pages(2);
  1253. unsigned int i;
  1254. if (!t) {
  1255. io_putstr("kcmd: out of memory\n");
  1256. } else {
  1257. unsigned char *p = (unsigned char *)t;
  1258. for (i = 0; i < 2 * PAGE_SIZE; i++)
  1259. p[i] = 0;
  1260. t->fg_pid = -1;
  1261. /* Kernel-command output has no cell screen; help uses
  1262. * this width to lay out its columns on the tty. */
  1263. t->screen.cols = 64;
  1264. t->screen.rows = 32;
  1265. for (i = 0; i + 1 < sizeof(t->cwd) &&
  1266. current_proc()->cwd[i]; i++)
  1267. t->cwd[i] = current_proc()->cwd[i];
  1268. t->cwd[i] = '\0';
  1269. {
  1270. const char *def = "/ramfs/bin";
  1271. for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
  1272. t->path[i] = def[i];
  1273. t->path[i] = '\0';
  1274. }
  1275. c->custom(t, ctx->args);
  1276. mm_free_page((unsigned int)t);
  1277. mm_free_page((unsigned int)t + PAGE_SIZE);
  1278. }
  1279. } else if (c->kind == CK_SPAWN) {
  1280. if (c->fixed)
  1281. c->fn_arg(c->fixed);
  1282. else
  1283. c->fn();
  1284. } else if (c->kind == CK_SPAWN_ARG) {
  1285. c->fn_arg(ctx->args);
  1286. }
  1287. } else {
  1288. io_putstr(ctx->name);
  1289. io_putstr(": no such kernel command\n");
  1290. }
  1291. kfree(ctx);
  1292. process_exit();
  1293. }
  1294. int term_kcmd_spawn(const char *name, const char *args, unsigned int tty)
  1295. {
  1296. const struct kcmd *c = kcmd_find(name);
  1297. struct kcmd_ctx *ctx;
  1298. unsigned int i;
  1299. int pid;
  1300. if (!c || ((c->flags & CMD_BUILTIN) && c->custom != cmd_help))
  1301. return -1;
  1302. ctx = (struct kcmd_ctx *)kmalloc(sizeof(*ctx));
  1303. if (!ctx)
  1304. return -1;
  1305. for (i = 0; i + 1 < sizeof(ctx->name) && name[i]; i++)
  1306. ctx->name[i] = name[i];
  1307. ctx->name[i] = '\0';
  1308. for (i = 0; i + 1 < sizeof(ctx->args) && args[i]; i++)
  1309. ctx->args[i] = args[i];
  1310. ctx->args[i] = '\0';
  1311. pid = process_create_ptr("kcmd", kcmd_entry, ctx);
  1312. if (pid < 0) {
  1313. kfree(ctx);
  1314. return -1;
  1315. }
  1316. if (tty)
  1317. process_set_tty((unsigned int)pid, (int)tty);
  1318. return pid;
  1319. }
  1320. void term_entry(void *arg)
  1321. {
  1322. struct term *t = (struct term *)arg;
  1323. process_set_screen(&t->screen);
  1324. term_run(t);
  1325. }