ucmd_edit.c 14 KB

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