terminal.c 35 KB

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