terminal.c 34 KB

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