terminal.c 35 KB

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