terminal.c 49 KB

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