terminal.c 29 KB

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