terminal.c 32 KB

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