ucmd_term.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. term_puts("builtins: cd path help clear ps kill\n");
  367. term_puts("commands: files in ");
  368. term_puts(path);
  369. term_putc('\n');
  370. prompt();
  371. return;
  372. }
  373. if (word[0] == 'c' && word[1] == 'l' && word[2] == 'e' &&
  374. word[3] == 'a' && word[4] == 'r' && !word[5]) {
  375. term_clear();
  376. prompt();
  377. return;
  378. }
  379. if (word[0] == 'p' && word[1] == 's' && !word[2]) {
  380. struct sys_procinfo pi;
  381. unsigned int i = 0;
  382. term_puts("PID STATE NAME\n");
  383. while (u_syscall2(SYS_PROCINFO, (int)i, (int)&pi) == 1) {
  384. print_num(pi.pid);
  385. term_puts(" ");
  386. if (pi.state == 1) term_puts("ready");
  387. else if (pi.state == 2) term_puts("run ");
  388. else if (pi.state == 4) term_puts("sleep");
  389. else term_puts("? ");
  390. term_puts(" ");
  391. term_puts(pi.name);
  392. term_putc('\n');
  393. i++;
  394. }
  395. prompt();
  396. return;
  397. }
  398. if (word[0] == 'k' && word[1] == 'i' && word[2] == 'l' &&
  399. word[3] == 'l' && !word[4]) {
  400. unsigned int pid = parse_num(args);
  401. if (!pid || u_kill(pid) != 0)
  402. term_puts("kill: no such process\n");
  403. prompt();
  404. return;
  405. }
  406. pid = try_spawn(word, args, used, sizeof(used));
  407. if (pid == -2)
  408. term_puts("not an executable\n");
  409. else if (pid < 0) {
  410. pid = u_spawn_kcmd(word, args, tty);
  411. if (pid < 0)
  412. term_puts("command not found\n");
  413. }
  414. if (pid > 0)
  415. fg_pid = pid;
  416. else
  417. prompt();
  418. }
  419. static void submit_line(void)
  420. {
  421. term_putc('\n');
  422. if (line_len) {
  423. unsigned int i;
  424. for (i = 0; line[i] && i < sizeof(hist[hist_next]) - 1; i++)
  425. hist[hist_next][i] = line[i];
  426. hist[hist_next][i] = '\0';
  427. hist_next = (hist_next + 1) % HIST_MAX;
  428. if (hist_count < HIST_MAX)
  429. hist_count++;
  430. }
  431. hist_browse = 0;
  432. run_line();
  433. }
  434. static void complete_line(void)
  435. {
  436. static const char * const builtins[] = {
  437. "cd", "path", "help", "clear", "ps", "kill"
  438. };
  439. struct sys_dirent de;
  440. char match[16];
  441. unsigned int matches = 0, i, n;
  442. int r;
  443. if (!line_len)
  444. return;
  445. for (i = 0; i < line_len; i++)
  446. if (line[i] == ' ')
  447. return;
  448. match[0] = '\0';
  449. for (i = 0; i < sizeof(builtins) / sizeof(builtins[0]); i++) {
  450. const char *b = builtins[i];
  451. for (n = 0; n < line_len && b[n] == line[n]; n++)
  452. ;
  453. if (n == line_len && b[n]) {
  454. matches++;
  455. for (n = 0; b[n] && n + 1 < sizeof(match); n++)
  456. match[n] = b[n];
  457. match[n] = '\0';
  458. }
  459. }
  460. for (i = 0;; i++) {
  461. r = u_syscall3(SYS_LIST_DIR, (int)"/ramfs/bin", (int)i, (int)&de);
  462. if (r != 1)
  463. break;
  464. for (n = 0; n < line_len && de.name[n] == line[n]; n++)
  465. ;
  466. if (n == line_len && de.name[n]) {
  467. matches++;
  468. for (n = 0; de.name[n] && n + 1 < sizeof(match); n++)
  469. match[n] = de.name[n];
  470. match[n] = '\0';
  471. }
  472. }
  473. if (matches != 1)
  474. return;
  475. for (n = line_len; match[n] && n + 1 < LINE_MAX; n++) {
  476. line[n] = match[n];
  477. term_putc(match[n]);
  478. }
  479. line_len = n;
  480. line[line_len] = '\0';
  481. if (line_len + 1 < LINE_MAX) {
  482. line[line_len++] = ' ';
  483. line[line_len] = '\0';
  484. term_putc(' ');
  485. }
  486. }
  487. static void handle_key(int k)
  488. {
  489. if (k == 0x03) {
  490. if (fg_pid > 0) {
  491. u_kill((unsigned int)fg_pid);
  492. fg_pid = -1;
  493. term_puts("^C\n");
  494. prompt();
  495. } else {
  496. line_len = 0;
  497. line[0] = '\0';
  498. term_puts("^C\n");
  499. prompt();
  500. }
  501. return;
  502. }
  503. if (k == '\n' || k == '\r') {
  504. submit_line();
  505. return;
  506. }
  507. if (k == '\b') {
  508. if (line_len) {
  509. line_len--;
  510. line[line_len] = '\0';
  511. line_show();
  512. }
  513. return;
  514. }
  515. if (k == IO_KEY_UP) {
  516. if ((int)hist_browse < (int)hist_count) {
  517. hist_browse++;
  518. history_load(hist_browse);
  519. }
  520. return;
  521. }
  522. if (k == IO_KEY_DOWN) {
  523. if (hist_browse > 0) {
  524. hist_browse--;
  525. if (hist_browse == 0)
  526. line_set("");
  527. else
  528. history_load(hist_browse);
  529. }
  530. return;
  531. }
  532. if (k == '\t') {
  533. complete_line();
  534. return;
  535. }
  536. if (k < 0x20 || k > 0x7E)
  537. return;
  538. if (line_len + 1 < LINE_MAX) {
  539. hist_browse = 0;
  540. line[line_len++] = (char)k;
  541. line[line_len] = '\0';
  542. term_putc((char)k);
  543. }
  544. }
  545. void _start(const char *arg)
  546. {
  547. unsigned int now;
  548. (void)arg;
  549. tty = u_tty_create();
  550. if (tty <= 0)
  551. u_exit(1);
  552. if (u_win_create(512, 320, "term", &wi) != 0)
  553. u_exit(1);
  554. usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
  555. term_clear();
  556. u_getcwd(cwd, sizeof(cwd));
  557. {
  558. const char *def = "/ramfs/bin";
  559. unsigned int i;
  560. for (i = 0; def[i] && i + 1 < sizeof(path); i++)
  561. path[i] = def[i];
  562. path[i] = '\0';
  563. }
  564. prompt();
  565. render();
  566. for (;;) {
  567. int got = 0;
  568. drain_tty();
  569. if (fg_pid > 0) {
  570. if (!u_alive((unsigned int)fg_pid)) {
  571. fg_pid = -1;
  572. drain_tty();
  573. prompt();
  574. } else {
  575. int k = u_key_poll();
  576. if (k == 0x03)
  577. handle_key(k);
  578. }
  579. } else {
  580. int k = u_key_poll();
  581. while (k) {
  582. handle_key(k);
  583. got = 1;
  584. k = u_key_poll();
  585. }
  586. }
  587. now = u_ticks_ms();
  588. if (now - last_blink >= 500) {
  589. last_blink = now;
  590. cursor_on = !cursor_on;
  591. dirty = 1;
  592. }
  593. render();
  594. u_sleep_ms(got ? 1 : 10);
  595. }
  596. }