terminal.c 10 KB

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