terminal.c 5.2 KB

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