terminal.c 37 KB

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