terminal.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  192. static void redraw_line_from(const char *line, unsigned int line_len,
  193. unsigned int line_pos, unsigned int line_start, unsigned int from)
  194. {
  195. unsigned int i;
  196. io_set_cursor(line_start + from);
  197. for (i = from; i < line_len; i++)
  198. io_putchar(line[i]);
  199. io_putchar(' ');
  200. io_set_cursor(line_start + line_pos);
  201. }
  202. void terminal_run(void)
  203. {
  204. static char line[LINE_MAX];
  205. static unsigned int line_len;
  206. static unsigned int line_pos; /* cursor index in line, 0..line_len */
  207. static unsigned int line_start; /* VGA position of first char of line */
  208. last_cursor_tick = get_ticks_ms();
  209. last_datetime_tick = get_ticks_ms();
  210. cursor_visible = 1;
  211. line_len = 0;
  212. line_pos = 0;
  213. line[0] = '\0';
  214. io_drain_keyboard();
  215. draw_header_row();
  216. io_putstr(prompt_first);
  217. line_start = io_get_cursor();
  218. for (;;) {
  219. update_datetime();
  220. update_cursor_blink();
  221. {
  222. int k = io_poll_key();
  223. if (k) {
  224. if (k == IO_KEY_LEFT) {
  225. if (line_pos > 0) {
  226. line_pos--;
  227. io_set_cursor(line_start + line_pos);
  228. }
  229. } else if (k == IO_KEY_RIGHT) {
  230. if (line_pos < line_len) {
  231. line_pos++;
  232. io_set_cursor(line_start + line_pos);
  233. }
  234. } else if (k == '\b') {
  235. if (line_pos > 0) {
  236. unsigned int i;
  237. line_pos--;
  238. for (i = line_pos; i < line_len; i++)
  239. line[i] = line[i + 1];
  240. line_len--;
  241. line[line_len] = '\0';
  242. redraw_line_from(line, line_len, line_pos, line_start, line_pos);
  243. }
  244. } else if (k == '\n') {
  245. static char cat_fname[CAT_FNAME_MAX];
  246. unsigned int drive_sel;
  247. io_putchar('\n');
  248. if (is_cmd_hello(line, line_len)) {
  249. program_hello_main();
  250. io_drain_keyboard();
  251. draw_header_row();
  252. } else if (is_cmd_reboot(line, line_len)) {
  253. program_reboot_main();
  254. } else if (is_cmd_ls(line, line_len)) {
  255. program_ls_main();
  256. io_drain_keyboard();
  257. draw_header_row();
  258. } else if (is_cmd_clock(line, line_len)) {
  259. program_clock_main();
  260. io_drain_keyboard();
  261. io_clear_screen();
  262. draw_header_row();
  263. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  264. program_cat_main(cat_fname);
  265. io_drain_keyboard();
  266. draw_header_row();
  267. } else if (is_cmd_display(line, line_len)) {
  268. program_display_main();
  269. } else if (is_cmd_drive(line, line_len, &drive_sel)) {
  270. disk_set_drive(drive_sel);
  271. io_putstr("drive ");
  272. io_putchar((char)('0' + disk_get_drive()));
  273. io_putstr(" selected\n");
  274. } else if (is_cmd_help(line, line_len)) {
  275. io_putstr("hello\n");
  276. io_putstr("reboot\n");
  277. io_putstr("ls\n");
  278. io_putstr("clock\n");
  279. io_putstr("cat <file>\n");
  280. io_putstr("display\n");
  281. io_putstr("drive <0|1>\n");
  282. io_putstr("help\n");
  283. } else {
  284. unsigned int i = 0;
  285. while (i < line_len && line[i] == ' ')
  286. i++;
  287. if (i < line_len)
  288. io_putstr("command not found\n");
  289. }
  290. line_len = 0;
  291. line_pos = 0;
  292. line[0] = '\0';
  293. io_putstr(prompt);
  294. line_start = io_get_cursor();
  295. if (io_did_scroll())
  296. redraw_datetime();
  297. } else {
  298. {
  299. unsigned char u = (unsigned char)k;
  300. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  301. unsigned int i;
  302. for (i = line_len; i > line_pos; i--)
  303. line[i] = line[i - 1];
  304. line[line_pos] = (char)k;
  305. line_len++;
  306. line[line_len] = '\0';
  307. line_pos++;
  308. redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
  309. }
  310. }
  311. }
  312. } else {
  313. __asm__ volatile ("hlt");
  314. }
  315. }
  316. }
  317. }