terminal.c 32 KB

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