terminal.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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_format_main(const char *arg);
  31. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  32. static const char prompt_first[] __attribute__((section(".text"))) = "> ";
  33. static int is_cmd_hello(const char *line, unsigned int line_len)
  34. {
  35. unsigned int i = 0;
  36. while (i < line_len && line[i] == ' ')
  37. i++;
  38. if (i + 5 > line_len)
  39. return 0;
  40. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  41. return 0;
  42. i += 5;
  43. while (i < line_len && line[i] == ' ')
  44. i++;
  45. return (i == line_len);
  46. }
  47. static int is_cmd_reboot(const char *line, unsigned int line_len)
  48. {
  49. unsigned int i = 0;
  50. while (i < line_len && line[i] == ' ')
  51. i++;
  52. if (i + 6 > line_len)
  53. return 0;
  54. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  55. return 0;
  56. i += 6;
  57. while (i < line_len && line[i] == ' ')
  58. i++;
  59. return (i == line_len);
  60. }
  61. static int is_cmd_poweroff(const char *line, unsigned int line_len)
  62. {
  63. unsigned int i = 0;
  64. while (i < line_len && line[i] == ' ')
  65. i++;
  66. if (i + 8 > line_len)
  67. return 0;
  68. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' ||
  69. line[i+4] != 'r' || line[i+5] != 'o' || line[i+6] != 'f' || line[i+7] != 'f')
  70. return 0;
  71. i += 8;
  72. while (i < line_len && line[i] == ' ')
  73. i++;
  74. return (i == line_len);
  75. }
  76. static int is_cmd_ls(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  77. {
  78. unsigned int i = 0, j = 0;
  79. while (i < line_len && line[i] == ' ')
  80. i++;
  81. if (i + 2 > line_len)
  82. return 0;
  83. if (line[i] != 'l' || line[i+1] != 's')
  84. return 0;
  85. i += 2;
  86. if (i < line_len && line[i] != ' ')
  87. return 0; /* "ls" must be a whole word, so reject "lsusb" etc. */
  88. while (i < line_len && line[i] == ' ')
  89. i++;
  90. if (i >= line_len) {
  91. if (buf_size > 0)
  92. path_buf[0] = '\0';
  93. return 1;
  94. }
  95. while (i < line_len && j + 1 < buf_size)
  96. path_buf[j++] = line[i++];
  97. path_buf[j] = '\0';
  98. while (i < line_len && line[i] == ' ')
  99. i++;
  100. return (i == line_len);
  101. }
  102. static int is_cmd_clock(const char *line, unsigned int line_len)
  103. {
  104. unsigned int i = 0;
  105. while (i < line_len && line[i] == ' ')
  106. i++;
  107. if (i + 5 > line_len)
  108. return 0;
  109. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  110. line[i+3] != 'c' || line[i+4] != 'k')
  111. return 0;
  112. i += 5;
  113. while (i < line_len && line[i] == ' ')
  114. i++;
  115. return (i == line_len);
  116. }
  117. static int is_cmd_colors(const char *line, unsigned int line_len)
  118. {
  119. unsigned int i = 0;
  120. while (i < line_len && line[i] == ' ')
  121. i++;
  122. if (i + 6 > line_len)
  123. return 0;
  124. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  125. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  126. return 0;
  127. i += 6;
  128. while (i < line_len && line[i] == ' ')
  129. i++;
  130. return (i == line_len);
  131. }
  132. static int is_cmd_display(const char *line, unsigned int line_len)
  133. {
  134. unsigned int i = 0;
  135. while (i < line_len && line[i] == ' ')
  136. i++;
  137. if (i + 7 > line_len)
  138. return 0;
  139. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  140. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  141. return 0;
  142. i += 7;
  143. while (i < line_len && line[i] == ' ')
  144. i++;
  145. return (i == line_len);
  146. }
  147. static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
  148. {
  149. unsigned int i = 0;
  150. while (i < line_len && line[i] == ' ')
  151. i++;
  152. if (i + 7 > line_len)
  153. return 0;
  154. if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
  155. line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
  156. return 0;
  157. i += 7;
  158. while (i < line_len && line[i] == ' ')
  159. i++;
  160. return (i == line_len);
  161. }
  162. static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
  163. {
  164. unsigned int i = 0;
  165. while (i < line_len && line[i] == ' ')
  166. i++;
  167. if (i + 9 > line_len)
  168. return 0;
  169. if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
  170. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
  171. line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
  172. return 0;
  173. i += 9;
  174. while (i < line_len && line[i] == ' ')
  175. i++;
  176. return (i == line_len);
  177. }
  178. static int is_cmd_lsusb(const char *line, unsigned int line_len)
  179. {
  180. unsigned int i = 0;
  181. while (i < line_len && line[i] == ' ')
  182. i++;
  183. if (i + 5 > line_len)
  184. return 0;
  185. if (line[i] != 'l' || line[i+1] != 's' || line[i+2] != 'u' ||
  186. line[i+3] != 's' || line[i+4] != 'b')
  187. return 0;
  188. i += 5;
  189. while (i < line_len && line[i] == ' ')
  190. i++;
  191. return (i == line_len);
  192. }
  193. static int is_cmd_space(const char *line, unsigned int line_len)
  194. {
  195. unsigned int i = 0;
  196. while (i < line_len && line[i] == ' ')
  197. i++;
  198. if (i + 5 > line_len)
  199. return 0;
  200. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  201. line[i+3] != 'c' || line[i+4] != 'e')
  202. return 0;
  203. i += 5;
  204. while (i < line_len && line[i] == ' ')
  205. i++;
  206. return (i == line_len);
  207. }
  208. #define CAT_FNAME_MAX 64
  209. static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  210. {
  211. unsigned int i = 0, j;
  212. while (i < line_len && line[i] == ' ')
  213. i++;
  214. if (i + 3 > line_len)
  215. return 0;
  216. if (line[i] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  217. return 0;
  218. i += 3;
  219. while (i < line_len && line[i] == ' ')
  220. i++;
  221. if (i >= line_len)
  222. return 0;
  223. j = 0;
  224. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  225. filename_buf[j++] = line[i++];
  226. filename_buf[j] = '\0';
  227. return (j > 0);
  228. }
  229. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  230. {
  231. unsigned int i = 0, j;
  232. while (i < line_len && line[i] == ' ')
  233. i++;
  234. if (i + 3 > line_len)
  235. return 0;
  236. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  237. return 0;
  238. i += 3;
  239. while (i < line_len && line[i] == ' ')
  240. i++;
  241. if (i >= line_len)
  242. return 0;
  243. j = 0;
  244. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  245. filename_buf[j++] = line[i++];
  246. filename_buf[j] = '\0';
  247. return (j > 0);
  248. }
  249. #define SAVE_ARGS_MAX 128
  250. #define RM_FNAME_MAX 64
  251. #define LS_PATH_MAX 64
  252. #define FORMAT_DEV_MAX 16
  253. static int is_cmd_save(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  254. {
  255. unsigned int i = 0, j = 0;
  256. while (i < line_len && line[i] == ' ')
  257. i++;
  258. if (i + 4 > line_len)
  259. return 0;
  260. if (line[i] != 's' || line[i+1] != 'a' || line[i+2] != 'v' || line[i+3] != 'e')
  261. return 0;
  262. i += 4;
  263. while (i < line_len && line[i] == ' ')
  264. i++;
  265. if (i >= line_len)
  266. return 0;
  267. while (i < line_len && j + 1 < buf_size)
  268. arg_buf[j++] = line[i++];
  269. while (j > 0 && arg_buf[j - 1] == ' ')
  270. j--;
  271. arg_buf[j] = '\0';
  272. while (i < line_len && line[i] == ' ')
  273. i++;
  274. return (i == line_len && j > 0);
  275. }
  276. static int is_cmd_rm(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  277. {
  278. unsigned int i = 0, j = 0;
  279. while (i < line_len && line[i] == ' ')
  280. i++;
  281. if (i + 2 > line_len)
  282. return 0;
  283. if (line[i] != 'r' || line[i+1] != 'm')
  284. return 0;
  285. i += 2;
  286. while (i < line_len && line[i] == ' ')
  287. i++;
  288. if (i >= line_len)
  289. return 0;
  290. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  291. filename_buf[j++] = line[i++];
  292. filename_buf[j] = '\0';
  293. return (j > 0);
  294. }
  295. static int is_cmd_mkdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  296. {
  297. unsigned int i = 0, j = 0;
  298. while (i < line_len && line[i] == ' ')
  299. i++;
  300. if (i + 5 > line_len)
  301. return 0;
  302. if (line[i] != 'm' || line[i+1] != 'k' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  303. return 0;
  304. i += 5;
  305. while (i < line_len && line[i] == ' ')
  306. i++;
  307. if (i >= line_len)
  308. return 0;
  309. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  310. path_buf[j++] = line[i++];
  311. path_buf[j] = '\0';
  312. return (j > 0);
  313. }
  314. static int is_cmd_rmdir(const char *line, unsigned int line_len, char *path_buf, unsigned int buf_size)
  315. {
  316. unsigned int i = 0, j = 0;
  317. while (i < line_len && line[i] == ' ')
  318. i++;
  319. if (i + 5 > line_len)
  320. return 0;
  321. if (line[i] != 'r' || line[i+1] != 'm' || line[i+2] != 'd' || line[i+3] != 'i' || line[i+4] != 'r')
  322. return 0;
  323. i += 5;
  324. while (i < line_len && line[i] == ' ')
  325. i++;
  326. if (i >= line_len)
  327. return 0;
  328. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  329. path_buf[j++] = line[i++];
  330. path_buf[j] = '\0';
  331. return (j > 0);
  332. }
  333. static int is_cmd_storage(const char *line, unsigned int line_len)
  334. {
  335. unsigned int i = 0;
  336. while (i < line_len && line[i] == ' ')
  337. i++;
  338. if (i + 7 > line_len)
  339. return 0;
  340. if (line[i] != 's' || line[i+1] != 't' || line[i+2] != 'o' ||
  341. line[i+3] != 'r' || line[i+4] != 'a' || line[i+5] != 'g' || line[i+6] != 'e')
  342. return 0;
  343. i += 7;
  344. while (i < line_len && line[i] == ' ')
  345. i++;
  346. return (i == line_len);
  347. }
  348. static int is_cmd_format(const char *line, unsigned int line_len, char *dev_buf, unsigned int buf_size)
  349. {
  350. unsigned int i = 0, j;
  351. while (i < line_len && line[i] == ' ')
  352. i++;
  353. if (i + 6 > line_len)
  354. return 0;
  355. if (line[i] != 'f' || line[i+1] != 'o' || line[i+2] != 'r' ||
  356. line[i+3] != 'm' || line[i+4] != 'a' || line[i+5] != 't')
  357. return 0;
  358. i += 6;
  359. if (i < line_len && line[i] != ' ')
  360. return 0; /* "format" must be a whole word */
  361. while (i < line_len && line[i] == ' ')
  362. i++;
  363. if (i >= line_len)
  364. return 0;
  365. j = 0;
  366. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  367. dev_buf[j++] = line[i++];
  368. dev_buf[j] = '\0';
  369. while (i < line_len && line[i] == ' ')
  370. i++;
  371. return (i == line_len && j > 0);
  372. }
  373. static int is_cmd_help(const char *line, unsigned int line_len)
  374. {
  375. unsigned int i = 0;
  376. while (i < line_len && line[i] == ' ')
  377. i++;
  378. if (i + 4 > line_len)
  379. return 0;
  380. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  381. return 0;
  382. i += 4;
  383. while (i < line_len && line[i] == ' ')
  384. i++;
  385. return (i == line_len);
  386. }
  387. static int is_cmd_clear(const char *line, unsigned int line_len)
  388. {
  389. unsigned int i = 0;
  390. while (i < line_len && line[i] == ' ')
  391. i++;
  392. if (i + 5 > line_len)
  393. return 0;
  394. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  395. line[i+3] != 'a' || line[i+4] != 'r')
  396. return 0;
  397. i += 5;
  398. while (i < line_len && line[i] == ' ')
  399. i++;
  400. return (i == line_len);
  401. }
  402. static int is_cmd_ps(const char *line, unsigned int line_len)
  403. {
  404. unsigned int i = 0;
  405. while (i < line_len && line[i] == ' ')
  406. i++;
  407. if (i + 2 > line_len)
  408. return 0;
  409. if (line[i] != 'p' || line[i+1] != 's')
  410. return 0;
  411. i += 2;
  412. while (i < line_len && line[i] == ' ')
  413. i++;
  414. return (i == line_len);
  415. }
  416. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  417. {
  418. unsigned int i = 0, num = 0;
  419. int has_digit = 0;
  420. while (i < line_len && line[i] == ' ')
  421. i++;
  422. if (i + 4 > line_len)
  423. return 0;
  424. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  425. return 0;
  426. i += 4;
  427. while (i < line_len && line[i] == ' ')
  428. i++;
  429. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  430. num = num * 10 + (unsigned int)(line[i] - '0');
  431. has_digit = 1;
  432. i++;
  433. }
  434. if (!has_digit)
  435. return 0;
  436. while (i < line_len && line[i] == ' ')
  437. i++;
  438. if (i != line_len)
  439. return 0;
  440. *pid_out = num;
  441. return 1;
  442. }
  443. static int is_cmd_kexec(const char *line, unsigned int line_len)
  444. {
  445. unsigned int i = 0;
  446. while (i < line_len && line[i] == ' ')
  447. i++;
  448. if (i + 5 > line_len)
  449. return 0;
  450. if (line[i] != 'k' || line[i+1] != 'e' || line[i+2] != 'x' || line[i+3] != 'e' || line[i+4] != 'c')
  451. return 0;
  452. i += 5;
  453. while (i < line_len && line[i] == ' ')
  454. i++;
  455. return (i == line_len);
  456. }
  457. static int is_cmd_dbg(const char *line, unsigned int line_len)
  458. {
  459. unsigned int i = 0;
  460. while (i < line_len && line[i] == ' ')
  461. i++;
  462. if (i + 3 > line_len)
  463. return 0;
  464. if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
  465. return 0;
  466. i += 3;
  467. while (i < line_len && line[i] == ' ')
  468. i++;
  469. return (i == line_len);
  470. }
  471. static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
  472. {
  473. unsigned int i = 0, j = 0;
  474. while (i < line_len && line[i] == ' ')
  475. i++;
  476. if (i + 6 > line_len)
  477. return 0;
  478. if (line[i] != 's' || line[i+1] != 'e' || line[i+2] != 'r' ||
  479. line[i+3] != 'i' || line[i+4] != 'a' || line[i+5] != 'l')
  480. return 0;
  481. i += 6;
  482. while (i < line_len && line[i] == ' ')
  483. i++;
  484. if (i >= line_len) {
  485. if (buf_size > 0)
  486. arg_buf[0] = '\0';
  487. return 1;
  488. }
  489. while (i < line_len && j + 1 < buf_size)
  490. arg_buf[j++] = line[i++];
  491. while (j > 0 && arg_buf[j - 1] == ' ')
  492. j--;
  493. arg_buf[j] = '\0';
  494. while (i < line_len && line[i] == ' ')
  495. i++;
  496. return (i == line_len);
  497. }
  498. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  499. static void redraw_line_from(const char *line, unsigned int line_len,
  500. unsigned int line_pos, unsigned int line_start, unsigned int from)
  501. {
  502. unsigned int i;
  503. io_set_cursor(line_start + from);
  504. for (i = from; i < line_len; i++)
  505. io_putchar(line[i]);
  506. io_putchar(' ');
  507. io_set_cursor(line_start + line_pos);
  508. }
  509. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  510. static void redraw_line_clear(const char *line, unsigned int line_len,
  511. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  512. {
  513. unsigned int i;
  514. io_set_cursor(line_start);
  515. for (i = 0; i < line_len; i++)
  516. io_putchar(line[i]);
  517. for (i = line_len; i <= clear_through; i++)
  518. io_putchar(' ');
  519. io_set_cursor(line_start + line_pos);
  520. }
  521. /* Copy history entry (1 = newest) into t->line, redraw from start. */
  522. static void history_load_into_line(struct term *t, unsigned int browse)
  523. {
  524. unsigned int slot, len, old_len;
  525. const char *src;
  526. if (browse < 1 || browse > t->history_count)
  527. return;
  528. old_len = t->line_len;
  529. slot = (t->history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  530. src = t->history[slot];
  531. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  532. t->line[len] = src[len];
  533. t->line[len] = '\0';
  534. t->line_len = len;
  535. t->line_pos = len;
  536. redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
  537. }
  538. /* Push current line to history if non-empty (and not duplicate of newest). */
  539. static void history_push(struct term *t, const char *line, unsigned int line_len)
  540. {
  541. unsigned int i, slot;
  542. int all_spaces = 1;
  543. if (line_len == 0)
  544. return;
  545. for (i = 0; i < line_len; i++) {
  546. if (line[i] != ' ') {
  547. all_spaces = 0;
  548. break;
  549. }
  550. }
  551. if (all_spaces)
  552. return;
  553. if (t->history_count > 0) {
  554. slot = (t->history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  555. for (i = 0; i < line_len && i < LINE_MAX - 1 && t->history[slot][i]; i++) {
  556. if (t->history[slot][i] != line[i])
  557. break;
  558. }
  559. if (i == line_len && t->history[slot][i] == '\0')
  560. return; /* duplicate of newest */
  561. }
  562. slot = t->history_next;
  563. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  564. t->history[slot][i] = line[i];
  565. t->history[slot][i] = '\0';
  566. t->history_next = (t->history_next + 1) % HISTORY_SIZE;
  567. if (t->history_count < HISTORY_SIZE)
  568. t->history_count++;
  569. }
  570. void term_run(struct term *t)
  571. {
  572. t->line_len = 0;
  573. t->line_pos = 0;
  574. t->line[0] = '\0';
  575. t->history_browse = 0;
  576. io_clear_screen();
  577. io_putstr(prompt_first);
  578. t->line_start = io_get_cursor();
  579. for (;;) {
  580. {
  581. int k = term_key_pop(t);
  582. if (k) {
  583. if (k == IO_KEY_LEFT) {
  584. if (t->line_pos > 0) {
  585. t->line_pos--;
  586. io_set_cursor(t->line_start + t->line_pos);
  587. }
  588. } else if (k == IO_KEY_RIGHT) {
  589. if (t->line_pos < t->line_len) {
  590. t->line_pos++;
  591. io_set_cursor(t->line_start + t->line_pos);
  592. }
  593. } else if (k == IO_KEY_UP) {
  594. if (t->history_count > 0) {
  595. if (t->history_browse < t->history_count)
  596. t->history_browse++;
  597. history_load_into_line(t, t->history_browse);
  598. }
  599. } else if (k == IO_KEY_DOWN) {
  600. if (t->history_browse > 0) {
  601. t->history_browse--;
  602. if (t->history_browse == 0) {
  603. unsigned int clear_len = t->line_len;
  604. t->line_len = 0;
  605. t->line_pos = 0;
  606. t->line[0] = '\0';
  607. redraw_line_clear(t->line, 0, 0, t->line_start, clear_len);
  608. } else {
  609. history_load_into_line(t, t->history_browse);
  610. }
  611. }
  612. } else if (k == '\b') {
  613. if (t->line_pos > 0) {
  614. unsigned int i;
  615. t->line_pos--;
  616. for (i = t->line_pos; i < t->line_len; i++)
  617. t->line[i] = t->line[i + 1];
  618. t->line_len--;
  619. t->line[t->line_len] = '\0';
  620. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
  621. }
  622. } else if (k == '\n') {
  623. char cat_fname[CAT_FNAME_MAX];
  624. char save_args[SAVE_ARGS_MAX];
  625. char rm_fname[RM_FNAME_MAX];
  626. char ls_path[LS_PATH_MAX];
  627. char format_dev[FORMAT_DEV_MAX];
  628. unsigned int kill_pid;
  629. int skip_prompt = 0;
  630. io_putchar('\n');
  631. history_push(t, t->line, t->line_len);
  632. t->history_browse = 0;
  633. if (is_cmd_hello(t->line, t->line_len)) {
  634. if (process_create("hello", program_hello_main) < 0)
  635. io_putstr("error: too many processes\n");
  636. } else if (is_cmd_reboot(t->line, t->line_len)) {
  637. program_reboot_main();
  638. } else if (is_cmd_poweroff(t->line, t->line_len)) {
  639. program_poweroff_main();
  640. } else if (is_cmd_ls(t->line, t->line_len, ls_path, LS_PATH_MAX)) {
  641. if (process_create_arg("ls", program_ls_main, ls_path) < 0)
  642. io_putstr("error: too many processes\n");
  643. } else if (is_cmd_lsusb(t->line, t->line_len)) {
  644. if (process_create("lsusb", program_lsusb_main) < 0)
  645. io_putstr("error: too many processes\n");
  646. } else if (is_cmd_storage(t->line, t->line_len)) {
  647. if (process_create("storage", program_storage_main) < 0)
  648. io_putstr("error: too many processes\n");
  649. } else if (is_cmd_clock(t->line, t->line_len)) {
  650. io_putstr("clock: not available in windowed mode yet\n");
  651. } else if (is_cmd_colors(t->line, t->line_len)) {
  652. io_putstr("colors: not available in windowed mode yet\n");
  653. } else if (is_cmd_cat(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  654. if (process_create_arg("cat", program_cat_main, cat_fname) < 0)
  655. io_putstr("error: too many processes\n");
  656. } else if (is_cmd_run(t->line, t->line_len, cat_fname, CAT_FNAME_MAX)) {
  657. if (cat_fname[0] == 's' && cat_fname[1] == 'e' &&
  658. cat_fname[2] == 'r' && cat_fname[3] == 'i' &&
  659. cat_fname[4] == 'a' && cat_fname[5] == 'l' &&
  660. cat_fname[6] == '\0') {
  661. if (process_create_arg("run_serial", program_run_serial_main, cat_fname) < 0)
  662. io_putstr("error: too many processes\n");
  663. } else {
  664. io_putstr("run: not available in windowed mode yet\n");
  665. }
  666. } else if (is_cmd_save(t->line, t->line_len, save_args, SAVE_ARGS_MAX)) {
  667. if (process_create_arg("save", program_save_main, save_args) < 0)
  668. io_putstr("error: too many processes\n");
  669. } else if (is_cmd_mkdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  670. if (process_create_arg("mkdir", program_mkdir_main, rm_fname) < 0)
  671. io_putstr("error: too many processes\n");
  672. } else if (is_cmd_rmdir(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  673. if (process_create_arg("rmdir", program_rmdir_main, rm_fname) < 0)
  674. io_putstr("error: too many processes\n");
  675. } else if (is_cmd_rm(t->line, t->line_len, rm_fname, RM_FNAME_MAX)) {
  676. if (process_create_arg("rm", program_rm_main, rm_fname) < 0)
  677. io_putstr("error: too many processes\n");
  678. } else if (is_cmd_space(t->line, t->line_len)) {
  679. io_putstr("space: not available in windowed mode yet\n");
  680. } else if (is_cmd_display(t->line, t->line_len)) {
  681. if (process_create("display", program_display_main) < 0)
  682. io_putstr("error: too many processes\n");
  683. } else if (is_cmd_cpuinfo(t->line, t->line_len)) {
  684. if (process_create("cpuinfo", program_cpuinfo_main) < 0)
  685. io_putstr("error: too many processes\n");
  686. } else if (is_cmd_mouseinfo(t->line, t->line_len)) {
  687. if (process_create("mouseinfo", program_mouseinfo_main) < 0)
  688. io_putstr("error: too many processes\n");
  689. } else if (is_cmd_format(t->line, t->line_len, format_dev, FORMAT_DEV_MAX)) {
  690. program_format_main(format_dev);
  691. } else if (is_cmd_ps(t->line, t->line_len)) {
  692. process_ps();
  693. } else if (is_cmd_kill(t->line, t->line_len, &kill_pid)) {
  694. if (process_kill(kill_pid) < 0)
  695. io_putstr("kill: no such process\n");
  696. else
  697. io_putstr("killed\n");
  698. } else if (is_cmd_kexec(t->line, t->line_len)) {
  699. if (kexec_busy) {
  700. io_putstr("kexec: already receiving\n");
  701. } else {
  702. kexec_busy = 1;
  703. if (process_create("kexec", program_kexec_main) < 0) {
  704. kexec_busy = 0;
  705. io_putstr("error: too many processes\n");
  706. }
  707. }
  708. } else if (is_cmd_dbg(t->line, t->line_len)) {
  709. program_dbg_main();
  710. } else if (is_cmd_help(t->line, t->line_len)) {
  711. io_putstr("hello\n");
  712. io_putstr("reboot\n");
  713. io_putstr("poweroff\n");
  714. io_putstr("ls [/dev[/dir]]\n");
  715. io_putstr("cat /dev/file\n");
  716. io_putstr("save /dev/file <text>\n");
  717. io_putstr("rm /dev/file\n");
  718. io_putstr("mkdir /dev/dir\n");
  719. io_putstr("rmdir /dev/dir\n");
  720. io_putstr("run /dev/file\n");
  721. io_putstr("space\n");
  722. io_putstr("clock\n");
  723. io_putstr("colors\n");
  724. io_putstr("display\n");
  725. io_putstr("cpuinfo\n");
  726. io_putstr("lsusb\n");
  727. io_putstr("mouseinfo\n");
  728. io_putstr("storage\n");
  729. io_putstr("format <device>\n");
  730. io_putstr("ps\n");
  731. io_putstr("kill <pid>\n");
  732. io_putstr("kexec\n");
  733. io_putstr("dbg\n");
  734. io_putstr("serial test|recv\n");
  735. io_putstr("clear\n");
  736. } else if (is_cmd_clear(t->line, t->line_len)) {
  737. io_clear_screen();
  738. io_putstr(prompt_first);
  739. t->line_start = io_get_cursor();
  740. skip_prompt = 1;
  741. } else {
  742. char serial_arg[8];
  743. if (is_cmd_serial(t->line, t->line_len, serial_arg, sizeof(serial_arg))) {
  744. if (serial_arg[0] == 't' && serial_arg[1] == 'e' &&
  745. serial_arg[2] == 's' && serial_arg[3] == 't' &&
  746. serial_arg[4] == '\0') {
  747. if (process_create_arg("serial_loop", program_serial_loop_main, serial_arg) < 0)
  748. io_putstr("error: too many processes\n");
  749. } else if (serial_arg[0] == 'r' && serial_arg[1] == 'e' &&
  750. serial_arg[2] == 'c' && serial_arg[3] == 'v' &&
  751. serial_arg[4] == '\0') {
  752. program_serial_recv_main(serial_arg);
  753. } else {
  754. io_putstr("serial: usage serial test|recv\n");
  755. }
  756. } else {
  757. unsigned int i = 0;
  758. while (i < t->line_len && t->line[i] == ' ')
  759. i++;
  760. if (i < t->line_len)
  761. io_putstr("command not found\n");
  762. }
  763. }
  764. t->line_len = 0;
  765. t->line_pos = 0;
  766. t->line[0] = '\0';
  767. if (!skip_prompt) {
  768. io_putstr(prompt);
  769. t->line_start = io_get_cursor();
  770. }
  771. } else {
  772. unsigned char u = (unsigned char)k;
  773. if (u >= 0x20 && u < 0x7F && t->line_len < LINE_MAX - 1) {
  774. unsigned int i;
  775. t->history_browse = 0;
  776. for (i = t->line_len; i > t->line_pos; i--)
  777. t->line[i] = t->line[i - 1];
  778. t->line[t->line_pos] = (char)k;
  779. t->line_len++;
  780. t->line[t->line_len] = '\0';
  781. t->line_pos++;
  782. redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos - 1);
  783. }
  784. }
  785. } else {
  786. cpu_idle();
  787. }
  788. }
  789. }
  790. }
  791. void term_entry(void *arg)
  792. {
  793. struct term *t = (struct term *)arg;
  794. process_set_screen(&t->screen);
  795. term_run(t);
  796. }