tinyrl.c 17 KB

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