terminal.c 33 KB

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