terminal.c 36 KB

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