terminal.c 30 KB

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