tinyrl.c 18 KB

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