terminal.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  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_netlog_main(const char *arg);
  42. extern void program_netconsole_main(const char *arg);
  43. extern void program_dhcp_main(void);
  44. extern void program_netregs_main(void);
  45. extern void program_netloop_main(void);
  46. extern void program_netreset_main(void);
  47. extern void program_pong_main(void);
  48. extern void program_ping_main(const char *arg);
  49. extern void program_curl_main(const char *arg);
  50. extern void program_netdns_main(const char *arg);
  51. extern void program_host_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. /* One optional single-token argument (hostname or dotted IP). */
  536. static int is_cmd_ping(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  537. {
  538. unsigned int i = 0, j = 0;
  539. while (i < line_len && line[i] == ' ')
  540. i++;
  541. if (i + 4 > line_len)
  542. return 0;
  543. if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' ||
  544. line[i+3] != 'g')
  545. return 0;
  546. i += 4;
  547. if (i < line_len && line[i] != ' ')
  548. return 0; /* "ping" 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. /* One optional single-token argument (DNS name). */
  619. static int is_cmd_host(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  620. {
  621. unsigned int i = 0, j = 0;
  622. while (i < line_len && line[i] == ' ')
  623. i++;
  624. if (i + 4 > line_len)
  625. return 0;
  626. if (line[i] != 'h' || line[i+1] != 'o' || line[i+2] != 's' ||
  627. line[i+3] != 't')
  628. return 0;
  629. i += 4;
  630. if (i < line_len && line[i] != ' ')
  631. return 0;
  632. while (i < line_len && line[i] == ' ')
  633. i++;
  634. if (i >= line_len) {
  635. if (buf_size > 0)
  636. arg_buf[0] = '\0';
  637. return 1;
  638. }
  639. while (i < line_len && j + 1 < buf_size)
  640. arg_buf[j++] = line[i++];
  641. arg_buf[j] = '\0';
  642. while (i < line_len && line[i] == ' ')
  643. i++;
  644. return (i == line_len);
  645. }
  646. static int is_cmd_pong(const char *line, unsigned int line_len)
  647. {
  648. unsigned int i = 0;
  649. while (i < line_len && line[i] == ' ')
  650. i++;
  651. if (i + 4 > line_len)
  652. return 0;
  653. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'n' || line[i+3] != 'g')
  654. return 0;
  655. i += 4;
  656. while (i < line_len && line[i] == ' ')
  657. i++;
  658. return (i == line_len);
  659. }
  660. static int is_cmd_space(const char *line, unsigned int line_len)
  661. {
  662. unsigned int i = 0;
  663. while (i < line_len && line[i] == ' ')
  664. i++;
  665. if (i + 5 > line_len)
  666. return 0;
  667. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  668. line[i+3] != 'c' || line[i+4] != 'e')
  669. return 0;
  670. i += 5;
  671. while (i < line_len && line[i] == ' ')
  672. i++;
  673. return (i == line_len);
  674. }
  675. #define NET_UDP_ARG_MAX 128
  676. #define CAT_FNAME_MAX 64
  677. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  678. {
  679. unsigned int i = 0, j;
  680. while (i < line_len && line[i] == ' ')
  681. i++;
  682. if (i + 3 > line_len)
  683. return 0;
  684. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  685. return 0;
  686. i += 3;
  687. while (i < line_len && line[i] == ' ')
  688. i++;
  689. if (i >= line_len)
  690. return 0;
  691. j = 0;
  692. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  693. filename_buf[j++] = line[i++];
  694. filename_buf[j] = '\0';
  695. return (j > 0);
  696. }
  697. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  698. {
  699. unsigned int i = 0, j;
  700. while (i < line_len && line[i] == ' ')
  701. i++;
  702. if (i + 3 > line_len)
  703. return 0;
  704. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  705. return 0;
  706. i += 3;
  707. while (i < line_len && line[i] == ' ')
  708. i++;
  709. if (i >= line_len)
  710. return 0;
  711. j = 0;
  712. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  713. filename_buf[j++] = line[i++];
  714. filename_buf[j] = '\0';
  715. return (j > 0);
  716. }
  717. #define SAVE_ARGS_MAX 128
  718. #define RM_FNAME_MAX 64
  719. #define LS_PATH_MAX 64
  720. #define FORMAT_DEV_MAX 16
  721. static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  722. {
  723. unsigned int i = 0, j = 0;
  724. while (i < line_len && line[i] == ' ')
  725. i++;
  726. if (i + 4 > line_len)
  727. return 0;
  728. if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
  729. return 0;
  730. i += 4;
  731. while (i < line_len && line[i] == ' ')
  732. i++;
  733. if (i >= line_len)
  734. return 0;
  735. while (i < line_len && j + 1 < buf_size)
  736. arg_buf[j++] = line[i++];
  737. while (j > 0 && arg_buf[j - 1] == ' ')
  738. j--;
  739. arg_buf[j] = '\0';
  740. while (i < line_len && line[i] == ' ')
  741. i++;
  742. return (i == line_len && j > 0);
  743. }
  744. static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  745. {
  746. unsigned int i = 0, j = 0;
  747. while (i < line_len && line[i] == ' ')
  748. i++;
  749. if (i + 2 > line_len)
  750. return 0;
  751. if (line[i] != 'r' || line[i+1] != 'm')
  752. return 0;
  753. i += 2;
  754. while (i < line_len && line[i] == ' ')
  755. i++;
  756. if (i >= line_len)
  757. return 0;
  758. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  759. filename_buf[j++] = line[i++];
  760. filename_buf[j] = '\0';
  761. return (j > 0);
  762. }
  763. static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  764. {
  765. unsigned int i = 0, j = 0;
  766. while (i < line_len && line[i] == ' ')
  767. i++;
  768. if (i + 5 > line_len)
  769. return 0;
  770. if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  771. return 0;
  772. i += 5;
  773. while (i < line_len && line[i] == ' ')
  774. i++;
  775. if (i >= line_len)
  776. return 0;
  777. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  778. path_buf[j++] = line[i++];
  779. path_buf[j] = '\0';
  780. return (j > 0);
  781. }
  782. static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  783. {
  784. unsigned int i = 0, j = 0;
  785. while (i < line_len && line[i] == ' ')
  786. i++;
  787. if (i + 5 > line_len)
  788. return 0;
  789. if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  790. return 0;
  791. i += 5;
  792. while (i < line_len && line[i] == ' ')
  793. i++;
  794. if (i >= line_len)
  795. return 0;
  796. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  797. path_buf[j++] = line[i++];
  798. path_buf[j] = '\0';
  799. return (j > 0);
  800. }
  801. static int is_cmd_storage(const char *line, unsigned int line_len)
  802. {
  803. unsigned int i = 0;
  804. while (i < line_len && line[i] == ' ')
  805. i++;
  806. if (i + 7 > line_len)
  807. return 0;
  808. if (line[i] != 's' || line[i+1] != 't' || line[i+2] != 'o' ||
  809. line[i+3] != 'r' || line[i+4] != 'a' || line[i+5] != 'g' || line[i+6] != 'e')
  810. return 0;
  811. i += 7;
  812. while (i < line_len && line[i] == ' ')
  813. i++;
  814. return (i == line_len);
  815. }
  816. static int is_cmd_meminfo(const char *line, unsigned int line_len)
  817. {
  818. unsigned int i = 0;
  819. while (i < line_len && line[i] == ' ')
  820. i++;
  821. if (i + 7 > line_len)
  822. return 0;
  823. if (line[i] != 'm' || line[i+1] != 'e' || line[i+2] != 'm' ||
  824. line[i+3] != 'i' || line[i+4] != 'n' || line[i+5] != 'f' ||
  825. line[i+6] != 'o')
  826. return 0;
  827. i += 7;
  828. if (i < line_len && line[i] != ' ')
  829. return 0; /* "meminfo" must be a whole word */
  830. while (i < line_len && line[i] == ' ')
  831. i++;
  832. return (i == line_len);
  833. }
  834. static int is_cmd_format(const char *line, unsigned int line_len, char *dev_buf, unsigned int buf_size)
  835. {
  836. unsigned int i = 0, j;
  837. while (i < line_len && line[i] == ' ')
  838. i++;
  839. if (i + 6 > line_len)
  840. return 0;
  841. if (line[i] != 'f' || line[i+1] != 'o' || line[i+2] != 'r' ||
  842. line[i+3] != 'm' || line[i+4] != 'a' || line[i+5] != 't')
  843. return 0;
  844. i += 6;
  845. if (i < line_len && line[i] != ' ')
  846. return 0; /* "format" must be a whole word */
  847. while (i < line_len && line[i] == ' ')
  848. i++;
  849. if (i >= line_len)
  850. return 0;
  851. j = 0;
  852. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  853. dev_buf[j++] = line[i++];
  854. dev_buf[j] = '\0';
  855. while (i < line_len && line[i] == ' ')
  856. i++;
  857. return (i == line_len && j > 0);
  858. }
  859. /* Help listing: printed as two 32-column columns so it fits the 64-column
  860. terminal without scrolling. */
  861. static const char *const help_items[] = {
  862. "cd [path]", "ls [path]", "cat <path>", "save <path> <text>",
  863. "rm <path>", "mkdir <path>", "rmdir <path>", "hello", "reboot",
  864. "poweroff", "run <name>|<file>", "space", "prog", "clock", "colors",
  865. "display", "cpuinfo", "lsusb", "mouseinfo", "storage", "meminfo",
  866. "format <device>", "lspci [id]", "peek <addr> [len]", "poke <addr> <val>",
  867. "netinfo",
  868. "netrecv", "netsend", "netip [a.b.c.d]", "ping <host|a.b.c.d>",
  869. "netlog <text>", "netconsole [on|off|host <ip>]", "dhcp", "netregs",
  870. "netloop", "netreset", "pong", "ps", "kill <pid>", "kexec", "dbg",
  871. "serial test|recv", "clear", "curl <url>", "netdns [a.b.c.d]",
  872. "host <name>",
  873. };
  874. static void help_puts(const char *s)
  875. {
  876. while (*s)
  877. io_putchar(*s++);
  878. }
  879. static void help_item_pad(const char *s)
  880. {
  881. unsigned int n = 0;
  882. while (s[n]) {
  883. io_putchar(s[n]);
  884. n++;
  885. }
  886. while (n < 32) {
  887. io_putchar(' ');
  888. n++;
  889. }
  890. }
  891. static void help_print(void)
  892. {
  893. unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
  894. unsigned int half = (nitems + 1) / 2;
  895. unsigned int k;
  896. for (k = 0; k < half; k++) {
  897. if (k + half < nitems) {
  898. help_item_pad(help_items[k]);
  899. help_puts(help_items[k + half]);
  900. } else {
  901. help_puts(help_items[k]);
  902. }
  903. io_putchar('\n');
  904. }
  905. }
  906. static int is_cmd_help(const char *line, unsigned int line_len)
  907. {
  908. unsigned int i = 0;
  909. while (i < line_len && line[i] == ' ')
  910. i++;
  911. if (i + 4 > line_len)
  912. return 0;
  913. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  914. return 0;
  915. i += 4;
  916. while (i < line_len && line[i] == ' ')
  917. i++;
  918. return (i == line_len);
  919. }
  920. static int is_cmd_clear(const char *line, unsigned int line_len)
  921. {
  922. unsigned int i = 0;
  923. while (i < line_len && line[i] == ' ')
  924. i++;
  925. if (i + 5 > line_len)
  926. return 0;
  927. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  928. line[i+3] != 'a' || line[i+4] != 'r')
  929. return 0;
  930. i += 5;
  931. while (i < line_len && line[i] == ' ')
  932. i++;
  933. return (i == line_len);
  934. }
  935. static int is_cmd_ps(const char *line, unsigned int line_len)
  936. {
  937. unsigned int i = 0;
  938. while (i < line_len && line[i] == ' ')
  939. i++;
  940. if (i + 2 > line_len)
  941. return 0;
  942. if (line[i] != 'p' || line[i+1] != 's')
  943. return 0;
  944. i += 2;
  945. while (i < line_len && line[i] == ' ')
  946. i++;
  947. return (i == line_len);
  948. }
  949. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  950. {
  951. unsigned int i = 0, num = 0;
  952. int has_digit = 0;
  953. while (i < line_len && line[i] == ' ')
  954. i++;
  955. if (i + 4 > line_len)
  956. return 0;
  957. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  958. return 0;
  959. i += 4;
  960. while (i < line_len && line[i] == ' ')
  961. i++;
  962. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  963. num = num * 10 + (unsigned int)(line[i] - '0');
  964. has_digit = 1;
  965. i++;
  966. }
  967. if (!has_digit)
  968. return 0;
  969. while (i < line_len && line[i] == ' ')
  970. i++;
  971. if (i != line_len)
  972. return 0;
  973. *pid_out = num;
  974. return 1;
  975. }
  976. static int is_cmd_kexec(const char *line, unsigned int line_len)
  977. {
  978. unsigned int i = 0;
  979. while (i < line_len && line[i] == ' ')
  980. i++;
  981. if (i + 5 > line_len)
  982. return 0;
  983. if (line[i] != 'k' || line[i+1] != 'e' || line[i+2] != 'x' || line[i+3] != 'e' || line[i+4] != 'c')
  984. return 0;
  985. i += 5;
  986. while (i < line_len && line[i] == ' ')
  987. i++;
  988. return (i == line_len);
  989. }
  990. static int is_cmd_dbg(const char *line, unsigned int line_len)
  991. {
  992. unsigned int i = 0;
  993. while (i < line_len && line[i] == ' ')
  994. i++;
  995. if (i + 3 > line_len)
  996. return 0;
  997. if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
  998. return 0;
  999. i += 3;
  1000. while (i < line_len && line[i] == ' ')
  1001. i++;
  1002. return (i == line_len);
  1003. }
  1004. static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  1005. {
  1006. unsigned int i = 0, j = 0;
  1007. while (i < line_len && line[i] == ' ')
  1008. i++;
  1009. if (i + 6 > line_len)
  1010. return 0;
  1011. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
  1012. line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
  1013. return 0;
  1014. i += 6;
  1015. while (i < line_len && line[i] == ' ')
  1016. i++;
  1017. if (i >= line_len) {
  1018. if (buf_size > 0)
  1019. arg_buf[0] = '\0';
  1020. return 1;
  1021. }
  1022. while (i < line_len && j + 1 < buf_size)
  1023. arg_buf[j++] = line[i++];
  1024. while (j > 0 && arg_buf[j - 1] == ' ')
  1025. j--;
  1026. arg_buf[j] = '\0';
  1027. while (i < line_len && line[i] == ' ')
  1028. i++;
  1029. return (i == line_len);
  1030. }
  1031. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  1032. static void redraw_line_from(const char *line, unsigned int line_len,
  1033. unsigned int line_pos, unsigned int line_start, unsigned int from)
  1034. {
  1035. unsigned int i;
  1036. io_set_cursor(line_start + from);
  1037. for (i = from; i < line_len; i++)
  1038. io_putchar(line[i]);
  1039. io_putchar(' ');
  1040. io_set_cursor(line_start + line_pos);
  1041. }
  1042. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  1043. static void redraw_line_clear(const char *line, unsigned int line_len,
  1044. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  1045. {
  1046. unsigned int i;
  1047. io_set_cursor(line_start);
  1048. for (i = 0; i < line_len; i++)
  1049. io_putchar(line[i]);
  1050. for (i = line_len; i <= clear_through; i++)
  1051. io_putchar(' ');
  1052. io_set_cursor(line_start + line_pos);
  1053. }
  1054. /* Copy history entry (1 = newest) into t->line, redraw from start. */
  1055. static void history_load_into_line(struct term *t, unsigned int browse)
  1056. {
  1057. unsigned int slot, len, old_len;
  1058. const char *src;
  1059. if (browse < 1 || browse > t->history_count)
  1060. return;
  1061. old_len = t->line_len;
  1062. slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  1063. src = t->history[slot];
  1064. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  1065. t->line[len] = src[len];
  1066. t->line[len] = '\0';
  1067. t->line_len = len;
  1068. t->line_pos = len;
  1069. redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
  1070. }
  1071. /* ---- Tab completion ---- */
  1072. /* Command names come from the help table ("ls [path]" -> "ls"). */
  1073. static unsigned int help_name_len(const char *s)
  1074. {
  1075. unsigned int n = 0;
  1076. while (s[n] && s[n] != ' ' && s[n] != '[' && s[n] != '<' && s[n] != '|')
  1077. n++;
  1078. return n;
  1079. }
  1080. static char lower_ch(char c)
  1081. {
  1082. return (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
  1083. }
  1084. static int prefix_ci(const char *s, const char *prefix, unsigned int plen)
  1085. {
  1086. unsigned int i;
  1087. for (i = 0; i < plen; i++)
  1088. if (lower_ch(s[i]) != lower_ch(prefix[i]))
  1089. return 0;
  1090. return 1;
  1091. }
  1092. /* "HELLO TXT" -> "hello.txt" (a completion-ready 8.3 display name). */
  1093. static void tab_display_name(const char *n, char *out)
  1094. {
  1095. unsigned int bl = 0, el = 0, i, j = 0;
  1096. for (i = 0; i < 8 && n[i] && n[i] != ' '; i++)
  1097. bl = i + 1;
  1098. for (i = 8; i < 11 && n[i] && n[i] != ' '; i++)
  1099. el = i - 8 + 1;
  1100. for (i = 0; i < bl; i++)
  1101. out[j++] = lower_ch(n[i]);
  1102. if (el) {
  1103. out[j++] = '.';
  1104. for (i = 0; i < el; i++)
  1105. out[j++] = lower_ch(n[8 + i]);
  1106. }
  1107. out[j] = '\0';
  1108. }
  1109. /* Shared completion state (fat_list_dir takes no context pointer). */
  1110. static char tab_prefix[16];
  1111. static unsigned int tab_prefix_len;
  1112. static unsigned int tab_count;
  1113. static unsigned int tab_lcp_len;
  1114. static char tab_lcp[16];
  1115. static int tab_single_dir;
  1116. static fat_fs_t *tab_fs;
  1117. static unsigned int tab_dir_cluster;
  1118. static unsigned int tab_col;
  1119. static unsigned int tab_width;
  1120. static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
  1121. {
  1122. char disp[16];
  1123. unsigned int i, n;
  1124. (void)size;
  1125. if (name[0] == '.') /* "." / ".." */
  1126. return;
  1127. tab_display_name(name, disp);
  1128. if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
  1129. return;
  1130. n = 0;
  1131. while (disp[n])
  1132. n++;
  1133. if (tab_count == 0) {
  1134. for (i = 0; i < n; i++)
  1135. tab_lcp[i] = disp[i];
  1136. tab_lcp_len = n;
  1137. tab_single_dir = is_dir;
  1138. } else {
  1139. i = 0;
  1140. while (i < tab_lcp_len && i < n && tab_lcp[i] == disp[i])
  1141. i++;
  1142. tab_lcp_len = i;
  1143. }
  1144. tab_count++;
  1145. }
  1146. static void tab_print_one(const char *name, int is_dir)
  1147. {
  1148. unsigned int n = 0, w;
  1149. while (name[n])
  1150. n++;
  1151. w = n + (is_dir ? 1 : 0);
  1152. if (tab_col > 0 && tab_col + 2 + w > tab_width) {
  1153. io_putchar('\n');
  1154. tab_col = 0;
  1155. }
  1156. if (tab_col > 0) {
  1157. io_putstr(" ");
  1158. tab_col += 2;
  1159. }
  1160. io_putstr(name);
  1161. if (is_dir)
  1162. io_putchar('/');
  1163. tab_col += w;
  1164. }
  1165. static void tab_print_cb(const char *name, int is_dir, unsigned int size)
  1166. {
  1167. char disp[16];
  1168. (void)size;
  1169. if (name[0] == '.')
  1170. return;
  1171. tab_display_name(name, disp);
  1172. if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
  1173. return;
  1174. tab_print_one(disp, is_dir);
  1175. }
  1176. static int tab_scan_dir(const char *abs)
  1177. {
  1178. fat_fs_t *fs;
  1179. const char *fs_path;
  1180. if (vfs_resolve(abs, &fs, &fs_path) != 0)
  1181. return 0;
  1182. if (fat_resolve_dir(fs, fs_path, &tab_dir_cluster) != 0)
  1183. return 0;
  1184. tab_fs = fs;
  1185. tab_count = 0;
  1186. tab_lcp_len = 0;
  1187. fat_list_dir(fs, tab_dir_cluster, tab_collect_cb);
  1188. return (int)tab_count;
  1189. }
  1190. static int tab_scan_mounts(void)
  1191. {
  1192. unsigned int i;
  1193. tab_count = 0;
  1194. tab_lcp_len = 0;
  1195. for (i = 0; i < vfs_mount_count; i++) {
  1196. const char *name = vfs_mounts[i].name;
  1197. unsigned int n = 0, j;
  1198. while (name[n])
  1199. n++;
  1200. if (!prefix_ci(name, tab_prefix, tab_prefix_len))
  1201. continue;
  1202. if (tab_count == 0) {
  1203. for (j = 0; j < n; j++)
  1204. tab_lcp[j] = name[j];
  1205. tab_lcp_len = n;
  1206. } else {
  1207. j = 0;
  1208. while (j < tab_lcp_len && j < n && tab_lcp[j] == name[j])
  1209. j++;
  1210. tab_lcp_len = j;
  1211. }
  1212. tab_count++;
  1213. }
  1214. return (int)tab_count;
  1215. }
  1216. /* Replace line[start,end) with ins, keep the tail and redraw. */
  1217. static void term_replace(struct term *t, unsigned int start, unsigned int end,
  1218. const char *ins)
  1219. {
  1220. unsigned int inslen = 0, tail, i, old_len = t->line_len;
  1221. while (ins[inslen])
  1222. inslen++;
  1223. tail = old_len - end;
  1224. if (start + inslen + tail >= LINE_MAX)
  1225. return;
  1226. for (i = 0; i < tail; i++)
  1227. t->line[start + inslen + i] = t->line[end + i];
  1228. for (i = 0; i < inslen; i++)
  1229. t->line[start + i] = ins[i];
  1230. t->line_len = start + inslen + tail;
  1231. t->line[t->line_len] = '\0';
  1232. t->line_pos = start + inslen;
  1233. redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start,
  1234. old_len > t->line_len ? old_len : t->line_len);
  1235. }
  1236. /* List the ambiguous matches below the line, then redraw prompt + line. */
  1237. static void term_tab_ambiguous(struct term *t, int mounts)
  1238. {
  1239. unsigned int i;
  1240. io_putchar('\n');
  1241. tab_col = 0;
  1242. tab_width = t->screen.cols;
  1243. if (mounts) {
  1244. for (i = 0; i < vfs_mount_count; i++) {
  1245. const char *name = vfs_mounts[i].name;
  1246. if (prefix_ci(name, tab_prefix, tab_prefix_len))
  1247. tab_print_one(name, 1);
  1248. }
  1249. } else {
  1250. fat_list_dir(tab_fs, tab_dir_cluster, tab_print_cb);
  1251. }
  1252. io_putchar('\n');
  1253. term_print_prompt(t, 1);
  1254. redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start, t->line_len);
  1255. }
  1256. /* Complete the token at the cursor: command name in the first token, a VFS
  1257. * path afterwards. One match completes, several extend to the common
  1258. * prefix or are listed below the line. */
  1259. static void term_complete(struct term *t)
  1260. {
  1261. char token[LINE_MAX + 1];
  1262. char ins[LINE_MAX + 1];
  1263. char dir[VFS_PATH_MAX];
  1264. unsigned int start = t->line_pos, end, tl, i, j;
  1265. while (start > 0 && t->line[start - 1] != ' ')
  1266. start--;
  1267. end = t->line_pos;
  1268. while (end < t->line_len && t->line[end] != ' ')
  1269. end++;
  1270. tl = end - start;
  1271. for (i = 0; i < tl; i++)
  1272. token[i] = t->line[start + i];
  1273. token[tl] = '\0';
  1274. /* First token -> command name. */
  1275. {
  1276. int first = 1;
  1277. for (i = 0; i < start; i++)
  1278. if (t->line[i] != ' ') {
  1279. first = 0;
  1280. break;
  1281. }
  1282. if (first) {
  1283. unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
  1284. unsigned int count = 0, lcp_len = 0, best = 0;
  1285. char lcp[16];
  1286. for (i = 0; i < nitems; i++) {
  1287. unsigned int nl = help_name_len(help_items[i]);
  1288. if (nl < tl || !prefix_ci(help_items[i], token, tl))
  1289. continue;
  1290. if (count == 0) {
  1291. for (j = 0; j < nl; j++)
  1292. lcp[j] = help_items[i][j];
  1293. lcp_len = nl;
  1294. best = i;
  1295. } else {
  1296. for (j = 0; j < lcp_len && j < nl &&
  1297. lcp[j] == help_items[i][j]; j++)
  1298. ;
  1299. lcp_len = j;
  1300. }
  1301. count++;
  1302. }
  1303. if (count == 0)
  1304. return;
  1305. if (count == 1) {
  1306. unsigned int nl = help_name_len(help_items[best]);
  1307. for (j = 0; j < nl; j++)
  1308. ins[j] = help_items[best][j];
  1309. ins[nl] = ' ';
  1310. ins[nl + 1] = '\0';
  1311. term_replace(t, start, end, ins);
  1312. return;
  1313. }
  1314. if (lcp_len > tl) {
  1315. for (j = 0; j < lcp_len; j++)
  1316. ins[j] = lcp[j];
  1317. ins[lcp_len] = '\0';
  1318. term_replace(t, start, end, ins);
  1319. return;
  1320. }
  1321. io_putchar('\n');
  1322. tab_col = 0;
  1323. tab_width = t->screen.cols;
  1324. for (i = 0; i < nitems; i++) {
  1325. unsigned int nl = help_name_len(help_items[i]);
  1326. char name[16];
  1327. if (nl < tl || !prefix_ci(help_items[i], token, tl))
  1328. continue;
  1329. for (j = 0; j < nl && j < 15; j++)
  1330. name[j] = help_items[i][j];
  1331. name[j] = '\0';
  1332. tab_print_one(name, 0);
  1333. }
  1334. io_putchar('\n');
  1335. term_print_prompt(t, 1);
  1336. redraw_line_clear(t->line, t->line_len, t->line_pos,
  1337. t->line_start, t->line_len);
  1338. return;
  1339. }
  1340. }
  1341. /* Argument token -> VFS path. */
  1342. {
  1343. char dirpart[LINE_MAX + 1];
  1344. const char *last = 0, *p;
  1345. unsigned int dp, n;
  1346. int mounts = 0;
  1347. for (p = token; *p; p++)
  1348. if (*p == '/')
  1349. last = p;
  1350. if (last) {
  1351. dp = (unsigned int)(last - token) + 1;
  1352. for (i = 0; i < dp; i++)
  1353. dirpart[i] = token[i];
  1354. dirpart[dp] = '\0';
  1355. p = last + 1;
  1356. } else {
  1357. dp = 0;
  1358. dirpart[0] = '\0';
  1359. p = token;
  1360. }
  1361. {
  1362. unsigned int k = 0;
  1363. while (*p && k < 15)
  1364. tab_prefix[k++] = *p++;
  1365. tab_prefix[k] = '\0';
  1366. tab_prefix_len = k;
  1367. }
  1368. if (dirpart[0] == '/' && dirpart[1] == '\0')
  1369. mounts = 1;
  1370. if (dirpart[0] == '/') {
  1371. if (dp >= VFS_PATH_MAX)
  1372. return;
  1373. for (i = 0; dirpart[i]; i++)
  1374. dir[i] = dirpart[i];
  1375. dir[i] = '\0';
  1376. } else {
  1377. if (vfs_join_path(t->cwd, dirpart, dir, sizeof(dir)) != 0)
  1378. return;
  1379. }
  1380. n = mounts ? (unsigned int)tab_scan_mounts()
  1381. : (unsigned int)tab_scan_dir(dir);
  1382. if (n == 0)
  1383. return;
  1384. if (dp + tab_lcp_len + 2 > LINE_MAX)
  1385. return;
  1386. if (n == 1) {
  1387. for (i = 0; i < dp; i++)
  1388. ins[i] = dirpart[i];
  1389. for (i = 0; i < tab_lcp_len; i++)
  1390. ins[dp + i] = tab_lcp[i];
  1391. ins[dp + tab_lcp_len] = (mounts || tab_single_dir) ? '/' : ' ';
  1392. ins[dp + tab_lcp_len + 1] = '\0';
  1393. term_replace(t, start, end, ins);
  1394. return;
  1395. }
  1396. if (tab_lcp_len > tab_prefix_len) {
  1397. for (i = 0; i < dp; i++)
  1398. ins[i] = dirpart[i];
  1399. for (i = 0; i < tab_lcp_len; i++)
  1400. ins[dp + i] = tab_lcp[i];
  1401. ins[dp + tab_lcp_len] = '\0';
  1402. term_replace(t, start, end, ins);
  1403. return;
  1404. }
  1405. term_tab_ambiguous(t, mounts);
  1406. }
  1407. }
  1408. /* Push current line to history if non-empty (and not duplicate of newest). */
  1409. static void history_push(struct term *t, const char *line, unsigned int line_len)
  1410. {
  1411. unsigned int i, slot;
  1412. int all_spaces = 1;
  1413. if (line_len == 0)
  1414. return;
  1415. for (i = 0; i < line_len; i++) {
  1416. if (line[i] != ' ') {
  1417. all_spaces = 0;
  1418. break;
  1419. }
  1420. }
  1421. if (all_spaces)
  1422. return;
  1423. if (t->history_count > 0) {
  1424. slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  1425. for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
  1426. if (t->history[slot][i] != line[i])
  1427. break;
  1428. }
  1429. if (i == line_len && t->history[slot][i] == '\0')
  1430. return; /* duplicate of newest */
  1431. }
  1432. slot = t->history_next;
  1433. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  1434. t->history[slot][i] = line[i];
  1435. t->history[slot][i] = '\0';
  1436. t->history_next = (t->history_next + 1) % HISTORY_SIZE;
  1437. if (t->history_count < HISTORY_SIZE)
  1438. t->history_count++;
  1439. }
  1440. /* Change directory: resolve `arg` (relative to t->cwd, or absolute) and
  1441. * validate it is an existing directory. Updates t->cwd on success. */
  1442. static void term_cd(struct term *t, const char *arg)
  1443. {
  1444. char abs[VFS_PATH_MAX];
  1445. fat_fs_t *fs;
  1446. const char *fs_path;
  1447. unsigned int dir;
  1448. int r;
  1449. unsigned int i;
  1450. if (arg[0] == '\0') {
  1451. t->cwd[0] = '/';
  1452. t->cwd[1] = '\0';
  1453. return;
  1454. }
  1455. if (vfs_join_path(t->cwd, arg, abs, sizeof(abs)) != 0) {
  1456. io_putstr("cd: path too long\n");
  1457. return;
  1458. }
  1459. if (abs[0] == '/' && abs[1] == '\0') {
  1460. t->cwd[0] = '/';
  1461. t->cwd[1] = '\0';
  1462. return;
  1463. }
  1464. if (vfs_resolve(abs, &fs, &fs_path) != 0) {
  1465. io_putstr("cd: no such directory\n");
  1466. return;
  1467. }
  1468. r = fat_resolve_dir(fs, fs_path, &dir);
  1469. if (r == -2) {
  1470. io_putstr("cd: disk error\n");
  1471. return;
  1472. }
  1473. if (r != 0) {
  1474. io_putstr("cd: no such directory\n");
  1475. return;
  1476. }
  1477. for (i = 0; abs[i] && i < sizeof(t->cwd) - 1; i++)
  1478. t->cwd[i] = abs[i];
  1479. t->cwd[i] = '\0';
  1480. }
  1481. /* Resolve the first token (path) of `args` against t->cwd, rebuilding
  1482. * "abs-path rest-of-args" into out. Used by `save`. */
  1483. static void term_resolve_first(struct term *t, const char *args,
  1484. char *out, unsigned int out_size)
  1485. {
  1486. char path[VFS_PATH_MAX];
  1487. char abs[VFS_PATH_MAX];
  1488. unsigned int i = 0, j = 0;
  1489. while (args[i] == ' ')
  1490. i++;
  1491. while (args[i] && args[i] != ' ' && j < sizeof(path) - 1)
  1492. path[j++] = args[i++];
  1493. path[j] = '\0';
  1494. if (vfs_join_path(t->cwd, path, abs, sizeof(abs)) != 0) {
  1495. abs[0] = '/';
  1496. abs[1] = '\0';
  1497. }
  1498. while (args[i] == ' ')
  1499. i++;
  1500. j = 0;
  1501. {
  1502. unsigned int k = 0;
  1503. while (abs[k] && j + 1 < out_size)
  1504. out[j++] = abs[k++];
  1505. }
  1506. if (args[i]) {
  1507. if (j + 1 < out_size)
  1508. out[j++] = ' ';
  1509. while (args[i] && j + 1 < out_size)
  1510. out[j++] = args[i++];
  1511. }
  1512. out[j] = '\0';
  1513. }
  1514. /* Spawn a foreground command process; the caller (term_run's wait loop)
  1515. * waits for it to finish and handles Ctrl+C. */
  1516. static void term_spawn(struct term *t, const char *name, void (*entry)(void))
  1517. {
  1518. int pid = process_create(name, entry);
  1519. if (pid < 0)
  1520. io_putstr("error: too many processes\n");
  1521. else
  1522. t->fg_pid = pid;
  1523. }
  1524. static void term_spawn_arg(struct term *t, const char *name,
  1525. void (*entry)(const char *), const char *arg)
  1526. {
  1527. int pid = process_create_arg(name, entry, arg);
  1528. if (pid < 0)
  1529. io_putstr("error: too many processes\n");
  1530. else
  1531. t->fg_pid = pid;
  1532. }
  1533. void term_run(struct term *t)
  1534. {
  1535. t->line_len = 0;
  1536. t->line_pos = 0;
  1537. t->line[0] = '\0';
  1538. t->history_browse = 0;
  1539. t->fg_pid = -1;
  1540. io_clear_screen();
  1541. term_print_prompt(t, 1);
  1542. for (;;) {
  1543. if (t->fg_pid >= 0) {
  1544. /* Waiting on a foreground child: poll keys for Ctrl+C
  1545. * and watch for the child to exit. */
  1546. int k = term_key_pop(t);
  1547. if (k == IO_KEY_CTRL_C) {
  1548. int killed = (process_kill((unsigned int)t->fg_pid) == 0);
  1549. t->fg_pid = -1;
  1550. if (killed)
  1551. io_putstr("^C\n");
  1552. term_print_prompt(t, 0);
  1553. } else if (!process_is_alive((unsigned int)t->fg_pid)) {
  1554. t->fg_pid = -1;
  1555. term_print_prompt(t, 0);
  1556. } else {
  1557. process_sleep_ms(10);
  1558. }
  1559. continue;
  1560. }
  1561. {
  1562. int k = term_key_pop(t);
  1563. if (k) {
  1564. kdbg_term_count++;
  1565. kdbg_term_last = (unsigned int)k;
  1566. kdbg_status();
  1567. if (k == IO_KEY_CTRL_C) {
  1568. /* Ctrl+C at the prompt: discard the line. */
  1569. io_putstr("^C\n");
  1570. t->line_len = 0;
  1571. t->line_pos = 0;
  1572. t->line[0] = '\0';
  1573. term_print_prompt(t, 0);
  1574. } else if (k == IO_KEY_LEFT) {
  1575. if (t->line_pos > 0) {
  1576. t->line_pos--;
  1577. io_set_cursor(t->line_start + t->line_pos);
  1578. }
  1579. } else if (k == IO_KEY_RIGHT) {
  1580. if (t->line_pos < t->line_len) {
  1581. t->line_pos++;
  1582. io_set_cursor(t->line_start + t->line_pos);
  1583. }
  1584. } else if (k == IO_KEY_UP) {
  1585. if (t->history_count > 0) {
  1586. if (t->history_browse < t->history_count)
  1587. t->history_browse++;
  1588. history_load_into_line(t, t->history_browse);
  1589. }
  1590. } else if (k == IO_KEY_DOWN) {
  1591. if (t->history_browse > 0) {
  1592. t->history_browse--;
  1593. if (t->history_browse == 0) {
  1594. unsigned int clear_len = t->line_len;
  1595. t->line_len = 0;
  1596. t->line_pos = 0;
  1597. t->line[0] = '\0';
  1598. redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
  1599. } else {
  1600. history_load_into_line(t, t->history_browse);
  1601. }
  1602. }
  1603. } else if (k == '\b') {
  1604. if (t->line_pos > 0) {
  1605. unsigned int i;
  1606. t->line_pos--;
  1607. for (i = t->line_pos; i < t->line_len; i++)
  1608. t->line[i] = t->line[i + 1];
  1609. t->line_len--;
  1610. t->line[t->line_len] = '\0';
  1611. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
  1612. }
  1613. } else if (k == '\t') {
  1614. term_complete(t);
  1615. } else if (k == '\n') {
  1616. char cat_fname[CAT_FNAME_MAX];
  1617. char save_args[SAVE_ARGS_MAX];
  1618. char rm_fname[RM_FNAME_MAX];
  1619. char ls_path[LS_PATH_MAX];
  1620. char format_dev[FORMAT_DEV_MAX];
  1621. char netip_arg[16];
  1622. char ping_arg[64];
  1623. char netlog_arg[NET_UDP_ARG_MAX];
  1624. char netconsole_arg[NET_UDP_ARG_MAX];
  1625. char curl_arg[128];
  1626. char netdns_arg[32];
  1627. char host_arg[64];
  1628. char lspci_arg[16];
  1629. char peek_arg[32];
  1630. char resolved[VFS_PATH_MAX];
  1631. char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
  1632. unsigned int kill_pid;
  1633. int skip_prompt = 0;
  1634. io_putchar('\n');
  1635. history_push(t, t->line, t->line_len);
  1636. t->history_browse = 0;
  1637. if (is_cmd_hello(t->line, t->line_len)) {
  1638. int pid = process_create_user("hello",
  1639. _binary_hello_elf_start,
  1640. (unsigned int)(_binary_hello_elf_end -
  1641. _binary_hello_elf_start));
  1642. if (pid < 0)
  1643. io_putstr("error: too many processes\n");
  1644. else
  1645. t->fg_pid = pid;
  1646. } else if (is_cmd_reboot(t->line, t->line_len)) {
  1647. program_reboot_main();
  1648. } else if (is_cmd_poweroff(t->line, t->line_len)) {
  1649. program_poweroff_main();
  1650. } else if (is_cmd_cd(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  1651. term_cd(t, ls_path);
  1652. } else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  1653. if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
  1654. term_spawn_arg(t, "ls", program_ls_main, resolved);
  1655. } else {
  1656. io_putstr("ls: path too long\n");
  1657. }
  1658. } else if (is_cmd_lsusb(t->line, t->line_len)) {
  1659. term_spawn(t, "lsusb", program_lsusb_main);
  1660. } else if (is_cmd_lspci(t->line, t->line_len, lspci_arg, sizeof(lspci_arg))) {
  1661. term_spawn_arg(t, "lspci", program_lspci_main, lspci_arg);
  1662. } else if (is_cmd_peek(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
  1663. term_spawn_arg(t, "peek", program_peek_main, peek_arg);
  1664. } else if (is_cmd_poke(t->line, t->line_len, peek_arg, sizeof(peek_arg))) {
  1665. term_spawn_arg(t, "poke", program_poke_main, peek_arg);
  1666. } else if (is_cmd_netinfo(t->line, t->line_len)) {
  1667. term_spawn(t, "netinfo", program_netinfo_main);
  1668. } else if (is_cmd_netrecv(t->line, t->line_len)) {
  1669. term_spawn(t, "netrecv", program_netrecv_main);
  1670. } else if (is_cmd_netsend(t->line, t->line_len)) {
  1671. term_spawn(t, "netsend", program_netsend_main);
  1672. } else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
  1673. term_spawn_arg(t, "netip", program_netip_main, netip_arg);
  1674. } else if (is_cmd_ping(t->line, t->line_len, ping_arg, sizeof(ping_arg))) {
  1675. term_spawn_arg(t, "ping", program_ping_main, ping_arg);
  1676. } else if (is_cmd_netlog(t->line, t->line_len, netlog_arg, sizeof(netlog_arg))) {
  1677. term_spawn_arg(t, "netlog", program_netlog_main, netlog_arg);
  1678. } else if (is_cmd_netconsole(t->line, t->line_len, netconsole_arg, sizeof(netconsole_arg))) {
  1679. term_spawn_arg(t, "netconsole", program_netconsole_main, netconsole_arg);
  1680. } else if (is_cmd_dhcp(t->line, t->line_len)) {
  1681. term_spawn(t, "dhcp", program_dhcp_main);
  1682. } else if (is_cmd_netregs(t->line, t->line_len)) {
  1683. term_spawn(t, "netregs", program_netregs_main);
  1684. } else if (is_cmd_netloop(t->line, t->line_len)) {
  1685. term_spawn(t, "netloop", program_netloop_main);
  1686. } else if (is_cmd_netreset(t->line, t->line_len)) {
  1687. term_spawn(t, "netreset", program_netreset_main);
  1688. } else if (is_cmd_netdns(t->line, t->line_len, netdns_arg, sizeof(netdns_arg))) {
  1689. term_spawn_arg(t, "netdns", program_netdns_main, netdns_arg);
  1690. } else if (is_cmd_curl(t->line, t->line_len, curl_arg, sizeof(curl_arg))) {
  1691. term_spawn_arg(t, "curl", program_curl_main, curl_arg);
  1692. } else if (is_cmd_host(t->line, t->line_len, host_arg, sizeof(host_arg))) {
  1693. term_spawn_arg(t, "host", program_host_main, host_arg);
  1694. } else if (is_cmd_pong(t->line, t->line_len)) {
  1695. term_spawn(t, "pong", program_pong_main);
  1696. } else if (is_cmd_storage(t->line, t->line_len)) {
  1697. term_spawn(t, "storage", program_storage_main);
  1698. } else if (is_cmd_meminfo(t->line, t->line_len)) {
  1699. term_spawn(t, "meminfo", program_mem_main);
  1700. } else if (is_cmd_clock(t->line, t->line_len)) {
  1701. io_putstr("clock: not available in windowed mode yet\n");
  1702. } else if (is_cmd_colors(t->line, t->line_len)) {
  1703. io_putstr("colors: not available in windowed mode yet\n");
  1704. } else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  1705. if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
  1706. term_spawn_arg(t, "cat", program_cat_main, resolved);
  1707. } else {
  1708. io_putstr("cat: path too long\n");
  1709. }
  1710. } else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  1711. if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
  1712. cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
  1713. cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
  1714. cat_fname[6] == '\0') {
  1715. if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
  1716. io_putstr("error: too many processes\n");
  1717. } else {
  1718. io_putstr("run: not available in windowed mode yet\n");
  1719. }
  1720. } else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
  1721. term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
  1722. term_spawn_arg(t, "save", program_save_main, save_resolved);
  1723. } else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1724. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1725. term_spawn_arg(t, "mkdir", program_mkdir_main, resolved);
  1726. } else {
  1727. io_putstr("mkdir: path too long\n");
  1728. }
  1729. } else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1730. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1731. term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
  1732. } else {
  1733. io_putstr("rmdir: path too long\n");
  1734. }
  1735. } else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  1736. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  1737. term_spawn_arg(t, "rm", program_rm_main, resolved);
  1738. } else {
  1739. io_putstr("rm: path too long\n");
  1740. }
  1741. } else if (is_cmd_space(t->line, t->line_len)) {
  1742. term_spawn_arg(t, "space", program_run_embedded_main, "space");
  1743. } else if (is_cmd_prog(t->line, t->line_len)) {
  1744. term_spawn_arg(t, "prog", program_run_embedded_main, "prog");
  1745. } else if (is_cmd_display(t->line, t->line_len)) {
  1746. term_spawn(t, "display", program_display_main);
  1747. } else if (is_cmd_cpuinfo(t->line, t->line_len)) {
  1748. term_spawn(t, "cpuinfo", program_cpuinfo_main);
  1749. } else if (is_cmd_mouseinfo(t->line, t->line_len)) {
  1750. term_spawn(t, "mouseinfo", program_mouseinfo_main);
  1751. } else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
  1752. program_format_main(format_dev);
  1753. } else if (is_cmd_ps(t->line, t->line_len)) {
  1754. process_ps();
  1755. } else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
  1756. if (process_kill(kill_pid) < 0)
  1757. io_putstr("kill: no such process\n");
  1758. else
  1759. io_putstr("killed\n");
  1760. } else if (is_cmd_kexec(t->line, t->line_len)) {
  1761. if (kexec_busy) {
  1762. io_putstr("kexec: already receiving\n");
  1763. } else {
  1764. kexec_busy = 1;
  1765. if (process_create("kexec", program_kexec_main) < 0) {
  1766. kexec_busy = 0;
  1767. io_putstr("error: too many processes\n");
  1768. }
  1769. }
  1770. } else if (is_cmd_dbg(t->line, t->line_len)) {
  1771. program_dbg_main();
  1772. } else if (is_cmd_help(t->line, t->line_len)) {
  1773. help_print();
  1774. } else if (is_cmd_clear(t->line, t->line_len)) {
  1775. io_clear_screen();
  1776. term_print_prompt(t, 1);
  1777. skip_prompt = 1;
  1778. } else {
  1779. char serial_arg[8];
  1780. if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
  1781. if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
  1782. serial_arg[2] == 's' && serial_arg[3] == 't' &&
  1783. serial_arg[4] == '\0') {
  1784. if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
  1785. io_putstr("error: too many processes\n");
  1786. } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
  1787. serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
  1788. serial_arg[4] == '\0') {
  1789. program_serial_recv_main(serial_arg);
  1790. } else {
  1791. io_putstr("serial: usage serial test|recv\n");
  1792. }
  1793. } else {
  1794. unsigned int i = 0;
  1795. while (i < t->line_len && t->line[i] == ' ')
  1796. i++;
  1797. if (i < t->line_len)
  1798. io_putstr("command not found\n");
  1799. }
  1800. }
  1801. t->line_len = 0;
  1802. t->line_pos = 0;
  1803. t->line[0] = '\0';
  1804. if (!skip_prompt && t->fg_pid < 0) {
  1805. term_print_prompt(t, 0);
  1806. }
  1807. } else {
  1808. unsigned char u = (unsigned char)k;
  1809. if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
  1810. unsigned int i;
  1811. t->history_browse = 0;
  1812. for (i = t->line_len; i > t->line_pos; i--)
  1813. t->line[i] = t->line[i - 1];
  1814. t->line[t->line_pos] = (char)k;
  1815. t->line_len++;
  1816. t->line[t->line_len] = '\0';
  1817. t->line_pos++;
  1818. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
  1819. }
  1820. }
  1821. } else {
  1822. process_sleep_ms(10);
  1823. }
  1824. }
  1825. }
  1826. }
  1827. void term_entry(void *arg)
  1828. {
  1829. struct term *t = (struct term *)arg;
  1830. process_set_screen(&t->screen);
  1831. term_run(t);
  1832. }