terminal.c 39 KB

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