terminal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch. */
  2. #include "io.h"
  3. #include "fbtext.h"
  4. #include "rtc.h"
  5. #include "terminal.h"
  6. #include "timer.h"
  7. extern void program_hello_main(void);
  8. extern void program_reboot_main(void);
  9. extern void program_ls_main(void);
  10. extern void program_cat_main(const char *filename);
  11. extern void program_run_main(const char *filename);
  12. extern void program_load_main(void);
  13. extern void program_serial_test_main(void);
  14. extern void program_serial_loopback_main(void);
  15. extern void program_serial_checksum_main(void);
  16. extern void program_serial_trim_main(unsigned int first, unsigned int last);
  17. extern void program_clock_main(void);
  18. extern void program_colors_main(void);
  19. extern void program_display_main(void);
  20. extern void disk_set_drive(unsigned int drive);
  21. extern unsigned int disk_get_drive(void);
  22. #define DATETIME_MS 1000
  23. #define LINE_MAX 64
  24. static unsigned int last_datetime_tick;
  25. static char datetime_buf[IO_DATETIME_LEN + 1];
  26. static int cursor_visible;
  27. static unsigned int last_cursor_tick;
  28. static void redraw_datetime(void)
  29. {
  30. rtc_read_datetime(datetime_buf);
  31. {
  32. unsigned int i;
  33. for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
  34. io_vga_write(IO_DATETIME_POS + i,
  35. (unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
  36. }
  37. }
  38. static void update_datetime(void)
  39. {
  40. unsigned int t = get_ticks_ms();
  41. if (t - last_datetime_tick >= DATETIME_MS) {
  42. last_datetime_tick = t;
  43. redraw_datetime();
  44. }
  45. }
  46. static void update_cursor_blink(void)
  47. {
  48. unsigned int t = get_ticks_ms();
  49. if (t - last_cursor_tick >= CURSOR_MS) {
  50. last_cursor_tick = t;
  51. cursor_visible = !cursor_visible;
  52. }
  53. if (cursor_visible)
  54. io_cursor_show_block();
  55. else
  56. io_cursor_hide_block();
  57. }
  58. static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
  59. #define BANNER_LEN 12
  60. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  61. static const char prompt_first[] __attribute__((section(".text"))) = "> ";
  62. /* Draw row 0: banner left, datetime right. Call when terminal is active. */
  63. static void draw_header_row(void)
  64. {
  65. unsigned int i;
  66. for (i = 0; i < BANNER_LEN && banner[i]; i++)
  67. io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
  68. for (; i < IO_DATETIME_POS; i++)
  69. io_vga_write(i, (unsigned short)(0x0A << 8));
  70. redraw_datetime();
  71. fbtext_draw_rainbow_separator();
  72. }
  73. static int is_cmd_hello(const char *line, unsigned int line_len)
  74. {
  75. unsigned int i = 0;
  76. while (i < line_len && line[i] == ' ')
  77. i++;
  78. if (i + 5 > line_len)
  79. return 0;
  80. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  81. return 0;
  82. i += 5;
  83. while (i < line_len && line[i] == ' ')
  84. i++;
  85. return (i == line_len);
  86. }
  87. static int is_cmd_reboot(const char *line, unsigned int line_len)
  88. {
  89. unsigned int i = 0;
  90. while (i < line_len && line[i] == ' ')
  91. i++;
  92. if (i + 6 > line_len)
  93. return 0;
  94. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  95. return 0;
  96. i += 6;
  97. while (i < line_len && line[i] == ' ')
  98. i++;
  99. return (i == line_len);
  100. }
  101. static int is_cmd_ls(const char *line, unsigned int line_len)
  102. {
  103. unsigned int i = 0;
  104. while (i < line_len && line[i] == ' ')
  105. i++;
  106. if (i + 2 > line_len)
  107. return 0;
  108. if (line[i] != 'l' || line[i+1] != 's')
  109. return 0;
  110. i += 2;
  111. while (i < line_len && line[i] == ' ')
  112. i++;
  113. return (i == line_len);
  114. }
  115. static int is_cmd_clock(const char *line, unsigned int line_len)
  116. {
  117. unsigned int i = 0;
  118. while (i < line_len && line[i] == ' ')
  119. i++;
  120. if (i + 5 > line_len)
  121. return 0;
  122. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  123. line[i+3] != 'c' || line[i+4] != 'k')
  124. return 0;
  125. i += 5;
  126. while (i < line_len && line[i] == ' ')
  127. i++;
  128. return (i == line_len);
  129. }
  130. static int is_cmd_colors(const char *line, unsigned int line_len)
  131. {
  132. unsigned int i = 0;
  133. while (i < line_len && line[i] == ' ')
  134. i++;
  135. if (i + 6 > line_len)
  136. return 0;
  137. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  138. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  139. return 0;
  140. i += 6;
  141. while (i < line_len && line[i] == ' ')
  142. i++;
  143. return (i == line_len);
  144. }
  145. static int is_cmd_display(const char *line, unsigned int line_len)
  146. {
  147. unsigned int i = 0;
  148. while (i < line_len && line[i] == ' ')
  149. i++;
  150. if (i + 7 > line_len)
  151. return 0;
  152. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  153. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  154. return 0;
  155. i += 7;
  156. while (i < line_len && line[i] == ' ')
  157. i++;
  158. return (i == line_len);
  159. }
  160. static int is_cmd_space(const char *line, unsigned int line_len)
  161. {
  162. unsigned int i = 0;
  163. while (i < line_len && line[i] == ' ')
  164. i++;
  165. if (i + 5 > line_len)
  166. return 0;
  167. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  168. line[i+3] != 'c' || line[i+4] != 'e')
  169. return 0;
  170. i += 5;
  171. while (i < line_len && line[i] == ' ')
  172. i++;
  173. return (i == line_len);
  174. }
  175. #define CAT_FNAME_MAX 64
  176. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  177. {
  178. unsigned int i = 0, j;
  179. while (i < line_len && line[i] == ' ')
  180. i++;
  181. if (i + 3 > line_len)
  182. return 0;
  183. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  184. return 0;
  185. i += 3;
  186. while (i < line_len && line[i] == ' ')
  187. i++;
  188. if (i >= line_len)
  189. return 0;
  190. j = 0;
  191. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  192. filename_buf[j++] = line[i++];
  193. filename_buf[j] = '\0';
  194. return (j > 0);
  195. }
  196. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  197. {
  198. unsigned int i = 0, j;
  199. while (i < line_len && line[i] == ' ')
  200. i++;
  201. if (i + 3 > line_len)
  202. return 0;
  203. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  204. return 0;
  205. i += 3;
  206. while (i < line_len && line[i] == ' ')
  207. i++;
  208. if (i >= line_len)
  209. return 0;
  210. j = 0;
  211. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  212. filename_buf[j++] = line[i++];
  213. filename_buf[j] = '\0';
  214. return (j > 0);
  215. }
  216. static int is_cmd_drive(const char *line, unsigned int line_len, unsigned int *drive_num)
  217. {
  218. unsigned int i = 0;
  219. while (i < line_len && line[i] == ' ')
  220. i++;
  221. if (i + 5 > line_len)
  222. return 0;
  223. if (line[i] != 'd' || line[i+1] != 'r' || line[i+2] != 'i' || line[i+3] != 'v' || line[i+4] != 'e')
  224. return 0;
  225. i += 5;
  226. while (i < line_len && line[i] == ' ')
  227. i++;
  228. if (i >= line_len || line[i] < '0' || line[i] > '1')
  229. return 0;
  230. *drive_num = (unsigned int)(line[i] - '0');
  231. i++;
  232. while (i < line_len && line[i] == ' ')
  233. i++;
  234. return (i == line_len);
  235. }
  236. static int is_cmd_help(const char *line, unsigned int line_len)
  237. {
  238. unsigned int i = 0;
  239. while (i < line_len && line[i] == ' ')
  240. i++;
  241. if (i + 4 > line_len)
  242. return 0;
  243. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  244. return 0;
  245. i += 4;
  246. while (i < line_len && line[i] == ' ')
  247. i++;
  248. return (i == line_len);
  249. }
  250. static int is_cmd_load(const char *line, unsigned int line_len)
  251. {
  252. unsigned int i = 0;
  253. while (i < line_len && line[i] == ' ')
  254. i++;
  255. if (i + 4 > line_len)
  256. return 0;
  257. if (line[i] != 'l' || line[i+1] != 'o' || line[i+2] != 'a' || line[i+3] != 'd')
  258. return 0;
  259. i += 4;
  260. while (i < line_len && line[i] == ' ')
  261. i++;
  262. return (i == line_len);
  263. }
  264. static int is_cmd_serial_test(const char *line, unsigned int line_len)
  265. {
  266. unsigned int i = 0;
  267. while (i < line_len && line[i] == ' ')
  268. i++;
  269. if (i + 11 > line_len)
  270. return 0;
  271. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
  272. line[i+4] != 'a' || line[i+5] != 'l')
  273. return 0;
  274. i += 6;
  275. while (i < line_len && line[i] == ' ')
  276. i++;
  277. if (i + 4 > line_len)
  278. return 0;
  279. if (line[i] != 't' || line[i+1] != 'e' || line[i+2] != 's' || line[i+3] != 't')
  280. return 0;
  281. i += 4;
  282. while (i < line_len && line[i] == ' ')
  283. i++;
  284. return (i == line_len);
  285. }
  286. static int is_cmd_serial_loopback(const char *line, unsigned int line_len)
  287. {
  288. unsigned int i = 0;
  289. while (i < line_len && line[i] == ' ')
  290. i++;
  291. if (i + 14 > line_len)
  292. return 0;
  293. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
  294. line[i+4] != 'a' || line[i+5] != 'l')
  295. return 0;
  296. i += 6;
  297. while (i < line_len && line[i] == ' ')
  298. i++;
  299. if (i + 8 > line_len)
  300. return 0;
  301. if (line[i] != 'l' || line[i+1] != 'o' || line[i+2] != 'o' || line[i+3] != 'p' ||
  302. line[i+4] != 'b' || line[i+5] != 'a' || line[i+6] != 'c' || line[i+7] != 'k')
  303. return 0;
  304. i += 8;
  305. while (i < line_len && line[i] == ' ')
  306. i++;
  307. return (i == line_len);
  308. }
  309. static int is_cmd_serial_checksum(const char *line, unsigned int line_len)
  310. {
  311. unsigned int i = 0;
  312. while (i < line_len && line[i] == ' ')
  313. i++;
  314. if (i + 14 > line_len)
  315. return 0;
  316. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
  317. line[i+4] != 'a' || line[i+5] != 'l')
  318. return 0;
  319. i += 6;
  320. while (i < line_len && line[i] == ' ')
  321. i++;
  322. if (i + 8 > line_len)
  323. return 0;
  324. if (line[i] != 'c' || line[i+1] != 'h' || line[i+2] != 'e' || line[i+3] != 'c' ||
  325. line[i+4] != 'k' || line[i+5] != 's' || line[i+6] != 'u' || line[i+7] != 'm')
  326. return 0;
  327. i += 8;
  328. while (i < line_len && line[i] == ' ')
  329. i++;
  330. return (i == line_len);
  331. }
  332. static int is_cmd_serial_trim(const char *line, unsigned int line_len,
  333. unsigned int *first, unsigned int *last)
  334. {
  335. unsigned int i = 0;
  336. unsigned int a = 0, b = 0;
  337. while (i < line_len && line[i] == ' ')
  338. i++;
  339. if (i + 11 > line_len)
  340. return 0;
  341. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' || line[i+3] != 'i' ||
  342. line[i+4] != 'a' || line[i+5] != 'l')
  343. return 0;
  344. i += 6;
  345. while (i < line_len && line[i] == ' ')
  346. i++;
  347. if (i + 4 > line_len)
  348. return 0;
  349. if (line[i] != 't' || line[i+1] != 'r' || line[i+2] != 'i' || line[i+3] != 'm')
  350. return 0;
  351. i += 4;
  352. while (i < line_len && line[i] == ' ')
  353. i++;
  354. if (i >= line_len || line[i] < '0' || line[i] > '9')
  355. return 0;
  356. while (i < line_len && line[i] >= '0' && line[i] <= '9')
  357. a = a * 10 + (unsigned int)(line[i++] - '0');
  358. while (i < line_len && line[i] == ' ')
  359. i++;
  360. if (i >= line_len || line[i] < '0' || line[i] > '9')
  361. return 0;
  362. while (i < line_len && line[i] >= '0' && line[i] <= '9')
  363. b = b * 10 + (unsigned int)(line[i++] - '0');
  364. while (i < line_len && line[i] == ' ')
  365. i++;
  366. if (i != line_len)
  367. return 0;
  368. *first = a;
  369. *last = b;
  370. return 1;
  371. }
  372. static int is_cmd_clear(const char *line, unsigned int line_len)
  373. {
  374. unsigned int i = 0;
  375. while (i < line_len && line[i] == ' ')
  376. i++;
  377. if (i + 5 > line_len)
  378. return 0;
  379. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  380. line[i+3] != 'a' || line[i+4] != 'r')
  381. return 0;
  382. i += 5;
  383. while (i < line_len && line[i] == ' ')
  384. i++;
  385. return (i == line_len);
  386. }
  387. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  388. static void redraw_line_from(const char *line, unsigned int line_len,
  389. unsigned int line_pos, unsigned int line_start, unsigned int from)
  390. {
  391. unsigned int i;
  392. io_set_cursor(line_start + from);
  393. for (i = from; i < line_len; i++)
  394. io_putchar(line[i]);
  395. io_putchar(' ');
  396. io_set_cursor(line_start + line_pos);
  397. }
  398. void terminal_run(void)
  399. {
  400. static char line[LINE_MAX];
  401. static unsigned int line_len;
  402. static unsigned int line_pos; /* cursor index in line, 0..line_len */
  403. static unsigned int line_start; /* VGA position of first char of line */
  404. last_cursor_tick = get_ticks_ms();
  405. last_datetime_tick = get_ticks_ms();
  406. cursor_visible = 1;
  407. line_len = 0;
  408. line_pos = 0;
  409. line[0] = '\0';
  410. io_drain_keyboard();
  411. draw_header_row();
  412. io_putstr(prompt_first);
  413. line_start = io_get_cursor();
  414. for (;;) {
  415. update_datetime();
  416. update_cursor_blink();
  417. {
  418. int k = io_poll_key();
  419. if (k) {
  420. if (k == IO_KEY_LEFT) {
  421. if (line_pos > 0) {
  422. line_pos--;
  423. io_set_cursor(line_start + line_pos);
  424. }
  425. } else if (k == IO_KEY_RIGHT) {
  426. if (line_pos < line_len) {
  427. line_pos++;
  428. io_set_cursor(line_start + line_pos);
  429. }
  430. } else if (k == '\b') {
  431. if (line_pos > 0) {
  432. unsigned int i;
  433. line_pos--;
  434. for (i = line_pos; i < line_len; i++)
  435. line[i] = line[i + 1];
  436. line_len--;
  437. line[line_len] = '\0';
  438. redraw_line_from(line, line_len, line_pos, line_start, line_pos);
  439. }
  440. } else if (k == '\n') {
  441. static char cat_fname[CAT_FNAME_MAX];
  442. unsigned int drive_sel;
  443. unsigned int trim_first, trim_last;
  444. int skip_prompt = 0;
  445. io_putchar('\n');
  446. if (is_cmd_hello(line, line_len)) {
  447. program_hello_main();
  448. io_drain_keyboard();
  449. draw_header_row();
  450. } else if (is_cmd_reboot(line, line_len)) {
  451. program_reboot_main();
  452. } else if (is_cmd_ls(line, line_len)) {
  453. program_ls_main();
  454. io_drain_keyboard();
  455. draw_header_row();
  456. } else if (is_cmd_clock(line, line_len)) {
  457. program_clock_main();
  458. io_drain_keyboard();
  459. io_clear_screen();
  460. draw_header_row();
  461. } else if (is_cmd_colors(line, line_len)) {
  462. program_colors_main();
  463. io_drain_keyboard();
  464. io_clear_screen();
  465. draw_header_row();
  466. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  467. program_cat_main(cat_fname);
  468. io_drain_keyboard();
  469. draw_header_row();
  470. } else if (is_cmd_load(line, line_len)) {
  471. program_load_main();
  472. io_drain_keyboard();
  473. draw_header_row();
  474. } else if (is_cmd_serial_test(line, line_len)) {
  475. program_serial_test_main();
  476. io_drain_keyboard();
  477. draw_header_row();
  478. } else if (is_cmd_serial_loopback(line, line_len)) {
  479. program_serial_loopback_main();
  480. io_drain_keyboard();
  481. draw_header_row();
  482. } else if (is_cmd_serial_checksum(line, line_len)) {
  483. program_serial_checksum_main();
  484. io_drain_keyboard();
  485. draw_header_row();
  486. } else if (is_cmd_serial_trim(line, line_len, &trim_first, &trim_last)) {
  487. program_serial_trim_main(trim_first, trim_last);
  488. io_drain_keyboard();
  489. draw_header_row();
  490. } else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  491. program_run_main(cat_fname);
  492. io_drain_keyboard();
  493. draw_header_row();
  494. } else if (is_cmd_space(line, line_len)) {
  495. program_run_main("space.bin");
  496. io_drain_keyboard();
  497. draw_header_row();
  498. } else if (is_cmd_display(line, line_len)) {
  499. program_display_main();
  500. } else if (is_cmd_drive(line, line_len, &drive_sel)) {
  501. disk_set_drive(drive_sel);
  502. io_putstr("drive ");
  503. io_putchar((char)('0' + disk_get_drive()));
  504. io_putstr(" selected\n");
  505. } else if (is_cmd_help(line, line_len)) {
  506. io_putstr("hello\n");
  507. io_putstr("reboot\n");
  508. io_putstr("ls\n");
  509. io_putstr("clock\n");
  510. io_putstr("colors\n");
  511. io_putstr("cat <file>\n");
  512. io_putstr("run <file>\n");
  513. io_putstr("load\n");
  514. io_putstr("serial test\n");
  515. io_putstr("serial loopback\n");
  516. io_putstr("serial checksum\n");
  517. io_putstr("serial trim <first> <last>\n");
  518. io_putstr("space\n");
  519. io_putstr("display\n");
  520. io_putstr("drive <0|1>\n");
  521. io_putstr("help\n");
  522. io_putstr("clear\n");
  523. } else if (is_cmd_clear(line, line_len)) {
  524. io_clear_screen();
  525. draw_header_row();
  526. io_set_cursor(IO_CONTENT_OFFSET);
  527. io_putstr(prompt_first);
  528. line_start = io_get_cursor();
  529. skip_prompt = 1;
  530. } else {
  531. unsigned int i = 0;
  532. while (i < line_len && line[i] == ' ')
  533. i++;
  534. if (i < line_len)
  535. io_putstr("command not found\n");
  536. }
  537. line_len = 0;
  538. line_pos = 0;
  539. line[0] = '\0';
  540. if (!skip_prompt) {
  541. io_putstr(prompt);
  542. line_start = io_get_cursor();
  543. }
  544. if (io_did_scroll())
  545. redraw_datetime();
  546. } else {
  547. {
  548. unsigned char u = (unsigned char)k;
  549. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  550. unsigned int i;
  551. for (i = line_len; i > line_pos; i--)
  552. line[i] = line[i - 1];
  553. line[line_pos] = (char)k;
  554. line_len++;
  555. line[line_len] = '\0';
  556. line_pos++;
  557. redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
  558. }
  559. }
  560. }
  561. } else {
  562. __asm__ volatile ("hlt");
  563. }
  564. }
  565. }
  566. }