terminal.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /* Terminal: prompt, datetime (top right), blinking cursor, echo loop, command dispatch.
  2. Commands are spawned as separate processes for preemptive multitasking. */
  3. #include "io.h"
  4. #include "dbg.h"
  5. #include "kdbg.h"
  6. #include "kexec.h"
  7. #include "process.h"
  8. #include "terminal.h"
  9. #include "timer.h"
  10. extern unsigned char _binary_hello_elf_start[];
  11. extern unsigned char _binary_hello_elf_end[];
  12. extern void program_reboot_main(void);
  13. extern void program_poweroff_main(void);
  14. extern void program_ls_main(const char *path);
  15. extern void program_cat_main(const char *filename);
  16. extern void program_run_main(const char *filename);
  17. extern void program_save_main(const char *arg);
  18. extern void program_rm_main(const char *arg);
  19. extern void program_mkdir_main(const char *arg);
  20. extern void program_rmdir_main(const char *arg);
  21. extern void program_clock_main(void);
  22. extern void program_colors_main(void);
  23. extern void program_display_main(void);
  24. extern void program_cpuinfo_main(void);
  25. extern void program_mouseinfo_main(void);
  26. extern void program_serial_loop_main(const char *arg);
  27. extern void program_serial_recv_main(const char *arg);
  28. extern void program_run_serial_main(const char *arg);
  29. extern void program_run_embedded_main(const char *name);
  30. extern void program_lsusb_main(void);
  31. extern void program_storage_main(void);
  32. extern void program_mem_main(void);
  33. extern void program_format_main(const char *arg);
  34. extern void program_lspci_main(const char *filter);
  35. extern void program_peek_main(const char *arg);
  36. extern void program_poke_main(const char *arg);
  37. extern void program_netinfo_main(void);
  38. extern void program_netrecv_main(void);
  39. extern void program_netsend_main(void);
  40. extern void program_netip_main(const char *arg);
  41. extern void program_netping_main(const char *arg);
  42. extern void program_netlog_main(const char *arg);
  43. extern void program_netconsole_main(const char *arg);
  44. extern void program_dhcp_main(void);
  45. extern void program_netregs_main(void);
  46. extern void program_netloop_main(void);
  47. extern void program_netreset_main(void);
  48. extern void program_ping_main(void);
  49. extern void program_curl_main(const char *arg);
  50. extern void program_netdns_main(const char *arg);
  51. extern void program_lookup_main(const char *arg);
  52. /* Print the prompt (with the current directory) and set line_start. */
  53. static void term_print_prompt(struct term *t, int first)
  54. {
  55. if (!first)
  56. io_putchar('\n');
  57. io_putchar('[');
  58. io_putstr(t->cwd);
  59. io_putstr("] > ");
  60. t->line_start = io_get_cursor();
  61. }
  62. static int is_cmd_hello(const char *line, unsigned int line_len)
  63. {
  64. unsigned int i = 0;
  65. while (i < line_len && line[i] == ' ')
  66. i++;
  67. if (i + 5 > line_len)
  68. return 0;
  69. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  70. return 0;
  71. i += 5;
  72. while (i < line_len && line[i] == ' ')
  73. i++;
  74. return (i == line_len);
  75. }
  76. static int is_cmd_reboot(const char *line, unsigned int line_len)
  77. {
  78. unsigned int i = 0;
  79. while (i < line_len && line[i] == ' ')
  80. i++;
  81. if (i + 6 > line_len)
  82. return 0;
  83. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  84. return 0;
  85. i += 6;
  86. while (i < line_len && line[i] == ' ')
  87. i++;
  88. return (i == line_len);
  89. }
  90. static int is_cmd_poweroff(const char *line, unsigned int line_len)
  91. {
  92. unsigned int i = 0;
  93. while (i < line_len && line[i] == ' ')
  94. i++;
  95. if (i + 8 > line_len)
  96. return 0;
  97. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' ||
  98. line[i+4] != 'r' || line[i+5] != 'o' || line[i+6] != 'f' || line[i+7] != 'f')
  99. return 0;
  100. i += 8;
  101. while (i < line_len && line[i] == ' ')
  102. i++;
  103. return (i == line_len);
  104. }
  105. static int is_cmd_ls(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  106. {
  107. unsigned int i = 0, j = 0;
  108. while (i < line_len && line[i] == ' ')
  109. i++;
  110. if (i + 2 > line_len)
  111. return 0;
  112. if (line[i] != 'l' || line[i+1] != 's')
  113. return 0;
  114. i += 2;
  115. if (i < line_len && line[i] != ' ')
  116. return 0; /* "ls" must be a whole word, so reject "lsusb" etc. */
  117. while (i < line_len && line[i] == ' ')
  118. i++;
  119. if (i >= line_len) {
  120. if (buf_size > 0)
  121. path_buf[0] = '\0';
  122. return 1;
  123. }
  124. while (i < line_len && j + 1 < buf_size)
  125. path_buf[j++] = line[i++];
  126. path_buf[j] = '\0';
  127. while (i < line_len && line[i] == ' ')
  128. i++;
  129. return (i == line_len);
  130. }
  131. static int is_cmd_cd(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  132. {
  133. unsigned int i = 0, j = 0;
  134. while (i < line_len && line[i] == ' ')
  135. i++;
  136. if (i + 2 > line_len)
  137. return 0;
  138. if (line[i] != 'c' || line[i+1] != 'd')
  139. return 0;
  140. i += 2;
  141. if (i < line_len && line[i] != ' ')
  142. return 0; /* "cd" must be a whole word */
  143. while (i < line_len && line[i] == ' ')
  144. i++;
  145. if (i >= line_len) {
  146. if (buf_size > 0)
  147. path_buf[0] = '\0';
  148. return 1;
  149. }
  150. while (i < line_len && j + 1 < buf_size)
  151. path_buf[j++] = line[i++];
  152. path_buf[j] = '\0';
  153. while (i < line_len && line[i] == ' ')
  154. i++;
  155. return (i == line_len);
  156. }
  157. static int is_cmd_clock(const char *line, unsigned int line_len)
  158. {
  159. unsigned int i = 0;
  160. while (i < line_len && line[i] == ' ')
  161. i++;
  162. if (i + 5 > line_len)
  163. return 0;
  164. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  165. line[i+3] != 'c' || line[i+4] != 'k')
  166. return 0;
  167. i += 5;
  168. while (i < line_len && line[i] == ' ')
  169. i++;
  170. return (i == line_len);
  171. }
  172. static int is_cmd_colors(const char *line, unsigned int line_len)
  173. {
  174. unsigned int i = 0;
  175. while (i < line_len && line[i] == ' ')
  176. i++;
  177. if (i + 6 > line_len)
  178. return 0;
  179. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  180. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  181. return 0;
  182. i += 6;
  183. while (i < line_len && line[i] == ' ')
  184. i++;
  185. return (i == line_len);
  186. }
  187. static int is_cmd_display(const char *line, unsigned int line_len)
  188. {
  189. unsigned int i = 0;
  190. while (i < line_len && line[i] == ' ')
  191. i++;
  192. if (i + 7 > line_len)
  193. return 0;
  194. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  195. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  196. return 0;
  197. i += 7;
  198. while (i < line_len && line[i] == ' ')
  199. i++;
  200. return (i == line_len);
  201. }
  202. static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
  203. {
  204. unsigned int i = 0;
  205. while (i < line_len && line[i] == ' ')
  206. i++;
  207. if (i + 7 > line_len)
  208. return 0;
  209. if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
  210. line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
  211. return 0;
  212. i += 7;
  213. while (i < line_len && line[i] == ' ')
  214. i++;
  215. return (i == line_len);
  216. }
  217. static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
  218. {
  219. unsigned int i = 0;
  220. while (i < line_len && line[i] == ' ')
  221. i++;
  222. if (i + 9 > line_len)
  223. return 0;
  224. if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
  225. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
  226. line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
  227. return 0;
  228. i += 9;
  229. while (i < line_len && line[i] == ' ')
  230. i++;
  231. return (i == line_len);
  232. }
  233. static int is_cmd_lsusb(const char *line, unsigned int line_len)
  234. {
  235. unsigned int i = 0;
  236. while (i < line_len && line[i] == ' ')
  237. i++;
  238. if (i + 5 > line_len)
  239. return 0;
  240. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'u' ||
  241. line[i+3] != 's' || line[i+4] != 'b')
  242. return 0;
  243. i += 5;
  244. while (i < line_len && line[i] == ' ')
  245. i++;
  246. return (i == line_len);
  247. }
  248. static int is_cmd_lspci(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  249. {
  250. unsigned int i = 0, j = 0;
  251. while (i < line_len && line[i] == ' ')
  252. i++;
  253. if (i + 5 > line_len)
  254. return 0;
  255. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'p' ||
  256. line[i+3] != 'c' || line[i+4] != 'i')
  257. return 0;
  258. i += 5;
  259. if (i < line_len && line[i] != ' ')
  260. return 0; /* "lspci" must be a whole word */
  261. while (i < line_len && line[i] == ' ')
  262. i++;
  263. if (i >= line_len) {
  264. if (buf_size > 0)
  265. arg_buf[0] = '\0';
  266. return 1;
  267. }
  268. while (i < line_len && j + 1 < buf_size)
  269. arg_buf[j++] = line[i++];
  270. arg_buf[j] = '\0';
  271. while (i < line_len && line[i] == ' ')
  272. i++;
  273. return (i == line_len);
  274. }
  275. static int is_cmd_peek(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  276. {
  277. unsigned int i = 0, j = 0;
  278. while (i < line_len && line[i] == ' ')
  279. i++;
  280. if (i + 4 > line_len)
  281. return 0;
  282. if (line[i] != 'p' || line[i+1] != 'e' || line[i+2] != 'e' ||
  283. line[i+3] != 'k')
  284. return 0;
  285. i += 4;
  286. if (i < line_len && line[i] != ' ')
  287. return 0; /* "peek" must be a whole word */
  288. while (i < line_len && line[i] == ' ')
  289. i++;
  290. if (i >= line_len) {
  291. if (buf_size > 0)
  292. arg_buf[0] = '\0';
  293. return 1;
  294. }
  295. while (i < line_len && j + 1 < buf_size)
  296. arg_buf[j++] = line[i++];
  297. arg_buf[j] = '\0';
  298. while (i < line_len && line[i] == ' ')
  299. i++;
  300. return (i == line_len);
  301. }
  302. /* poke <addr> <val>: write one dword to memory/MMIO. */
  303. static int is_cmd_poke(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  304. {
  305. unsigned int i = 0, j = 0;
  306. while (i < line_len && line[i] == ' ')
  307. i++;
  308. if (i + 4 > line_len)
  309. return 0;
  310. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'k' ||
  311. line[i+3] != 'e')
  312. return 0;
  313. i += 4;
  314. if (i < line_len && line[i] != ' ')
  315. return 0; /* "poke" must be a whole word */
  316. while (i < line_len && line[i] == ' ')
  317. i++;
  318. if (i >= line_len)
  319. return 0; /* address and value required */
  320. while (i < line_len && j + 1 < buf_size)
  321. arg_buf[j++] = line[i++];
  322. arg_buf[j] = '\0';
  323. while (i < line_len && line[i] == ' ')
  324. i++;
  325. return (i == line_len);
  326. }
  327. static int is_cmd_prog(const char *line, unsigned int line_len){
  328. unsigned int i = 0;
  329. while (i < line_len && line[i] == ' ')
  330. i++;
  331. if (i + 4 > line_len)
  332. return 0;
  333. if (line[i] != 'p' || line[i+1] != 'r' || line[i+2] != 'o' ||
  334. line[i+3] != 'g')
  335. return 0;
  336. i += 4;
  337. while (i < line_len && line[i] == ' ')
  338. i++;
  339. return (i == line_len);
  340. }
  341. static int is_cmd_netinfo(const char *line, unsigned int line_len)
  342. {
  343. unsigned int i = 0;
  344. while (i < line_len && line[i] == ' ')
  345. i++;
  346. if (i + 7 > line_len)
  347. return 0;
  348. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  349. line[i+3] != 'i' || line[i+4] != 'n' || line[i+5] != 'f' ||
  350. line[i+6] != 'o')
  351. return 0;
  352. i += 7;
  353. while (i < line_len && line[i] == ' ')
  354. i++;
  355. return (i == line_len);
  356. }
  357. static int is_cmd_netlog(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  358. {
  359. unsigned int i = 0, j = 0;
  360. while (i < line_len && line[i] == ' ')
  361. i++;
  362. if (i + 6 > line_len)
  363. return 0;
  364. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  365. line[i+3] != 'l' || line[i+4] != 'o' || line[i+5] != 'g')
  366. return 0;
  367. i += 6;
  368. if (i < line_len && line[i] != ' ')
  369. return 0; /* "netlog" must be a whole word */
  370. while (i < line_len && line[i] == ' ')
  371. i++;
  372. if (i >= line_len) {
  373. if (buf_size > 0)
  374. arg_buf[0] = '\0';
  375. return 1;
  376. }
  377. while (i < line_len && j + 1 < buf_size)
  378. arg_buf[j++] = line[i++];
  379. arg_buf[j] = '\0';
  380. while (i < line_len && line[i] == ' ')
  381. i++;
  382. return (i == line_len);
  383. }
  384. static int is_cmd_netconsole(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  385. {
  386. unsigned int i = 0, j = 0;
  387. while (i < line_len && line[i] == ' ')
  388. i++;
  389. if (i + 10 > line_len)
  390. return 0;
  391. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  392. line[i+3] != 'c' || line[i+4] != 'o' || line[i+5] != 'n' ||
  393. line[i+6] != 's' || line[i+7] != 'o' || line[i+8] != 'l' ||
  394. line[i+9] != 'e')
  395. return 0;
  396. i += 10;
  397. if (i < line_len && line[i] != ' ')
  398. return 0; /* "netconsole" must be a whole word */
  399. while (i < line_len && line[i] == ' ')
  400. i++;
  401. if (i >= line_len) {
  402. if (buf_size > 0)
  403. arg_buf[0] = '\0';
  404. return 1;
  405. }
  406. while (i < line_len && j + 1 < buf_size)
  407. arg_buf[j++] = line[i++];
  408. arg_buf[j] = '\0';
  409. while (i < line_len && line[i] == ' ')
  410. i++;
  411. return (i == line_len);
  412. }
  413. static int is_cmd_dhcp(const char *line, unsigned int line_len)
  414. {
  415. unsigned int i = 0;
  416. while (i < line_len && line[i] == ' ')
  417. i++;
  418. if (i + 4 > line_len)
  419. return 0;
  420. if (line[i] != 'd' || line[i+1] != 'h' || line[i+2] != 'c' ||
  421. line[i+3] != 'p')
  422. return 0;
  423. i += 4;
  424. while (i < line_len && line[i] == ' ')
  425. i++;
  426. return (i == line_len);
  427. }
  428. static int is_cmd_netregs(const char *line, unsigned int line_len)
  429. {
  430. unsigned int i = 0;
  431. while (i < line_len && line[i] == ' ')
  432. i++;
  433. if (i + 7 > line_len)
  434. return 0;
  435. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  436. line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 'g' ||
  437. line[i+6] != 's')
  438. return 0;
  439. i += 7;
  440. while (i < line_len && line[i] == ' ')
  441. i++;
  442. return (i == line_len);
  443. }
  444. static int is_cmd_netloop(const char *line, unsigned int line_len)
  445. {
  446. unsigned int i = 0;
  447. while (i < line_len && line[i] == ' ')
  448. i++;
  449. if (i + 7 > line_len)
  450. return 0;
  451. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  452. line[i+3] != 'l' || line[i+4] != 'o' || line[i+5] != 'o' ||
  453. line[i+6] != 'p')
  454. return 0;
  455. i += 7;
  456. while (i < line_len && line[i] == ' ')
  457. i++;
  458. return (i == line_len);
  459. }
  460. static int is_cmd_netreset(const char *line, unsigned int line_len)
  461. {
  462. unsigned int i = 0;
  463. while (i < line_len && line[i] == ' ')
  464. i++;
  465. if (i + 8 > line_len)
  466. return 0;
  467. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  468. line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 's' ||
  469. line[i+6] != 'e' || line[i+7] != 't')
  470. return 0;
  471. i += 8;
  472. while (i < line_len && line[i] == ' ')
  473. i++;
  474. return (i == line_len);
  475. }
  476. static int is_cmd_netrecv(const char *line, unsigned int line_len)
  477. {
  478. unsigned int i = 0;
  479. while (i < line_len && line[i] == ' ')
  480. i++;
  481. if (i + 7 > line_len)
  482. return 0;
  483. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  484. line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 'c' ||
  485. line[i+6] != 'v')
  486. return 0;
  487. i += 7;
  488. while (i < line_len && line[i] == ' ')
  489. i++;
  490. return (i == line_len);
  491. }
  492. static int is_cmd_netsend(const char *line, unsigned int line_len)
  493. {
  494. unsigned int i = 0;
  495. while (i < line_len && line[i] == ' ')
  496. i++;
  497. if (i + 7 > line_len)
  498. return 0;
  499. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  500. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'n' ||
  501. line[i+6] != 'd')
  502. return 0;
  503. i += 7;
  504. while (i < line_len && line[i] == ' ')
  505. i++;
  506. return (i == line_len);
  507. }
  508. static int is_cmd_netip(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  509. {
  510. unsigned int i = 0, j = 0;
  511. while (i < line_len && line[i] == ' ')
  512. i++;
  513. if (i + 5 > line_len)
  514. return 0;
  515. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  516. line[i+3] != 'i' || line[i+4] != 'p')
  517. return 0;
  518. i += 5;
  519. if (i < line_len && line[i] != ' ')
  520. return 0; /* "netip" must be a whole word */
  521. while (i < line_len && line[i] == ' ')
  522. i++;
  523. if (i >= line_len) {
  524. if (buf_size > 0)
  525. arg_buf[0] = '\0';
  526. return 1;
  527. }
  528. while (i < line_len && j + 1 < buf_size)
  529. arg_buf[j++] = line[i++];
  530. arg_buf[j] = '\0';
  531. while (i < line_len && line[i] == ' ')
  532. i++;
  533. return (i == line_len);
  534. }
  535. static int is_cmd_netping(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  536. {
  537. unsigned int i = 0, j = 0;
  538. while (i < line_len && line[i] == ' ')
  539. i++;
  540. if (i + 7 > line_len)
  541. return 0;
  542. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  543. line[i+3] != 'p' || line[i+4] != 'i' || line[i+5] != 'n' ||
  544. line[i+6] != 'g')
  545. return 0;
  546. i += 7;
  547. if (i < line_len && line[i] != ' ')
  548. return 0; /* "netping" must be a whole word */
  549. while (i < line_len && line[i] == ' ')
  550. i++;
  551. if (i >= line_len) {
  552. if (buf_size > 0)
  553. arg_buf[0] = '\0';
  554. return 1;
  555. }
  556. while (i < line_len && j + 1 < buf_size)
  557. arg_buf[j++] = line[i++];
  558. arg_buf[j] = '\0';
  559. while (i < line_len && line[i] == ' ')
  560. i++;
  561. return (i == line_len);
  562. }
  563. /* One optional single-token argument (URL, name or dotted IP). */
  564. static int is_cmd_curl(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  565. {
  566. unsigned int i = 0, j = 0;
  567. while (i < line_len && line[i] == ' ')
  568. i++;
  569. if (i + 4 > line_len)
  570. return 0;
  571. if (line[i] != 'c' || line[i+1] != 'u' || line[i+2] != 'r' ||
  572. line[i+3] != 'l')
  573. return 0;
  574. i += 4;
  575. if (i < line_len && line[i] != ' ')
  576. return 0;
  577. while (i < line_len && line[i] == ' ')
  578. i++;
  579. if (i >= line_len) {
  580. if (buf_size > 0)
  581. arg_buf[0] = '\0';
  582. return 1;
  583. }
  584. while (i < line_len && j + 1 < buf_size)
  585. arg_buf[j++] = line[i++];
  586. arg_buf[j] = '\0';
  587. while (i < line_len && line[i] == ' ')
  588. i++;
  589. return (i == line_len);
  590. }
  591. static int is_cmd_netdns(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  592. {
  593. unsigned int i = 0, j = 0;
  594. while (i < line_len && line[i] == ' ')
  595. i++;
  596. if (i + 6 > line_len)
  597. return 0;
  598. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  599. line[i+3] != 'd' || line[i+4] != 'n' || line[i+5] != 's')
  600. return 0;
  601. i += 6;
  602. if (i < line_len && line[i] != ' ')
  603. return 0;
  604. while (i < line_len && line[i] == ' ')
  605. i++;
  606. if (i >= line_len) {
  607. if (buf_size > 0)
  608. arg_buf[0] = '\0';
  609. return 1;
  610. }
  611. while (i < line_len && j + 1 < buf_size)
  612. arg_buf[j++] = line[i++];
  613. arg_buf[j] = '\0';
  614. while (i < line_len && line[i] == ' ')
  615. i++;
  616. return (i == line_len);
  617. }
  618. static int is_cmd_lookup(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  619. {
  620. unsigned int i = 0, j = 0;
  621. while (i < line_len && line[i] == ' ')
  622. i++;
  623. if (i + 6 > line_len)
  624. return 0;
  625. if (line[i] != 'l' || line[i+1] != 'o' || line[i+2] != 'o' ||
  626. line[i+3] != 'k' || line[i+4] != 'u' || line[i+5] != 'p')
  627. return 0;
  628. i += 6;
  629. if (i < line_len && line[i] != ' ')
  630. return 0;
  631. while (i < line_len && line[i] == ' ')
  632. i++;
  633. if (i >= line_len) {
  634. if (buf_size > 0)
  635. arg_buf[0] = '\0';
  636. return 1;
  637. }
  638. while (i < line_len && j + 1 < buf_size)
  639. arg_buf[j++] = line[i++];
  640. arg_buf[j] = '\0';
  641. while (i < line_len && line[i] == ' ')
  642. i++;
  643. return (i == line_len);
  644. }
  645. static int is_cmd_ping(const char *line, unsigned int line_len)
  646. {
  647. unsigned int i = 0;
  648. while (i < line_len && line[i] == ' ')
  649. i++;
  650. if (i + 4 > line_len)
  651. return 0;
  652. if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' || line[i+3] != 'g')
  653. return 0;
  654. i += 4;
  655. while (i < line_len && line[i] == ' ')
  656. i++;
  657. return (i == line_len);
  658. }
  659. static int is_cmd_space(const char *line, unsigned int line_len)
  660. {
  661. unsigned int i = 0;
  662. while (i < line_len && line[i] == ' ')
  663. i++;
  664. if (i + 5 > line_len)
  665. return 0;
  666. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  667. line[i+3] != 'c' || line[i+4] != 'e')
  668. return 0;
  669. i += 5;
  670. while (i < line_len && line[i] == ' ')
  671. i++;
  672. return (i == line_len);
  673. }
  674. #define NET_UDP_ARG_MAX 128
  675. #define CAT_FNAME_MAX 64
  676. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  677. {
  678. unsigned int i = 0, j;
  679. while (i < line_len && line[i] == ' ')
  680. i++;
  681. if (i + 3 > line_len)
  682. return 0;
  683. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  684. return 0;
  685. i += 3;
  686. while (i < line_len && line[i] == ' ')
  687. i++;
  688. if (i >= line_len)
  689. return 0;
  690. j = 0;
  691. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  692. filename_buf[j++] = line[i++];
  693. filename_buf[j] = '\0';
  694. return (j > 0);
  695. }
  696. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  697. {
  698. unsigned int i = 0, j;
  699. while (i < line_len && line[i] == ' ')
  700. i++;
  701. if (i + 3 > line_len)
  702. return 0;
  703. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  704. return 0;
  705. i += 3;
  706. while (i < line_len && line[i] == ' ')
  707. i++;
  708. if (i >= line_len)
  709. return 0;
  710. j = 0;
  711. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  712. filename_buf[j++] = line[i++];
  713. filename_buf[j] = '\0';
  714. return (j > 0);
  715. }
  716. #define SAVE_ARGS_MAX 128
  717. #define RM_FNAME_MAX 64
  718. #define LS_PATH_MAX 64
  719. #define FORMAT_DEV_MAX 16
  720. static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  721. {
  722. unsigned int i = 0, j = 0;
  723. while (i < line_len && line[i] == ' ')
  724. i++;
  725. if (i + 4 > line_len)
  726. return 0;
  727. if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
  728. return 0;
  729. i += 4;
  730. while (i < line_len && line[i] == ' ')
  731. i++;
  732. if (i >= line_len)
  733. return 0;
  734. while (i < line_len && j + 1 < buf_size)
  735. arg_buf[j++] = line[i++];
  736. while (j > 0 && arg_buf[j - 1] == ' ')
  737. j--;
  738. arg_buf[j] = '\0';
  739. while (i < line_len && line[i] == ' ')
  740. i++;
  741. return (i == line_len && j > 0);
  742. }
  743. static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  744. {
  745. unsigned int i = 0, j = 0;
  746. while (i < line_len && line[i] == ' ')
  747. i++;
  748. if (i + 2 > line_len)
  749. return 0;
  750. if (line[i] != 'r' || line[i+1] != 'm')
  751. return 0;
  752. i += 2;
  753. while (i < line_len && line[i] == ' ')
  754. i++;
  755. if (i >= line_len)
  756. return 0;
  757. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  758. filename_buf[j++] = line[i++];
  759. filename_buf[j] = '\0';
  760. return (j > 0);
  761. }
  762. static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  763. {
  764. unsigned int i = 0, j = 0;
  765. while (i < line_len && line[i] == ' ')
  766. i++;
  767. if (i + 5 > line_len)
  768. return 0;
  769. if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  770. return 0;
  771. i += 5;
  772. while (i < line_len && line[i] == ' ')
  773. i++;
  774. if (i >= line_len)
  775. return 0;
  776. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  777. path_buf[j++] = line[i++];
  778. path_buf[j] = '\0';
  779. return (j > 0);
  780. }
  781. static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  782. {
  783. unsigned int i = 0, j = 0;
  784. while (i < line_len && line[i] == ' ')
  785. i++;
  786. if (i + 5 > line_len)
  787. return 0;
  788. if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  789. return 0;
  790. i += 5;
  791. while (i < line_len && line[i] == ' ')
  792. i++;
  793. if (i >= line_len)
  794. return 0;
  795. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  796. path_buf[j++] = line[i++];
  797. path_buf[j] = '\0';
  798. return (j > 0);
  799. }
  800. static int is_cmd_storage(const char *line, unsigned int line_len)
  801. {
  802. unsigned int i = 0;
  803. while (i < line_len && line[i] == ' ')
  804. i++;
  805. if (i + 7 > line_len)
  806. return 0;
  807. if (line[i] != 's' || line[i+1] != 't' || line[i+2] != 'o' ||
  808. line[i+3] != 'r' || line[i+4] != 'a' || line[i+5] != 'g' || line[i+6] != 'e')
  809. return 0;
  810. i += 7;
  811. while (i < line_len && line[i] == ' ')
  812. i++;
  813. return (i == line_len);
  814. }
  815. static int is_cmd_meminfo(const char *line, unsigned int line_len)
  816. {
  817. unsigned int i = 0;
  818. while (i < line_len && line[i] == ' ')
  819. i++;
  820. if (i + 7 > line_len)
  821. return 0;
  822. if (line[i] != 'm' || line[i+1] != 'e' || line[i+2] != 'm' ||
  823. line[i+3] != 'i' || line[i+4] != 'n' || line[i+5] != 'f' ||
  824. line[i+6] != 'o')
  825. return 0;
  826. i += 7;
  827. if (i < line_len && line[i] != ' ')
  828. return 0; /* "meminfo" must be a whole word */
  829. while (i < line_len && line[i] == ' ')
  830. i++;
  831. return (i == line_len);
  832. }
  833. static int is_cmd_format(const char *line, unsigned int line_len, char *dev_buf, unsigned int buf_size)
  834. {
  835. unsigned int i = 0, j;
  836. while (i < line_len && line[i] == ' ')
  837. i++;
  838. if (i + 6 > line_len)
  839. return 0;
  840. if (line[i] != 'f' || line[i+1] != 'o' || line[i+2] != 'r' ||
  841. line[i+3] != 'm' || line[i+4] != 'a' || line[i+5] != 't')
  842. return 0;
  843. i += 6;
  844. if (i < line_len && line[i] != ' ')
  845. return 0; /* "format" must be a whole word */
  846. while (i < line_len && line[i] == ' ')
  847. i++;
  848. if (i >= line_len)
  849. return 0;
  850. j = 0;
  851. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  852. dev_buf[j++] = line[i++];
  853. dev_buf[j] = '\0';
  854. while (i < line_len && line[i] == ' ')
  855. i++;
  856. return (i == line_len && j > 0);
  857. }
  858. /* Help listing: printed as two 32-column columns so it fits the 64-column
  859. terminal without scrolling. */
  860. static const char *const help_items[] = {
  861. "cd [path]", "ls [path]", "cat <path>", "save <path> <text>",
  862. "rm <path>", "mkdir <path>", "rmdir <path>", "hello", "reboot",
  863. "poweroff", "run <name>|<file>", "space", "prog", "clock", "colors",
  864. "display", "cpuinfo", "lsusb", "mouseinfo", "storage", "meminfo",
  865. "format <device>", "lspci [id]", "peek <addr> [len]", "poke <addr> <val>",
  866. "netinfo",
  867. "netrecv", "netsend", "netip [a.b.c.d]", "netping <a.b.c.d>",
  868. "netlog <text>", "netconsole [on|off|host <ip>]", "dhcp", "netregs",
  869. "netloop", "netreset", "ping", "ps", "kill <pid>", "kexec", "dbg",
  870. "serial test|recv", "clear", "curl <url>", "netdns [a.b.c.d]",
  871. "lookup <name>",
  872. };
  873. static void help_puts(const char *s)
  874. {
  875. while (*s)
  876. io_putchar(*s++);
  877. }
  878. static void help_item_pad(const char *s)
  879. {
  880. unsigned int n = 0;
  881. while (s[n]) {
  882. io_putchar(s[n]);
  883. n++;
  884. }
  885. while (n < 32) {
  886. io_putchar(' ');
  887. n++;
  888. }
  889. }
  890. static void help_print(void)
  891. {
  892. unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
  893. unsigned int half = (nitems + 1) / 2;
  894. unsigned int k;
  895. for (k = 0; k < half; k++) {
  896. if (k + half < nitems) {
  897. help_item_pad(help_items[k]);
  898. help_puts(help_items[k + half]);
  899. } else {
  900. help_puts(help_items[k]);
  901. }
  902. io_putchar('\n');
  903. }
  904. }
  905. static int is_cmd_help(const char *line, unsigned int line_len)
  906. {
  907. unsigned int i = 0;
  908. while (i < line_len && line[i] == ' ')
  909. i++;
  910. if (i + 4 > line_len)
  911. return 0;
  912. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  913. return 0;
  914. i += 4;
  915. while (i < line_len && line[i] == ' ')
  916. i++;
  917. return (i == line_len);
  918. }
  919. static int is_cmd_clear(const char *line, unsigned int line_len)
  920. {
  921. unsigned int i = 0;
  922. while (i < line_len && line[i] == ' ')
  923. i++;
  924. if (i + 5 > line_len)
  925. return 0;
  926. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  927. line[i+3] != 'a' || line[i+4] != 'r')
  928. return 0;
  929. i += 5;
  930. while (i < line_len && line[i] == ' ')
  931. i++;
  932. return (i == line_len);
  933. }
  934. static int is_cmd_ps(const char *line, unsigned int line_len)
  935. {
  936. unsigned int i = 0;
  937. while (i < line_len && line[i] == ' ')
  938. i++;
  939. if (i + 2 > line_len)
  940. return 0;
  941. if (line[i] != 'p' || line[i+1] != 's')
  942. return 0;
  943. i += 2;
  944. while (i < line_len && line[i] == ' ')
  945. i++;
  946. return (i == line_len);
  947. }
  948. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  949. {
  950. unsigned int i = 0, num = 0;
  951. int has_digit = 0;
  952. while (i < line_len && line[i] == ' ')
  953. i++;
  954. if (i + 4 > line_len)
  955. return 0;
  956. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  957. return 0;
  958. i += 4;
  959. while (i < line_len && line[i] == ' ')
  960. i++;
  961. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  962. num = num * 10 + (unsigned int)(line[i] - '0');
  963. has_digit = 1;
  964. i++;
  965. }
  966. if (!has_digit)
  967. return 0;
  968. while (i < line_len && line[i] == ' ')
  969. i++;
  970. if (i != line_len)
  971. return 0;
  972. *pid_out = num;
  973. return 1;
  974. }
  975. static int is_cmd_kexec(const char *line, unsigned int line_len)
  976. {
  977. unsigned int i = 0;
  978. while (i < line_len && line[i] == ' ')
  979. i++;
  980. if (i + 5 > line_len)
  981. return 0;
  982. if (line[i] != 'k' || line[i+1] != 'e' || line[i+2] != 'x' || line[i+3] != 'e' || line[i+4] != 'c')
  983. return 0;
  984. i += 5;
  985. while (i < line_len && line[i] == ' ')
  986. i++;
  987. return (i == line_len);
  988. }
  989. static int is_cmd_dbg(const char *line, unsigned int line_len)
  990. {
  991. unsigned int i = 0;
  992. while (i < line_len && line[i] == ' ')
  993. i++;
  994. if (i + 3 > line_len)
  995. return 0;
  996. if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
  997. return 0;
  998. i += 3;
  999. while (i < line_len && line[i] == ' ')
  1000. i++;
  1001. return (i == line_len);
  1002. }
  1003. static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  1004. {
  1005. unsigned int i = 0, j = 0;
  1006. while (i < line_len && line[i] == ' ')
  1007. i++;
  1008. if (i + 6 > line_len)
  1009. return 0;
  1010. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
  1011. line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
  1012. return 0;
  1013. i += 6;
  1014. while (i < line_len && line[i] == ' ')
  1015. i++;
  1016. if (i >= line_len) {
  1017. if (buf_size > 0)
  1018. arg_buf[0] = '\0';
  1019. return 1;
  1020. }
  1021. while (i < line_len && j + 1 < buf_size)
  1022. arg_buf[j++] = line[i++];
  1023. while (j > 0 && arg_buf[j - 1] == ' ')
  1024. j--;
  1025. arg_buf[j] = '\0';
  1026. while (i < line_len && line[i] == ' ')
  1027. i++;
  1028. return (i == line_len);
  1029. }
  1030. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  1031. static void redraw_line_from(const char *line, unsigned int line_len,
  1032. unsigned int line_pos, unsigned int line_start, unsigned int from)
  1033. {
  1034. unsigned int i;
  1035. io_set_cursor(line_start + from);
  1036. for (i = from; i < line_len; i++)
  1037. io_putchar(line[i]);
  1038. io_putchar(' ');
  1039. io_set_cursor(line_start + line_pos);
  1040. }
  1041. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  1042. static void redraw_line_clear(const char *line, unsigned int line_len,
  1043. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  1044. {
  1045. unsigned int i;
  1046. io_set_cursor(line_start);
  1047. for (i = 0; i < line_len; i++)
  1048. io_putchar(line[i]);
  1049. for (i = line_len; i <= clear_through; i++)
  1050. io_putchar(' ');
  1051. io_set_cursor(line_start + line_pos);
  1052. }
  1053. /* Copy history entry (1 = newest) into t->line, redraw from start. */
  1054. static void history_load_into_line(struct term *t, unsigned int browse)
  1055. {
  1056. unsigned int slot, len, old_len;
  1057. const char *src;
  1058. if (browse < 1 || browse > t->history_count)
  1059. return;
  1060. old_len = t->line_len;
  1061. slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  1062. src = t->history[slot];
  1063. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  1064. t->line[len] = src[len];
  1065. t->line[len] = '\0';
  1066. t->line_len = len;
  1067. t->line_pos = len;
  1068. redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
  1069. }
  1070. /* ---- Tab completion ---- */
  1071. /* Command names come from the help table ("ls [path]" -> "ls"). */
  1072. static unsigned int help_name_len(const char *s)
  1073. {
  1074. unsigned int n = 0;
  1075. while (s[n] && s[n] != ' ' && s[n] != '[' && s[n] != '<' && s[n] != '|')
  1076. n++;
  1077. return n;
  1078. }
  1079. static char lower_ch(char c)
  1080. {
  1081. return (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
  1082. }
  1083. static int prefix_ci(const char *s, const char *prefix, unsigned int plen)
  1084. {
  1085. unsigned int i;
  1086. for (i = 0; i < plen; i++)
  1087. if (lower_ch(s[i]) != lower_ch(prefix[i]))
  1088. return 0;
  1089. return 1;
  1090. }
  1091. /* "HELLO TXT" -> "hello.txt" (a completion-ready 8.3 display name). */
  1092. static void tab_display_name(const char *n, char *out)
  1093. {
  1094. unsigned int bl = 0, el = 0, i, j = 0;
  1095. for (i = 0; i < 8 && n[i] && n[i] != ' '; i++)
  1096. bl = i + 1;
  1097. for (i = 8; i < 11 && n[i] && n[i] != ' '; i++)
  1098. el = i - 8 + 1;
  1099. for (i = 0; i < bl; i++)
  1100. out[j++] = lower_ch(n[i]);
  1101. if (el) {
  1102. out[j++] = '.';
  1103. for (i = 0; i < el; i++)
  1104. out[j++] = lower_ch(n[8 + i]);
  1105. }
  1106. out[j] = '\0';
  1107. }
  1108. /* Shared completion state (fat_list_dir takes no context pointer). */
  1109. static char tab_prefix[16];
  1110. static unsigned int tab_prefix_len;
  1111. static unsigned int tab_count;
  1112. static unsigned int tab_lcp_len;
  1113. static char tab_lcp[16];
  1114. static int tab_single_dir;
  1115. static fat_fs_t *tab_fs;
  1116. static unsigned int tab_dir_cluster;
  1117. static unsigned int tab_col;
  1118. static unsigned int tab_width;
  1119. static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
  1120. {
  1121. char disp[16];
  1122. unsigned int i, n;
  1123. (void)size;
  1124. if (name[0] == '.') /* "." / ".." */
  1125. return;
  1126. tab_display_name(name, disp);
  1127. if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
  1128. return;
  1129. n = 0;
  1130. while (disp[n])
  1131. n++;
  1132. if (tab_count == 0) {
  1133. for (i = 0; i < n; i++)
  1134. tab_lcp[i] = disp[i];
  1135. tab_lcp_len = n;
  1136. tab_single_dir = is_dir;
  1137. } else {
  1138. i = 0;
  1139. while (i < tab_lcp_len && i < n && tab_lcp[i] == disp[i])
  1140. i++;
  1141. tab_lcp_len = i;
  1142. }
  1143. tab_count++;
  1144. }
  1145. static void tab_print_one(const char *name, int is_dir)
  1146. {
  1147. unsigned int n = 0, w;
  1148. while (name[n])
  1149. n++;
  1150. w = n + (is_dir ? 1 : 0);
  1151. if (tab_col > 0 && tab_col + 2 + w > tab_width) {
  1152. io_putchar('\n');
  1153. tab_col = 0;
  1154. }
  1155. if (tab_col > 0) {
  1156. io_putstr(" ");
  1157. tab_col += 2;
  1158. }
  1159. io_putstr(name);
  1160. if (is_dir)
  1161. io_putchar('/');
  1162. tab_col += w;
  1163. }
  1164. static void tab_print_cb(const char *name, int is_dir, unsigned int size)
  1165. {
  1166. char disp[16];
  1167. (void)size;
  1168. if (name[0] == '.')
  1169. return;
  1170. tab_display_name(name, disp);
  1171. if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
  1172. return;
  1173. tab_print_one(disp, is_dir);
  1174. }
  1175. static int tab_scan_dir(const char *abs)
  1176. {
  1177. fat_fs_t *fs;
  1178. const char *fs_path;
  1179. if (vfs_resolve(abs, &fs, &fs_path) != 0)
  1180. return 0;
  1181. if (fat_resolve_dir(fs, fs_path, &tab_dir_cluster) != 0)
  1182. return 0;
  1183. tab_fs = fs;
  1184. tab_count = 0;
  1185. tab_lcp_len = 0;
  1186. fat_list_dir(fs, tab_dir_cluster, tab_collect_cb);
  1187. return (int)tab_count;
  1188. }
  1189. static int tab_scan_mounts(void)
  1190. {
  1191. unsigned int i;
  1192. tab_count = 0;
  1193. tab_lcp_len = 0;
  1194. for (i = 0; i < vfs_mount_count; i++) {
  1195. const char *name = vfs_mounts[i].name;
  1196. unsigned int n = 0, j;
  1197. while (name[n])
  1198. n++;
  1199. if (!prefix_ci(name, tab_prefix, tab_prefix_len))
  1200. continue;
  1201. if (tab_count == 0) {
  1202. for (j = 0; j < n; j++)
  1203. tab_lcp[j] = name[j];
  1204. tab_lcp_len = n;
  1205. } else {
  1206. j = 0;
  1207. while (j < tab_lcp_len && j < n && tab_lcp[j] == name[j])
  1208. j++;
  1209. tab_lcp_len = j;
  1210. }
  1211. tab_count++;
  1212. }
  1213. return (int)tab_count;
  1214. }
  1215. /* Replace line[start,end) with ins, keep the tail and redraw. */
  1216. static void term_replace(struct term *t, unsigned int start, unsigned int end,
  1217. const char *ins)
  1218. {
  1219. unsigned int inslen = 0, tail, i, old_len = t->line_len;
  1220. while (ins[inslen])
  1221. inslen++;
  1222. tail = old_len - end;
  1223. if (start + inslen + tail >= LINE_MAX)
  1224. return;
  1225. for (i = 0; i < tail; i++)
  1226. t->line[start + inslen + i] = t->line[end + i];
  1227. for (i = 0; i < inslen; i++)
  1228. t->line[start + i] = ins[i];
  1229. t->line_len = start + inslen + tail;
  1230. t->line[t->line_len] = '\0';
  1231. t->line_pos = start + inslen;
  1232. redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start,
  1233. old_len > t->line_len ? old_len : t->line_len);
  1234. }
  1235. /* List the ambiguous matches below the line, then redraw prompt + line. */
  1236. static void term_tab_ambiguous(struct term *t, int mounts)
  1237. {
  1238. unsigned int i;
  1239. io_putchar('\n');
  1240. tab_col = 0;
  1241. tab_width = t->screen.cols;
  1242. if (mounts) {
  1243. for (i = 0; i < vfs_mount_count; i++) {
  1244. const char *name = vfs_mounts[i].name;
  1245. if (prefix_ci(name, tab_prefix, tab_prefix_len))
  1246. tab_print_one(name, 1);
  1247. }
  1248. } else {
  1249. fat_list_dir(tab_fs, tab_dir_cluster, tab_print_cb);
  1250. }
  1251. io_putchar('\n');
  1252. term_print_prompt(t, 1);
  1253. redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start, t->line_len);
  1254. }
  1255. /* Complete the token at the cursor: command name in the first token, a VFS
  1256. * path afterwards. One match completes, several extend to the common
  1257. * prefix or are listed below the line. */
  1258. static void term_complete(struct term *t)
  1259. {
  1260. char token[LINE_MAX + 1];
  1261. char ins[LINE_MAX + 1];
  1262. char dir[VFS_PATH_MAX];
  1263. unsigned int start = t->line_pos, end, tl, i, j;
  1264. while (start > 0 && t->line[start - 1] != ' ')
  1265. start--;
  1266. end = t->line_pos;
  1267. while (end < t->line_len && t->line[end] != ' ')
  1268. end++;
  1269. tl = end - start;
  1270. for (i = 0; i < tl; i++)
  1271. token[i] = t->line[start + i];
  1272. token[tl] = '\0';
  1273. /* First token -> command name. */
  1274. {
  1275. int first = 1;
  1276. for (i = 0; i < start; i++)
  1277. if (t->line[i] != ' ') {
  1278. first = 0;
  1279. break;
  1280. }
  1281. if (first) {
  1282. unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
  1283. unsigned int count = 0, lcp_len = 0, best = 0;
  1284. char lcp[16];
  1285. for (i = 0; i < nitems; i++) {
  1286. unsigned int nl = help_name_len(help_items[i]);
  1287. if (nl < tl || !prefix_ci(help_items[i], token, tl))
  1288. continue;
  1289. if (count == 0) {
  1290. for (j = 0; j < nl; j++)
  1291. lcp[j] = help_items[i][j];
  1292. lcp_len = nl;
  1293. best = i;
  1294. } else {
  1295. for (j = 0; j < lcp_len && j < nl &&
  1296. lcp[j] == help_items[i][j]; j++)
  1297. ;
  1298. lcp_len = j;
  1299. }
  1300. count++;
  1301. }
  1302. if (count == 0)
  1303. return;
  1304. if (count == 1) {
  1305. unsigned int nl = help_name_len(help_items[best]);
  1306. for (j = 0; j < nl; j++)
  1307. ins[j] = help_items[best][j];
  1308. ins[nl] = ' ';
  1309. ins[nl + 1] = '\0';
  1310. term_replace(t, start, end, ins);
  1311. return;
  1312. }
  1313. if (lcp_len > tl) {
  1314. for (j = 0; j < lcp_len; j++)
  1315. ins[j] = lcp[j];
  1316. ins[lcp_len] = '\0';
  1317. term_replace(t, start, end, ins);
  1318. return;
  1319. }
  1320. io_putchar('\n');
  1321. tab_col = 0;
  1322. tab_width = t->screen.cols;
  1323. for (i = 0; i < nitems; i++) {
  1324. unsigned int nl = help_name_len(help_items[i]);
  1325. char name[16];
  1326. if (nl < tl || !prefix_ci(help_items[i], token, tl))
  1327. continue;
  1328. for (j = 0; j < nl && j < 15; j++)
  1329. name[j] = help_items[i][j];
  1330. name[j] = '\0';
  1331. tab_print_one(name, 0);
  1332. }
  1333. io_putchar('\n');
  1334. term_print_prompt(t, 1);
  1335. redraw_line_clear(t->line, t->line_len, t->line_pos,
  1336. t->line_start, t->line_len);
  1337. return;
  1338. }
  1339. }
  1340. /* Argument token -> VFS path. */
  1341. {
  1342. char dirpart[LINE_MAX + 1];
  1343. const char *last = 0, *p;
  1344. unsigned int dp, n;
  1345. int mounts = 0;
  1346. for (p = token; *p; p++)
  1347. if (*p == '/')
  1348. last = p;
  1349. if (last) {
  1350. dp = (unsigned int)(last - token) + 1;
  1351. for (i = 0; i < dp; i++)
  1352. dirpart[i] = token[i];
  1353. dirpart[dp] = '\0';
  1354. p = last + 1;
  1355. } else {
  1356. dp = 0;
  1357. dirpart[0] = '\0';
  1358. p = token;
  1359. }
  1360. {
  1361. unsigned int k = 0;
  1362. while (*p && k < 15)
  1363. tab_prefix[k++] = *p++;
  1364. tab_prefix[k] = '\0';
  1365. tab_prefix_len = k;
  1366. }
  1367. if (dirpart[0] == '/' && dirpart[1] == '\0')
  1368. mounts = 1;
  1369. if (dirpart[0] == '/') {
  1370. if (dp >= VFS_PATH_MAX)
  1371. return;
  1372. for (i = 0; dirpart[i]; i++)
  1373. dir[i] = dirpart[i];
  1374. dir[i] = '\0';
  1375. } else {
  1376. if (vfs_join_path(t->cwd, dirpart, dir, sizeof(dir)) != 0)
  1377. return;
  1378. }
  1379. n = mounts ? (unsigned int)tab_scan_mounts()
  1380. : (unsigned int)tab_scan_dir(dir);
  1381. if (n == 0)
  1382. return;
  1383. if (dp + tab_lcp_len + 2 > LINE_MAX)
  1384. return;
  1385. if (n == 1) {
  1386. for (i = 0; i < dp; i++)
  1387. ins[i] = dirpart[i];
  1388. for (i = 0; i < tab_lcp_len; i++)
  1389. ins[dp + i] = tab_lcp[i];
  1390. ins[dp + tab_lcp_len] = (mounts || tab_single_dir) ? '/' : ' ';
  1391. ins[dp + tab_lcp_len + 1] = '\0';
  1392. term_replace(t, start, end, ins);
  1393. return;
  1394. }
  1395. if (tab_lcp_len > tab_prefix_len) {
  1396. for (i = 0; i < dp; i++)
  1397. ins[i] = dirpart[i];
  1398. for (i = 0; i < tab_lcp_len; i++)
  1399. ins[dp + i] = tab_lcp[i];
  1400. ins[dp + tab_lcp_len] = '\0';
  1401. term_replace(t, start, end, ins);
  1402. return;
  1403. }
  1404. term_tab_ambiguous(t, mounts);
  1405. }
  1406. }
  1407. /* Push current line to history if non-empty (and not duplicate of newest). */
  1408. static void history_push(struct term *t, const char *line, unsigned int line_len)
  1409. {
  1410. unsigned int i, slot;
  1411. int all_spaces = 1;
  1412. if (line_len == 0)
  1413. return;
  1414. for (i = 0; i < line_len; i++) {
  1415. if (line[i] != ' ') {
  1416. all_spaces = 0;
  1417. break;
  1418. }
  1419. }
  1420. if (all_spaces)
  1421. return;
  1422. if (t->history_count > 0) {
  1423. slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  1424. for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
  1425. if (t->history[slot][i] != line[i])
  1426. break;
  1427. }
  1428. if (i == line_len && t->history[slot][i] == '\0')
  1429. return; /* duplicate of newest */
  1430. }
  1431. slot = t->history_next;
  1432. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  1433. t->history[slot][i] = line[i];
  1434. t->history[slot][i] = '\0';
  1435. t->history_next = (t->history_next + 1) % HISTORY_SIZE;
  1436. if (t->history_count < HISTORY_SIZE)
  1437. t->history_count++;
  1438. }
  1439. /* Change directory: resolve `arg` (relative to t->cwd, or absolute) and
  1440. * validate it is an existing directory. Updates t->cwd on success. */
  1441. static void term_cd(struct term *t, const char *arg)
  1442. {
  1443. char abs[VFS_PATH_MAX];
  1444. fat_fs_t *fs;
  1445. const char *fs_path;
  1446. unsigned int dir;
  1447. int r;
  1448. unsigned int i;
  1449. if (arg[0] == '\0') {
  1450. t->cwd[0] = '/';
  1451. t->cwd[1] = '\0';
  1452. return;
  1453. }
  1454. if (vfs_join_path(t->cwd, arg, abs, sizeof(abs)) != 0) {
  1455. io_putstr("cd: path too long\n");
  1456. return;
  1457. }
  1458. if (abs[0] == '/' && abs[1] == '\0') {
  1459. t->cwd[0] = '/';
  1460. t->cwd[1] = '\0';
  1461. return;
  1462. }
  1463. if (vfs_resolve(abs, &fs, &fs_path) != 0) {
  1464. io_putstr("cd: no such directory\n");
  1465. return;
  1466. }
  1467. r = fat_resolve_dir(fs, fs_path, &dir);
  1468. if (r == -2) {
  1469. io_putstr("cd: disk error\n");
  1470. return;
  1471. }
  1472. if (r != 0) {
  1473. io_putstr("cd: no such directory\n");
  1474. return;
  1475. }
  1476. for (i = 0; abs[i] && i < sizeof(t->cwd) - 1; i++)
  1477. t->cwd[i] = abs[i];
  1478. t->cwd[i] = '\0';
  1479. }
  1480. /* Resolve the first token (path) of `args` against t->cwd, rebuilding
  1481. * "abs-path rest-of-args" into out. Used by `save`. */
  1482. static void term_resolve_first(struct term *t, const char *args,
  1483. char *out, unsigned int out_size)
  1484. {
  1485. char path[VFS_PATH_MAX];
  1486. char abs[VFS_PATH_MAX];
  1487. unsigned int i = 0, j = 0;
  1488. while (args[i] == ' ')
  1489. i++;
  1490. while (args[i] && args[i] != ' ' && j < sizeof(path) - 1)
  1491. path[j++] = args[i++];
  1492. path[j] = '\0';
  1493. if (vfs_join_path(t->cwd, path, abs, sizeof(abs)) != 0) {
  1494. abs[0] = '/';
  1495. abs[1] = '\0';
  1496. }
  1497. while (args[i] == ' ')
  1498. i++;
  1499. j = 0;
  1500. {
  1501. unsigned int k = 0;
  1502. while (abs[k] && j + 1 < out_size)
  1503. out[j++] = abs[k++];
  1504. }
  1505. if (args[i]) {
  1506. if (j + 1 < out_size)
  1507. out[j++] = ' ';
  1508. while (args[i] && j + 1 < out_size)
  1509. out[j++] = args[i++];
  1510. }
  1511. out[j] = '\0';
  1512. }
  1513. /* Spawn a foreground command process; the caller (term_run's wait loop)
  1514. * waits for it to finish and handles Ctrl+C. */
  1515. static void term_spawn(struct term *t, const char *name, void (*entry)(void))
  1516. {
  1517. int pid = process_create(name, entry);
  1518. if (pid < 0)
  1519. io_putstr("error: too many processes\n");
  1520. else
  1521. t->fg_pid = pid;
  1522. }
  1523. static void term_spawn_arg(struct term *t, const char *name,
  1524. void (*entry)(const char *), const char *arg)
  1525. {
  1526. int pid = process_create_arg(name, entry, arg);
  1527. if (pid < 0)
  1528. io_putstr("error: too many processes\n");
  1529. else
  1530. t->fg_pid = pid;
  1531. }
  1532. void term_run(struct term *t)
  1533. {
  1534. t->line_len = 0;
  1535. t->line_pos = 0;
  1536. t->line[0] = '\0';
  1537. t->history_browse = 0;
  1538. t->fg_pid = -1;
  1539. io_clear_screen();
  1540. term_print_prompt(t, 1);
  1541. for (;;) {
  1542. if (t->fg_pid >= 0) {
  1543. /* Waiting on a foreground child: poll keys for Ctrl+C
  1544. * and watch for the child to exit. */
  1545. int k = term_key_pop(t);
  1546. if (k == IO_KEY_CTRL_C) {
  1547. int killed = (process_kill((unsigned int)t->fg_pid) == 0);
  1548. t->fg_pid = -1;
  1549. if (killed)
  1550. io_putstr("^C\n");
  1551. term_print_prompt(t, 0);
  1552. } else if (!process_is_alive((unsigned int)t->fg_pid)) {
  1553. t->fg_pid = -1;
  1554. term_print_prompt(t, 0);
  1555. } else {
  1556. process_sleep_ms(10);
  1557. }
  1558. continue;
  1559. }
  1560. {
  1561. int k = term_key_pop(t);
  1562. if (k) {
  1563. kdbg_term_count++;
  1564. kdbg_term_last = (unsigned int)k;
  1565. kdbg_status();
  1566. if (k == IO_KEY_CTRL_C) {
  1567. /* Ctrl+C at the prompt: discard the line. */
  1568. io_putstr("^C\n");
  1569. t->line_len = 0;
  1570. t->line_pos = 0;
  1571. t->line[0] = '\0';
  1572. term_print_prompt(t, 0);
  1573. } else if (k == IO_KEY_LEFT) {
  1574. if (t->line_pos > 0) {
  1575. t->line_pos--;
  1576. io_set_cursor(t->line_start + t->line_pos);
  1577. }
  1578. } else if (k == IO_KEY_RIGHT) {
  1579. if (t->line_pos < t->line_len) {
  1580. t->line_pos++;
  1581. io_set_cursor(t->line_start + t->line_pos);
  1582. }
  1583. } else if (k == IO_KEY_UP) {
  1584. if (t->history_count > 0) {
  1585. if (t->history_browse < t->history_count)
  1586. t->history_browse++;
  1587. history_load_into_line(t, t->history_browse);
  1588. }
  1589. } else if (k == IO_KEY_DOWN) {
  1590. if (t->history_browse > 0) {
  1591. t->history_browse--;
  1592. if (t->history_browse == 0) {
  1593. unsigned int clear_len = t->line_len;
  1594. t->line_len = 0;
  1595. t->line_pos = 0;
  1596. t->line[0] = '\0';
  1597. redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
  1598. } else {
  1599. history_load_into_line(t, t->history_browse);
  1600. }
  1601. }
  1602. } else if (k == '\b') {
  1603. if (t->line_pos > 0) {
  1604. unsigned int i;
  1605. t->line_pos--;
  1606. for (i = t->line_pos; i < t->line_len; i++)
  1607. t->line[i] = t->line[i + 1];
  1608. t->line_len--;
  1609. t->line[t->line_len] = '\0';
  1610. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
  1611. }
  1612. } else if (k == '\t') {
  1613. term_complete(t);
  1614. } else if (k == '\n') {
  1615. char cat_fname[CAT_FNAME_MAX];
  1616. char save_args[SAVE_ARGS_MAX];
  1617. char rm_fname[RM_FNAME_MAX];
  1618. char ls_path[LS_PATH_MAX];
  1619. char format_dev[FORMAT_DEV_MAX];
  1620. char netip_arg[16];
  1621. char netping_arg[16];
  1622. char netlog_arg[NET_UDP_ARG_MAX];
  1623. char netconsole_arg[NET_UDP_ARG_MAX];
  1624. char curl_arg[128];
  1625. char netdns_arg[32];
  1626. char lookup_arg[64];
  1627. char lspci_arg[16];
  1628. char peek_arg[32];
  1629. char resolved[VFS_PATH_MAX];
  1630. char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
  1631. unsigned int kill_pid;
  1632. int skip_prompt = 0;
  1633. io_putchar('\n');
  1634. history_push(t, t->line, t->line_len);
  1635. t->history_browse = 0;
  1636. if (is_cmd_hello(t->line, t->line_len)) {
  1637. int pid = process_create_user("hello",
  1638. _binary_hello_elf_start,
  1639. (unsigned int)(_binary_hello_elf_end -
  1640. _binary_hello_elf_start));
  1641. if (pid < 0)
  1642. io_putstr("error: too many processes\n");
  1643. else
  1644. t->fg_pid = pid;
  1645. } else if (is_cmd_reboot(t->line, t->line_len)) {
  1646. program_reboot_main();
  1647. } else if (is_cmd_poweroff(t->line, t->line_len)) {
  1648. program_poweroff_main();
  1649. } else if (is_cmd_cd(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  1650. term_cd(t, ls_path);
  1651. } else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  1652. if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
  1653. term_spawn_arg(t, "ls", program_ls_main, resolved);
  1654. } else {
  1655. io_putstr("ls: path too long\n");
  1656. }
  1657. } else if (is_cmd_lsusb(t->line, t->line_len)) {
  1658. term_spawn(t, "lsusb", program_lsusb_main);
  1659. } else if (is_cmd_lspci(t->line, t->line_len, lspci_arg, sizeof(lspci_arg))) {
  1660. term_spawn_arg(t, "lspci", program_lspci_main, lspci_arg);
  1661. } else if (is_cmd_peek(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
  1662. term_spawn_arg(t, "peek", program_peek_main, peek_arg);
  1663. } else if (is_cmd_poke(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
  1664. term_spawn_arg(t, "poke", program_poke_main, peek_arg);
  1665. } else if (is_cmd_netinfo(t->line, t->line_len)) {
  1666. term_spawn(t, "netinfo", program_netinfo_main);
  1667. } else if (is_cmd_netrecv(t->line, t->line_len)) {
  1668. term_spawn(t, "netrecv", program_netrecv_main);
  1669. } else if (is_cmd_netsend(t->line, t->line_len)) {
  1670. term_spawn(t, "netsend", program_netsend_main);
  1671. } else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
  1672. term_spawn_arg(t, "netip", program_netip_main, netip_arg);
  1673. } else if (is_cmd_netping(t->line, t->line_len, netping_arg, sizeof(netping_arg))) {
  1674. term_spawn_arg(t, "netping", program_netping_main, netping_arg);
  1675. } else if (is_cmd_netlog(t->line, t->line_len, netlog_arg, sizeof(netlog_arg))) {
  1676. term_spawn_arg(t, "netlog", program_netlog_main, netlog_arg);
  1677. } else if (is_cmd_netconsole(t->line, t->line_len, netconsole_arg, sizeof(netconsole_arg))) {
  1678. term_spawn_arg(t, "netconsole", program_netconsole_main, netconsole_arg);
  1679. } else if (is_cmd_dhcp(t->line, t->line_len)) {
  1680. term_spawn(t, "dhcp", program_dhcp_main);
  1681. } else if (is_cmd_netregs(t->line, t->line_len)) {
  1682. term_spawn(t, "netregs", program_netregs_main);
  1683. } else if (is_cmd_netloop(t->line, t->line_len)) {
  1684. term_spawn(t, "netloop", program_netloop_main);
  1685. } else if (is_cmd_netreset(t->line, t->line_len)) {
  1686. term_spawn(t, "netreset", program_netreset_main);
  1687. } else if (is_cmd_netdns(t->line, t->line_len, netdns_arg, sizeof(netdns_arg))) {
  1688. term_spawn_arg(t, "netdns", program_netdns_main, netdns_arg);
  1689. } else if (is_cmd_curl(t->line, t->line_len, curl_arg, sizeof(curl_arg))) {
  1690. term_spawn_arg(t, "curl", program_curl_main, curl_arg);
  1691. } else if (is_cmd_lookup(t->line, t->line_len, lookup_arg, sizeof(lookup_arg))) {
  1692. term_spawn_arg(t, "lookup", program_lookup_main, lookup_arg);
  1693. } else if (is_cmd_ping(t->line, t->line_len)) {
  1694. term_spawn(t, "ping", program_ping_main);
  1695. } else if (is_cmd_storage(t->line, t->line_len)) {
  1696. term_spawn(t, "storage", program_storage_main);
  1697. } else if (is_cmd_meminfo(t->line, t->line_len)) {
  1698. term_spawn(t, "meminfo", program_mem_main);
  1699. } else if (is_cmd_clock(t->line, t->line_len)) {
  1700. io_putstr("clock: not available in windowed mode yet\n");
  1701. } else if (is_cmd_colors(t->line, t->line_len)) {
  1702. io_putstr("colors: not available in windowed mode yet\n");
  1703. } else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  1704. if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
  1705. term_spawn_arg(t, "cat", program_cat_main, resolved);
  1706. } else {
  1707. io_putstr("cat: path too long\n");
  1708. }
  1709. } else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  1710. if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
  1711. cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
  1712. cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
  1713. cat_fname[6] == '\0') {
  1714. if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
  1715. io_putstr("error: too many processes\n");
  1716. } else {
  1717. io_putstr("run: not available in windowed mode yet\n");
  1718. }
  1719. } else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
  1720. term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
  1721. term_spawn_arg(t, "save", program_save_main, save_resolved);
  1722. } else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1723. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1724. term_spawn_arg(t, "mkdir", program_mkdir_main, resolved);
  1725. } else {
  1726. io_putstr("mkdir: path too long\n");
  1727. }
  1728. } else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1729. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1730. term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
  1731. } else {
  1732. io_putstr("rmdir: path too long\n");
  1733. }
  1734. } else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1735. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1736. term_spawn_arg(t, "rm", program_rm_main, resolved);
  1737. } else {
  1738. io_putstr("rm: path too long\n");
  1739. }
  1740. } else if (is_cmd_space(t->line, t->line_len)) {
  1741. term_spawn_arg(t, "space", program_run_embedded_main, "space");
  1742. } else if (is_cmd_prog(t->line, t->line_len)) {
  1743. term_spawn_arg(t, "prog", program_run_embedded_main, "prog");
  1744. } else if (is_cmd_display(t->line, t->line_len)) {
  1745. term_spawn(t, "display", program_display_main);
  1746. } else if (is_cmd_cpuinfo(t->line, t->line_len)) {
  1747. term_spawn(t, "cpuinfo", program_cpuinfo_main);
  1748. } else if (is_cmd_mouseinfo(t->line, t->line_len)) {
  1749. term_spawn(t, "mouseinfo", program_mouseinfo_main);
  1750. } else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
  1751. program_format_main(format_dev);
  1752. } else if (is_cmd_ps(t->line, t->line_len)) {
  1753. process_ps();
  1754. } else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
  1755. if (process_kill(kill_pid) < 0)
  1756. io_putstr("kill: no such process\n");
  1757. else
  1758. io_putstr("killed\n");
  1759. } else if (is_cmd_kexec(t->line, t->line_len)) {
  1760. if (kexec_busy) {
  1761. io_putstr("kexec: already receiving\n");
  1762. } else {
  1763. kexec_busy = 1;
  1764. if (process_create("kexec", program_kexec_main) < 0) {
  1765. kexec_busy = 0;
  1766. io_putstr("error: too many processes\n");
  1767. }
  1768. }
  1769. } else if (is_cmd_dbg(t->line, t->line_len)) {
  1770. program_dbg_main();
  1771. } else if (is_cmd_help(t->line, t->line_len)) {
  1772. help_print();
  1773. } else if (is_cmd_clear(t->line, t->line_len)) {
  1774. io_clear_screen();
  1775. term_print_prompt(t, 1);
  1776. skip_prompt = 1;
  1777. } else {
  1778. char serial_arg[8];
  1779. if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
  1780. if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
  1781. serial_arg[2] == 's' && serial_arg[3] == 't' &&
  1782. serial_arg[4] == '\0') {
  1783. if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
  1784. io_putstr("error: too many processes\n");
  1785. } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
  1786. serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
  1787. serial_arg[4] == '\0') {
  1788. program_serial_recv_main(serial_arg);
  1789. } else {
  1790. io_putstr("serial: usage serial test|recv\n");
  1791. }
  1792. } else {
  1793. unsigned int i = 0;
  1794. while (i < t->line_len && t->line[i] == ' ')
  1795. i++;
  1796. if (i < t->line_len)
  1797. io_putstr("command not found\n");
  1798. }
  1799. }
  1800. t->line_len = 0;
  1801. t->line_pos = 0;
  1802. t->line[0] = '\0';
  1803. if (!skip_prompt && t->fg_pid < 0) {
  1804. term_print_prompt(t, 0);
  1805. }
  1806. } else {
  1807. unsigned char u = (unsigned char)k;
  1808. if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
  1809. unsigned int i;
  1810. t->history_browse = 0;
  1811. for (i = t->line_len; i > t->line_pos; i--)
  1812. t->line[i] = t->line[i - 1];
  1813. t->line[t->line_pos] = (char)k;
  1814. t->line_len++;
  1815. t->line[t->line_len] = '\0';
  1816. t->line_pos++;
  1817. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
  1818. }
  1819. }
  1820. } else {
  1821. process_sleep_ms(10);
  1822. }
  1823. }
  1824. }
  1825. }
  1826. void term_entry(void *arg)
  1827. {
  1828. struct term *t = (struct term *)arg;
  1829. process_set_screen(&t->screen);
  1830. term_run(t);
  1831. }