terminal.c 35 KB

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