readline.c revision 1.79 1 /* $NetBSD: readline.c,v 1.79 2009/02/12 13:39:49 sketch Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jaromir Dolecek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33 #if !defined(lint) && !defined(SCCSID)
34 __RCSID("$NetBSD: readline.c,v 1.79 2009/02/12 13:39:49 sketch Exp $");
35 #endif /* not lint && not SCCSID */
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <stdio.h>
40 #include <dirent.h>
41 #include <string.h>
42 #include <pwd.h>
43 #include <ctype.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <setjmp.h>
50 #ifdef HAVE_VIS_H
51 #include <vis.h>
52 #else
53 #include "np/vis.h"
54 #endif
55 #include "readline/readline.h"
56 #include "el.h"
57 #include "fcns.h" /* for EL_NUM_FCNS */
58 #include "histedit.h"
59 #include "filecomplete.h"
60
61 void rl_prep_terminal(int);
62 void rl_deprep_terminal(void);
63
64 /* for rl_complete() */
65 #define TAB '\r'
66
67 /* see comment at the #ifdef for sense of this */
68 /* #define GDB_411_HACK */
69
70 /* readline compatibility stuff - look at readline sources/documentation */
71 /* to see what these variables mean */
72 const char *rl_library_version = "EditLine wrapper";
73 static char empty[] = { '\0' };
74 static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
75 static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
76 '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
77 char *rl_readline_name = empty;
78 FILE *rl_instream = NULL;
79 FILE *rl_outstream = NULL;
80 int rl_point = 0;
81 int rl_end = 0;
82 char *rl_line_buffer = NULL;
83 VCPFunction *rl_linefunc = NULL;
84 int rl_done = 0;
85 VFunction *rl_event_hook = NULL;
86 KEYMAP_ENTRY_ARRAY emacs_standard_keymap,
87 emacs_meta_keymap,
88 emacs_ctlx_keymap;
89
90 int history_base = 1; /* probably never subject to change */
91 int history_length = 0;
92 int max_input_history = 0;
93 char history_expansion_char = '!';
94 char history_subst_char = '^';
95 char *history_no_expand_chars = expand_chars;
96 Function *history_inhibit_expansion_function = NULL;
97 char *history_arg_extract(int start, int end, const char *str);
98
99 int rl_inhibit_completion = 0;
100 int rl_attempted_completion_over = 0;
101 char *rl_basic_word_break_characters = break_chars;
102 char *rl_completer_word_break_characters = NULL;
103 char *rl_completer_quote_characters = NULL;
104 Function *rl_completion_entry_function = NULL;
105 CPPFunction *rl_attempted_completion_function = NULL;
106 Function *rl_pre_input_hook = NULL;
107 Function *rl_startup1_hook = NULL;
108 int (*rl_getc_function)(FILE *) = NULL;
109 char *rl_terminal_name = NULL;
110 int rl_already_prompted = 0;
111 int rl_filename_completion_desired = 0;
112 int rl_ignore_completion_duplicates = 0;
113 int rl_catch_signals = 1;
114 int readline_echoing_p = 1;
115 int _rl_print_completions_horizontally = 0;
116 VFunction *rl_redisplay_function = NULL;
117 Function *rl_startup_hook = NULL;
118 VFunction *rl_completion_display_matches_hook = NULL;
119 VFunction *rl_prep_term_function = (VFunction *)rl_prep_terminal;
120 VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;
121
122 /*
123 * The current prompt string.
124 */
125 char *rl_prompt = NULL;
126 /*
127 * This is set to character indicating type of completion being done by
128 * rl_complete_internal(); this is available for application completion
129 * functions.
130 */
131 int rl_completion_type = 0;
132
133 /*
134 * If more than this number of items results from query for possible
135 * completions, we ask user if they are sure to really display the list.
136 */
137 int rl_completion_query_items = 100;
138
139 /*
140 * List of characters which are word break characters, but should be left
141 * in the parsed text when it is passed to the completion function.
142 * Shell uses this to help determine what kind of completing to do.
143 */
144 char *rl_special_prefixes = NULL;
145
146 /*
147 * This is the character appended to the completed words if at the end of
148 * the line. Default is ' ' (a space).
149 */
150 int rl_completion_append_character = ' ';
151
152 /* stuff below is used internally by libedit for readline emulation */
153
154 static History *h = NULL;
155 static EditLine *e = NULL;
156 static Function *map[256];
157 static jmp_buf topbuf;
158
159 /* internal functions */
160 static unsigned char _el_rl_complete(EditLine *, int);
161 static unsigned char _el_rl_tstp(EditLine *, int);
162 static char *_get_prompt(EditLine *);
163 static int _getc_function(EditLine *, char *);
164 static HIST_ENTRY *_move_history(int);
165 static int _history_expand_command(const char *, size_t, size_t,
166 char **);
167 static char *_rl_compat_sub(const char *, const char *,
168 const char *, int);
169 static int _rl_event_read_char(EditLine *, char *);
170 static void _rl_update_pos(void);
171
172
173 /* ARGSUSED */
174 static char *
175 _get_prompt(EditLine *el __attribute__((__unused__)))
176 {
177 rl_already_prompted = 1;
178 return (rl_prompt);
179 }
180
181
182 /*
183 * generic function for moving around history
184 */
185 static HIST_ENTRY *
186 _move_history(int op)
187 {
188 HistEvent ev;
189 static HIST_ENTRY rl_he;
190
191 if (history(h, &ev, op) != 0)
192 return (HIST_ENTRY *) NULL;
193
194 rl_he.line = ev.str;
195 rl_he.data = NULL;
196
197 return (&rl_he);
198 }
199
200
201 /*
202 * read one key from user defined input function
203 */
204 static int
205 /*ARGSUSED*/
206 _getc_function(EditLine *el, char *c)
207 {
208 int i;
209
210 i = (*rl_getc_function)(NULL);
211 if (i == -1)
212 return 0;
213 *c = i;
214 return 1;
215 }
216
217
218 /*
219 * READLINE compatibility stuff
220 */
221
222 /*
223 * Set the prompt
224 */
225 int
226 rl_set_prompt(const char *prompt)
227 {
228 if (!prompt)
229 prompt = "";
230 if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0)
231 return 0;
232 if (rl_prompt)
233 free(rl_prompt);
234 rl_prompt = strdup(prompt);
235 return rl_prompt == NULL ? -1 : 0;
236 }
237
238 /*
239 * initialize rl compat stuff
240 */
241 int
242 rl_initialize(void)
243 {
244 HistEvent ev;
245 const LineInfo *li;
246 int editmode = 1;
247 struct termios t;
248
249 if (e != NULL)
250 el_end(e);
251 if (h != NULL)
252 history_end(h);
253
254 if (!rl_instream)
255 rl_instream = stdin;
256 if (!rl_outstream)
257 rl_outstream = stdout;
258
259 /*
260 * See if we don't really want to run the editor
261 */
262 if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)
263 editmode = 0;
264
265 e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
266
267 if (!editmode)
268 el_set(e, EL_EDITMODE, 0);
269
270 h = history_init();
271 if (!e || !h)
272 return (-1);
273
274 history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */
275 history_length = 0;
276 max_input_history = INT_MAX;
277 el_set(e, EL_HIST, history, h);
278
279 /* setup getc function if valid */
280 if (rl_getc_function)
281 el_set(e, EL_GETCFN, _getc_function);
282
283 /* for proper prompt printing in readline() */
284 if (rl_set_prompt("") == -1) {
285 history_end(h);
286 el_end(e);
287 return -1;
288 }
289 el_set(e, EL_PROMPT, _get_prompt);
290 el_set(e, EL_SIGNAL, rl_catch_signals);
291
292 /* set default mode to "emacs"-style and read setting afterwards */
293 /* so this can be overriden */
294 el_set(e, EL_EDITOR, "emacs");
295 if (rl_terminal_name != NULL)
296 el_set(e, EL_TERMINAL, rl_terminal_name);
297 else
298 el_get(e, EL_TERMINAL, &rl_terminal_name);
299
300 /*
301 * Word completion - this has to go AFTER rebinding keys
302 * to emacs-style.
303 */
304 el_set(e, EL_ADDFN, "rl_complete",
305 "ReadLine compatible completion function",
306 _el_rl_complete);
307 el_set(e, EL_BIND, "^I", "rl_complete", NULL);
308
309 /*
310 * Send TSTP when ^Z is pressed.
311 */
312 el_set(e, EL_ADDFN, "rl_tstp",
313 "ReadLine compatible suspend function",
314 _el_rl_tstp);
315 el_set(e, EL_BIND, "^Z", "rl_tstp", NULL);
316
317 /* read settings from configuration file */
318 el_source(e, NULL);
319
320 /*
321 * Unfortunately, some applications really do use rl_point
322 * and rl_line_buffer directly.
323 */
324 li = el_line(e);
325 /* a cheesy way to get rid of const cast. */
326 rl_line_buffer = memchr(li->buffer, *li->buffer, 1);
327 _rl_update_pos();
328
329 if (rl_startup_hook)
330 (*rl_startup_hook)(NULL, 0);
331
332 return (0);
333 }
334
335
336 /*
337 * read one line from input stream and return it, chomping
338 * trailing newline (if there is any)
339 */
340 char *
341 readline(const char *p)
342 {
343 HistEvent ev;
344 const char * volatile prompt = p;
345 int count;
346 const char *ret;
347 char *buf;
348 static int used_event_hook;
349
350 if (e == NULL || h == NULL)
351 rl_initialize();
352
353 rl_done = 0;
354
355 (void)setjmp(topbuf);
356
357 /* update prompt accordingly to what has been passed */
358 if (rl_set_prompt(prompt) == -1)
359 return NULL;
360
361 if (rl_pre_input_hook)
362 (*rl_pre_input_hook)(NULL, 0);
363
364 if (rl_event_hook && !(e->el_flags&NO_TTY)) {
365 el_set(e, EL_GETCFN, _rl_event_read_char);
366 used_event_hook = 1;
367 }
368
369 if (!rl_event_hook && used_event_hook) {
370 el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN);
371 used_event_hook = 0;
372 }
373
374 rl_already_prompted = 0;
375
376 /* get one line from input stream */
377 ret = el_gets(e, &count);
378
379 if (ret && count > 0) {
380 int lastidx;
381
382 buf = strdup(ret);
383 if (buf == NULL)
384 return NULL;
385 lastidx = count - 1;
386 if (buf[lastidx] == '\n')
387 buf[lastidx] = '\0';
388 } else
389 buf = NULL;
390
391 history(h, &ev, H_GETSIZE);
392 history_length = ev.num;
393
394 return buf;
395 }
396
397 /*
398 * history functions
399 */
400
401 /*
402 * is normally called before application starts to use
403 * history expansion functions
404 */
405 void
406 using_history(void)
407 {
408 if (h == NULL || e == NULL)
409 rl_initialize();
410 }
411
412
413 /*
414 * substitute ``what'' with ``with'', returning resulting string; if
415 * globally == 1, substitutes all occurrences of what, otherwise only the
416 * first one
417 */
418 static char *
419 _rl_compat_sub(const char *str, const char *what, const char *with,
420 int globally)
421 {
422 const char *s;
423 char *r, *result;
424 size_t len, with_len, what_len;
425
426 len = strlen(str);
427 with_len = strlen(with);
428 what_len = strlen(what);
429
430 /* calculate length we need for result */
431 s = str;
432 while (*s) {
433 if (*s == *what && !strncmp(s, what, what_len)) {
434 len += with_len - what_len;
435 if (!globally)
436 break;
437 s += what_len;
438 } else
439 s++;
440 }
441 r = result = malloc(len + 1);
442 if (result == NULL)
443 return NULL;
444 s = str;
445 while (*s) {
446 if (*s == *what && !strncmp(s, what, what_len)) {
447 (void)strncpy(r, with, with_len);
448 r += with_len;
449 s += what_len;
450 if (!globally) {
451 (void)strcpy(r, s);
452 return(result);
453 }
454 } else
455 *r++ = *s++;
456 }
457 *r = '\0';
458 return(result);
459 }
460
461 static char *last_search_pat; /* last !?pat[?] search pattern */
462 static char *last_search_match; /* last !?pat[?] that matched */
463
464 const char *
465 get_history_event(const char *cmd, int *cindex, int qchar)
466 {
467 int idx, sign, sub, num, begin, ret;
468 size_t len;
469 char *pat;
470 const char *rptr;
471 HistEvent ev;
472
473 idx = *cindex;
474 if (cmd[idx++] != history_expansion_char)
475 return(NULL);
476
477 /* find out which event to take */
478 if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') {
479 if (history(h, &ev, H_FIRST) != 0)
480 return(NULL);
481 *cindex = cmd[idx]? (idx + 1):idx;
482 return(ev.str);
483 }
484 sign = 0;
485 if (cmd[idx] == '-') {
486 sign = 1;
487 idx++;
488 }
489
490 if ('0' <= cmd[idx] && cmd[idx] <= '9') {
491 HIST_ENTRY *rl_he;
492
493 num = 0;
494 while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') {
495 num = num * 10 + cmd[idx] - '0';
496 idx++;
497 }
498 if (sign)
499 num = history_length - num + 1;
500
501 if (!(rl_he = history_get(num)))
502 return(NULL);
503
504 *cindex = idx;
505 return(rl_he->line);
506 }
507 sub = 0;
508 if (cmd[idx] == '?') {
509 sub = 1;
510 idx++;
511 }
512 begin = idx;
513 while (cmd[idx]) {
514 if (cmd[idx] == '\n')
515 break;
516 if (sub && cmd[idx] == '?')
517 break;
518 if (!sub && (cmd[idx] == ':' || cmd[idx] == ' '
519 || cmd[idx] == '\t' || cmd[idx] == qchar))
520 break;
521 idx++;
522 }
523 len = idx - begin;
524 if (sub && cmd[idx] == '?')
525 idx++;
526 if (sub && len == 0 && last_search_pat && *last_search_pat)
527 pat = last_search_pat;
528 else if (len == 0)
529 return(NULL);
530 else {
531 if ((pat = malloc(len + 1)) == NULL)
532 return NULL;
533 (void)strncpy(pat, cmd + begin, len);
534 pat[len] = '\0';
535 }
536
537 if (history(h, &ev, H_CURR) != 0) {
538 if (pat != last_search_pat)
539 free(pat);
540 return (NULL);
541 }
542 num = ev.num;
543
544 if (sub) {
545 if (pat != last_search_pat) {
546 if (last_search_pat)
547 free(last_search_pat);
548 last_search_pat = pat;
549 }
550 ret = history_search(pat, -1);
551 } else
552 ret = history_search_prefix(pat, -1);
553
554 if (ret == -1) {
555 /* restore to end of list on failed search */
556 history(h, &ev, H_FIRST);
557 (void)fprintf(rl_outstream, "%s: Event not found\n", pat);
558 if (pat != last_search_pat)
559 free(pat);
560 return(NULL);
561 }
562
563 if (sub && len) {
564 if (last_search_match && last_search_match != pat)
565 free(last_search_match);
566 last_search_match = pat;
567 }
568
569 if (pat != last_search_pat)
570 free(pat);
571
572 if (history(h, &ev, H_CURR) != 0)
573 return(NULL);
574 *cindex = idx;
575 rptr = ev.str;
576
577 /* roll back to original position */
578 (void)history(h, &ev, H_SET, num);
579
580 return rptr;
581 }
582
583 /*
584 * the real function doing history expansion - takes as argument command
585 * to do and data upon which the command should be executed
586 * does expansion the way I've understood readline documentation
587 *
588 * returns 0 if data was not modified, 1 if it was and 2 if the string
589 * should be only printed and not executed; in case of error,
590 * returns -1 and *result points to NULL
591 * it's callers responsibility to free() string returned in *result
592 */
593 static int
594 _history_expand_command(const char *command, size_t offs, size_t cmdlen,
595 char **result)
596 {
597 char *tmp, *search = NULL, *aptr;
598 const char *ptr, *cmd;
599 static char *from = NULL, *to = NULL;
600 int start, end, idx, has_mods = 0;
601 int p_on = 0, g_on = 0;
602
603 *result = NULL;
604 aptr = NULL;
605 ptr = NULL;
606
607 /* First get event specifier */
608 idx = 0;
609
610 if (strchr(":^*$", command[offs + 1])) {
611 char str[4];
612 /*
613 * "!:" is shorthand for "!!:".
614 * "!^", "!*" and "!$" are shorthand for
615 * "!!:^", "!!:*" and "!!:$" respectively.
616 */
617 str[0] = str[1] = '!';
618 str[2] = '0';
619 ptr = get_history_event(str, &idx, 0);
620 idx = (command[offs + 1] == ':')? 1:0;
621 has_mods = 1;
622 } else {
623 if (command[offs + 1] == '#') {
624 /* use command so far */
625 if ((aptr = malloc(offs + 1)) == NULL)
626 return -1;
627 (void)strncpy(aptr, command, offs);
628 aptr[offs] = '\0';
629 idx = 1;
630 } else {
631 int qchar;
632
633 qchar = (offs > 0 && command[offs - 1] == '"')? '"':0;
634 ptr = get_history_event(command + offs, &idx, qchar);
635 }
636 has_mods = command[offs + idx] == ':';
637 }
638
639 if (ptr == NULL && aptr == NULL)
640 return(-1);
641
642 if (!has_mods) {
643 *result = strdup(aptr? aptr : ptr);
644 if (aptr)
645 free(aptr);
646 return(1);
647 }
648
649 cmd = command + offs + idx + 1;
650
651 /* Now parse any word designators */
652
653 if (*cmd == '%') /* last word matched by ?pat? */
654 tmp = strdup(last_search_match? last_search_match:"");
655 else if (strchr("^*$-0123456789", *cmd)) {
656 start = end = -1;
657 if (*cmd == '^')
658 start = end = 1, cmd++;
659 else if (*cmd == '$')
660 start = -1, cmd++;
661 else if (*cmd == '*')
662 start = 1, cmd++;
663 else if (*cmd == '-' || isdigit((unsigned char) *cmd)) {
664 start = 0;
665 while (*cmd && '0' <= *cmd && *cmd <= '9')
666 start = start * 10 + *cmd++ - '0';
667
668 if (*cmd == '-') {
669 if (isdigit((unsigned char) cmd[1])) {
670 cmd++;
671 end = 0;
672 while (*cmd && '0' <= *cmd && *cmd <= '9')
673 end = end * 10 + *cmd++ - '0';
674 } else if (cmd[1] == '$') {
675 cmd += 2;
676 end = -1;
677 } else {
678 cmd++;
679 end = -2;
680 }
681 } else if (*cmd == '*')
682 end = -1, cmd++;
683 else
684 end = start;
685 }
686 tmp = history_arg_extract(start, end, aptr? aptr:ptr);
687 if (tmp == NULL) {
688 (void)fprintf(rl_outstream, "%s: Bad word specifier",
689 command + offs + idx);
690 if (aptr)
691 free(aptr);
692 return(-1);
693 }
694 } else
695 tmp = strdup(aptr? aptr:ptr);
696
697 if (aptr)
698 free(aptr);
699
700 if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) {
701 *result = tmp;
702 return(1);
703 }
704
705 for (; *cmd; cmd++) {
706 if (*cmd == ':')
707 continue;
708 else if (*cmd == 'h') { /* remove trailing path */
709 if ((aptr = strrchr(tmp, '/')) != NULL)
710 *aptr = '\0';
711 } else if (*cmd == 't') { /* remove leading path */
712 if ((aptr = strrchr(tmp, '/')) != NULL) {
713 aptr = strdup(aptr + 1);
714 free(tmp);
715 tmp = aptr;
716 }
717 } else if (*cmd == 'r') { /* remove trailing suffix */
718 if ((aptr = strrchr(tmp, '.')) != NULL)
719 *aptr = '\0';
720 } else if (*cmd == 'e') { /* remove all but suffix */
721 if ((aptr = strrchr(tmp, '.')) != NULL) {
722 aptr = strdup(aptr);
723 free(tmp);
724 tmp = aptr;
725 }
726 } else if (*cmd == 'p') /* print only */
727 p_on = 1;
728 else if (*cmd == 'g')
729 g_on = 2;
730 else if (*cmd == 's' || *cmd == '&') {
731 char *what, *with, delim;
732 size_t len, from_len;
733 size_t size;
734
735 if (*cmd == '&' && (from == NULL || to == NULL))
736 continue;
737 else if (*cmd == 's') {
738 delim = *(++cmd), cmd++;
739 size = 16;
740 what = realloc(from, size);
741 if (what == NULL) {
742 free(from);
743 free(tmp);
744 return 0;
745 }
746 len = 0;
747 for (; *cmd && *cmd != delim; cmd++) {
748 if (*cmd == '\\' && cmd[1] == delim)
749 cmd++;
750 if (len >= size) {
751 char *nwhat;
752 nwhat = realloc(what,
753 (size <<= 1));
754 if (nwhat == NULL) {
755 free(what);
756 free(tmp);
757 return 0;
758 }
759 what = nwhat;
760 }
761 what[len++] = *cmd;
762 }
763 what[len] = '\0';
764 from = what;
765 if (*what == '\0') {
766 free(what);
767 if (search) {
768 from = strdup(search);
769 if (from == NULL) {
770 free(tmp);
771 return 0;
772 }
773 } else {
774 from = NULL;
775 free(tmp);
776 return (-1);
777 }
778 }
779 cmd++; /* shift after delim */
780 if (!*cmd)
781 continue;
782
783 size = 16;
784 with = realloc(to, size);
785 if (with == NULL) {
786 free(to);
787 free(tmp);
788 return -1;
789 }
790 len = 0;
791 from_len = strlen(from);
792 for (; *cmd && *cmd != delim; cmd++) {
793 if (len + from_len + 1 >= size) {
794 char *nwith;
795 size += from_len + 1;
796 nwith = realloc(with, size);
797 if (nwith == NULL) {
798 free(with);
799 free(tmp);
800 return -1;
801 }
802 with = nwith;
803 }
804 if (*cmd == '&') {
805 /* safe */
806 (void)strcpy(&with[len], from);
807 len += from_len;
808 continue;
809 }
810 if (*cmd == '\\'
811 && (*(cmd + 1) == delim
812 || *(cmd + 1) == '&'))
813 cmd++;
814 with[len++] = *cmd;
815 }
816 with[len] = '\0';
817 to = with;
818 }
819
820 aptr = _rl_compat_sub(tmp, from, to, g_on);
821 if (aptr) {
822 free(tmp);
823 tmp = aptr;
824 }
825 g_on = 0;
826 }
827 }
828 *result = tmp;
829 return (p_on? 2:1);
830 }
831
832
833 /*
834 * csh-style history expansion
835 */
836 int
837 history_expand(char *str, char **output)
838 {
839 int ret = 0;
840 size_t idx, i, size;
841 char *tmp, *result;
842
843 if (h == NULL || e == NULL)
844 rl_initialize();
845
846 if (history_expansion_char == 0) {
847 *output = strdup(str);
848 return(0);
849 }
850
851 *output = NULL;
852 if (str[0] == history_subst_char) {
853 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
854 *output = malloc(strlen(str) + 4 + 1);
855 if (*output == NULL)
856 return 0;
857 (*output)[0] = (*output)[1] = history_expansion_char;
858 (*output)[2] = ':';
859 (*output)[3] = 's';
860 (void)strcpy((*output) + 4, str);
861 str = *output;
862 } else {
863 *output = strdup(str);
864 if (*output == NULL)
865 return 0;
866 }
867
868 #define ADD_STRING(what, len, fr) \
869 { \
870 if (idx + len + 1 > size) { \
871 char *nresult = realloc(result, (size += len + 1));\
872 if (nresult == NULL) { \
873 free(*output); \
874 if (/*CONSTCOND*/fr) \
875 free(tmp); \
876 return 0; \
877 } \
878 result = nresult; \
879 } \
880 (void)strncpy(&result[idx], what, len); \
881 idx += len; \
882 result[idx] = '\0'; \
883 }
884
885 result = NULL;
886 size = idx = 0;
887 tmp = NULL;
888 for (i = 0; str[i];) {
889 int qchar, loop_again;
890 size_t len, start, j;
891
892 qchar = 0;
893 loop_again = 1;
894 start = j = i;
895 loop:
896 for (; str[j]; j++) {
897 if (str[j] == '\\' &&
898 str[j + 1] == history_expansion_char) {
899 (void)strcpy(&str[j], &str[j + 1]);
900 continue;
901 }
902 if (!loop_again) {
903 if (isspace((unsigned char) str[j])
904 || str[j] == qchar)
905 break;
906 }
907 if (str[j] == history_expansion_char
908 && !strchr(history_no_expand_chars, str[j + 1])
909 && (!history_inhibit_expansion_function ||
910 (*history_inhibit_expansion_function)(str,
911 (int)j) == 0))
912 break;
913 }
914
915 if (str[j] && loop_again) {
916 i = j;
917 qchar = (j > 0 && str[j - 1] == '"' )? '"':0;
918 j++;
919 if (str[j] == history_expansion_char)
920 j++;
921 loop_again = 0;
922 goto loop;
923 }
924 len = i - start;
925 ADD_STRING(&str[start], len, 0);
926
927 if (str[i] == '\0' || str[i] != history_expansion_char) {
928 len = j - i;
929 ADD_STRING(&str[i], len, 0);
930 if (start == 0)
931 ret = 0;
932 else
933 ret = 1;
934 break;
935 }
936 ret = _history_expand_command (str, i, (j - i), &tmp);
937 if (ret > 0 && tmp) {
938 len = strlen(tmp);
939 ADD_STRING(tmp, len, 1);
940 }
941 if (tmp) {
942 free(tmp);
943 tmp = NULL;
944 }
945 i = j;
946 }
947
948 /* ret is 2 for "print only" option */
949 if (ret == 2) {
950 add_history(result);
951 #ifdef GDB_411_HACK
952 /* gdb 4.11 has been shipped with readline, where */
953 /* history_expand() returned -1 when the line */
954 /* should not be executed; in readline 2.1+ */
955 /* it should return 2 in such a case */
956 ret = -1;
957 #endif
958 }
959 free(*output);
960 *output = result;
961
962 return (ret);
963 }
964
965 /*
966 * Return a string consisting of arguments of "str" from "start" to "end".
967 */
968 char *
969 history_arg_extract(int start, int end, const char *str)
970 {
971 size_t i, len, max;
972 char **arr, *result;
973
974 arr = history_tokenize(str);
975 if (!arr)
976 return(NULL);
977 if (arr && *arr == NULL) {
978 free(arr);
979 return(NULL);
980 }
981
982 for (max = 0; arr[max]; max++)
983 continue;
984 max--;
985
986 if (start == '$')
987 start = max;
988 if (end == '$')
989 end = max;
990 if (end < 0)
991 end = max + end + 1;
992 if (start < 0)
993 start = end;
994
995 if (start < 0 || end < 0 || (size_t)start > max || (size_t)end > max || start > end)
996 return(NULL);
997
998 for (i = start, len = 0; i <= (size_t)end; i++)
999 len += strlen(arr[i]) + 1;
1000 len++;
1001 result = malloc(len);
1002 if (result == NULL)
1003 return NULL;
1004
1005 for (i = start, len = 0; i <= (size_t)end; i++) {
1006 (void)strcpy(result + len, arr[i]);
1007 len += strlen(arr[i]);
1008 if (i < (size_t)end)
1009 result[len++] = ' ';
1010 }
1011 result[len] = '\0';
1012
1013 for (i = 0; arr[i]; i++)
1014 free(arr[i]);
1015 free(arr);
1016
1017 return(result);
1018 }
1019
1020 /*
1021 * Parse the string into individual tokens,
1022 * similar to how shell would do it.
1023 */
1024 char **
1025 history_tokenize(const char *str)
1026 {
1027 int size = 1, idx = 0, i, start;
1028 size_t len;
1029 char **result = NULL, *temp, delim = '\0';
1030
1031 for (i = 0; str[i];) {
1032 while (isspace((unsigned char) str[i]))
1033 i++;
1034 start = i;
1035 for (; str[i];) {
1036 if (str[i] == '\\') {
1037 if (str[i+1] != '\0')
1038 i++;
1039 } else if (str[i] == delim)
1040 delim = '\0';
1041 else if (!delim &&
1042 (isspace((unsigned char) str[i]) ||
1043 strchr("()<>;&|$", str[i])))
1044 break;
1045 else if (!delim && strchr("'`\"", str[i]))
1046 delim = str[i];
1047 if (str[i])
1048 i++;
1049 }
1050
1051 if (idx + 2 >= size) {
1052 char **nresult;
1053 size <<= 1;
1054 nresult = realloc(result, size * sizeof(char *));
1055 if (nresult == NULL) {
1056 free(result);
1057 return NULL;
1058 }
1059 result = nresult;
1060 }
1061 len = i - start;
1062 temp = malloc(len + 1);
1063 if (temp == NULL) {
1064 for (i = 0; i < idx; i++)
1065 free(result[i]);
1066 free(result);
1067 return NULL;
1068 }
1069 (void)strncpy(temp, &str[start], len);
1070 temp[len] = '\0';
1071 result[idx++] = temp;
1072 result[idx] = NULL;
1073 if (str[i])
1074 i++;
1075 }
1076 return (result);
1077 }
1078
1079
1080 /*
1081 * limit size of history record to ``max'' events
1082 */
1083 void
1084 stifle_history(int max)
1085 {
1086 HistEvent ev;
1087
1088 if (h == NULL || e == NULL)
1089 rl_initialize();
1090
1091 if (history(h, &ev, H_SETSIZE, max) == 0)
1092 max_input_history = max;
1093 }
1094
1095
1096 /*
1097 * "unlimit" size of history - set the limit to maximum allowed int value
1098 */
1099 int
1100 unstifle_history(void)
1101 {
1102 HistEvent ev;
1103 int omax;
1104
1105 history(h, &ev, H_SETSIZE, INT_MAX);
1106 omax = max_input_history;
1107 max_input_history = INT_MAX;
1108 return (omax); /* some value _must_ be returned */
1109 }
1110
1111
1112 int
1113 history_is_stifled(void)
1114 {
1115
1116 /* cannot return true answer */
1117 return (max_input_history != INT_MAX);
1118 }
1119
1120
1121 /*
1122 * read history from a file given
1123 */
1124 int
1125 read_history(const char *filename)
1126 {
1127 HistEvent ev;
1128
1129 if (h == NULL || e == NULL)
1130 rl_initialize();
1131 return (history(h, &ev, H_LOAD, filename) == -1);
1132 }
1133
1134
1135 /*
1136 * write history to a file given
1137 */
1138 int
1139 write_history(const char *filename)
1140 {
1141 HistEvent ev;
1142
1143 if (h == NULL || e == NULL)
1144 rl_initialize();
1145 return (history(h, &ev, H_SAVE, filename) == -1);
1146 }
1147
1148
1149 /*
1150 * returns history ``num''th event
1151 *
1152 * returned pointer points to static variable
1153 */
1154 HIST_ENTRY *
1155 history_get(int num)
1156 {
1157 static HIST_ENTRY she;
1158 HistEvent ev;
1159 int curr_num;
1160
1161 if (h == NULL || e == NULL)
1162 rl_initialize();
1163
1164 /* save current position */
1165 if (history(h, &ev, H_CURR) != 0)
1166 return (NULL);
1167 curr_num = ev.num;
1168
1169 /* start from most recent */
1170 if (history(h, &ev, H_FIRST) != 0)
1171 return (NULL); /* error */
1172
1173 /* look backwards for event matching specified offset */
1174 if (history(h, &ev, H_NEXT_EVENT, num + 1))
1175 return (NULL);
1176
1177 she.line = ev.str;
1178 she.data = NULL;
1179
1180 /* restore pointer to where it was */
1181 (void)history(h, &ev, H_SET, curr_num);
1182
1183 return (&she);
1184 }
1185
1186
1187 /*
1188 * add the line to history table
1189 */
1190 int
1191 add_history(const char *line)
1192 {
1193 HistEvent ev;
1194
1195 if (h == NULL || e == NULL)
1196 rl_initialize();
1197
1198 (void)history(h, &ev, H_ENTER, line);
1199 if (history(h, &ev, H_GETSIZE) == 0)
1200 history_length = ev.num;
1201
1202 return (!(history_length > 0)); /* return 0 if all is okay */
1203 }
1204
1205
1206 /*
1207 * remove the specified entry from the history list and return it.
1208 */
1209 HIST_ENTRY *
1210 remove_history(int num)
1211 {
1212 HIST_ENTRY *she;
1213 HistEvent ev;
1214
1215 if (h == NULL || e == NULL)
1216 rl_initialize();
1217
1218 if (history(h, &ev, H_DEL, num) != 0)
1219 return NULL;
1220
1221 if ((she = malloc(sizeof(*she))) == NULL)
1222 return NULL;
1223
1224 she->line = ev.str;
1225 she->data = NULL;
1226
1227 return she;
1228 }
1229
1230
1231 /*
1232 * clear the history list - delete all entries
1233 */
1234 void
1235 clear_history(void)
1236 {
1237 HistEvent ev;
1238
1239 history(h, &ev, H_CLEAR);
1240 }
1241
1242
1243 /*
1244 * returns offset of the current history event
1245 */
1246 int
1247 where_history(void)
1248 {
1249 HistEvent ev;
1250 int curr_num, off;
1251
1252 if (history(h, &ev, H_CURR) != 0)
1253 return (0);
1254 curr_num = ev.num;
1255
1256 history(h, &ev, H_FIRST);
1257 off = 1;
1258 while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
1259 off++;
1260
1261 return (off);
1262 }
1263
1264
1265 /*
1266 * returns current history event or NULL if there is no such event
1267 */
1268 HIST_ENTRY *
1269 current_history(void)
1270 {
1271
1272 return (_move_history(H_CURR));
1273 }
1274
1275
1276 /*
1277 * returns total number of bytes history events' data are using
1278 */
1279 int
1280 history_total_bytes(void)
1281 {
1282 HistEvent ev;
1283 int curr_num, size;
1284
1285 if (history(h, &ev, H_CURR) != 0)
1286 return (-1);
1287 curr_num = ev.num;
1288
1289 history(h, &ev, H_FIRST);
1290 size = 0;
1291 do
1292 size += strlen(ev.str);
1293 while (history(h, &ev, H_NEXT) == 0);
1294
1295 /* get to the same position as before */
1296 history(h, &ev, H_PREV_EVENT, curr_num);
1297
1298 return (size);
1299 }
1300
1301
1302 /*
1303 * sets the position in the history list to ``pos''
1304 */
1305 int
1306 history_set_pos(int pos)
1307 {
1308 HistEvent ev;
1309 int curr_num;
1310
1311 if (pos > history_length || pos < 0)
1312 return (-1);
1313
1314 history(h, &ev, H_CURR);
1315 curr_num = ev.num;
1316
1317 if (history(h, &ev, H_SET, pos)) {
1318 history(h, &ev, H_SET, curr_num);
1319 return(-1);
1320 }
1321 return (0);
1322 }
1323
1324
1325 /*
1326 * returns previous event in history and shifts pointer accordingly
1327 */
1328 HIST_ENTRY *
1329 previous_history(void)
1330 {
1331
1332 return (_move_history(H_PREV));
1333 }
1334
1335
1336 /*
1337 * returns next event in history and shifts pointer accordingly
1338 */
1339 HIST_ENTRY *
1340 next_history(void)
1341 {
1342
1343 return (_move_history(H_NEXT));
1344 }
1345
1346
1347 /*
1348 * searches for first history event containing the str
1349 */
1350 int
1351 history_search(const char *str, int direction)
1352 {
1353 HistEvent ev;
1354 const char *strp;
1355 int curr_num;
1356
1357 if (history(h, &ev, H_CURR) != 0)
1358 return (-1);
1359 curr_num = ev.num;
1360
1361 for (;;) {
1362 if ((strp = strstr(ev.str, str)) != NULL)
1363 return (int) (strp - ev.str);
1364 if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
1365 break;
1366 }
1367 history(h, &ev, H_SET, curr_num);
1368 return (-1);
1369 }
1370
1371
1372 /*
1373 * searches for first history event beginning with str
1374 */
1375 int
1376 history_search_prefix(const char *str, int direction)
1377 {
1378 HistEvent ev;
1379
1380 return (history(h, &ev, direction < 0? H_PREV_STR:H_NEXT_STR, str));
1381 }
1382
1383
1384 /*
1385 * search for event in history containing str, starting at offset
1386 * abs(pos); continue backward, if pos<0, forward otherwise
1387 */
1388 /* ARGSUSED */
1389 int
1390 history_search_pos(const char *str,
1391 int direction __attribute__((__unused__)), int pos)
1392 {
1393 HistEvent ev;
1394 int curr_num, off;
1395
1396 off = (pos > 0) ? pos : -pos;
1397 pos = (pos > 0) ? 1 : -1;
1398
1399 if (history(h, &ev, H_CURR) != 0)
1400 return (-1);
1401 curr_num = ev.num;
1402
1403 if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
1404 return (-1);
1405
1406
1407 for (;;) {
1408 if (strstr(ev.str, str))
1409 return (off);
1410 if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
1411 break;
1412 }
1413
1414 /* set "current" pointer back to previous state */
1415 history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
1416
1417 return (-1);
1418 }
1419
1420
1421 /********************************/
1422 /* completion functions */
1423
1424 char *
1425 tilde_expand(char *name)
1426 {
1427 return fn_tilde_expand(name);
1428 }
1429
1430 char *
1431 filename_completion_function(const char *name, int state)
1432 {
1433 return fn_filename_completion_function(name, state);
1434 }
1435
1436 /*
1437 * a completion generator for usernames; returns _first_ username
1438 * which starts with supplied text
1439 * text contains a partial username preceded by random character
1440 * (usually '~'); state is ignored
1441 * it's callers responsibility to free returned value
1442 */
1443 char *
1444 username_completion_function(const char *text, int state)
1445 {
1446 struct passwd *pwd, pwres;
1447 char pwbuf[1024];
1448
1449 if (text[0] == '\0')
1450 return (NULL);
1451
1452 if (*text == '~')
1453 text++;
1454
1455 if (state == 0)
1456 setpwent();
1457
1458 while (getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pwd) == 0
1459 && pwd != NULL && text[0] == pwd->pw_name[0]
1460 && strcmp(text, pwd->pw_name) == 0);
1461
1462 if (pwd == NULL) {
1463 endpwent();
1464 return (NULL);
1465 }
1466 return (strdup(pwd->pw_name));
1467 }
1468
1469
1470 /*
1471 * el-compatible wrapper to send TSTP on ^Z
1472 */
1473 /* ARGSUSED */
1474 static unsigned char
1475 _el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__)))
1476 {
1477 (void)kill(0, SIGTSTP);
1478 return CC_NORM;
1479 }
1480
1481 /*
1482 * Display list of strings in columnar format on readline's output stream.
1483 * 'matches' is list of strings, 'len' is number of strings in 'matches',
1484 * 'max' is maximum length of string in 'matches'.
1485 */
1486 void
1487 rl_display_match_list(char **matches, int len, int max)
1488 {
1489
1490 fn_display_match_list(e, matches, len, max);
1491 }
1492
1493 static const char *
1494 /*ARGSUSED*/
1495 _rl_completion_append_character_function(const char *dummy
1496 __attribute__((__unused__)))
1497 {
1498 static char buf[2];
1499 buf[0] = rl_completion_append_character;
1500 buf[1] = '\0';
1501 return buf;
1502 }
1503
1504
1505 /*
1506 * complete word at current point
1507 */
1508 /* ARGSUSED */
1509 int
1510 rl_complete(int ignore __attribute__((__unused__)), int invoking_key)
1511 {
1512 if (h == NULL || e == NULL)
1513 rl_initialize();
1514
1515 if (rl_inhibit_completion) {
1516 char arr[2];
1517 arr[0] = (char)invoking_key;
1518 arr[1] = '\0';
1519 el_insertstr(e, arr);
1520 return (CC_REFRESH);
1521 }
1522
1523 /* Just look at how many global variables modify this operation! */
1524 return fn_complete(e,
1525 (CPFunction *)rl_completion_entry_function,
1526 rl_attempted_completion_function,
1527 rl_basic_word_break_characters, rl_special_prefixes,
1528 _rl_completion_append_character_function, rl_completion_query_items,
1529 &rl_completion_type, &rl_attempted_completion_over,
1530 &rl_point, &rl_end);
1531 }
1532
1533
1534 /* ARGSUSED */
1535 static unsigned char
1536 _el_rl_complete(EditLine *el __attribute__((__unused__)), int ch)
1537 {
1538 return (unsigned char)rl_complete(0, ch);
1539 }
1540
1541 /*
1542 * misc other functions
1543 */
1544
1545 /*
1546 * bind key c to readline-type function func
1547 */
1548 int
1549 rl_bind_key(int c, int func(int, int))
1550 {
1551 int retval = -1;
1552
1553 if (h == NULL || e == NULL)
1554 rl_initialize();
1555
1556 if (func == rl_insert) {
1557 /* XXX notice there is no range checking of ``c'' */
1558 e->el_map.key[c] = ED_INSERT;
1559 retval = 0;
1560 }
1561 return (retval);
1562 }
1563
1564
1565 /*
1566 * read one key from input - handles chars pushed back
1567 * to input stream also
1568 */
1569 int
1570 rl_read_key(void)
1571 {
1572 char fooarr[2 * sizeof(int)];
1573
1574 if (e == NULL || h == NULL)
1575 rl_initialize();
1576
1577 return (el_getc(e, fooarr));
1578 }
1579
1580
1581 /*
1582 * reset the terminal
1583 */
1584 /* ARGSUSED */
1585 void
1586 rl_reset_terminal(const char *p __attribute__((__unused__)))
1587 {
1588
1589 if (h == NULL || e == NULL)
1590 rl_initialize();
1591 el_reset(e);
1592 }
1593
1594
1595 /*
1596 * insert character ``c'' back into input stream, ``count'' times
1597 */
1598 int
1599 rl_insert(int count, int c)
1600 {
1601 char arr[2];
1602
1603 if (h == NULL || e == NULL)
1604 rl_initialize();
1605
1606 /* XXX - int -> char conversion can lose on multichars */
1607 arr[0] = c;
1608 arr[1] = '\0';
1609
1610 for (; count > 0; count--)
1611 el_push(e, arr);
1612
1613 return (0);
1614 }
1615
1616 /*ARGSUSED*/
1617 int
1618 rl_newline(int count, int c)
1619 {
1620 /*
1621 * Readline-4.0 appears to ignore the args.
1622 */
1623 return rl_insert(1, '\n');
1624 }
1625
1626 /*ARGSUSED*/
1627 static unsigned char
1628 rl_bind_wrapper(EditLine *el, unsigned char c)
1629 {
1630 if (map[c] == NULL)
1631 return CC_ERROR;
1632
1633 _rl_update_pos();
1634
1635 (*map[c])(NULL, c);
1636
1637 /* If rl_done was set by the above call, deal with it here */
1638 if (rl_done)
1639 return CC_EOF;
1640
1641 return CC_NORM;
1642 }
1643
1644 int
1645 rl_add_defun(const char *name, Function *fun, int c)
1646 {
1647 char dest[8];
1648 if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0)
1649 return -1;
1650 map[(unsigned char)c] = fun;
1651 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper);
1652 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0);
1653 el_set(e, EL_BIND, dest, name);
1654 return 0;
1655 }
1656
1657 void
1658 rl_callback_read_char()
1659 {
1660 int count = 0, done = 0;
1661 const char *buf = el_gets(e, &count);
1662 char *wbuf;
1663
1664 if (buf == NULL || count-- <= 0)
1665 return;
1666 if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF])
1667 done = 1;
1668 if (buf[count] == '\n' || buf[count] == '\r')
1669 done = 2;
1670
1671 if (done && rl_linefunc != NULL) {
1672 el_set(e, EL_UNBUFFERED, 0);
1673 if (done == 2) {
1674 if ((wbuf = strdup(buf)) != NULL)
1675 wbuf[count] = '\0';
1676 } else
1677 wbuf = NULL;
1678 (*(void (*)(const char *))rl_linefunc)(wbuf);
1679 el_set(e, EL_UNBUFFERED, 1);
1680 }
1681 }
1682
1683 void
1684 rl_callback_handler_install(const char *prompt, VCPFunction *linefunc)
1685 {
1686 if (e == NULL) {
1687 rl_initialize();
1688 }
1689 (void)rl_set_prompt(prompt);
1690 rl_linefunc = linefunc;
1691 el_set(e, EL_UNBUFFERED, 1);
1692 }
1693
1694 void
1695 rl_callback_handler_remove(void)
1696 {
1697 el_set(e, EL_UNBUFFERED, 0);
1698 rl_linefunc = NULL;
1699 }
1700
1701 void
1702 rl_redisplay(void)
1703 {
1704 char a[2];
1705 a[0] = e->el_tty.t_c[TS_IO][C_REPRINT];
1706 a[1] = '\0';
1707 el_push(e, a);
1708 }
1709
1710 int
1711 rl_get_previous_history(int count, int key)
1712 {
1713 char a[2];
1714 a[0] = key;
1715 a[1] = '\0';
1716 while (count--)
1717 el_push(e, a);
1718 return 0;
1719 }
1720
1721 void
1722 /*ARGSUSED*/
1723 rl_prep_terminal(int meta_flag)
1724 {
1725 el_set(e, EL_PREP_TERM, 1);
1726 }
1727
1728 void
1729 rl_deprep_terminal(void)
1730 {
1731 el_set(e, EL_PREP_TERM, 0);
1732 }
1733
1734 int
1735 rl_read_init_file(const char *s)
1736 {
1737 return(el_source(e, s));
1738 }
1739
1740 int
1741 rl_parse_and_bind(const char *line)
1742 {
1743 const char **argv;
1744 int argc;
1745 Tokenizer *tok;
1746
1747 tok = tok_init(NULL);
1748 tok_str(tok, line, &argc, &argv);
1749 argc = el_parse(e, argc, argv);
1750 tok_end(tok);
1751 return (argc ? 1 : 0);
1752 }
1753
1754 int
1755 rl_variable_bind(const char *var, const char *value)
1756 {
1757 /*
1758 * The proper return value is undocument, but this is what the
1759 * readline source seems to do.
1760 */
1761 return ((el_set(e, EL_BIND, "", var, value) == -1) ? 1 : 0);
1762 }
1763
1764 void
1765 rl_stuff_char(int c)
1766 {
1767 char buf[2];
1768
1769 buf[0] = c;
1770 buf[1] = '\0';
1771 el_insertstr(e, buf);
1772 }
1773
1774 static int
1775 _rl_event_read_char(EditLine *el, char *cp)
1776 {
1777 int n, num_read = 0;
1778
1779 *cp = '\0';
1780 while (rl_event_hook) {
1781
1782 (*rl_event_hook)();
1783
1784 #if defined(FIONREAD)
1785 if (ioctl(el->el_infd, FIONREAD, &n) < 0)
1786 return(-1);
1787 if (n)
1788 num_read = read(el->el_infd, cp, 1);
1789 else
1790 num_read = 0;
1791 #elif defined(F_SETFL) && defined(O_NDELAY)
1792 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0)
1793 return(-1);
1794 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0)
1795 return(-1);
1796 num_read = read(el->el_infd, cp, 1);
1797 if (fcntl(el->el_infd, F_SETFL, n))
1798 return(-1);
1799 #else
1800 /* not non-blocking, but what you gonna do? */
1801 num_read = read(el->el_infd, cp, 1);
1802 return(-1);
1803 #endif
1804
1805 if (num_read < 0 && errno == EAGAIN)
1806 continue;
1807 if (num_read == 0)
1808 continue;
1809 break;
1810 }
1811 if (!rl_event_hook)
1812 el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN);
1813 return(num_read);
1814 }
1815
1816 static void
1817 _rl_update_pos(void)
1818 {
1819 const LineInfo *li = el_line(e);
1820
1821 rl_point = li->cursor - li->buffer;
1822 rl_end = li->lastchar - li->buffer;
1823 }
1824
1825 void
1826 rl_get_screen_size(int *rows, int *cols)
1827 {
1828 if (rows)
1829 el_get(e, EL_GETTC, "li", rows);
1830 if (cols)
1831 el_get(e, EL_GETTC, "co", cols);
1832 }
1833
1834 void
1835 rl_set_screen_size(int rows, int cols)
1836 {
1837 char buf[64];
1838 (void)snprintf(buf, sizeof(buf), "%d", rows);
1839 el_set(e, EL_SETTC, "li", buf);
1840 (void)snprintf(buf, sizeof(buf), "%d", cols);
1841 el_set(e, EL_SETTC, "co", buf);
1842 }
1843
1844 char **
1845 rl_completion_matches(const char *str, rl_compentry_func_t *fun)
1846 {
1847 size_t len, max, i, j, min;
1848 char **list, *match, *a, *b;
1849
1850 len = 1;
1851 max = 10;
1852 if ((list = malloc(max * sizeof(*list))) == NULL)
1853 return NULL;
1854
1855 while ((match = (*fun)(str, (int)(len - 1))) != NULL) {
1856 if (len == max) {
1857 char **nl;
1858 max += 10;
1859 if ((nl = realloc(list, max * sizeof(*nl))) == NULL)
1860 goto out;
1861 list = nl;
1862 }
1863 list[len++] = match;
1864 }
1865 if (len == 1)
1866 goto out;
1867 list[len] = NULL;
1868 if (len == 2) {
1869 if ((list[0] = strdup(list[1])) == NULL)
1870 goto out;
1871 return list;
1872 }
1873 qsort(&list[1], len - 1, sizeof(*list),
1874 (int (*)(const void *, const void *)) strcmp);
1875 min = SIZE_T_MAX;
1876 for (i = 1, a = list[i]; i < len - 1; i++, a = b) {
1877 b = list[i + 1];
1878 for (j = 0; a[j] && a[j] == b[j]; j++)
1879 continue;
1880 if (min > j)
1881 min = j;
1882 }
1883 if (min == 0 && *str) {
1884 if ((list[0] = strdup(str)) == NULL)
1885 goto out;
1886 } else {
1887 if ((list[0] = malloc(min + 1)) == NULL)
1888 goto out;
1889 (void)memcpy(list[0], list[1], min);
1890 list[0][min] = '\0';
1891 }
1892 return list;
1893
1894 out:
1895 free(list);
1896 return NULL;
1897 }
1898
1899 char *
1900 rl_filename_completion_function (const char *text, int state)
1901 {
1902 return fn_filename_completion_function(text, state);
1903 }
1904
1905 void
1906 rl_forced_update_display(void)
1907 {
1908 el_set(e, EL_REFRESH);
1909 }
1910
1911 int
1912 _rl_abort_internal(void)
1913 {
1914 el_beep(e);
1915 longjmp(topbuf, 1);
1916 /*NOTREACHED*/
1917 }
1918
1919 int
1920 _rl_qsort_string_compare(char **s1, char **s2)
1921 {
1922 return strcoll(*s1, *s2);
1923 }
1924
1925 int
1926 /*ARGSUSED*/
1927 rl_kill_text(int from, int to)
1928 {
1929 return 0;
1930 }
1931
1932 Keymap
1933 rl_make_bare_keymap(void)
1934 {
1935 return NULL;
1936 }
1937
1938 Keymap
1939 rl_get_keymap(void)
1940 {
1941 return NULL;
1942 }
1943
1944 void
1945 /*ARGSUSED*/
1946 rl_set_keymap(Keymap k)
1947 {
1948 }
1949
1950 int
1951 /*ARGSUSED*/
1952 rl_generic_bind(int type, const char * keyseq, const char * data, Keymap k)
1953 {
1954 return 0;
1955 }
1956
1957 int
1958 /*ARGSUSED*/
1959 rl_bind_key_in_map(int key, Function *fun, Keymap k)
1960 {
1961 return 0;
1962 }
1963