terminal.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 "idle.h"
  6. #include "kexec.h"
  7. #include "process.h"
  8. #include "terminal.h"
  9. #include "timer.h"
  10. extern void program_hello_main(void);
  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. cpu_idle();
  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. term_spawn(t, "hello", program_hello_main);
  915. } else if (is_cmd_reboot(t->line, t->line_len)) {
  916. program_reboot_main();
  917. } else if (is_cmd_poweroff(t->line, t->line_len)) {
  918. program_poweroff_main();
  919. } else if (is_cmd_cd(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  920. term_cd(t, ls_path);
  921. } else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  922. if (vfs_join_path(t->cwd, ls_path, resolved, sizeof(resolved)) == 0) {
  923. term_spawn_arg(t, "ls", program_ls_main, resolved);
  924. } else {
  925. io_putstr("ls: path too long\n");
  926. }
  927. } else if (is_cmd_lsusb(t->line, t->line_len)) {
  928. term_spawn(t, "lsusb", program_lsusb_main);
  929. } else if (is_cmd_lspci(t->line, t->line_len)) {
  930. term_spawn(t, "lspci", program_lspci_main);
  931. } else if (is_cmd_netinfo(t->line, t->line_len)) {
  932. term_spawn(t, "netinfo", program_netinfo_main);
  933. } else if (is_cmd_netrecv(t->line, t->line_len)) {
  934. term_spawn(t, "netrecv", program_netrecv_main);
  935. } else if (is_cmd_netsend(t->line, t->line_len)) {
  936. term_spawn(t, "netsend", program_netsend_main);
  937. } else if (is_cmd_netip(t->line, t->line_len, netip_arg, sizeof(netip_arg))) {
  938. term_spawn_arg(t, "netip", program_netip_main, netip_arg);
  939. } else if (is_cmd_ping(t->line, t->line_len)) {
  940. term_spawn(t, "ping", program_ping_main);
  941. } else if (is_cmd_storage(t->line, t->line_len)) {
  942. term_spawn(t, "storage", program_storage_main);
  943. } else if (is_cmd_mem(t->line, t->line_len)) {
  944. term_spawn(t, "mem", program_mem_main);
  945. } else if (is_cmd_clock(t->line, t->line_len)) {
  946. io_putstr("clock: not available in windowed mode yet\n");
  947. } else if (is_cmd_colors(t->line, t->line_len)) {
  948. io_putstr("colors: not available in windowed mode yet\n");
  949. } else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  950. if (vfs_join_path(t->cwd, cat_fname, resolved, sizeof(resolved)) == 0) {
  951. term_spawn_arg(t, "cat", program_cat_main, resolved);
  952. } else {
  953. io_putstr("cat: path too long\n");
  954. }
  955. } else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  956. if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
  957. cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
  958. cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
  959. cat_fname[6] == '\0') {
  960. if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
  961. io_putstr("error: too many processes\n");
  962. } else {
  963. io_putstr("run: not available in windowed mode yet\n");
  964. }
  965. } else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
  966. term_resolve_first(t, save_args, save_resolved, sizeof(save_resolved));
  967. term_spawn_arg(t, "save", program_save_main, save_resolved);
  968. } else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  969. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  970. term_spawn_arg(t, "mkdir", program_mkdir_main, resolved);
  971. } else {
  972. io_putstr("mkdir: path too long\n");
  973. }
  974. } else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  975. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  976. term_spawn_arg(t, "rmdir", program_rmdir_main, resolved);
  977. } else {
  978. io_putstr("rmdir: path too long\n");
  979. }
  980. } else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  981. if (vfs_join_path(t->cwd, rm_fname, resolved, sizeof(resolved)) == 0) {
  982. term_spawn_arg(t, "rm", program_rm_main, resolved);
  983. } else {
  984. io_putstr("rm: path too long\n");
  985. }
  986. } else if (is_cmd_space(t->line, t->line_len)) {
  987. io_putstr("space: not available in windowed mode yet\n");
  988. } else if (is_cmd_display(t->line, t->line_len)) {
  989. term_spawn(t, "display", program_display_main);
  990. } else if (is_cmd_cpuinfo(t->line, t->line_len)) {
  991. term_spawn(t, "cpuinfo", program_cpuinfo_main);
  992. } else if (is_cmd_mouseinfo(t->line, t->line_len)) {
  993. term_spawn(t, "mouseinfo", program_mouseinfo_main);
  994. } else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
  995. program_format_main(format_dev);
  996. } else if (is_cmd_ps(t->line, t->line_len)) {
  997. process_ps();
  998. } else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
  999. if (process_kill(kill_pid) < 0)
  1000. io_putstr("kill: no such process\n");
  1001. else
  1002. io_putstr("killed\n");
  1003. } else if (is_cmd_kexec(t->line, t->line_len)) {
  1004. if (kexec_busy) {
  1005. io_putstr("kexec: already receiving\n");
  1006. } else {
  1007. kexec_busy = 1;
  1008. if (process_create("kexec", program_kexec_main) < 0) {
  1009. kexec_busy = 0;
  1010. io_putstr("error: too many processes\n");
  1011. }
  1012. }
  1013. } else if (is_cmd_dbg(t->line, t->line_len)) {
  1014. program_dbg_main();
  1015. } else if (is_cmd_help(t->line, t->line_len)) {
  1016. io_putstr("cd [path]\n");
  1017. io_putstr("ls [path]\n");
  1018. io_putstr("cat <path>\n");
  1019. io_putstr("save <path> <text>\n");
  1020. io_putstr("rm <path>\n");
  1021. io_putstr("mkdir <path>\n");
  1022. io_putstr("rmdir <path>\n");
  1023. io_putstr("hello\n");
  1024. io_putstr("reboot\n");
  1025. io_putstr("poweroff\n");
  1026. io_putstr("run <file>\n");
  1027. io_putstr("space\n");
  1028. io_putstr("clock\n");
  1029. io_putstr("colors\n");
  1030. io_putstr("display\n");
  1031. io_putstr("cpuinfo\n");
  1032. io_putstr("lsusb\n");
  1033. io_putstr("mouseinfo\n");
  1034. io_putstr("storage\n");
  1035. io_putstr("mem\n");
  1036. io_putstr("format <device>\n");
  1037. io_putstr("lspci\n");
  1038. io_putstr("netinfo\n");
  1039. io_putstr("netrecv\n");
  1040. io_putstr("netsend\n");
  1041. io_putstr("netip [a.b.c.d]\n");
  1042. io_putstr("ping\n");
  1043. io_putstr("ps\n");
  1044. io_putstr("kill <pid>\n");
  1045. io_putstr("kexec\n");
  1046. io_putstr("dbg\n");
  1047. io_putstr("serial test|recv\n");
  1048. io_putstr("clear\n");
  1049. } else if (is_cmd_clear(t->line, t->line_len)) {
  1050. io_clear_screen();
  1051. term_print_prompt(t, 1);
  1052. skip_prompt = 1;
  1053. } else {
  1054. char serial_arg[8];
  1055. if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
  1056. if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
  1057. serial_arg[2] == 's' && serial_arg[3] == 't' &&
  1058. serial_arg[4] == '\0') {
  1059. if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
  1060. io_putstr("error: too many processes\n");
  1061. } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
  1062. serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
  1063. serial_arg[4] == '\0') {
  1064. program_serial_recv_main(serial_arg);
  1065. } else {
  1066. io_putstr("serial: usage serial test|recv\n");
  1067. }
  1068. } else {
  1069. unsigned int i = 0;
  1070. while (i < t->line_len && t->line[i] == ' ')
  1071. i++;
  1072. if (i < t->line_len)
  1073. io_putstr("command not found\n");
  1074. }
  1075. }
  1076. t->line_len = 0;
  1077. t->line_pos = 0;
  1078. t->line[0] = '\0';
  1079. if (!skip_prompt && t->fg_pid < 0) {
  1080. term_print_prompt(t, 0);
  1081. }
  1082. } else {
  1083. unsigned char u = (unsigned char)k;
  1084. if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
  1085. unsigned int i;
  1086. t->history_browse = 0;
  1087. for (i = t->line_len; i > t->line_pos; i--)
  1088. t->line[i] = t->line[i - 1];
  1089. t->line[t->line_pos] = (char)k;
  1090. t->line_len++;
  1091. t->line[t->line_len] = '\0';
  1092. t->line_pos++;
  1093. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
  1094. }
  1095. }
  1096. } else {
  1097. cpu_idle();
  1098. }
  1099. }
  1100. }
  1101. }
  1102. void term_entry(void *arg)
  1103. {
  1104. struct term *t = (struct term *)arg;
  1105. process_set_screen(&t->screen);
  1106. term_run(t);
  1107. }