terminal.c 20 KB

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