terminal.c 32 KB

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