tinyrl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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_oflag = ONLCR;
  107. new_termios.c_lflag = 0;
  108. // new_termios.c_cflag = CS8 | CREAD;
  109. // new_termios.c_iflag = IGNPAR | IUTF8;
  110. // new_termios.c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
  111. // new_termios.c_lflag = ECHOCTL | ECHOKE;
  112. new_termios.c_cc[VMIN] = 1;
  113. new_termios.c_cc[VTIME] = 0;
  114. // cfsetospeed(&new_termios, B38400);
  115. // Mode switch
  116. tcsetattr(fd, TCSADRAIN, &new_termios);
  117. }
  118. void tty_restore_mode(tinyrl_t *tinyrl)
  119. {
  120. FILE *istream = NULL;
  121. int fd = -1;
  122. istream = vt100_istream(tinyrl->term);
  123. if (!istream)
  124. return;
  125. fd = fileno(istream);
  126. // Do the mode switch
  127. tcsetattr(fd, TCSADRAIN, &tinyrl->default_termios);
  128. }
  129. bool_t tinyrl_bind_key(tinyrl_t *tinyrl, int key, tinyrl_key_func_t *fn)
  130. {
  131. assert(tinyrl);
  132. if (!tinyrl)
  133. return BOOL_FALSE;
  134. if ((key < 0) || (key > 255))
  135. return BOOL_FALSE;
  136. tinyrl->handlers[key] = fn;
  137. return BOOL_TRUE;
  138. }
  139. void tinyrl_set_hotkey_fn(tinyrl_t *tinyrl, tinyrl_key_func_t *fn)
  140. {
  141. tinyrl->hotkey_fn = fn;
  142. }
  143. void tinyrl_set_istream(tinyrl_t *tinyrl, FILE *istream)
  144. {
  145. assert(tinyrl);
  146. if (!tinyrl)
  147. return;
  148. vt100_set_istream(tinyrl->term, istream);
  149. // Save terminal settings to restore on exit
  150. if (istream)
  151. tcgetattr(fileno(istream), &tinyrl->default_termios);
  152. }
  153. FILE *tinyrl_istream(const tinyrl_t *tinyrl)
  154. {
  155. return vt100_istream(tinyrl->term);
  156. }
  157. void tinyrl_set_ostream(tinyrl_t *tinyrl, FILE *ostream)
  158. {
  159. assert(tinyrl);
  160. if (!tinyrl)
  161. return;
  162. vt100_set_ostream(tinyrl->term, ostream);
  163. }
  164. FILE *tinyrl_ostream(const tinyrl_t *tinyrl)
  165. {
  166. return vt100_ostream(tinyrl->term);
  167. }
  168. bool_t tinyrl_utf8(const tinyrl_t *tinyrl)
  169. {
  170. assert(tinyrl);
  171. if (!tinyrl)
  172. return BOOL_TRUE;
  173. return tinyrl->utf8;
  174. }
  175. void tinyrl_set_utf8(tinyrl_t *tinyrl, bool_t utf8)
  176. {
  177. assert(tinyrl);
  178. if (!tinyrl)
  179. return;
  180. tinyrl->utf8 = utf8;
  181. }
  182. bool_t tinyrl_busy(const tinyrl_t *tinyrl)
  183. {
  184. assert(tinyrl);
  185. if (!tinyrl)
  186. return BOOL_FALSE;
  187. return tinyrl->busy;
  188. }
  189. void tinyrl_set_busy(tinyrl_t *tinyrl, bool_t busy)
  190. {
  191. assert(tinyrl);
  192. if (!tinyrl)
  193. return;
  194. tinyrl->busy = busy;
  195. }
  196. const char *tinyrl_prompt(const tinyrl_t *tinyrl)
  197. {
  198. assert(tinyrl);
  199. if (!tinyrl)
  200. return NULL;
  201. return tinyrl->prompt;
  202. }
  203. void tinyrl_set_prompt(tinyrl_t *tinyrl, const char *prompt)
  204. {
  205. const char *last_cr = NULL;
  206. assert(tinyrl);
  207. if (!tinyrl)
  208. return;
  209. if (tinyrl->prompt)
  210. faux_str_free(tinyrl->prompt);
  211. tinyrl->prompt = faux_str_dup(prompt);
  212. tinyrl->prompt_len = strlen(tinyrl->prompt);
  213. // Prompt can contain '\n' characters so let prompt_chars count symbols
  214. // of last line only.
  215. last_cr = strrchr(tinyrl->prompt, '\n');
  216. if (!last_cr)
  217. last_cr = tinyrl->prompt;
  218. else
  219. last_cr++; // Skip '\n' itself
  220. tinyrl->prompt_chars = utf8_nsyms(last_cr, tinyrl->prompt_len);
  221. }
  222. void *tinyrl_udata(const tinyrl_t *tinyrl)
  223. {
  224. assert(tinyrl);
  225. if (!tinyrl)
  226. return NULL;
  227. return tinyrl->udata;
  228. }
  229. void tinyrl_set_udata(tinyrl_t *tinyrl, void *udata)
  230. {
  231. assert(tinyrl);
  232. if (!tinyrl)
  233. return;
  234. tinyrl->udata = udata;
  235. }
  236. const char *tinyrl_line(const tinyrl_t *tinyrl)
  237. {
  238. return tinyrl->line.str;
  239. }
  240. bool_t tinyrl_hist_save(const tinyrl_t *tinyrl)
  241. {
  242. assert(tinyrl);
  243. if (!tinyrl)
  244. return BOOL_FALSE;
  245. return hist_save(tinyrl->hist);
  246. }
  247. bool_t tinyrl_hist_restore(tinyrl_t *tinyrl)
  248. {
  249. assert(tinyrl);
  250. if (!tinyrl)
  251. return BOOL_FALSE;
  252. return hist_restore(tinyrl->hist);
  253. }
  254. static bool_t process_char(tinyrl_t *tinyrl, char key)
  255. {
  256. // Begin of ESC sequence
  257. if (!tinyrl->esc_cont && (KEY_ESC == key)) {
  258. tinyrl->esc_cont = BOOL_TRUE; // Start ESC sequence
  259. tinyrl->esc_p = tinyrl->esc_seq;
  260. // Note: Don't put ESC symbol itself to buffer
  261. return BOOL_TRUE;
  262. }
  263. // Continue ESC sequence
  264. if (tinyrl->esc_cont) {
  265. // Broken sequence. Too long
  266. if ((tinyrl->esc_p - tinyrl->esc_seq) >= (long int)(sizeof(tinyrl->esc_seq) - 1)) {
  267. tinyrl->esc_cont = BOOL_FALSE;
  268. return BOOL_FALSE;
  269. }
  270. // Save the curren char to sequence buffer
  271. *tinyrl->esc_p = key;
  272. tinyrl->esc_p++;
  273. // ANSI standard control sequences will end
  274. // with a character between 64 - 126
  275. if ((key != '[') && (key > 63)) {
  276. *tinyrl->esc_p = '\0';
  277. tinyrl_esc_seq(tinyrl, tinyrl->esc_seq);
  278. tinyrl->esc_cont = BOOL_FALSE;
  279. //tinyrl_redisplay(tinyrl);
  280. }
  281. return BOOL_TRUE;
  282. }
  283. // Call the handler for key
  284. // Handler (that has no special meaning) will put new char to line buffer
  285. if (!tinyrl->handlers[(unsigned char)key](tinyrl, key))
  286. vt100_ding(tinyrl->term);
  287. // if (tinyrl->done) // Some handler set the done flag
  288. // continue; // It will break the loop
  289. if (tinyrl->utf8) {
  290. // ASCII char (one byte)
  291. if (!(UTF8_7BIT_MASK & key)) {
  292. tinyrl->utf8_cont = 0;
  293. // First byte of multibyte symbol
  294. } else if (UTF8_11 == (key & UTF8_MASK)) {
  295. // Find out number of symbol's bytes
  296. unsigned int b = (unsigned int)key;
  297. tinyrl->utf8_cont = 0;
  298. while ((tinyrl->utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  299. tinyrl->utf8_cont++;
  300. b = b << 1;
  301. }
  302. // Continue of multibyte symbol
  303. } else if ((tinyrl->utf8_cont > 0) && (UTF8_10 == (key & UTF8_MASK))) {
  304. tinyrl->utf8_cont--;
  305. }
  306. }
  307. // For non UTF-8 encoding the utf8_cont is always 0.
  308. // For UTF-8 it's 0 when one-byte symbol or we get
  309. // all bytes for the current multibyte character
  310. // if (!tinyrl->utf8_cont) {
  311. // //tinyrl_redisplay(tinyrl);
  312. // printf("%s\n", tinyrl->line.str);
  313. // }
  314. return BOOL_TRUE;
  315. }
  316. int tinyrl_read(tinyrl_t *tinyrl)
  317. {
  318. int rc = 0;
  319. unsigned char key = 0;
  320. int count = 0;
  321. assert(tinyrl);
  322. tinyrl_set_busy(tinyrl, BOOL_FALSE);
  323. while ((rc = vt100_getchar(tinyrl->term, &key)) > 0) {
  324. count++;
  325. process_char(tinyrl, key);
  326. // Some commands can't be processed immediately by handlers and
  327. // need some network exchange for example. In this case we will
  328. // not execute redisplay() here.
  329. if (!tinyrl->utf8_cont && !tinyrl_busy(tinyrl)) {
  330. tinyrl_redisplay(tinyrl);
  331. // printf("%s\n", tinyrl->line.str);
  332. }
  333. //printf("key=%u, pos=%lu, len=%lu\n", key, tinyrl->line.pos, tinyrl->line.len);
  334. }
  335. if ((rc < 0) && (EAGAIN == errno))
  336. return count;
  337. return rc;
  338. }
  339. /*
  340. * Ensure that buffer has enough space to hold len characters,
  341. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  342. * if the line is successfully extended, BOOL_FALSE if not.
  343. */
  344. bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len)
  345. {
  346. char *new_buf = NULL;
  347. size_t new_size = 0;
  348. size_t chunk_num = 0;
  349. if (tinyrl->line.len >= len)
  350. return BOOL_TRUE;
  351. chunk_num = len / LINE_CHUNK;
  352. if ((len % LINE_CHUNK) > 0)
  353. chunk_num++;
  354. new_size = chunk_num * LINE_CHUNK;
  355. // First initialization
  356. if (tinyrl->line.str == NULL) {
  357. tinyrl->line.str = faux_zmalloc(new_size);
  358. if (!tinyrl->line.str)
  359. return BOOL_FALSE;
  360. tinyrl->line.size = new_size;
  361. return BOOL_TRUE;
  362. }
  363. new_buf = realloc(tinyrl->line.str, new_size);
  364. if (!new_buf)
  365. return BOOL_FALSE;
  366. tinyrl->line.str = new_buf;
  367. tinyrl->line.size = new_size;
  368. return BOOL_TRUE;
  369. }
  370. bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq)
  371. {
  372. bool_t result = BOOL_FALSE;
  373. switch (vt100_esc_decode(esc_seq)) {
  374. case VT100_CURSOR_UP:
  375. result = tinyrl_key_up(tinyrl, 0);
  376. break;
  377. case VT100_CURSOR_DOWN:
  378. result = tinyrl_key_down(tinyrl, 0);
  379. break;
  380. case VT100_CURSOR_LEFT:
  381. result = tinyrl_key_left(tinyrl, 0);
  382. break;
  383. case VT100_CURSOR_RIGHT:
  384. result = tinyrl_key_right(tinyrl, 0);
  385. break;
  386. case VT100_HOME:
  387. result = tinyrl_key_start_of_line(tinyrl, 0);
  388. break;
  389. case VT100_END:
  390. result = tinyrl_key_end_of_line(tinyrl, 0);
  391. break;
  392. case VT100_DELETE:
  393. result = tinyrl_key_delete(tinyrl, 0);
  394. break;
  395. case VT100_INSERT:
  396. case VT100_PGDOWN:
  397. case VT100_PGUP:
  398. case VT100_UNKNOWN:
  399. break;
  400. }
  401. return result;
  402. }
  403. bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len)
  404. {
  405. size_t new_size = tinyrl->line.len + len + 1;
  406. if (len == 0)
  407. return BOOL_TRUE;
  408. tinyrl_line_extend(tinyrl, new_size);
  409. if (tinyrl->line.pos < tinyrl->line.len) {
  410. memmove(tinyrl->line.str + tinyrl->line.pos + len,
  411. tinyrl->line.str + tinyrl->line.pos,
  412. tinyrl->line.len - tinyrl->line.pos);
  413. }
  414. memcpy(tinyrl->line.str + tinyrl->line.pos, text, len);
  415. tinyrl->line.pos += len;
  416. tinyrl->line.len += len;
  417. tinyrl->line.str[tinyrl->line.len] = '\0';
  418. return BOOL_TRUE;
  419. }
  420. bool_t tinyrl_line_delete(tinyrl_t *tinyrl, size_t start, size_t len)
  421. {
  422. if (start >= tinyrl->line.len)
  423. return BOOL_TRUE;
  424. if ((start + len) >= tinyrl->line.len) {
  425. tinyrl->line.len = start;
  426. } else {
  427. memmove(tinyrl->line.str + start,
  428. tinyrl->line.str + start + len,
  429. tinyrl->line.len - (start + len));
  430. tinyrl->line.len -= len;
  431. }
  432. tinyrl->line.pos = start;
  433. tinyrl->line.str[tinyrl->line.len] = '\0';
  434. return BOOL_TRUE;
  435. }
  436. bool_t tinyrl_line_replace(tinyrl_t *tinyrl, const char *text)
  437. {
  438. size_t len = 0;
  439. if (faux_str_is_empty(text)) {
  440. tinyrl_reset_line(tinyrl);
  441. return BOOL_TRUE;
  442. }
  443. len = strlen(text);
  444. tinyrl_line_extend(tinyrl, len + 1);
  445. memcpy(tinyrl->line.str, text, len);
  446. tinyrl->line.pos = len;
  447. tinyrl->line.len = len;
  448. tinyrl->line.str[tinyrl->line.len] = '\0';
  449. return BOOL_TRUE;
  450. }
  451. static void move_cursor(const tinyrl_t *tinyrl, size_t cur_pos, size_t target_pos)
  452. {
  453. int rows = 0;
  454. int cols = 0;
  455. // Note: The '/' is not real math division. It's integer part of division
  456. // so we need separate division for two values.
  457. rows = (target_pos / tinyrl->width) - (cur_pos / tinyrl->width);
  458. cols = (target_pos % tinyrl->width) - (cur_pos % tinyrl->width);
  459. if (cols > 0)
  460. vt100_cursor_forward(tinyrl->term, cols);
  461. else if (cols < 0)
  462. vt100_cursor_back(tinyrl->term, -cols);
  463. if (rows > 0)
  464. vt100_cursor_down(tinyrl->term, rows);
  465. else if (rows < 0)
  466. vt100_cursor_up(tinyrl->term, -rows);
  467. }
  468. size_t tinyrl_equal_part(const tinyrl_t *tinyrl,
  469. const char *s1, const char *s2)
  470. {
  471. const char *str1 = s1;
  472. const char *str2 = s2;
  473. if (!str1 || !str2)
  474. return 0;
  475. while (*str1 && *str2) {
  476. if (*str1 != *str2)
  477. break;
  478. str1++;
  479. str2++;
  480. }
  481. if (!tinyrl->utf8)
  482. return str1 - s1;
  483. // UTF8
  484. // If UTF8_10 byte (intermediate byte of UTF-8 sequence) is not equal
  485. // then we need to find starting of this UTF-8 character because whole
  486. // UTF-8 character is not equal.
  487. if (UTF8_10 == (*str1 & UTF8_MASK)) {
  488. // Skip intermediate bytes
  489. while ((str1 > s1) && (UTF8_10 == (*str1 & UTF8_MASK)))
  490. str1--;
  491. }
  492. return str1 - s1;
  493. }
  494. void tinyrl_save_last(tinyrl_t *tinyrl)
  495. {
  496. faux_str_free(tinyrl->last.str);
  497. tinyrl->last = tinyrl->line;
  498. tinyrl->last.str = faux_str_dup(tinyrl->line.str);
  499. }
  500. void tinyrl_reset_line_state(tinyrl_t *tinyrl)
  501. {
  502. faux_str_free(tinyrl->last.str);
  503. faux_bzero(&tinyrl->last, sizeof(tinyrl->last));
  504. }
  505. void tinyrl_reset_line(tinyrl_t *tinyrl)
  506. {
  507. tinyrl_line_delete(tinyrl, 0, tinyrl->line.len);
  508. }
  509. void tinyrl_redisplay(tinyrl_t *tinyrl)
  510. {
  511. size_t width = vt100_width(tinyrl->term);
  512. // unsigned int line_size = strlen(tinyrl->line);
  513. unsigned int line_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.len);
  514. size_t cols = 0;
  515. size_t eq_bytes = 0;
  516. // Prepare print position
  517. if (tinyrl->last.str && (width == tinyrl->width)) {
  518. size_t eq_chars = 0; // Printable symbols
  519. size_t last_pos_chars = 0;
  520. // If line and last line have the equal chars at begining
  521. eq_bytes = tinyrl_equal_part(tinyrl, tinyrl->line.str, tinyrl->last.str);
  522. eq_chars = utf8_nsyms(tinyrl->last.str, eq_bytes);
  523. last_pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  524. move_cursor(tinyrl, tinyrl->prompt_chars + last_pos_chars,
  525. tinyrl->prompt_chars + eq_chars);
  526. } else {
  527. // Prepare to resize
  528. if (width != tinyrl->width) {
  529. vt100_next_line(tinyrl->term);
  530. vt100_erase_down(tinyrl->term);
  531. }
  532. vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  533. }
  534. // Print current line
  535. vt100_printf(tinyrl->term, "%s", tinyrl->line.str + eq_bytes);
  536. cols = (tinyrl->prompt_chars + line_chars) % width;
  537. if ((cols == 0) && (tinyrl->line.len - eq_bytes))
  538. vt100_next_line(tinyrl->term);
  539. // Erase down if current line is shorter than previous one
  540. if (tinyrl->last.len > tinyrl->line.len)
  541. vt100_erase_down(tinyrl->term);
  542. // Move the cursor to the insertion point
  543. if (tinyrl->line.pos < tinyrl->line.len) {
  544. size_t pos_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.pos);
  545. move_cursor(tinyrl, tinyrl->prompt_chars + line_chars,
  546. tinyrl->prompt_chars + pos_chars);
  547. }
  548. // Update the display
  549. vt100_oflush(tinyrl->term);
  550. // Save the last line buffer
  551. tinyrl_save_last(tinyrl);
  552. tinyrl->width = width;
  553. }
  554. void tinyrl_crlf(const tinyrl_t *tinyrl)
  555. {
  556. vt100_printf(tinyrl->term, "\n");
  557. }
  558. // Jump to first free line after current multiline input
  559. void tinyrl_multi_crlf(const tinyrl_t *tinyrl)
  560. {
  561. size_t full_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.len);
  562. size_t pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  563. move_cursor(tinyrl, tinyrl->prompt_chars + pos_chars,
  564. tinyrl->prompt_chars + full_chars);
  565. tinyrl_crlf(tinyrl);
  566. vt100_oflush(tinyrl->term);
  567. }
  568. void tinyrl_line_to_hist(tinyrl_t *tinyrl)
  569. {
  570. if (tinyrl->line.len == 0)
  571. return;
  572. hist_add(tinyrl->hist, tinyrl->line.str, BOOL_FALSE);
  573. }
  574. void tinyrl_reset_hist_pos(tinyrl_t *tinyrl)
  575. {
  576. hist_pos_reset(tinyrl->hist);
  577. }
  578. void tinyrl_winsize(const tinyrl_t *tinyrl, size_t *width, size_t *height)
  579. {
  580. vt100_t *term = NULL;
  581. if (tinyrl)
  582. term = tinyrl->term;
  583. vt100_winsize(term, width, height);
  584. }
  585. size_t tinyrl_width(const tinyrl_t *tinyrl)
  586. {
  587. vt100_t *term = NULL;
  588. if (tinyrl)
  589. term = tinyrl->term;
  590. return vt100_width(term);
  591. }
  592. size_t tinyrl_height(const tinyrl_t *tinyrl)
  593. {
  594. vt100_t *term = NULL;
  595. if (tinyrl)
  596. term = tinyrl->term;
  597. return vt100_height(term);
  598. }
  599. // Because terminal is in raw mode and standard libc printf() can don't flush()
  600. int tinyrl_printf(const tinyrl_t *tinyrl, const char *fmt, ...)
  601. {
  602. va_list args;
  603. int len = 0;
  604. va_start(args, fmt);
  605. len = vt100_vprintf(tinyrl->term, fmt, args);
  606. va_end(args);
  607. return len;
  608. }