terminal.c 22 KB

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