terminal.c 9.5 KB

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