tinyrl.c 17 KB

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