ucmd_term.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 char cwd[VFS_PATH_MAX];
  21. static char path[VFS_PATH_MAX];
  22. static char hist[HIST_MAX][LINE_MAX];
  23. static unsigned int hist_count, hist_next;
  24. static int fg_pid = -1;
  25. static int dirty = 1;
  26. static int cursor_on = 1;
  27. static unsigned int last_blink;
  28. static unsigned short *cell_at(unsigned int x, unsigned int y)
  29. {
  30. return &cells[y * COLS + x];
  31. }
  32. static void cell_put(unsigned int x, unsigned int y, char ch)
  33. {
  34. *cell_at(x, y) = (unsigned short)(0x0A00 | (unsigned char)ch);
  35. }
  36. static void scroll_up(void)
  37. {
  38. unsigned int i;
  39. for (i = 0; i < (ROWS - 1) * COLS; i++)
  40. cells[i] = cells[i + COLS];
  41. for (; i < ROWS * COLS; i++)
  42. cells[i] = 0x0A20;
  43. }
  44. static void term_newline(void)
  45. {
  46. cx = 0;
  47. cy++;
  48. if (cy >= ROWS) {
  49. scroll_up();
  50. cy = ROWS - 1;
  51. }
  52. dirty = 1;
  53. }
  54. static void term_putc(char c)
  55. {
  56. if (c == '\n') {
  57. term_newline();
  58. return;
  59. }
  60. if (c == '\r') {
  61. cx = 0;
  62. dirty = 1;
  63. return;
  64. }
  65. if (c == '\b') {
  66. if (cx > 0)
  67. cx--;
  68. cell_put(cx, cy, ' ');
  69. dirty = 1;
  70. return;
  71. }
  72. if (c == '\t') {
  73. unsigned int n = 8 - (cx % 8);
  74. while (n--)
  75. term_putc(' ');
  76. return;
  77. }
  78. if (c < 0x20 || c > 0x7E)
  79. return;
  80. cell_put(cx, cy, c);
  81. cx++;
  82. if (cx >= COLS)
  83. term_newline();
  84. dirty = 1;
  85. }
  86. static void term_puts(const char *s)
  87. {
  88. while (*s)
  89. term_putc(*s++);
  90. }
  91. static void term_clear(void)
  92. {
  93. unsigned int i;
  94. for (i = 0; i < COLS * ROWS; i++)
  95. cells[i] = 0x0A20;
  96. cx = cy = 0;
  97. dirty = 1;
  98. }
  99. static void cursor_set(unsigned int row, unsigned int col)
  100. {
  101. if (row >= ROWS) row = ROWS - 1;
  102. if (col >= COLS) col = COLS - 1;
  103. cy = row;
  104. cx = col;
  105. dirty = 1;
  106. }
  107. /* Parse the exact "ESC[row;colH" cursor sequence used by io_set_cursor. */
  108. static int esc_cursor(const char *p, unsigned int n)
  109. {
  110. unsigned int row = 0, col = 0, i = 0;
  111. if (n < 5 || p[0] != 0x1B || p[1] != '[')
  112. return 0;
  113. while (i < n && p[i] >= '0' && p[i] <= '9')
  114. row = row * 10 + (unsigned int)(p[i++] - '0');
  115. if (i >= n || p[i++] != ';')
  116. return 0;
  117. while (i < n && p[i] >= '0' && p[i] <= '9')
  118. col = col * 10 + (unsigned int)(p[i++] - '0');
  119. if (i >= n || p[i] != 'H')
  120. return 0;
  121. if (row < 1 || col < 1)
  122. return 0;
  123. cursor_set(row - 1, col - 1);
  124. return 1;
  125. }
  126. static void tty_feed(const char *buf, unsigned int n)
  127. {
  128. unsigned int i = 0;
  129. while (i < n) {
  130. if (buf[i] == 0x1B) {
  131. unsigned int j = i + 1;
  132. char seq[24];
  133. unsigned int sn = 0;
  134. while (j < n && sn + 1 < sizeof(seq)) {
  135. seq[sn++] = buf[j];
  136. if ((buf[j] >= 'A' && buf[j] <= 'Z') ||
  137. (buf[j] >= 'a' && buf[j] <= 'z'))
  138. break;
  139. j++;
  140. }
  141. seq[sn] = '\0';
  142. /* Rebuild with ESC prefix for the helper. */
  143. {
  144. char full[28];
  145. unsigned int m;
  146. full[0] = 0x1B;
  147. for (m = 0; m < sn && m + 1 < sizeof(full); m++)
  148. full[m + 1] = seq[m];
  149. full[sn + 1] = '\0';
  150. if (esc_cursor(full, sn + 1)) {
  151. i = j + 1;
  152. continue;
  153. }
  154. }
  155. if (sn > 1 && seq[sn - 1] == 'J' && seq[1] == '2') {
  156. term_clear();
  157. } else if (sn && seq[sn - 1] == 'H') {
  158. cursor_set(0, 0);
  159. }
  160. i = j < n ? j + 1 : n;
  161. continue;
  162. }
  163. term_putc(buf[i++]);
  164. }
  165. }
  166. static void drain_tty(void)
  167. {
  168. char buf[BUF_MAX];
  169. int n;
  170. while ((n = u_tty_read(tty, buf, sizeof(buf))) > 0) {
  171. tty_feed(buf, (unsigned int)n);
  172. dirty = 1;
  173. }
  174. }
  175. static void render(void)
  176. {
  177. if (!dirty)
  178. return;
  179. usurf_fill(&surf, 0, 0, wi.width, wi.height, 0x000000u);
  180. usurf_cells(&surf, cells, COLS, ROWS, 0, 0, 0, 0, wi.width, wi.height);
  181. if (cursor_on && cx < COLS && cy < ROWS)
  182. usurf_fill(&surf, (int)cx * 8, (int)cy * 10, 2, 10, 0xFFFFFFu);
  183. u_win_present();
  184. dirty = 0;
  185. }
  186. static void prompt(void)
  187. {
  188. term_putc('[');
  189. term_puts(cwd);
  190. term_puts("] > ");
  191. line_len = 0;
  192. line[0] = '\0';
  193. dirty = 1;
  194. }
  195. static void print_num(unsigned int n)
  196. {
  197. char b[12];
  198. unsigned int i = 0;
  199. if (!n) {
  200. term_putc('0');
  201. return;
  202. }
  203. while (n) {
  204. b[i++] = (char)('0' + n % 10);
  205. n /= 10;
  206. }
  207. while (i)
  208. term_putc(b[--i]);
  209. }
  210. static unsigned int parse_num(const char *s)
  211. {
  212. unsigned int n = 0;
  213. while (*s == ' ')
  214. s++;
  215. while (*s >= '0' && *s <= '9')
  216. n = n * 10 + (unsigned int)(*s++ - '0');
  217. return n;
  218. }
  219. static void split_line(char *word, unsigned int wsz, char *args,
  220. unsigned int asz)
  221. {
  222. unsigned int i = 0, n = 0;
  223. while (line[i] == ' ')
  224. i++;
  225. while (line[i] && line[i] != ' ' && n + 1 < wsz)
  226. word[n++] = line[i++];
  227. word[n] = '\0';
  228. while (line[i] == ' ')
  229. i++;
  230. n = 0;
  231. while (line[i] && n + 1 < asz)
  232. args[n++] = line[i++];
  233. while (n && args[n - 1] == ' ')
  234. n--;
  235. args[n] = '\0';
  236. }
  237. static int make_path(const char *dir, const char *word, char *out,
  238. unsigned int max)
  239. {
  240. unsigned int i = 0, j;
  241. if (word[0] == '/') {
  242. for (j = 0; word[j] && j + 1 < max; j++)
  243. out[j] = word[j];
  244. out[j] = '\0';
  245. return 0;
  246. }
  247. while (dir[i] && i + 1 < max)
  248. out[i] = dir[i], i++;
  249. if (i && out[i - 1] != '/' && i + 1 < max)
  250. out[i++] = '/';
  251. for (j = 0; word[j] && i + 1 < max; j++)
  252. out[i++] = word[j];
  253. out[i] = '\0';
  254. return 0;
  255. }
  256. static int try_spawn(const char *word, const char *args, char *used_path,
  257. unsigned int used_size)
  258. {
  259. char p[VFS_PATH_MAX];
  260. int pid;
  261. if (word[0] == '/') {
  262. for (unsigned int i = 0; word[i] && i + 1 < used_size; i++)
  263. used_path[i] = word[i];
  264. return u_spawn(word, args, tty, cwd);
  265. }
  266. /* Try each PATH component. */
  267. {
  268. unsigned int i = 0, start = 0;
  269. while (1) {
  270. unsigned int n = 0;
  271. char dir[VFS_PATH_MAX];
  272. while (path[i] && path[i] != ':') {
  273. if (n + 1 < sizeof(dir))
  274. dir[n++] = path[i];
  275. i++;
  276. }
  277. dir[n] = '\0';
  278. if (dir[0]) {
  279. make_path(dir, word, p, sizeof(p));
  280. pid = u_spawn(p, args, tty, cwd);
  281. if (pid >= 0) {
  282. for (n = 0; p[n] && n + 1 < used_size; n++)
  283. used_path[n] = p[n];
  284. used_path[n] = '\0';
  285. return pid;
  286. }
  287. if (pid == -2)
  288. return -2;
  289. }
  290. if (!path[i])
  291. break;
  292. i++;
  293. start = i;
  294. (void)start;
  295. }
  296. }
  297. return -1;
  298. }
  299. static void run_line(void)
  300. {
  301. char word[LINE_MAX], args[LINE_MAX];
  302. char used[VFS_PATH_MAX];
  303. int pid;
  304. split_line(word, sizeof(word), args, sizeof(args));
  305. if (!word[0]) {
  306. prompt();
  307. return;
  308. }
  309. if (word[0] == 'c' && word[1] == 'd' && !word[2]) {
  310. if (u_chdir(args[0] ? args : "/") != 0)
  311. term_puts("cd: no such directory\n");
  312. u_getcwd(cwd, sizeof(cwd));
  313. prompt();
  314. return;
  315. }
  316. if (word[0] == 'p' && word[1] == 'a' && word[2] == 't' && !word[3]) {
  317. unsigned int i;
  318. if (!args[0]) {
  319. term_puts(path);
  320. term_putc('\n');
  321. } else {
  322. for (i = 0; args[i] && i + 1 < sizeof(path); i++)
  323. path[i] = args[i];
  324. path[i] = '\0';
  325. }
  326. prompt();
  327. return;
  328. }
  329. if (word[0] == 'h' && word[1] == 'e' && word[2] == 'l' &&
  330. word[3] == 'p' && !word[4]) {
  331. term_puts("builtins: cd path help clear ps kill\n");
  332. term_puts("commands: files in ");
  333. term_puts(path);
  334. term_putc('\n');
  335. prompt();
  336. return;
  337. }
  338. if (word[0] == 'c' && word[1] == 'l' && word[2] == 'e' &&
  339. word[3] == 'a' && word[4] == 'r' && !word[5]) {
  340. term_clear();
  341. prompt();
  342. return;
  343. }
  344. if (word[0] == 'p' && word[1] == 's' && !word[2]) {
  345. struct sys_procinfo pi;
  346. unsigned int i = 0;
  347. term_puts("PID STATE NAME\n");
  348. while (u_syscall2(SYS_PROCINFO, (int)i, (int)&pi) == 1) {
  349. print_num(pi.pid);
  350. term_puts(" ");
  351. if (pi.state == 1) term_puts("ready");
  352. else if (pi.state == 2) term_puts("run ");
  353. else if (pi.state == 4) term_puts("sleep");
  354. else term_puts("? ");
  355. term_puts(" ");
  356. term_puts(pi.name);
  357. term_putc('\n');
  358. i++;
  359. }
  360. prompt();
  361. return;
  362. }
  363. if (word[0] == 'k' && word[1] == 'i' && word[2] == 'l' &&
  364. word[3] == 'l' && !word[4]) {
  365. unsigned int pid = parse_num(args);
  366. if (!pid || u_kill(pid) != 0)
  367. term_puts("kill: no such process\n");
  368. prompt();
  369. return;
  370. }
  371. pid = try_spawn(word, args, used, sizeof(used));
  372. if (pid == -2)
  373. term_puts("not an executable\n");
  374. else if (pid < 0) {
  375. pid = u_spawn_kcmd(word, args, tty);
  376. if (pid < 0)
  377. term_puts("command not found\n");
  378. }
  379. if (pid > 0)
  380. fg_pid = pid;
  381. else
  382. prompt();
  383. }
  384. static void submit_line(void)
  385. {
  386. term_putc('\n');
  387. if (line_len) {
  388. unsigned int i;
  389. for (i = 0; line[i] && i < sizeof(hist[hist_next]) - 1; i++)
  390. hist[hist_next][i] = line[i];
  391. hist[hist_next][i] = '\0';
  392. hist_next = (hist_next + 1) % HIST_MAX;
  393. if (hist_count < HIST_MAX)
  394. hist_count++;
  395. }
  396. run_line();
  397. }
  398. static void handle_key(int k)
  399. {
  400. if (k == 0x03) {
  401. if (fg_pid > 0) {
  402. u_kill((unsigned int)fg_pid);
  403. fg_pid = -1;
  404. term_puts("^C\n");
  405. prompt();
  406. } else {
  407. line_len = 0;
  408. line[0] = '\0';
  409. term_puts("^C\n");
  410. prompt();
  411. }
  412. return;
  413. }
  414. if (k == '\n' || k == '\r') {
  415. submit_line();
  416. return;
  417. }
  418. if (k == '\b') {
  419. if (line_len) {
  420. line_len--;
  421. line[line_len] = '\0';
  422. term_putc('\b');
  423. }
  424. return;
  425. }
  426. if (k < 0x20 || k > 0x7E)
  427. return;
  428. if (line_len + 1 < LINE_MAX) {
  429. line[line_len++] = (char)k;
  430. line[line_len] = '\0';
  431. term_putc((char)k);
  432. }
  433. }
  434. void _start(const char *arg)
  435. {
  436. unsigned int now;
  437. (void)arg;
  438. tty = u_tty_create();
  439. if (tty <= 0)
  440. u_exit(1);
  441. if (u_win_create(512, 320, "term", &wi) != 0)
  442. u_exit(1);
  443. usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
  444. term_clear();
  445. u_getcwd(cwd, sizeof(cwd));
  446. {
  447. const char *def = "/ramfs/bin";
  448. unsigned int i;
  449. for (i = 0; def[i] && i + 1 < sizeof(path); i++)
  450. path[i] = def[i];
  451. path[i] = '\0';
  452. }
  453. prompt();
  454. render();
  455. for (;;) {
  456. int got = 0;
  457. drain_tty();
  458. if (fg_pid > 0) {
  459. if (!u_alive((unsigned int)fg_pid)) {
  460. fg_pid = -1;
  461. drain_tty();
  462. prompt();
  463. } else {
  464. int k = u_key_poll();
  465. if (k == 0x03)
  466. handle_key(k);
  467. }
  468. } else {
  469. int k = u_key_poll();
  470. while (k) {
  471. handle_key(k);
  472. got = 1;
  473. k = u_key_poll();
  474. }
  475. }
  476. now = u_ticks_ms();
  477. if (now - last_blink >= 500) {
  478. last_blink = now;
  479. cursor_on = !cursor_on;
  480. dirty = 1;
  481. }
  482. render();
  483. u_sleep_ms(got ? 1 : 10);
  484. }
  485. }