terminal.c 27 KB

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