terminal.c 14 KB

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