terminal.c 49 KB

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