terminal.c 16 KB

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