terminal.c 37 KB

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