terminal.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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_run_embedded_main(const char *name);
  30. extern void program_lsusb_main(void);
  31. extern void program_storage_main(void);
  32. extern void program_mem_main(void);
  33. extern void program_format_main(const char *arg);
  34. extern void program_lspci_main(const char *filter);
  35. extern void program_peek_main(const char *arg);
  36. extern void program_poke_main(const char *arg);
  37. extern void program_netinfo_main(void);
  38. extern void program_netrecv_main(void);
  39. extern void program_netsend_main(void);
  40. extern void program_netip_main(const char *arg);
  41. extern void program_netping_main(const char *arg);
  42. extern void program_netlog_main(const char *arg);
  43. extern void program_netconsole_main(const char *arg);
  44. extern void program_dhcp_main(void);
  45. extern void program_netregs_main(void);
  46. extern void program_netloop_main(void);
  47. extern void program_netreset_main(void);
  48. extern void program_ping_main(void);
  49. /* Print the prompt (with the current directory) and set line_start. */
  50. static void term_print_prompt(struct term *t, int first)
  51. {
  52. if (!first)
  53. io_putchar('\n');
  54. io_putchar('[');
  55. io_putstr(t->cwd);
  56. io_putstr("] > ");
  57. t->line_start = io_get_cursor();
  58. }
  59. static int is_cmd_hello(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 + 5 > line_len)
  65. return 0;
  66. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  67. return 0;
  68. i += 5;
  69. while (i < line_len && line[i] == ' ')
  70. i++;
  71. return (i == line_len);
  72. }
  73. static int is_cmd_reboot(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 + 6 > line_len)
  79. return 0;
  80. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  81. return 0;
  82. i += 6;
  83. while (i < line_len && line[i] == ' ')
  84. i++;
  85. return (i == line_len);
  86. }
  87. static int is_cmd_poweroff(const char *line, unsigned int line_len)
  88. {
  89. unsigned int i = 0;
  90. while (i < line_len && line[i] == ' ')
  91. i++;
  92. if (i + 8 > line_len)
  93. return 0;
  94. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' ||
  95. line[i+4] != 'r' || line[i+5] != 'o' || line[i+6] != 'f' || line[i+7] != 'f')
  96. return 0;
  97. i += 8;
  98. while (i < line_len && line[i] == ' ')
  99. i++;
  100. return (i == line_len);
  101. }
  102. static int is_cmd_ls(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  103. {
  104. unsigned int i = 0, j = 0;
  105. while (i < line_len && line[i] == ' ')
  106. i++;
  107. if (i + 2 > line_len)
  108. return 0;
  109. if (line[i] != 'l' || line[i+1] != 's')
  110. return 0;
  111. i += 2;
  112. if (i < line_len && line[i] != ' ')
  113. return 0; /* "ls" must be a whole word, so reject "lsusb" etc. */
  114. while (i < line_len && line[i] == ' ')
  115. i++;
  116. if (i >= line_len) {
  117. if (buf_size > 0)
  118. path_buf[0] = '\0';
  119. return 1;
  120. }
  121. while (i < line_len && j + 1 < buf_size)
  122. path_buf[j++] = line[i++];
  123. path_buf[j] = '\0';
  124. while (i < line_len && line[i] == ' ')
  125. i++;
  126. return (i == line_len);
  127. }
  128. static int is_cmd_cd(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  129. {
  130. unsigned int i = 0, j = 0;
  131. while (i < line_len && line[i] == ' ')
  132. i++;
  133. if (i + 2 > line_len)
  134. return 0;
  135. if (line[i] != 'c' || line[i+1] != 'd')
  136. return 0;
  137. i += 2;
  138. if (i < line_len && line[i] != ' ')
  139. return 0; /* "cd" must be a whole word */
  140. while (i < line_len && line[i] == ' ')
  141. i++;
  142. if (i >= line_len) {
  143. if (buf_size > 0)
  144. path_buf[0] = '\0';
  145. return 1;
  146. }
  147. while (i < line_len && j + 1 < buf_size)
  148. path_buf[j++] = line[i++];
  149. path_buf[j] = '\0';
  150. while (i < line_len && line[i] == ' ')
  151. i++;
  152. return (i == line_len);
  153. }
  154. static int is_cmd_clock(const char *line, unsigned int line_len)
  155. {
  156. unsigned int i = 0;
  157. while (i < line_len && line[i] == ' ')
  158. i++;
  159. if (i + 5 > line_len)
  160. return 0;
  161. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  162. line[i+3] != 'c' || line[i+4] != 'k')
  163. return 0;
  164. i += 5;
  165. while (i < line_len && line[i] == ' ')
  166. i++;
  167. return (i == line_len);
  168. }
  169. static int is_cmd_colors(const char *line, unsigned int line_len)
  170. {
  171. unsigned int i = 0;
  172. while (i < line_len && line[i] == ' ')
  173. i++;
  174. if (i + 6 > line_len)
  175. return 0;
  176. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  177. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  178. return 0;
  179. i += 6;
  180. while (i < line_len && line[i] == ' ')
  181. i++;
  182. return (i == line_len);
  183. }
  184. static int is_cmd_display(const char *line, unsigned int line_len)
  185. {
  186. unsigned int i = 0;
  187. while (i < line_len && line[i] == ' ')
  188. i++;
  189. if (i + 7 > line_len)
  190. return 0;
  191. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  192. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  193. return 0;
  194. i += 7;
  195. while (i < line_len && line[i] == ' ')
  196. i++;
  197. return (i == line_len);
  198. }
  199. static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
  200. {
  201. unsigned int i = 0;
  202. while (i < line_len && line[i] == ' ')
  203. i++;
  204. if (i + 7 > line_len)
  205. return 0;
  206. if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
  207. line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
  208. return 0;
  209. i += 7;
  210. while (i < line_len && line[i] == ' ')
  211. i++;
  212. return (i == line_len);
  213. }
  214. static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
  215. {
  216. unsigned int i = 0;
  217. while (i < line_len && line[i] == ' ')
  218. i++;
  219. if (i + 9 > line_len)
  220. return 0;
  221. if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
  222. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
  223. line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
  224. return 0;
  225. i += 9;
  226. while (i < line_len && line[i] == ' ')
  227. i++;
  228. return (i == line_len);
  229. }
  230. static int is_cmd_lsusb(const char *line, unsigned int line_len)
  231. {
  232. unsigned int i = 0;
  233. while (i < line_len && line[i] == ' ')
  234. i++;
  235. if (i + 5 > line_len)
  236. return 0;
  237. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'u' ||
  238. line[i+3] != 's' || line[i+4] != 'b')
  239. return 0;
  240. i += 5;
  241. while (i < line_len && line[i] == ' ')
  242. i++;
  243. return (i == line_len);
  244. }
  245. static int is_cmd_lspci(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  246. {
  247. unsigned int i = 0, j = 0;
  248. while (i < line_len && line[i] == ' ')
  249. i++;
  250. if (i + 5 > line_len)
  251. return 0;
  252. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'p' ||
  253. line[i+3] != 'c' || line[i+4] != 'i')
  254. return 0;
  255. i += 5;
  256. if (i < line_len && line[i] != ' ')
  257. return 0; /* "lspci" must be a whole word */
  258. while (i < line_len && line[i] == ' ')
  259. i++;
  260. if (i >= line_len) {
  261. if (buf_size > 0)
  262. arg_buf[0] = '\0';
  263. return 1;
  264. }
  265. while (i < line_len && j + 1 < buf_size)
  266. arg_buf[j++] = line[i++];
  267. arg_buf[j] = '\0';
  268. while (i < line_len && line[i] == ' ')
  269. i++;
  270. return (i == line_len);
  271. }
  272. static int is_cmd_peek(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  273. {
  274. unsigned int i = 0, j = 0;
  275. while (i < line_len && line[i] == ' ')
  276. i++;
  277. if (i + 4 > line_len)
  278. return 0;
  279. if (line[i] != 'p' || line[i+1] != 'e' || line[i+2] != 'e' ||
  280. line[i+3] != 'k')
  281. return 0;
  282. i += 4;
  283. if (i < line_len && line[i] != ' ')
  284. return 0; /* "peek" must be a whole word */
  285. while (i < line_len && line[i] == ' ')
  286. i++;
  287. if (i >= line_len) {
  288. if (buf_size > 0)
  289. arg_buf[0] = '\0';
  290. return 1;
  291. }
  292. while (i < line_len && j + 1 < buf_size)
  293. arg_buf[j++] = line[i++];
  294. arg_buf[j] = '\0';
  295. while (i < line_len && line[i] == ' ')
  296. i++;
  297. return (i == line_len);
  298. }
  299. /* poke <addr> <val>: write one dword to memory/MMIO. */
  300. static int is_cmd_poke(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  301. {
  302. unsigned int i = 0, j = 0;
  303. while (i < line_len && line[i] == ' ')
  304. i++;
  305. if (i + 4 > line_len)
  306. return 0;
  307. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'k' ||
  308. line[i+3] != 'e')
  309. return 0;
  310. i += 4;
  311. if (i < line_len && line[i] != ' ')
  312. return 0; /* "poke" must be a whole word */
  313. while (i < line_len && line[i] == ' ')
  314. i++;
  315. if (i >= line_len)
  316. return 0; /* address and value required */
  317. while (i < line_len && j + 1 < buf_size)
  318. arg_buf[j++] = line[i++];
  319. arg_buf[j] = '\0';
  320. while (i < line_len && line[i] == ' ')
  321. i++;
  322. return (i == line_len);
  323. }
  324. static int is_cmd_prog(const char *line, unsigned int line_len){
  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] != 'r' || line[i+2] != 'o' ||
  331. line[i+3] != 'g')
  332. return 0;
  333. i += 4;
  334. while (i < line_len && line[i] == ' ')
  335. i++;
  336. return (i == line_len);
  337. }
  338. static int is_cmd_netinfo(const char *line, unsigned int line_len)
  339. {
  340. unsigned int i = 0;
  341. while (i < line_len && line[i] == ' ')
  342. i++;
  343. if (i + 7 > line_len)
  344. return 0;
  345. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  346. line[i+3] != 'i' || line[i+4] != 'n' || line[i+5] != 'f' ||
  347. line[i+6] != 'o')
  348. return 0;
  349. i += 7;
  350. while (i < line_len && line[i] == ' ')
  351. i++;
  352. return (i == line_len);
  353. }
  354. static int is_cmd_netlog(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 + 6 > line_len)
  360. return 0;
  361. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  362. line[i+3] != 'l' || line[i+4] != 'o' || line[i+5] != 'g')
  363. return 0;
  364. i += 6;
  365. if (i < line_len && line[i] != ' ')
  366. return 0; /* "netlog" must be a whole word */
  367. while (i < line_len && line[i] == ' ')
  368. i++;
  369. if (i >= line_len) {
  370. if (buf_size > 0)
  371. arg_buf[0] = '\0';
  372. return 1;
  373. }
  374. while (i < line_len && j + 1 < buf_size)
  375. arg_buf[j++] = line[i++];
  376. arg_buf[j] = '\0';
  377. while (i < line_len && line[i] == ' ')
  378. i++;
  379. return (i == line_len);
  380. }
  381. static int is_cmd_netconsole(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  382. {
  383. unsigned int i = 0, j = 0;
  384. while (i < line_len && line[i] == ' ')
  385. i++;
  386. if (i + 10 > line_len)
  387. return 0;
  388. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  389. line[i+3] != 'c' || line[i+4] != 'o' || line[i+5] != 'n' ||
  390. line[i+6] != 's' || line[i+7] != 'o' || line[i+8] != 'l' ||
  391. line[i+9] != 'e')
  392. return 0;
  393. i += 10;
  394. if (i < line_len && line[i] != ' ')
  395. return 0; /* "netconsole" must be a whole word */
  396. while (i < line_len && line[i] == ' ')
  397. i++;
  398. if (i >= line_len) {
  399. if (buf_size > 0)
  400. arg_buf[0] = '\0';
  401. return 1;
  402. }
  403. while (i < line_len && j + 1 < buf_size)
  404. arg_buf[j++] = line[i++];
  405. arg_buf[j] = '\0';
  406. while (i < line_len && line[i] == ' ')
  407. i++;
  408. return (i == line_len);
  409. }
  410. static int is_cmd_dhcp(const char *line, unsigned int line_len)
  411. {
  412. unsigned int i = 0;
  413. while (i < line_len && line[i] == ' ')
  414. i++;
  415. if (i + 4 > line_len)
  416. return 0;
  417. if (line[i] != 'd' || line[i+1] != 'h' || line[i+2] != 'c' ||
  418. line[i+3] != 'p')
  419. return 0;
  420. i += 4;
  421. while (i < line_len && line[i] == ' ')
  422. i++;
  423. return (i == line_len);
  424. }
  425. static int is_cmd_netregs(const char *line, unsigned int line_len)
  426. {
  427. unsigned int i = 0;
  428. while (i < line_len && line[i] == ' ')
  429. i++;
  430. if (i + 7 > line_len)
  431. return 0;
  432. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  433. line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 'g' ||
  434. line[i+6] != 's')
  435. return 0;
  436. i += 7;
  437. while (i < line_len && line[i] == ' ')
  438. i++;
  439. return (i == line_len);
  440. }
  441. static int is_cmd_netloop(const char *line, unsigned int line_len)
  442. {
  443. unsigned int i = 0;
  444. while (i < line_len && line[i] == ' ')
  445. i++;
  446. if (i + 7 > line_len)
  447. return 0;
  448. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  449. line[i+3] != 'l' || line[i+4] != 'o' || line[i+5] != 'o' ||
  450. line[i+6] != 'p')
  451. return 0;
  452. i += 7;
  453. while (i < line_len && line[i] == ' ')
  454. i++;
  455. return (i == line_len);
  456. }
  457. static int is_cmd_netreset(const char *line, unsigned int line_len)
  458. {
  459. unsigned int i = 0;
  460. while (i < line_len && line[i] == ' ')
  461. i++;
  462. if (i + 8 > line_len)
  463. return 0;
  464. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  465. line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 's' ||
  466. line[i+6] != 'e' || line[i+7] != 't')
  467. return 0;
  468. i += 8;
  469. while (i < line_len && line[i] == ' ')
  470. i++;
  471. return (i == line_len);
  472. }
  473. static int is_cmd_netrecv(const char *line, unsigned int line_len)
  474. {
  475. unsigned int i = 0;
  476. while (i < line_len && line[i] == ' ')
  477. i++;
  478. if (i + 7 > line_len)
  479. return 0;
  480. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  481. line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 'c' ||
  482. line[i+6] != 'v')
  483. return 0;
  484. i += 7;
  485. while (i < line_len && line[i] == ' ')
  486. i++;
  487. return (i == line_len);
  488. }
  489. static int is_cmd_netsend(const char *line, unsigned int line_len)
  490. {
  491. unsigned int i = 0;
  492. while (i < line_len && line[i] == ' ')
  493. i++;
  494. if (i + 7 > line_len)
  495. return 0;
  496. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  497. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'n' ||
  498. line[i+6] != 'd')
  499. return 0;
  500. i += 7;
  501. while (i < line_len && line[i] == ' ')
  502. i++;
  503. return (i == line_len);
  504. }
  505. static int is_cmd_netip(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  506. {
  507. unsigned int i = 0, j = 0;
  508. while (i < line_len && line[i] == ' ')
  509. i++;
  510. if (i + 5 > line_len)
  511. return 0;
  512. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  513. line[i+3] != 'i' || line[i+4] != 'p')
  514. return 0;
  515. i += 5;
  516. if (i < line_len && line[i] != ' ')
  517. return 0; /* "netip" must be a whole word */
  518. while (i < line_len && line[i] == ' ')
  519. i++;
  520. if (i >= line_len) {
  521. if (buf_size > 0)
  522. arg_buf[0] = '\0';
  523. return 1;
  524. }
  525. while (i < line_len && j + 1 < buf_size)
  526. arg_buf[j++] = line[i++];
  527. arg_buf[j] = '\0';
  528. while (i < line_len && line[i] == ' ')
  529. i++;
  530. return (i == line_len);
  531. }
  532. static int is_cmd_netping(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  533. {
  534. unsigned int i = 0, j = 0;
  535. while (i < line_len && line[i] == ' ')
  536. i++;
  537. if (i + 7 > line_len)
  538. return 0;
  539. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  540. line[i+3] != 'p' || line[i+4] != 'i' || line[i+5] != 'n' ||
  541. line[i+6] != 'g')
  542. return 0;
  543. i += 7;
  544. if (i < line_len && line[i] != ' ')
  545. return 0; /* "netping" must be a whole word */
  546. while (i < line_len && line[i] == ' ')
  547. i++;
  548. if (i >= line_len) {
  549. if (buf_size > 0)
  550. arg_buf[0] = '\0';
  551. return 1;
  552. }
  553. while (i < line_len && j + 1 < buf_size)
  554. arg_buf[j++] = line[i++];
  555. arg_buf[j] = '\0';
  556. while (i < line_len && line[i] == ' ')
  557. i++;
  558. return (i == line_len);
  559. }
  560. static int is_cmd_ping(const char *line, unsigned int line_len)
  561. {
  562. unsigned int i = 0;
  563. while (i < line_len && line[i] == ' ')
  564. i++;
  565. if (i + 4 > line_len)
  566. return 0;
  567. if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' || line[i+3] != 'g')
  568. return 0;
  569. i += 4;
  570. while (i < line_len && line[i] == ' ')
  571. i++;
  572. return (i == line_len);
  573. }
  574. static int is_cmd_space(const char *line, unsigned int line_len)
  575. {
  576. unsigned int i = 0;
  577. while (i < line_len && line[i] == ' ')
  578. i++;
  579. if (i + 5 > line_len)
  580. return 0;
  581. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  582. line[i+3] != 'c' || line[i+4] != 'e')
  583. return 0;
  584. i += 5;
  585. while (i < line_len && line[i] == ' ')
  586. i++;
  587. return (i == line_len);
  588. }
  589. #define NET_UDP_ARG_MAX 128
  590. #define CAT_FNAME_MAX 64
  591. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  592. {
  593. unsigned int i = 0, j;
  594. while (i < line_len && line[i] == ' ')
  595. i++;
  596. if (i + 3 > line_len)
  597. return 0;
  598. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  599. return 0;
  600. i += 3;
  601. while (i < line_len && line[i] == ' ')
  602. i++;
  603. if (i >= line_len)
  604. return 0;
  605. j = 0;
  606. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  607. filename_buf[j++] = line[i++];
  608. filename_buf[j] = '\0';
  609. return (j > 0);
  610. }
  611. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  612. {
  613. unsigned int i = 0, j;
  614. while (i < line_len && line[i] == ' ')
  615. i++;
  616. if (i + 3 > line_len)
  617. return 0;
  618. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  619. return 0;
  620. i += 3;
  621. while (i < line_len && line[i] == ' ')
  622. i++;
  623. if (i >= line_len)
  624. return 0;
  625. j = 0;
  626. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  627. filename_buf[j++] = line[i++];
  628. filename_buf[j] = '\0';
  629. return (j > 0);
  630. }
  631. #define SAVE_ARGS_MAX 128
  632. #define RM_FNAME_MAX 64
  633. #define LS_PATH_MAX 64
  634. #define FORMAT_DEV_MAX 16
  635. static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  636. {
  637. unsigned int i = 0, j = 0;
  638. while (i < line_len && line[i] == ' ')
  639. i++;
  640. if (i + 4 > line_len)
  641. return 0;
  642. if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
  643. return 0;
  644. i += 4;
  645. while (i < line_len && line[i] == ' ')
  646. i++;
  647. if (i >= line_len)
  648. return 0;
  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 && j > 0);
  657. }
  658. static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  659. {
  660. unsigned int i = 0, j = 0;
  661. while (i < line_len && line[i] == ' ')
  662. i++;
  663. if (i + 2 > line_len)
  664. return 0;
  665. if (line[i] != 'r' || line[i+1] != 'm')
  666. return 0;
  667. i += 2;
  668. while (i < line_len && line[i] == ' ')
  669. i++;
  670. if (i >= line_len)
  671. return 0;
  672. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  673. filename_buf[j++] = line[i++];
  674. filename_buf[j] = '\0';
  675. return (j > 0);
  676. }
  677. static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  678. {
  679. unsigned int i = 0, j = 0;
  680. while (i < line_len && line[i] == ' ')
  681. i++;
  682. if (i + 5 > line_len)
  683. return 0;
  684. if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  685. return 0;
  686. i += 5;
  687. while (i < line_len && line[i] == ' ')
  688. i++;
  689. if (i >= line_len)
  690. return 0;
  691. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  692. path_buf[j++] = line[i++];
  693. path_buf[j] = '\0';
  694. return (j > 0);
  695. }
  696. static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  697. {
  698. unsigned int i = 0, j = 0;
  699. while (i < line_len && line[i] == ' ')
  700. i++;
  701. if (i + 5 > line_len)
  702. return 0;
  703. if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  704. return 0;
  705. i += 5;
  706. while (i < line_len && line[i] == ' ')
  707. i++;
  708. if (i >= line_len)
  709. return 0;
  710. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  711. path_buf[j++] = line[i++];
  712. path_buf[j] = '\0';
  713. return (j > 0);
  714. }
  715. static int is_cmd_storage(const char *line, unsigned int line_len)
  716. {
  717. unsigned int i = 0;
  718. while (i < line_len && line[i] == ' ')
  719. i++;
  720. if (i + 7 > line_len)
  721. return 0;
  722. if (line[i] != 's' || line[i+1] != 't' || line[i+2] != 'o' ||
  723. line[i+3] != 'r' || line[i+4] != 'a' || line[i+5] != 'g' || line[i+6] != 'e')
  724. return 0;
  725. i += 7;
  726. while (i < line_len && line[i] == ' ')
  727. i++;
  728. return (i == line_len);
  729. }
  730. static int is_cmd_mem(const char *line, unsigned int line_len)
  731. {
  732. unsigned int i = 0;
  733. while (i < line_len && line[i] == ' ')
  734. i++;
  735. if (i + 3 > line_len)
  736. return 0;
  737. if (line[i] != 'm' || line[i+1] != 'e' || line[i+2] != 'm')
  738. return 0;
  739. i += 3;
  740. while (i < line_len && line[i] == ' ')
  741. i++;
  742. return (i == line_len);
  743. }
  744. static int is_cmd_format(const char *line, unsigned int line_len, char *dev_buf, unsigned int buf_size)
  745. {
  746. unsigned int i = 0, j;
  747. while (i < line_len && line[i] == ' ')
  748. i++;
  749. if (i + 6 > line_len)
  750. return 0;
  751. if (line[i] != 'f' || line[i+1] != 'o' || line[i+2] != 'r' ||
  752. line[i+3] != 'm' || line[i+4] != 'a' || line[i+5] != 't')
  753. return 0;
  754. i += 6;
  755. if (i < line_len && line[i] != ' ')
  756. return 0; /* "format" must be a whole word */
  757. while (i < line_len && line[i] == ' ')
  758. i++;
  759. if (i >= line_len)
  760. return 0;
  761. j = 0;
  762. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  763. dev_buf[j++] = line[i++];
  764. dev_buf[j] = '\0';
  765. while (i < line_len && line[i] == ' ')
  766. i++;
  767. return (i == line_len && j > 0);
  768. }
  769. /* Help listing: printed as two 32-column columns so it fits the 64-column
  770. terminal without scrolling. */
  771. static const char *const help_items[] = {
  772. "cd [path]", "ls [path]", "cat <path>", "save <path> <text>",
  773. "rm <path>", "mkdir <path>", "rmdir <path>", "hello", "reboot",
  774. "poweroff", "run <name>|<file>", "space", "prog", "clock", "colors",
  775. "display", "cpuinfo", "lsusb", "mouseinfo", "storage", "mem",
  776. "format <device>", "lspci [id]", "peek <addr> [len]", "poke <addr> <val>",
  777. "netinfo",
  778. "netrecv", "netsend", "netip [a.b.c.d]", "netping <a.b.c.d>",
  779. "netlog <text>", "netconsole [on|off|host <ip>]", "dhcp", "netregs",
  780. "netloop", "netreset", "ping", "ps", "kill <pid>", "kexec", "dbg",
  781. "serial test|recv", "clear",
  782. };
  783. static void help_puts(const char *s)
  784. {
  785. while (*s)
  786. io_putchar(*s++);
  787. }
  788. static void help_item_pad(const char *s)
  789. {
  790. unsigned int n = 0;
  791. while (s[n]) {
  792. io_putchar(s[n]);
  793. n++;
  794. }
  795. while (n < 32) {
  796. io_putchar(' ');
  797. n++;
  798. }
  799. }
  800. static void help_print(void)
  801. {
  802. unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
  803. unsigned int half = (nitems + 1) / 2;
  804. unsigned int k;
  805. for (k = 0; k < half; k++) {
  806. if (k + half < nitems) {
  807. help_item_pad(help_items[k]);
  808. help_puts(help_items[k + half]);
  809. } else {
  810. help_puts(help_items[k]);
  811. }
  812. io_putchar('\n');
  813. }
  814. }
  815. static int is_cmd_help(const char *line, unsigned int line_len)
  816. {
  817. unsigned int i = 0;
  818. while (i < line_len && line[i] == ' ')
  819. i++;
  820. if (i + 4 > line_len)
  821. return 0;
  822. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  823. return 0;
  824. i += 4;
  825. while (i < line_len && line[i] == ' ')
  826. i++;
  827. return (i == line_len);
  828. }
  829. static int is_cmd_clear(const char *line, unsigned int line_len)
  830. {
  831. unsigned int i = 0;
  832. while (i < line_len && line[i] == ' ')
  833. i++;
  834. if (i + 5 > line_len)
  835. return 0;
  836. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  837. line[i+3] != 'a' || line[i+4] != 'r')
  838. return 0;
  839. i += 5;
  840. while (i < line_len && line[i] == ' ')
  841. i++;
  842. return (i == line_len);
  843. }
  844. static int is_cmd_ps(const char *line, unsigned int line_len)
  845. {
  846. unsigned int i = 0;
  847. while (i < line_len && line[i] == ' ')
  848. i++;
  849. if (i + 2 > line_len)
  850. return 0;
  851. if (line[i] != 'p' || line[i+1] != 's')
  852. return 0;
  853. i += 2;
  854. while (i < line_len && line[i] == ' ')
  855. i++;
  856. return (i == line_len);
  857. }
  858. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  859. {
  860. unsigned int i = 0, num = 0;
  861. int has_digit = 0;
  862. while (i < line_len && line[i] == ' ')
  863. i++;
  864. if (i + 4 > line_len)
  865. return 0;
  866. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  867. return 0;
  868. i += 4;
  869. while (i < line_len && line[i] == ' ')
  870. i++;
  871. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  872. num = num * 10 + (unsigned int)(line[i] - '0');
  873. has_digit = 1;
  874. i++;
  875. }
  876. if (!has_digit)
  877. return 0;
  878. while (i < line_len && line[i] == ' ')
  879. i++;
  880. if (i != line_len)
  881. return 0;
  882. *pid_out = num;
  883. return 1;
  884. }
  885. static int is_cmd_kexec(const char *line, unsigned int line_len)
  886. {
  887. unsigned int i = 0;
  888. while (i < line_len && line[i] == ' ')
  889. i++;
  890. if (i + 5 > line_len)
  891. return 0;
  892. if (line[i] != 'k' || line[i+1] != 'e' || line[i+2] != 'x' || line[i+3] != 'e' || line[i+4] != 'c')
  893. return 0;
  894. i += 5;
  895. while (i < line_len && line[i] == ' ')
  896. i++;
  897. return (i == line_len);
  898. }
  899. static int is_cmd_dbg(const char *line, unsigned int line_len)
  900. {
  901. unsigned int i = 0;
  902. while (i < line_len && line[i] == ' ')
  903. i++;
  904. if (i + 3 > line_len)
  905. return 0;
  906. if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
  907. return 0;
  908. i += 3;
  909. while (i < line_len && line[i] == ' ')
  910. i++;
  911. return (i == line_len);
  912. }
  913. static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  914. {
  915. unsigned int i = 0, j = 0;
  916. while (i < line_len && line[i] == ' ')
  917. i++;
  918. if (i + 6 > line_len)
  919. return 0;
  920. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
  921. line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
  922. return 0;
  923. i += 6;
  924. while (i < line_len && line[i] == ' ')
  925. i++;
  926. if (i >= line_len) {
  927. if (buf_size > 0)
  928. arg_buf[0] = '\0';
  929. return 1;
  930. }
  931. while (i < line_len && j + 1 < buf_size)
  932. arg_buf[j++] = line[i++];
  933. while (j > 0 && arg_buf[j - 1] == ' ')
  934. j--;
  935. arg_buf[j] = '\0';
  936. while (i < line_len && line[i] == ' ')
  937. i++;
  938. return (i == line_len);
  939. }
  940. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  941. static void redraw_line_from(const char *line, unsigned int line_len,
  942. unsigned int line_pos, unsigned int line_start, unsigned int from)
  943. {
  944. unsigned int i;
  945. io_set_cursor(line_start + from);
  946. for (i = from; i < line_len; i++)
  947. io_putchar(line[i]);
  948. io_putchar(' ');
  949. io_set_cursor(line_start + line_pos);
  950. }
  951. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  952. static void redraw_line_clear(const char *line, unsigned int line_len,
  953. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  954. {
  955. unsigned int i;
  956. io_set_cursor(line_start);
  957. for (i = 0; i < line_len; i++)
  958. io_putchar(line[i]);
  959. for (i = line_len; i <= clear_through; i++)
  960. io_putchar(' ');
  961. io_set_cursor(line_start + line_pos);
  962. }
  963. /* Copy history entry (1 = newest) into t->line, redraw from start. */
  964. static void history_load_into_line(struct term *t, unsigned int browse)
  965. {
  966. unsigned int slot, len, old_len;
  967. const char *src;
  968. if (browse < 1 || browse > t->history_count)
  969. return;
  970. old_len = t->line_len;
  971. slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  972. src = t->history[slot];
  973. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  974. t->line[len] = src[len];
  975. t->line[len] = '\0';
  976. t->line_len = len;
  977. t->line_pos = len;
  978. redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
  979. }
  980. /* Push current line to history if non-empty (and not duplicate of newest). */
  981. static void history_push(struct term *t, const char *line, unsigned int line_len)
  982. {
  983. unsigned int i, slot;
  984. int all_spaces = 1;
  985. if (line_len == 0)
  986. return;
  987. for (i = 0; i < line_len; i++) {
  988. if (line[i] != ' ') {
  989. all_spaces = 0;
  990. break;
  991. }
  992. }
  993. if (all_spaces)
  994. return;
  995. if (t->history_count > 0) {
  996. slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  997. for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
  998. if (t->history[slot][i] != line[i])
  999. break;
  1000. }
  1001. if (i == line_len && t->history[slot][i] == '\0')
  1002. return; /* duplicate of newest */
  1003. }
  1004. slot = t->history_next;
  1005. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  1006. t->history[slot][i] = line[i];
  1007. t->history[slot][i] = '\0';
  1008. t->history_next = (t->history_next + 1) % HISTORY_SIZE;
  1009. if (t->history_count < HISTORY_SIZE)
  1010. t->history_count++;
  1011. }
  1012. /* Change directory: resolve `arg` (relative to t->cwd, or absolute) and
  1013. * validate it is an existing directory. Updates t->cwd on success. */
  1014. static void term_cd(struct term *t, const char *arg)
  1015. {
  1016. char abs[VFS_PATH_MAX];
  1017. fat_fs_t *fs;
  1018. const char *fs_path;
  1019. unsigned int dir;
  1020. int r;
  1021. unsigned int i;
  1022. if (arg[0] == '\0') {
  1023. t->cwd[0] = '/';
  1024. t->cwd[1] = '\0';
  1025. return;
  1026. }
  1027. if (vfs_join_path(t->cwd, arg, abs, sizeof(abs)) != 0) {
  1028. io_putstr("cd: path too long\n");
  1029. return;
  1030. }
  1031. if (abs[0] == '/' && abs[1] == '\0') {
  1032. t->cwd[0] = '/';
  1033. t->cwd[1] = '\0';
  1034. return;
  1035. }
  1036. if (vfs_resolve(abs, &fs, &fs_path) != 0) {
  1037. io_putstr("cd: no such directory\n");
  1038. return;
  1039. }
  1040. r = fat_resolve_dir(fs, fs_path, &dir);
  1041. if (r == -2) {
  1042. io_putstr("cd: disk error\n");
  1043. return;
  1044. }
  1045. if (r != 0) {
  1046. io_putstr("cd: no such directory\n");
  1047. return;
  1048. }
  1049. for (i = 0; abs[i] && i < sizeof(t->cwd) - 1; i++)
  1050. t->cwd[i] = abs[i];
  1051. t->cwd[i] = '\0';
  1052. }
  1053. /* Resolve the first token (path) of `args` against t->cwd, rebuilding
  1054. * "abs-path rest-of-args" into out. Used by `save`. */
  1055. static void term_resolve_first(struct term *t, const char *args,
  1056. char *out, unsigned int out_size)
  1057. {
  1058. char path[VFS_PATH_MAX];
  1059. char abs[VFS_PATH_MAX];
  1060. unsigned int i = 0, j = 0;
  1061. while (args[i] == ' ')
  1062. i++;
  1063. while (args[i] && args[i] != ' ' && j < sizeof(path) - 1)
  1064. path[j++] = args[i++];
  1065. path[j] = '\0';
  1066. if (vfs_join_path(t->cwd, path, abs, sizeof(abs)) != 0) {
  1067. abs[0] = '/';
  1068. abs[1] = '\0';
  1069. }
  1070. while (args[i] == ' ')
  1071. i++;
  1072. j = 0;
  1073. {
  1074. unsigned int k = 0;
  1075. while (abs[k] && j + 1 < out_size)
  1076. out[j++] = abs[k++];
  1077. }
  1078. if (args[i]) {
  1079. if (j + 1 < out_size)
  1080. out[j++] = ' ';
  1081. while (args[i] && j + 1 < out_size)
  1082. out[j++] = args[i++];
  1083. }
  1084. out[j] = '\0';
  1085. }
  1086. /* Spawn a foreground command process; the caller (term_run's wait loop)
  1087. * waits for it to finish and handles Ctrl+C. */
  1088. static void term_spawn(struct term *t, const char *name, void (*entry)(void))
  1089. {
  1090. int pid = process_create(name, entry);
  1091. if (pid < 0)
  1092. io_putstr("error: too many processes\n");
  1093. else
  1094. t->fg_pid = pid;
  1095. }
  1096. static void term_spawn_arg(struct term *t, const char *name,
  1097. void (*entry)(const char *), const char *arg)
  1098. {
  1099. int pid = process_create_arg(name, entry, arg);
  1100. if (pid < 0)
  1101. io_putstr("error: too many processes\n");
  1102. else
  1103. t->fg_pid = pid;
  1104. }
  1105. void term_run(struct term *t)
  1106. {
  1107. t->line_len = 0;
  1108. t->line_pos = 0;
  1109. t->line[0] = '\0';
  1110. t->history_browse = 0;
  1111. t->fg_pid = -1;
  1112. io_clear_screen();
  1113. term_print_prompt(t, 1);
  1114. for (;;) {
  1115. if (t->fg_pid >= 0) {
  1116. /* Waiting on a foreground child: poll keys for Ctrl+C
  1117. * and watch for the child to exit. */
  1118. int k = term_key_pop(t);
  1119. if (k == IO_KEY_CTRL_C) {
  1120. int killed = (process_kill((unsigned int)t->fg_pid) == 0);
  1121. t->fg_pid = -1;
  1122. if (killed)
  1123. io_putstr("^C\n");
  1124. term_print_prompt(t, 0);
  1125. } else if (!process_is_alive((unsigned int)t->fg_pid)) {
  1126. t->fg_pid = -1;
  1127. term_print_prompt(t, 0);
  1128. } else {
  1129. process_sleep_ms(10);
  1130. }
  1131. continue;
  1132. }
  1133. {
  1134. int k = term_key_pop(t);
  1135. if (k) {
  1136. kdbg_term_count++;
  1137. kdbg_term_last = (unsigned int)k;
  1138. kdbg_status();
  1139. if (k == IO_KEY_CTRL_C) {
  1140. /* Ctrl+C at the prompt: discard the line. */
  1141. io_putstr("^C\n");
  1142. t->line_len = 0;
  1143. t->line_pos = 0;
  1144. t->line[0] = '\0';
  1145. term_print_prompt(t, 0);
  1146. } else if (k == IO_KEY_LEFT) {
  1147. if (t->line_pos > 0) {
  1148. t->line_pos--;
  1149. io_set_cursor(t->line_start + t->line_pos);
  1150. }
  1151. } else if (k == IO_KEY_RIGHT) {
  1152. if (t->line_pos < t->line_len) {
  1153. t->line_pos++;
  1154. io_set_cursor(t->line_start + t->line_pos);
  1155. }
  1156. } else if (k == IO_KEY_UP) {
  1157. if (t->history_count > 0) {
  1158. if (t->history_browse < t->history_count)
  1159. t->history_browse++;
  1160. history_load_into_line(t, t->history_browse);
  1161. }
  1162. } else if (k == IO_KEY_DOWN) {
  1163. if (t->history_browse > 0) {
  1164. t->history_browse--;
  1165. if (t->history_browse == 0) {
  1166. unsigned int clear_len = t->line_len;
  1167. t->line_len = 0;
  1168. t->line_pos = 0;
  1169. t->line[0] = '\0';
  1170. redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
  1171. } else {
  1172. history_load_into_line(t, t->history_browse);
  1173. }
  1174. }
  1175. } else if (k == '\b') {
  1176. if (t->line_pos > 0) {
  1177. unsigned int i;
  1178. t->line_pos--;
  1179. for (i = t->line_pos; i < t->line_len; i++)
  1180. t->line[i] = t->line[i + 1];
  1181. t->line_len--;
  1182. t->line[t->line_len] = '\0';
  1183. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
  1184. }
  1185. } else if (k == '\n') {
  1186. char cat_fname[CAT_FNAME_MAX];
  1187. char save_args[SAVE_ARGS_MAX];
  1188. char rm_fname[RM_FNAME_MAX];
  1189. char ls_path[LS_PATH_MAX];
  1190. char format_dev[FORMAT_DEV_MAX];
  1191. char netip_arg[16];
  1192. char netping_arg[16];
  1193. char netlog_arg[NET_UDP_ARG_MAX];
  1194. char netconsole_arg[NET_UDP_ARG_MAX];
  1195. char lspci_arg[16];
  1196. char peek_arg[32];
  1197. char resolved[VFS_PATH_MAX];
  1198. char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
  1199. unsigned int kill_pid;
  1200. int skip_prompt = 0;
  1201. io_putchar('\n');
  1202. history_push(t, t->line, t->line_len);
  1203. t->history_browse = 0;
  1204. if (is_cmd_hello(t->line, t->line_len)) {
  1205. int pid = process_create_user("hello",
  1206. _binary_hello_elf_start,
  1207. (unsigned int)(_binary_hello_elf_end -
  1208. _binary_hello_elf_start));
  1209. if (pid < 0)
  1210. io_putstr("error: too many processes\n");
  1211. else
  1212. t->fg_pid = pid;
  1213. } else if (is_cmd_reboot(t->line, t->line_len)) {
  1214. program_reboot_main();
  1215. } else if (is_cmd_poweroff(t->line, t->line_len)) {
  1216. program_poweroff_main();
  1217. } else if (is_cmd_cd(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  1218. term_cd(t, ls_path);
  1219. } else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  1220. if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
  1221. term_spawn_arg(t, "ls", program_ls_main, resolved);
  1222. } else {
  1223. io_putstr("ls: path too long\n");
  1224. }
  1225. } else if (is_cmd_lsusb(t->line, t->line_len)) {
  1226. term_spawn(t, "lsusb", program_lsusb_main);
  1227. } else if (is_cmd_lspci(t->line, t->line_len, lspci_arg, sizeof(lspci_arg))) {
  1228. term_spawn_arg(t, "lspci", program_lspci_main, lspci_arg);
  1229. } else if (is_cmd_peek(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
  1230. term_spawn_arg(t, "peek", program_peek_main, peek_arg);
  1231. } else if (is_cmd_poke(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
  1232. term_spawn_arg(t, "poke", program_poke_main, peek_arg);
  1233. } else if (is_cmd_netinfo(t->line, t->line_len)) {
  1234. term_spawn(t, "netinfo", program_netinfo_main);
  1235. } else if (is_cmd_netrecv(t->line, t->line_len)) {
  1236. term_spawn(t, "netrecv", program_netrecv_main);
  1237. } else if (is_cmd_netsend(t->line, t->line_len)) {
  1238. term_spawn(t, "netsend", program_netsend_main);
  1239. } else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
  1240. term_spawn_arg(t, "netip", program_netip_main, netip_arg);
  1241. } else if (is_cmd_netping(t->line, t->line_len, netping_arg, sizeof(netping_arg))) {
  1242. term_spawn_arg(t, "netping", program_netping_main, netping_arg);
  1243. } else if (is_cmd_netlog(t->line, t->line_len, netlog_arg, sizeof(netlog_arg))) {
  1244. term_spawn_arg(t, "netlog", program_netlog_main, netlog_arg);
  1245. } else if (is_cmd_netconsole(t->line, t->line_len, netconsole_arg, sizeof(netconsole_arg))) {
  1246. term_spawn_arg(t, "netconsole", program_netconsole_main, netconsole_arg);
  1247. } else if (is_cmd_dhcp(t->line, t->line_len)) {
  1248. term_spawn(t, "dhcp", program_dhcp_main);
  1249. } else if (is_cmd_netregs(t->line, t->line_len)) {
  1250. term_spawn(t, "netregs", program_netregs_main);
  1251. } else if (is_cmd_netloop(t->line, t->line_len)) {
  1252. term_spawn(t, "netloop", program_netloop_main);
  1253. } else if (is_cmd_netreset(t->line, t->line_len)) {
  1254. term_spawn(t, "netreset", program_netreset_main);
  1255. } else if (is_cmd_ping(t->line, t->line_len)) {
  1256. term_spawn(t, "ping", program_ping_main);
  1257. } else if (is_cmd_storage(t->line, t->line_len)) {
  1258. term_spawn(t, "storage", program_storage_main);
  1259. } else if (is_cmd_mem(t->line, t->line_len)) {
  1260. term_spawn(t, "mem", program_mem_main);
  1261. } else if (is_cmd_clock(t->line, t->line_len)) {
  1262. io_putstr("clock: not available in windowed mode yet\n");
  1263. } else if (is_cmd_colors(t->line, t->line_len)) {
  1264. io_putstr("colors: not available in windowed mode yet\n");
  1265. } else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  1266. if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
  1267. term_spawn_arg(t, "cat", program_cat_main, resolved);
  1268. } else {
  1269. io_putstr("cat: path too long\n");
  1270. }
  1271. } else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  1272. if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
  1273. cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
  1274. cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
  1275. cat_fname[6] == '\0') {
  1276. if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
  1277. io_putstr("error: too many processes\n");
  1278. } else {
  1279. io_putstr("run: not available in windowed mode yet\n");
  1280. }
  1281. } else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
  1282. term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
  1283. term_spawn_arg(t, "save", program_save_main, save_resolved);
  1284. } else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1285. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1286. term_spawn_arg(t, "mkdir", program_mkdir_main, resolved);
  1287. } else {
  1288. io_putstr("mkdir: path too long\n");
  1289. }
  1290. } else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1291. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1292. term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
  1293. } else {
  1294. io_putstr("rmdir: path too long\n");
  1295. }
  1296. } else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1297. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1298. term_spawn_arg(t, "rm", program_rm_main, resolved);
  1299. } else {
  1300. io_putstr("rm: path too long\n");
  1301. }
  1302. } else if (is_cmd_space(t->line, t->line_len)) {
  1303. term_spawn_arg(t, "space", program_run_embedded_main, "space");
  1304. } else if (is_cmd_prog(t->line, t->line_len)) {
  1305. term_spawn_arg(t, "prog", program_run_embedded_main, "prog");
  1306. } else if (is_cmd_display(t->line, t->line_len)) {
  1307. term_spawn(t, "display", program_display_main);
  1308. } else if (is_cmd_cpuinfo(t->line, t->line_len)) {
  1309. term_spawn(t, "cpuinfo", program_cpuinfo_main);
  1310. } else if (is_cmd_mouseinfo(t->line, t->line_len)) {
  1311. term_spawn(t, "mouseinfo", program_mouseinfo_main);
  1312. } else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
  1313. program_format_main(format_dev);
  1314. } else if (is_cmd_ps(t->line, t->line_len)) {
  1315. process_ps();
  1316. } else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
  1317. if (process_kill(kill_pid) < 0)
  1318. io_putstr("kill: no such process\n");
  1319. else
  1320. io_putstr("killed\n");
  1321. } else if (is_cmd_kexec(t->line, t->line_len)) {
  1322. if (kexec_busy) {
  1323. io_putstr("kexec: already receiving\n");
  1324. } else {
  1325. kexec_busy = 1;
  1326. if (process_create("kexec", program_kexec_main) < 0) {
  1327. kexec_busy = 0;
  1328. io_putstr("error: too many processes\n");
  1329. }
  1330. }
  1331. } else if (is_cmd_dbg(t->line, t->line_len)) {
  1332. program_dbg_main();
  1333. } else if (is_cmd_help(t->line, t->line_len)) {
  1334. help_print();
  1335. } else if (is_cmd_clear(t->line, t->line_len)) {
  1336. io_clear_screen();
  1337. term_print_prompt(t, 1);
  1338. skip_prompt = 1;
  1339. } else {
  1340. char serial_arg[8];
  1341. if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
  1342. if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
  1343. serial_arg[2] == 's' && serial_arg[3] == 't' &&
  1344. serial_arg[4] == '\0') {
  1345. if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
  1346. io_putstr("error: too many processes\n");
  1347. } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
  1348. serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
  1349. serial_arg[4] == '\0') {
  1350. program_serial_recv_main(serial_arg);
  1351. } else {
  1352. io_putstr("serial: usage serial test|recv\n");
  1353. }
  1354. } else {
  1355. unsigned int i = 0;
  1356. while (i < t->line_len && t->line[i] == ' ')
  1357. i++;
  1358. if (i < t->line_len)
  1359. io_putstr("command not found\n");
  1360. }
  1361. }
  1362. t->line_len = 0;
  1363. t->line_pos = 0;
  1364. t->line[0] = '\0';
  1365. if (!skip_prompt && t->fg_pid < 0) {
  1366. term_print_prompt(t, 0);
  1367. }
  1368. } else {
  1369. unsigned char u = (unsigned char)k;
  1370. if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
  1371. unsigned int i;
  1372. t->history_browse = 0;
  1373. for (i = t->line_len; i > t->line_pos; i--)
  1374. t->line[i] = t->line[i - 1];
  1375. t->line[t->line_pos] = (char)k;
  1376. t->line_len++;
  1377. t->line[t->line_len] = '\0';
  1378. t->line_pos++;
  1379. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
  1380. }
  1381. }
  1382. } else {
  1383. process_sleep_ms(10);
  1384. }
  1385. }
  1386. }
  1387. }
  1388. void term_entry(void *arg)
  1389. {
  1390. struct term *t = (struct term *)arg;
  1391. process_set_screen(&t->screen);
  1392. term_run(t);
  1393. }