terminal.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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 "kexec.h"
  6. #include "process.h"
  7. #include "terminal.h"
  8. #include "timer.h"
  9. extern unsigned char _binary_hello_elf_start[];
  10. extern unsigned char _binary_hello_elf_end[];
  11. extern void program_reboot_main(void);
  12. extern void program_poweroff_main(void);
  13. extern void program_ls_main(const char *path);
  14. extern void program_cat_main(const char *filename);
  15. extern void program_run_main(const char *filename);
  16. extern void program_save_main(const char *arg);
  17. extern void program_rm_main(const char *arg);
  18. extern void program_mkdir_main(const char *arg);
  19. extern void program_rmdir_main(const char *arg);
  20. extern void program_clock_main(void);
  21. extern void program_colors_main(void);
  22. extern void program_display_main(void);
  23. extern void program_cpuinfo_main(void);
  24. extern void program_mouseinfo_main(void);
  25. extern void program_serial_loop_main(const char *arg);
  26. extern void program_serial_recv_main(const char *arg);
  27. extern void program_run_serial_main(const char *arg);
  28. extern void program_lsusb_main(void);
  29. extern void program_storage_main(void);
  30. extern void program_mem_main(void);
  31. extern void program_format_main(const char *arg);
  32. extern void program_lspci_main(void);
  33. extern void program_netinfo_main(void);
  34. extern void program_netrecv_main(void);
  35. extern void program_netsend_main(void);
  36. extern void program_netip_main(const char *arg);
  37. extern void program_ping_main(void);
  38. /* Print the prompt (with the current directory) and set line_start. */
  39. static void term_print_prompt(struct term *t, int first)
  40. {
  41. if (!first)
  42. io_putchar('\n');
  43. io_putchar('[');
  44. io_putstr(t->cwd);
  45. io_putstr("] > ");
  46. t->line_start = io_get_cursor();
  47. }
  48. static int is_cmd_hello(const char *line, unsigned int line_len)
  49. {
  50. unsigned int i = 0;
  51. while (i < line_len && line[i] == ' ')
  52. i++;
  53. if (i + 5 > line_len)
  54. return 0;
  55. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  56. return 0;
  57. i += 5;
  58. while (i < line_len && line[i] == ' ')
  59. i++;
  60. return (i == line_len);
  61. }
  62. static int is_cmd_reboot(const char *line, unsigned int line_len)
  63. {
  64. unsigned int i = 0;
  65. while (i < line_len && line[i] == ' ')
  66. i++;
  67. if (i + 6 > line_len)
  68. return 0;
  69. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  70. return 0;
  71. i += 6;
  72. while (i < line_len && line[i] == ' ')
  73. i++;
  74. return (i == line_len);
  75. }
  76. static int is_cmd_poweroff(const char *line, unsigned int line_len)
  77. {
  78. unsigned int i = 0;
  79. while (i < line_len && line[i] == ' ')
  80. i++;
  81. if (i + 8 > line_len)
  82. return 0;
  83. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' ||
  84. line[i+4] != 'r' || line[i+5] != 'o' || line[i+6] != 'f' || line[i+7] != 'f')
  85. return 0;
  86. i += 8;
  87. while (i < line_len && line[i] == ' ')
  88. i++;
  89. return (i == line_len);
  90. }
  91. static int is_cmd_ls(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  92. {
  93. unsigned int i = 0, j = 0;
  94. while (i < line_len && line[i] == ' ')
  95. i++;
  96. if (i + 2 > line_len)
  97. return 0;
  98. if (line[i] != 'l' || line[i+1] != 's')
  99. return 0;
  100. i += 2;
  101. if (i < line_len && line[i] != ' ')
  102. return 0; /* "ls" must be a whole word, so reject "lsusb" etc. */
  103. while (i < line_len && line[i] == ' ')
  104. i++;
  105. if (i >= line_len) {
  106. if (buf_size > 0)
  107. path_buf[0] = '\0';
  108. return 1;
  109. }
  110. while (i < line_len && j + 1 < buf_size)
  111. path_buf[j++] = line[i++];
  112. path_buf[j] = '\0';
  113. while (i < line_len && line[i] == ' ')
  114. i++;
  115. return (i == line_len);
  116. }
  117. static int is_cmd_cd(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  118. {
  119. unsigned int i = 0, j = 0;
  120. while (i < line_len && line[i] == ' ')
  121. i++;
  122. if (i + 2 > line_len)
  123. return 0;
  124. if (line[i] != 'c' || line[i+1] != 'd')
  125. return 0;
  126. i += 2;
  127. if (i < line_len && line[i] != ' ')
  128. return 0; /* "cd" must be a whole word */
  129. while (i < line_len && line[i] == ' ')
  130. i++;
  131. if (i >= line_len) {
  132. if (buf_size > 0)
  133. path_buf[0] = '\0';
  134. return 1;
  135. }
  136. while (i < line_len && j + 1 < buf_size)
  137. path_buf[j++] = line[i++];
  138. path_buf[j] = '\0';
  139. while (i < line_len && line[i] == ' ')
  140. i++;
  141. return (i == line_len);
  142. }
  143. static int is_cmd_clock(const char *line, unsigned int line_len)
  144. {
  145. unsigned int i = 0;
  146. while (i < line_len && line[i] == ' ')
  147. i++;
  148. if (i + 5 > line_len)
  149. return 0;
  150. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  151. line[i+3] != 'c' || line[i+4] != 'k')
  152. return 0;
  153. i += 5;
  154. while (i < line_len && line[i] == ' ')
  155. i++;
  156. return (i == line_len);
  157. }
  158. static int is_cmd_colors(const char *line, unsigned int line_len)
  159. {
  160. unsigned int i = 0;
  161. while (i < line_len && line[i] == ' ')
  162. i++;
  163. if (i + 6 > line_len)
  164. return 0;
  165. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  166. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  167. return 0;
  168. i += 6;
  169. while (i < line_len && line[i] == ' ')
  170. i++;
  171. return (i == line_len);
  172. }
  173. static int is_cmd_display(const char *line, unsigned int line_len)
  174. {
  175. unsigned int i = 0;
  176. while (i < line_len && line[i] == ' ')
  177. i++;
  178. if (i + 7 > line_len)
  179. return 0;
  180. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  181. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  182. return 0;
  183. i += 7;
  184. while (i < line_len && line[i] == ' ')
  185. i++;
  186. return (i == line_len);
  187. }
  188. static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
  189. {
  190. unsigned int i = 0;
  191. while (i < line_len && line[i] == ' ')
  192. i++;
  193. if (i + 7 > line_len)
  194. return 0;
  195. if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
  196. line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
  197. return 0;
  198. i += 7;
  199. while (i < line_len && line[i] == ' ')
  200. i++;
  201. return (i == line_len);
  202. }
  203. static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
  204. {
  205. unsigned int i = 0;
  206. while (i < line_len && line[i] == ' ')
  207. i++;
  208. if (i + 9 > line_len)
  209. return 0;
  210. if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
  211. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
  212. line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
  213. return 0;
  214. i += 9;
  215. while (i < line_len && line[i] == ' ')
  216. i++;
  217. return (i == line_len);
  218. }
  219. static int is_cmd_lsusb(const char *line, unsigned int line_len)
  220. {
  221. unsigned int i = 0;
  222. while (i < line_len && line[i] == ' ')
  223. i++;
  224. if (i + 5 > line_len)
  225. return 0;
  226. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'u' ||
  227. line[i+3] != 's' || line[i+4] != 'b')
  228. return 0;
  229. i += 5;
  230. while (i < line_len && line[i] == ' ')
  231. i++;
  232. return (i == line_len);
  233. }
  234. static int is_cmd_lspci(const char *line, unsigned int line_len)
  235. {
  236. unsigned int i = 0;
  237. while (i < line_len && line[i] == ' ')
  238. i++;
  239. if (i + 5 > line_len)
  240. return 0;
  241. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'p' ||
  242. line[i+3] != 'c' || line[i+4] != 'i')
  243. return 0;
  244. i += 5;
  245. while (i < line_len && line[i] == ' ')
  246. i++;
  247. return (i == line_len);
  248. }
  249. static int is_cmd_netinfo(const char *line, unsigned int line_len)
  250. {
  251. unsigned int i = 0;
  252. while (i < line_len && line[i] == ' ')
  253. i++;
  254. if (i + 7 > line_len)
  255. return 0;
  256. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  257. line[i+3] != 'i' || line[i+4] != 'n' || line[i+5] != 'f' ||
  258. line[i+6] != 'o')
  259. return 0;
  260. i += 7;
  261. while (i < line_len && line[i] == ' ')
  262. i++;
  263. return (i == line_len);
  264. }
  265. static int is_cmd_netrecv(const char *line, unsigned int line_len)
  266. {
  267. unsigned int i = 0;
  268. while (i < line_len && line[i] == ' ')
  269. i++;
  270. if (i + 7 > line_len)
  271. return 0;
  272. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  273. line[i+3] != 'r' || line[i+4] != 'e' || line[i+5] != 'c' ||
  274. line[i+6] != 'v')
  275. return 0;
  276. i += 7;
  277. while (i < line_len && line[i] == ' ')
  278. i++;
  279. return (i == line_len);
  280. }
  281. static int is_cmd_netsend(const char *line, unsigned int line_len)
  282. {
  283. unsigned int i = 0;
  284. while (i < line_len && line[i] == ' ')
  285. i++;
  286. if (i + 7 > line_len)
  287. return 0;
  288. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  289. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'n' ||
  290. line[i+6] != 'd')
  291. return 0;
  292. i += 7;
  293. while (i < line_len && line[i] == ' ')
  294. i++;
  295. return (i == line_len);
  296. }
  297. static int is_cmd_netip(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  298. {
  299. unsigned int i = 0, j = 0;
  300. while (i < line_len && line[i] == ' ')
  301. i++;
  302. if (i + 5 > line_len)
  303. return 0;
  304. if (line[i] != 'n' || line[i+1] != 'e' || line[i+2] != 't' ||
  305. line[i+3] != 'i' || line[i+4] != 'p')
  306. return 0;
  307. i += 5;
  308. if (i < line_len && line[i] != ' ')
  309. return 0; /* "netip" must be a whole word */
  310. while (i < line_len && line[i] == ' ')
  311. i++;
  312. if (i >= line_len) {
  313. if (buf_size > 0)
  314. arg_buf[0] = '\0';
  315. return 1;
  316. }
  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_ping(const char *line, unsigned int line_len)
  325. {
  326. unsigned int i = 0;
  327. while (i < line_len && line[i] == ' ')
  328. i++;
  329. if (i + 4 > line_len)
  330. return 0;
  331. if (line[i] != 'p' || line[i+1] != 'i' || line[i+2] != 'n' || 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_space(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 + 5 > line_len)
  344. return 0;
  345. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  346. line[i+3] != 'c' || line[i+4] != 'e')
  347. return 0;
  348. i += 5;
  349. while (i < line_len && line[i] == ' ')
  350. i++;
  351. return (i == line_len);
  352. }
  353. #define CAT_FNAME_MAX 64
  354. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  355. {
  356. unsigned int i = 0, j;
  357. while (i < line_len && line[i] == ' ')
  358. i++;
  359. if (i + 3 > line_len)
  360. return 0;
  361. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  362. return 0;
  363. i += 3;
  364. while (i < line_len && line[i] == ' ')
  365. i++;
  366. if (i >= line_len)
  367. return 0;
  368. j = 0;
  369. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  370. filename_buf[j++] = line[i++];
  371. filename_buf[j] = '\0';
  372. return (j > 0);
  373. }
  374. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  375. {
  376. unsigned int i = 0, j;
  377. while (i < line_len && line[i] == ' ')
  378. i++;
  379. if (i + 3 > line_len)
  380. return 0;
  381. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  382. return 0;
  383. i += 3;
  384. while (i < line_len && line[i] == ' ')
  385. i++;
  386. if (i >= line_len)
  387. return 0;
  388. j = 0;
  389. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  390. filename_buf[j++] = line[i++];
  391. filename_buf[j] = '\0';
  392. return (j > 0);
  393. }
  394. #define SAVE_ARGS_MAX 128
  395. #define RM_FNAME_MAX 64
  396. #define LS_PATH_MAX 64
  397. #define FORMAT_DEV_MAX 16
  398. static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  399. {
  400. unsigned int i = 0, j = 0;
  401. while (i < line_len && line[i] == ' ')
  402. i++;
  403. if (i + 4 > line_len)
  404. return 0;
  405. if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
  406. return 0;
  407. i += 4;
  408. while (i < line_len && line[i] == ' ')
  409. i++;
  410. if (i >= line_len)
  411. return 0;
  412. while (i < line_len && j + 1 < buf_size)
  413. arg_buf[j++] = line[i++];
  414. while (j > 0 && arg_buf[j - 1] == ' ')
  415. j--;
  416. arg_buf[j] = '\0';
  417. while (i < line_len && line[i] == ' ')
  418. i++;
  419. return (i == line_len && j > 0);
  420. }
  421. static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  422. {
  423. unsigned int i = 0, j = 0;
  424. while (i < line_len && line[i] == ' ')
  425. i++;
  426. if (i + 2 > line_len)
  427. return 0;
  428. if (line[i] != 'r' || line[i+1] != 'm')
  429. return 0;
  430. i += 2;
  431. while (i < line_len && line[i] == ' ')
  432. i++;
  433. if (i >= line_len)
  434. return 0;
  435. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  436. filename_buf[j++] = line[i++];
  437. filename_buf[j] = '\0';
  438. return (j > 0);
  439. }
  440. static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  441. {
  442. unsigned int i = 0, j = 0;
  443. while (i < line_len && line[i] == ' ')
  444. i++;
  445. if (i + 5 > line_len)
  446. return 0;
  447. if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  448. return 0;
  449. i += 5;
  450. while (i < line_len && line[i] == ' ')
  451. i++;
  452. if (i >= line_len)
  453. return 0;
  454. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  455. path_buf[j++] = line[i++];
  456. path_buf[j] = '\0';
  457. return (j > 0);
  458. }
  459. static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  460. {
  461. unsigned int i = 0, j = 0;
  462. while (i < line_len && line[i] == ' ')
  463. i++;
  464. if (i + 5 > line_len)
  465. return 0;
  466. if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  467. return 0;
  468. i += 5;
  469. while (i < line_len && line[i] == ' ')
  470. i++;
  471. if (i >= line_len)
  472. return 0;
  473. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  474. path_buf[j++] = line[i++];
  475. path_buf[j] = '\0';
  476. return (j > 0);
  477. }
  478. static int is_cmd_storage(const char *line, unsigned int line_len)
  479. {
  480. unsigned int i = 0;
  481. while (i < line_len && line[i] == ' ')
  482. i++;
  483. if (i + 7 > line_len)
  484. return 0;
  485. if (line[i] != 's' || line[i+1] != 't' || line[i+2] != 'o' ||
  486. line[i+3] != 'r' || line[i+4] != 'a' || line[i+5] != 'g' || line[i+6] != 'e')
  487. return 0;
  488. i += 7;
  489. while (i < line_len && line[i] == ' ')
  490. i++;
  491. return (i == line_len);
  492. }
  493. static int is_cmd_mem(const char *line, unsigned int line_len)
  494. {
  495. unsigned int i = 0;
  496. while (i < line_len && line[i] == ' ')
  497. i++;
  498. if (i + 3 > line_len)
  499. return 0;
  500. if (line[i] != 'm' || line[i+1] != 'e' || line[i+2] != 'm')
  501. return 0;
  502. i += 3;
  503. while (i < line_len && line[i] == ' ')
  504. i++;
  505. return (i == line_len);
  506. }
  507. static int is_cmd_format(const char *line, unsigned int line_len, char *dev_buf, unsigned int buf_size)
  508. {
  509. unsigned int i = 0, j;
  510. while (i < line_len && line[i] == ' ')
  511. i++;
  512. if (i + 6 > line_len)
  513. return 0;
  514. if (line[i] != 'f' || line[i+1] != 'o' || line[i+2] != 'r' ||
  515. line[i+3] != 'm' || line[i+4] != 'a' || line[i+5] != 't')
  516. return 0;
  517. i += 6;
  518. if (i < line_len && line[i] != ' ')
  519. return 0; /* "format" must be a whole word */
  520. while (i < line_len && line[i] == ' ')
  521. i++;
  522. if (i >= line_len)
  523. return 0;
  524. j = 0;
  525. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  526. dev_buf[j++] = line[i++];
  527. dev_buf[j] = '\0';
  528. while (i < line_len && line[i] == ' ')
  529. i++;
  530. return (i == line_len && j > 0);
  531. }
  532. static int is_cmd_help(const char *line, unsigned int line_len)
  533. {
  534. unsigned int i = 0;
  535. while (i < line_len && line[i] == ' ')
  536. i++;
  537. if (i + 4 > line_len)
  538. return 0;
  539. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  540. return 0;
  541. i += 4;
  542. while (i < line_len && line[i] == ' ')
  543. i++;
  544. return (i == line_len);
  545. }
  546. static int is_cmd_clear(const char *line, unsigned int line_len)
  547. {
  548. unsigned int i = 0;
  549. while (i < line_len && line[i] == ' ')
  550. i++;
  551. if (i + 5 > line_len)
  552. return 0;
  553. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  554. line[i+3] != 'a' || line[i+4] != 'r')
  555. return 0;
  556. i += 5;
  557. while (i < line_len && line[i] == ' ')
  558. i++;
  559. return (i == line_len);
  560. }
  561. static int is_cmd_ps(const char *line, unsigned int line_len)
  562. {
  563. unsigned int i = 0;
  564. while (i < line_len && line[i] == ' ')
  565. i++;
  566. if (i + 2 > line_len)
  567. return 0;
  568. if (line[i] != 'p' || line[i+1] != 's')
  569. return 0;
  570. i += 2;
  571. while (i < line_len && line[i] == ' ')
  572. i++;
  573. return (i == line_len);
  574. }
  575. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  576. {
  577. unsigned int i = 0, num = 0;
  578. int has_digit = 0;
  579. while (i < line_len && line[i] == ' ')
  580. i++;
  581. if (i + 4 > line_len)
  582. return 0;
  583. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  584. return 0;
  585. i += 4;
  586. while (i < line_len && line[i] == ' ')
  587. i++;
  588. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  589. num = num * 10 + (unsigned int)(line[i] - '0');
  590. has_digit = 1;
  591. i++;
  592. }
  593. if (!has_digit)
  594. return 0;
  595. while (i < line_len && line[i] == ' ')
  596. i++;
  597. if (i != line_len)
  598. return 0;
  599. *pid_out = num;
  600. return 1;
  601. }
  602. static int is_cmd_kexec(const char *line, unsigned int line_len)
  603. {
  604. unsigned int i = 0;
  605. while (i < line_len && line[i] == ' ')
  606. i++;
  607. if (i + 5 > line_len)
  608. return 0;
  609. if (line[i] != 'k' || line[i+1] != 'e' || line[i+2] != 'x' || line[i+3] != 'e' || line[i+4] != 'c')
  610. return 0;
  611. i += 5;
  612. while (i < line_len && line[i] == ' ')
  613. i++;
  614. return (i == line_len);
  615. }
  616. static int is_cmd_dbg(const char *line, unsigned int line_len)
  617. {
  618. unsigned int i = 0;
  619. while (i < line_len && line[i] == ' ')
  620. i++;
  621. if (i + 3 > line_len)
  622. return 0;
  623. if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
  624. return 0;
  625. i += 3;
  626. while (i < line_len && line[i] == ' ')
  627. i++;
  628. return (i == line_len);
  629. }
  630. static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  631. {
  632. unsigned int i = 0, j = 0;
  633. while (i < line_len && line[i] == ' ')
  634. i++;
  635. if (i + 6 > line_len)
  636. return 0;
  637. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
  638. line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
  639. return 0;
  640. i += 6;
  641. while (i < line_len && line[i] == ' ')
  642. i++;
  643. if (i >= line_len) {
  644. if (buf_size > 0)
  645. arg_buf[0] = '\0';
  646. return 1;
  647. }
  648. while (i < line_len && j + 1 < buf_size)
  649. arg_buf[j++] = line[i++];
  650. while (j > 0 && arg_buf[j - 1] == ' ')
  651. j--;
  652. arg_buf[j] = '\0';
  653. while (i < line_len && line[i] == ' ')
  654. i++;
  655. return (i == line_len);
  656. }
  657. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  658. static void redraw_line_from(const char *line, unsigned int line_len,
  659. unsigned int line_pos, unsigned int line_start, unsigned int from)
  660. {
  661. unsigned int i;
  662. io_set_cursor(line_start + from);
  663. for (i = from; i < line_len; i++)
  664. io_putchar(line[i]);
  665. io_putchar(' ');
  666. io_set_cursor(line_start + line_pos);
  667. }
  668. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  669. static void redraw_line_clear(const char *line, unsigned int line_len,
  670. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  671. {
  672. unsigned int i;
  673. io_set_cursor(line_start);
  674. for (i = 0; i < line_len; i++)
  675. io_putchar(line[i]);
  676. for (i = line_len; i <= clear_through; i++)
  677. io_putchar(' ');
  678. io_set_cursor(line_start + line_pos);
  679. }
  680. /* Copy history entry (1 = newest) into t->line, redraw from start. */
  681. static void history_load_into_line(struct term *t, unsigned int browse)
  682. {
  683. unsigned int slot, len, old_len;
  684. const char *src;
  685. if (browse < 1 || browse > t->history_count)
  686. return;
  687. old_len = t->line_len;
  688. slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  689. src = t->history[slot];
  690. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  691. t->line[len] = src[len];
  692. t->line[len] = '\0';
  693. t->line_len = len;
  694. t->line_pos = len;
  695. redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
  696. }
  697. /* Push current line to history if non-empty (and not duplicate of newest). */
  698. static void history_push(struct term *t, const char *line, unsigned int line_len)
  699. {
  700. unsigned int i, slot;
  701. int all_spaces = 1;
  702. if (line_len == 0)
  703. return;
  704. for (i = 0; i < line_len; i++) {
  705. if (line[i] != ' ') {
  706. all_spaces = 0;
  707. break;
  708. }
  709. }
  710. if (all_spaces)
  711. return;
  712. if (t->history_count > 0) {
  713. slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  714. for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
  715. if (t->history[slot][i] != line[i])
  716. break;
  717. }
  718. if (i == line_len && t->history[slot][i] == '\0')
  719. return; /* duplicate of newest */
  720. }
  721. slot = t->history_next;
  722. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  723. t->history[slot][i] = line[i];
  724. t->history[slot][i] = '\0';
  725. t->history_next = (t->history_next + 1) % HISTORY_SIZE;
  726. if (t->history_count < HISTORY_SIZE)
  727. t->history_count++;
  728. }
  729. /* Change directory: resolve `arg` (relative to t->cwd, or absolute) and
  730. * validate it is an existing directory. Updates t->cwd on success. */
  731. static void term_cd(struct term *t, const char *arg)
  732. {
  733. char abs[VFS_PATH_MAX];
  734. fat_fs_t *fs;
  735. const char *fs_path;
  736. unsigned int dir;
  737. int r;
  738. unsigned int i;
  739. if (arg[0] == '\0') {
  740. t->cwd[0] = '/';
  741. t->cwd[1] = '\0';
  742. return;
  743. }
  744. if (vfs_join_path(t->cwd, arg, abs, sizeof(abs)) != 0) {
  745. io_putstr("cd: path too long\n");
  746. return;
  747. }
  748. if (abs[0] == '/' && abs[1] == '\0') {
  749. t->cwd[0] = '/';
  750. t->cwd[1] = '\0';
  751. return;
  752. }
  753. if (vfs_resolve(abs, &fs, &fs_path) != 0) {
  754. io_putstr("cd: no such directory\n");
  755. return;
  756. }
  757. r = fat_resolve_dir(fs, fs_path, &dir);
  758. if (r == -2) {
  759. io_putstr("cd: disk error\n");
  760. return;
  761. }
  762. if (r != 0) {
  763. io_putstr("cd: no such directory\n");
  764. return;
  765. }
  766. for (i = 0; abs[i] && i < sizeof(t->cwd) - 1; i++)
  767. t->cwd[i] = abs[i];
  768. t->cwd[i] = '\0';
  769. }
  770. /* Resolve the first token (path) of `args` against t->cwd, rebuilding
  771. * "abs-path rest-of-args" into out. Used by `save`. */
  772. static void term_resolve_first(struct term *t, const char *args,
  773. char *out, unsigned int out_size)
  774. {
  775. char path[VFS_PATH_MAX];
  776. char abs[VFS_PATH_MAX];
  777. unsigned int i = 0, j = 0;
  778. while (args[i] == ' ')
  779. i++;
  780. while (args[i] && args[i] != ' ' && j < sizeof(path) - 1)
  781. path[j++] = args[i++];
  782. path[j] = '\0';
  783. if (vfs_join_path(t->cwd, path, abs, sizeof(abs)) != 0) {
  784. abs[0] = '/';
  785. abs[1] = '\0';
  786. }
  787. while (args[i] == ' ')
  788. i++;
  789. j = 0;
  790. {
  791. unsigned int k = 0;
  792. while (abs[k] && j + 1 < out_size)
  793. out[j++] = abs[k++];
  794. }
  795. if (args[i]) {
  796. if (j + 1 < out_size)
  797. out[j++] = ' ';
  798. while (args[i] && j + 1 < out_size)
  799. out[j++] = args[i++];
  800. }
  801. out[j] = '\0';
  802. }
  803. /* Spawn a foreground command process; the caller (term_run's wait loop)
  804. * waits for it to finish and handles Ctrl+C. */
  805. static void term_spawn(struct term *t, const char *name, void (*entry)(void))
  806. {
  807. int pid = process_create(name, entry);
  808. if (pid < 0)
  809. io_putstr("error: too many processes\n");
  810. else
  811. t->fg_pid = pid;
  812. }
  813. static void term_spawn_arg(struct term *t, const char *name,
  814. void (*entry)(const char *), const char *arg)
  815. {
  816. int pid = process_create_arg(name, entry, arg);
  817. if (pid < 0)
  818. io_putstr("error: too many processes\n");
  819. else
  820. t->fg_pid = pid;
  821. }
  822. void term_run(struct term *t)
  823. {
  824. t->line_len = 0;
  825. t->line_pos = 0;
  826. t->line[0] = '\0';
  827. t->history_browse = 0;
  828. t->fg_pid = -1;
  829. io_clear_screen();
  830. term_print_prompt(t, 1);
  831. for (;;) {
  832. if (t->fg_pid >= 0) {
  833. /* Waiting on a foreground child: poll keys for Ctrl+C
  834. * and watch for the child to exit. */
  835. int k = term_key_pop(t);
  836. if (k == IO_KEY_CTRL_C) {
  837. int killed = (process_kill((unsigned int)t->fg_pid) == 0);
  838. t->fg_pid = -1;
  839. if (killed)
  840. io_putstr("^C\n");
  841. term_print_prompt(t, 0);
  842. } else if (!process_is_alive((unsigned int)t->fg_pid)) {
  843. t->fg_pid = -1;
  844. term_print_prompt(t, 0);
  845. } else {
  846. process_sleep_ms(10);
  847. }
  848. continue;
  849. }
  850. {
  851. int k = term_key_pop(t);
  852. if (k) {
  853. if (k == IO_KEY_CTRL_C) {
  854. /* Ctrl+C at the prompt: discard the line. */
  855. io_putstr("^C\n");
  856. t->line_len = 0;
  857. t->line_pos = 0;
  858. t->line[0] = '\0';
  859. term_print_prompt(t, 0);
  860. } else if (k == IO_KEY_LEFT) {
  861. if (t->line_pos > 0) {
  862. t->line_pos--;
  863. io_set_cursor(t->line_start + t->line_pos);
  864. }
  865. } else if (k == IO_KEY_RIGHT) {
  866. if (t->line_pos < t->line_len) {
  867. t->line_pos++;
  868. io_set_cursor(t->line_start + t->line_pos);
  869. }
  870. } else if (k == IO_KEY_UP) {
  871. if (t->history_count > 0) {
  872. if (t->history_browse < t->history_count)
  873. t->history_browse++;
  874. history_load_into_line(t, t->history_browse);
  875. }
  876. } else if (k == IO_KEY_DOWN) {
  877. if (t->history_browse > 0) {
  878. t->history_browse--;
  879. if (t->history_browse == 0) {
  880. unsigned int clear_len = t->line_len;
  881. t->line_len = 0;
  882. t->line_pos = 0;
  883. t->line[0] = '\0';
  884. redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
  885. } else {
  886. history_load_into_line(t, t->history_browse);
  887. }
  888. }
  889. } else if (k == '\b') {
  890. if (t->line_pos > 0) {
  891. unsigned int i;
  892. t->line_pos--;
  893. for (i = t->line_pos; i < t->line_len; i++)
  894. t->line[i] = t->line[i + 1];
  895. t->line_len--;
  896. t->line[t->line_len] = '\0';
  897. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
  898. }
  899. } else if (k == '\n') {
  900. char cat_fname[CAT_FNAME_MAX];
  901. char save_args[SAVE_ARGS_MAX];
  902. char rm_fname[RM_FNAME_MAX];
  903. char ls_path[LS_PATH_MAX];
  904. char format_dev[FORMAT_DEV_MAX];
  905. char netip_arg[16];
  906. char resolved[VFS_PATH_MAX];
  907. char save_resolved[SAVE_ARGS_MAX + VFS_PATH_MAX];
  908. unsigned int kill_pid;
  909. int skip_prompt = 0;
  910. io_putchar('\n');
  911. history_push(t, t->line, t->line_len);
  912. t->history_browse = 0;
  913. if (is_cmd_hello(t->line, t->line_len)) {
  914. int pid = process_create_user("hello",
  915. _binary_hello_elf_start,
  916. (unsigned int)(_binary_hello_elf_end -
  917. _binary_hello_elf_start));
  918. if (pid < 0)
  919. io_putstr("error: too many processes\n");
  920. else
  921. t->fg_pid = pid;
  922. } else if (is_cmd_reboot(t->line, t->line_len)) {
  923. program_reboot_main();
  924. } else if (is_cmd_poweroff(t->line, t->line_len)) {
  925. program_poweroff_main();
  926. } else if (is_cmd_cd(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  927. term_cd(t, ls_path);
  928. } else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  929. if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
  930. term_spawn_arg(t, "ls", program_ls_main, resolved);
  931. } else {
  932. io_putstr("ls: path too long\n");
  933. }
  934. } else if (is_cmd_lsusb(t->line, t->line_len)) {
  935. term_spawn(t, "lsusb", program_lsusb_main);
  936. } else if (is_cmd_lspci(t->line, t->line_len)) {
  937. term_spawn(t, "lspci", program_lspci_main);
  938. } else if (is_cmd_netinfo(t->line, t->line_len)) {
  939. term_spawn(t, "netinfo", program_netinfo_main);
  940. } else if (is_cmd_netrecv(t->line, t->line_len)) {
  941. term_spawn(t, "netrecv", program_netrecv_main);
  942. } else if (is_cmd_netsend(t->line, t->line_len)) {
  943. term_spawn(t, "netsend", program_netsend_main);
  944. } else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
  945. term_spawn_arg(t, "netip", program_netip_main, netip_arg);
  946. } else if (is_cmd_ping(t->line, t->line_len)) {
  947. term_spawn(t, "ping", program_ping_main);
  948. } else if (is_cmd_storage(t->line, t->line_len)) {
  949. term_spawn(t, "storage", program_storage_main);
  950. } else if (is_cmd_mem(t->line, t->line_len)) {
  951. term_spawn(t, "mem", program_mem_main);
  952. } else if (is_cmd_clock(t->line, t->line_len)) {
  953. io_putstr("clock: not available in windowed mode yet\n");
  954. } else if (is_cmd_colors(t->line, t->line_len)) {
  955. io_putstr("colors: not available in windowed mode yet\n");
  956. } else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  957. if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
  958. term_spawn_arg(t, "cat", program_cat_main, resolved);
  959. } else {
  960. io_putstr("cat: path too long\n");
  961. }
  962. } else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  963. if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
  964. cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
  965. cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
  966. cat_fname[6] == '\0') {
  967. if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
  968. io_putstr("error: too many processes\n");
  969. } else {
  970. io_putstr("run: not available in windowed mode yet\n");
  971. }
  972. } else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
  973. term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
  974. term_spawn_arg(t, "save", program_save_main, save_resolved);
  975. } else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  976. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  977. term_spawn_arg(t, "mkdir", program_mkdir_main, resolved);
  978. } else {
  979. io_putstr("mkdir: path too long\n");
  980. }
  981. } else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  982. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  983. term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
  984. } else {
  985. io_putstr("rmdir: path too long\n");
  986. }
  987. } else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  988. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  989. term_spawn_arg(t, "rm", program_rm_main, resolved);
  990. } else {
  991. io_putstr("rm: path too long\n");
  992. }
  993. } else if (is_cmd_space(t->line, t->line_len)) {
  994. io_putstr("space: not available in windowed mode yet\n");
  995. } else if (is_cmd_display(t->line, t->line_len)) {
  996. term_spawn(t, "display", program_display_main);
  997. } else if (is_cmd_cpuinfo(t->line, t->line_len)) {
  998. term_spawn(t, "cpuinfo", program_cpuinfo_main);
  999. } else if (is_cmd_mouseinfo(t->line, t->line_len)) {
  1000. term_spawn(t, "mouseinfo", program_mouseinfo_main);
  1001. } else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
  1002. program_format_main(format_dev);
  1003. } else if (is_cmd_ps(t->line, t->line_len)) {
  1004. process_ps();
  1005. } else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
  1006. if (process_kill(kill_pid) < 0)
  1007. io_putstr("kill: no such process\n");
  1008. else
  1009. io_putstr("killed\n");
  1010. } else if (is_cmd_kexec(t->line, t->line_len)) {
  1011. if (kexec_busy) {
  1012. io_putstr("kexec: already receiving\n");
  1013. } else {
  1014. kexec_busy = 1;
  1015. if (process_create("kexec", program_kexec_main) < 0) {
  1016. kexec_busy = 0;
  1017. io_putstr("error: too many processes\n");
  1018. }
  1019. }
  1020. } else if (is_cmd_dbg(t->line, t->line_len)) {
  1021. program_dbg_main();
  1022. } else if (is_cmd_help(t->line, t->line_len)) {
  1023. io_putstr("cd [path]\n");
  1024. io_putstr("ls [path]\n");
  1025. io_putstr("cat <path>\n");
  1026. io_putstr("save <path> <text>\n");
  1027. io_putstr("rm <path>\n");
  1028. io_putstr("mkdir <path>\n");
  1029. io_putstr("rmdir <path>\n");
  1030. io_putstr("hello\n");
  1031. io_putstr("reboot\n");
  1032. io_putstr("poweroff\n");
  1033. io_putstr("run <file>\n");
  1034. io_putstr("space\n");
  1035. io_putstr("clock\n");
  1036. io_putstr("colors\n");
  1037. io_putstr("display\n");
  1038. io_putstr("cpuinfo\n");
  1039. io_putstr("lsusb\n");
  1040. io_putstr("mouseinfo\n");
  1041. io_putstr("storage\n");
  1042. io_putstr("mem\n");
  1043. io_putstr("format <device>\n");
  1044. io_putstr("lspci\n");
  1045. io_putstr("netinfo\n");
  1046. io_putstr("netrecv\n");
  1047. io_putstr("netsend\n");
  1048. io_putstr("netip [a.b.c.d]\n");
  1049. io_putstr("ping\n");
  1050. io_putstr("ps\n");
  1051. io_putstr("kill <pid>\n");
  1052. io_putstr("kexec\n");
  1053. io_putstr("dbg\n");
  1054. io_putstr("serial test|recv\n");
  1055. io_putstr("clear\n");
  1056. } else if (is_cmd_clear(t->line, t->line_len)) {
  1057. io_clear_screen();
  1058. term_print_prompt(t, 1);
  1059. skip_prompt = 1;
  1060. } else {
  1061. char serial_arg[8];
  1062. if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
  1063. if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
  1064. serial_arg[2] == 's' && serial_arg[3] == 't' &&
  1065. serial_arg[4] == '\0') {
  1066. if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
  1067. io_putstr("error: too many processes\n");
  1068. } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
  1069. serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
  1070. serial_arg[4] == '\0') {
  1071. program_serial_recv_main(serial_arg);
  1072. } else {
  1073. io_putstr("serial: usage serial test|recv\n");
  1074. }
  1075. } else {
  1076. unsigned int i = 0;
  1077. while (i < t->line_len && t->line[i] == ' ')
  1078. i++;
  1079. if (i < t->line_len)
  1080. io_putstr("command not found\n");
  1081. }
  1082. }
  1083. t->line_len = 0;
  1084. t->line_pos = 0;
  1085. t->line[0] = '\0';
  1086. if (!skip_prompt && t->fg_pid < 0) {
  1087. term_print_prompt(t, 0);
  1088. }
  1089. } else {
  1090. unsigned char u = (unsigned char)k;
  1091. if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
  1092. unsigned int i;
  1093. t->history_browse = 0;
  1094. for (i = t->line_len; i > t->line_pos; i--)
  1095. t->line[i] = t->line[i - 1];
  1096. t->line[t->line_pos] = (char)k;
  1097. t->line_len++;
  1098. t->line[t->line_len] = '\0';
  1099. t->line_pos++;
  1100. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
  1101. }
  1102. }
  1103. } else {
  1104. process_sleep_ms(10);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. void term_entry(void *arg)
  1110. {
  1111. struct term *t = (struct term *)arg;
  1112. process_set_screen(&t->screen);
  1113. term_run(t);
  1114. }