terminal.c 8.2 KB

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