terminal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. /* Copy history entry (1 = newest) into line, set line_len/line_pos, redraw from start. */
  276. static void history_load_into_line(char *line, unsigned int *line_len, unsigned int *line_pos,
  277. unsigned int line_start, unsigned int browse)
  278. {
  279. unsigned int slot, len;
  280. const char *src;
  281. if (browse < 1 || browse > history_count)
  282. return;
  283. slot = (history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  284. src = history[slot];
  285. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  286. line[len] = src[len];
  287. line[len] = '\0';
  288. *line_len = len;
  289. *line_pos = len;
  290. redraw_line_from(line, len, len, line_start, 0);
  291. }
  292. /* Push current line to history if non-empty (and not duplicate of newest). */
  293. static void history_push(const char *line, unsigned int line_len)
  294. {
  295. unsigned int i, slot;
  296. int all_spaces = 1;
  297. if (line_len == 0)
  298. return;
  299. for (i = 0; i < line_len; i++) {
  300. if (line[i] != ' ') {
  301. all_spaces = 0;
  302. break;
  303. }
  304. }
  305. if (all_spaces)
  306. return;
  307. if (history_count > 0) {
  308. slot = (history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  309. for (i = 0; i < line_len && i < LINE_MAX - 1 && history[slot][i]; i++) {
  310. if (history[slot][i] != line[i])
  311. break;
  312. }
  313. if (i == line_len && history[slot][i] == '\0')
  314. return; /* duplicate of newest */
  315. }
  316. slot = history_next;
  317. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  318. history[slot][i] = line[i];
  319. history[slot][i] = '\0';
  320. history_next = (history_next + 1) % HISTORY_SIZE;
  321. if (history_count < HISTORY_SIZE)
  322. history_count++;
  323. }
  324. void terminal_run(void)
  325. {
  326. static char line[LINE_MAX];
  327. static unsigned int line_len;
  328. static unsigned int line_pos; /* cursor index in line, 0..line_len */
  329. static unsigned int line_start; /* VGA position of first char of line */
  330. static unsigned int history_browse; /* 0 = not browsing, 1 = newest, 2 = older, ... */
  331. last_cursor_tick = get_ticks_ms();
  332. last_datetime_tick = get_ticks_ms();
  333. cursor_visible = 1;
  334. line_len = 0;
  335. line_pos = 0;
  336. line[0] = '\0';
  337. history_browse = 0;
  338. io_drain_keyboard();
  339. draw_header_row();
  340. io_putstr(prompt_first);
  341. line_start = io_get_cursor();
  342. for (;;) {
  343. update_datetime();
  344. update_cursor_blink();
  345. {
  346. int k = io_poll_key();
  347. if (k) {
  348. if (k == IO_KEY_LEFT) {
  349. if (line_pos > 0) {
  350. line_pos--;
  351. io_set_cursor(line_start + line_pos);
  352. }
  353. } else if (k == IO_KEY_RIGHT) {
  354. if (line_pos < line_len) {
  355. line_pos++;
  356. io_set_cursor(line_start + line_pos);
  357. }
  358. } else if (k == IO_KEY_UP) {
  359. if (history_count > 0) {
  360. if (history_browse < history_count)
  361. history_browse++;
  362. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  363. }
  364. } else if (k == IO_KEY_DOWN) {
  365. if (history_browse > 0) {
  366. history_browse--;
  367. if (history_browse == 0) {
  368. line_len = 0;
  369. line_pos = 0;
  370. line[0] = '\0';
  371. redraw_line_from(line, 0, 0, line_start, 0);
  372. } else {
  373. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  374. }
  375. }
  376. } else if (k == '\b') {
  377. if (line_pos > 0) {
  378. unsigned int i;
  379. line_pos--;
  380. for (i = line_pos; i < line_len; i++)
  381. line[i] = line[i + 1];
  382. line_len--;
  383. line[line_len] = '\0';
  384. redraw_line_from(line, line_len, line_pos, line_start, line_pos);
  385. }
  386. } else if (k == '\n') {
  387. static char cat_fname[CAT_FNAME_MAX];
  388. unsigned int drive_sel;
  389. int skip_prompt = 0;
  390. io_putchar('\n');
  391. history_push(line, line_len);
  392. history_browse = 0;
  393. if (is_cmd_hello(line, line_len)) {
  394. program_hello_main();
  395. io_drain_keyboard();
  396. draw_header_row();
  397. } else if (is_cmd_reboot(line, line_len)) {
  398. program_reboot_main();
  399. } else if (is_cmd_ls(line, line_len)) {
  400. program_ls_main();
  401. io_drain_keyboard();
  402. draw_header_row();
  403. } else if (is_cmd_clock(line, line_len)) {
  404. program_clock_main();
  405. io_drain_keyboard();
  406. io_clear_screen();
  407. draw_header_row();
  408. } else if (is_cmd_colors(line, line_len)) {
  409. program_colors_main();
  410. io_drain_keyboard();
  411. io_clear_screen();
  412. draw_header_row();
  413. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  414. program_cat_main(cat_fname);
  415. io_drain_keyboard();
  416. draw_header_row();
  417. } else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  418. program_run_main(cat_fname);
  419. io_drain_keyboard();
  420. draw_header_row();
  421. } else if (is_cmd_space(line, line_len)) {
  422. program_run_main("space.bin");
  423. io_drain_keyboard();
  424. draw_header_row();
  425. } else if (is_cmd_display(line, line_len)) {
  426. program_display_main();
  427. } else if (is_cmd_drive(line, line_len, &drive_sel)) {
  428. disk_set_drive(drive_sel);
  429. io_putstr("drive ");
  430. io_putchar((char)('0' + disk_get_drive()));
  431. io_putstr(" selected\n");
  432. } else if (is_cmd_help(line, line_len)) {
  433. io_putstr("hello\n");
  434. io_putstr("reboot\n");
  435. io_putstr("ls\n");
  436. io_putstr("clock\n");
  437. io_putstr("colors\n");
  438. io_putstr("cat <file>\n");
  439. io_putstr("run <file>\n");
  440. io_putstr("space\n");
  441. io_putstr("display\n");
  442. io_putstr("drive <0|1>\n");
  443. io_putstr("help\n");
  444. io_putstr("clear\n");
  445. } else if (is_cmd_clear(line, line_len)) {
  446. io_clear_screen();
  447. draw_header_row();
  448. io_set_cursor(IO_CONTENT_OFFSET);
  449. io_putstr(prompt_first);
  450. line_start = io_get_cursor();
  451. skip_prompt = 1;
  452. } else {
  453. unsigned int i = 0;
  454. while (i < line_len && line[i] == ' ')
  455. i++;
  456. if (i < line_len)
  457. io_putstr("command not found\n");
  458. }
  459. line_len = 0;
  460. line_pos = 0;
  461. line[0] = '\0';
  462. if (!skip_prompt) {
  463. io_putstr(prompt);
  464. line_start = io_get_cursor();
  465. }
  466. if (io_did_scroll())
  467. redraw_datetime();
  468. } else {
  469. {
  470. unsigned char u = (unsigned char)k;
  471. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  472. unsigned int i;
  473. history_browse = 0;
  474. for (i = line_len; i > line_pos; i--)
  475. line[i] = line[i - 1];
  476. line[line_pos] = (char)k;
  477. line_len++;
  478. line[line_len] = '\0';
  479. line_pos++;
  480. redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
  481. }
  482. }
  483. }
  484. } else {
  485. __asm__ volatile ("hlt");
  486. }
  487. }
  488. }
  489. }