ucmd_term.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /* Ring-3 terminal: owns a canvas window, a scrollback of text rows and the
  2. * shell. Text commands write bytes to the tty; this process drains and
  3. * renders them. Privileged kernel commands are spawned as kernel processes
  4. * (term_kcmd).
  5. *
  6. * The screen is the tail of a ring of display rows, so output that scrolls
  7. * off the top stays available behind a scrollbar. Rows carry a "wrapped"
  8. * flag (the line continued because it did not fit), which lets a window
  9. * resize re-flow the whole log at the new width instead of clearing it. */
  10. #include "ucmd_lib.h"
  11. #include "uwm_lib.h"
  12. #include "io.h"
  13. #include "vfs.h"
  14. #define MAX_COLS 128
  15. #define MAX_ROWS 64
  16. #define BUF_MAX 256
  17. #define LINE_MAX 128
  18. #define HIST_MAX 16
  19. /* Scrollbar on the right edge of the canvas. The text area is narrowed by
  20. * SB_W so the bar never covers a glyph; the bottom SB_BOTTOM pixels stay
  21. * free for the window manager's resize grip. */
  22. #define SB_W 8
  23. #define SB_BOTTOM 16
  24. /* Total row storage (screen + scrollback). Allocated from the per-process
  25. * heap; the fallback keeps a screen-only terminal working if that fails. */
  26. #define SCROLL_ROWS_PREF 512
  27. #define SCROLL_ROWS_MIN 128
  28. #define POS_NONE 0xFFFFFFFFu
  29. /* One display row. `wrapped` marks a row that continues on the next one
  30. * because the logical line did not fit at the width it was written at. */
  31. struct trow {
  32. unsigned short cells[MAX_COLS];
  33. unsigned int len;
  34. unsigned int wrapped;
  35. };
  36. /* A position captured before a reflow and mapped through it. */
  37. struct pos_map {
  38. unsigned int abs; /* absolute row index in the row log */
  39. unsigned int col; /* column within that row */
  40. unsigned int start; /* first row of the containing logical line */
  41. unsigned int off; /* character offset inside that logical line */
  42. unsigned int dst; /* destination row of the logical line start */
  43. };
  44. static struct win_info wi;
  45. static struct usurf surf;
  46. static int tty;
  47. static unsigned short view_cells[MAX_COLS * MAX_ROWS];
  48. static struct trow base_rows[MAX_ROWS];
  49. static struct trow base_scratch[MAX_ROWS];
  50. static struct trow *row_mem = base_rows;
  51. static struct trow *row_scratch = base_scratch;
  52. static unsigned int row_cap = MAX_ROWS;
  53. static unsigned int row_first; /* ring index of logical row 0 */
  54. static unsigned int row_count; /* valid rows (always >= rows) */
  55. static unsigned int view; /* rows scrolled back from the bottom */
  56. static unsigned int cols = 64, rows = 32;
  57. static unsigned int cx, cy;
  58. static char line[LINE_MAX];
  59. static unsigned int line_len;
  60. static unsigned int line_abs, line_col;
  61. static int hist_browse;
  62. static char cwd[VFS_PATH_MAX];
  63. static char path[VFS_PATH_MAX];
  64. static char hist[HIST_MAX][LINE_MAX];
  65. static unsigned int hist_count, hist_next;
  66. static int fg_pid = -1;
  67. static int dirty = 1;
  68. static int cursor_on = 1;
  69. static unsigned int last_blink;
  70. static int sb_drag;
  71. static int sb_grab_ofs;
  72. static int wrap_pending; /* the row is full; the next glyph wraps */
  73. /* ---- Row log ---- */
  74. static struct trow *row_at(unsigned int i)
  75. {
  76. return &row_mem[(row_first + i) % row_cap];
  77. }
  78. static struct trow *dst_row(unsigned int i)
  79. {
  80. return &row_scratch[i % row_cap];
  81. }
  82. static unsigned int origin_row(void)
  83. {
  84. return row_count - rows;
  85. }
  86. static void row_blank(struct trow *r)
  87. {
  88. unsigned int i;
  89. for (i = 0; i < MAX_COLS; i++)
  90. r->cells[i] = 0x0A20;
  91. r->len = 0;
  92. r->wrapped = 0;
  93. }
  94. static void row_add_blank(void)
  95. {
  96. struct trow *r;
  97. if (row_count < row_cap) {
  98. r = &row_mem[(row_first + row_count) % row_cap];
  99. row_count++;
  100. } else {
  101. row_first = (row_first + 1) % row_cap;
  102. r = &row_mem[(row_first + row_count - 1) % row_cap];
  103. }
  104. row_blank(r);
  105. }
  106. /* Append a row at the bottom and scroll the screen one line. When the log is
  107. * not full yet the existing rows keep their place, so the view offset grows
  108. * with the origin to leave what the user is reading on screen. */
  109. static void append_blank_row(void)
  110. {
  111. int was_full = (row_count >= row_cap);
  112. row_add_blank();
  113. if (was_full) {
  114. /* Every logical row moved down one slot; keep the input-line
  115. * anchor pointing at the same row. */
  116. if (line_abs > 0)
  117. line_abs--;
  118. } else if (view) {
  119. view++;
  120. }
  121. }
  122. static void scroll_by(int delta)
  123. {
  124. unsigned int maxs = row_count - rows;
  125. unsigned int nv = view;
  126. if (delta > 0) {
  127. nv += (unsigned int)delta;
  128. } else {
  129. unsigned int d = (unsigned int)(-delta);
  130. nv = (nv > d) ? nv - d : 0;
  131. }
  132. if (nv > maxs)
  133. nv = maxs;
  134. if (nv != view) {
  135. view = nv;
  136. dirty = 1;
  137. }
  138. }
  139. /* ---- Text output ---- */
  140. static void cell_put(unsigned int x, unsigned int y, char ch)
  141. {
  142. struct trow *r;
  143. if (x >= MAX_COLS || y >= rows)
  144. return;
  145. r = row_at(origin_row() + y);
  146. r->cells[x] = (unsigned short)(0x0A00 | (unsigned char)ch);
  147. if (x + 1 > r->len)
  148. r->len = x + 1;
  149. }
  150. static void term_scroll(void)
  151. {
  152. append_blank_row();
  153. cy = rows - 1;
  154. }
  155. static void term_newline(void)
  156. {
  157. row_at(origin_row() + cy)->wrapped = 0;
  158. wrap_pending = 0;
  159. cx = 0;
  160. cy++;
  161. if (cy >= rows)
  162. term_scroll();
  163. dirty = 1;
  164. }
  165. /* The next glyph does not fit: the row continues on the next one, and the
  166. * two rows form one logical line that reflow must re-wrap as a unit. */
  167. static void term_wrap(void)
  168. {
  169. row_at(origin_row() + cy)->wrapped = 1;
  170. wrap_pending = 0;
  171. cx = 0;
  172. cy++;
  173. if (cy >= rows)
  174. term_scroll();
  175. dirty = 1;
  176. }
  177. static void term_putc(char c)
  178. {
  179. if (c == '\n') {
  180. term_newline();
  181. return;
  182. }
  183. if (c == '\r') {
  184. wrap_pending = 0;
  185. cx = 0;
  186. dirty = 1;
  187. return;
  188. }
  189. if (c == '\b') {
  190. wrap_pending = 0;
  191. if (cx > 0)
  192. cx--;
  193. cell_put(cx, cy, ' ');
  194. dirty = 1;
  195. return;
  196. }
  197. if (c == '\t') {
  198. unsigned int n = 8 - (cx % 8);
  199. while (n--)
  200. term_putc(' ');
  201. return;
  202. }
  203. if (c < 0x20 || c > 0x7E)
  204. return;
  205. if (wrap_pending)
  206. term_wrap();
  207. cell_put(cx, cy, c);
  208. cx++;
  209. if (cx >= cols)
  210. wrap_pending = 1; /* stay on this row until the next glyph */
  211. dirty = 1;
  212. }
  213. static void term_puts(const char *s)
  214. {
  215. while (*s)
  216. term_putc(*s++);
  217. }
  218. /* ESC[2J: erase the visible screen in place. The rows stay in the log, so a
  219. * full-screen repaint (top) does not push a frame into the scrollback every
  220. * second, and anything above the screen is still there to scroll back to. */
  221. static void term_erase_screen(void)
  222. {
  223. unsigned int i;
  224. for (i = 0; i < rows; i++)
  225. row_blank(row_at(origin_row() + i));
  226. cx = cy = 0;
  227. wrap_pending = 0;
  228. dirty = 1;
  229. }
  230. /* The `clear` builtin: drop the screen and the whole scrollback. */
  231. static void term_reset(void)
  232. {
  233. row_first = 0;
  234. row_count = 0;
  235. view = 0;
  236. while (row_count < rows)
  237. row_add_blank();
  238. cx = cy = 0;
  239. wrap_pending = 0;
  240. dirty = 1;
  241. }
  242. static void cursor_set(unsigned int row, unsigned int col)
  243. {
  244. if (row >= rows)
  245. row = rows - 1;
  246. if (col >= cols)
  247. col = cols - 1;
  248. cy = row;
  249. cx = col;
  250. wrap_pending = 0;
  251. dirty = 1;
  252. }
  253. /* Parse the exact "ESC[row;colH" cursor sequence used by io_set_cursor. */
  254. static int esc_cursor(const char *p, unsigned int n)
  255. {
  256. unsigned int row = 0, col = 0, i = 0;
  257. if (n < 5 || p[0] != 0x1B || p[1] != '[')
  258. return 0;
  259. i = 2; /* skip "ESC [" */
  260. while (i < n && p[i] >= '0' && p[i] <= '9')
  261. row = row * 10 + (unsigned int)(p[i++] - '0');
  262. if (i >= n || p[i++] != ';')
  263. return 0;
  264. while (i < n && p[i] >= '0' && p[i] <= '9')
  265. col = col * 10 + (unsigned int)(p[i++] - '0');
  266. if (i >= n || p[i] != 'H')
  267. return 0;
  268. if (row < 1 || col < 1)
  269. return 0;
  270. cursor_set(row - 1, col - 1);
  271. return 1;
  272. }
  273. static void tty_feed(const char *buf, unsigned int n)
  274. {
  275. unsigned int i = 0;
  276. while (i < n) {
  277. if (buf[i] == 0x1B) {
  278. unsigned int j = i + 1;
  279. char seq[24];
  280. unsigned int sn = 0;
  281. while (j < n && sn + 1 < sizeof(seq)) {
  282. seq[sn++] = buf[j];
  283. if ((buf[j] >= 'A' && buf[j] <= 'Z') ||
  284. (buf[j] >= 'a' && buf[j] <= 'z'))
  285. break;
  286. j++;
  287. }
  288. seq[sn] = '\0';
  289. /* Rebuild with ESC prefix for the helper. */
  290. {
  291. char full[28];
  292. unsigned int m;
  293. full[0] = 0x1B;
  294. for (m = 0; m < sn && m + 1 < sizeof(full); m++)
  295. full[m + 1] = seq[m];
  296. full[sn + 1] = '\0';
  297. if (esc_cursor(full, sn + 1)) {
  298. i = j + 1;
  299. continue;
  300. }
  301. }
  302. if (sn > 1 && seq[sn - 1] == 'J' && seq[1] == '2') {
  303. term_erase_screen();
  304. } else if (sn && seq[sn - 1] == 'H') {
  305. cursor_set(0, 0);
  306. }
  307. i = j < n ? j + 1 : n;
  308. continue;
  309. }
  310. term_putc(buf[i++]);
  311. }
  312. }
  313. static void drain_tty(void)
  314. {
  315. char buf[BUF_MAX];
  316. int n;
  317. while ((n = u_tty_read(tty, buf, sizeof(buf))) > 0) {
  318. tty_feed(buf, (unsigned int)n);
  319. dirty = 1;
  320. }
  321. }
  322. /* ---- Scrollbar ---- */
  323. static void sb_metrics(unsigned int *track_h, unsigned int *thumb_h,
  324. unsigned int *thumb_y)
  325. {
  326. unsigned int total = row_count ? row_count : 1;
  327. unsigned int t = (wi.height > SB_BOTTOM) ? wi.height - SB_BOTTOM
  328. : wi.height;
  329. unsigned int maxs = row_count - rows;
  330. unsigned int h = (rows * t) / total;
  331. if (h < 12)
  332. h = 12;
  333. if (h > t)
  334. h = t;
  335. if (maxs == 0)
  336. *thumb_y = t - h;
  337. else
  338. *thumb_y = (t - h) * (maxs - view) / maxs;
  339. *track_h = t;
  340. *thumb_h = h;
  341. }
  342. static void draw_scrollbar(void)
  343. {
  344. unsigned int track_h, thumb_h, thumb_y, color;
  345. unsigned int x = wi.width - SB_W;
  346. sb_metrics(&track_h, &thumb_h, &thumb_y);
  347. usurf_fill(&surf, (int)x, 0, SB_W, track_h, 0x1C1C24u);
  348. if (sb_drag)
  349. color = 0xD0D0E0u;
  350. else if (view)
  351. color = 0xB0A050u;
  352. else
  353. color = 0x808090u;
  354. usurf_fill(&surf, (int)x, (int)thumb_y, SB_W, thumb_h, color);
  355. }
  356. static void sb_set_from_y(int y)
  357. {
  358. unsigned int track_h, thumb_h, thumb_y;
  359. unsigned int maxs = row_count - rows;
  360. unsigned int nv;
  361. int ty = y - sb_grab_ofs;
  362. sb_metrics(&track_h, &thumb_h, &thumb_y);
  363. (void)thumb_y;
  364. if (ty < 0)
  365. ty = 0;
  366. if (maxs == 0 || track_h <= thumb_h) {
  367. nv = 0;
  368. } else {
  369. if ((unsigned int)ty > track_h - thumb_h)
  370. ty = (int)(track_h - thumb_h);
  371. nv = maxs - (unsigned int)ty * maxs / (track_h - thumb_h);
  372. }
  373. if (nv != view) {
  374. view = nv;
  375. dirty = 1;
  376. }
  377. }
  378. static void handle_pointer(void)
  379. {
  380. struct win_event e;
  381. struct win_pointer p;
  382. while (u_win_event(&e) == 1) {
  383. if (e.type == WIN_EV_POINTER_DOWN) {
  384. if (e.x >= (int)(wi.width - SB_W) && e.y >= 0 &&
  385. e.y < (int)wi.height) {
  386. unsigned int track_h, thumb_h, thumb_y;
  387. sb_metrics(&track_h, &thumb_h, &thumb_y);
  388. (void)track_h;
  389. if ((unsigned int)e.y >= thumb_y &&
  390. (unsigned int)e.y < thumb_y + thumb_h) {
  391. sb_drag = 1;
  392. sb_grab_ofs = e.y - (int)thumb_y;
  393. } else if ((unsigned int)e.y < thumb_y) {
  394. scroll_by((int)rows - 1);
  395. } else {
  396. scroll_by(-((int)rows - 1));
  397. }
  398. }
  399. } else if (e.type == WIN_EV_POINTER_UP) {
  400. if (sb_drag) {
  401. sb_drag = 0;
  402. dirty = 1;
  403. }
  404. }
  405. }
  406. if (sb_drag && u_win_pointer(&p) == 0) {
  407. if (!(p.buttons & 1)) {
  408. sb_drag = 0;
  409. dirty = 1;
  410. } else {
  411. sb_set_from_y(p.y);
  412. }
  413. }
  414. }
  415. /* ---- Rendering ---- */
  416. static void render(void)
  417. {
  418. unsigned int x, y, maxs, start;
  419. if (!dirty)
  420. return;
  421. maxs = row_count - rows;
  422. if (view > maxs)
  423. view = maxs;
  424. start = origin_row() - view;
  425. for (y = 0; y < rows; y++) {
  426. unsigned int idx = start + y;
  427. unsigned short *dst = &view_cells[y * cols];
  428. if (idx < row_count) {
  429. struct trow *r = row_at(idx);
  430. unsigned int n = r->len;
  431. if (n > cols)
  432. n = cols;
  433. for (x = 0; x < n; x++)
  434. dst[x] = r->cells[x];
  435. for (; x < cols; x++)
  436. dst[x] = 0x0A20;
  437. } else {
  438. for (x = 0; x < cols; x++)
  439. dst[x] = 0x0A20;
  440. }
  441. }
  442. usurf_fill(&surf, 0, 0, wi.width, wi.height, 0x000000u);
  443. usurf_cells(&surf, view_cells, cols, rows, 0, 0, 0, 0,
  444. wi.width, wi.height);
  445. if (view == 0 && cursor_on && cy < rows) {
  446. unsigned int ccx = (cx < cols) ? cx : (cols ? cols - 1 : 0);
  447. usurf_fill(&surf, (int)ccx * 8, (int)cy * 16, 2, 16,
  448. 0xFFFFFFu);
  449. }
  450. draw_scrollbar();
  451. u_win_present();
  452. dirty = 0;
  453. }
  454. static void line_show(void)
  455. {
  456. unsigned int i, x, base, k;
  457. struct trow *r;
  458. if (view) {
  459. view = 0;
  460. dirty = 1;
  461. }
  462. base = line_abs;
  463. if (base < origin_row())
  464. base = origin_row();
  465. if (base >= row_count)
  466. base = row_count - 1;
  467. line_abs = base;
  468. r = row_at(base);
  469. if (r->len > line_col)
  470. r->len = line_col;
  471. r->wrapped = 0;
  472. /* The input line is the tail of the log: rows after its start are
  473. * leftovers of the previous, possibly longer, version of the line. */
  474. for (k = 1; base + k < row_count; k++)
  475. row_blank(row_at(base + k));
  476. cx = line_col;
  477. cy = base - origin_row();
  478. wrap_pending = 0;
  479. if (cx >= cols)
  480. term_wrap(); /* the prompt filled the row exactly */
  481. for (i = 0; i < line_len; i++)
  482. term_putc(line[i]);
  483. for (x = cx; x < cols; x++)
  484. cell_put(x, cy, ' ');
  485. dirty = 1;
  486. }
  487. /* ---- Reflow ---- */
  488. /* Absolute row index of `idx` and the character offset of (idx, col) in the
  489. * logical line that contains it. */
  490. static void pos_capture(struct pos_map *m, unsigned int idx, unsigned int col)
  491. {
  492. unsigned int ls, off = 0, j;
  493. if (idx >= row_count)
  494. idx = row_count - 1;
  495. if (col > MAX_COLS)
  496. col = MAX_COLS;
  497. ls = idx;
  498. while (ls > 0 && row_at(ls - 1)->wrapped)
  499. ls--;
  500. for (j = ls; j < idx; j++) {
  501. unsigned int l = row_at(j)->len;
  502. if (l > MAX_COLS)
  503. l = MAX_COLS;
  504. off += l;
  505. }
  506. {
  507. unsigned int l = row_at(idx)->len;
  508. if (col > l)
  509. col = l;
  510. off += col;
  511. }
  512. m->abs = idx;
  513. m->col = col;
  514. m->start = ls;
  515. m->off = off;
  516. m->dst = POS_NONE;
  517. }
  518. /* Re-wrap every logical line at `new_cols` into the scratch ring, then copy
  519. * the retained tail back. The two maps are moved through the same rebuild so
  520. * the cursor and the shell's input line keep pointing at the same text. */
  521. static void reflow(unsigned int new_cols, struct pos_map *cur,
  522. struct pos_map *lin)
  523. {
  524. unsigned int i = 0, out_total = 0, keep, drop, k;
  525. while (i < row_count) {
  526. unsigned int ls = i, j = i, len = 0;
  527. unsigned int out_start = out_total;
  528. while (1) {
  529. struct trow *r = row_at(j);
  530. unsigned int l = r->len;
  531. if (l > MAX_COLS)
  532. l = MAX_COLS;
  533. len += l;
  534. j++;
  535. if (!r->wrapped || j >= row_count)
  536. break;
  537. }
  538. if (ls == cur->start)
  539. cur->dst = out_start;
  540. if (ls == lin->start)
  541. lin->dst = out_start;
  542. if (len == 0) {
  543. row_blank(dst_row(out_total++));
  544. } else {
  545. unsigned int p = 0, srow = ls, sc = 0;
  546. while (p < len) {
  547. struct trow *d = dst_row(out_total++);
  548. unsigned int n = len - p;
  549. unsigned int q = 0;
  550. if (n > new_cols)
  551. n = new_cols;
  552. row_blank(d);
  553. while (q < n) {
  554. struct trow *s = row_at(srow);
  555. unsigned int sl = s->len;
  556. if (sl > MAX_COLS)
  557. sl = MAX_COLS;
  558. if (sc >= sl) {
  559. srow++;
  560. sc = 0;
  561. continue;
  562. }
  563. d->cells[q++] = s->cells[sc++];
  564. p++;
  565. }
  566. d->len = n;
  567. d->wrapped = (p < len);
  568. }
  569. }
  570. i = j;
  571. }
  572. keep = (out_total < row_cap) ? out_total : row_cap;
  573. drop = out_total - keep;
  574. for (k = 0; k < keep; k++) {
  575. struct trow *s = dst_row(drop + k);
  576. struct trow *d = &row_mem[k];
  577. unsigned int c;
  578. d->len = s->len;
  579. d->wrapped = s->wrapped;
  580. for (c = 0; c < MAX_COLS; c++)
  581. d->cells[c] = s->cells[c];
  582. }
  583. row_first = 0;
  584. row_count = keep;
  585. if (cur->dst != POS_NONE) {
  586. unsigned int a = cur->dst + cur->off / new_cols;
  587. cur->abs = (a >= drop) ? a - drop : 0;
  588. cur->col = cur->off % new_cols;
  589. }
  590. if (lin->dst != POS_NONE) {
  591. unsigned int a = lin->dst + lin->off / new_cols;
  592. lin->abs = (a >= drop) ? a - drop : 0;
  593. lin->col = lin->off % new_cols;
  594. }
  595. }
  596. static void finish_resize(unsigned int new_rows, struct pos_map *cur,
  597. struct pos_map *lin)
  598. {
  599. unsigned int origin;
  600. /* Blank rows below the last written one are only padding. Dropping
  601. * them keeps the cursor on screen when the window gets shorter;
  602. * otherwise the tail-anchored screen would scroll the prompt away. */
  603. while (row_count > 0) {
  604. struct trow *r = row_at(row_count - 1);
  605. if (r->len != 0 || r->wrapped)
  606. break;
  607. row_count--;
  608. }
  609. rows = new_rows;
  610. while (row_count < rows)
  611. row_add_blank();
  612. origin = row_count - rows;
  613. if (view > origin)
  614. view = origin;
  615. if (cur->abs < origin)
  616. cur->abs = origin;
  617. if (cur->abs >= row_count) {
  618. row_add_blank();
  619. cur->abs = row_count - 1;
  620. }
  621. origin = row_count - rows;
  622. cy = cur->abs - origin;
  623. cx = (cur->col < cols) ? cur->col : cols - 1;
  624. if (lin->abs < origin)
  625. lin->abs = origin;
  626. if (lin->abs >= row_count)
  627. lin->abs = row_count - 1;
  628. line_abs = lin->abs;
  629. line_col = (lin->col < cols) ? lin->col : cols - 1;
  630. dirty = 1;
  631. }
  632. static void handle_resize(void)
  633. {
  634. struct win_info cur;
  635. struct pos_map cpos, lpos;
  636. unsigned int new_cols, new_rows;
  637. if (u_win_info(&cur) != 0)
  638. return;
  639. if (cur.width == wi.width && cur.height == wi.height)
  640. return;
  641. if (u_win_ack_resize() != 0)
  642. return;
  643. new_cols = (cur.width > SB_W) ? (cur.width - SB_W) / 8 : 1;
  644. if (new_cols < 1)
  645. new_cols = 1;
  646. if (new_cols > MAX_COLS)
  647. new_cols = MAX_COLS;
  648. new_rows = cur.height / 16;
  649. if (new_rows < 1)
  650. new_rows = 1;
  651. if (new_rows > MAX_ROWS)
  652. new_rows = MAX_ROWS;
  653. if (new_rows > row_cap)
  654. new_rows = row_cap;
  655. pos_capture(&cpos, origin_row() + cy, cx);
  656. pos_capture(&lpos, line_abs, line_col);
  657. wi = cur;
  658. usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
  659. if (new_cols != cols) {
  660. reflow(new_cols, &cpos, &lpos);
  661. cols = new_cols;
  662. }
  663. finish_resize(new_rows, &cpos, &lpos);
  664. }
  665. static void line_set(const char *s)
  666. {
  667. unsigned int i = 0;
  668. while (s[i] && i + 1 < LINE_MAX) {
  669. line[i] = s[i];
  670. i++;
  671. }
  672. line[i] = '\0';
  673. line_len = i;
  674. line_show();
  675. }
  676. static void history_load(int n)
  677. {
  678. unsigned int idx;
  679. if (n <= 0 || n > (int)hist_count)
  680. return;
  681. idx = (hist_next + HIST_MAX - (unsigned int)n) % HIST_MAX;
  682. line_set(hist[idx]);
  683. }
  684. static void prompt(void)
  685. {
  686. term_putc('[');
  687. term_puts(cwd);
  688. term_puts("] > ");
  689. line_len = 0;
  690. line[0] = '\0';
  691. line_abs = origin_row() + cy;
  692. line_col = cx;
  693. hist_browse = 0;
  694. dirty = 1;
  695. }
  696. static void print_num(unsigned int n)
  697. {
  698. char b[12];
  699. unsigned int i = 0;
  700. if (!n) {
  701. term_putc('0');
  702. return;
  703. }
  704. while (n) {
  705. b[i++] = (char)('0' + n % 10);
  706. n /= 10;
  707. }
  708. while (i)
  709. term_putc(b[--i]);
  710. }
  711. static unsigned int parse_num(const char *s)
  712. {
  713. unsigned int n = 0;
  714. while (*s == ' ')
  715. s++;
  716. while (*s >= '0' && *s <= '9')
  717. n = n * 10 + (unsigned int)(*s++ - '0');
  718. return n;
  719. }
  720. static void split_line(char *word, unsigned int wsz, char *args,
  721. unsigned int asz)
  722. {
  723. unsigned int i = 0, n = 0;
  724. while (line[i] == ' ')
  725. i++;
  726. while (line[i] && line[i] != ' ' && n + 1 < wsz)
  727. word[n++] = line[i++];
  728. word[n] = '\0';
  729. while (line[i] == ' ')
  730. i++;
  731. n = 0;
  732. while (line[i] && n + 1 < asz)
  733. args[n++] = line[i++];
  734. while (n && args[n - 1] == ' ')
  735. n--;
  736. args[n] = '\0';
  737. }
  738. static int make_path(const char *dir, const char *word, char *out,
  739. unsigned int max)
  740. {
  741. unsigned int i = 0, j;
  742. if (word[0] == '/') {
  743. for (j = 0; word[j] && j + 1 < max; j++)
  744. out[j] = word[j];
  745. out[j] = '\0';
  746. return 0;
  747. }
  748. while (dir[i] && i + 1 < max)
  749. out[i] = dir[i], i++;
  750. if (i && out[i - 1] != '/' && i + 1 < max)
  751. out[i++] = '/';
  752. for (j = 0; word[j] && i + 1 < max; j++)
  753. out[i++] = word[j];
  754. out[i] = '\0';
  755. return 0;
  756. }
  757. static int try_spawn(const char *word, const char *args, char *used_path,
  758. unsigned int used_size)
  759. {
  760. char p[VFS_PATH_MAX];
  761. int pid;
  762. if (word[0] == '/') {
  763. for (unsigned int i = 0; word[i] && i + 1 < used_size; i++)
  764. used_path[i] = word[i];
  765. return u_spawn(word, args, tty, cwd);
  766. }
  767. /* Try each PATH component. */
  768. {
  769. unsigned int i = 0, start = 0;
  770. while (1) {
  771. unsigned int n = 0;
  772. char dir[VFS_PATH_MAX];
  773. while (path[i] && path[i] != ':') {
  774. if (n + 1 < sizeof(dir))
  775. dir[n++] = path[i];
  776. i++;
  777. }
  778. dir[n] = '\0';
  779. if (dir[0]) {
  780. make_path(dir, word, p, sizeof(p));
  781. pid = u_spawn(p, args, tty, cwd);
  782. if (pid >= 0) {
  783. for (n = 0; p[n] && n + 1 < used_size; n++)
  784. used_path[n] = p[n];
  785. used_path[n] = '\0';
  786. return pid;
  787. }
  788. if (pid == -2)
  789. return -2;
  790. }
  791. if (!path[i])
  792. break;
  793. i++;
  794. start = i;
  795. (void)start;
  796. }
  797. }
  798. return -1;
  799. }
  800. static void run_line(void)
  801. {
  802. char word[LINE_MAX], args[LINE_MAX];
  803. char used[VFS_PATH_MAX];
  804. int pid;
  805. split_line(word, sizeof(word), args, sizeof(args));
  806. if (!word[0]) {
  807. prompt();
  808. return;
  809. }
  810. if (word[0] == 'c' && word[1] == 'd' && !word[2]) {
  811. if (u_chdir(args[0] ? args : "/") != 0)
  812. term_puts("cd: no such directory\n");
  813. u_getcwd(cwd, sizeof(cwd));
  814. prompt();
  815. return;
  816. }
  817. if (word[0] == 'p' && word[1] == 'a' && word[2] == 't' && !word[3]) {
  818. unsigned int i;
  819. if (!args[0]) {
  820. term_puts(path);
  821. term_putc('\n');
  822. } else {
  823. for (i = 0; args[i] && i + 1 < sizeof(path); i++)
  824. path[i] = args[i];
  825. path[i] = '\0';
  826. }
  827. prompt();
  828. return;
  829. }
  830. if (word[0] == 'h' && word[1] == 'e' && word[2] == 'l' &&
  831. word[3] == 'p' && !word[4]) {
  832. pid = u_spawn_kcmd("help", "", tty);
  833. if (pid > 0) {
  834. fg_pid = pid;
  835. return;
  836. }
  837. term_puts("builtins: cd path help clear ps kill\n");
  838. term_puts("commands: files in ");
  839. term_puts(path);
  840. term_putc('\n');
  841. prompt();
  842. return;
  843. }
  844. if (word[0] == 'c' && word[1] == 'l' && word[2] == 'e' &&
  845. word[3] == 'a' && word[4] == 'r' && !word[5]) {
  846. term_reset();
  847. prompt();
  848. return;
  849. }
  850. if (word[0] == 'p' && word[1] == 's' && !word[2]) {
  851. struct sys_procinfo pi;
  852. unsigned int i = 0;
  853. term_puts("PID STATE W CPU NAME\n");
  854. while (u_syscall2(SYS_PROCINFO, (int)i, (int)&pi) == 1) {
  855. print_num(pi.pid);
  856. term_puts(" ");
  857. if (pi.state == 1) term_puts("ready");
  858. else if (pi.state == 2) term_puts("run ");
  859. else if (pi.state == 4) term_puts("sleep");
  860. else if (pi.state == 5) term_puts("block");
  861. else term_puts("? ");
  862. term_puts(" ");
  863. print_num(pi.weight);
  864. term_puts(" ");
  865. if (pi.cpu != 0xFFFFFFFFu)
  866. print_num(pi.cpu);
  867. else
  868. term_putc('-');
  869. term_puts(" ");
  870. term_puts(pi.name);
  871. term_putc('\n');
  872. i++;
  873. }
  874. prompt();
  875. return;
  876. }
  877. if (word[0] == 'k' && word[1] == 'i' && word[2] == 'l' &&
  878. word[3] == 'l' && !word[4]) {
  879. unsigned int pid = parse_num(args);
  880. if (!pid || u_kill(pid) != 0)
  881. term_puts("kill: no such process\n");
  882. prompt();
  883. return;
  884. }
  885. pid = try_spawn(word, args, used, sizeof(used));
  886. if (pid == -2)
  887. term_puts("not an executable\n");
  888. else if (pid < 0) {
  889. pid = u_spawn_kcmd(word, args, tty);
  890. if (pid < 0)
  891. term_puts("command not found\n");
  892. }
  893. if (pid > 0)
  894. fg_pid = pid;
  895. else
  896. prompt();
  897. }
  898. static void submit_line(void)
  899. {
  900. term_putc('\n');
  901. if (line_len) {
  902. unsigned int i;
  903. for (i = 0; line[i] && i < sizeof(hist[hist_next]) - 1; i++)
  904. hist[hist_next][i] = line[i];
  905. hist[hist_next][i] = '\0';
  906. hist_next = (hist_next + 1) % HIST_MAX;
  907. if (hist_count < HIST_MAX)
  908. hist_count++;
  909. }
  910. hist_browse = 0;
  911. run_line();
  912. }
  913. static unsigned int name_len(const char *s, unsigned int max)
  914. {
  915. unsigned int n = 0;
  916. while (n < max && s[n])
  917. n++;
  918. while (n && s[n - 1] == ' ')
  919. n--;
  920. return n;
  921. }
  922. static int prefix_is(const char *word, unsigned int wl, const char *name,
  923. unsigned int nl)
  924. {
  925. unsigned int i;
  926. if (wl > nl)
  927. return 0;
  928. for (i = 0; i < wl; i++)
  929. if (word[i] != name[i])
  930. return 0;
  931. return 1;
  932. }
  933. /* Complete the token at the end of the line. A token after the command word
  934. * (or any token containing '/') is a path; the first token is a command. */
  935. static void complete_line(void)
  936. {
  937. unsigned int start = line_len;
  938. unsigned int tlen, slash = 0, i, n, bl, cl, matches = 0;
  939. char token[LINE_MAX], dir[VFS_PATH_MAX], base[LINE_MAX];
  940. char cand[LINE_MAX];
  941. int has_slash = 0, cand_dir = 0;
  942. while (start > 0 && line[start - 1] != ' ')
  943. start--;
  944. tlen = line_len - start;
  945. if (!tlen)
  946. return;
  947. for (i = 0; i < tlen && i + 1 < sizeof(token); i++)
  948. token[i] = line[start + i];
  949. token[i] = '\0';
  950. for (i = 0; i < tlen; i++)
  951. if (token[i] == '/') {
  952. has_slash = 1;
  953. slash = i;
  954. }
  955. if (has_slash || start > 0) {
  956. /* Path token: split into directory + basename prefix. */
  957. if (!has_slash) {
  958. dir[0] = '.';
  959. dir[1] = '\0';
  960. for (i = 0; i < tlen && i + 1 < sizeof(base); i++)
  961. base[i] = token[i];
  962. base[i] = '\0';
  963. bl = i;
  964. } else if (slash == 0) {
  965. dir[0] = '/';
  966. dir[1] = '\0';
  967. for (i = 0; i + 1 < tlen && i + 1 < sizeof(base); i++)
  968. base[i] = token[i + 1];
  969. base[i] = '\0';
  970. bl = i;
  971. } else {
  972. if (slash >= sizeof(dir))
  973. return;
  974. for (i = 0; i < slash; i++)
  975. dir[i] = token[i];
  976. dir[i] = '\0';
  977. for (i = 0; slash + 1 + i < tlen &&
  978. i + 1 < sizeof(base); i++)
  979. base[i] = token[slash + 1 + i];
  980. base[i] = '\0';
  981. bl = i;
  982. }
  983. if (dir[0] == '/' && dir[1] == '\0') {
  984. struct sys_mount m;
  985. for (i = 0;; i++) {
  986. if (u_syscall2(SYS_LIST_MOUNTS, (int)i,
  987. (int)&m) != 1)
  988. break;
  989. n = name_len(m.name, sizeof(m.name));
  990. if (!prefix_is(base, bl, m.name, n))
  991. continue;
  992. matches++;
  993. if (matches == 1) {
  994. cl = 0;
  995. cand[cl++] = '/';
  996. for (n = 0; m.name[n] && cl + 1 < sizeof(cand); n++)
  997. cand[cl++] = m.name[n];
  998. cand[cl] = '\0';
  999. cand_dir = 1;
  1000. }
  1001. }
  1002. } else {
  1003. struct sys_dirent d;
  1004. for (i = 0;; i++) {
  1005. char disp[64];
  1006. unsigned int dl;
  1007. if (u_syscall3(SYS_LIST_DIR, (int)dir, (int)i,
  1008. (int)&d) != 1)
  1009. break;
  1010. if (d.raw_83)
  1011. u_fat_name(d.name, disp, sizeof(disp));
  1012. else {
  1013. for (dl = 0; dl + 1 < sizeof(disp) &&
  1014. d.name[dl]; dl++)
  1015. disp[dl] = d.name[dl];
  1016. disp[dl] = '\0';
  1017. }
  1018. n = name_len(disp, sizeof(disp));
  1019. if (n == 0 ||
  1020. (n == 1 && disp[0] == '.') ||
  1021. (n == 2 && disp[0] == '.' && disp[1] == '.'))
  1022. continue;
  1023. if (!prefix_is(base, bl, disp, n))
  1024. continue;
  1025. matches++;
  1026. if (matches == 1) {
  1027. cl = 0;
  1028. if (slash > 0) {
  1029. unsigned int k;
  1030. for (k = 0; k < slash &&
  1031. cl + 1 < sizeof(cand); k++)
  1032. cand[cl++] = token[k];
  1033. if (cl && cand[cl - 1] != '/' &&
  1034. cl + 1 < sizeof(cand))
  1035. cand[cl++] = '/';
  1036. }
  1037. for (n = 0; disp[n] &&
  1038. n < sizeof(disp) &&
  1039. cl + 1 < sizeof(cand); n++)
  1040. cand[cl++] = disp[n];
  1041. cand[cl] = '\0';
  1042. cand_dir = d.is_dir ? 1 : 0;
  1043. }
  1044. }
  1045. }
  1046. } else {
  1047. /* Command-name completion: builtins + /ramfs/bin. */
  1048. static const char * const builtins[] = {
  1049. "cd", "path", "help", "clear", "ps", "kill"
  1050. };
  1051. struct sys_dirent d;
  1052. for (i = 0; i < sizeof(builtins) / sizeof(builtins[0]); i++) {
  1053. n = name_len(builtins[i], 16);
  1054. if (!prefix_is(token, tlen, builtins[i], n))
  1055. continue;
  1056. matches++;
  1057. if (matches == 1) {
  1058. for (n = 0; builtins[i][n] && n + 1 < sizeof(cand); n++)
  1059. cand[n] = builtins[i][n];
  1060. cand[n] = '\0';
  1061. }
  1062. }
  1063. for (i = 0;; i++) {
  1064. if (u_syscall3(SYS_LIST_DIR, (int)"/ramfs/bin", (int)i,
  1065. (int)&d) != 1)
  1066. break;
  1067. n = name_len(d.name, sizeof(d.name));
  1068. if (!prefix_is(token, tlen, d.name, n))
  1069. continue;
  1070. matches++;
  1071. if (matches == 1) {
  1072. for (n = 0; d.name[n] && n < sizeof(d.name) &&
  1073. n + 1 < sizeof(cand); n++)
  1074. cand[n] = d.name[n];
  1075. cand[n] = '\0';
  1076. }
  1077. }
  1078. cand_dir = 0;
  1079. }
  1080. if (matches != 1)
  1081. return;
  1082. line_len = start;
  1083. for (i = 0; cand[i] && line_len + 1 < LINE_MAX; i++)
  1084. line[line_len++] = cand[i];
  1085. if (cand_dir && line_len + 1 < LINE_MAX)
  1086. line[line_len++] = '/';
  1087. else if (line_len + 1 < LINE_MAX)
  1088. line[line_len++] = ' ';
  1089. line[line_len] = '\0';
  1090. line_show();
  1091. }
  1092. static int is_page_key(int k)
  1093. {
  1094. return k == IO_KEY_PAGE_UP || k == IO_KEY_PAGE_DOWN ||
  1095. k == IO_KEY_HOME || k == IO_KEY_END;
  1096. }
  1097. static void handle_key(int k)
  1098. {
  1099. if (k == IO_KEY_PAGE_UP) {
  1100. scroll_by((int)rows - 1);
  1101. return;
  1102. }
  1103. if (k == IO_KEY_PAGE_DOWN) {
  1104. scroll_by(-((int)rows - 1));
  1105. return;
  1106. }
  1107. if (k == IO_KEY_HOME) {
  1108. scroll_by((int)row_count);
  1109. return;
  1110. }
  1111. if (k == IO_KEY_END) {
  1112. scroll_by(-(int)row_count);
  1113. return;
  1114. }
  1115. if (view) {
  1116. /* Typing always means the prompt: jump back to the tail. */
  1117. view = 0;
  1118. dirty = 1;
  1119. }
  1120. if (k == 0x03) {
  1121. if (fg_pid > 0) {
  1122. u_kill((unsigned int)fg_pid);
  1123. fg_pid = -1;
  1124. term_puts("^C\n");
  1125. prompt();
  1126. } else {
  1127. line_len = 0;
  1128. line[0] = '\0';
  1129. term_puts("^C\n");
  1130. prompt();
  1131. }
  1132. return;
  1133. }
  1134. if (k == '\n' || k == '\r') {
  1135. submit_line();
  1136. return;
  1137. }
  1138. if (k == '\b') {
  1139. if (line_len) {
  1140. line_len--;
  1141. line[line_len] = '\0';
  1142. line_show();
  1143. }
  1144. return;
  1145. }
  1146. if (k == IO_KEY_UP) {
  1147. if ((int)hist_browse < (int)hist_count) {
  1148. hist_browse++;
  1149. history_load(hist_browse);
  1150. }
  1151. return;
  1152. }
  1153. if (k == IO_KEY_DOWN) {
  1154. if (hist_browse > 0) {
  1155. hist_browse--;
  1156. if (hist_browse == 0)
  1157. line_set("");
  1158. else
  1159. history_load(hist_browse);
  1160. }
  1161. return;
  1162. }
  1163. if (k == '\t') {
  1164. complete_line();
  1165. return;
  1166. }
  1167. if (k < 0x20 || k > 0x7E)
  1168. return;
  1169. if (line_len + 1 < LINE_MAX) {
  1170. hist_browse = 0;
  1171. line[line_len++] = (char)k;
  1172. line[line_len] = '\0';
  1173. term_putc((char)k);
  1174. }
  1175. }
  1176. static void term_alloc_rows(void)
  1177. {
  1178. unsigned int bytes;
  1179. row_cap = SCROLL_ROWS_PREF;
  1180. bytes = 2 * row_cap * (unsigned int)sizeof(struct trow);
  1181. row_mem = (struct trow *)u_mem_alloc(bytes);
  1182. if (!row_mem) {
  1183. row_cap = SCROLL_ROWS_MIN;
  1184. bytes = 2 * row_cap * (unsigned int)sizeof(struct trow);
  1185. row_mem = (struct trow *)u_mem_alloc(bytes);
  1186. }
  1187. if (row_mem) {
  1188. row_scratch = row_mem + row_cap;
  1189. } else {
  1190. row_mem = base_rows;
  1191. row_scratch = base_scratch;
  1192. row_cap = MAX_ROWS;
  1193. }
  1194. }
  1195. void _start(const char *arg)
  1196. {
  1197. unsigned int now;
  1198. (void)arg;
  1199. tty = u_tty_create();
  1200. if (tty <= 0)
  1201. u_exit(1);
  1202. if (u_win_create(512, 512, "term", &wi) != 0)
  1203. u_exit(1);
  1204. usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
  1205. term_alloc_rows();
  1206. cols = (wi.width > SB_W) ? (wi.width - SB_W) / 8 : 1;
  1207. if (cols < 1)
  1208. cols = 1;
  1209. if (cols > MAX_COLS)
  1210. cols = MAX_COLS;
  1211. rows = wi.height / 16;
  1212. if (rows < 1)
  1213. rows = 1;
  1214. if (rows > MAX_ROWS)
  1215. rows = MAX_ROWS;
  1216. if (rows > row_cap)
  1217. rows = row_cap;
  1218. term_reset();
  1219. u_getcwd(cwd, sizeof(cwd));
  1220. {
  1221. const char *def = "/ramfs/bin";
  1222. unsigned int i;
  1223. for (i = 0; def[i] && i + 1 < sizeof(path); i++)
  1224. path[i] = def[i];
  1225. path[i] = '\0';
  1226. }
  1227. prompt();
  1228. render();
  1229. for (;;) {
  1230. int got = 0;
  1231. handle_resize();
  1232. drain_tty();
  1233. if (fg_pid > 0) {
  1234. if (!u_alive((unsigned int)fg_pid)) {
  1235. fg_pid = -1;
  1236. drain_tty();
  1237. prompt();
  1238. } else {
  1239. int k = u_key_poll();
  1240. while (k) {
  1241. if (k == 0x03 || is_page_key(k))
  1242. handle_key(k);
  1243. k = u_key_poll();
  1244. }
  1245. }
  1246. } else {
  1247. int k = u_win_key_wait(sb_drag ? 10 : 100);
  1248. if (k < 0)
  1249. u_exit(1);
  1250. while (k) {
  1251. handle_key(k);
  1252. got = 1;
  1253. k = u_key_poll();
  1254. }
  1255. }
  1256. handle_pointer();
  1257. now = u_ticks_ms();
  1258. if (now - last_blink >= 500) {
  1259. last_blink = now;
  1260. cursor_on = !cursor_on;
  1261. if (view == 0)
  1262. dirty = 1;
  1263. }
  1264. render();
  1265. u_sleep_ms((got || sb_drag) ? 1 : 10);
  1266. }
  1267. }