terminal.c 38 KB

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