terminal.c 19 KB

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