tinyrl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * tinyrl.c
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <faux/faux.h>
  12. #include <faux/str.h>
  13. #include "private.h"
  14. #define LINE_CHUNK 80
  15. tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
  16. const char *hist_fname, size_t hist_stifle)
  17. {
  18. tinyrl_t *tinyrl = NULL;
  19. int i = 0;
  20. tinyrl = faux_zmalloc(sizeof(tinyrl_t));
  21. if (!tinyrl)
  22. return NULL;
  23. // Line
  24. faux_bzero(&tinyrl->line, sizeof(tinyrl->line));
  25. tinyrl_line_extend(tinyrl, LINE_CHUNK);
  26. // Last line
  27. tinyrl_reset_line_state(tinyrl);
  28. // Input processing vars
  29. tinyrl->utf8_cont = 0;
  30. tinyrl->esc_cont = BOOL_FALSE;
  31. tinyrl->esc_seq[0] = '\0';
  32. tinyrl->esc_p = tinyrl->esc_seq;
  33. // Prompt
  34. tinyrl_set_prompt(tinyrl, "> ");
  35. // Key handlers
  36. for (i = 0; i < NUM_HANDLERS; i++) {
  37. tinyrl->handlers[i] = tinyrl_key_default;
  38. }
  39. tinyrl->handlers[KEY_CR] = tinyrl_key_crlf;
  40. tinyrl->handlers[KEY_LF] = tinyrl_key_crlf;
  41. tinyrl->handlers[KEY_ETX] = tinyrl_key_interrupt;
  42. tinyrl->handlers[KEY_DEL] = tinyrl_key_backspace;
  43. tinyrl->handlers[KEY_BS] = tinyrl_key_backspace;
  44. tinyrl->handlers[KEY_EOT] = tinyrl_key_delete;
  45. tinyrl->handlers[KEY_FF] = tinyrl_key_clear_screen;
  46. tinyrl->handlers[KEY_NAK] = tinyrl_key_erase_line;
  47. tinyrl->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  48. tinyrl->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  49. tinyrl->handlers[KEY_VT] = tinyrl_key_kill;
  50. tinyrl->handlers[KEY_EM] = tinyrl_key_yank;
  51. tinyrl->handlers[KEY_HT] = tinyrl_key_tab;
  52. tinyrl->handlers[KEY_ETB] = tinyrl_key_backword;
  53. tinyrl->max_line_length = 0;
  54. tinyrl->buffer = NULL;
  55. tinyrl->buffer_size = 0;
  56. tinyrl->done = BOOL_FALSE;
  57. tinyrl->completion_over = BOOL_FALSE;
  58. tinyrl->attempted_completion_function = NULL;
  59. tinyrl->hotkey_fn = NULL;
  60. tinyrl->state = 0;
  61. tinyrl->kill_string = NULL;
  62. tinyrl->echo_char = '\0';
  63. tinyrl->echo_enabled = BOOL_TRUE;
  64. tinyrl->utf8 = BOOL_TRUE;
  65. tinyrl->busy = BOOL_FALSE;
  66. // VT100 terminal
  67. tinyrl->term = vt100_new(istream, ostream);
  68. // To save terminal settings
  69. tinyrl_set_istream(tinyrl, istream);
  70. tinyrl->width = vt100_width(tinyrl->term);
  71. // History object
  72. tinyrl->hist = hist_new(hist_fname, hist_stifle);
  73. tinyrl_hist_restore(tinyrl);
  74. tty_raw_mode(tinyrl);
  75. return tinyrl;
  76. }
  77. void tinyrl_free(tinyrl_t *tinyrl)
  78. {
  79. assert(tinyrl);
  80. if (!tinyrl)
  81. return;
  82. tty_restore_mode(tinyrl);
  83. tinyrl_hist_save(tinyrl);
  84. hist_free(tinyrl->hist);
  85. vt100_free(tinyrl->term);
  86. faux_str_free(tinyrl->prompt);
  87. tinyrl_reset_line_state(tinyrl); // It's really reset 'last' string
  88. faux_str_free(tinyrl->line.str);
  89. faux_free(tinyrl);
  90. }
  91. void tty_raw_mode(tinyrl_t *tinyrl)
  92. {
  93. struct termios new_termios = {};
  94. FILE *istream = NULL;
  95. int fd = -1;
  96. if (!tinyrl)
  97. return;
  98. istream = vt100_istream(tinyrl->term);
  99. if (!istream)
  100. return;
  101. fd = fileno(istream);
  102. if (tcgetattr(fd, &new_termios) < 0)
  103. return;
  104. new_termios.c_iflag = 0;
  105. new_termios.c_oflag = OPOST | ONLCR;
  106. new_termios.c_lflag = 0;
  107. new_termios.c_cc[VMIN] = 1;
  108. new_termios.c_cc[VTIME] = 0;
  109. // Mode switch
  110. tcsetattr(fd, TCSADRAIN, &new_termios);
  111. }
  112. void tty_restore_mode(tinyrl_t *tinyrl)
  113. {
  114. FILE *istream = NULL;
  115. int fd = -1;
  116. istream = vt100_istream(tinyrl->term);
  117. if (!istream)
  118. return;
  119. fd = fileno(istream);
  120. // Do the mode switch
  121. tcsetattr(fd, TCSADRAIN, &tinyrl->default_termios);
  122. }
  123. bool_t tinyrl_bind_key(tinyrl_t *tinyrl, int key, tinyrl_key_func_t *fn)
  124. {
  125. assert(tinyrl);
  126. if (!tinyrl)
  127. return BOOL_FALSE;
  128. if ((key < 0) || (key > 255))
  129. return BOOL_FALSE;
  130. tinyrl->handlers[key] = fn;
  131. return BOOL_TRUE;
  132. }
  133. void tinyrl_set_hotkey_fn(tinyrl_t *tinyrl, tinyrl_key_func_t *fn)
  134. {
  135. tinyrl->hotkey_fn = fn;
  136. }
  137. void tinyrl_set_istream(tinyrl_t *tinyrl, FILE *istream)
  138. {
  139. assert(tinyrl);
  140. if (!tinyrl)
  141. return;
  142. vt100_set_istream(tinyrl->term, istream);
  143. // Save terminal settings to restore on exit
  144. if (istream)
  145. tcgetattr(fileno(istream), &tinyrl->default_termios);
  146. }
  147. FILE *tinyrl_istream(const tinyrl_t *tinyrl)
  148. {
  149. return vt100_istream(tinyrl->term);
  150. }
  151. void tinyrl_set_ostream(tinyrl_t *tinyrl, FILE *ostream)
  152. {
  153. assert(tinyrl);
  154. if (!tinyrl)
  155. return;
  156. vt100_set_ostream(tinyrl->term, ostream);
  157. }
  158. FILE *tinyrl_ostream(const tinyrl_t *tinyrl)
  159. {
  160. return vt100_ostream(tinyrl->term);
  161. }
  162. bool_t tinyrl_utf8(const tinyrl_t *tinyrl)
  163. {
  164. assert(tinyrl);
  165. if (!tinyrl)
  166. return BOOL_TRUE;
  167. return tinyrl->utf8;
  168. }
  169. void tinyrl_set_utf8(tinyrl_t *tinyrl, bool_t utf8)
  170. {
  171. assert(tinyrl);
  172. if (!tinyrl)
  173. return;
  174. tinyrl->utf8 = utf8;
  175. }
  176. bool_t tinyrl_busy(const tinyrl_t *tinyrl)
  177. {
  178. assert(tinyrl);
  179. if (!tinyrl)
  180. return BOOL_FALSE;
  181. return tinyrl->busy;
  182. }
  183. void tinyrl_set_busy(tinyrl_t *tinyrl, bool_t busy)
  184. {
  185. assert(tinyrl);
  186. if (!tinyrl)
  187. return;
  188. tinyrl->busy = busy;
  189. }
  190. const char *tinyrl_prompt(const tinyrl_t *tinyrl)
  191. {
  192. assert(tinyrl);
  193. if (!tinyrl)
  194. return NULL;
  195. return tinyrl->prompt;
  196. }
  197. void tinyrl_set_prompt(tinyrl_t *tinyrl, const char *prompt)
  198. {
  199. assert(tinyrl);
  200. if (!tinyrl)
  201. return;
  202. if (tinyrl->prompt)
  203. faux_str_free(tinyrl->prompt);
  204. tinyrl->prompt = faux_str_dup(prompt);
  205. tinyrl->prompt_len = strlen(tinyrl->prompt);
  206. tinyrl->prompt_chars = utf8_nsyms(tinyrl->prompt, tinyrl->prompt_len);
  207. }
  208. void *tinyrl_udata(const tinyrl_t *tinyrl)
  209. {
  210. assert(tinyrl);
  211. if (!tinyrl)
  212. return NULL;
  213. return tinyrl->udata;
  214. }
  215. void tinyrl_set_udata(tinyrl_t *tinyrl, void *udata)
  216. {
  217. assert(tinyrl);
  218. if (!tinyrl)
  219. return;
  220. tinyrl->udata = udata;
  221. }
  222. const char *tinyrl_line(const tinyrl_t *tinyrl)
  223. {
  224. return tinyrl->line.str;
  225. }
  226. bool_t tinyrl_hist_save(const tinyrl_t *tinyrl)
  227. {
  228. assert(tinyrl);
  229. if (!tinyrl)
  230. return BOOL_FALSE;
  231. return hist_save(tinyrl->hist);
  232. }
  233. bool_t tinyrl_hist_restore(tinyrl_t *tinyrl)
  234. {
  235. assert(tinyrl);
  236. if (!tinyrl)
  237. return BOOL_FALSE;
  238. return hist_restore(tinyrl->hist);
  239. }
  240. static bool_t process_char(tinyrl_t *tinyrl, char key)
  241. {
  242. // Begin of ESC sequence
  243. if (!tinyrl->esc_cont && (KEY_ESC == key)) {
  244. tinyrl->esc_cont = BOOL_TRUE; // Start ESC sequence
  245. tinyrl->esc_p = tinyrl->esc_seq;
  246. // Note: Don't put ESC symbol itself to buffer
  247. return BOOL_TRUE;
  248. }
  249. // Continue ESC sequence
  250. if (tinyrl->esc_cont) {
  251. // Broken sequence. Too long
  252. if ((tinyrl->esc_p - tinyrl->esc_seq) >= (sizeof(tinyrl->esc_seq) - 1)) {
  253. tinyrl->esc_cont = BOOL_FALSE;
  254. return BOOL_FALSE;
  255. }
  256. // Save the curren char to sequence buffer
  257. *tinyrl->esc_p = key;
  258. tinyrl->esc_p++;
  259. // ANSI standard control sequences will end
  260. // with a character between 64 - 126
  261. if ((key != '[') && (key > 63)) {
  262. *tinyrl->esc_p = '\0';
  263. tinyrl_esc_seq(tinyrl, tinyrl->esc_seq);
  264. tinyrl->esc_cont = BOOL_FALSE;
  265. //tinyrl_redisplay(tinyrl);
  266. }
  267. return BOOL_TRUE;
  268. }
  269. // Call the handler for key
  270. // Handler (that has no special meaning) will put new char to line buffer
  271. if (!tinyrl->handlers[(unsigned char)key](tinyrl, key))
  272. vt100_ding(tinyrl->term);
  273. // if (tinyrl->done) // Some handler set the done flag
  274. // continue; // It will break the loop
  275. if (tinyrl->utf8) {
  276. // ASCII char (one byte)
  277. if (!(UTF8_7BIT_MASK & key)) {
  278. tinyrl->utf8_cont = 0;
  279. // First byte of multibyte symbol
  280. } else if (UTF8_11 == (key & UTF8_MASK)) {
  281. // Find out number of symbol's bytes
  282. unsigned int b = (unsigned int)key;
  283. tinyrl->utf8_cont = 0;
  284. while ((tinyrl->utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  285. tinyrl->utf8_cont++;
  286. b = b << 1;
  287. }
  288. // Continue of multibyte symbol
  289. } else if ((tinyrl->utf8_cont > 0) && (UTF8_10 == (key & UTF8_MASK))) {
  290. tinyrl->utf8_cont--;
  291. }
  292. }
  293. // For non UTF-8 encoding the utf8_cont is always 0.
  294. // For UTF-8 it's 0 when one-byte symbol or we get
  295. // all bytes for the current multibyte character
  296. // if (!tinyrl->utf8_cont) {
  297. // //tinyrl_redisplay(tinyrl);
  298. // printf("%s\n", tinyrl->line.str);
  299. // }
  300. return BOOL_TRUE;
  301. }
  302. int tinyrl_read(tinyrl_t *tinyrl)
  303. {
  304. int rc = 0;
  305. unsigned char key = 0;
  306. int count = 0;
  307. assert(tinyrl);
  308. tinyrl_set_busy(tinyrl, BOOL_FALSE);
  309. while ((rc = vt100_getchar(tinyrl->term, &key)) > 0) {
  310. count++;
  311. process_char(tinyrl, key);
  312. // Some commands can't be processed immediately by handlers and
  313. // need some network exchange for example. In this case we will
  314. // not execute redisplay() here.
  315. if (!tinyrl->utf8_cont && !tinyrl_busy(tinyrl)) {
  316. tinyrl_redisplay(tinyrl);
  317. // printf("%s\n", tinyrl->line.str);
  318. }
  319. //printf("key=%u, pos=%lu, len=%lu\n", key, tinyrl->line.pos, tinyrl->line.len);
  320. }
  321. if ((rc < 0) && (EAGAIN == errno))
  322. return count;
  323. return rc;
  324. }
  325. /*
  326. * Ensure that buffer has enough space to hold len characters,
  327. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  328. * if the line is successfully extended, BOOL_FALSE if not.
  329. */
  330. bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len)
  331. {
  332. char *new_buf = NULL;
  333. size_t new_size = 0;
  334. size_t chunk_num = 0;
  335. if (tinyrl->line.len >= len)
  336. return BOOL_TRUE;
  337. chunk_num = len / LINE_CHUNK;
  338. if ((len % LINE_CHUNK) > 0)
  339. chunk_num++;
  340. new_size = chunk_num * LINE_CHUNK;
  341. // First initialization
  342. if (tinyrl->line.str == NULL) {
  343. tinyrl->line.str = faux_zmalloc(new_size);
  344. if (!tinyrl->line.str)
  345. return BOOL_FALSE;
  346. tinyrl->line.size = new_size;
  347. return BOOL_TRUE;
  348. }
  349. new_buf = realloc(tinyrl->line.str, new_size);
  350. if (!new_buf)
  351. return BOOL_FALSE;
  352. tinyrl->line.str = new_buf;
  353. tinyrl->line.size = new_size;
  354. return BOOL_TRUE;
  355. }
  356. bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq)
  357. {
  358. bool_t result = BOOL_FALSE;
  359. switch (vt100_esc_decode(tinyrl->term, esc_seq)) {
  360. case VT100_CURSOR_UP:
  361. result = tinyrl_key_up(tinyrl, 0);
  362. break;
  363. case VT100_CURSOR_DOWN:
  364. result = tinyrl_key_down(tinyrl, 0);
  365. break;
  366. case VT100_CURSOR_LEFT:
  367. result = tinyrl_key_left(tinyrl, 0);
  368. break;
  369. case VT100_CURSOR_RIGHT:
  370. result = tinyrl_key_right(tinyrl, 0);
  371. break;
  372. case VT100_HOME:
  373. result = tinyrl_key_start_of_line(tinyrl, 0);
  374. break;
  375. case VT100_END:
  376. result = tinyrl_key_end_of_line(tinyrl, 0);
  377. break;
  378. case VT100_DELETE:
  379. result = tinyrl_key_delete(tinyrl, 0);
  380. break;
  381. case VT100_INSERT:
  382. case VT100_PGDOWN:
  383. case VT100_PGUP:
  384. case VT100_UNKNOWN:
  385. break;
  386. }
  387. return result;
  388. }
  389. bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len)
  390. {
  391. size_t new_size = tinyrl->line.len + len + 1;
  392. if (len == 0)
  393. return BOOL_TRUE;
  394. tinyrl_line_extend(tinyrl, new_size);
  395. if (tinyrl->line.pos < tinyrl->line.len) {
  396. memmove(tinyrl->line.str + tinyrl->line.pos + len,
  397. tinyrl->line.str + tinyrl->line.pos,
  398. tinyrl->line.len - tinyrl->line.pos);
  399. }
  400. memcpy(tinyrl->line.str + tinyrl->line.pos, text, len);
  401. tinyrl->line.pos += len;
  402. tinyrl->line.len += len;
  403. tinyrl->line.str[tinyrl->line.len] = '\0';
  404. return BOOL_TRUE;
  405. }
  406. bool_t tinyrl_line_delete(tinyrl_t *tinyrl, off_t start, size_t len)
  407. {
  408. if (start >= tinyrl->line.len)
  409. return BOOL_TRUE;
  410. if ((start + len) >= tinyrl->line.len) {
  411. tinyrl->line.len = start;
  412. } else {
  413. memmove(tinyrl->line.str + start,
  414. tinyrl->line.str + start + len,
  415. tinyrl->line.len - (start + len));
  416. tinyrl->line.len -= len;
  417. }
  418. tinyrl->line.pos = start;
  419. tinyrl->line.str[tinyrl->line.len] = '\0';
  420. return BOOL_TRUE;
  421. }
  422. bool_t tinyrl_line_replace(tinyrl_t *tinyrl, const char *text)
  423. {
  424. size_t len = 0;
  425. if (faux_str_is_empty(text)) {
  426. tinyrl_reset_line(tinyrl);
  427. return BOOL_TRUE;
  428. }
  429. len = strlen(text);
  430. tinyrl_line_extend(tinyrl, len + 1);
  431. memcpy(tinyrl->line.str, text, len);
  432. tinyrl->line.pos = len;
  433. tinyrl->line.len = len;
  434. tinyrl->line.str[tinyrl->line.len] = '\0';
  435. return BOOL_TRUE;
  436. }
  437. static void move_cursor(const tinyrl_t *tinyrl, size_t cur_pos, size_t target_pos)
  438. {
  439. int rows = 0;
  440. int cols = 0;
  441. // Note: The '/' is not real math division. It's integer part of division
  442. // so we need separate division for two values.
  443. rows = (target_pos / tinyrl->width) - (cur_pos / tinyrl->width);
  444. cols = (target_pos % tinyrl->width) - (cur_pos % tinyrl->width);
  445. if (cols > 0)
  446. vt100_cursor_forward(tinyrl->term, cols);
  447. else if (cols < 0)
  448. vt100_cursor_back(tinyrl->term, -cols);
  449. if (rows > 0)
  450. vt100_cursor_down(tinyrl->term, rows);
  451. else if (rows < 0)
  452. vt100_cursor_up(tinyrl->term, -rows);
  453. }
  454. static size_t str_equal_part(const tinyrl_t *tinyrl,
  455. const char *s1, const char *s2)
  456. {
  457. const char *str1 = s1;
  458. const char *str2 = s2;
  459. if (!str1 || !str2)
  460. return 0;
  461. while (*str1 && *str2) {
  462. if (*str1 != *str2)
  463. break;
  464. str1++;
  465. str2++;
  466. }
  467. if (!tinyrl->utf8)
  468. return str1 - s1;
  469. // UTF8
  470. // If UTF8_10 byte (intermediate byte of UTF-8 sequence) is not equal
  471. // then we need to find starting of this UTF-8 character because whole
  472. // UTF-8 character is not equal.
  473. if (UTF8_10 == (*str1 & UTF8_MASK)) {
  474. // Skip intermediate bytes
  475. while ((str1 > s1) && (UTF8_10 == (*str1 & UTF8_MASK)))
  476. str1--;
  477. }
  478. return str1 - s1;
  479. }
  480. void tinyrl_save_last(tinyrl_t *tinyrl)
  481. {
  482. faux_str_free(tinyrl->last.str);
  483. tinyrl->last = tinyrl->line;
  484. tinyrl->last.str = faux_str_dup(tinyrl->line.str);
  485. }
  486. void tinyrl_reset_line_state(tinyrl_t *tinyrl)
  487. {
  488. faux_str_free(tinyrl->last.str);
  489. faux_bzero(&tinyrl->last, sizeof(tinyrl->last));
  490. }
  491. void tinyrl_reset_line(tinyrl_t *tinyrl)
  492. {
  493. tinyrl_line_delete(tinyrl, 0, tinyrl->line.len);
  494. }
  495. void tinyrl_redisplay(tinyrl_t *tinyrl)
  496. {
  497. size_t width = vt100_width(tinyrl->term);
  498. // unsigned int line_size = strlen(tinyrl->line);
  499. unsigned int line_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.len);
  500. size_t cols = 0;
  501. size_t eq_bytes = 0;
  502. // Prepare print position
  503. if (tinyrl->last.str && (width == tinyrl->width)) {
  504. size_t eq_chars = 0; // Printable symbols
  505. size_t last_pos_chars = 0;
  506. // If line and last line have the equal chars at begining
  507. eq_bytes = str_equal_part(tinyrl, tinyrl->line.str, tinyrl->last.str);
  508. eq_chars = utf8_nsyms(tinyrl->last.str, eq_bytes);
  509. last_pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  510. move_cursor(tinyrl, tinyrl->prompt_chars + last_pos_chars,
  511. tinyrl->prompt_chars + eq_chars);
  512. } else {
  513. // Prepare to resize
  514. if (width != tinyrl->width) {
  515. vt100_next_line(tinyrl->term);
  516. vt100_erase_down(tinyrl->term);
  517. }
  518. vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  519. }
  520. // Print current line
  521. vt100_printf(tinyrl->term, "%s", tinyrl->line.str + eq_bytes);
  522. cols = (tinyrl->prompt_chars + line_chars) % width;
  523. if ((cols == 0) && (tinyrl->line.len - eq_bytes))
  524. vt100_next_line(tinyrl->term);
  525. // Erase down if current line is shorter than previous one
  526. if (tinyrl->last.len > tinyrl->line.len)
  527. vt100_erase_down(tinyrl->term);
  528. // Move the cursor to the insertion point
  529. if (tinyrl->line.pos < tinyrl->line.len) {
  530. size_t pos_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.pos);
  531. move_cursor(tinyrl, tinyrl->prompt_chars + line_chars,
  532. tinyrl->prompt_chars + pos_chars);
  533. }
  534. // Update the display
  535. vt100_oflush(tinyrl->term);
  536. // Save the last line buffer
  537. tinyrl_save_last(tinyrl);
  538. tinyrl->width = width;
  539. }
  540. void tinyrl_crlf(const tinyrl_t *tinyrl)
  541. {
  542. vt100_printf(tinyrl->term, "\n");
  543. }
  544. // Jump to first free line after current multiline input
  545. void tinyrl_multi_crlf(const tinyrl_t *tinyrl)
  546. {
  547. size_t full_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.len);
  548. size_t pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  549. move_cursor(tinyrl, tinyrl->prompt_chars + pos_chars,
  550. tinyrl->prompt_chars + full_chars);
  551. tinyrl_crlf(tinyrl);
  552. vt100_oflush(tinyrl->term);
  553. }
  554. void tinyrl_line_to_hist(tinyrl_t *tinyrl)
  555. {
  556. if (tinyrl->line.len == 0)
  557. return;
  558. hist_add(tinyrl->hist, tinyrl->line.str, BOOL_FALSE);
  559. }
  560. void tinyrl_reset_hist_pos(tinyrl_t *tinyrl)
  561. {
  562. hist_pos_reset(tinyrl->hist);
  563. }