terminal.c 7.6 KB

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