tinyrl.c 32 KB

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