terminal.c 37 KB

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