terminal.c 26 KB

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