terminal.c 39 KB

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