ucmd_term.c 17 KB

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