tinyrl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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->max_line_length = 0;
  54. tinyrl->buffer = NULL;
  55. tinyrl->buffer_size = 0;
  56. tinyrl->done = BOOL_FALSE;
  57. tinyrl->completion_over = BOOL_FALSE;
  58. tinyrl->attempted_completion_function = NULL;
  59. tinyrl->hotkey_fn = NULL;
  60. tinyrl->state = 0;
  61. tinyrl->kill_string = NULL;
  62. tinyrl->echo_char = '\0';
  63. tinyrl->echo_enabled = BOOL_TRUE;
  64. tinyrl->utf8 = BOOL_TRUE;
  65. // VT100 terminal
  66. tinyrl->term = vt100_new(istream, ostream);
  67. // To save terminal settings
  68. tinyrl_set_istream(tinyrl, istream);
  69. tinyrl->width = vt100_width(tinyrl->term);
  70. // History object
  71. tinyrl->hist = hist_new(hist_fname, hist_stifle);
  72. tinyrl_hist_restore(tinyrl);
  73. tty_raw_mode(tinyrl);
  74. return tinyrl;
  75. }
  76. void tinyrl_free(tinyrl_t *tinyrl)
  77. {
  78. assert(tinyrl);
  79. if (!tinyrl)
  80. return;
  81. tty_restore_mode(tinyrl);
  82. hist_free(tinyrl->hist);
  83. vt100_free(tinyrl->term);
  84. faux_str_free(tinyrl->prompt);
  85. tinyrl_reset_line_state(tinyrl);
  86. // faux_str_free(tinyrl->kill_string);
  87. faux_free(tinyrl);
  88. }
  89. void tty_raw_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. // Mode switch
  108. tcsetattr(fd, TCSADRAIN, &new_termios);
  109. }
  110. void tty_restore_mode(tinyrl_t *tinyrl)
  111. {
  112. FILE *istream = NULL;
  113. int fd = -1;
  114. istream = vt100_istream(tinyrl->term);
  115. if (!istream)
  116. return;
  117. fd = fileno(istream);
  118. // Do the mode switch
  119. tcsetattr(fd, TCSADRAIN, &tinyrl->default_termios);
  120. }
  121. bool_t tinyrl_bind_key(tinyrl_t *tinyrl, int key, tinyrl_key_func_t *fn)
  122. {
  123. assert(tinyrl);
  124. if (!tinyrl)
  125. return BOOL_FALSE;
  126. if ((key < 0) || (key > 255))
  127. return BOOL_FALSE;
  128. tinyrl->handlers[key] = fn;
  129. return BOOL_TRUE;
  130. }
  131. void tinyrl_set_hotkey_fn(tinyrl_t *tinyrl, tinyrl_key_func_t *fn)
  132. {
  133. tinyrl->hotkey_fn = fn;
  134. }
  135. void tinyrl_set_istream(tinyrl_t *tinyrl, FILE *istream)
  136. {
  137. assert(tinyrl);
  138. if (!tinyrl)
  139. return;
  140. vt100_set_istream(tinyrl->term, istream);
  141. // Save terminal settings to restore on exit
  142. if (istream)
  143. tcgetattr(fileno(istream), &tinyrl->default_termios);
  144. }
  145. FILE *tinyrl_istream(const tinyrl_t *tinyrl)
  146. {
  147. return vt100_istream(tinyrl->term);
  148. }
  149. void tinyrl_set_ostream(tinyrl_t *tinyrl, FILE *ostream)
  150. {
  151. assert(tinyrl);
  152. if (!tinyrl)
  153. return;
  154. vt100_set_ostream(tinyrl->term, ostream);
  155. }
  156. FILE *tinyrl_ostream(const tinyrl_t *tinyrl)
  157. {
  158. return vt100_ostream(tinyrl->term);
  159. }
  160. bool_t tinyrl_utf8(const tinyrl_t *tinyrl)
  161. {
  162. assert(tinyrl);
  163. if (!tinyrl)
  164. return BOOL_TRUE;
  165. return tinyrl->utf8;
  166. }
  167. void tinyrl_set_utf8(tinyrl_t *tinyrl, bool_t utf8)
  168. {
  169. assert(tinyrl);
  170. if (!tinyrl)
  171. return;
  172. tinyrl->utf8 = utf8;
  173. }
  174. const char *tinyrl_prompt(const tinyrl_t *tinyrl)
  175. {
  176. assert(tinyrl);
  177. if (!tinyrl)
  178. return NULL;
  179. return tinyrl->prompt;
  180. }
  181. void tinyrl_set_prompt(tinyrl_t *tinyrl, const char *prompt)
  182. {
  183. assert(tinyrl);
  184. if (!tinyrl)
  185. return;
  186. if (tinyrl->prompt)
  187. faux_str_free(tinyrl->prompt);
  188. tinyrl->prompt = faux_str_dup(prompt);
  189. tinyrl->prompt_len = strlen(tinyrl->prompt);
  190. tinyrl->prompt_chars = utf8_nsyms(tinyrl->prompt, tinyrl->prompt_len);
  191. }
  192. void *tinyrl_udata(const tinyrl_t *tinyrl)
  193. {
  194. assert(tinyrl);
  195. if (!tinyrl)
  196. return NULL;
  197. return tinyrl->udata;
  198. }
  199. void tinyrl_set_udata(tinyrl_t *tinyrl, void *udata)
  200. {
  201. assert(tinyrl);
  202. if (!tinyrl)
  203. return;
  204. tinyrl->udata = udata;
  205. }
  206. const char *tinyrl_line(const tinyrl_t *tinyrl)
  207. {
  208. return tinyrl->line.str;
  209. }
  210. bool_t tinyrl_hist_save(const tinyrl_t *tinyrl)
  211. {
  212. assert(tinyrl);
  213. if (!tinyrl)
  214. return BOOL_FALSE;
  215. return hist_save(tinyrl->hist);
  216. }
  217. bool_t tinyrl_hist_restore(tinyrl_t *tinyrl)
  218. {
  219. assert(tinyrl);
  220. if (!tinyrl)
  221. return BOOL_FALSE;
  222. return hist_restore(tinyrl->hist);
  223. }
  224. static bool_t process_char(tinyrl_t *tinyrl, char key)
  225. {
  226. // Begin of ESC sequence
  227. if (!tinyrl->esc_cont && (KEY_ESC == key)) {
  228. tinyrl->esc_cont = BOOL_TRUE; // Start ESC sequence
  229. tinyrl->esc_p = tinyrl->esc_seq;
  230. // Note: Don't put ESC symbol itself to buffer
  231. return BOOL_TRUE;
  232. }
  233. // Continue ESC sequence
  234. if (tinyrl->esc_cont) {
  235. // Broken sequence. Too long
  236. if ((tinyrl->esc_p - tinyrl->esc_seq) >= (sizeof(tinyrl->esc_seq) - 1)) {
  237. tinyrl->esc_cont = BOOL_FALSE;
  238. return BOOL_FALSE;
  239. }
  240. // Save the curren char to sequence buffer
  241. *tinyrl->esc_p = key;
  242. tinyrl->esc_p++;
  243. // ANSI standard control sequences will end
  244. // with a character between 64 - 126
  245. if ((key != '[') && (key > 63)) {
  246. *tinyrl->esc_p = '\0';
  247. tinyrl_esc_seq(tinyrl, tinyrl->esc_seq);
  248. tinyrl->esc_cont = BOOL_FALSE;
  249. //tinyrl_redisplay(tinyrl);
  250. }
  251. return BOOL_TRUE;
  252. }
  253. // Call the handler for key
  254. // Handler (that has no special meaning) will put new char to line buffer
  255. if (!tinyrl->handlers[(unsigned char)key](tinyrl, key))
  256. vt100_ding(tinyrl->term);
  257. // if (tinyrl->done) // Some handler set the done flag
  258. // continue; // It will break the loop
  259. if (tinyrl->utf8) {
  260. // ASCII char (one byte)
  261. if (!(UTF8_7BIT_MASK & key)) {
  262. tinyrl->utf8_cont = 0;
  263. // First byte of multibyte symbol
  264. } else if (UTF8_11 == (key & UTF8_MASK)) {
  265. // Find out number of symbol's bytes
  266. unsigned int b = (unsigned int)key;
  267. tinyrl->utf8_cont = 0;
  268. while ((tinyrl->utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  269. tinyrl->utf8_cont++;
  270. b = b << 1;
  271. }
  272. // Continue of multibyte symbol
  273. } else if ((tinyrl->utf8_cont > 0) && (UTF8_10 == (key & UTF8_MASK))) {
  274. tinyrl->utf8_cont--;
  275. }
  276. }
  277. // For non UTF-8 encoding the utf8_cont is always 0.
  278. // For UTF-8 it's 0 when one-byte symbol or we get
  279. // all bytes for the current multibyte character
  280. // if (!tinyrl->utf8_cont) {
  281. // //tinyrl_redisplay(tinyrl);
  282. // printf("%s\n", tinyrl->line.str);
  283. // }
  284. return BOOL_TRUE;
  285. }
  286. int tinyrl_read(tinyrl_t *tinyrl)
  287. {
  288. int rc = 0;
  289. unsigned char key = 0;
  290. int count = 0;
  291. assert(tinyrl);
  292. while ((rc = vt100_getchar(tinyrl->term, &key)) > 0) {
  293. count++;
  294. process_char(tinyrl, key);
  295. if (!tinyrl->utf8_cont) {
  296. tinyrl_redisplay(tinyrl);
  297. // printf("%s\n", tinyrl->line.str);
  298. }
  299. //printf("key=%u, pos=%lu, len=%lu\n", key, tinyrl->line.pos, tinyrl->line.len);
  300. }
  301. if ((rc < 0) && (EAGAIN == errno))
  302. return count;
  303. return rc;
  304. }
  305. /*
  306. * Ensure that buffer has enough space to hold len characters,
  307. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  308. * if the line is successfully extended, BOOL_FALSE if not.
  309. */
  310. bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len)
  311. {
  312. char *new_buf = NULL;
  313. size_t new_size = 0;
  314. size_t chunk_num = 0;
  315. if (tinyrl->line.len >= len)
  316. return BOOL_TRUE;
  317. chunk_num = len / LINE_CHUNK;
  318. if ((len % LINE_CHUNK) > 0)
  319. chunk_num++;
  320. new_size = chunk_num * LINE_CHUNK;
  321. // First initialization
  322. if (tinyrl->line.str == NULL) {
  323. tinyrl->line.str = faux_zmalloc(new_size);
  324. if (!tinyrl->line.str)
  325. return BOOL_FALSE;
  326. tinyrl->line.size = new_size;
  327. return BOOL_TRUE;
  328. }
  329. new_buf = realloc(tinyrl->line.str, new_size);
  330. if (!new_buf)
  331. return BOOL_FALSE;
  332. tinyrl->line.str = new_buf;
  333. tinyrl->line.size = new_size;
  334. return BOOL_TRUE;
  335. }
  336. bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq)
  337. {
  338. bool_t result = BOOL_FALSE;
  339. switch (vt100_esc_decode(tinyrl->term, esc_seq)) {
  340. case VT100_CURSOR_UP:
  341. result = tinyrl_key_up(tinyrl, 0);
  342. break;
  343. case VT100_CURSOR_DOWN:
  344. result = tinyrl_key_down(tinyrl, 0);
  345. break;
  346. case VT100_CURSOR_LEFT:
  347. result = tinyrl_key_left(tinyrl, 0);
  348. break;
  349. case VT100_CURSOR_RIGHT:
  350. result = tinyrl_key_right(tinyrl, 0);
  351. break;
  352. case VT100_HOME:
  353. result = tinyrl_key_start_of_line(tinyrl, 0);
  354. break;
  355. case VT100_END:
  356. result = tinyrl_key_end_of_line(tinyrl, 0);
  357. break;
  358. case VT100_DELETE:
  359. result = tinyrl_key_delete(tinyrl, 0);
  360. break;
  361. case VT100_INSERT:
  362. case VT100_PGDOWN:
  363. case VT100_PGUP:
  364. case VT100_UNKNOWN:
  365. break;
  366. }
  367. return result;
  368. }
  369. bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len)
  370. {
  371. size_t new_size = tinyrl->line.len + len + 1;
  372. if (len == 0)
  373. return BOOL_TRUE;
  374. tinyrl_line_extend(tinyrl, new_size);
  375. if (tinyrl->line.pos < tinyrl->line.len) {
  376. memmove(tinyrl->line.str + tinyrl->line.pos + len,
  377. tinyrl->line.str + tinyrl->line.pos,
  378. tinyrl->line.len - tinyrl->line.pos);
  379. }
  380. memcpy(tinyrl->line.str + tinyrl->line.pos, text, len);
  381. tinyrl->line.pos += len;
  382. tinyrl->line.len += len;
  383. tinyrl->line.str[tinyrl->line.len] = '\0';
  384. return BOOL_TRUE;
  385. }
  386. bool_t tinyrl_line_delete(tinyrl_t *tinyrl, off_t start, size_t len)
  387. {
  388. if (start >= tinyrl->line.len)
  389. return BOOL_TRUE;
  390. if ((start + len) >= tinyrl->line.len) {
  391. tinyrl->line.len = start;
  392. } else {
  393. memmove(tinyrl->line.str + start,
  394. tinyrl->line.str + start + len,
  395. tinyrl->line.len - (start + len));
  396. tinyrl->line.len -= len;
  397. }
  398. tinyrl->line.pos = start;
  399. tinyrl->line.str[tinyrl->line.len] = '\0';
  400. return BOOL_TRUE;
  401. }
  402. static void move_cursor(const tinyrl_t *tinyrl, size_t cur_pos, size_t target_pos)
  403. {
  404. int rows = 0;
  405. int cols = 0;
  406. // Note: The '/' is not real math division. It's integer part of division
  407. // so we need separate division for two values.
  408. rows = (target_pos / tinyrl->width) - (cur_pos / tinyrl->width);
  409. cols = (target_pos % tinyrl->width) - (cur_pos % tinyrl->width);
  410. if (cols > 0)
  411. vt100_cursor_forward(tinyrl->term, cols);
  412. else if (cols < 0)
  413. vt100_cursor_back(tinyrl->term, -cols);
  414. if (rows > 0)
  415. vt100_cursor_down(tinyrl->term, rows);
  416. else if (rows < 0)
  417. vt100_cursor_up(tinyrl->term, -rows);
  418. }
  419. static size_t str_equal_part(const tinyrl_t *tinyrl,
  420. const char *s1, const char *s2)
  421. {
  422. const char *str1 = s1;
  423. const char *str2 = s2;
  424. if (!str1 || !str2)
  425. return 0;
  426. while (*str1 && *str2) {
  427. if (*str1 != *str2)
  428. break;
  429. str1++;
  430. str2++;
  431. }
  432. if (!tinyrl->utf8)
  433. return str1 - s1;
  434. // UTF8
  435. // If UTF8_10 byte (intermediate byte of UTF-8 sequence) is not equal
  436. // then we need to find starting of this UTF-8 character because whole
  437. // UTF-8 character is not equal.
  438. if (UTF8_10 == (*str1 & UTF8_MASK)) {
  439. // Skip intermediate bytes
  440. while ((str1 > s1) && (UTF8_10 == (*str1 & UTF8_MASK)))
  441. str1--;
  442. }
  443. return str1 - s1;
  444. }
  445. void tinyrl_save_last(tinyrl_t *tinyrl)
  446. {
  447. faux_str_free(tinyrl->last.str);
  448. tinyrl->last = tinyrl->line;
  449. tinyrl->last.str = faux_str_dup(tinyrl->line.str);
  450. }
  451. void tinyrl_reset_line_state(tinyrl_t *tinyrl)
  452. {
  453. faux_str_free(tinyrl->last.str);
  454. faux_bzero(&tinyrl->last, sizeof(tinyrl->last));
  455. }
  456. void tinyrl_reset_line(tinyrl_t *tinyrl)
  457. {
  458. tinyrl_line_delete(tinyrl, 0, tinyrl->line.len);
  459. }
  460. void tinyrl_redisplay(tinyrl_t *tinyrl)
  461. {
  462. size_t width = vt100_width(tinyrl->term);
  463. // unsigned int line_size = strlen(tinyrl->line);
  464. unsigned int line_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.len);
  465. size_t cols = 0;
  466. size_t eq_bytes = 0;
  467. // Prepare print position
  468. if (tinyrl->last.str && (width == tinyrl->width)) {
  469. size_t eq_chars = 0; // Printable symbols
  470. size_t last_pos_chars = 0;
  471. // If line and last line have the equal chars at begining
  472. eq_bytes = str_equal_part(tinyrl, tinyrl->line.str, tinyrl->last.str);
  473. eq_chars = utf8_nsyms(tinyrl->last.str, eq_bytes);
  474. last_pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  475. move_cursor(tinyrl, tinyrl->prompt_chars + last_pos_chars,
  476. tinyrl->prompt_chars + eq_chars);
  477. } else {
  478. // Prepare to resize
  479. if (width != tinyrl->width) {
  480. vt100_next_line(tinyrl->term);
  481. vt100_erase_down(tinyrl->term);
  482. }
  483. vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  484. }
  485. // Print current line
  486. vt100_printf(tinyrl->term, "%s", tinyrl->line.str + eq_bytes);
  487. cols = (tinyrl->prompt_chars + line_chars) % width;
  488. if ((cols == 0) && (tinyrl->line.len - eq_bytes))
  489. vt100_next_line(tinyrl->term);
  490. // Erase down if current line is shorter than previous one
  491. if (tinyrl->last.len > tinyrl->line.len)
  492. vt100_erase_down(tinyrl->term);
  493. // Move the cursor to the insertion point
  494. if (tinyrl->line.pos < tinyrl->line.len) {
  495. size_t pos_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.pos);
  496. move_cursor(tinyrl, tinyrl->prompt_chars + line_chars,
  497. tinyrl->prompt_chars + pos_chars);
  498. }
  499. // Update the display
  500. vt100_oflush(tinyrl->term);
  501. // Save the last line buffer
  502. tinyrl_save_last(tinyrl);
  503. tinyrl->width = width;
  504. }
  505. void tinyrl_crlf(const tinyrl_t *tinyrl)
  506. {
  507. vt100_printf(tinyrl->term, "\n");
  508. }
  509. // Jump to first free line after current multiline input
  510. void tinyrl_multi_crlf(const tinyrl_t *tinyrl)
  511. {
  512. size_t full_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.len);
  513. size_t pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  514. move_cursor(tinyrl, tinyrl->prompt_chars + pos_chars,
  515. tinyrl->prompt_chars + full_chars);
  516. tinyrl_crlf(tinyrl);
  517. vt100_oflush(tinyrl->term);
  518. }
  519. #if 0
  520. /*----------------------------------------------------------------------- */
  521. /*
  522. tinyrl is called whenever a line is edited in any way.
  523. It signals that if we are currently viewing a history line we should transfer it
  524. to the current buffer
  525. */
  526. static void changed_line(tinyrl_t * tinyrl)
  527. {
  528. /* if the current line is not our buffer then make it so */
  529. if (tinyrl->line != tinyrl->buffer) {
  530. /* replace the current buffer with the new details */
  531. free(tinyrl->buffer);
  532. tinyrl->line = tinyrl->buffer = lub_string_dup(tinyrl->line);
  533. tinyrl->buffer_size = strlen(tinyrl->buffer);
  534. assert(tinyrl->line);
  535. }
  536. }
  537. /*----------------------------------------------------------------------- */
  538. /*
  539. * A convenience function for displaying a list of strings in columnar
  540. * format on Readline's output stream. matches is the list of strings,
  541. * in argv format, such as a list of completion matches. len is the number
  542. * of strings in matches, and max is the length of the longest string in matches.
  543. * tinyrl function uses the setting of print-completions-horizontally to select
  544. * how the matches are displayed
  545. */
  546. void tinyrl_display_matches(const tinyrl_t *tinyrl,
  547. char *const *matches, unsigned int len, size_t max)
  548. {
  549. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  550. unsigned int cols, rows;
  551. /* Find out column and rows number */
  552. if (max < width)
  553. cols = (width + 1) / (max + 1); /* allow for a space between words */
  554. else
  555. cols = 1;
  556. rows = len / cols + 1;
  557. assert(matches);
  558. if (matches) {
  559. unsigned int r, c;
  560. len--, matches++; /* skip the subtitution string */
  561. /* Print out a table of completions */
  562. for (r = 0; r < rows && len; r++) {
  563. for (c = 0; c < cols && len; c++) {
  564. const char *match = *matches++;
  565. len--;
  566. if ((c + 1) == cols) /* Last str in row */
  567. tinyrl_vt100_printf(tinyrl->term, "%s",
  568. match);
  569. else
  570. tinyrl_vt100_printf(tinyrl->term, "%-*s ",
  571. max, match);
  572. }
  573. tinyrl_crlf(tinyrl);
  574. }
  575. }
  576. }
  577. /*-------------------------------------------------------- */
  578. /*
  579. * Returns an array of strings which is a list of completions for text.
  580. * If there are no completions, returns NULL. The first entry in the
  581. * returned array is the substitution for text. The remaining entries
  582. * are the possible completions. The array is terminated with a NULL pointer.
  583. *
  584. * entry_func is a function of two args, and returns a char *.
  585. * The first argument is text. The second is a state argument;
  586. * it is zero on the first call, and non-zero on subsequent calls.
  587. * entry_func returns a NULL pointer to the caller when there are no
  588. * more matches.
  589. */
  590. char **tinyrl_completion(tinyrl_t * tinyrl,
  591. const char *line, unsigned int start, unsigned int end,
  592. tinyrl_compentry_func_t * entry_func)
  593. {
  594. unsigned int state = 0;
  595. size_t size = 1;
  596. unsigned int offset = 1; /* Need at least one entry for the substitution */
  597. char **matches = NULL;
  598. char *match;
  599. /* duplicate the string upto the insertion point */
  600. char *text = lub_string_dupn(line, end);
  601. /* now try and find possible completions */
  602. while ((match = entry_func(tinyrl, text, start, state++))) {
  603. if (size == offset) {
  604. /* resize the buffer if needed - the +1 is for the NULL terminator */
  605. size += 10;
  606. matches =
  607. realloc(matches, (sizeof(char *) * (size + 1)));
  608. }
  609. /* not much we can do... */
  610. if (!matches)
  611. break;
  612. matches[offset] = match;
  613. /*
  614. * augment the substitute string with tinyrl entry
  615. */
  616. if (1 == offset) {
  617. /* let's be optimistic */
  618. matches[0] = lub_string_dup(match);
  619. } else {
  620. char *p = matches[0];
  621. size_t match_len = strlen(p);
  622. /* identify the common prefix */
  623. while ((tolower(*p) == tolower(*match)) && match_len--) {
  624. p++, match++;
  625. }
  626. /* terminate the prefix string */
  627. *p = '\0';
  628. }
  629. offset++;
  630. }
  631. /* be a good memory citizen */
  632. lub_string_free(text);
  633. if (matches)
  634. matches[offset] = NULL;
  635. return matches;
  636. }
  637. /*-------------------------------------------------------- */
  638. void tinyrl_delete_matches(char **tinyrl)
  639. {
  640. char **matches = tinyrl;
  641. while (*matches) {
  642. /* release the memory for each contained string */
  643. free(*matches++);
  644. }
  645. /* release the memory for the array */
  646. free(tinyrl);
  647. }
  648. /*-------------------------------------------------------- */
  649. /*-------------------------------------------------------- */
  650. /*
  651. * Ring the terminal bell, obeying the setting of bell-style.
  652. */
  653. void tinyrl_ding(const tinyrl_t * tinyrl)
  654. {
  655. tinyrl_vt100_ding(tinyrl->term);
  656. }
  657. /*-------------------------------------------------------- */
  658. void tinyrl_reset_line_state(tinyrl_t * tinyrl)
  659. {
  660. lub_string_free(tinyrl->last_buffer);
  661. tinyrl->last_buffer = NULL;
  662. tinyrl->last_line_size = 0;
  663. tinyrl_redisplay(tinyrl);
  664. }
  665. /*-------------------------------------------------------- */
  666. void tinyrl_replace_line(tinyrl_t * tinyrl, const char *text, int clear_undo)
  667. {
  668. size_t new_len = strlen(text);
  669. /* ignored for now */
  670. clear_undo = clear_undo;
  671. /* ensure there is sufficient space */
  672. if (tinyrl_extend_line_buffer(tinyrl, new_len)) {
  673. /* overwrite the current contents of the buffer */
  674. strcpy(tinyrl->buffer, text);
  675. /* set the insert point and end point */
  676. tinyrl->point = tinyrl->end = new_len;
  677. }
  678. tinyrl_redisplay(tinyrl);
  679. }
  680. /*-------------------------------------------------------- */
  681. static tinyrl_match_e
  682. tinyrl_do_complete(tinyrl_t * tinyrl, bool_t with_extensions)
  683. {
  684. tinyrl_match_e result = TINYRL_NO_MATCH;
  685. char **matches = NULL;
  686. unsigned int start, end;
  687. bool_t completion = BOOL_FALSE;
  688. bool_t prefix = BOOL_FALSE;
  689. int i = 0;
  690. /* find the start and end of the current word */
  691. start = end = tinyrl->point;
  692. while (start && !isspace(tinyrl->line[start - 1]))
  693. start--;
  694. if (tinyrl->attempted_completion_function) {
  695. tinyrl->completion_over = BOOL_FALSE;
  696. tinyrl->completion_error_over = BOOL_FALSE;
  697. /* try and complete the current line buffer */
  698. matches = tinyrl->attempted_completion_function(tinyrl,
  699. tinyrl->line, start, end);
  700. }
  701. if (!matches && (BOOL_FALSE == tinyrl->completion_over)) {
  702. /* insert default completion call here... */
  703. }
  704. if (!matches)
  705. return result;
  706. /* identify and insert a common prefix if there is one */
  707. if (0 != strncmp(matches[0], &tinyrl->line[start],
  708. strlen(matches[0]))) {
  709. /*
  710. * delete the original text not including
  711. * the current insertion point character
  712. */
  713. if (tinyrl->end != end)
  714. end--;
  715. tinyrl_delete_text(tinyrl, start, end);
  716. if (BOOL_FALSE == tinyrl_insert_text(tinyrl, matches[0]))
  717. return TINYRL_NO_MATCH;
  718. completion = BOOL_TRUE;
  719. }
  720. for (i = 1; matches[i]; i++) {
  721. /* tinyrl is just a prefix string */
  722. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  723. prefix = BOOL_TRUE;
  724. }
  725. /* is there more than one completion? */
  726. if (matches[2]) {
  727. char **tmp = matches;
  728. unsigned int max, len;
  729. max = len = 0;
  730. while (*tmp) {
  731. size_t size = strlen(*tmp++);
  732. len++;
  733. if (size > max)
  734. max = size;
  735. }
  736. if (completion)
  737. result = TINYRL_COMPLETED_AMBIGUOUS;
  738. else if (prefix)
  739. result = TINYRL_MATCH_WITH_EXTENSIONS;
  740. else
  741. result = TINYRL_AMBIGUOUS;
  742. if (with_extensions || !prefix) {
  743. /* Either we always want to show extensions or
  744. * we haven't been able to complete the current line
  745. * and there is just a prefix, so let the user see the options
  746. */
  747. tinyrl_crlf(tinyrl);
  748. tinyrl_display_matches(tinyrl, matches, len, max);
  749. tinyrl_reset_line_state(tinyrl);
  750. }
  751. } else {
  752. result = completion ?
  753. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  754. }
  755. /* free the memory */
  756. tinyrl_delete_matches(matches);
  757. /* redisplay the line */
  758. tinyrl_redisplay(tinyrl);
  759. return result;
  760. }
  761. /*-------------------------------------------------------- */
  762. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * tinyrl)
  763. {
  764. return tinyrl_do_complete(tinyrl, BOOL_TRUE);
  765. }
  766. /*-------------------------------------------------------- */
  767. tinyrl_match_e tinyrl_complete(tinyrl_t * tinyrl)
  768. {
  769. return tinyrl_do_complete(tinyrl, BOOL_FALSE);
  770. }
  771. /*-------------------------------------------------------- */
  772. void *tinyrl__get_context(const tinyrl_t * tinyrl)
  773. {
  774. return tinyrl->context;
  775. }
  776. /*--------------------------------------------------------- */
  777. const char *tinyrl__get_line(const tinyrl_t * tinyrl)
  778. {
  779. return tinyrl->line;
  780. }
  781. /*--------------------------------------------------------- */
  782. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * tinyrl)
  783. {
  784. return tinyrl->history;
  785. }
  786. /*--------------------------------------------------------- */
  787. void tinyrl_completion_over(tinyrl_t * tinyrl)
  788. {
  789. tinyrl->completion_over = BOOL_TRUE;
  790. }
  791. /*--------------------------------------------------------- */
  792. void tinyrl_completion_error_over(tinyrl_t * tinyrl)
  793. {
  794. tinyrl->completion_error_over = BOOL_TRUE;
  795. }
  796. /*--------------------------------------------------------- */
  797. bool_t tinyrl_is_completion_error_over(const tinyrl_t * tinyrl)
  798. {
  799. return tinyrl->completion_error_over;
  800. }
  801. /*--------------------------------------------------------- */
  802. void tinyrl_done(tinyrl_t * tinyrl)
  803. {
  804. tinyrl->done = BOOL_TRUE;
  805. }
  806. /*--------------------------------------------------------- */
  807. void tinyrl_enable_echo(tinyrl_t * tinyrl)
  808. {
  809. tinyrl->echo_enabled = BOOL_TRUE;
  810. }
  811. /*--------------------------------------------------------- */
  812. void tinyrl_disable_echo(tinyrl_t * tinyrl, char echo_char)
  813. {
  814. tinyrl->echo_enabled = BOOL_FALSE;
  815. tinyrl->echo_char = echo_char;
  816. }
  817. /*-------------------------------------------------------- */
  818. bool_t tinyrl_is_quoting(const tinyrl_t * tinyrl)
  819. {
  820. bool_t result = BOOL_FALSE;
  821. /* count the quotes upto the current insertion point */
  822. unsigned int i = 0;
  823. while (i < tinyrl->point) {
  824. if (result && (tinyrl->line[i] == '\\')) {
  825. i++;
  826. if (i >= tinyrl->point)
  827. break;
  828. i++;
  829. continue;
  830. }
  831. if (tinyrl->line[i++] == '"') {
  832. result = result ? BOOL_FALSE : BOOL_TRUE;
  833. }
  834. }
  835. return result;
  836. }
  837. /*-------------------------------------------------------- */
  838. bool_t tinyrl_is_empty(const tinyrl_t *tinyrl)
  839. {
  840. return (tinyrl->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  841. }
  842. /*--------------------------------------------------------- */
  843. void tinyrl_limit_line_length(tinyrl_t * tinyrl, unsigned int length)
  844. {
  845. tinyrl->max_line_length = length;
  846. }
  847. #endif