terminal.c 39 KB

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