tinyrl.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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. tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
  15. const char *hist_fname, size_t hist_stifle)
  16. {
  17. tinyrl_t *tinyrl = NULL;
  18. int i = 0;
  19. tinyrl = faux_zmalloc(sizeof(tinyrl_t));
  20. if (!tinyrl)
  21. return NULL;
  22. // Key handlers
  23. for (i = 0; i < NUM_HANDLERS; i++) {
  24. tinyrl->handlers[i] = tinyrl_key_default;
  25. }
  26. tinyrl->handlers[KEY_CR] = tinyrl_key_crlf;
  27. tinyrl->handlers[KEY_LF] = tinyrl_key_crlf;
  28. tinyrl->handlers[KEY_ETX] = tinyrl_key_interrupt;
  29. tinyrl->handlers[KEY_DEL] = tinyrl_key_backspace;
  30. tinyrl->handlers[KEY_BS] = tinyrl_key_backspace;
  31. tinyrl->handlers[KEY_EOT] = tinyrl_key_delete;
  32. tinyrl->handlers[KEY_FF] = tinyrl_key_clear_screen;
  33. tinyrl->handlers[KEY_NAK] = tinyrl_key_erase_line;
  34. tinyrl->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  35. tinyrl->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  36. tinyrl->handlers[KEY_VT] = tinyrl_key_kill;
  37. tinyrl->handlers[KEY_EM] = tinyrl_key_yank;
  38. tinyrl->handlers[KEY_HT] = tinyrl_key_tab;
  39. tinyrl->handlers[KEY_ETB] = tinyrl_key_backword;
  40. tinyrl->line = NULL;
  41. tinyrl->max_line_length = 0;
  42. tinyrl->prompt = NULL;
  43. tinyrl->prompt_size = 0;
  44. tinyrl->buffer = NULL;
  45. tinyrl->buffer_size = 0;
  46. tinyrl->done = BOOL_FALSE;
  47. tinyrl->completion_over = BOOL_FALSE;
  48. tinyrl->point = 0;
  49. tinyrl->end = 0;
  50. tinyrl->attempted_completion_function = NULL;
  51. tinyrl->keypress_fn = NULL;
  52. tinyrl->hotkey_fn = NULL;
  53. tinyrl->state = 0;
  54. tinyrl->kill_string = NULL;
  55. tinyrl->echo_char = '\0';
  56. tinyrl->echo_enabled = BOOL_TRUE;
  57. tinyrl->last_buffer = NULL;
  58. tinyrl->last_point = 0;
  59. tinyrl->last_line_size = 0;
  60. tinyrl->utf8 = BOOL_FALSE;
  61. // VT100 terminal
  62. tinyrl->term = vt100_new(istream, ostream);
  63. tinyrl->width = vt100_width(tinyrl->term);
  64. // History object
  65. tinyrl->hist = hist_new(hist_fname, hist_stifle);
  66. return tinyrl;
  67. }
  68. void tinyrl_free(tinyrl_t *tinyrl)
  69. {
  70. assert(tinyrl);
  71. if (!tinyrl)
  72. return;
  73. hist_free(tinyrl->hist);
  74. vt100_free(tinyrl->term);
  75. faux_str_free(tinyrl->buffer);
  76. faux_str_free(tinyrl->kill_string);
  77. faux_str_free(tinyrl->last_buffer);
  78. faux_str_free(tinyrl->prompt);
  79. faux_free(tinyrl);
  80. }
  81. static void tty_raw_mode(tinyrl_t *tinyrl)
  82. {
  83. struct termios new_termios = {};
  84. FILE *istream = NULL;
  85. int fd = -1;
  86. if (!tinyrl)
  87. return;
  88. istream = vt100_istream(tinyrl->term);
  89. if (!istream)
  90. return;
  91. fd = fileno(istream);
  92. if (tcgetattr(fd, &new_termios) < 0)
  93. return;
  94. new_termios.c_iflag = 0;
  95. new_termios.c_oflag = OPOST | ONLCR;
  96. new_termios.c_lflag = 0;
  97. new_termios.c_cc[VMIN] = 1;
  98. new_termios.c_cc[VTIME] = 0;
  99. // Mode switch
  100. tcsetattr(fd, TCSADRAIN, &new_termios);
  101. }
  102. static void tty_restore_mode(tinyrl_t *tinyrl)
  103. {
  104. FILE *istream = NULL;
  105. int fd = -1;
  106. istream = vt100_istream(tinyrl->term);
  107. if (!istream)
  108. return;
  109. fd = fileno(istream);
  110. // Do the mode switch
  111. tcsetattr(fd, TCSADRAIN, &tinyrl->default_termios);
  112. }
  113. #if 0
  114. /*----------------------------------------------------------------------- */
  115. /*
  116. tinyrl is called whenever a line is edited in any way.
  117. It signals that if we are currently viewing a history line we should transfer it
  118. to the current buffer
  119. */
  120. static void changed_line(tinyrl_t * tinyrl)
  121. {
  122. /* if the current line is not our buffer then make it so */
  123. if (tinyrl->line != tinyrl->buffer) {
  124. /* replace the current buffer with the new details */
  125. free(tinyrl->buffer);
  126. tinyrl->line = tinyrl->buffer = lub_string_dup(tinyrl->line);
  127. tinyrl->buffer_size = strlen(tinyrl->buffer);
  128. assert(tinyrl->line);
  129. }
  130. }
  131. /*----------------------------------------------------------------------- */
  132. static int tinyrl_timeout_default(tinyrl_t *tinyrl)
  133. {
  134. tinyrl = tinyrl; /* Happy compiler */
  135. /* Return -1 to close session on timeout */
  136. return -1;
  137. }
  138. static bool_t tinyrl_escape_seq(tinyrl_t *tinyrl, const char *esc_seq)
  139. {
  140. int key = 0;
  141. bool_t result = BOOL_FALSE;
  142. switch (tinyrl_vt100_escape_decode(tinyrl->term, esc_seq)) {
  143. case tinyrl_vt100_CURSOR_UP:
  144. result = tinyrl_key_up(tinyrl, key);
  145. break;
  146. case tinyrl_vt100_CURSOR_DOWN:
  147. result = tinyrl_key_down(tinyrl, key);
  148. break;
  149. case tinyrl_vt100_CURSOR_LEFT:
  150. result = tinyrl_key_left(tinyrl, key);
  151. break;
  152. case tinyrl_vt100_CURSOR_RIGHT:
  153. result = tinyrl_key_right(tinyrl, key);
  154. break;
  155. case tinyrl_vt100_HOME:
  156. result = tinyrl_key_start_of_line(tinyrl,key);
  157. break;
  158. case tinyrl_vt100_END:
  159. result = tinyrl_key_end_of_line(tinyrl,key);
  160. break;
  161. case tinyrl_vt100_DELETE:
  162. result = tinyrl_key_delete(tinyrl,key);
  163. break;
  164. case tinyrl_vt100_INSERT:
  165. case tinyrl_vt100_PGDOWN:
  166. case tinyrl_vt100_PGUP:
  167. case tinyrl_vt100_UNKNOWN:
  168. break;
  169. }
  170. return result;
  171. }
  172. /*-------------------------------------------------------- */
  173. int tinyrl_printf(const tinyrl_t * tinyrl, const char *fmt, ...)
  174. {
  175. va_list args;
  176. int len;
  177. va_start(args, fmt);
  178. len = tinyrl_vt100_vprintf(tinyrl->term, fmt, args);
  179. va_end(args);
  180. return len;
  181. }
  182. /*-------------------------------------------------------- */
  183. /*#####################################
  184. * EXPORTED INTERFACE
  185. *##################################### */
  186. /*----------------------------------------------------------------------- */
  187. int tinyrl_getchar(const tinyrl_t * tinyrl)
  188. {
  189. return tinyrl_vt100_getchar(tinyrl->term);
  190. }
  191. /*----------------------------------------------------------------------- */
  192. static void tinyrl_internal_print(const tinyrl_t * tinyrl, const char *text)
  193. {
  194. if (tinyrl->echo_enabled) {
  195. /* simply echo the line */
  196. tinyrl_vt100_printf(tinyrl->term, "%s", text);
  197. } else {
  198. /* replace the line with echo char if defined */
  199. if (tinyrl->echo_char) {
  200. unsigned int i = strlen(text);
  201. while (i--) {
  202. tinyrl_vt100_printf(tinyrl->term, "%c",
  203. tinyrl->echo_char);
  204. }
  205. }
  206. }
  207. }
  208. /*----------------------------------------------------------------------- */
  209. static void tinyrl_internal_position(const tinyrl_t *tinyrl, int prompt_len,
  210. int line_len, unsigned int width)
  211. {
  212. int rows, cols;
  213. rows = ((line_len + prompt_len) / width) - (prompt_len / width);
  214. cols = ((line_len + prompt_len) % width) - (prompt_len % width);
  215. if (cols > 0)
  216. tinyrl_vt100_cursor_back(tinyrl->term, cols);
  217. else if (cols < 0)
  218. tinyrl_vt100_cursor_forward(tinyrl->term, -cols);
  219. if (rows > 0)
  220. tinyrl_vt100_cursor_up(tinyrl->term, rows);
  221. else if (rows < 0)
  222. tinyrl_vt100_cursor_down(tinyrl->term, -rows);
  223. }
  224. /*-------------------------------------------------------- */
  225. /* Jump to first free line after current multiline input */
  226. void tinyrl_multi_crlf(const tinyrl_t * tinyrl)
  227. {
  228. unsigned int line_size = strlen(tinyrl->last_buffer);
  229. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, line_size);
  230. unsigned int count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  231. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + line_len,
  232. - (line_len - count), tinyrl->width);
  233. tinyrl_crlf(tinyrl);
  234. tinyrl_vt100_oflush(tinyrl->term);
  235. }
  236. /*----------------------------------------------------------------------- */
  237. void tinyrl_redisplay(tinyrl_t * tinyrl)
  238. {
  239. unsigned int line_size = strlen(tinyrl->line);
  240. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->line, line_size);
  241. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  242. unsigned int count, eq_chars = 0;
  243. int cols;
  244. /* Prepare print position */
  245. if (tinyrl->last_buffer && (width == tinyrl->width)) {
  246. unsigned int eq_len = 0;
  247. /* If line and last line have the equal chars at begining */
  248. eq_chars = lub_string_equal_part(tinyrl->line, tinyrl->last_buffer,
  249. tinyrl->utf8);
  250. eq_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, eq_chars);
  251. count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  252. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + eq_len,
  253. count - eq_len, width);
  254. } else {
  255. /* Prepare to resize */
  256. if (width != tinyrl->width) {
  257. tinyrl_vt100_next_line(tinyrl->term);
  258. tinyrl_vt100_erase_down(tinyrl->term);
  259. }
  260. tinyrl_vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  261. }
  262. /* Print current line */
  263. tinyrl_internal_print(tinyrl, tinyrl->line + eq_chars);
  264. cols = (tinyrl->prompt_len + line_len) % width;
  265. if (!cols && (line_size - eq_chars))
  266. tinyrl_vt100_next_line(tinyrl->term);
  267. /* Erase down if current line is shorter than previous one */
  268. if (tinyrl->last_line_size > line_size)
  269. tinyrl_vt100_erase_down(tinyrl->term);
  270. /* Move the cursor to the insertion point */
  271. if (tinyrl->point < line_size) {
  272. unsigned int pre_len = utf8_nsyms(tinyrl,
  273. tinyrl->line, tinyrl->point);
  274. count = utf8_nsyms(tinyrl, tinyrl->line + tinyrl->point,
  275. line_size - tinyrl->point);
  276. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + pre_len,
  277. count, width);
  278. }
  279. /* Update the display */
  280. tinyrl_vt100_oflush(tinyrl->term);
  281. /* Save the last line buffer */
  282. lub_string_free(tinyrl->last_buffer);
  283. tinyrl->last_buffer = lub_string_dup(tinyrl->line);
  284. tinyrl->last_point = tinyrl->point;
  285. tinyrl->width = width;
  286. tinyrl->last_line_size = line_size;
  287. }
  288. /*----------------------------------------------------------------------- */
  289. tinyrl_t *tinyrl_new(FILE * istream, FILE * ostream,
  290. unsigned int stifle, tinyrl_completion_func_t * complete_fn)
  291. {
  292. tinyrl_t *tinyrl = NULL;
  293. tinyrl = malloc(sizeof(tinyrl_t));
  294. if (tinyrl)
  295. tinyrl_init(tinyrl, istream, ostream, stifle, complete_fn);
  296. return tinyrl;
  297. }
  298. /*----------------------------------------------------------------------- */
  299. static char *internal_insertline(tinyrl_t * tinyrl, char *buffer)
  300. {
  301. char *p;
  302. char *s = buffer;
  303. /* strip any spurious '\r' or '\n' */
  304. if ((p = strchr(buffer, '\r')))
  305. *p = '\0';
  306. if ((p = strchr(buffer, '\n')))
  307. *p = '\0';
  308. /* skip any whitespace at the beginning of the line */
  309. if (0 == tinyrl->point) {
  310. while (*s && isspace(*s))
  311. s++;
  312. }
  313. if (*s) {
  314. /* append tinyrl string to the input buffer */
  315. (void)tinyrl_insert_text(tinyrl, s);
  316. }
  317. /* echo the command to the output stream */
  318. tinyrl_redisplay(tinyrl);
  319. return s;
  320. }
  321. /*----------------------------------------------------------------------- */
  322. static char *internal_readline(tinyrl_t * tinyrl,
  323. void *context, const char *str)
  324. {
  325. FILE *istream = tinyrl_vt100__get_istream(tinyrl->term);
  326. char *result = NULL;
  327. int lerrno = 0;
  328. tinyrl->done = BOOL_FALSE;
  329. tinyrl->point = 0;
  330. tinyrl->end = 0;
  331. tinyrl->buffer = lub_string_dup("");
  332. tinyrl->buffer_size = strlen(tinyrl->buffer);
  333. tinyrl->line = tinyrl->buffer;
  334. tinyrl->context = context;
  335. /* Interactive session */
  336. if (tinyrl->isatty && !str) {
  337. unsigned int utf8_cont = 0; /* UTF-8 continue bytes */
  338. unsigned int esc_cont = 0; /* Escape sequence continues */
  339. char esc_seq[10]; /* Buffer for ESC sequence */
  340. char *esc_p = esc_seq;
  341. /* Set the terminal into raw mode */
  342. tty_raw_mode(tinyrl);
  343. tinyrl_reset_line_state(tinyrl);
  344. while (!tinyrl->done) {
  345. int key;
  346. key = tinyrl_getchar(tinyrl);
  347. /* Error || EOF || Timeout */
  348. if (key < 0) {
  349. if ((VT100_TIMEOUT == key) &&
  350. !tinyrl->timeout_fn(tinyrl))
  351. continue;
  352. /* It's time to finish the session */
  353. tinyrl->done = BOOL_TRUE;
  354. tinyrl->line = NULL;
  355. lerrno = ENOENT;
  356. continue;
  357. }
  358. /* Real key pressed */
  359. /* Common callback for any key */
  360. if (tinyrl->keypress_fn)
  361. tinyrl->keypress_fn(tinyrl, key);
  362. /* Check for ESC sequence. It's a special case. */
  363. if (!esc_cont && (key == KEY_ESC)) {
  364. esc_cont = 1; /* Start ESC sequence */
  365. esc_p = esc_seq;
  366. continue;
  367. }
  368. if (esc_cont) {
  369. /* Broken sequence */
  370. if (esc_p >= (esc_seq + sizeof(esc_seq) - 1)) {
  371. esc_cont = 0;
  372. continue;
  373. }
  374. /* Dump the control sequence into sequence buffer
  375. ANSI standard control sequences will end
  376. with a character between 64 - 126 */
  377. *esc_p = key & 0xff;
  378. esc_p++;
  379. /* tinyrl is an ANSI control sequence terminator code */
  380. if ((key != '[') && (key > 63)) {
  381. *esc_p = '\0';
  382. tinyrl_escape_seq(tinyrl, esc_seq);
  383. esc_cont = 0;
  384. tinyrl_redisplay(tinyrl);
  385. }
  386. continue;
  387. }
  388. /* Call the handler for tinyrl key */
  389. if (!tinyrl->handlers[key](tinyrl, key))
  390. tinyrl_ding(tinyrl);
  391. if (tinyrl->done) /* Some handler set the done flag */
  392. continue; /* It will break the loop */
  393. if (tinyrl->utf8) {
  394. if (!(UTF8_7BIT_MASK & key)) /* ASCII char */
  395. utf8_cont = 0;
  396. else if (utf8_cont && (UTF8_10 == (key & UTF8_MASK))) /* Continue byte */
  397. utf8_cont--;
  398. else if (UTF8_11 == (key & UTF8_MASK)) { /* First byte of multibyte char */
  399. /* Find out number of char's bytes */
  400. int b = key;
  401. utf8_cont = 0;
  402. while ((utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  403. utf8_cont++;
  404. b = b << 1;
  405. }
  406. }
  407. }
  408. /* For non UTF-8 encoding the utf8_cont is always 0.
  409. For UTF-8 it's 0 when one-byte symbol or we get
  410. all bytes for the current multibyte character. */
  411. if (!utf8_cont)
  412. tinyrl_redisplay(tinyrl);
  413. }
  414. /* If the last character in the line (other than NULL)
  415. is a space remove it. */
  416. if (tinyrl->end && tinyrl->line && isspace(tinyrl->line[tinyrl->end - 1]))
  417. tinyrl_delete_text(tinyrl, tinyrl->end - 1, tinyrl->end);
  418. /* Restores the terminal mode */
  419. tty_restore_mode(tinyrl);
  420. /* Non-interactive session */
  421. } else {
  422. char *s = NULL, buffer[80];
  423. size_t len = sizeof(buffer);
  424. char *tmp = NULL;
  425. /* manually reset the line state without redisplaying */
  426. lub_string_free(tinyrl->last_buffer);
  427. tinyrl->last_buffer = NULL;
  428. if (str) {
  429. tmp = lub_string_dup(str);
  430. internal_insertline(tinyrl, tmp);
  431. } else {
  432. while (istream && (sizeof(buffer) == len) &&
  433. (s = fgets(buffer, sizeof(buffer), istream))) {
  434. s = internal_insertline(tinyrl, buffer);
  435. len = strlen(buffer) + 1; /* account for the '\0' */
  436. }
  437. if (!s || ((tinyrl->line[0] == '\0') && feof(istream))) {
  438. /* time to finish the session */
  439. tinyrl->line = NULL;
  440. lerrno = ENOENT;
  441. }
  442. }
  443. /*
  444. * check against fgets returning null as either error or end of file.
  445. * tinyrl is a measure to stop potential task spin on encountering an
  446. * error from fgets.
  447. */
  448. if (tinyrl->line && !tinyrl->handlers[KEY_LF](tinyrl, KEY_LF)) {
  449. /* an issue has occured */
  450. tinyrl->line = NULL;
  451. lerrno = ENOEXEC;
  452. }
  453. if (str)
  454. lub_string_free(tmp);
  455. }
  456. /*
  457. * duplicate the string for return to the client
  458. * we have to duplicate as we may be referencing a
  459. * history entry or our internal buffer
  460. */
  461. result = tinyrl->line ? lub_string_dup(tinyrl->line) : NULL;
  462. /* free our internal buffer */
  463. free(tinyrl->buffer);
  464. tinyrl->buffer = NULL;
  465. if (!result)
  466. errno = lerrno; /* get saved errno */
  467. return result;
  468. }
  469. /*----------------------------------------------------------------------- */
  470. char *tinyrl_readline(tinyrl_t * tinyrl, void *context)
  471. {
  472. return internal_readline(tinyrl, context, NULL);
  473. }
  474. /*----------------------------------------------------------------------- */
  475. char *tinyrl_forceline(tinyrl_t * tinyrl, void *context, const char *line)
  476. {
  477. return internal_readline(tinyrl, context, line);
  478. }
  479. /*----------------------------------------------------------------------- */
  480. /*
  481. * Ensure that buffer has enough space to hold len characters,
  482. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  483. * if the line is successfully extended, BOOL_FALSE if not.
  484. */
  485. bool_t tinyrl_extend_line_buffer(tinyrl_t * tinyrl, unsigned int len)
  486. {
  487. bool_t result = BOOL_TRUE;
  488. char *new_buffer;
  489. size_t new_len = len;
  490. if (tinyrl->buffer_size >= len)
  491. return result;
  492. /*
  493. * What we do depends on whether we are limited by
  494. * memory or a user imposed limit.
  495. */
  496. if (tinyrl->max_line_length == 0) {
  497. /* make sure we don't realloc too often */
  498. if (new_len < tinyrl->buffer_size + 10)
  499. new_len = tinyrl->buffer_size + 10;
  500. /* leave space for terminator */
  501. new_buffer = realloc(tinyrl->buffer, new_len + 1);
  502. if (!new_buffer) {
  503. tinyrl_ding(tinyrl);
  504. result = BOOL_FALSE;
  505. } else {
  506. tinyrl->buffer_size = new_len;
  507. tinyrl->line = tinyrl->buffer = new_buffer;
  508. }
  509. } else {
  510. if (new_len < tinyrl->max_line_length) {
  511. /* Just reallocate once to the max size */
  512. new_buffer = realloc(tinyrl->buffer,
  513. tinyrl->max_line_length);
  514. if (!new_buffer) {
  515. tinyrl_ding(tinyrl);
  516. result = BOOL_FALSE;
  517. } else {
  518. tinyrl->buffer_size =
  519. tinyrl->max_line_length - 1;
  520. tinyrl->line = tinyrl->buffer = new_buffer;
  521. }
  522. } else {
  523. tinyrl_ding(tinyrl);
  524. result = BOOL_FALSE;
  525. }
  526. }
  527. return result;
  528. }
  529. /*----------------------------------------------------------------------- */
  530. /*
  531. * Insert text into the line at the current cursor position.
  532. */
  533. bool_t tinyrl_insert_text(tinyrl_t * tinyrl, const char *text)
  534. {
  535. unsigned int delta = strlen(text);
  536. /*
  537. * If the client wants to change the line ensure that the line and buffer
  538. * references are in sync
  539. */
  540. changed_line(tinyrl);
  541. if ((delta + tinyrl->end) > (tinyrl->buffer_size)) {
  542. /* extend the current buffer */
  543. if (BOOL_FALSE ==
  544. tinyrl_extend_line_buffer(tinyrl, tinyrl->end + delta))
  545. return BOOL_FALSE;
  546. }
  547. if (tinyrl->point < tinyrl->end) {
  548. /* move the current text to the right (including the terminator) */
  549. memmove(&tinyrl->buffer[tinyrl->point + delta],
  550. &tinyrl->buffer[tinyrl->point],
  551. (tinyrl->end - tinyrl->point) + 1);
  552. } else {
  553. /* terminate the string */
  554. tinyrl->buffer[tinyrl->end + delta] = '\0';
  555. }
  556. /* insert the new text */
  557. strncpy(&tinyrl->buffer[tinyrl->point], text, delta);
  558. /* now update the indexes */
  559. tinyrl->point += delta;
  560. tinyrl->end += delta;
  561. return BOOL_TRUE;
  562. }
  563. /*----------------------------------------------------------------------- */
  564. /*
  565. * A convenience function for displaying a list of strings in columnar
  566. * format on Readline's output stream. matches is the list of strings,
  567. * in argv format, such as a list of completion matches. len is the number
  568. * of strings in matches, and max is the length of the longest string in matches.
  569. * tinyrl function uses the setting of print-completions-horizontally to select
  570. * how the matches are displayed
  571. */
  572. void tinyrl_display_matches(const tinyrl_t *tinyrl,
  573. char *const *matches, unsigned int len, size_t max)
  574. {
  575. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  576. unsigned int cols, rows;
  577. /* Find out column and rows number */
  578. if (max < width)
  579. cols = (width + 1) / (max + 1); /* allow for a space between words */
  580. else
  581. cols = 1;
  582. rows = len / cols + 1;
  583. assert(matches);
  584. if (matches) {
  585. unsigned int r, c;
  586. len--, matches++; /* skip the subtitution string */
  587. /* Print out a table of completions */
  588. for (r = 0; r < rows && len; r++) {
  589. for (c = 0; c < cols && len; c++) {
  590. const char *match = *matches++;
  591. len--;
  592. if ((c + 1) == cols) /* Last str in row */
  593. tinyrl_vt100_printf(tinyrl->term, "%s",
  594. match);
  595. else
  596. tinyrl_vt100_printf(tinyrl->term, "%-*s ",
  597. max, match);
  598. }
  599. tinyrl_crlf(tinyrl);
  600. }
  601. }
  602. }
  603. /*----------------------------------------------------------------------- */
  604. /*
  605. * Delete the text between start and end in the current line. (inclusive)
  606. * tinyrl adjusts the rl_point and rl_end indexes appropriately.
  607. */
  608. void tinyrl_delete_text(tinyrl_t * tinyrl, unsigned int start, unsigned int end)
  609. {
  610. unsigned int delta;
  611. /*
  612. * If the client wants to change the line ensure that the line and buffer
  613. * references are in sync
  614. */
  615. changed_line(tinyrl);
  616. /* make sure we play it safe */
  617. if (start > end) {
  618. unsigned int tmp = end;
  619. start = end;
  620. end = tmp;
  621. }
  622. if (end > tinyrl->end)
  623. end = tinyrl->end;
  624. delta = (end - start) + 1;
  625. /* move any text which is left */
  626. memmove(&tinyrl->buffer[start],
  627. &tinyrl->buffer[start + delta], tinyrl->end - end);
  628. /* now adjust the indexs */
  629. if (tinyrl->point >= start) {
  630. if (tinyrl->point > end) {
  631. /* move the insertion point back appropriately */
  632. tinyrl->point -= delta;
  633. } else {
  634. /* move the insertion point to the start */
  635. tinyrl->point = start;
  636. }
  637. }
  638. if (tinyrl->end > end)
  639. tinyrl->end -= delta;
  640. else
  641. tinyrl->end = start;
  642. /* put a terminator at the end of the buffer */
  643. tinyrl->buffer[tinyrl->end] = '\0';
  644. }
  645. /*----------------------------------------------------------------------- */
  646. bool_t tinyrl_bind_key(tinyrl_t * tinyrl, int key, tinyrl_key_func_t * fn)
  647. {
  648. bool_t result = BOOL_FALSE;
  649. if ((key >= 0) && (key < 256)) {
  650. /* set the key handling function */
  651. tinyrl->handlers[key] = fn;
  652. result = BOOL_TRUE;
  653. }
  654. return result;
  655. }
  656. /*-------------------------------------------------------- */
  657. /*
  658. * Returns an array of strings which is a list of completions for text.
  659. * If there are no completions, returns NULL. The first entry in the
  660. * returned array is the substitution for text. The remaining entries
  661. * are the possible completions. The array is terminated with a NULL pointer.
  662. *
  663. * entry_func is a function of two args, and returns a char *.
  664. * The first argument is text. The second is a state argument;
  665. * it is zero on the first call, and non-zero on subsequent calls.
  666. * entry_func returns a NULL pointer to the caller when there are no
  667. * more matches.
  668. */
  669. char **tinyrl_completion(tinyrl_t * tinyrl,
  670. const char *line, unsigned int start, unsigned int end,
  671. tinyrl_compentry_func_t * entry_func)
  672. {
  673. unsigned int state = 0;
  674. size_t size = 1;
  675. unsigned int offset = 1; /* Need at least one entry for the substitution */
  676. char **matches = NULL;
  677. char *match;
  678. /* duplicate the string upto the insertion point */
  679. char *text = lub_string_dupn(line, end);
  680. /* now try and find possible completions */
  681. while ((match = entry_func(tinyrl, text, start, state++))) {
  682. if (size == offset) {
  683. /* resize the buffer if needed - the +1 is for the NULL terminator */
  684. size += 10;
  685. matches =
  686. realloc(matches, (sizeof(char *) * (size + 1)));
  687. }
  688. /* not much we can do... */
  689. if (!matches)
  690. break;
  691. matches[offset] = match;
  692. /*
  693. * augment the substitute string with tinyrl entry
  694. */
  695. if (1 == offset) {
  696. /* let's be optimistic */
  697. matches[0] = lub_string_dup(match);
  698. } else {
  699. char *p = matches[0];
  700. size_t match_len = strlen(p);
  701. /* identify the common prefix */
  702. while ((tolower(*p) == tolower(*match)) && match_len--) {
  703. p++, match++;
  704. }
  705. /* terminate the prefix string */
  706. *p = '\0';
  707. }
  708. offset++;
  709. }
  710. /* be a good memory citizen */
  711. lub_string_free(text);
  712. if (matches)
  713. matches[offset] = NULL;
  714. return matches;
  715. }
  716. /*-------------------------------------------------------- */
  717. void tinyrl_delete_matches(char **tinyrl)
  718. {
  719. char **matches = tinyrl;
  720. while (*matches) {
  721. /* release the memory for each contained string */
  722. free(*matches++);
  723. }
  724. /* release the memory for the array */
  725. free(tinyrl);
  726. }
  727. /*-------------------------------------------------------- */
  728. void tinyrl_crlf(const tinyrl_t * tinyrl)
  729. {
  730. tinyrl_vt100_printf(tinyrl->term, "\n");
  731. }
  732. /*-------------------------------------------------------- */
  733. /*
  734. * Ring the terminal bell, obeying the setting of bell-style.
  735. */
  736. void tinyrl_ding(const tinyrl_t * tinyrl)
  737. {
  738. tinyrl_vt100_ding(tinyrl->term);
  739. }
  740. /*-------------------------------------------------------- */
  741. void tinyrl_reset_line_state(tinyrl_t * tinyrl)
  742. {
  743. lub_string_free(tinyrl->last_buffer);
  744. tinyrl->last_buffer = NULL;
  745. tinyrl->last_line_size = 0;
  746. tinyrl_redisplay(tinyrl);
  747. }
  748. /*-------------------------------------------------------- */
  749. void tinyrl_replace_line(tinyrl_t * tinyrl, const char *text, int clear_undo)
  750. {
  751. size_t new_len = strlen(text);
  752. /* ignored for now */
  753. clear_undo = clear_undo;
  754. /* ensure there is sufficient space */
  755. if (tinyrl_extend_line_buffer(tinyrl, new_len)) {
  756. /* overwrite the current contents of the buffer */
  757. strcpy(tinyrl->buffer, text);
  758. /* set the insert point and end point */
  759. tinyrl->point = tinyrl->end = new_len;
  760. }
  761. tinyrl_redisplay(tinyrl);
  762. }
  763. /*-------------------------------------------------------- */
  764. static tinyrl_match_e
  765. tinyrl_do_complete(tinyrl_t * tinyrl, bool_t with_extensions)
  766. {
  767. tinyrl_match_e result = TINYRL_NO_MATCH;
  768. char **matches = NULL;
  769. unsigned int start, end;
  770. bool_t completion = BOOL_FALSE;
  771. bool_t prefix = BOOL_FALSE;
  772. int i = 0;
  773. /* find the start and end of the current word */
  774. start = end = tinyrl->point;
  775. while (start && !isspace(tinyrl->line[start - 1]))
  776. start--;
  777. if (tinyrl->attempted_completion_function) {
  778. tinyrl->completion_over = BOOL_FALSE;
  779. tinyrl->completion_error_over = BOOL_FALSE;
  780. /* try and complete the current line buffer */
  781. matches = tinyrl->attempted_completion_function(tinyrl,
  782. tinyrl->line, start, end);
  783. }
  784. if (!matches && (BOOL_FALSE == tinyrl->completion_over)) {
  785. /* insert default completion call here... */
  786. }
  787. if (!matches)
  788. return result;
  789. /* identify and insert a common prefix if there is one */
  790. if (0 != strncmp(matches[0], &tinyrl->line[start],
  791. strlen(matches[0]))) {
  792. /*
  793. * delete the original text not including
  794. * the current insertion point character
  795. */
  796. if (tinyrl->end != end)
  797. end--;
  798. tinyrl_delete_text(tinyrl, start, end);
  799. if (BOOL_FALSE == tinyrl_insert_text(tinyrl, matches[0]))
  800. return TINYRL_NO_MATCH;
  801. completion = BOOL_TRUE;
  802. }
  803. for (i = 1; matches[i]; i++) {
  804. /* tinyrl is just a prefix string */
  805. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  806. prefix = BOOL_TRUE;
  807. }
  808. /* is there more than one completion? */
  809. if (matches[2]) {
  810. char **tmp = matches;
  811. unsigned int max, len;
  812. max = len = 0;
  813. while (*tmp) {
  814. size_t size = strlen(*tmp++);
  815. len++;
  816. if (size > max)
  817. max = size;
  818. }
  819. if (completion)
  820. result = TINYRL_COMPLETED_AMBIGUOUS;
  821. else if (prefix)
  822. result = TINYRL_MATCH_WITH_EXTENSIONS;
  823. else
  824. result = TINYRL_AMBIGUOUS;
  825. if (with_extensions || !prefix) {
  826. /* Either we always want to show extensions or
  827. * we haven't been able to complete the current line
  828. * and there is just a prefix, so let the user see the options
  829. */
  830. tinyrl_crlf(tinyrl);
  831. tinyrl_display_matches(tinyrl, matches, len, max);
  832. tinyrl_reset_line_state(tinyrl);
  833. }
  834. } else {
  835. result = completion ?
  836. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  837. }
  838. /* free the memory */
  839. tinyrl_delete_matches(matches);
  840. /* redisplay the line */
  841. tinyrl_redisplay(tinyrl);
  842. return result;
  843. }
  844. /*-------------------------------------------------------- */
  845. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * tinyrl)
  846. {
  847. return tinyrl_do_complete(tinyrl, BOOL_TRUE);
  848. }
  849. /*-------------------------------------------------------- */
  850. tinyrl_match_e tinyrl_complete(tinyrl_t * tinyrl)
  851. {
  852. return tinyrl_do_complete(tinyrl, BOOL_FALSE);
  853. }
  854. /*-------------------------------------------------------- */
  855. void *tinyrl__get_context(const tinyrl_t * tinyrl)
  856. {
  857. return tinyrl->context;
  858. }
  859. /*--------------------------------------------------------- */
  860. const char *tinyrl__get_line(const tinyrl_t * tinyrl)
  861. {
  862. return tinyrl->line;
  863. }
  864. /*--------------------------------------------------------- */
  865. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * tinyrl)
  866. {
  867. return tinyrl->history;
  868. }
  869. /*--------------------------------------------------------- */
  870. void tinyrl_completion_over(tinyrl_t * tinyrl)
  871. {
  872. tinyrl->completion_over = BOOL_TRUE;
  873. }
  874. /*--------------------------------------------------------- */
  875. void tinyrl_completion_error_over(tinyrl_t * tinyrl)
  876. {
  877. tinyrl->completion_error_over = BOOL_TRUE;
  878. }
  879. /*--------------------------------------------------------- */
  880. bool_t tinyrl_is_completion_error_over(const tinyrl_t * tinyrl)
  881. {
  882. return tinyrl->completion_error_over;
  883. }
  884. /*--------------------------------------------------------- */
  885. void tinyrl_done(tinyrl_t * tinyrl)
  886. {
  887. tinyrl->done = BOOL_TRUE;
  888. }
  889. /*--------------------------------------------------------- */
  890. void tinyrl_enable_echo(tinyrl_t * tinyrl)
  891. {
  892. tinyrl->echo_enabled = BOOL_TRUE;
  893. }
  894. /*--------------------------------------------------------- */
  895. void tinyrl_disable_echo(tinyrl_t * tinyrl, char echo_char)
  896. {
  897. tinyrl->echo_enabled = BOOL_FALSE;
  898. tinyrl->echo_char = echo_char;
  899. }
  900. /*--------------------------------------------------------- */
  901. void tinyrl__set_istream(tinyrl_t * tinyrl, FILE * istream)
  902. {
  903. tinyrl_vt100__set_istream(tinyrl->term, istream);
  904. if (istream) {
  905. int fd;
  906. tinyrl->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
  907. /* Save terminal settings to restore on exit */
  908. fd = fileno(istream);
  909. tcgetattr(fd, &tinyrl->default_termios);
  910. } else
  911. tinyrl->isatty = BOOL_FALSE;
  912. }
  913. /*-------------------------------------------------------- */
  914. bool_t tinyrl__get_isatty(const tinyrl_t * tinyrl)
  915. {
  916. return tinyrl->isatty;
  917. }
  918. /*-------------------------------------------------------- */
  919. FILE *tinyrl__get_istream(const tinyrl_t * tinyrl)
  920. {
  921. return tinyrl_vt100__get_istream(tinyrl->term);
  922. }
  923. /*-------------------------------------------------------- */
  924. FILE *tinyrl__get_ostream(const tinyrl_t * tinyrl)
  925. {
  926. return tinyrl_vt100__get_ostream(tinyrl->term);
  927. }
  928. /*-------------------------------------------------------- */
  929. const char *tinyrl__get_prompt(const tinyrl_t * tinyrl)
  930. {
  931. return tinyrl->prompt;
  932. }
  933. /*-------------------------------------------------------- */
  934. void tinyrl__set_prompt(tinyrl_t *tinyrl, const char *prompt)
  935. {
  936. if (tinyrl->prompt) {
  937. lub_string_free(tinyrl->prompt);
  938. tinyrl->prompt_size = 0;
  939. tinyrl->prompt_len = 0;
  940. }
  941. tinyrl->prompt = lub_string_dup(prompt);
  942. if (tinyrl->prompt) {
  943. tinyrl->prompt_size = strlen(tinyrl->prompt);
  944. tinyrl->prompt_len = utf8_nsyms(tinyrl, tinyrl->prompt,
  945. tinyrl->prompt_size);
  946. }
  947. }
  948. /*-------------------------------------------------------- */
  949. bool_t tinyrl__get_utf8(const tinyrl_t * tinyrl)
  950. {
  951. return tinyrl->utf8;
  952. }
  953. /*-------------------------------------------------------- */
  954. void tinyrl__set_utf8(tinyrl_t * tinyrl, bool_t utf8)
  955. {
  956. tinyrl->utf8 = utf8;
  957. }
  958. /*-------------------------------------------------------- */
  959. void tinyrl__set_timeout(tinyrl_t *tinyrl, int timeout)
  960. {
  961. tinyrl_vt100__set_timeout(tinyrl->term, timeout);
  962. }
  963. /*-------------------------------------------------------- */
  964. void tinyrl__set_timeout_fn(tinyrl_t *tinyrl,
  965. tinyrl_timeout_fn_t *fn)
  966. {
  967. tinyrl->timeout_fn = fn;
  968. }
  969. /*-------------------------------------------------------- */
  970. void tinyrl__set_keypress_fn(tinyrl_t *tinyrl,
  971. tinyrl_keypress_fn_t *fn)
  972. {
  973. tinyrl->keypress_fn = fn;
  974. }
  975. /*-------------------------------------------------------- */
  976. void tinyrl__set_hotkey_fn(tinyrl_t *tinyrl,
  977. tinyrl_key_func_t *fn)
  978. {
  979. tinyrl->hotkey_fn = fn;
  980. }
  981. /*-------------------------------------------------------- */
  982. bool_t tinyrl_is_quoting(const tinyrl_t * tinyrl)
  983. {
  984. bool_t result = BOOL_FALSE;
  985. /* count the quotes upto the current insertion point */
  986. unsigned int i = 0;
  987. while (i < tinyrl->point) {
  988. if (result && (tinyrl->line[i] == '\\')) {
  989. i++;
  990. if (i >= tinyrl->point)
  991. break;
  992. i++;
  993. continue;
  994. }
  995. if (tinyrl->line[i++] == '"') {
  996. result = result ? BOOL_FALSE : BOOL_TRUE;
  997. }
  998. }
  999. return result;
  1000. }
  1001. /*-------------------------------------------------------- */
  1002. bool_t tinyrl_is_empty(const tinyrl_t *tinyrl)
  1003. {
  1004. return (tinyrl->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  1005. }
  1006. /*--------------------------------------------------------- */
  1007. void tinyrl_limit_line_length(tinyrl_t * tinyrl, unsigned int length)
  1008. {
  1009. tinyrl->max_line_length = length;
  1010. }
  1011. /*--------------------------------------------------------- */
  1012. extern unsigned int tinyrl__get_width(const tinyrl_t *tinyrl)
  1013. {
  1014. return tinyrl_vt100__get_width(tinyrl->term);
  1015. }
  1016. /*--------------------------------------------------------- */
  1017. extern unsigned int tinyrl__get_height(const tinyrl_t *tinyrl)
  1018. {
  1019. return tinyrl_vt100__get_height(tinyrl->term);
  1020. }
  1021. /*----------------------------------------------------------*/
  1022. int tinyrl__save_history(const tinyrl_t *tinyrl, const char *fname)
  1023. {
  1024. return tinyrl_history_save(tinyrl->history, fname);
  1025. }
  1026. /*----------------------------------------------------------*/
  1027. int tinyrl__restore_history(tinyrl_t *tinyrl, const char *fname)
  1028. {
  1029. return tinyrl_history_restore(tinyrl->history, fname);
  1030. }
  1031. /*----------------------------------------------------------*/
  1032. void tinyrl__stifle_history(tinyrl_t *tinyrl, unsigned int stifle)
  1033. {
  1034. tinyrl_history_stifle(tinyrl->history, stifle);
  1035. }
  1036. /*--------------------------------------------------------- */
  1037. #endif