terminal.c 37 KB

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