terminal.c 19 KB

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