terminal.c 21 KB

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