terminal.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 "fbtext.h"
  5. #include "idle.h"
  6. #include "mouse.h"
  7. #include "process.h"
  8. #include "rtc.h"
  9. #include "terminal.h"
  10. #include "timer.h"
  11. extern void program_hello_main(void);
  12. extern void program_reboot_main(void);
  13. extern void program_poweroff_main(void);
  14. extern void program_ls_main(void);
  15. extern void program_cat_main(const char *filename);
  16. extern void program_run_main(const char *filename);
  17. extern void program_clock_main(void);
  18. extern void program_colors_main(void);
  19. extern void program_display_main(void);
  20. extern void program_cpuinfo_main(void);
  21. extern void program_mouseinfo_main(void);
  22. extern void program_power_main(const char *args);
  23. extern void disk_set_drive(unsigned int drive);
  24. extern unsigned int disk_get_drive(void);
  25. #define DATETIME_MS 1000
  26. #define LINE_MAX 64
  27. #define HISTORY_SIZE 32
  28. static unsigned int last_datetime_tick;
  29. static char datetime_buf[IO_DATETIME_LEN + 1];
  30. static char history[HISTORY_SIZE][LINE_MAX];
  31. static unsigned int history_count;
  32. static unsigned int history_next;
  33. static int cursor_visible;
  34. static unsigned int last_cursor_tick;
  35. static void redraw_datetime(void)
  36. {
  37. rtc_read_datetime(datetime_buf);
  38. {
  39. unsigned int i;
  40. for (i = 0; i < IO_DATETIME_LEN && datetime_buf[i]; i++)
  41. io_vga_write(IO_DATETIME_POS + i,
  42. (unsigned short)((0x0A << 8) | (unsigned char)datetime_buf[i]));
  43. }
  44. }
  45. static void update_datetime(void)
  46. {
  47. unsigned int t = get_ticks_ms();
  48. if (t - last_datetime_tick >= DATETIME_MS) {
  49. last_datetime_tick = t;
  50. redraw_datetime();
  51. }
  52. }
  53. static void update_cursor_blink(void)
  54. {
  55. unsigned int t = get_ticks_ms();
  56. if (t - last_cursor_tick >= CURSOR_MS) {
  57. last_cursor_tick = t;
  58. cursor_visible = !cursor_visible;
  59. }
  60. if (cursor_visible)
  61. io_cursor_show_block();
  62. else
  63. io_cursor_hide_block();
  64. }
  65. static const char banner[] __attribute__((section(".text"))) = "Nebs OS |o|";
  66. #define BANNER_LEN 12
  67. static const char prompt[] __attribute__((section(".text"))) = "\n> ";
  68. static const char prompt_first[] __attribute__((section(".text"))) = "> ";
  69. /* Draw row 0: banner left, datetime right. Call when terminal is active. */
  70. static void draw_header_row(void)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < BANNER_LEN && banner[i]; i++)
  74. io_vga_write(i, (unsigned short)((0x0A << 8) | (unsigned char)banner[i]));
  75. for (; i < IO_DATETIME_POS; i++)
  76. io_vga_write(i, (unsigned short)(0x0A << 8));
  77. redraw_datetime();
  78. fbtext_draw_rainbow_separator();
  79. }
  80. static int is_cmd_hello(const char *line, unsigned int line_len)
  81. {
  82. unsigned int i = 0;
  83. while (i < line_len && line[i] == ' ')
  84. i++;
  85. if (i + 5 > line_len)
  86. return 0;
  87. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'l' || line[i+4] != 'o')
  88. return 0;
  89. i += 5;
  90. while (i < line_len && line[i] == ' ')
  91. i++;
  92. return (i == line_len);
  93. }
  94. static int is_cmd_reboot(const char *line, unsigned int line_len)
  95. {
  96. unsigned int i = 0;
  97. while (i < line_len && line[i] == ' ')
  98. i++;
  99. if (i + 6 > line_len)
  100. return 0;
  101. if (line[i] != 'r' || line[i+1] != 'e' || line[i+2] != 'b' || line[i+3] != 'o' || line[i+4] != 'o' || line[i+5] != 't')
  102. return 0;
  103. i += 6;
  104. while (i < line_len && line[i] == ' ')
  105. i++;
  106. return (i == line_len);
  107. }
  108. static int is_cmd_poweroff(const char *line, unsigned int line_len)
  109. {
  110. unsigned int i = 0;
  111. while (i < line_len && line[i] == ' ')
  112. i++;
  113. if (i + 8 > line_len)
  114. return 0;
  115. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' ||
  116. line[i+4] != 'r' || line[i+5] != 'o' || line[i+6] != 'f' || line[i+7] != 'f')
  117. return 0;
  118. i += 8;
  119. while (i < line_len && line[i] == ' ')
  120. i++;
  121. return (i == line_len);
  122. }
  123. static int is_cmd_ls(const char *line, unsigned int line_len)
  124. {
  125. unsigned int i = 0;
  126. while (i < line_len && line[i] == ' ')
  127. i++;
  128. if (i + 2 > line_len)
  129. return 0;
  130. if (line[i] != 'l' || line[i+1] != 's')
  131. return 0;
  132. i += 2;
  133. while (i < line_len && line[i] == ' ')
  134. i++;
  135. return (i == line_len);
  136. }
  137. static int is_cmd_clock(const char *line, unsigned int line_len)
  138. {
  139. unsigned int i = 0;
  140. while (i < line_len && line[i] == ' ')
  141. i++;
  142. if (i + 5 > line_len)
  143. return 0;
  144. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'o' ||
  145. line[i+3] != 'c' || line[i+4] != 'k')
  146. return 0;
  147. i += 5;
  148. while (i < line_len && line[i] == ' ')
  149. i++;
  150. return (i == line_len);
  151. }
  152. static int is_cmd_colors(const char *line, unsigned int line_len)
  153. {
  154. unsigned int i = 0;
  155. while (i < line_len && line[i] == ' ')
  156. i++;
  157. if (i + 6 > line_len)
  158. return 0;
  159. if (line[i] != 'c' || line[i+1] != 'o' || line[i+2] != 'l' ||
  160. line[i+3] != 'o' || line[i+4] != 'r' || line[i+5] != 's')
  161. return 0;
  162. i += 6;
  163. while (i < line_len && line[i] == ' ')
  164. i++;
  165. return (i == line_len);
  166. }
  167. static int is_cmd_display(const char *line, unsigned int line_len)
  168. {
  169. unsigned int i = 0;
  170. while (i < line_len && line[i] == ' ')
  171. i++;
  172. if (i + 7 > line_len)
  173. return 0;
  174. if (line[i] != 'd' || line[i+1] != 'i' || line[i+2] != 's' || line[i+3] != 'p' ||
  175. line[i+4] != 'l' || line[i+5] != 'a' || line[i+6] != 'y')
  176. return 0;
  177. i += 7;
  178. while (i < line_len && line[i] == ' ')
  179. i++;
  180. return (i == line_len);
  181. }
  182. static int is_cmd_cpuinfo(const char *line, unsigned int line_len)
  183. {
  184. unsigned int i = 0;
  185. while (i < line_len && line[i] == ' ')
  186. i++;
  187. if (i + 7 > line_len)
  188. return 0;
  189. if (line[i] != 'c' || line[i+1] != 'p' || line[i+2] != 'u' || line[i+3] != 'i' ||
  190. line[i+4] != 'n' || line[i+5] != 'f' || line[i+6] != 'o')
  191. return 0;
  192. i += 7;
  193. while (i < line_len && line[i] == ' ')
  194. i++;
  195. return (i == line_len);
  196. }
  197. static int is_cmd_mouseinfo(const char *line, unsigned int line_len)
  198. {
  199. unsigned int i = 0;
  200. while (i < line_len && line[i] == ' ')
  201. i++;
  202. if (i + 9 > line_len)
  203. return 0;
  204. if (line[i] != 'm' || line[i+1] != 'o' || line[i+2] != 'u' ||
  205. line[i+3] != 's' || line[i+4] != 'e' || line[i+5] != 'i' ||
  206. line[i+6] != 'n' || line[i+7] != 'f' || line[i+8] != 'o')
  207. return 0;
  208. i += 9;
  209. while (i < line_len && line[i] == ' ')
  210. i++;
  211. return (i == line_len);
  212. }
  213. static int is_cmd_space(const char *line, unsigned int line_len)
  214. {
  215. unsigned int i = 0;
  216. while (i < line_len && line[i] == ' ')
  217. i++;
  218. if (i + 5 > line_len)
  219. return 0;
  220. if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
  221. line[i+3] != 'c' || line[i+4] != 'e')
  222. return 0;
  223. i += 5;
  224. while (i < line_len && line[i] == ' ')
  225. i++;
  226. return (i == line_len);
  227. }
  228. #define CAT_FNAME_MAX 64
  229. static int is_cmd_cat(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] != 'c' || line[i+1] != 'a' || line[i+2] != 't')
  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. static int is_cmd_run(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
  250. {
  251. unsigned int i = 0, j;
  252. while (i < line_len && line[i] == ' ')
  253. i++;
  254. if (i + 3 > line_len)
  255. return 0;
  256. if (line[i] != 'r' || line[i+1] != 'u' || line[i+2] != 'n')
  257. return 0;
  258. i += 3;
  259. while (i < line_len && line[i] == ' ')
  260. i++;
  261. if (i >= line_len)
  262. return 0;
  263. j = 0;
  264. while (i < line_len && line[i] != ' ' && j < buf_size - 1)
  265. filename_buf[j++] = line[i++];
  266. filename_buf[j] = '\0';
  267. return (j > 0);
  268. }
  269. static int is_cmd_drive(const char *line, unsigned int line_len, unsigned int *drive_num)
  270. {
  271. unsigned int i = 0;
  272. while (i < line_len && line[i] == ' ')
  273. i++;
  274. if (i + 5 > line_len)
  275. return 0;
  276. if (line[i] != 'd' || line[i+1] != 'r' || line[i+2] != 'i' || line[i+3] != 'v' || line[i+4] != 'e')
  277. return 0;
  278. i += 5;
  279. while (i < line_len && line[i] == ' ')
  280. i++;
  281. if (i >= line_len || line[i] < '0' || line[i] > '1')
  282. return 0;
  283. *drive_num = (unsigned int)(line[i] - '0');
  284. i++;
  285. while (i < line_len && line[i] == ' ')
  286. i++;
  287. return (i == line_len);
  288. }
  289. static int is_cmd_help(const char *line, unsigned int line_len)
  290. {
  291. unsigned int i = 0;
  292. while (i < line_len && line[i] == ' ')
  293. i++;
  294. if (i + 4 > line_len)
  295. return 0;
  296. if (line[i] != 'h' || line[i+1] != 'e' || line[i+2] != 'l' || line[i+3] != 'p')
  297. return 0;
  298. i += 4;
  299. while (i < line_len && line[i] == ' ')
  300. i++;
  301. return (i == line_len);
  302. }
  303. static int is_cmd_clear(const char *line, unsigned int line_len)
  304. {
  305. unsigned int i = 0;
  306. while (i < line_len && line[i] == ' ')
  307. i++;
  308. if (i + 5 > line_len)
  309. return 0;
  310. if (line[i] != 'c' || line[i+1] != 'l' || line[i+2] != 'e' ||
  311. line[i+3] != 'a' || line[i+4] != 'r')
  312. return 0;
  313. i += 5;
  314. while (i < line_len && line[i] == ' ')
  315. i++;
  316. return (i == line_len);
  317. }
  318. static int is_cmd_ps(const char *line, unsigned int line_len)
  319. {
  320. unsigned int i = 0;
  321. while (i < line_len && line[i] == ' ')
  322. i++;
  323. if (i + 2 > line_len)
  324. return 0;
  325. if (line[i] != 'p' || line[i+1] != 's')
  326. return 0;
  327. i += 2;
  328. while (i < line_len && line[i] == ' ')
  329. i++;
  330. return (i == line_len);
  331. }
  332. static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pid_out)
  333. {
  334. unsigned int i = 0, num = 0;
  335. int has_digit = 0;
  336. while (i < line_len && line[i] == ' ')
  337. i++;
  338. if (i + 4 > line_len)
  339. return 0;
  340. if (line[i] != 'k' || line[i+1] != 'i' || line[i+2] != 'l' || line[i+3] != 'l')
  341. return 0;
  342. i += 4;
  343. while (i < line_len && line[i] == ' ')
  344. i++;
  345. while (i < line_len && line[i] >= '0' && line[i] <= '9') {
  346. num = num * 10 + (unsigned int)(line[i] - '0');
  347. has_digit = 1;
  348. i++;
  349. }
  350. if (!has_digit)
  351. return 0;
  352. while (i < line_len && line[i] == ' ')
  353. i++;
  354. if (i != line_len)
  355. return 0;
  356. *pid_out = num;
  357. return 1;
  358. }
  359. #define POWER_ARGS_MAX 32
  360. static int is_cmd_power(const char *line, unsigned int line_len, char *args_buf, unsigned int buf_size)
  361. {
  362. unsigned int i = 0, j = 0;
  363. while (i < line_len && line[i] == ' ')
  364. i++;
  365. if (i + 5 > line_len)
  366. return 0;
  367. if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' || line[i+4] != 'r')
  368. return 0;
  369. i += 5;
  370. while (i < line_len && line[i] == ' ')
  371. i++;
  372. if (i == line_len) {
  373. if (buf_size > 0)
  374. args_buf[0] = '\0';
  375. return 1;
  376. }
  377. while (i < line_len && j + 1 < buf_size)
  378. args_buf[j++] = line[i++];
  379. while (j > 0 && args_buf[j - 1] == ' ')
  380. j--;
  381. args_buf[j] = '\0';
  382. while (i < line_len && line[i] == ' ')
  383. i++;
  384. return (i == line_len);
  385. }
  386. /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
  387. static void redraw_line_from(const char *line, unsigned int line_len,
  388. unsigned int line_pos, unsigned int line_start, unsigned int from)
  389. {
  390. unsigned int i;
  391. io_set_cursor(line_start + from);
  392. for (i = from; i < line_len; i++)
  393. io_putchar(line[i]);
  394. io_putchar(' ');
  395. io_set_cursor(line_start + line_pos);
  396. }
  397. /* Redraw entire line and clear through clear_through (so leftover chars are erased). */
  398. static void redraw_line_clear(const char *line, unsigned int line_len,
  399. unsigned int line_pos, unsigned int line_start, unsigned int clear_through)
  400. {
  401. unsigned int i;
  402. io_set_cursor(line_start);
  403. for (i = 0; i < line_len; i++)
  404. io_putchar(line[i]);
  405. for (i = line_len; i <= clear_through; i++)
  406. io_putchar(' ');
  407. io_set_cursor(line_start + line_pos);
  408. }
  409. /* Copy history entry (1 = newest) into line, set line_len/line_pos, redraw from start. */
  410. static void history_load_into_line(char *line, unsigned int *line_len, unsigned int *line_pos,
  411. unsigned int line_start, unsigned int browse)
  412. {
  413. unsigned int slot, len, old_len;
  414. const char *src;
  415. if (browse < 1 || browse > history_count)
  416. return;
  417. old_len = *line_len;
  418. slot = (history_next + HISTORY_SIZE - browse) % HISTORY_SIZE;
  419. src = history[slot];
  420. for (len = 0; len < LINE_MAX - 1 && src[len]; len++)
  421. line[len] = src[len];
  422. line[len] = '\0';
  423. *line_len = len;
  424. *line_pos = len;
  425. redraw_line_clear(line, len, len, line_start, old_len > len ? old_len : len);
  426. }
  427. /* Push current line to history if non-empty (and not duplicate of newest). */
  428. static void history_push(const char *line, unsigned int line_len)
  429. {
  430. unsigned int i, slot;
  431. int all_spaces = 1;
  432. if (line_len == 0)
  433. return;
  434. for (i = 0; i < line_len; i++) {
  435. if (line[i] != ' ') {
  436. all_spaces = 0;
  437. break;
  438. }
  439. }
  440. if (all_spaces)
  441. return;
  442. if (history_count > 0) {
  443. slot = (history_next + HISTORY_SIZE - 1) % HISTORY_SIZE;
  444. for (i = 0; i < line_len && i < LINE_MAX - 1 && history[slot][i]; i++) {
  445. if (history[slot][i] != line[i])
  446. break;
  447. }
  448. if (i == line_len && history[slot][i] == '\0')
  449. return; /* duplicate of newest */
  450. }
  451. slot = history_next;
  452. for (i = 0; i < line_len && i < LINE_MAX - 1; i++)
  453. history[slot][i] = line[i];
  454. history[slot][i] = '\0';
  455. history_next = (history_next + 1) % HISTORY_SIZE;
  456. if (history_count < HISTORY_SIZE)
  457. history_count++;
  458. }
  459. void terminal_run(void)
  460. {
  461. static char line[LINE_MAX];
  462. static unsigned int line_len;
  463. static unsigned int line_pos; /* cursor index in line, 0..line_len */
  464. static unsigned int line_start; /* VGA position of first char of line */
  465. static unsigned int history_browse; /* 0 = not browsing, 1 = newest, 2 = older, ... */
  466. last_cursor_tick = get_ticks_ms();
  467. last_datetime_tick = get_ticks_ms();
  468. cursor_visible = 1;
  469. line_len = 0;
  470. line_pos = 0;
  471. line[0] = '\0';
  472. history_browse = 0;
  473. io_drain_keyboard();
  474. draw_header_row();
  475. io_putstr(prompt_first);
  476. line_start = io_get_cursor();
  477. for (;;) {
  478. mouse_poll_update();
  479. update_datetime();
  480. update_cursor_blink();
  481. {
  482. int k = io_poll_key();
  483. if (k) {
  484. if (k == IO_KEY_LEFT) {
  485. if (line_pos > 0) {
  486. line_pos--;
  487. io_set_cursor(line_start + line_pos);
  488. }
  489. } else if (k == IO_KEY_RIGHT) {
  490. if (line_pos < line_len) {
  491. line_pos++;
  492. io_set_cursor(line_start + line_pos);
  493. }
  494. } else if (k == IO_KEY_UP) {
  495. if (history_count > 0) {
  496. if (history_browse < history_count)
  497. history_browse++;
  498. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  499. }
  500. } else if (k == IO_KEY_DOWN) {
  501. if (history_browse > 0) {
  502. history_browse--;
  503. if (history_browse == 0) {
  504. {
  505. unsigned int clear_len = line_len;
  506. line_len = 0;
  507. line_pos = 0;
  508. line[0] = '\0';
  509. redraw_line_clear(line, 0, 0, line_start, clear_len);
  510. }
  511. } else {
  512. history_load_into_line(line, &line_len, &line_pos, line_start, history_browse);
  513. }
  514. }
  515. } else if (k == '\b') {
  516. if (line_pos > 0) {
  517. unsigned int i;
  518. line_pos--;
  519. for (i = line_pos; i < line_len; i++)
  520. line[i] = line[i + 1];
  521. line_len--;
  522. line[line_len] = '\0';
  523. redraw_line_from(line, line_len, line_pos, line_start, line_pos);
  524. }
  525. } else if (k == '\n') {
  526. static char cat_fname[CAT_FNAME_MAX];
  527. static char power_args[POWER_ARGS_MAX];
  528. unsigned int drive_sel, kill_pid;
  529. int skip_prompt = 0;
  530. io_putchar('\n');
  531. history_push(line, line_len);
  532. history_browse = 0;
  533. if (is_cmd_hello(line, line_len)) {
  534. if (process_create("hello", program_hello_main) < 0)
  535. io_putstr("error: too many processes\n");
  536. } else if (is_cmd_reboot(line, line_len)) {
  537. program_reboot_main();
  538. } else if (is_cmd_poweroff(line, line_len)) {
  539. program_poweroff_main();
  540. } else if (is_cmd_ls(line, line_len)) {
  541. if (process_create("ls", program_ls_main) < 0)
  542. io_putstr("error: too many processes\n");
  543. } else if (is_cmd_clock(line, line_len)) {
  544. if (process_create("clock", program_clock_main) < 0)
  545. io_putstr("error: too many processes\n");
  546. } else if (is_cmd_colors(line, line_len)) {
  547. if (process_create("colors", program_colors_main) < 0)
  548. io_putstr("error: too many processes\n");
  549. } else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  550. if (process_create_arg("cat", program_cat_main, cat_fname) < 0)
  551. io_putstr("error: too many processes\n");
  552. } else if (is_cmd_run(line, line_len, cat_fname, CAT_FNAME_MAX)) {
  553. if (process_create_arg("run", program_run_main, cat_fname) < 0)
  554. io_putstr("error: too many processes\n");
  555. } else if (is_cmd_space(line, line_len)) {
  556. if (process_create_arg("space", program_run_main, "space.bin") < 0)
  557. io_putstr("error: too many processes\n");
  558. } else if (is_cmd_display(line, line_len)) {
  559. if (process_create("display", program_display_main) < 0)
  560. io_putstr("error: too many processes\n");
  561. } else if (is_cmd_cpuinfo(line, line_len)) {
  562. if (process_create("cpuinfo", program_cpuinfo_main) < 0)
  563. io_putstr("error: too many processes\n");
  564. } else if (is_cmd_mouseinfo(line, line_len)) {
  565. if (process_create("mouseinfo", program_mouseinfo_main) < 0)
  566. io_putstr("error: too many processes\n");
  567. } else if (is_cmd_power(line, line_len, power_args, POWER_ARGS_MAX)) {
  568. if (process_create_arg("power", program_power_main, power_args) < 0)
  569. io_putstr("error: too many processes\n");
  570. } else if (is_cmd_drive(line, line_len, &drive_sel)) {
  571. disk_set_drive(drive_sel);
  572. io_putstr("drive ");
  573. io_putchar((char)('0' + disk_get_drive()));
  574. io_putstr(" selected\n");
  575. } else if (is_cmd_ps(line, line_len)) {
  576. process_ps();
  577. } else if (is_cmd_kill(line, line_len, &kill_pid)) {
  578. if (process_kill(kill_pid) < 0)
  579. io_putstr("kill: no such process\n");
  580. else
  581. io_putstr("killed\n");
  582. } else if (is_cmd_help(line, line_len)) {
  583. io_putstr("hello reboot poweroff\n");
  584. io_putstr("ls cat <file>\n");
  585. io_putstr("run <file> space\n");
  586. io_putstr("clock colors\n");
  587. io_putstr("display cpuinfo\n");
  588. io_putstr("mouseinfo\n");
  589. io_putstr("power [info|set <low|balanced|max>]\n");
  590. io_putstr("drive <0|1>\n");
  591. io_putstr("ps kill <pid>\n");
  592. io_putstr("help clear\n");
  593. } else if (is_cmd_clear(line, line_len)) {
  594. io_clear_screen();
  595. draw_header_row();
  596. io_set_cursor(IO_CONTENT_OFFSET);
  597. io_putstr(prompt_first);
  598. line_start = io_get_cursor();
  599. skip_prompt = 1;
  600. } else {
  601. unsigned int i = 0;
  602. while (i < line_len && line[i] == ' ')
  603. i++;
  604. if (i < line_len)
  605. io_putstr("command not found\n");
  606. }
  607. line_len = 0;
  608. line_pos = 0;
  609. line[0] = '\0';
  610. if (!skip_prompt) {
  611. io_putstr(prompt);
  612. line_start = io_get_cursor();
  613. }
  614. if (io_did_scroll())
  615. redraw_datetime();
  616. } else {
  617. {
  618. unsigned char u = (unsigned char)k;
  619. if (u >= 0x20 && u < 0x7F && line_len < LINE_MAX - 1) {
  620. unsigned int i;
  621. history_browse = 0;
  622. for (i = line_len; i > line_pos; i--)
  623. line[i] = line[i - 1];
  624. line[line_pos] = (char)k;
  625. line_len++;
  626. line[line_len] = '\0';
  627. line_pos++;
  628. redraw_line_from(line, line_len, line_pos, line_start, line_pos - 1);
  629. }
  630. }
  631. }
  632. } else {
  633. cpu_idle();
  634. }
  635. }
  636. }
  637. }