tinyrl.c 18 KB

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