terminal.c 24 KB

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