tinyrl.c 17 KB

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