terminal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch.
  2. Commands are spawned as separate processes for preemptive multitasking. */
  3. #include "io.h"
  4. #include "fbtext.h"
  5. #include "process.h"
  6. #include "rtc.h"
  7. #include "terminal.h"
  8. #include "timer.h"
  9. extern void program_hello_main(void);
  10. extern void program_reboot_main(void);
  11. extern void program_ls_main(void);
  12. extern void program_cat_main(const char *filename);
  13. extern void program_run_main(const char *filename);
  14. extern void program_clock_main(void);
  15. extern void program_colors_main(void);
  16. extern void program_display_main(void);
  17. extern void disk_set_drive(unsigned int drive);
  18. extern unsigned int disk_get_drive(void);
  19. #define DATETIME_MS 1000
  20. #define LINE_MAX 64
  21. #define HISTORY_SIZE 32
  22. static unsigned int last_datetime_tick;
  23. static char datetime_buf[IO_DATETIME_LEN + 1];
  24. static char history[HISTORY_SIZE][LINE_MAX];
  25. static unsigned int history_count;
  26. static unsigned int history_next;
  27. static int cursor_visible;
  28. static unsigned int last_cursor_tick;
  29. static void redraw_datetime(void)
  30. {
  31. rtc_read_datetime(datetime_buf);
  32. {
  33. unsigned int i;
  34. for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
  35. io_vga_write(IO_DATETIME_POS + i,
  36. (unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
  37. }
  38. }
  39. static void update_datetime(void)
  40. {
  41. unsigned int t = get_ticks_ms();
  42. if (t - last_datetime_tick >= DATETIME_MS) {
  43. last_datetime_tick = t;
  44. redraw_datetime();
  45. }
  46. }
  47. static void update_cursor_blink(void)
  48. {
  49. unsigned int t = get_ticks_ms();
  50. if (t - last_cursor_tick >= CURSOR_MS) {
  51. last_cursor_tick = t;
  52. cursor_visible = !cursor_visible;
  53. }
  54. if (cursor_visible)
  55. io_cursor_show_block();
  56. else
  57. io_cursor_hide_block();
  58. }
  59. static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
  60. #define BANNER_LEN 12
  61. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  62. static const char prompt_first[] __attribute__((section(".text"))) = "> ";
  63. /* Draw row 0: banner left, datetime right. Call when terminal is active. */
  64. static void draw_header_row(void)
  65. {
  66. unsigned int i;
  67. for (i = 0; i < BANNER_LEN && banner[i]; i++)
  68. io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
  69. for (; i < IO_DATETIME_POS; i++)
  70. io_vga_write(i, (unsigned short)(0x0A << 8));
  71. redraw_datetime();
  72. fbtext_draw_rainbow_separator();
  73. }
  74. static int is_cmd_hello(const char *line, unsigned int line_len)
  75. {
  76. unsigned int i = 0;
  77. while (i < line_len && line[i] == ' ')
  78. i++;
  79. if (i + 5 > line_len)
  80. return 0;
  81. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  82. return 0;
  83. i += 5;
  84. while (i < line_len && line[i] == ' ')
  85. i++;
  86. return (i == line_len);
  87. }
  88. static int is_cmd_reboot(const char *line, unsigned int line_len)
  89. {
  90. unsigned int i = 0;
  91. while (i < line_len && line[i] == ' ')
  92. i++;
  93. if (i + 6 > line_len)
  94. return 0;
  95. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  96. return 0;
  97. i += 6;
  98. while (i < line_len && line[i] == ' ')
  99. i++;
  100. return (i == line_len);
  101. }
  102. static int is_cmd_ls(const char *line, unsigned int line_len)
  103. {
  104. unsigned int i = 0;
  105. while (i < line_len && line[i] == ' ')
  106. i++;
  107. if (i + 2 > line_len)
  108. return 0;
  109. if (line[i] != 'l' || line[i+1] != 's')
  110. return 0;
  111. i += 2;
  112. while (i < line_len && line[i] == ' ')
  113. i++;
  114. return (i == line_len);
  115. }
  116. static int is_cmd_clock(const char *line, unsigned int line_len)
  117. {
  118. unsigned int i = 0;
  119. while (i < line_len && line[i] == ' ')
  120. i++;
  121. if (i + 5 > line_len)
  122. return 0;
  123. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  124. line[i+3] != 'c' || line[i+4] != 'k')
  125. return 0;
  126. i += 5;
  127. while (i < line_len && line[i] == ' ')
  128. i++;
  129. return (i == line_len);
  130. }
  131. static int is_cmd_colors(const char *line, unsigned int line_len)
  132. {
  133. unsigned int i = 0;
  134. while (i < line_len && line[i] == ' ')
  135. i++;
  136. if (i + 6 > line_len)
  137. return 0;
  138. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  139. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  140. return 0;
  141. i += 6;
  142. while (i < line_len && line[i] == ' ')
  143. i++;
  144. return (i == line_len);
  145. }
  146. static int is_cmd_display(const char *line, unsigned int line_len)
  147. {
  148. unsigned int i = 0;
  149. while (i < line_len && line[i] == ' ')
  150. i++;
  151. if (i + 7 > line_len)
  152. return 0;
  153. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  154. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  155. return 0;
  156. i += 7;
  157. while (i < line_len && line[i] == ' ')
  158. i++;
  159. return (i == line_len);
  160. }
  161. static int is_cmd_space(const char *line, unsigned int line_len)
  162. {
  163. unsigned int i = 0;
  164. while (i < line_len && line[i] == ' ')
  165. i++;
  166. if (i + 5 > line_len)
  167. return 0;
  168. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  169. line[i+3] != 'c' || line[i+4] != 'e')
  170. return 0;
  171. i += 5;
  172. while (i < line_len && line[i] == ' ')
  173. i++;
  174. return (i == line_len);
  175. }
  176. #define CAT_FNAME_MAX 64
  177. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  178. {
  179. unsigned int i = 0, j;
  180. while (i < line_len && line[i] == ' ')
  181. i++;
  182. if (i + 3 > line_len)
  183. return 0;
  184. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  185. return 0;
  186. i += 3;
  187. while (i < line_len && line[i] == ' ')
  188. i++;
  189. if (i >= line_len)
  190. return 0;
  191. j = 0;
  192. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  193. filename_buf[j++] = line[i++];
  194. filename_buf[j] = '\0';
  195. return (j > 0);
  196. }
  197. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  198. {
  199. unsigned int i = 0, j;
  200. while (i < line_len && line[i] == ' ')
  201. i++;
  202. if (i + 3 > line_len)
  203. return 0;
  204. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  205. return 0;
  206. i += 3;
  207. while (i < line_len && line[i] == ' ')
  208. i++;
  209. if (i >= line_len)
  210. return 0;
  211. j = 0;
  212. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  213. filename_buf[j++] = line[i++];
  214. filename_buf[j] = '\0';
  215. return (j > 0);
  216. }
  217. static int is_cmd_drive(const char *line, unsigned int line_len, unsigned int *drive_num)
  218. {
  219. unsigned int i = 0;
  220. while (i < line_len && line[i] == ' ')
  221. i++;
  222. if (i + 5 > line_len)
  223. return 0;
  224. if (line[i] != 'd' || line[i+1] != 'r' || line[i+2] != 'i' || line[i+3] != 'v' || line[i+4] != 'e')
  225. return 0;
  226. i += 5;
  227. while (i < line_len && line[i] == ' ')
  228. i++;
  229. if (i >= line_len || line[i] < '0' || line[i] > '1')
  230. return 0;
  231. *drive_num = (unsigned int)(line[i] - '0');
  232. i++;
  233. while (i < line_len && line[i] == ' ')
  234. i++;
  235. return (i == line_len);
  236. }
  237. static int is_cmd_help(const char *line, unsigned int line_len)
  238. {
  239. unsigned int i = 0;
  240. while (i < line_len && line[i] == ' ')
  241. i++;
  242. if (i + 4 > line_len)
  243. return 0;
  244. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  245. return 0;
  246. i += 4;
  247. while (i < line_len && line[i] == ' ')
  248. i++;
  249. return (i == line_len);
  250. }
  251. static int is_cmd_clear(const char *line, unsigned int line_len)
  252. {
  253. unsigned int i = 0;
  254. while (i < line_len && line[i] == ' ')
  255. i++;
  256. if (i + 5 > line_len)
  257. return 0;
  258. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  259. line[i+3] != 'a' || line[i+4] != 'r')
  260. return 0;
  261. i += 5;
  262. while (i < line_len && line[i] == ' ')
  263. i++;
  264. return (i == line_len);
  265. }
  266. static int is_cmd_ps(const char *line, unsigned int line_len)
  267. {
  268. unsigned int i = 0;
  269. while (i < line_len && line[i] == ' ')
  270. i++;
  271. if (i + 2 > line_len)
  272. return 0;
  273. if (line[i] != 'p' || line[i+1] != 's')
  274. return 0;
  275. i += 2;
  276. while (i < line_len && line[i] == ' ')
  277. i++;
  278. return (i == line_len);
  279. }
  280. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  281. {
  282. unsigned int i = 0, num = 0;
  283. int has_digit = 0;
  284. while (i < line_len && line[i] == ' ')
  285. i++;
  286. if (i + 4 > line_len)
  287. return 0;
  288. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  289. return 0;
  290. i += 4;
  291. while (i < line_len && line[i] == ' ')
  292. i++;
  293. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  294. num = num * 10 + (unsigned int)(line[i] - '0');
  295. has_digit = 1;
  296. i++;
  297. }
  298. if (!has_digit)
  299. return 0;
  300. while (i < line_len && line[i] == ' ')
  301. i++;
  302. if (i != line_len)
  303. return 0;
  304. *pid_out = num;
  305. return 1;
  306. }
  307. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  308. static void redraw_line_from(const char *line, unsigned int line_len,
  309. unsigned int line_pos, unsigned int line_start, unsigned int from)
  310. {
  311. unsigned int i;
  312. io_set_cursor(line_start + from);
  313. for (i = from; i < line_len; i++)
  314. io_putchar(line[i]);
  315. io_putchar(' ');
  316. io_set_cursor(line_start + line_pos);
  317. }
  318. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  319. static void redraw_line_clear(const char *line, unsigned int line_len,
  320. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  321. {
  322. unsigned int i;
  323. io_set_cursor(line_start);
  324. for (i = 0; i < line_len; i++)
  325. io_putchar(line[i]);
  326. for (i = line_len; i <= clear_through; i++)
  327. io_putchar(' ');
  328. io_set_cursor(line_start + line_pos);
  329. }
  330. /* Copy history entry (1 = newest) into line, set line_len/line_pos, redraw from start. */
  331. static void history_load_into_line(char *line, unsigned int *line_len, unsigned int *line_pos,
  332. unsigned int line_start, unsigned int browse)
  333. {
  334. unsigned int slot, len, old_len;
  335. const char *src;
  336. if (browse < 1 || browse > history_count)
  337. return;
  338. old_len = *line_len;
  339. slot = (history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  340. src = history[slot];
  341. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  342. line[len] = src[len];
  343. line[len] = '\0';
  344. *line_len = len;
  345. *line_pos = len;
  346. redraw_line_clear(line, len, len, line_start, old_len > len ? old_len : len);
  347. }
  348. /* Push current line to history if non-empty (and not duplicate of newest). */
  349. static void history_push(const char *line, unsigned int line_len)
  350. {
  351. unsigned int i, slot;
  352. int all_spaces = 1;
  353. if (line_len == 0)
  354. return;
  355. for (i = 0; i < line_len; i++) {
  356. if (line[i] != ' ') {
  357. all_spaces = 0;
  358. break;
  359. }
  360. }
  361. if (all_spaces)
  362. return;
  363. if (history_count > 0) {
  364. slot = (history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  365. for (i = 0; i < line_len && i < LINE_MAX - 1 && history[slot][i]; i++) {
  366. if (history[slot][i] != line[i])
  367. break;
  368. }
  369. if (i == line_len && history[slot][i] == '\0')
  370. return; /* duplicate of newest */
  371. }
  372. slot = history_next;
  373. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  374. history[slot][i] = line[i];
  375. history[slot][i] = '\0';
  376. history_next = (history_next + 1) % HISTORY_SIZE;
  377. if (history_count < HISTORY_SIZE)
  378. history_count++;
  379. }
  380. void terminal_run(void)
  381. {
  382. static char line[LINE_MAX];
  383. static unsigned int line_len;
  384. static unsigned int line_pos; /* cursor index in line, 0..line_len */
  385. static unsigned int line_start; /* VGA position of first char of line */
  386. static unsigned int history_browse; /* 0 = not browsing, 1 = newest, 2 = older, ... */
  387. last_cursor_tick = get_ticks_ms();
  388. last_datetime_tick = get_ticks_ms();
  389. cursor_visible = 1;
  390. line_len = 0;
  391. line_pos = 0;
  392. line[0] = '\0';
  393. history_browse = 0;
  394. io_drain_keyboard();
  395. draw_header_row();
  396. io_putstr(prompt_first);
  397. line_start = io_get_cursor();
  398. for (;;) {
  399. update_datetime();
  400. update_cursor_blink();
  401. {
  402. int k = io_poll_key();
  403. if (k) {
  404. if (k == IO_KEY_LEFT) {
  405. if (line_pos > 0) {
  406. line_pos--;
  407. io_set_cursor(line_start + line_pos);
  408. }
  409. } else if (k == IO_KEY_RIGHT) {
  410. if (line_pos < line_len) {
  411. line_pos++;
  412. io_set_cursor(line_start + line_pos);
  413. }
  414. } else if (k == IO_KEY_UP) {
  415. if (history_count > 0) {
  416. if (history_browse < history_count)
  417. history_browse++;
  418. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  419. }
  420. } else if (k == IO_KEY_DOWN) {
  421. if (history_browse > 0) {
  422. history_browse--;
  423. if (history_browse == 0) {
  424. {
  425. unsigned int clear_len = line_len;
  426. line_len = 0;
  427. line_pos = 0;
  428. line[0] = '\0';
  429. redraw_line_clear(line, 0, 0, line_start, clear_len);
  430. }
  431. } else {
  432. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  433. }
  434. }
  435. } else if (k == '\b') {
  436. if (line_pos > 0) {
  437. unsigned int i;
  438. line_pos--;
  439. for (i = line_pos; i < line_len; i++)
  440. line[i] = line[i + 1];
  441. line_len--;
  442. line[line_len] = '\0';
  443. redraw_line_from(line, line_len, line_pos, line_start, line_pos);
  444. }
  445. } else if (k == '\n') {
  446. static char cat_fname[CAT_FNAME_MAX];
  447. unsigned int drive_sel, kill_pid;
  448. int skip_prompt = 0;
  449. io_putchar('\n');
  450. history_push(line, line_len);
  451. history_browse = 0;
  452. if (is_cmd_hello(line, line_len)) {
  453. if (process_create("hello", program_hello_main) < 0)
  454. io_putstr("error: too many processes\n");
  455. } else if (is_cmd_reboot(line, line_len)) {
  456. program_reboot_main();
  457. } else if (is_cmd_ls(line, line_len)) {
  458. if (process_create("ls", program_ls_main) < 0)
  459. io_putstr("error: too many processes\n");
  460. } else if (is_cmd_clock(line, line_len)) {
  461. if (process_create("clock", program_clock_main) < 0)
  462. io_putstr("error: too many processes\n");
  463. } else if (is_cmd_colors(line, line_len)) {
  464. if (process_create("colors", program_colors_main) < 0)
  465. io_putstr("error: too many processes\n");
  466. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  467. if (process_create_arg("cat", program_cat_main, cat_fname) < 0)
  468. io_putstr("error: too many processes\n");
  469. } else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  470. if (process_create_arg("run", program_run_main, cat_fname) < 0)
  471. io_putstr("error: too many processes\n");
  472. } else if (is_cmd_space(line, line_len)) {
  473. if (process_create_arg("space", program_run_main, "space.bin") < 0)
  474. io_putstr("error: too many processes\n");
  475. } else if (is_cmd_display(line, line_len)) {
  476. if (process_create("display", program_display_main) < 0)
  477. io_putstr("error: too many processes\n");
  478. } else if (is_cmd_drive(line, line_len, &drive_sel)) {
  479. disk_set_drive(drive_sel);
  480. io_putstr("drive ");
  481. io_putchar((char)('0' + disk_get_drive()));
  482. io_putstr(" selected\n");
  483. } else if (is_cmd_ps(line, line_len)) {
  484. process_ps();
  485. } else if (is_cmd_kill(line, line_len, &kill_pid)) {
  486. if (process_kill(kill_pid) < 0)
  487. io_putstr("kill: no such process\n");
  488. else
  489. io_putstr("killed\n");
  490. } else if (is_cmd_help(line, line_len)) {
  491. io_putstr("hello reboot\n");
  492. io_putstr("ls cat <file>\n");
  493. io_putstr("run <file> space\n");
  494. io_putstr("clock colors\n");
  495. io_putstr("display drive <0|1>\n");
  496. io_putstr("ps kill <pid>\n");
  497. io_putstr("help clear\n");
  498. } else if (is_cmd_clear(line, line_len)) {
  499. io_clear_screen();
  500. draw_header_row();
  501. io_set_cursor(IO_CONTENT_OFFSET);
  502. io_putstr(prompt_first);
  503. line_start = io_get_cursor();
  504. skip_prompt = 1;
  505. } else {
  506. unsigned int i = 0;
  507. while (i < line_len && line[i] == ' ')
  508. i++;
  509. if (i < line_len)
  510. io_putstr("command not found\n");
  511. }
  512. line_len = 0;
  513. line_pos = 0;
  514. line[0] = '\0';
  515. if (!skip_prompt) {
  516. io_putstr(prompt);
  517. line_start = io_get_cursor();
  518. }
  519. if (io_did_scroll())
  520. redraw_datetime();
  521. } else {
  522. {
  523. unsigned char u = (unsigned char)k;
  524. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  525. unsigned int i;
  526. history_browse = 0;
  527. for (i = line_len; i > line_pos; i--)
  528. line[i] = line[i - 1];
  529. line[line_pos] = (char)k;
  530. line_len++;
  531. line[line_len] = '\0';
  532. line_pos++;
  533. redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
  534. }
  535. }
  536. }
  537. } else {
  538. __asm__ volatile ("hlt");
  539. }
  540. }
  541. }
  542. }