ucmd_term.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /* Ring-3 terminal: owns a canvas window, a cell grid and the shell. Text
  2. * commands write bytes to the tty; this process drains and renders them.
  3. * Privileged kernel commands are spawned as kernel processes (term_kcmd). */
  4. #include "ucmd_lib.h"
  5. #include "uwm_lib.h"
  6. #include "io.h"
  7. #include "vfs.h"
  8. #define COLS 64
  9. #define ROWS 32
  10. #define BUF_MAX 256
  11. #define LINE_MAX 128
  12. #define HIST_MAX 16
  13. static struct win_info wi;
  14. static struct usurf surf;
  15. static int tty;
  16. static unsigned short cells[COLS * ROWS];
  17. static unsigned int cx, cy;
  18. static char line[LINE_MAX];
  19. static unsigned int line_len;
  20. static unsigned int line_row, line_col;
  21. static int hist_browse;
  22. static char cwd[VFS_PATH_MAX];
  23. static char path[VFS_PATH_MAX];
  24. static char hist[HIST_MAX][LINE_MAX];
  25. static unsigned int hist_count, hist_next;
  26. static int fg_pid = -1;
  27. static int dirty = 1;
  28. static int cursor_on = 1;
  29. static unsigned int last_blink;
  30. static unsigned short *cell_at(unsigned int x, unsigned int y)
  31. {
  32. return &cells[y * COLS + x];
  33. }
  34. static void cell_put(unsigned int x, unsigned int y, char ch)
  35. {
  36. *cell_at(x, y) = (unsigned short)(0x0A00 | (unsigned char)ch);
  37. }
  38. static void scroll_up(void)
  39. {
  40. unsigned int i;
  41. for (i = 0; i < (ROWS - 1) * COLS; i++)
  42. cells[i] = cells[i + COLS];
  43. for (; i < ROWS * COLS; i++)
  44. cells[i] = 0x0A20;
  45. }
  46. static void term_newline(void)
  47. {
  48. cx = 0;
  49. cy++;
  50. if (cy >= ROWS) {
  51. scroll_up();
  52. cy = ROWS - 1;
  53. }
  54. dirty = 1;
  55. }
  56. static void term_putc(char c)
  57. {
  58. if (c == '\n') {
  59. term_newline();
  60. return;
  61. }
  62. if (c == '\r') {
  63. cx = 0;
  64. dirty = 1;
  65. return;
  66. }
  67. if (c == '\b') {
  68. if (cx > 0)
  69. cx--;
  70. cell_put(cx, cy, ' ');
  71. dirty = 1;
  72. return;
  73. }
  74. if (c == '\t') {
  75. unsigned int n = 8 - (cx % 8);
  76. while (n--)
  77. term_putc(' ');
  78. return;
  79. }
  80. if (c < 0x20 || c > 0x7E)
  81. return;
  82. cell_put(cx, cy, c);
  83. cx++;
  84. if (cx >= COLS)
  85. term_newline();
  86. dirty = 1;
  87. }
  88. static void term_puts(const char *s)
  89. {
  90. while (*s)
  91. term_putc(*s++);
  92. }
  93. static void term_clear(void)
  94. {
  95. unsigned int i;
  96. for (i = 0; i < COLS * ROWS; i++)
  97. cells[i] = 0x0A20;
  98. cx = cy = 0;
  99. dirty = 1;
  100. }
  101. static void cursor_set(unsigned int row, unsigned int col)
  102. {
  103. if (row >= ROWS) row = ROWS - 1;
  104. if (col >= COLS) col = COLS - 1;
  105. cy = row;
  106. cx = col;
  107. dirty = 1;
  108. }
  109. /* Parse the exact "ESC[row;colH" cursor sequence used by io_set_cursor. */
  110. static int esc_cursor(const char *p, unsigned int n)
  111. {
  112. unsigned int row = 0, col = 0, i = 0;
  113. if (n < 5 || p[0] != 0x1B || p[1] != '[')
  114. return 0;
  115. while (i < n && p[i] >= '0' && p[i] <= '9')
  116. row = row * 10 + (unsigned int)(p[i++] - '0');
  117. if (i >= n || p[i++] != ';')
  118. return 0;
  119. while (i < n && p[i] >= '0' && p[i] <= '9')
  120. col = col * 10 + (unsigned int)(p[i++] - '0');
  121. if (i >= n || p[i] != 'H')
  122. return 0;
  123. if (row < 1 || col < 1)
  124. return 0;
  125. cursor_set(row - 1, col - 1);
  126. return 1;
  127. }
  128. static void tty_feed(const char *buf, unsigned int n)
  129. {
  130. unsigned int i = 0;
  131. while (i < n) {
  132. if (buf[i] == 0x1B) {
  133. unsigned int j = i + 1;
  134. char seq[24];
  135. unsigned int sn = 0;
  136. while (j < n && sn + 1 < sizeof(seq)) {
  137. seq[sn++] = buf[j];
  138. if ((buf[j] >= 'A' && buf[j] <= 'Z') ||
  139. (buf[j] >= 'a' && buf[j] <= 'z'))
  140. break;
  141. j++;
  142. }
  143. seq[sn] = '\0';
  144. /* Rebuild with ESC prefix for the helper. */
  145. {
  146. char full[28];
  147. unsigned int m;
  148. full[0] = 0x1B;
  149. for (m = 0; m < sn && m + 1 < sizeof(full); m++)
  150. full[m + 1] = seq[m];
  151. full[sn + 1] = '\0';
  152. if (esc_cursor(full, sn + 1)) {
  153. i = j + 1;
  154. continue;
  155. }
  156. }
  157. if (sn > 1 && seq[sn - 1] == 'J' && seq[1] == '2') {
  158. term_clear();
  159. } else if (sn && seq[sn - 1] == 'H') {
  160. cursor_set(0, 0);
  161. }
  162. i = j < n ? j + 1 : n;
  163. continue;
  164. }
  165. term_putc(buf[i++]);
  166. }
  167. }
  168. static void drain_tty(void)
  169. {
  170. char buf[BUF_MAX];
  171. int n;
  172. while ((n = u_tty_read(tty, buf, sizeof(buf))) > 0) {
  173. tty_feed(buf, (unsigned int)n);
  174. dirty = 1;
  175. }
  176. }
  177. static void render(void)
  178. {
  179. if (!dirty)
  180. return;
  181. usurf_fill(&surf, 0, 0, wi.width, wi.height, 0x000000u);
  182. usurf_cells(&surf, cells, COLS, ROWS, 0, 0, 0, 0, wi.width, wi.height);
  183. if (cursor_on && cx < COLS && cy < ROWS)
  184. usurf_fill(&surf, (int)cx * 8, (int)cy * 10, 2, 10, 0xFFFFFFu);
  185. u_win_present();
  186. dirty = 0;
  187. }
  188. static void line_show(void)
  189. {
  190. unsigned int i, x;
  191. cy = line_row;
  192. cx = line_col;
  193. for (i = 0; i < line_len; i++)
  194. term_putc(line[i]);
  195. for (x = cx; x < COLS; x++)
  196. cell_put(x, cy, ' ');
  197. dirty = 1;
  198. }
  199. static void line_set(const char *s)
  200. {
  201. unsigned int i = 0;
  202. while (s[i] && i + 1 < LINE_MAX) {
  203. line[i] = s[i];
  204. i++;
  205. }
  206. line[i] = '\0';
  207. line_len = i;
  208. line_show();
  209. }
  210. static void history_load(int n)
  211. {
  212. unsigned int idx;
  213. if (n <= 0 || n > (int)hist_count)
  214. return;
  215. idx = (hist_next + HIST_MAX - (unsigned int)n) % HIST_MAX;
  216. line_set(hist[idx]);
  217. }
  218. static void prompt(void)
  219. {
  220. term_putc('[');
  221. term_puts(cwd);
  222. term_puts("] > ");
  223. line_len = 0;
  224. line[0] = '\0';
  225. line_row = cy;
  226. line_col = cx;
  227. hist_browse = 0;
  228. dirty = 1;
  229. }
  230. static void print_num(unsigned int n)
  231. {
  232. char b[12];
  233. unsigned int i = 0;
  234. if (!n) {
  235. term_putc('0');
  236. return;
  237. }
  238. while (n) {
  239. b[i++] = (char)('0' + n % 10);
  240. n /= 10;
  241. }
  242. while (i)
  243. term_putc(b[--i]);
  244. }
  245. static unsigned int parse_num(const char *s)
  246. {
  247. unsigned int n = 0;
  248. while (*s == ' ')
  249. s++;
  250. while (*s >= '0' && *s <= '9')
  251. n = n * 10 + (unsigned int)(*s++ - '0');
  252. return n;
  253. }
  254. static void split_line(char *word, unsigned int wsz, char *args,
  255. unsigned int asz)
  256. {
  257. unsigned int i = 0, n = 0;
  258. while (line[i] == ' ')
  259. i++;
  260. while (line[i] && line[i] != ' ' && n + 1 < wsz)
  261. word[n++] = line[i++];
  262. word[n] = '\0';
  263. while (line[i] == ' ')
  264. i++;
  265. n = 0;
  266. while (line[i] && n + 1 < asz)
  267. args[n++] = line[i++];
  268. while (n && args[n - 1] == ' ')
  269. n--;
  270. args[n] = '\0';
  271. }
  272. static int make_path(const char *dir, const char *word, char *out,
  273. unsigned int max)
  274. {
  275. unsigned int i = 0, j;
  276. if (word[0] == '/') {
  277. for (j = 0; word[j] && j + 1 < max; j++)
  278. out[j] = word[j];
  279. out[j] = '\0';
  280. return 0;
  281. }
  282. while (dir[i] && i + 1 < max)
  283. out[i] = dir[i], i++;
  284. if (i && out[i - 1] != '/' && i + 1 < max)
  285. out[i++] = '/';
  286. for (j = 0; word[j] && i + 1 < max; j++)
  287. out[i++] = word[j];
  288. out[i] = '\0';
  289. return 0;
  290. }
  291. static int try_spawn(const char *word, const char *args, char *used_path,
  292. unsigned int used_size)
  293. {
  294. char p[VFS_PATH_MAX];
  295. int pid;
  296. if (word[0] == '/') {
  297. for (unsigned int i = 0; word[i] && i + 1 < used_size; i++)
  298. used_path[i] = word[i];
  299. return u_spawn(word, args, tty, cwd);
  300. }
  301. /* Try each PATH component. */
  302. {
  303. unsigned int i = 0, start = 0;
  304. while (1) {
  305. unsigned int n = 0;
  306. char dir[VFS_PATH_MAX];
  307. while (path[i] && path[i] != ':') {
  308. if (n + 1 < sizeof(dir))
  309. dir[n++] = path[i];
  310. i++;
  311. }
  312. dir[n] = '\0';
  313. if (dir[0]) {
  314. make_path(dir, word, p, sizeof(p));
  315. pid = u_spawn(p, args, tty, cwd);
  316. if (pid >= 0) {
  317. for (n = 0; p[n] && n + 1 < used_size; n++)
  318. used_path[n] = p[n];
  319. used_path[n] = '\0';
  320. return pid;
  321. }
  322. if (pid == -2)
  323. return -2;
  324. }
  325. if (!path[i])
  326. break;
  327. i++;
  328. start = i;
  329. (void)start;
  330. }
  331. }
  332. return -1;
  333. }
  334. static void run_line(void)
  335. {
  336. char word[LINE_MAX], args[LINE_MAX];
  337. char used[VFS_PATH_MAX];
  338. int pid;
  339. split_line(word, sizeof(word), args, sizeof(args));
  340. if (!word[0]) {
  341. prompt();
  342. return;
  343. }
  344. if (word[0] == 'c' && word[1] == 'd' && !word[2]) {
  345. if (u_chdir(args[0] ? args : "/") != 0)
  346. term_puts("cd: no such directory\n");
  347. u_getcwd(cwd, sizeof(cwd));
  348. prompt();
  349. return;
  350. }
  351. if (word[0] == 'p' && word[1] == 'a' && word[2] == 't' && !word[3]) {
  352. unsigned int i;
  353. if (!args[0]) {
  354. term_puts(path);
  355. term_putc('\n');
  356. } else {
  357. for (i = 0; args[i] && i + 1 < sizeof(path); i++)
  358. path[i] = args[i];
  359. path[i] = '\0';
  360. }
  361. prompt();
  362. return;
  363. }
  364. if (word[0] == 'h' && word[1] == 'e' && word[2] == 'l' &&
  365. word[3] == 'p' && !word[4]) {
  366. pid = u_spawn_kcmd("help", "", tty);
  367. if (pid > 0) {
  368. fg_pid = pid;
  369. return;
  370. }
  371. term_puts("builtins: cd path help clear ps kill\n");
  372. term_puts("commands: files in ");
  373. term_puts(path);
  374. term_putc('\n');
  375. prompt();
  376. return;
  377. }
  378. if (word[0] == 'c' && word[1] == 'l' && word[2] == 'e' &&
  379. word[3] == 'a' && word[4] == 'r' && !word[5]) {
  380. term_clear();
  381. prompt();
  382. return;
  383. }
  384. if (word[0] == 'p' && word[1] == 's' && !word[2]) {
  385. struct sys_procinfo pi;
  386. unsigned int i = 0;
  387. term_puts("PID STATE W CPU NAME\n");
  388. while (u_syscall2(SYS_PROCINFO, (int)i, (int)&pi) == 1) {
  389. print_num(pi.pid);
  390. term_puts(" ");
  391. if (pi.state == 1) term_puts("ready");
  392. else if (pi.state == 2) term_puts("run ");
  393. else if (pi.state == 4) term_puts("sleep");
  394. else if (pi.state == 5) term_puts("block");
  395. else term_puts("? ");
  396. term_puts(" ");
  397. print_num(pi.weight);
  398. term_puts(" ");
  399. if (pi.cpu != 0xFFFFFFFFu)
  400. print_num(pi.cpu);
  401. else
  402. term_putc('-');
  403. term_puts(" ");
  404. term_puts(pi.name);
  405. term_putc('\n');
  406. i++;
  407. }
  408. prompt();
  409. return;
  410. }
  411. if (word[0] == 'k' && word[1] == 'i' && word[2] == 'l' &&
  412. word[3] == 'l' && !word[4]) {
  413. unsigned int pid = parse_num(args);
  414. if (!pid || u_kill(pid) != 0)
  415. term_puts("kill: no such process\n");
  416. prompt();
  417. return;
  418. }
  419. pid = try_spawn(word, args, used, sizeof(used));
  420. if (pid == -2)
  421. term_puts("not an executable\n");
  422. else if (pid < 0) {
  423. pid = u_spawn_kcmd(word, args, tty);
  424. if (pid < 0)
  425. term_puts("command not found\n");
  426. }
  427. if (pid > 0)
  428. fg_pid = pid;
  429. else
  430. prompt();
  431. }
  432. static void submit_line(void)
  433. {
  434. term_putc('\n');
  435. if (line_len) {
  436. unsigned int i;
  437. for (i = 0; line[i] && i < sizeof(hist[hist_next]) - 1; i++)
  438. hist[hist_next][i] = line[i];
  439. hist[hist_next][i] = '\0';
  440. hist_next = (hist_next + 1) % HIST_MAX;
  441. if (hist_count < HIST_MAX)
  442. hist_count++;
  443. }
  444. hist_browse = 0;
  445. run_line();
  446. }
  447. static unsigned int name_len(const char *s, unsigned int max)
  448. {
  449. unsigned int n = 0;
  450. while (n < max && s[n])
  451. n++;
  452. while (n && s[n - 1] == ' ')
  453. n--;
  454. return n;
  455. }
  456. static int prefix_is(const char *word, unsigned int wl, const char *name,
  457. unsigned int nl)
  458. {
  459. unsigned int i;
  460. if (wl > nl)
  461. return 0;
  462. for (i = 0; i < wl; i++)
  463. if (word[i] != name[i])
  464. return 0;
  465. return 1;
  466. }
  467. /* Complete the token at the end of the line. A token after the command word
  468. * (or any token containing '/') is a path; the first token is a command. */
  469. static void complete_line(void)
  470. {
  471. unsigned int start = line_len;
  472. unsigned int tlen, slash = 0, i, n, bl, cl, matches = 0;
  473. char token[LINE_MAX], dir[VFS_PATH_MAX], base[LINE_MAX];
  474. char cand[LINE_MAX];
  475. int has_slash = 0, cand_dir = 0;
  476. while (start > 0 && line[start - 1] != ' ')
  477. start--;
  478. tlen = line_len - start;
  479. if (!tlen)
  480. return;
  481. for (i = 0; i < tlen && i + 1 < sizeof(token); i++)
  482. token[i] = line[start + i];
  483. token[i] = '\0';
  484. for (i = 0; i < tlen; i++)
  485. if (token[i] == '/') {
  486. has_slash = 1;
  487. slash = i;
  488. }
  489. if (has_slash || start > 0) {
  490. /* Path token: split into directory + basename prefix. */
  491. if (!has_slash) {
  492. dir[0] = '.';
  493. dir[1] = '\0';
  494. for (i = 0; i < tlen && i + 1 < sizeof(base); i++)
  495. base[i] = token[i];
  496. base[i] = '\0';
  497. bl = i;
  498. } else if (slash == 0) {
  499. dir[0] = '/';
  500. dir[1] = '\0';
  501. for (i = 0; i + 1 < tlen && i + 1 < sizeof(base); i++)
  502. base[i] = token[i + 1];
  503. base[i] = '\0';
  504. bl = i;
  505. } else {
  506. if (slash >= sizeof(dir))
  507. return;
  508. for (i = 0; i < slash; i++)
  509. dir[i] = token[i];
  510. dir[i] = '\0';
  511. for (i = 0; slash + 1 + i < tlen &&
  512. i + 1 < sizeof(base); i++)
  513. base[i] = token[slash + 1 + i];
  514. base[i] = '\0';
  515. bl = i;
  516. }
  517. if (dir[0] == '/' && dir[1] == '\0') {
  518. struct sys_mount m;
  519. for (i = 0;; i++) {
  520. if (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&m) != 1)
  521. break;
  522. n = name_len(m.name, sizeof(m.name));
  523. if (!prefix_is(base, bl, m.name, n))
  524. continue;
  525. matches++;
  526. if (matches == 1) {
  527. cl = 0;
  528. cand[cl++] = '/';
  529. for (n = 0; m.name[n] && cl + 1 < sizeof(cand); n++)
  530. cand[cl++] = m.name[n];
  531. cand[cl] = '\0';
  532. cand_dir = 1;
  533. }
  534. }
  535. } else {
  536. struct sys_dirent d;
  537. for (i = 0;; i++) {
  538. if (u_syscall3(SYS_LIST_DIR, (int)dir, (int)i,
  539. (int)&d) != 1)
  540. break;
  541. n = name_len(d.name, sizeof(d.name));
  542. if (n == 0 ||
  543. (n == 1 && d.name[0] == '.') ||
  544. (n == 2 && d.name[0] == '.' && d.name[1] == '.'))
  545. continue;
  546. if (!prefix_is(base, bl, d.name, n))
  547. continue;
  548. matches++;
  549. if (matches == 1) {
  550. cl = 0;
  551. if (slash > 0) {
  552. unsigned int k;
  553. for (k = 0; k < slash &&
  554. cl + 1 < sizeof(cand); k++)
  555. cand[cl++] = token[k];
  556. if (cl && cand[cl - 1] != '/' &&
  557. cl + 1 < sizeof(cand))
  558. cand[cl++] = '/';
  559. }
  560. for (n = 0; d.name[n] && n < sizeof(d.name) &&
  561. cl + 1 < sizeof(cand); n++)
  562. cand[cl++] = d.name[n];
  563. cand[cl] = '\0';
  564. cand_dir = d.is_dir ? 1 : 0;
  565. }
  566. }
  567. }
  568. } else {
  569. /* Command-name completion: builtins + /ramfs/bin. */
  570. static const char * const builtins[] = {
  571. "cd", "path", "help", "clear", "ps", "kill"
  572. };
  573. struct sys_dirent d;
  574. for (i = 0; i < sizeof(builtins) / sizeof(builtins[0]); i++) {
  575. n = name_len(builtins[i], 16);
  576. if (!prefix_is(token, tlen, builtins[i], n))
  577. continue;
  578. matches++;
  579. if (matches == 1) {
  580. for (n = 0; builtins[i][n] && n + 1 < sizeof(cand); n++)
  581. cand[n] = builtins[i][n];
  582. cand[n] = '\0';
  583. }
  584. }
  585. for (i = 0;; i++) {
  586. if (u_syscall3(SYS_LIST_DIR, (int)"/ramfs/bin", (int)i,
  587. (int)&d) != 1)
  588. break;
  589. n = name_len(d.name, sizeof(d.name));
  590. if (!prefix_is(token, tlen, d.name, n))
  591. continue;
  592. matches++;
  593. if (matches == 1) {
  594. for (n = 0; d.name[n] && n < sizeof(d.name) &&
  595. n + 1 < sizeof(cand); n++)
  596. cand[n] = d.name[n];
  597. cand[n] = '\0';
  598. }
  599. }
  600. cand_dir = 0;
  601. }
  602. if (matches != 1)
  603. return;
  604. line_len = start;
  605. for (i = 0; cand[i] && line_len + 1 < LINE_MAX; i++)
  606. line[line_len++] = cand[i];
  607. if (cand_dir && line_len + 1 < LINE_MAX)
  608. line[line_len++] = '/';
  609. else if (line_len + 1 < LINE_MAX)
  610. line[line_len++] = ' ';
  611. line[line_len] = '\0';
  612. line_show();
  613. }
  614. static void handle_key(int k)
  615. {
  616. if (k == 0x03) {
  617. if (fg_pid > 0) {
  618. u_kill((unsigned int)fg_pid);
  619. fg_pid = -1;
  620. term_puts("^C\n");
  621. prompt();
  622. } else {
  623. line_len = 0;
  624. line[0] = '\0';
  625. term_puts("^C\n");
  626. prompt();
  627. }
  628. return;
  629. }
  630. if (k == '\n' || k == '\r') {
  631. submit_line();
  632. return;
  633. }
  634. if (k == '\b') {
  635. if (line_len) {
  636. line_len--;
  637. line[line_len] = '\0';
  638. line_show();
  639. }
  640. return;
  641. }
  642. if (k == IO_KEY_UP) {
  643. if ((int)hist_browse < (int)hist_count) {
  644. hist_browse++;
  645. history_load(hist_browse);
  646. }
  647. return;
  648. }
  649. if (k == IO_KEY_DOWN) {
  650. if (hist_browse > 0) {
  651. hist_browse--;
  652. if (hist_browse == 0)
  653. line_set("");
  654. else
  655. history_load(hist_browse);
  656. }
  657. return;
  658. }
  659. if (k == '\t') {
  660. complete_line();
  661. return;
  662. }
  663. if (k < 0x20 || k > 0x7E)
  664. return;
  665. if (line_len + 1 < LINE_MAX) {
  666. hist_browse = 0;
  667. line[line_len++] = (char)k;
  668. line[line_len] = '\0';
  669. term_putc((char)k);
  670. }
  671. }
  672. void _start(const char *arg)
  673. {
  674. unsigned int now;
  675. (void)arg;
  676. tty = u_tty_create();
  677. if (tty <= 0)
  678. u_exit(1);
  679. if (u_win_create(512, 320, "term", &wi) != 0)
  680. u_exit(1);
  681. usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
  682. term_clear();
  683. u_getcwd(cwd, sizeof(cwd));
  684. {
  685. const char *def = "/ramfs/bin";
  686. unsigned int i;
  687. for (i = 0; def[i] && i + 1 < sizeof(path); i++)
  688. path[i] = def[i];
  689. path[i] = '\0';
  690. }
  691. prompt();
  692. render();
  693. for (;;) {
  694. int got = 0;
  695. drain_tty();
  696. if (fg_pid > 0) {
  697. if (!u_alive((unsigned int)fg_pid)) {
  698. fg_pid = -1;
  699. drain_tty();
  700. prompt();
  701. } else {
  702. int k = u_key_poll();
  703. if (k == 0x03)
  704. handle_key(k);
  705. }
  706. } else {
  707. int k = u_win_key_wait(100);
  708. if (k < 0)
  709. u_exit(1);
  710. while (k) {
  711. handle_key(k);
  712. got = 1;
  713. k = u_key_poll();
  714. }
  715. }
  716. now = u_ticks_ms();
  717. if (now - last_blink >= 500) {
  718. last_blink = now;
  719. cursor_on = !cursor_on;
  720. dirty = 1;
  721. }
  722. render();
  723. u_sleep_ms(got ? 1 : 10);
  724. }
  725. }