ucmd_term.c 31 KB

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