terminal.c 32 KB

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