terminal.c 21 KB

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