terminal.c 40 KB

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