terminal.c 26 KB

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