ucmd_edit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /* edit: small ring-3 text editor drawn into the terminal window's canvas.
  2. *
  3. * Controls: printable keys, Enter, Backspace and Tab edit the buffer; the
  4. * arrow keys move; Ctrl+S saves the file named by the first argument and
  5. * Ctrl+Q quits (when the buffer is modified, a second Ctrl+Q discards the
  6. * changes). Ctrl+C still reaches the terminal and kills the command.
  7. *
  8. * A gfx session is the only key-input path available to a ring-3 command, so
  9. * the editor repaints the window canvas itself. The 8x16 userland font is
  10. * linked into this ELF by the Makefile (see ucmd_edit.elf). */
  11. #include "ucmd_lib.h"
  12. #include "font_8x16.h"
  13. #define EDIT_MAX 16384
  14. #define EDIT_READ_MAX (EDIT_MAX + 1) /* one extra byte detects "too large" */
  15. /* The FAT read path copies whole 512-byte sectors, so the last partial
  16. * sector can write up to 511 bytes past the requested length. Keep that
  17. * slack inside the buffer instead of letting it hit the next (unmapped)
  18. * page. It is never displayed or saved. */
  19. #define EDIT_BUF_SIZE (EDIT_READ_MAX + 512)
  20. #define TAB_WIDTH 4
  21. #define CELL_W 8
  22. #define CELL_H 16
  23. #define PATH_MAX 128
  24. #define BASE_MAX 64
  25. #define STATUS_MAX 96
  26. /* Key codes produced by the kernel keyboard path (see io.h). */
  27. #define KEY_ESC 0x1B
  28. #define KEY_BACKSPACE 0x08
  29. #define KEY_CTRL_Q 0x11
  30. #define KEY_CTRL_S 0x13
  31. #define KEY_LEFT 0x100
  32. #define KEY_RIGHT 0x101
  33. #define KEY_UP 0x102
  34. #define KEY_DOWN 0x103
  35. #define RGB565(r, g, b) \
  36. ((unsigned short)((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3)))
  37. static const unsigned short col_bg = RGB565(0, 0, 0);
  38. static const unsigned short col_fg = RGB565(170, 170, 170);
  39. static const unsigned short col_hint_bg = RGB565(32, 32, 48);
  40. static const unsigned short col_hint_fg = RGB565(85, 255, 255);
  41. static const unsigned short col_status_bg = RGB565(0, 0, 170);
  42. static const unsigned short col_status_fg = RGB565(255, 255, 255);
  43. static const unsigned short col_cursor = RGB565(255, 255, 255);
  44. static const unsigned short col_cursor_fg = RGB565(0, 0, 0);
  45. static char text[EDIT_BUF_SIZE];
  46. static unsigned int len;
  47. static unsigned int cur;
  48. static char path[PATH_MAX];
  49. static char base_name[BASE_MAX];
  50. static unsigned int dirty;
  51. static unsigned int scroll_top;
  52. static int scroll_acc;
  53. static int view_locked;
  54. static unsigned int xoff;
  55. static int quit_armed;
  56. static char status_msg[STATUS_MAX];
  57. static unsigned int status_until;
  58. static unsigned short *fb;
  59. static unsigned int fb_w, fb_h;
  60. static unsigned int cols, rows, text_rows;
  61. /* ---- tiny string and drawing helpers (no libc) ---- */
  62. static unsigned int append_str(char *dst, unsigned int n, const char *s,
  63. unsigned int max)
  64. {
  65. while (*s && n + 1 < max)
  66. dst[n++] = *s++;
  67. dst[n] = '\0';
  68. return n;
  69. }
  70. static unsigned int append_uint(char *dst, unsigned int n, unsigned int v,
  71. unsigned int max)
  72. {
  73. char tmp[11];
  74. unsigned int i = 0, j;
  75. do {
  76. tmp[i++] = (char)('0' + (v % 10));
  77. v /= 10;
  78. } while (v && i < sizeof(tmp));
  79. for (j = 0; j < i && n + 1 < max; j++)
  80. dst[n++] = tmp[i - 1 - j];
  81. dst[n] = '\0';
  82. return n;
  83. }
  84. static void fill_rect(unsigned int x, unsigned int y, unsigned int w,
  85. unsigned int h, unsigned short color)
  86. {
  87. unsigned int row, col;
  88. if (x >= fb_w || y >= fb_h)
  89. return;
  90. if (w > fb_w - x)
  91. w = fb_w - x;
  92. if (h > fb_h - y)
  93. h = fb_h - y;
  94. for (row = 0; row < h; row++) {
  95. unsigned short *p = fb + (y + row) * fb_w + x;
  96. for (col = 0; col < w; col++)
  97. p[col] = color;
  98. }
  99. }
  100. /* Draw the foreground pixels of one glyph; the background is left alone. */
  101. static void draw_glyph_at(unsigned int x, unsigned int y, unsigned char c,
  102. unsigned short fg)
  103. {
  104. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  105. unsigned int row, col;
  106. if (x >= fb_w)
  107. return;
  108. for (row = 0; row < FONT_8X16_ROWS; row++) {
  109. unsigned int py = y + row;
  110. unsigned short *p;
  111. unsigned char bits;
  112. if (py >= fb_h)
  113. break;
  114. p = fb + py * fb_w + x;
  115. bits = g[row];
  116. for (col = 0; col < 8 && x + col < fb_w; col++)
  117. if (bits & (0x80u >> col))
  118. p[col] = fg;
  119. }
  120. }
  121. static void draw_string(unsigned int x, unsigned int y, const char *s,
  122. unsigned int max_chars, unsigned short fg)
  123. {
  124. unsigned int i;
  125. for (i = 0; s[i] && i < max_chars; i++)
  126. draw_glyph_at(x + i * CELL_W, y, (unsigned char)s[i], fg);
  127. }
  128. static void set_msg(const char *s, unsigned int ms)
  129. {
  130. append_str(status_msg, 0, s, sizeof(status_msg));
  131. status_until = u_ticks_ms() + ms;
  132. if (!status_until)
  133. status_until = 1;
  134. }
  135. /* ---- buffer / cursor geometry ---- */
  136. static unsigned int cursor_line(void)
  137. {
  138. unsigned int i, line = 0;
  139. for (i = 0; i < cur; i++)
  140. if (text[i] == '\n')
  141. line++;
  142. return line;
  143. }
  144. static unsigned int line_start_for(unsigned int line)
  145. {
  146. unsigned int i, l = 0, start = 0;
  147. if (line == 0)
  148. return 0;
  149. for (i = 0; i < len; i++) {
  150. if (text[i] == '\n') {
  151. l++;
  152. start = i + 1;
  153. if (l == line)
  154. return start;
  155. }
  156. }
  157. return start;
  158. }
  159. static unsigned int line_end_from(unsigned int start)
  160. {
  161. unsigned int e = start;
  162. while (e < len && text[e] != '\n')
  163. e++;
  164. return e;
  165. }
  166. static unsigned int display_col_at(unsigned int start, unsigned int pos)
  167. {
  168. unsigned int i, d = 0;
  169. for (i = start; i < pos && i < len; i++) {
  170. if (text[i] == '\t')
  171. d = (d / TAB_WIDTH + 1) * TAB_WIDTH;
  172. else
  173. d++;
  174. }
  175. return d;
  176. }
  177. static void insert_char(char c)
  178. {
  179. unsigned int i;
  180. if (len >= EDIT_MAX) {
  181. set_msg("Buffer full (16 KB max)", 1500);
  182. return;
  183. }
  184. i = len;
  185. while (i > cur) {
  186. text[i] = text[i - 1];
  187. i--;
  188. }
  189. text[cur] = c;
  190. cur++;
  191. len++;
  192. dirty = 1;
  193. }
  194. static void delete_char(void)
  195. {
  196. unsigned int i;
  197. if (cur == 0)
  198. return;
  199. i = cur - 1;
  200. while (i + 1 < len) {
  201. text[i] = text[i + 1];
  202. i++;
  203. }
  204. cur--;
  205. len--;
  206. dirty = 1;
  207. }
  208. static void move_vertical(int dir)
  209. {
  210. unsigned int line = cursor_line();
  211. unsigned int start = line_start_for(line);
  212. unsigned int col = cur - start;
  213. unsigned int target, target_end;
  214. if (dir < 0) {
  215. if (line == 0)
  216. return;
  217. target = line_start_for(line - 1);
  218. } else {
  219. unsigned int end = line_end_from(start);
  220. if (end >= len)
  221. return; /* already on the last line */
  222. target = end + 1;
  223. }
  224. target_end = line_end_from(target);
  225. if (target_end - target < col)
  226. col = target_end - target;
  227. cur = target + col;
  228. }
  229. /* ---- file handling ---- */
  230. static int parse_arg(const char *arg)
  231. {
  232. unsigned int i = 0, n = 0, k, b = 0;
  233. if (!arg)
  234. return -1;
  235. while (arg[i] == ' ')
  236. i++;
  237. while (arg[i] && arg[i] != ' ' && n + 1 < sizeof(path))
  238. path[n++] = arg[i++];
  239. path[n] = '\0';
  240. if (n == 0)
  241. return -1;
  242. if (arg[i] && arg[i] != ' ')
  243. return -1; /* filename does not fit */
  244. for (k = 0; path[k]; k++)
  245. if (path[k] == '/')
  246. b = k + 1;
  247. if (path[b] == '\0' && b > 0)
  248. b = 0; /* trailing slash: show the whole path */
  249. for (k = 0; path[b + k] && k + 1 < sizeof(base_name); k++)
  250. base_name[k] = path[b + k];
  251. base_name[k] = '\0';
  252. return 0;
  253. }
  254. /* 0 = loaded, 1 = new file (does not exist), -2 = too large, other <0 = err */
  255. static int load_file(void)
  256. {
  257. int n = u_syscall3(SYS_READ_FILE, (int)path, (int)text, EDIT_READ_MAX);
  258. unsigned int i, j;
  259. if (n >= 0) {
  260. if ((unsigned int)n >= EDIT_READ_MAX)
  261. return -2;
  262. len = (unsigned int)n;
  263. j = 0;
  264. for (i = 0; i < len; i++) {
  265. if (text[i] == '\r') {
  266. if (i + 1 < len && text[i + 1] == '\n')
  267. continue;
  268. text[j++] = '\n';
  269. } else {
  270. text[j++] = text[i];
  271. }
  272. }
  273. len = j;
  274. return 0;
  275. }
  276. if (n == SYS_ENOENT) {
  277. len = 0;
  278. return 1;
  279. }
  280. return n;
  281. }
  282. static const char *read_error(int r)
  283. {
  284. switch (r) {
  285. case SYS_ENODEV:
  286. return "edit: no such device\n";
  287. case SYS_ENOFS:
  288. return "edit: no filesystem on device\n";
  289. case SYS_EIO:
  290. return "edit: disk read error\n";
  291. case SYS_EINVAL:
  292. return "edit: invalid path\n";
  293. default:
  294. return "edit: cannot read file\n";
  295. }
  296. }
  297. static void save_file(void)
  298. {
  299. int r = u_syscall3(SYS_WRITE_FILE, (int)path, (int)text, (int)len);
  300. if (r == 0) {
  301. dirty = 0;
  302. set_msg("Saved", 1500);
  303. return;
  304. }
  305. switch (r) {
  306. case SYS_ENODEV:
  307. set_msg("Save failed: no such device", 2500);
  308. break;
  309. case SYS_ENOFS:
  310. set_msg("Save failed: no filesystem on device", 2500);
  311. break;
  312. case SYS_ENOENT:
  313. set_msg("Save failed: path not found", 2500);
  314. break;
  315. case SYS_EROFS:
  316. set_msg("Save failed: read-only filesystem", 2500);
  317. break;
  318. case SYS_EIO:
  319. set_msg("Save failed: disk write error", 2500);
  320. break;
  321. case SYS_EWRITE:
  322. set_msg("Save failed: disk full or refused", 2500);
  323. break;
  324. case SYS_EINVAL:
  325. set_msg("Save failed: invalid path", 2500);
  326. break;
  327. default:
  328. set_msg("Save failed", 2500);
  329. break;
  330. }
  331. }
  332. /* ---- rendering ---- */
  333. static void draw_text_area(void)
  334. {
  335. unsigned int row, pos = 0, i;
  336. fill_rect(0, 0, fb_w, text_rows * CELL_H, col_bg);
  337. /* Skip the lines above the viewport. */
  338. for (i = 0; i < scroll_top && pos < len; i++) {
  339. while (pos < len && text[pos] != '\n')
  340. pos++;
  341. if (pos < len)
  342. pos++;
  343. }
  344. for (row = 0; row < text_rows; row++) {
  345. unsigned int start = pos, end, disp = 0, col = 0, p;
  346. while (pos < len && text[pos] != '\n')
  347. pos++;
  348. end = pos;
  349. if (pos < len)
  350. pos++;
  351. for (p = start; p < end && col < cols; p++) {
  352. char c = text[p];
  353. if (c == '\t') {
  354. unsigned int next = (disp / TAB_WIDTH + 1) * TAB_WIDTH;
  355. while (disp < next && col < cols) {
  356. if (disp >= xoff)
  357. col++;
  358. disp++;
  359. }
  360. } else {
  361. if (disp >= xoff) {
  362. draw_glyph_at(col * CELL_W, row * CELL_H,
  363. (unsigned char)c, col_fg);
  364. col++;
  365. }
  366. disp++;
  367. }
  368. }
  369. }
  370. }
  371. static void draw_cursor(unsigned int line, unsigned int disp)
  372. {
  373. unsigned int row, col;
  374. if (line < scroll_top || disp < xoff)
  375. return;
  376. row = line - scroll_top;
  377. col = disp - xoff;
  378. if (row >= text_rows || col >= cols)
  379. return;
  380. fill_rect(col * CELL_W, row * CELL_H, CELL_W, CELL_H, col_cursor);
  381. if (cur < len && text[cur] != '\n' && text[cur] != '\t')
  382. draw_glyph_at(col * CELL_W, row * CELL_H,
  383. (unsigned char)text[cur], col_cursor_fg);
  384. }
  385. static void draw_status(unsigned int line, unsigned int disp)
  386. {
  387. char left[STATUS_MAX], right[48];
  388. unsigned int n, rn, max_left, x, y;
  389. const char *msg;
  390. if (quit_armed)
  391. msg = "Unsaved changes - Ctrl+Q again to discard, ESC to cancel";
  392. else if (status_until)
  393. msg = status_msg;
  394. else
  395. msg = "^S Save ^Q Quit Arrows move Enter newline Backspace delete";
  396. y = text_rows * CELL_H;
  397. fill_rect(0, y, fb_w, CELL_H, col_hint_bg);
  398. draw_string(0, y, msg, cols, col_hint_fg);
  399. y += CELL_H;
  400. fill_rect(0, y, fb_w, CELL_H, col_status_bg);
  401. n = append_str(left, 0, base_name, sizeof(left));
  402. if (dirty)
  403. n = append_str(left, n, " *", sizeof(left));
  404. rn = append_str(right, 0, "Ln ", sizeof(right));
  405. rn = append_uint(right, rn, line + 1, sizeof(right));
  406. rn = append_str(right, rn, " Col ", sizeof(right));
  407. rn = append_uint(right, rn, disp + 1, sizeof(right));
  408. if (rn >= cols) {
  409. draw_string(0, y, right, cols, col_status_fg);
  410. return;
  411. }
  412. max_left = cols - rn - 1;
  413. if (n > max_left)
  414. n = max_left;
  415. draw_string(0, y, left, n, col_status_fg);
  416. x = (cols - rn) * CELL_W;
  417. draw_string(x, y, right, rn, col_status_fg);
  418. }
  419. static unsigned int total_lines(void)
  420. {
  421. unsigned int i, n = 1;
  422. for (i = 0; i < len; i++)
  423. if (text[i] == '\n')
  424. n++;
  425. return n;
  426. }
  427. static int handle_scroll(void)
  428. {
  429. struct win_scroll ws;
  430. unsigned int total, max_top;
  431. int lines, top;
  432. if (u_win_scroll(&ws) != 1 || !ws.dy)
  433. return 0;
  434. scroll_acc += ws.dy;
  435. lines = scroll_acc / CELL_H;
  436. scroll_acc -= lines * CELL_H;
  437. if (!lines)
  438. return 0;
  439. total = total_lines();
  440. max_top = total > text_rows ? total - text_rows : 0;
  441. top = (int)scroll_top + lines;
  442. if (top < 0)
  443. top = 0;
  444. if ((unsigned int)top > max_top)
  445. top = (int)max_top;
  446. if ((unsigned int)top == scroll_top)
  447. return 0;
  448. scroll_top = (unsigned int)top;
  449. view_locked = 1;
  450. return 1;
  451. }
  452. static void redraw(void)
  453. {
  454. unsigned int line = cursor_line();
  455. unsigned int start = line_start_for(line);
  456. unsigned int disp = display_col_at(start, cur);
  457. if (!view_locked) {
  458. if (line < scroll_top)
  459. scroll_top = line;
  460. else if (line >= scroll_top + text_rows)
  461. scroll_top = line - text_rows + 1;
  462. }
  463. if (disp < xoff)
  464. xoff = disp;
  465. else if (disp >= xoff + cols)
  466. xoff = disp - cols + 1;
  467. draw_text_area();
  468. draw_cursor(line, disp);
  469. draw_status(line, disp);
  470. u_win_present();
  471. }
  472. /* ---- input ---- */
  473. /* 1 = quit, 0 = keep editing */
  474. static int handle_key(int k)
  475. {
  476. view_locked = 0;
  477. if (quit_armed) {
  478. if (k == KEY_CTRL_Q)
  479. return 1;
  480. if (k == KEY_ESC) {
  481. quit_armed = 0;
  482. return 0;
  483. }
  484. quit_armed = 0;
  485. }
  486. if (k == KEY_CTRL_Q) {
  487. if (dirty) {
  488. quit_armed = 1;
  489. status_until = 0;
  490. return 0;
  491. }
  492. return 1;
  493. }
  494. switch (k) {
  495. case KEY_CTRL_S:
  496. save_file();
  497. break;
  498. case KEY_LEFT:
  499. if (cur > 0)
  500. cur--;
  501. break;
  502. case KEY_RIGHT:
  503. if (cur < len)
  504. cur++;
  505. break;
  506. case KEY_UP:
  507. move_vertical(-1);
  508. break;
  509. case KEY_DOWN:
  510. move_vertical(1);
  511. break;
  512. case KEY_BACKSPACE:
  513. case 0x7F:
  514. delete_char();
  515. break;
  516. case '\n':
  517. case '\r':
  518. insert_char('\n');
  519. break;
  520. case '\t':
  521. insert_char('\t');
  522. break;
  523. case KEY_ESC:
  524. status_until = 0;
  525. break;
  526. default:
  527. if (k >= 0x20 && k <= 0xFF)
  528. insert_char((char)k);
  529. break;
  530. }
  531. return 0;
  532. }
  533. void _start(const char *arg)
  534. {
  535. struct win_info info;
  536. int r, new_file;
  537. if (parse_arg(arg) != 0) {
  538. u_puts("edit: usage edit <filename>\n");
  539. u_exit(1);
  540. }
  541. r = load_file();
  542. if (r == -2) {
  543. u_puts("edit: file too large (max 16 KB)\n");
  544. u_exit(1);
  545. }
  546. if (r < 0) {
  547. u_puts(read_error(r));
  548. u_exit(1);
  549. }
  550. new_file = (r == 1);
  551. if (u_win_create(512, 512, "edit", &info) != 0) {
  552. u_puts("edit: no graphics window\n");
  553. u_exit(1);
  554. }
  555. fb = (unsigned short *)info.fb;
  556. fb_w = info.width;
  557. fb_h = info.height;
  558. cols = fb_w / CELL_W;
  559. rows = fb_h / CELL_H;
  560. if (cols == 0 || rows < 2) {
  561. u_win_close();
  562. u_puts("edit: window too small\n");
  563. u_exit(1);
  564. }
  565. text_rows = rows - 2;
  566. cur = 0;
  567. scroll_top = 0;
  568. scroll_acc = 0;
  569. view_locked = 0;
  570. xoff = 0;
  571. dirty = 0;
  572. quit_armed = 0;
  573. status_until = 0;
  574. status_msg[0] = '\0';
  575. if (new_file)
  576. set_msg("New file - Ctrl+S saves", 2000);
  577. redraw();
  578. for (;;) {
  579. struct win_info cur;
  580. int quit = 0, got = 0, k;
  581. unsigned int now;
  582. if (u_win_info(&cur) == 0 &&
  583. (cur.width != fb_w || cur.height != fb_h)) {
  584. if (u_win_ack_resize() == 0) {
  585. fb = (unsigned short *)cur.fb;
  586. fb_w = cur.width;
  587. fb_h = cur.height;
  588. cols = fb_w / CELL_W;
  589. rows = fb_h / CELL_H;
  590. text_rows = (rows > 2) ? rows - 2 : 1;
  591. view_locked = 0;
  592. got = 1;
  593. }
  594. }
  595. if (handle_scroll())
  596. got = 1;
  597. while ((k = u_key_poll()) != 0) {
  598. got = 1;
  599. if (handle_key(k)) {
  600. quit = 1;
  601. break;
  602. }
  603. }
  604. if (quit)
  605. break;
  606. now = u_ticks_ms();
  607. if (status_until && (int)(now - status_until) >= 0) {
  608. status_until = 0;
  609. got = 1;
  610. }
  611. if (got)
  612. redraw();
  613. u_sleep_ms(15);
  614. }
  615. u_win_close();
  616. u_exit(0);
  617. }