rline.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2015-2018 K. Lange
  5. *
  6. * rline - a line reading library.
  7. *
  8. * Implements an interface similar to readline, providing more
  9. * complex line editing than what the raw tty interface supplies.
  10. */
  11. #define _POSIX_C_SOURCE 1
  12. #define _XOPEN_SOURCE 500
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <termios.h>
  18. #include <toaru/kbd.h>
  19. #include <toaru/rline.h>
  20. static struct termios old;
  21. static void set_unbuffered() {
  22. tcgetattr(fileno(stdin), &old);
  23. struct termios new = old;
  24. new.c_lflag &= (~ICANON & ~ECHO);
  25. tcsetattr(fileno(stdin), TCSAFLUSH, &new);
  26. }
  27. static void set_buffered() {
  28. tcsetattr(fileno(stdin), TCSAFLUSH, &old);
  29. }
  30. void rline_redraw(rline_context_t * context) {
  31. if (context->quiet) return;
  32. printf("\033[u%s\033[K", context->buffer);
  33. for (int i = context->offset; i < context->collected; ++i) {
  34. printf("\033[D");
  35. }
  36. fflush(stdout);
  37. }
  38. void rline_redraw_clean(rline_context_t * context) {
  39. if (context->quiet) return;
  40. printf("\033[u%s", context->buffer);
  41. for (int i = context->offset; i < context->collected; ++i) {
  42. printf("\033[D");
  43. }
  44. fflush(stdout);
  45. }
  46. char * rline_history[RLINE_HISTORY_ENTRIES];
  47. int rline_history_count = 0;
  48. int rline_history_offset = 0;
  49. int rline_scroll = 0;
  50. char * rline_exit_string = "exit\n";
  51. static char rline_temp[1024];
  52. void rline_history_insert(char * str) {
  53. if (str[strlen(str)-1] == '\n') {
  54. str[strlen(str)-1] = '\0';
  55. }
  56. if (rline_history_count) {
  57. if (!strcmp(str, rline_history_prev(1))) {
  58. free(str);
  59. return;
  60. }
  61. }
  62. if (rline_history_count == RLINE_HISTORY_ENTRIES) {
  63. free(rline_history[rline_history_offset]);
  64. rline_history[rline_history_offset] = str;
  65. rline_history_offset = (rline_history_offset + 1) % RLINE_HISTORY_ENTRIES;
  66. } else {
  67. rline_history[rline_history_count] = str;
  68. rline_history_count++;
  69. }
  70. }
  71. void rline_history_append_line(char * str) {
  72. if (rline_history_count) {
  73. char ** s = &rline_history[(rline_history_count - 1 + rline_history_offset) % RLINE_HISTORY_ENTRIES];
  74. char * c = malloc(strlen(*s) + strlen(str) + 2);
  75. sprintf(c, "%s\n%s", *s, str);
  76. if (c[strlen(c)-1] == '\n') {
  77. c[strlen(c)-1] = '\0';
  78. }
  79. free(*s);
  80. *s = c;
  81. } else {
  82. /* wat */
  83. }
  84. }
  85. char * rline_history_get(int item) {
  86. return rline_history[(item + rline_history_offset) % RLINE_HISTORY_ENTRIES];
  87. }
  88. char * rline_history_prev(int item) {
  89. return rline_history_get(rline_history_count - item);
  90. }
  91. void rline_reverse_search(rline_context_t * context) {
  92. char input[512] = {0};
  93. int collected = 0;
  94. int start_at = 0;
  95. int changed = 0;
  96. fprintf(stderr, "\033[G\033[0m\033[s");
  97. fflush(stderr);
  98. key_event_state_t kbd_state = {0};
  99. char * match = "";
  100. int match_index = 0;
  101. while (1) {
  102. /* Find matches */
  103. try_rev_search_again:
  104. if (collected && changed) {
  105. match = "";
  106. match_index = 0;
  107. for (int i = start_at; i < rline_history_count; i++) {
  108. char * c = rline_history_prev(i+1);
  109. if (strstr(c, input)) {
  110. match = c;
  111. match_index = i;
  112. break;
  113. }
  114. }
  115. if (!strcmp(match,"")) {
  116. if (start_at) {
  117. start_at = 0;
  118. goto try_rev_search_again;
  119. }
  120. collected--;
  121. input[collected] = '\0';
  122. if (collected) {
  123. goto try_rev_search_again;
  124. }
  125. }
  126. }
  127. fprintf(stderr, "\033[u(reverse-i-search)`%s': %s\033[K", input, match);
  128. fflush(stderr);
  129. changed = 0;
  130. uint32_t key_sym = kbd_key(&kbd_state, fgetc(stdin));
  131. switch (key_sym) {
  132. case KEY_NONE:
  133. break;
  134. case KEY_BACKSPACE:
  135. case 0x7F: /* delete */
  136. if (collected > 0) {
  137. collected--;
  138. input[collected] = '\0';
  139. start_at = 0;
  140. changed = 1;
  141. }
  142. break;
  143. case KEY_CTRL_C:
  144. printf("^C\n");
  145. return;
  146. case KEY_CTRL_R:
  147. start_at = match_index + 1;
  148. changed = 1;
  149. break;
  150. case KEY_ESCAPE:
  151. case KEY_ARROW_LEFT:
  152. case KEY_ARROW_RIGHT:
  153. context->cancel = 1;
  154. case '\n':
  155. memcpy(context->buffer, match, strlen(match) + 1);
  156. context->collected = strlen(match);
  157. context->offset = context->collected;
  158. if (!context->quiet && context->callbacks->redraw_prompt) {
  159. fprintf(stderr, "\033[G\033[K");
  160. context->callbacks->redraw_prompt(context);
  161. }
  162. fprintf(stderr, "\033[s");
  163. rline_redraw_clean(context);
  164. if (key_sym == '\n' && !context->quiet) {
  165. fprintf(stderr, "\n");
  166. }
  167. return;
  168. default:
  169. if (key_sym < KEY_NORMAL_MAX) {
  170. input[collected] = (char)key_sym;
  171. collected++;
  172. input[collected] = '\0';
  173. start_at = 0;
  174. changed = 1;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. static void history_previous(rline_context_t * context) {
  181. if (rline_scroll == 0) {
  182. memcpy(rline_temp, context->buffer, strlen(context->buffer) + 1);
  183. }
  184. if (rline_scroll < rline_history_count) {
  185. rline_scroll++;
  186. for (int i = 0; i < (int)strlen(context->buffer); ++i) {
  187. printf("\010 \010");
  188. }
  189. char * h = rline_history_prev(rline_scroll);
  190. memcpy(context->buffer, h, strlen(h) + 1);
  191. printf("\033[u%s\033[K", h);
  192. fflush(stdout);
  193. }
  194. context->collected = strlen(context->buffer);
  195. context->offset = context->collected;
  196. }
  197. static void history_next(rline_context_t * context) {
  198. if (rline_scroll > 1) {
  199. rline_scroll--;
  200. for (int i = 0; i < (int)strlen(context->buffer); ++i) {
  201. printf("\010 \010");
  202. }
  203. char * h = rline_history_prev(rline_scroll);
  204. memcpy(context->buffer, h, strlen(h) + 1);
  205. printf("%s", h);
  206. fflush(stdout);
  207. } else if (rline_scroll == 1) {
  208. for (int i = 0; i < (int)strlen(context->buffer); ++i) {
  209. printf("\010 \010");
  210. }
  211. rline_scroll = 0;
  212. memcpy(context->buffer, rline_temp, strlen(rline_temp) + 1);
  213. printf("\033[u%s\033[K", context->buffer);
  214. fflush(stdout);
  215. }
  216. context->collected = strlen(context->buffer);
  217. context->offset = context->collected;
  218. }
  219. /**
  220. * Insert characters at the current cursor offset.
  221. */
  222. void rline_insert(rline_context_t * context, const char * what) {
  223. size_t insertion_length = strlen(what);
  224. if (context->collected + (int)insertion_length > context->requested) {
  225. insertion_length = context->requested - context->collected;
  226. }
  227. /* Move */
  228. memmove(&context->buffer[context->offset + insertion_length], &context->buffer[context->offset], context->collected - context->offset);
  229. memcpy(&context->buffer[context->offset], what, insertion_length);
  230. context->collected += insertion_length;
  231. context->offset += insertion_length;
  232. }
  233. static rline_callbacks_t _rline_null_callbacks = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
  234. int rline(char * buffer, int buf_size, rline_callbacks_t * callbacks) {
  235. /* Initialize context */
  236. rline_context_t context = {
  237. buffer,
  238. callbacks,
  239. 0,
  240. buf_size,
  241. 0,
  242. 0,
  243. 0,
  244. 0,
  245. 0,
  246. };
  247. if (!callbacks) {
  248. callbacks = &_rline_null_callbacks;
  249. }
  250. set_unbuffered();
  251. printf("\033[s");
  252. fflush(stdout);
  253. key_event_state_t kbd_state = {0};
  254. /* Read keys */
  255. while ((context.collected < context.requested) && (!context.newline)) {
  256. uint32_t key_sym = kbd_key(&kbd_state, fgetc(stdin));
  257. if (key_sym == KEY_NONE) continue;
  258. if (key_sym != '\t') context.tabbed = 0;
  259. switch (key_sym) {
  260. case KEY_CTRL_C:
  261. printf("^C\n");
  262. context.buffer[0] = '\0';
  263. set_buffered();
  264. return 0;
  265. case KEY_CTRL_R:
  266. if (callbacks->rev_search) {
  267. callbacks->rev_search(&context);
  268. } else {
  269. rline_reverse_search(&context);
  270. }
  271. if (context.cancel) {
  272. context.cancel = 0;
  273. continue;
  274. } else {
  275. set_buffered();
  276. return context.collected;
  277. }
  278. case KEY_ARROW_UP:
  279. case KEY_CTRL_P:
  280. if (callbacks->key_up) {
  281. callbacks->key_up(&context);
  282. } else {
  283. history_previous(&context);
  284. }
  285. continue;
  286. case KEY_ARROW_DOWN:
  287. case KEY_CTRL_N:
  288. if (callbacks->key_down) {
  289. callbacks->key_down(&context);
  290. } else {
  291. history_next(&context);
  292. }
  293. continue;
  294. case KEY_CTRL_ARROW_RIGHT:
  295. while (context.offset < context.collected && context.buffer[context.offset] == ' ') {
  296. context.offset++;
  297. printf("\033[C");
  298. }
  299. while (context.offset < context.collected) {
  300. context.offset++;
  301. printf("\033[C");
  302. if (context.buffer[context.offset] == ' ') break;
  303. }
  304. fflush(stdout);
  305. continue;
  306. case KEY_CTRL_ARROW_LEFT:
  307. if (context.offset == 0) continue;
  308. context.offset--;
  309. printf("\033[D");
  310. while (context.offset && context.buffer[context.offset] == ' ') {
  311. context.offset--;
  312. printf("\033[D");
  313. }
  314. while (context.offset > 0) {
  315. if (context.buffer[context.offset-1] == ' ') break;
  316. context.offset--;
  317. printf("\033[D");
  318. }
  319. fflush(stdout);
  320. continue;
  321. case KEY_ARROW_RIGHT:
  322. if (callbacks->key_right) {
  323. callbacks->key_right(&context);
  324. } else {
  325. if (context.offset < context.collected) {
  326. printf("\033[C");
  327. fflush(stdout);
  328. context.offset++;
  329. }
  330. }
  331. continue;
  332. case KEY_ARROW_LEFT:
  333. if (callbacks->key_left) {
  334. callbacks->key_left(&context);
  335. } else {
  336. if (context.offset > 0) {
  337. printf("\033[D");
  338. fflush(stdout);
  339. context.offset--;
  340. }
  341. }
  342. continue;
  343. case KEY_CTRL_A:
  344. case KEY_HOME:
  345. while (context.offset > 0) {
  346. printf("\033[D");
  347. context.offset--;
  348. }
  349. fflush(stdout);
  350. continue;
  351. case KEY_CTRL_E:
  352. case KEY_END:
  353. while (context.offset < context.collected) {
  354. printf("\033[C");
  355. context.offset++;
  356. }
  357. fflush(stdout);
  358. continue;
  359. case KEY_CTRL_K:
  360. context.collected = context.offset;
  361. printf("\033[K");
  362. fflush(stdout);
  363. continue;
  364. case KEY_CTRL_D:
  365. if (context.collected == 0) {
  366. printf(rline_exit_string);
  367. sprintf(context.buffer, rline_exit_string);
  368. set_buffered();
  369. return strlen(context.buffer);
  370. }
  371. /* Intentional fallthrough */
  372. case KEY_DEL:
  373. if (context.collected) {
  374. if (context.offset == context.collected) {
  375. continue;
  376. }
  377. int remaining = context.collected - context.offset;
  378. for (int i = 1; i < remaining; ++i) {
  379. printf("%c", context.buffer[context.offset + i]);
  380. context.buffer[context.offset + i - 1] = context.buffer[context.offset + i];
  381. }
  382. printf(" ");
  383. for (int i = 0; i < remaining; ++i) {
  384. printf("\033[D");
  385. }
  386. context.collected--;
  387. fflush(stdout);
  388. }
  389. continue;
  390. case KEY_BACKSPACE:
  391. case 0x7F: /* delete */
  392. if (context.collected) {
  393. int should_redraw = 0;
  394. if (!context.offset) {
  395. continue;
  396. }
  397. printf("\010 \010");
  398. if (context.buffer[context.offset-1] == '\t') {
  399. should_redraw = 1;
  400. }
  401. if (context.offset != context.collected) {
  402. int remaining = context.collected - context.offset;
  403. for (int i = 0; i < remaining; ++i) {
  404. printf("%c", context.buffer[context.offset + i]);
  405. context.buffer[context.offset + i - 1] = context.buffer[context.offset + i];
  406. }
  407. printf(" ");
  408. for (int i = 0; i < remaining + 1; ++i) {
  409. printf("\033[D");
  410. }
  411. context.offset--;
  412. context.collected--;
  413. } else {
  414. context.buffer[--context.collected] = '\0';
  415. context.offset--;
  416. }
  417. if (should_redraw) {
  418. rline_redraw_clean(&context);
  419. }
  420. fflush(stdout);
  421. }
  422. continue;
  423. case KEY_CTRL_L: /* ^L: Clear Screen, redraw prompt and buffer */
  424. printf("\033[H\033[2J");
  425. fflush(stdout);
  426. /* Flush before yielding control to potentially foreign environment. */
  427. if (callbacks->redraw_prompt) {
  428. callbacks->redraw_prompt(&context);
  429. }
  430. printf("\033[s");
  431. rline_redraw_clean(&context);
  432. continue;
  433. case KEY_CTRL_W:
  434. /*
  435. * Erase word before cursor.
  436. * If the character before the cursor is a space, delete it.
  437. * Continue deleting until the previous character is a space.
  438. */
  439. if (context.collected) {
  440. if (!context.offset) {
  441. continue;
  442. }
  443. do {
  444. printf("\010 \010");
  445. if (context.offset != context.collected) {
  446. int remaining = context.collected - context.offset;
  447. for (int i = 0; i < remaining; ++i) {
  448. printf("%c", context.buffer[context.offset + i]);
  449. context.buffer[context.offset + i - 1] = context.buffer[context.offset + i];
  450. }
  451. printf(" ");
  452. for (int i = 0; i < remaining + 1; ++i) {
  453. printf("\033[D");
  454. }
  455. context.offset--;
  456. context.collected--;
  457. } else {
  458. context.buffer[--context.collected] = '\0';
  459. context.offset--;
  460. }
  461. } while ((context.offset) && (context.buffer[context.offset-1] != ' '));
  462. fflush(stdout);
  463. }
  464. continue;
  465. case '\t':
  466. if (callbacks->tab_complete) {
  467. callbacks->tab_complete(&context);
  468. }
  469. continue;
  470. case '\n':
  471. while (context.offset < context.collected) {
  472. printf("\033[C");
  473. context.offset++;
  474. }
  475. if (context.collected < context.requested) {
  476. context.buffer[context.collected] = '\n';
  477. context.buffer[++context.collected] = '\0';
  478. context.offset++;
  479. }
  480. printf("\n");
  481. fflush(stdout);
  482. context.newline = 1;
  483. continue;
  484. }
  485. if (context.offset != context.collected) {
  486. for (int i = context.collected; i > context.offset; --i) {
  487. context.buffer[i] = context.buffer[i-1];
  488. }
  489. if (context.collected < context.requested) {
  490. context.buffer[context.offset] = (char)key_sym;
  491. context.buffer[++context.collected] = '\0';
  492. context.offset++;
  493. }
  494. for (int i = context.offset - 1; i < context.collected; ++i) {
  495. printf("%c", context.buffer[i]);
  496. }
  497. for (int i = context.offset; i < context.collected; ++i) {
  498. printf("\033[D");
  499. }
  500. fflush(stdout);
  501. } else {
  502. printf("%c", (char)key_sym);
  503. if (context.collected < context.requested) {
  504. context.buffer[context.collected] = (char)key_sym;
  505. context.buffer[++context.collected] = '\0';
  506. context.offset++;
  507. }
  508. fflush(stdout);
  509. }
  510. }
  511. /* Cap that with a null */
  512. context.buffer[context.collected] = '\0';
  513. set_buffered();
  514. return context.collected;
  515. }
  516. static char * last_prompt = NULL;
  517. static void redraw_prompt(rline_context_t * c) {
  518. (void)c;
  519. printf("%s", last_prompt);
  520. fflush(stdout);
  521. return;
  522. }
  523. static void insert_tab(rline_context_t * c) {
  524. rline_insert(c, "\t");
  525. rline_redraw_clean(c);
  526. }
  527. void * rline_for_python(void * _stdin, void * _stdout, char * prompt) {
  528. last_prompt = prompt;
  529. rline_callbacks_t callbacks = {
  530. insert_tab, redraw_prompt, NULL,
  531. NULL, NULL, NULL, NULL, NULL
  532. };
  533. redraw_prompt(NULL);
  534. char * buf = malloc(1024);
  535. memset(buf, 0, 1024);
  536. rline(buf, 1024, &callbacks);
  537. rline_history_insert(strdup(buf));
  538. rline_scroll = 0;
  539. return buf;
  540. }