terminal.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 disk_set_drive(unsigned int drive);
  11. extern unsigned int disk_get_drive(void);
  12. #define DATETIME_MS 1000
  13. #define LINE_MAX 64
  14. static unsigned int last_datetime_tick;
  15. static char datetime_buf[IO_DATETIME_LEN + 1];
  16. static int cursor_visible;
  17. static unsigned int last_cursor_tick;
  18. static void redraw_datetime(void)
  19. {
  20. rtc_read_datetime(datetime_buf);
  21. {
  22. unsigned int i;
  23. for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
  24. io_vga_write(IO_DATETIME_POS + i,
  25. (unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
  26. }
  27. }
  28. static void update_datetime(void)
  29. {
  30. unsigned int t = get_ticks_ms();
  31. if (t - last_datetime_tick >= DATETIME_MS) {
  32. last_datetime_tick = t;
  33. redraw_datetime();
  34. }
  35. }
  36. static void update_cursor_blink(void)
  37. {
  38. unsigned int t = get_ticks_ms();
  39. if (t - last_cursor_tick >= CURSOR_MS) {
  40. last_cursor_tick = t;
  41. cursor_visible = !cursor_visible;
  42. }
  43. if (cursor_visible)
  44. io_cursor_show_block();
  45. else
  46. io_cursor_hide_block();
  47. }
  48. static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
  49. #define BANNER_LEN 12
  50. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  51. static const char prompt_first[] __attribute__((section(".text"))) = "> ";
  52. /* Draw row 0: banner left, datetime right. Call when terminal is active. */
  53. static void draw_header_row(void)
  54. {
  55. unsigned int i;
  56. for (i = 0; i < BANNER_LEN && banner[i]; i++)
  57. io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
  58. for (; i < IO_DATETIME_POS; i++)
  59. io_vga_write(i, (unsigned short)(0x0A << 8));
  60. redraw_datetime();
  61. }
  62. static int is_cmd_hello(const char *line, unsigned int line_len)
  63. {
  64. unsigned int i = 0;
  65. while (i < line_len && line[i] == ' ')
  66. i++;
  67. if (i + 5 > line_len)
  68. return 0;
  69. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  70. return 0;
  71. i += 5;
  72. while (i < line_len && line[i] == ' ')
  73. i++;
  74. return (i == line_len);
  75. }
  76. static int is_cmd_reboot(const char *line, unsigned int line_len)
  77. {
  78. unsigned int i = 0;
  79. while (i < line_len && line[i] == ' ')
  80. i++;
  81. if (i + 6 > line_len)
  82. return 0;
  83. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  84. return 0;
  85. i += 6;
  86. while (i < line_len && line[i] == ' ')
  87. i++;
  88. return (i == line_len);
  89. }
  90. static int is_cmd_ls(const char *line, unsigned int line_len)
  91. {
  92. unsigned int i = 0;
  93. while (i < line_len && line[i] == ' ')
  94. i++;
  95. if (i + 2 > line_len)
  96. return 0;
  97. if (line[i] != 'l' || line[i+1] != 's')
  98. return 0;
  99. i += 2;
  100. while (i < line_len && line[i] == ' ')
  101. i++;
  102. return (i == line_len);
  103. }
  104. #define CAT_FNAME_MAX 64
  105. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  106. {
  107. unsigned int i = 0, j;
  108. while (i < line_len && line[i] == ' ')
  109. i++;
  110. if (i + 3 > line_len)
  111. return 0;
  112. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  113. return 0;
  114. i += 3;
  115. while (i < line_len && line[i] == ' ')
  116. i++;
  117. if (i >= line_len)
  118. return 0;
  119. j = 0;
  120. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  121. filename_buf[j++] = line[i++];
  122. filename_buf[j] = '\0';
  123. return (j > 0);
  124. }
  125. static int is_cmd_drive(const char *line, unsigned int line_len, unsigned int *drive_num)
  126. {
  127. unsigned int i = 0;
  128. while (i < line_len && line[i] == ' ')
  129. i++;
  130. if (i + 5 > line_len)
  131. return 0;
  132. if (line[i] != 'd' || line[i+1] != 'r' || line[i+2] != 'i' || line[i+3] != 'v' || line[i+4] != 'e')
  133. return 0;
  134. i += 5;
  135. while (i < line_len && line[i] == ' ')
  136. i++;
  137. if (i >= line_len || line[i] < '0' || line[i] > '1')
  138. return 0;
  139. *drive_num = (unsigned int)(line[i] - '0');
  140. i++;
  141. while (i < line_len && line[i] == ' ')
  142. i++;
  143. return (i == line_len);
  144. }
  145. void terminal_run(void)
  146. {
  147. static char line[LINE_MAX];
  148. static unsigned int line_len;
  149. last_cursor_tick = get_ticks_ms();
  150. last_datetime_tick = get_ticks_ms();
  151. cursor_visible = 1;
  152. line_len = 0;
  153. line[0] = '\0';
  154. io_drain_keyboard();
  155. draw_header_row();
  156. io_putstr(prompt_first);
  157. for (;;) {
  158. update_datetime();
  159. update_cursor_blink();
  160. {
  161. char c = io_poll_key();
  162. if (c) {
  163. if (c == '\b') {
  164. if (line_len > 0) {
  165. line_len--;
  166. line[line_len] = '\0';
  167. io_putchar(c);
  168. }
  169. } else if (c == '\n') {
  170. static char cat_fname[CAT_FNAME_MAX];
  171. unsigned int drive_sel;
  172. io_putchar(c);
  173. if (is_cmd_hello(line, line_len)) {
  174. program_hello_main();
  175. io_drain_keyboard();
  176. draw_header_row();
  177. } else if (is_cmd_reboot(line, line_len)) {
  178. program_reboot_main();
  179. } else if (is_cmd_ls(line, line_len)) {
  180. program_ls_main();
  181. io_drain_keyboard();
  182. draw_header_row();
  183. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  184. program_cat_main(cat_fname);
  185. io_drain_keyboard();
  186. draw_header_row();
  187. } else if (is_cmd_drive(line, line_len, &drive_sel)) {
  188. disk_set_drive(drive_sel);
  189. io_putstr("drive ");
  190. io_putchar((char)('0' + disk_get_drive()));
  191. io_putstr(" selected\n");
  192. }
  193. line_len = 0;
  194. line[0] = '\0';
  195. io_putstr(prompt);
  196. if (io_did_scroll())
  197. redraw_datetime();
  198. } else {
  199. {
  200. unsigned char u = (unsigned char)c;
  201. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  202. line[line_len++] = c;
  203. line[line_len] = '\0';
  204. }
  205. }
  206. io_putchar(c);
  207. }
  208. } else {
  209. __asm__ volatile ("hlt");
  210. }
  211. }
  212. }
  213. }