terminal.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch. */
  2. #include "io.h"
  3. #include "fbtext.h"
  4. #include "rtc.h"
  5. #include "terminal.h"
  6. #include "timer.h"
  7. extern void program_hello_main(void);
  8. extern void program_reboot_main(void);
  9. extern void program_ls_main(void);
  10. extern void program_cat_main(const char *filename);
  11. extern void program_clock_main(void);
  12. extern void program_display_main(void);
  13. extern void disk_set_drive(unsigned int drive);
  14. extern unsigned int disk_get_drive(void);
  15. #define DATETIME_MS 1000
  16. #define LINE_MAX 64
  17. static unsigned int last_datetime_tick;
  18. static char datetime_buf[IO_DATETIME_LEN + 1];
  19. static int cursor_visible;
  20. static unsigned int last_cursor_tick;
  21. static void redraw_datetime(void)
  22. {
  23. rtc_read_datetime(datetime_buf);
  24. {
  25. unsigned int i;
  26. for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
  27. io_vga_write(IO_DATETIME_POS + i,
  28. (unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
  29. }
  30. }
  31. static void update_datetime(void)
  32. {
  33. unsigned int t = get_ticks_ms();
  34. if (t - last_datetime_tick >= DATETIME_MS) {
  35. last_datetime_tick = t;
  36. redraw_datetime();
  37. }
  38. }
  39. static void update_cursor_blink(void)
  40. {
  41. unsigned int t = get_ticks_ms();
  42. if (t - last_cursor_tick >= CURSOR_MS) {
  43. last_cursor_tick = t;
  44. cursor_visible = !cursor_visible;
  45. }
  46. if (cursor_visible)
  47. io_cursor_show_block();
  48. else
  49. io_cursor_hide_block();
  50. }
  51. static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
  52. #define BANNER_LEN 12
  53. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  54. static const char prompt_first[] __attribute__((section(".text"))) = "> ";
  55. /* Draw row 0: banner left, datetime right. Call when terminal is active. */
  56. static void draw_header_row(void)
  57. {
  58. unsigned int i;
  59. for (i = 0; i < BANNER_LEN && banner[i]; i++)
  60. io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
  61. for (; i < IO_DATETIME_POS; i++)
  62. io_vga_write(i, (unsigned short)(0x0A << 8));
  63. redraw_datetime();
  64. fbtext_draw_rainbow_separator();
  65. }
  66. static int is_cmd_hello(const char *line, unsigned int line_len)
  67. {
  68. unsigned int i = 0;
  69. while (i < line_len && line[i] == ' ')
  70. i++;
  71. if (i + 5 > line_len)
  72. return 0;
  73. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  74. return 0;
  75. i += 5;
  76. while (i < line_len && line[i] == ' ')
  77. i++;
  78. return (i == line_len);
  79. }
  80. static int is_cmd_reboot(const char *line, unsigned int line_len)
  81. {
  82. unsigned int i = 0;
  83. while (i < line_len && line[i] == ' ')
  84. i++;
  85. if (i + 6 > line_len)
  86. return 0;
  87. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  88. return 0;
  89. i += 6;
  90. while (i < line_len && line[i] == ' ')
  91. i++;
  92. return (i == line_len);
  93. }
  94. static int is_cmd_ls(const char *line, unsigned int line_len)
  95. {
  96. unsigned int i = 0;
  97. while (i < line_len && line[i] == ' ')
  98. i++;
  99. if (i + 2 > line_len)
  100. return 0;
  101. if (line[i] != 'l' || line[i+1] != 's')
  102. return 0;
  103. i += 2;
  104. while (i < line_len && line[i] == ' ')
  105. i++;
  106. return (i == line_len);
  107. }
  108. static int is_cmd_clock(const char *line, unsigned int line_len)
  109. {
  110. unsigned int i = 0;
  111. while (i < line_len && line[i] == ' ')
  112. i++;
  113. if (i + 5 > line_len)
  114. return 0;
  115. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  116. line[i+3] != 'c' || line[i+4] != 'k')
  117. return 0;
  118. i += 5;
  119. while (i < line_len && line[i] == ' ')
  120. i++;
  121. return (i == line_len);
  122. }
  123. static int is_cmd_display(const char *line, unsigned int line_len)
  124. {
  125. unsigned int i = 0;
  126. while (i < line_len && line[i] == ' ')
  127. i++;
  128. if (i + 7 > line_len)
  129. return 0;
  130. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  131. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  132. return 0;
  133. i += 7;
  134. while (i < line_len && line[i] == ' ')
  135. i++;
  136. return (i == line_len);
  137. }
  138. #define CAT_FNAME_MAX 64
  139. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  140. {
  141. unsigned int i = 0, j;
  142. while (i < line_len && line[i] == ' ')
  143. i++;
  144. if (i + 3 > line_len)
  145. return 0;
  146. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  147. return 0;
  148. i += 3;
  149. while (i < line_len && line[i] == ' ')
  150. i++;
  151. if (i >= line_len)
  152. return 0;
  153. j = 0;
  154. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  155. filename_buf[j++] = line[i++];
  156. filename_buf[j] = '\0';
  157. return (j > 0);
  158. }
  159. static int is_cmd_drive(const char *line, unsigned int line_len, unsigned int *drive_num)
  160. {
  161. unsigned int i = 0;
  162. while (i < line_len && line[i] == ' ')
  163. i++;
  164. if (i + 5 > line_len)
  165. return 0;
  166. if (line[i] != 'd' || line[i+1] != 'r' || line[i+2] != 'i' || line[i+3] != 'v' || line[i+4] != 'e')
  167. return 0;
  168. i += 5;
  169. while (i < line_len && line[i] == ' ')
  170. i++;
  171. if (i >= line_len || line[i] < '0' || line[i] > '1')
  172. return 0;
  173. *drive_num = (unsigned int)(line[i] - '0');
  174. i++;
  175. while (i < line_len && line[i] == ' ')
  176. i++;
  177. return (i == line_len);
  178. }
  179. static int is_cmd_help(const char *line, unsigned int line_len)
  180. {
  181. unsigned int i = 0;
  182. while (i < line_len && line[i] == ' ')
  183. i++;
  184. if (i + 4 > line_len)
  185. return 0;
  186. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  187. return 0;
  188. i += 4;
  189. while (i < line_len && line[i] == ' ')
  190. i++;
  191. return (i == line_len);
  192. }
  193. static int is_cmd_clear(const char *line, unsigned int line_len)
  194. {
  195. unsigned int i = 0;
  196. while (i < line_len && line[i] == ' ')
  197. i++;
  198. if (i + 5 > line_len)
  199. return 0;
  200. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  201. line[i+3] != 'a' || line[i+4] != 'r')
  202. return 0;
  203. i += 5;
  204. while (i < line_len && line[i] == ' ')
  205. i++;
  206. return (i == line_len);
  207. }
  208. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  209. static void redraw_line_from(const char *line, unsigned int line_len,
  210. unsigned int line_pos, unsigned int line_start, unsigned int from)
  211. {
  212. unsigned int i;
  213. io_set_cursor(line_start + from);
  214. for (i = from; i < line_len; i++)
  215. io_putchar(line[i]);
  216. io_putchar(' ');
  217. io_set_cursor(line_start + line_pos);
  218. }
  219. void terminal_run(void)
  220. {
  221. static char line[LINE_MAX];
  222. static unsigned int line_len;
  223. static unsigned int line_pos; /* cursor index in line, 0..line_len */
  224. static unsigned int line_start; /* VGA position of first char of line */
  225. last_cursor_tick = get_ticks_ms();
  226. last_datetime_tick = get_ticks_ms();
  227. cursor_visible = 1;
  228. line_len = 0;
  229. line_pos = 0;
  230. line[0] = '\0';
  231. io_drain_keyboard();
  232. draw_header_row();
  233. io_putstr(prompt_first);
  234. line_start = io_get_cursor();
  235. for (;;) {
  236. update_datetime();
  237. update_cursor_blink();
  238. {
  239. int k = io_poll_key();
  240. if (k) {
  241. if (k == IO_KEY_LEFT) {
  242. if (line_pos > 0) {
  243. line_pos--;
  244. io_set_cursor(line_start + line_pos);
  245. }
  246. } else if (k == IO_KEY_RIGHT) {
  247. if (line_pos < line_len) {
  248. line_pos++;
  249. io_set_cursor(line_start + line_pos);
  250. }
  251. } else if (k == '\b') {
  252. if (line_pos > 0) {
  253. unsigned int i;
  254. line_pos--;
  255. for (i = line_pos; i < line_len; i++)
  256. line[i] = line[i + 1];
  257. line_len--;
  258. line[line_len] = '\0';
  259. redraw_line_from(line, line_len, line_pos, line_start, line_pos);
  260. }
  261. } else if (k == '\n') {
  262. static char cat_fname[CAT_FNAME_MAX];
  263. unsigned int drive_sel;
  264. int skip_prompt = 0;
  265. io_putchar('\n');
  266. if (is_cmd_hello(line, line_len)) {
  267. program_hello_main();
  268. io_drain_keyboard();
  269. draw_header_row();
  270. } else if (is_cmd_reboot(line, line_len)) {
  271. program_reboot_main();
  272. } else if (is_cmd_ls(line, line_len)) {
  273. program_ls_main();
  274. io_drain_keyboard();
  275. draw_header_row();
  276. } else if (is_cmd_clock(line, line_len)) {
  277. program_clock_main();
  278. io_drain_keyboard();
  279. io_clear_screen();
  280. draw_header_row();
  281. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  282. program_cat_main(cat_fname);
  283. io_drain_keyboard();
  284. draw_header_row();
  285. } else if (is_cmd_display(line, line_len)) {
  286. program_display_main();
  287. } else if (is_cmd_drive(line, line_len, &drive_sel)) {
  288. disk_set_drive(drive_sel);
  289. io_putstr("drive ");
  290. io_putchar((char)('0' + disk_get_drive()));
  291. io_putstr(" selected\n");
  292. } else if (is_cmd_help(line, line_len)) {
  293. io_putstr("hello\n");
  294. io_putstr("reboot\n");
  295. io_putstr("ls\n");
  296. io_putstr("clock\n");
  297. io_putstr("cat <file>\n");
  298. io_putstr("display\n");
  299. io_putstr("drive <0|1>\n");
  300. io_putstr("help\n");
  301. io_putstr("clear\n");
  302. } else if (is_cmd_clear(line, line_len)) {
  303. io_clear_screen();
  304. draw_header_row();
  305. io_set_cursor(IO_CONTENT_OFFSET);
  306. io_putstr(prompt_first);
  307. line_start = io_get_cursor();
  308. skip_prompt = 1;
  309. } else {
  310. unsigned int i = 0;
  311. while (i < line_len && line[i] == ' ')
  312. i++;
  313. if (i < line_len)
  314. io_putstr("command not found\n");
  315. }
  316. line_len = 0;
  317. line_pos = 0;
  318. line[0] = '\0';
  319. if (!skip_prompt) {
  320. io_putstr(prompt);
  321. line_start = io_get_cursor();
  322. }
  323. if (io_did_scroll())
  324. redraw_datetime();
  325. } else {
  326. {
  327. unsigned char u = (unsigned char)k;
  328. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  329. unsigned int i;
  330. for (i = line_len; i > line_pos; i--)
  331. line[i] = line[i - 1];
  332. line[line_pos] = (char)k;
  333. line_len++;
  334. line[line_len] = '\0';
  335. line_pos++;
  336. redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
  337. }
  338. }
  339. }
  340. } else {
  341. __asm__ volatile ("hlt");
  342. }
  343. }
  344. }
  345. }