ucmd_term.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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. if (e.x >= (int)(wi.width - SB_W) && e.y >= 0 &&
  423. e.y < (int)wi.height) {
  424. unsigned int track_h, thumb_h, thumb_y;
  425. sb_metrics(&track_h, &thumb_h, &thumb_y);
  426. (void)track_h;
  427. if ((unsigned int)e.y >= thumb_y &&
  428. (unsigned int)e.y < thumb_y + thumb_h) {
  429. sb_drag = 1;
  430. sb_grab_ofs = e.y - (int)thumb_y;
  431. } else if ((unsigned int)e.y < thumb_y) {
  432. scroll_by((int)rows - 1);
  433. } else {
  434. scroll_by(-((int)rows - 1));
  435. }
  436. }
  437. } else if (e.type == WIN_EV_POINTER_UP) {
  438. if (sb_drag) {
  439. sb_drag = 0;
  440. mark_full();
  441. }
  442. }
  443. }
  444. if (sb_drag && u_win_pointer(&p) == 0) {
  445. if (!(p.buttons & 1)) {
  446. sb_drag = 0;
  447. mark_full();
  448. } else {
  449. sb_set_from_y(p.y);
  450. }
  451. }
  452. {
  453. struct win_scroll ws;
  454. if (u_win_scroll(&ws) == 1 && ws.dy) {
  455. int lines;
  456. wheel_acc += ws.dy;
  457. lines = wheel_acc / 16;
  458. wheel_acc -= lines * 16;
  459. if (lines)
  460. scroll_by(-lines);
  461. }
  462. }
  463. }
  464. /* ---- Rendering ---- */
  465. static void render(void)
  466. {
  467. unsigned int x, y, y0, y1, maxs, start;
  468. if (!dirty)
  469. return;
  470. maxs = row_count - rows;
  471. if (view > maxs) {
  472. view = maxs;
  473. dirty_full = 1;
  474. }
  475. start = origin_row() - view;
  476. if (dirty_full) {
  477. y0 = 0;
  478. y1 = rows - 1;
  479. } else if (dmg_valid) {
  480. y0 = dmg_y0;
  481. y1 = dmg_y1;
  482. } else {
  483. dirty = 0;
  484. return;
  485. }
  486. for (y = y0; y <= y1; y++) {
  487. unsigned int idx = start + y;
  488. unsigned short *dst = &view_cells[y * cols];
  489. if (idx < row_count) {
  490. struct trow *r = row_at(idx);
  491. unsigned int n = r->len;
  492. if (n > cols)
  493. n = cols;
  494. for (x = 0; x < n; x++)
  495. dst[x] = r->cells[x];
  496. for (; x < cols; x++)
  497. dst[x] = 0x0A20;
  498. } else {
  499. for (x = 0; x < cols; x++)
  500. dst[x] = 0x0A20;
  501. }
  502. }
  503. if (dirty_full)
  504. usurf_fill(&surf, 0, 0, wi.width, wi.height, 0x000000u);
  505. else
  506. usurf_fill(&surf, 0, (int)(y0 * 16), wi.width,
  507. (y1 - y0 + 1) * 16, 0x000000u);
  508. usurf_cells(&surf, &view_cells[y0 * cols], cols, y1 - y0 + 1,
  509. 0, (int)(y0 * 16), 0, 0, wi.width, wi.height);
  510. if (view == 0 && cursor_on && cy >= y0 && cy <= y1) {
  511. unsigned int ccx = (cx < cols) ? cx : (cols ? cols - 1 : 0);
  512. usurf_fill(&surf, (int)ccx * 8, (int)cy * 16, 2, 16,
  513. 0xFFFFFFu);
  514. }
  515. draw_scrollbar();
  516. if (dirty_full)
  517. u_win_present();
  518. else
  519. u_win_present_rect(0, (int)(y0 * 16), wi.width,
  520. (y1 - y0 + 1) * 16);
  521. dirty = 0;
  522. dirty_full = 0;
  523. dmg_valid = 0;
  524. }
  525. static void line_show(void)
  526. {
  527. unsigned int i, x, base, k;
  528. struct trow *r;
  529. mark_row(cy);
  530. if (view) {
  531. view = 0;
  532. mark_full();
  533. }
  534. base = line_abs;
  535. if (base < origin_row())
  536. base = origin_row();
  537. if (base >= row_count)
  538. base = row_count - 1;
  539. line_abs = base;
  540. r = row_at(base);
  541. if (r->len > line_col)
  542. r->len = line_col;
  543. r->wrapped = 0;
  544. /* The input line is the tail of the log: rows after its start are
  545. * leftovers of the previous, possibly longer, version of the line. */
  546. for (k = 1; base + k < row_count; k++) {
  547. row_blank(row_at(base + k));
  548. if (base + k >= origin_row())
  549. mark_row(base + k - origin_row());
  550. }
  551. cx = line_col;
  552. cy = base - origin_row();
  553. wrap_pending = 0;
  554. if (cx >= cols)
  555. term_wrap(); /* the prompt filled the row exactly */
  556. for (i = 0; i < line_len; i++)
  557. term_putc(line[i]);
  558. for (x = cx; x < cols; x++)
  559. cell_put(x, cy, ' ');
  560. mark_row(cy);
  561. }
  562. /* ---- Reflow ---- */
  563. /* Absolute row index of `idx` and the character offset of (idx, col) in the
  564. * logical line that contains it. */
  565. static void pos_capture(struct pos_map *m, unsigned int idx, unsigned int col)
  566. {
  567. unsigned int ls, off = 0, j;
  568. if (idx >= row_count)
  569. idx = row_count - 1;
  570. if (col > MAX_COLS)
  571. col = MAX_COLS;
  572. ls = idx;
  573. while (ls > 0 && row_at(ls - 1)->wrapped)
  574. ls--;
  575. for (j = ls; j < idx; j++) {
  576. unsigned int l = row_at(j)->len;
  577. if (l > MAX_COLS)
  578. l = MAX_COLS;
  579. off += l;
  580. }
  581. {
  582. unsigned int l = row_at(idx)->len;
  583. if (col > l)
  584. col = l;
  585. off += col;
  586. }
  587. m->abs = idx;
  588. m->col = col;
  589. m->start = ls;
  590. m->off = off;
  591. m->dst = POS_NONE;
  592. }
  593. /* Re-wrap every logical line at `new_cols` into the scratch ring, then copy
  594. * the retained tail back. The two maps are moved through the same rebuild so
  595. * the cursor and the shell's input line keep pointing at the same text. */
  596. static void reflow(unsigned int new_cols, struct pos_map *cur,
  597. struct pos_map *lin)
  598. {
  599. unsigned int i = 0, out_total = 0, keep, drop, k;
  600. while (i < row_count) {
  601. unsigned int ls = i, j = i, len = 0;
  602. unsigned int out_start = out_total;
  603. while (1) {
  604. struct trow *r = row_at(j);
  605. unsigned int l = r->len;
  606. if (l > MAX_COLS)
  607. l = MAX_COLS;
  608. len += l;
  609. j++;
  610. if (!r->wrapped || j >= row_count)
  611. break;
  612. }
  613. if (ls == cur->start)
  614. cur->dst = out_start;
  615. if (ls == lin->start)
  616. lin->dst = out_start;
  617. if (len == 0) {
  618. row_blank(dst_row(out_total++));
  619. } else {
  620. unsigned int p = 0, srow = ls, sc = 0;
  621. while (p < len) {
  622. struct trow *d = dst_row(out_total++);
  623. unsigned int n = len - p;
  624. unsigned int q = 0;
  625. if (n > new_cols)
  626. n = new_cols;
  627. row_blank(d);
  628. while (q < n) {
  629. struct trow *s = row_at(srow);
  630. unsigned int sl = s->len;
  631. if (sl > MAX_COLS)
  632. sl = MAX_COLS;
  633. if (sc >= sl) {
  634. srow++;
  635. sc = 0;
  636. continue;
  637. }
  638. d->cells[q++] = s->cells[sc++];
  639. p++;
  640. }
  641. d->len = n;
  642. d->wrapped = (p < len);
  643. }
  644. }
  645. i = j;
  646. }
  647. keep = (out_total < row_cap) ? out_total : row_cap;
  648. drop = out_total - keep;
  649. for (k = 0; k < keep; k++) {
  650. struct trow *s = dst_row(drop + k);
  651. struct trow *d = &row_mem[k];
  652. unsigned int c;
  653. d->len = s->len;
  654. d->wrapped = s->wrapped;
  655. for (c = 0; c < MAX_COLS; c++)
  656. d->cells[c] = s->cells[c];
  657. }
  658. row_first = 0;
  659. row_count = keep;
  660. if (cur->dst != POS_NONE) {
  661. unsigned int a = cur->dst + cur->off / new_cols;
  662. cur->abs = (a >= drop) ? a - drop : 0;
  663. cur->col = cur->off % new_cols;
  664. }
  665. if (lin->dst != POS_NONE) {
  666. unsigned int a = lin->dst + lin->off / new_cols;
  667. lin->abs = (a >= drop) ? a - drop : 0;
  668. lin->col = lin->off % new_cols;
  669. }
  670. }
  671. static void finish_resize(unsigned int new_rows, struct pos_map *cur,
  672. struct pos_map *lin)
  673. {
  674. unsigned int origin;
  675. /* Blank rows below the last written one are only padding. Dropping
  676. * them keeps the cursor on screen when the window gets shorter;
  677. * otherwise the tail-anchored screen would scroll the prompt away. */
  678. while (row_count > 0) {
  679. struct trow *r = row_at(row_count - 1);
  680. if (r->len != 0 || r->wrapped)
  681. break;
  682. row_count--;
  683. }
  684. rows = new_rows;
  685. while (row_count < rows)
  686. row_add_blank();
  687. origin = row_count - rows;
  688. if (view > origin)
  689. view = origin;
  690. if (cur->abs < origin)
  691. cur->abs = origin;
  692. if (cur->abs >= row_count) {
  693. row_add_blank();
  694. cur->abs = row_count - 1;
  695. }
  696. origin = row_count - rows;
  697. cy = cur->abs - origin;
  698. cx = (cur->col < cols) ? cur->col : cols - 1;
  699. if (lin->abs < origin)
  700. lin->abs = origin;
  701. if (lin->abs >= row_count)
  702. lin->abs = row_count - 1;
  703. line_abs = lin->abs;
  704. line_col = (lin->col < cols) ? lin->col : cols - 1;
  705. mark_full();
  706. }
  707. static void handle_resize(void)
  708. {
  709. struct win_info cur;
  710. struct pos_map cpos, lpos;
  711. unsigned int new_cols, new_rows;
  712. if (u_win_info(&cur) != 0)
  713. return;
  714. if (cur.width == wi.width && cur.height == wi.height)
  715. return;
  716. if (u_win_ack_resize() != 0)
  717. return;
  718. new_cols = (cur.width > SB_W) ? (cur.width - SB_W) / 8 : 1;
  719. if (new_cols < 1)
  720. new_cols = 1;
  721. if (new_cols > MAX_COLS)
  722. new_cols = MAX_COLS;
  723. new_rows = cur.height / 16;
  724. if (new_rows < 1)
  725. new_rows = 1;
  726. if (new_rows > MAX_ROWS)
  727. new_rows = MAX_ROWS;
  728. if (new_rows > row_cap)
  729. new_rows = row_cap;
  730. pos_capture(&cpos, origin_row() + cy, cx);
  731. pos_capture(&lpos, line_abs, line_col);
  732. wi = cur;
  733. usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
  734. if (new_cols != cols) {
  735. reflow(new_cols, &cpos, &lpos);
  736. cols = new_cols;
  737. }
  738. finish_resize(new_rows, &cpos, &lpos);
  739. }
  740. static void line_set(const char *s)
  741. {
  742. unsigned int i = 0;
  743. while (s[i] && i + 1 < LINE_MAX) {
  744. line[i] = s[i];
  745. i++;
  746. }
  747. line[i] = '\0';
  748. line_len = i;
  749. line_show();
  750. }
  751. static void history_load(int n)
  752. {
  753. unsigned int idx;
  754. if (n <= 0 || n > (int)hist_count)
  755. return;
  756. idx = (hist_next + HIST_MAX - (unsigned int)n) % HIST_MAX;
  757. line_set(hist[idx]);
  758. }
  759. static void prompt(void)
  760. {
  761. term_putc('[');
  762. term_puts(cwd);
  763. term_puts("] > ");
  764. line_len = 0;
  765. line[0] = '\0';
  766. line_abs = origin_row() + cy;
  767. line_col = cx;
  768. hist_browse = 0;
  769. mark_row(cy);
  770. }
  771. static void print_num(unsigned int n)
  772. {
  773. char b[12];
  774. unsigned int i = 0;
  775. if (!n) {
  776. term_putc('0');
  777. return;
  778. }
  779. while (n) {
  780. b[i++] = (char)('0' + n % 10);
  781. n /= 10;
  782. }
  783. while (i)
  784. term_putc(b[--i]);
  785. }
  786. static unsigned int parse_num(const char *s)
  787. {
  788. unsigned int n = 0;
  789. while (*s == ' ')
  790. s++;
  791. while (*s >= '0' && *s <= '9')
  792. n = n * 10 + (unsigned int)(*s++ - '0');
  793. return n;
  794. }
  795. static void split_line(char *word, unsigned int wsz, char *args,
  796. unsigned int asz)
  797. {
  798. unsigned int i = 0, n = 0;
  799. while (line[i] == ' ')
  800. i++;
  801. while (line[i] && line[i] != ' ' && n + 1 < wsz)
  802. word[n++] = line[i++];
  803. word[n] = '\0';
  804. while (line[i] == ' ')
  805. i++;
  806. n = 0;
  807. while (line[i] && n + 1 < asz)
  808. args[n++] = line[i++];
  809. while (n && args[n - 1] == ' ')
  810. n--;
  811. args[n] = '\0';
  812. }
  813. static int make_path(const char *dir, const char *word, char *out,
  814. unsigned int max)
  815. {
  816. unsigned int i = 0, j;
  817. if (word[0] == '/') {
  818. for (j = 0; word[j] && j + 1 < max; j++)
  819. out[j] = word[j];
  820. out[j] = '\0';
  821. return 0;
  822. }
  823. while (dir[i] && i + 1 < max)
  824. out[i] = dir[i], i++;
  825. if (i && out[i - 1] != '/' && i + 1 < max)
  826. out[i++] = '/';
  827. for (j = 0; word[j] && i + 1 < max; j++)
  828. out[i++] = word[j];
  829. out[i] = '\0';
  830. return 0;
  831. }
  832. static int try_spawn(const char *word, const char *args, char *used_path,
  833. unsigned int used_size)
  834. {
  835. char p[VFS_PATH_MAX];
  836. int pid;
  837. if (word[0] == '/') {
  838. for (unsigned int i = 0; word[i] && i + 1 < used_size; i++)
  839. used_path[i] = word[i];
  840. return u_spawn(word, args, tty, cwd);
  841. }
  842. /* Try each PATH component. */
  843. {
  844. unsigned int i = 0, start = 0;
  845. while (1) {
  846. unsigned int n = 0;
  847. char dir[VFS_PATH_MAX];
  848. while (path[i] && path[i] != ':') {
  849. if (n + 1 < sizeof(dir))
  850. dir[n++] = path[i];
  851. i++;
  852. }
  853. dir[n] = '\0';
  854. if (dir[0]) {
  855. make_path(dir, word, p, sizeof(p));
  856. pid = u_spawn(p, args, tty, cwd);
  857. if (pid >= 0) {
  858. for (n = 0; p[n] && n + 1 < used_size; n++)
  859. used_path[n] = p[n];
  860. used_path[n] = '\0';
  861. return pid;
  862. }
  863. if (pid == -2)
  864. return -2;
  865. }
  866. if (!path[i])
  867. break;
  868. i++;
  869. start = i;
  870. (void)start;
  871. }
  872. }
  873. return -1;
  874. }
  875. static void run_line(void)
  876. {
  877. char word[LINE_MAX], args[LINE_MAX];
  878. char used[VFS_PATH_MAX];
  879. int pid;
  880. split_line(word, sizeof(word), args, sizeof(args));
  881. if (!word[0]) {
  882. prompt();
  883. return;
  884. }
  885. if (word[0] == 'c' && word[1] == 'd' && !word[2]) {
  886. if (u_chdir(args[0] ? args : "/") != 0)
  887. term_puts("cd: no such directory\n");
  888. u_getcwd(cwd, sizeof(cwd));
  889. prompt();
  890. return;
  891. }
  892. if (word[0] == 'p' && word[1] == 'a' && word[2] == 't' && !word[3]) {
  893. unsigned int i;
  894. if (!args[0]) {
  895. term_puts(path);
  896. term_putc('\n');
  897. } else {
  898. for (i = 0; args[i] && i + 1 < sizeof(path); i++)
  899. path[i] = args[i];
  900. path[i] = '\0';
  901. }
  902. prompt();
  903. return;
  904. }
  905. if (word[0] == 'h' && word[1] == 'e' && word[2] == 'l' &&
  906. word[3] == 'p' && !word[4]) {
  907. pid = u_spawn_kcmd("help", "", tty);
  908. if (pid > 0) {
  909. fg_pid = pid;
  910. return;
  911. }
  912. term_puts("builtins: cd path help clear ps kill\n");
  913. term_puts("commands: files in ");
  914. term_puts(path);
  915. term_putc('\n');
  916. prompt();
  917. return;
  918. }
  919. if (word[0] == 'c' && word[1] == 'l' && word[2] == 'e' &&
  920. word[3] == 'a' && word[4] == 'r' && !word[5]) {
  921. term_reset();
  922. prompt();
  923. return;
  924. }
  925. if (word[0] == 'p' && word[1] == 's' && !word[2]) {
  926. struct sys_procinfo pi;
  927. unsigned int i = 0;
  928. term_puts("PID STATE W CPU NAME\n");
  929. while (u_syscall2(SYS_PROCINFO, (int)i, (int)&pi) == 1) {
  930. print_num(pi.pid);
  931. term_puts(" ");
  932. if (pi.state == 1) term_puts("ready");
  933. else if (pi.state == 2) term_puts("run ");
  934. else if (pi.state == 4) term_puts("sleep");
  935. else if (pi.state == 5) term_puts("block");
  936. else term_puts("? ");
  937. term_puts(" ");
  938. print_num(pi.weight);
  939. term_puts(" ");
  940. if (pi.cpu != 0xFFFFFFFFu)
  941. print_num(pi.cpu);
  942. else
  943. term_putc('-');
  944. term_puts(" ");
  945. term_puts(pi.name);
  946. term_putc('\n');
  947. i++;
  948. }
  949. prompt();
  950. return;
  951. }
  952. if (word[0] == 'k' && word[1] == 'i' && word[2] == 'l' &&
  953. word[3] == 'l' && !word[4]) {
  954. unsigned int pid = parse_num(args);
  955. if (!pid || u_kill(pid) != 0)
  956. term_puts("kill: no such process\n");
  957. prompt();
  958. return;
  959. }
  960. pid = try_spawn(word, args, used, sizeof(used));
  961. if (pid == -2)
  962. term_puts("not an executable\n");
  963. else if (pid < 0) {
  964. pid = u_spawn_kcmd(word, args, tty);
  965. if (pid < 0)
  966. term_puts("command not found\n");
  967. }
  968. if (pid > 0)
  969. fg_pid = pid;
  970. else
  971. prompt();
  972. }
  973. static void submit_line(void)
  974. {
  975. term_putc('\n');
  976. if (line_len) {
  977. unsigned int i;
  978. for (i = 0; line[i] && i < sizeof(hist[hist_next]) - 1; i++)
  979. hist[hist_next][i] = line[i];
  980. hist[hist_next][i] = '\0';
  981. hist_next = (hist_next + 1) % HIST_MAX;
  982. if (hist_count < HIST_MAX)
  983. hist_count++;
  984. }
  985. hist_browse = 0;
  986. run_line();
  987. }
  988. static unsigned int name_len(const char *s, unsigned int max)
  989. {
  990. unsigned int n = 0;
  991. while (n < max && s[n])
  992. n++;
  993. while (n && s[n - 1] == ' ')
  994. n--;
  995. return n;
  996. }
  997. static int prefix_is(const char *word, unsigned int wl, const char *name,
  998. unsigned int nl)
  999. {
  1000. unsigned int i;
  1001. if (wl > nl)
  1002. return 0;
  1003. for (i = 0; i < wl; i++)
  1004. if (word[i] != name[i])
  1005. return 0;
  1006. return 1;
  1007. }
  1008. /* Complete the token at the end of the line. A token after the command word
  1009. * (or any token containing '/') is a path; the first token is a command. */
  1010. static void complete_line(void)
  1011. {
  1012. unsigned int start = line_len;
  1013. unsigned int tlen, slash = 0, i, n, bl, cl, matches = 0;
  1014. char token[LINE_MAX], dir[VFS_PATH_MAX], base[LINE_MAX];
  1015. char cand[LINE_MAX];
  1016. int has_slash = 0, cand_dir = 0;
  1017. while (start > 0 && line[start - 1] != ' ')
  1018. start--;
  1019. tlen = line_len - start;
  1020. if (!tlen)
  1021. return;
  1022. for (i = 0; i < tlen && i + 1 < sizeof(token); i++)
  1023. token[i] = line[start + i];
  1024. token[i] = '\0';
  1025. for (i = 0; i < tlen; i++)
  1026. if (token[i] == '/') {
  1027. has_slash = 1;
  1028. slash = i;
  1029. }
  1030. if (has_slash || start > 0) {
  1031. /* Path token: split into directory + basename prefix. */
  1032. if (!has_slash) {
  1033. dir[0] = '.';
  1034. dir[1] = '\0';
  1035. for (i = 0; i < tlen && i + 1 < sizeof(base); i++)
  1036. base[i] = token[i];
  1037. base[i] = '\0';
  1038. bl = i;
  1039. } else if (slash == 0) {
  1040. dir[0] = '/';
  1041. dir[1] = '\0';
  1042. for (i = 0; i + 1 < tlen && i + 1 < sizeof(base); i++)
  1043. base[i] = token[i + 1];
  1044. base[i] = '\0';
  1045. bl = i;
  1046. } else {
  1047. if (slash >= sizeof(dir))
  1048. return;
  1049. for (i = 0; i < slash; i++)
  1050. dir[i] = token[i];
  1051. dir[i] = '\0';
  1052. for (i = 0; slash + 1 + i < tlen &&
  1053. i + 1 < sizeof(base); i++)
  1054. base[i] = token[slash + 1 + i];
  1055. base[i] = '\0';
  1056. bl = i;
  1057. }
  1058. if (dir[0] == '/' && dir[1] == '\0') {
  1059. struct sys_mount m;
  1060. for (i = 0;; i++) {
  1061. if (u_syscall2(SYS_LIST_MOUNTS, (int)i,
  1062. (int)&m) != 1)
  1063. break;
  1064. n = name_len(m.name, sizeof(m.name));
  1065. if (!prefix_is(base, bl, m.name, n))
  1066. continue;
  1067. matches++;
  1068. if (matches == 1) {
  1069. cl = 0;
  1070. cand[cl++] = '/';
  1071. for (n = 0; m.name[n] && cl + 1 < sizeof(cand); n++)
  1072. cand[cl++] = m.name[n];
  1073. cand[cl] = '\0';
  1074. cand_dir = 1;
  1075. }
  1076. }
  1077. } else {
  1078. struct sys_dirent d;
  1079. for (i = 0;; i++) {
  1080. char disp[64];
  1081. unsigned int dl;
  1082. if (u_syscall3(SYS_LIST_DIR, (int)dir, (int)i,
  1083. (int)&d) != 1)
  1084. break;
  1085. if (d.raw_83)
  1086. u_fat_name(d.name, disp, sizeof(disp));
  1087. else {
  1088. for (dl = 0; dl + 1 < sizeof(disp) &&
  1089. d.name[dl]; dl++)
  1090. disp[dl] = d.name[dl];
  1091. disp[dl] = '\0';
  1092. }
  1093. n = name_len(disp, sizeof(disp));
  1094. if (n == 0 ||
  1095. (n == 1 && disp[0] == '.') ||
  1096. (n == 2 && disp[0] == '.' && disp[1] == '.'))
  1097. continue;
  1098. if (!prefix_is(base, bl, disp, n))
  1099. continue;
  1100. matches++;
  1101. if (matches == 1) {
  1102. cl = 0;
  1103. if (slash > 0) {
  1104. unsigned int k;
  1105. for (k = 0; k < slash &&
  1106. cl + 1 < sizeof(cand); k++)
  1107. cand[cl++] = token[k];
  1108. if (cl && cand[cl - 1] != '/' &&
  1109. cl + 1 < sizeof(cand))
  1110. cand[cl++] = '/';
  1111. }
  1112. for (n = 0; disp[n] &&
  1113. n < sizeof(disp) &&
  1114. cl + 1 < sizeof(cand); n++)
  1115. cand[cl++] = disp[n];
  1116. cand[cl] = '\0';
  1117. cand_dir = d.is_dir ? 1 : 0;
  1118. }
  1119. }
  1120. }
  1121. } else {
  1122. /* Command-name completion: builtins + /ramfs/bin. */
  1123. static const char * const builtins[] = {
  1124. "cd", "path", "help", "clear", "bootlog", "ps", "kill"
  1125. };
  1126. struct sys_dirent d;
  1127. for (i = 0; i < sizeof(builtins) / sizeof(builtins[0]); i++) {
  1128. n = name_len(builtins[i], 16);
  1129. if (!prefix_is(token, tlen, builtins[i], n))
  1130. continue;
  1131. matches++;
  1132. if (matches == 1) {
  1133. for (n = 0; builtins[i][n] && n + 1 < sizeof(cand); n++)
  1134. cand[n] = builtins[i][n];
  1135. cand[n] = '\0';
  1136. }
  1137. }
  1138. for (i = 0;; i++) {
  1139. if (u_syscall3(SYS_LIST_DIR, (int)"/ramfs/bin", (int)i,
  1140. (int)&d) != 1)
  1141. break;
  1142. n = name_len(d.name, sizeof(d.name));
  1143. if (!prefix_is(token, tlen, d.name, n))
  1144. continue;
  1145. matches++;
  1146. if (matches == 1) {
  1147. for (n = 0; d.name[n] && n < sizeof(d.name) &&
  1148. n + 1 < sizeof(cand); n++)
  1149. cand[n] = d.name[n];
  1150. cand[n] = '\0';
  1151. }
  1152. }
  1153. cand_dir = 0;
  1154. }
  1155. if (matches != 1)
  1156. return;
  1157. line_len = start;
  1158. for (i = 0; cand[i] && line_len + 1 < LINE_MAX; i++)
  1159. line[line_len++] = cand[i];
  1160. if (cand_dir && line_len + 1 < LINE_MAX)
  1161. line[line_len++] = '/';
  1162. else if (line_len + 1 < LINE_MAX)
  1163. line[line_len++] = ' ';
  1164. line[line_len] = '\0';
  1165. line_show();
  1166. }
  1167. static int is_page_key(int k)
  1168. {
  1169. return k == IO_KEY_PAGE_UP || k == IO_KEY_PAGE_DOWN ||
  1170. k == IO_KEY_HOME || k == IO_KEY_END;
  1171. }
  1172. static void handle_key(int k)
  1173. {
  1174. if (k == IO_KEY_PAGE_UP) {
  1175. scroll_by((int)rows - 1);
  1176. return;
  1177. }
  1178. if (k == IO_KEY_PAGE_DOWN) {
  1179. scroll_by(-((int)rows - 1));
  1180. return;
  1181. }
  1182. if (k == IO_KEY_HOME) {
  1183. scroll_by((int)row_count);
  1184. return;
  1185. }
  1186. if (k == IO_KEY_END) {
  1187. scroll_by(-(int)row_count);
  1188. return;
  1189. }
  1190. if (view) {
  1191. /* Typing always means the prompt: jump back to the tail. */
  1192. view = 0;
  1193. mark_full();
  1194. }
  1195. if (k == 0x03) {
  1196. if (fg_pid > 0) {
  1197. u_kill((unsigned int)fg_pid);
  1198. fg_pid = -1;
  1199. term_puts("^C\n");
  1200. prompt();
  1201. } else {
  1202. line_len = 0;
  1203. line[0] = '\0';
  1204. term_puts("^C\n");
  1205. prompt();
  1206. }
  1207. return;
  1208. }
  1209. if (k == '\n' || k == '\r') {
  1210. submit_line();
  1211. return;
  1212. }
  1213. if (k == '\b') {
  1214. if (line_len) {
  1215. line_len--;
  1216. line[line_len] = '\0';
  1217. line_show();
  1218. }
  1219. return;
  1220. }
  1221. if (k == IO_KEY_UP) {
  1222. if ((int)hist_browse < (int)hist_count) {
  1223. hist_browse++;
  1224. history_load(hist_browse);
  1225. }
  1226. return;
  1227. }
  1228. if (k == IO_KEY_DOWN) {
  1229. if (hist_browse > 0) {
  1230. hist_browse--;
  1231. if (hist_browse == 0)
  1232. line_set("");
  1233. else
  1234. history_load(hist_browse);
  1235. }
  1236. return;
  1237. }
  1238. if (k == '\t') {
  1239. complete_line();
  1240. return;
  1241. }
  1242. if (k < 0x20 || k > 0x7E)
  1243. return;
  1244. if (line_len + 1 < LINE_MAX) {
  1245. hist_browse = 0;
  1246. line[line_len++] = (char)k;
  1247. line[line_len] = '\0';
  1248. term_putc((char)k);
  1249. }
  1250. }
  1251. static void term_alloc_rows(void)
  1252. {
  1253. unsigned int bytes;
  1254. row_cap = SCROLL_ROWS_PREF;
  1255. bytes = 2 * row_cap * (unsigned int)sizeof(struct trow);
  1256. row_mem = (struct trow *)malloc(bytes);
  1257. if (!row_mem) {
  1258. row_cap = SCROLL_ROWS_MIN;
  1259. bytes = 2 * row_cap * (unsigned int)sizeof(struct trow);
  1260. row_mem = (struct trow *)malloc(bytes);
  1261. }
  1262. if (row_mem) {
  1263. row_scratch = row_mem + row_cap;
  1264. } else {
  1265. row_mem = base_rows;
  1266. row_scratch = base_scratch;
  1267. row_cap = MAX_ROWS;
  1268. }
  1269. }
  1270. void _start(const char *arg)
  1271. {
  1272. unsigned int now;
  1273. (void)arg;
  1274. tty = u_tty_create();
  1275. if (tty <= 0)
  1276. u_exit(1);
  1277. if (u_win_create(512, 512, "term", &wi) != 0)
  1278. u_exit(1);
  1279. /* Text cells: the I-beam over the whole content area. */
  1280. u_win_set_cursor(CURSOR_TEXT);
  1281. usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
  1282. term_alloc_rows();
  1283. cols = (wi.width > SB_W) ? (wi.width - SB_W) / 8 : 1;
  1284. if (cols < 1)
  1285. cols = 1;
  1286. if (cols > MAX_COLS)
  1287. cols = MAX_COLS;
  1288. rows = wi.height / 16;
  1289. if (rows < 1)
  1290. rows = 1;
  1291. if (rows > MAX_ROWS)
  1292. rows = MAX_ROWS;
  1293. if (rows > row_cap)
  1294. rows = row_cap;
  1295. term_reset();
  1296. u_getcwd(cwd, sizeof(cwd));
  1297. {
  1298. const char *def = "/ramfs/bin";
  1299. unsigned int i;
  1300. for (i = 0; def[i] && i + 1 < sizeof(path); i++)
  1301. path[i] = def[i];
  1302. path[i] = '\0';
  1303. }
  1304. prompt();
  1305. render();
  1306. for (;;) {
  1307. int got = 0;
  1308. handle_resize();
  1309. drain_tty();
  1310. if (fg_pid > 0) {
  1311. if (!u_alive((unsigned int)fg_pid)) {
  1312. fg_pid = -1;
  1313. drain_tty();
  1314. prompt();
  1315. } else {
  1316. int k = u_key_poll();
  1317. while (k) {
  1318. if (k == 0x03 || is_page_key(k))
  1319. handle_key(k);
  1320. k = u_key_poll();
  1321. }
  1322. }
  1323. } else {
  1324. int k = u_win_key_wait(sb_drag ? 10 : 100);
  1325. if (k < 0)
  1326. u_exit(1);
  1327. while (k) {
  1328. handle_key(k);
  1329. got = 1;
  1330. k = u_key_poll();
  1331. }
  1332. }
  1333. handle_pointer();
  1334. now = u_ticks_ms();
  1335. if (now - last_blink >= 500) {
  1336. last_blink = now;
  1337. cursor_on = !cursor_on;
  1338. if (view == 0)
  1339. mark_row(cy);
  1340. }
  1341. render();
  1342. u_sleep_ms((got || sb_drag) ? 1 : 10);
  1343. }
  1344. }