terminal.c 11 KB

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