ucmd_term.c 31 KB

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