terminal.c 11 KB

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