tinyrl.c 25 KB

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