terminal.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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(const char *path);
  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_mkdir_main(const char *arg);
  24. extern void program_rmdir_main(const char *arg);
  25. extern void program_clock_main(void);
  26. extern void program_colors_main(void);
  27. extern void program_display_main(void);
  28. extern void program_cpuinfo_main(void);
  29. extern void program_mouseinfo_main(void);
  30. extern void program_serial_loop_main(const char *arg);
  31. extern void program_serial_recv_main(const char *arg);
  32. extern void program_run_serial_main(const char *arg);
  33. extern void program_lsusb_main(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, char *path_buf, unsigned int buf_size)
  133. {
  134. unsigned int i = 0, j = 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. if (i < line_len && line[i] != ' ')
  143. return 0; /* "ls" must be a whole word, so reject "lsusb" etc. */
  144. while (i < line_len && line[i] == ' ')
  145. i++;
  146. if (i >= line_len) {
  147. if (buf_size > 0)
  148. path_buf[0] = '\0';
  149. return 1;
  150. }
  151. while (i < line_len && j + 1 < buf_size)
  152. path_buf[j++] = line[i++];
  153. path_buf[j] = '\0';
  154. while (i < line_len && line[i] == ' ')
  155. i++;
  156. return (i == line_len);
  157. }
  158. static int is_cmd_clock(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 + 5 > line_len)
  164. return 0;
  165. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  166. line[i+3] != 'c' || line[i+4] != 'k')
  167. return 0;
  168. i += 5;
  169. while (i < line_len && line[i] == ' ')
  170. i++;
  171. return (i == line_len);
  172. }
  173. static int is_cmd_colors(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 + 6 > line_len)
  179. return 0;
  180. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  181. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  182. return 0;
  183. i += 6;
  184. while (i < line_len && line[i] == ' ')
  185. i++;
  186. return (i == line_len);
  187. }
  188. static int is_cmd_display(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] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  196. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  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_cpuinfo(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 + 7 > line_len)
  209. return 0;
  210. if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
  211. line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
  212. return 0;
  213. i += 7;
  214. while (i < line_len && line[i] == ' ')
  215. i++;
  216. return (i == line_len);
  217. }
  218. static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
  219. {
  220. unsigned int i = 0;
  221. while (i < line_len && line[i] == ' ')
  222. i++;
  223. if (i + 9 > line_len)
  224. return 0;
  225. if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
  226. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
  227. line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
  228. return 0;
  229. i += 9;
  230. while (i < line_len && line[i] == ' ')
  231. i++;
  232. return (i == line_len);
  233. }
  234. static int is_cmd_lsusb(const char *line, unsigned int line_len)
  235. {
  236. unsigned int i = 0;
  237. while (i < line_len && line[i] == ' ')
  238. i++;
  239. if (i + 5 > line_len)
  240. return 0;
  241. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'u' ||
  242. line[i+3] != 's' || line[i+4] != 'b')
  243. return 0;
  244. i += 5;
  245. while (i < line_len && line[i] == ' ')
  246. i++;
  247. return (i == line_len);
  248. }
  249. static int is_cmd_space(const char *line, unsigned int line_len)
  250. {
  251. unsigned int i = 0;
  252. while (i < line_len && line[i] == ' ')
  253. i++;
  254. if (i + 5 > line_len)
  255. return 0;
  256. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  257. line[i+3] != 'c' || line[i+4] != 'e')
  258. return 0;
  259. i += 5;
  260. while (i < line_len && line[i] == ' ')
  261. i++;
  262. return (i == line_len);
  263. }
  264. #define CAT_FNAME_MAX 64
  265. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  266. {
  267. unsigned int i = 0, j;
  268. while (i < line_len && line[i] == ' ')
  269. i++;
  270. if (i + 3 > line_len)
  271. return 0;
  272. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  273. return 0;
  274. i += 3;
  275. while (i < line_len && line[i] == ' ')
  276. i++;
  277. if (i >= line_len)
  278. return 0;
  279. j = 0;
  280. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  281. filename_buf[j++] = line[i++];
  282. filename_buf[j] = '\0';
  283. return (j > 0);
  284. }
  285. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  286. {
  287. unsigned int i = 0, j;
  288. while (i < line_len && line[i] == ' ')
  289. i++;
  290. if (i + 3 > line_len)
  291. return 0;
  292. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  293. return 0;
  294. i += 3;
  295. while (i < line_len && line[i] == ' ')
  296. i++;
  297. if (i >= line_len)
  298. return 0;
  299. j = 0;
  300. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  301. filename_buf[j++] = line[i++];
  302. filename_buf[j] = '\0';
  303. return (j > 0);
  304. }
  305. #define SAVE_ARGS_MAX 128
  306. #define RM_FNAME_MAX 64
  307. #define LS_PATH_MAX 64
  308. static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  309. {
  310. unsigned int i = 0, j = 0;
  311. while (i < line_len && line[i] == ' ')
  312. i++;
  313. if (i + 4 > line_len)
  314. return 0;
  315. if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
  316. return 0;
  317. i += 4;
  318. while (i < line_len && line[i] == ' ')
  319. i++;
  320. if (i >= line_len)
  321. return 0;
  322. while (i < line_len && j + 1 < buf_size)
  323. arg_buf[j++] = line[i++];
  324. while (j > 0 && arg_buf[j - 1] == ' ')
  325. j--;
  326. arg_buf[j] = '\0';
  327. while (i < line_len && line[i] == ' ')
  328. i++;
  329. return (i == line_len && j > 0);
  330. }
  331. static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  332. {
  333. unsigned int i = 0, j = 0;
  334. while (i < line_len && line[i] == ' ')
  335. i++;
  336. if (i + 2 > line_len)
  337. return 0;
  338. if (line[i] != 'r' || line[i+1] != 'm')
  339. return 0;
  340. i += 2;
  341. while (i < line_len && line[i] == ' ')
  342. i++;
  343. if (i >= line_len)
  344. return 0;
  345. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  346. filename_buf[j++] = line[i++];
  347. filename_buf[j] = '\0';
  348. return (j > 0);
  349. }
  350. static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  351. {
  352. unsigned int i = 0, j = 0;
  353. while (i < line_len && line[i] == ' ')
  354. i++;
  355. if (i + 5 > line_len)
  356. return 0;
  357. if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  358. return 0;
  359. i += 5;
  360. while (i < line_len && line[i] == ' ')
  361. i++;
  362. if (i >= line_len)
  363. return 0;
  364. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  365. path_buf[j++] = line[i++];
  366. path_buf[j] = '\0';
  367. return (j > 0);
  368. }
  369. static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  370. {
  371. unsigned int i = 0, j = 0;
  372. while (i < line_len && line[i] == ' ')
  373. i++;
  374. if (i + 5 > line_len)
  375. return 0;
  376. if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  377. return 0;
  378. i += 5;
  379. while (i < line_len && line[i] == ' ')
  380. i++;
  381. if (i >= line_len)
  382. return 0;
  383. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  384. path_buf[j++] = line[i++];
  385. path_buf[j] = '\0';
  386. return (j > 0);
  387. }
  388. static int is_cmd_help(const char *line, unsigned int line_len)
  389. {
  390. unsigned int i = 0;
  391. while (i < line_len && line[i] == ' ')
  392. i++;
  393. if (i + 4 > line_len)
  394. return 0;
  395. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  396. return 0;
  397. i += 4;
  398. while (i < line_len && line[i] == ' ')
  399. i++;
  400. return (i == line_len);
  401. }
  402. static int is_cmd_clear(const char *line, unsigned int line_len)
  403. {
  404. unsigned int i = 0;
  405. while (i < line_len && line[i] == ' ')
  406. i++;
  407. if (i + 5 > line_len)
  408. return 0;
  409. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  410. line[i+3] != 'a' || line[i+4] != 'r')
  411. return 0;
  412. i += 5;
  413. while (i < line_len && line[i] == ' ')
  414. i++;
  415. return (i == line_len);
  416. }
  417. static int is_cmd_ps(const char *line, unsigned int line_len)
  418. {
  419. unsigned int i = 0;
  420. while (i < line_len && line[i] == ' ')
  421. i++;
  422. if (i + 2 > line_len)
  423. return 0;
  424. if (line[i] != 'p' || line[i+1] != 's')
  425. return 0;
  426. i += 2;
  427. while (i < line_len && line[i] == ' ')
  428. i++;
  429. return (i == line_len);
  430. }
  431. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  432. {
  433. unsigned int i = 0, num = 0;
  434. int has_digit = 0;
  435. while (i < line_len && line[i] == ' ')
  436. i++;
  437. if (i + 4 > line_len)
  438. return 0;
  439. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  440. return 0;
  441. i += 4;
  442. while (i < line_len && line[i] == ' ')
  443. i++;
  444. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  445. num = num * 10 + (unsigned int)(line[i] - '0');
  446. has_digit = 1;
  447. i++;
  448. }
  449. if (!has_digit)
  450. return 0;
  451. while (i < line_len && line[i] == ' ')
  452. i++;
  453. if (i != line_len)
  454. return 0;
  455. *pid_out = num;
  456. return 1;
  457. }
  458. static int is_cmd_kexec(const char *line, unsigned int line_len)
  459. {
  460. unsigned int i = 0;
  461. while (i < line_len && line[i] == ' ')
  462. i++;
  463. if (i + 5 > line_len)
  464. return 0;
  465. if (line[i] != 'k' || line[i+1] != 'e' || line[i+2] != 'x' || line[i+3] != 'e' || line[i+4] != 'c')
  466. return 0;
  467. i += 5;
  468. while (i < line_len && line[i] == ' ')
  469. i++;
  470. return (i == line_len);
  471. }
  472. static int is_cmd_dbg(const char *line, unsigned int line_len)
  473. {
  474. unsigned int i = 0;
  475. while (i < line_len && line[i] == ' ')
  476. i++;
  477. if (i + 3 > line_len)
  478. return 0;
  479. if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
  480. return 0;
  481. i += 3;
  482. while (i < line_len && line[i] == ' ')
  483. i++;
  484. return (i == line_len);
  485. }
  486. static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  487. {
  488. unsigned int i = 0, j = 0;
  489. while (i < line_len && line[i] == ' ')
  490. i++;
  491. if (i + 6 > line_len)
  492. return 0;
  493. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
  494. line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
  495. return 0;
  496. i += 6;
  497. while (i < line_len && line[i] == ' ')
  498. i++;
  499. if (i >= line_len) {
  500. if (buf_size > 0)
  501. arg_buf[0] = '\0';
  502. return 1;
  503. }
  504. while (i < line_len && j + 1 < buf_size)
  505. arg_buf[j++] = line[i++];
  506. while (j > 0 && arg_buf[j - 1] == ' ')
  507. j--;
  508. arg_buf[j] = '\0';
  509. while (i < line_len && line[i] == ' ')
  510. i++;
  511. return (i == line_len);
  512. }
  513. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  514. static void redraw_line_from(const char *line, unsigned int line_len,
  515. unsigned int line_pos, unsigned int line_start, unsigned int from)
  516. {
  517. unsigned int i;
  518. io_set_cursor(line_start + from);
  519. for (i = from; i < line_len; i++)
  520. io_putchar(line[i]);
  521. io_putchar(' ');
  522. io_set_cursor(line_start + line_pos);
  523. }
  524. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  525. static void redraw_line_clear(const char *line, unsigned int line_len,
  526. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  527. {
  528. unsigned int i;
  529. io_set_cursor(line_start);
  530. for (i = 0; i < line_len; i++)
  531. io_putchar(line[i]);
  532. for (i = line_len; i <= clear_through; i++)
  533. io_putchar(' ');
  534. io_set_cursor(line_start + line_pos);
  535. }
  536. /* Copy history entry (1 = newest) into line, set line_len/line_pos, redraw from start. */
  537. static void history_load_into_line(char *line, unsigned int *line_len, unsigned int *line_pos,
  538. unsigned int line_start, unsigned int browse)
  539. {
  540. unsigned int slot, len, old_len;
  541. const char *src;
  542. if (browse < 1 || browse > history_count)
  543. return;
  544. old_len = *line_len;
  545. slot = (history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  546. src = history[slot];
  547. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  548. line[len] = src[len];
  549. line[len] = '\0';
  550. *line_len = len;
  551. *line_pos = len;
  552. redraw_line_clear(line, len, len, line_start, old_len > len ? old_len : len);
  553. }
  554. /* Push current line to history if non-empty (and not duplicate of newest). */
  555. static void history_push(const char *line, unsigned int line_len)
  556. {
  557. unsigned int i, slot;
  558. int all_spaces = 1;
  559. if (line_len == 0)
  560. return;
  561. for (i = 0; i < line_len; i++) {
  562. if (line[i] != ' ') {
  563. all_spaces = 0;
  564. break;
  565. }
  566. }
  567. if (all_spaces)
  568. return;
  569. if (history_count > 0) {
  570. slot = (history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  571. for (i = 0; i < line_len && i < LINE_MAX - 1 && history[slot][i]; i++) {
  572. if (history[slot][i] != line[i])
  573. break;
  574. }
  575. if (i == line_len && history[slot][i] == '\0')
  576. return; /* duplicate of newest */
  577. }
  578. slot = history_next;
  579. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  580. history[slot][i] = line[i];
  581. history[slot][i] = '\0';
  582. history_next = (history_next + 1) % HISTORY_SIZE;
  583. if (history_count < HISTORY_SIZE)
  584. history_count++;
  585. }
  586. void terminal_run(void)
  587. {
  588. static char line[LINE_MAX];
  589. static unsigned int line_len;
  590. static unsigned int line_pos; /* cursor index in line, 0..line_len */
  591. static unsigned int line_start; /* VGA position of first char of line */
  592. static unsigned int history_browse; /* 0 = not browsing, 1 = newest, 2 = older, ... */
  593. last_cursor_tick = get_ticks_ms();
  594. last_datetime_tick = get_ticks_ms();
  595. cursor_visible = 1;
  596. line_len = 0;
  597. line_pos = 0;
  598. line[0] = '\0';
  599. history_browse = 0;
  600. io_drain_keyboard();
  601. io_clear_screen();
  602. draw_header_row();
  603. io_set_cursor(IO_CONTENT_OFFSET);
  604. io_putstr(prompt_first);
  605. line_start = io_get_cursor();
  606. for (;;) {
  607. vga_fb_flush_pending_check();
  608. mouse_poll_update();
  609. update_datetime();
  610. update_cursor_blink();
  611. {
  612. int k = io_poll_key();
  613. if (k) {
  614. if (k == IO_KEY_LEFT) {
  615. if (line_pos > 0) {
  616. line_pos--;
  617. io_set_cursor(line_start + line_pos);
  618. }
  619. } else if (k == IO_KEY_RIGHT) {
  620. if (line_pos < line_len) {
  621. line_pos++;
  622. io_set_cursor(line_start + line_pos);
  623. }
  624. } else if (k == IO_KEY_UP) {
  625. if (history_count > 0) {
  626. if (history_browse < history_count)
  627. history_browse++;
  628. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  629. }
  630. } else if (k == IO_KEY_DOWN) {
  631. if (history_browse > 0) {
  632. history_browse--;
  633. if (history_browse == 0) {
  634. {
  635. unsigned int clear_len = line_len;
  636. line_len = 0;
  637. line_pos = 0;
  638. line[0] = '\0';
  639. redraw_line_clear(line, 0, 0, line_start, clear_len);
  640. }
  641. } else {
  642. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  643. }
  644. }
  645. } else if (k == '\b') {
  646. if (line_pos > 0) {
  647. unsigned int i;
  648. line_pos--;
  649. for (i = line_pos; i < line_len; i++)
  650. line[i] = line[i + 1];
  651. line_len--;
  652. line[line_len] = '\0';
  653. redraw_line_from(line, line_len, line_pos, line_start, line_pos);
  654. }
  655. } else if (k == '\n') {
  656. static char cat_fname[CAT_FNAME_MAX];
  657. static char save_args[SAVE_ARGS_MAX];
  658. static char rm_fname[RM_FNAME_MAX];
  659. static char ls_path[LS_PATH_MAX];
  660. unsigned int kill_pid;
  661. int skip_prompt = 0;
  662. io_putchar('\n');
  663. history_push(line, line_len);
  664. history_browse = 0;
  665. if (is_cmd_hello(line, line_len)) {
  666. if (process_create("hello", program_hello_main) < 0)
  667. io_putstr("error: too many processes\n");
  668. } else if (is_cmd_reboot(line, line_len)) {
  669. program_reboot_main();
  670. } else if (is_cmd_poweroff(line, line_len)) {
  671. program_poweroff_main();
  672. } else if (is_cmd_ls(line, line_len, ls_path, LS_PATH_MAX)) {
  673. if (process_create_arg("ls", program_ls_main, ls_path) < 0)
  674. io_putstr("error: too many processes\n");
  675. } else if (is_cmd_lsusb(line, line_len)) {
  676. if (process_create("lsusb", program_lsusb_main) < 0)
  677. io_putstr("error: too many processes\n");
  678. } else if (is_cmd_clock(line, line_len)) {
  679. if (process_create("clock", program_clock_main) < 0)
  680. io_putstr("error: too many processes\n");
  681. } else if (is_cmd_colors(line, line_len)) {
  682. if (process_create("colors", program_colors_main) < 0)
  683. io_putstr("error: too many processes\n");
  684. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  685. if (process_create_arg("cat", program_cat_main, cat_fname) < 0)
  686. io_putstr("error: too many processes\n");
  687. } else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  688. if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
  689. cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
  690. cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
  691. cat_fname[6] == '\0') {
  692. if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
  693. io_putstr("error: too many processes\n");
  694. } else {
  695. if (process_create_arg("run", program_run_main, cat_fname) < 0)
  696. io_putstr("error: too many processes\n");
  697. }
  698. } else if (is_cmd_save(line, line_len, save_args, SAVE_ARGS_MAX)) {
  699. if (process_create_arg("save", program_save_main, save_args) < 0)
  700. io_putstr("error: too many processes\n");
  701. } else if (is_cmd_mkdir(line, line_len, rm_fname, RM_FNAME_MAX)) {
  702. if (process_create_arg("mkdir", program_mkdir_main, rm_fname) < 0)
  703. io_putstr("error: too many processes\n");
  704. } else if (is_cmd_rmdir(line, line_len, rm_fname, RM_FNAME_MAX)) {
  705. if (process_create_arg("rmdir", program_rmdir_main, rm_fname) < 0)
  706. io_putstr("error: too many processes\n");
  707. } else if (is_cmd_rm(line, line_len, rm_fname, RM_FNAME_MAX)) {
  708. if (process_create_arg("rm", program_rm_main, rm_fname) < 0)
  709. io_putstr("error: too many processes\n");
  710. } else if (is_cmd_space(line, line_len)) {
  711. if (process_create_arg("space", program_run_main, "space.bin") < 0)
  712. io_putstr("error: too many processes\n");
  713. } else if (is_cmd_display(line, line_len)) {
  714. if (process_create("display", program_display_main) < 0)
  715. io_putstr("error: too many processes\n");
  716. } else if (is_cmd_cpuinfo(line, line_len)) {
  717. if (process_create("cpuinfo", program_cpuinfo_main) < 0)
  718. io_putstr("error: too many processes\n");
  719. } else if (is_cmd_mouseinfo(line, line_len)) {
  720. if (process_create("mouseinfo", program_mouseinfo_main) < 0)
  721. io_putstr("error: too many processes\n");
  722. } else if (is_cmd_ps(line, line_len)) {
  723. process_ps();
  724. } else if (is_cmd_kill(line, line_len, &kill_pid)) {
  725. if (process_kill(kill_pid) < 0)
  726. io_putstr("kill: no such process\n");
  727. else
  728. io_putstr("killed\n");
  729. } else if (is_cmd_kexec(line, line_len)) {
  730. if (kexec_busy) {
  731. io_putstr("kexec: already receiving\n");
  732. } else {
  733. kexec_busy = 1;
  734. if (process_create("kexec", program_kexec_main) < 0) {
  735. kexec_busy = 0;
  736. io_putstr("error: too many processes\n");
  737. }
  738. }
  739. } else if (is_cmd_dbg(line, line_len)) {
  740. program_dbg_main();
  741. } else if (is_cmd_help(line, line_len)) {
  742. io_putstr("hello\n");
  743. io_putstr("reboot\n");
  744. io_putstr("poweroff\n");
  745. io_putstr("ls [dir]\n");
  746. io_putstr("cat <file>\n");
  747. io_putstr("save <f> <text>\n");
  748. io_putstr("rm <file>\n");
  749. io_putstr("mkdir <dir>\n");
  750. io_putstr("rmdir <dir>\n");
  751. io_putstr("run <file>\n");
  752. io_putstr("space\n");
  753. io_putstr("clock\n");
  754. io_putstr("colors\n");
  755. io_putstr("display\n");
  756. io_putstr("cpuinfo\n");
  757. io_putstr("lsusb\n");
  758. io_putstr("mouseinfo\n");
  759. io_putstr("ps\n");
  760. io_putstr("kill <pid>\n");
  761. io_putstr("kexec\n");
  762. io_putstr("dbg\n");
  763. io_putstr("serial test|recv\n");
  764. io_putstr("clear\n");
  765. } else if (is_cmd_clear(line, line_len)) {
  766. io_clear_screen();
  767. draw_header_row();
  768. io_set_cursor(IO_CONTENT_OFFSET);
  769. io_putstr(prompt_first);
  770. line_start = io_get_cursor();
  771. skip_prompt = 1;
  772. } else {
  773. static char serial_arg[8];
  774. if (is_cmd_serial(line, line_len, serial_arg, sizeof(serial_arg))) {
  775. if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
  776. serial_arg[2] == 's' && serial_arg[3] == 't' &&
  777. serial_arg[4] == '\0') {
  778. if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
  779. io_putstr("error: too many processes\n");
  780. } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
  781. serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
  782. serial_arg[4] == '\0') {
  783. program_serial_recv_main(serial_arg);
  784. } else {
  785. io_putstr("serial: usage serial test|recv\n");
  786. }
  787. } else {
  788. unsigned int i = 0;
  789. while (i < line_len && line[i] == ' ')
  790. i++;
  791. if (i < line_len)
  792. io_putstr("command not found\n");
  793. }
  794. }
  795. line_len = 0;
  796. line_pos = 0;
  797. line[0] = '\0';
  798. if (!skip_prompt) {
  799. io_putstr(prompt);
  800. line_start = io_get_cursor();
  801. }
  802. if (io_did_scroll())
  803. redraw_datetime();
  804. } else {
  805. {
  806. unsigned char u = (unsigned char)k;
  807. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  808. unsigned int i;
  809. history_browse = 0;
  810. for (i = line_len; i > line_pos; i--)
  811. line[i] = line[i - 1];
  812. line[line_pos] = (char)k;
  813. line_len++;
  814. line[line_len] = '\0';
  815. line_pos++;
  816. redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
  817. }
  818. }
  819. }
  820. } else {
  821. if (!kexec_busy && kexec_pending()) {
  822. kexec_busy = 1;
  823. if (process_create("kexec", program_kexec_main) < 0)
  824. kexec_busy = 0;
  825. }
  826. cpu_idle();
  827. }
  828. }
  829. }
  830. }