ucmd_term.c 31 KB

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