terminal.c 32 KB

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