2 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * $NetBSD: readline.c,v 1.75 2008/04/29 06:53:01 martin Exp $
30 * $DragonFly: src/lib/libedit/readline.c,v 1.5 2008/09/30 16:57:05 swildner Exp $
35 #include <sys/types.h>
57 #include "fcns.h" /* for EL_NUM_FCNS */
59 #include "readline/readline.h"
60 #include "filecomplete.h"
62 void rl_prep_terminal(int);
63 void rl_deprep_terminal(void);
65 /* for rl_complete() */
68 /* see comment at the #ifdef for sense of this */
69 /* #define GDB_411_HACK */
71 /* readline compatibility stuff - look at readline sources/documentation */
72 /* to see what these variables mean */
73 const char *rl_library_version
= "EditLine wrapper";
74 static char empty
[] = { '\0' };
75 static char expand_chars
[] = { ' ', '\t', '\n', '=', '(', '\0' };
76 static char break_chars
[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
77 '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
78 char *rl_readline_name
= empty
;
79 FILE *rl_instream
= NULL
;
80 FILE *rl_outstream
= NULL
;
83 char *rl_line_buffer
= NULL
;
84 VCPFunction
*rl_linefunc
= NULL
;
86 VFunction
*rl_event_hook
= NULL
;
87 KEYMAP_ENTRY_ARRAY emacs_standard_keymap
,
91 int history_base
= 1; /* probably never subject to change */
92 int history_length
= 0;
93 int max_input_history
= 0;
94 char history_expansion_char
= '!';
95 char history_subst_char
= '^';
96 char *history_no_expand_chars
= expand_chars
;
97 Function
*history_inhibit_expansion_function
= NULL
;
98 char *history_arg_extract(int start
, int end
, const char *str
);
100 int rl_inhibit_completion
= 0;
101 int rl_attempted_completion_over
= 0;
102 char *rl_basic_word_break_characters
= break_chars
;
103 char *rl_completer_word_break_characters
= NULL
;
104 char *rl_completer_quote_characters
= NULL
;
105 char *(* rl_completion_word_break_hook
)(void) = NULL
;
106 Function
*rl_completion_entry_function
= NULL
;
107 CPPFunction
*rl_attempted_completion_function
= NULL
;
108 Function
*rl_pre_input_hook
= NULL
;
109 Function
*rl_startup1_hook
= NULL
;
110 int (*rl_getc_function
)(FILE *) = NULL
;
111 char *rl_terminal_name
= NULL
;
112 int rl_already_prompted
= 0;
113 int rl_filename_completion_desired
= 0;
114 int rl_ignore_completion_duplicates
= 0;
115 int rl_catch_signals
= 1;
116 int readline_echoing_p
= 1;
117 int _rl_print_completions_horizontally
= 0;
118 VFunction
*rl_redisplay_function
= NULL
;
119 Function
*rl_startup_hook
= NULL
;
120 VFunction
*rl_completion_display_matches_hook
= NULL
;
121 VFunction
*rl_prep_term_function
= (VFunction
*)rl_prep_terminal
;
122 VFunction
*rl_deprep_term_function
= (VFunction
*)rl_deprep_terminal
;
125 * The current prompt string.
127 char *rl_prompt
= NULL
;
129 * This is set to character indicating type of completion being done by
130 * rl_complete_internal(); this is available for application completion
133 int rl_completion_type
= 0;
136 * If more than this number of items results from query for possible
137 * completions, we ask user if they are sure to really display the list.
139 int rl_completion_query_items
= 100;
142 * List of characters which are word break characters, but should be left
143 * in the parsed text when it is passed to the completion function.
144 * Shell uses this to help determine what kind of completing to do.
146 char *rl_special_prefixes
= NULL
;
149 * This is the character appended to the completed words if at the end of
150 * the line. Default is ' ' (a space).
152 int rl_completion_append_character
= ' ';
154 /* stuff below is used internally by libedit for readline emulation */
156 static History
*h
= NULL
;
157 static EditLine
*e
= NULL
;
158 static Function
*map
[256];
159 static jmp_buf topbuf
;
161 /* internal functions */
162 static unsigned char _el_rl_complete(EditLine
*, int);
163 static unsigned char _el_rl_tstp(EditLine
*, int);
164 static char *_get_prompt(EditLine
*);
165 static int _getc_function(EditLine
*, char *);
166 static HIST_ENTRY
*_move_history(int);
167 static int _history_expand_command(const char *, size_t, size_t,
169 static char *_rl_compat_sub(const char *, const char *,
171 static int _rl_event_read_char(EditLine
*, char *);
172 static void _rl_update_pos(void);
177 _get_prompt(EditLine
*el
__attribute__((__unused__
)))
179 rl_already_prompted
= 1;
185 * generic function for moving around history
188 _move_history(int op
)
191 static HIST_ENTRY rl_he
;
193 if (history(h
, &ev
, op
) != 0)
204 * read one key from user defined input function
208 _getc_function(EditLine
*el
, char *c
)
212 i
= (*rl_getc_function
)(NULL
);
221 * READLINE compatibility stuff
225 * initialize rl compat stuff
243 rl_outstream
= stdout
;
246 * See if we don't really want to run the editor
248 if (tcgetattr(fileno(rl_instream
), &t
) != -1 && (t
.c_lflag
& ECHO
) == 0)
251 e
= el_init(rl_readline_name
, rl_instream
, rl_outstream
, stderr
);
254 el_set(e
, EL_EDITMODE
, 0);
260 history(h
, &ev
, H_SETSIZE
, INT_MAX
); /* unlimited */
262 max_input_history
= INT_MAX
;
263 el_set(e
, EL_HIST
, history
, h
);
265 /* setup getc function if valid */
266 if (rl_getc_function
)
267 el_set(e
, EL_GETCFN
, _getc_function
);
269 /* for proper prompt printing in readline() */
270 rl_prompt
= strdup("");
271 if (rl_prompt
== NULL
) {
276 el_set(e
, EL_PROMPT
, _get_prompt
);
277 el_set(e
, EL_SIGNAL
, rl_catch_signals
);
279 /* set default mode to "emacs"-style and read setting afterwards */
280 /* so this can be overriden */
281 el_set(e
, EL_EDITOR
, "emacs");
282 if (rl_terminal_name
!= NULL
)
283 el_set(e
, EL_TERMINAL
, rl_terminal_name
);
285 el_get(e
, EL_TERMINAL
, &rl_terminal_name
);
288 * Word completion - this has to go AFTER rebinding keys
291 el_set(e
, EL_ADDFN
, "rl_complete",
292 "ReadLine compatible completion function",
294 el_set(e
, EL_BIND
, "^I", "rl_complete", NULL
);
297 * Send TSTP when ^Z is pressed.
299 el_set(e
, EL_ADDFN
, "rl_tstp",
300 "ReadLine compatible suspend function",
302 el_set(e
, EL_BIND
, "^Z", "rl_tstp", NULL
);
304 /* read settings from configuration file */
308 * Unfortunately, some applications really do use rl_point
309 * and rl_line_buffer directly.
312 /* a cheesy way to get rid of const cast. */
313 rl_line_buffer
= memchr(li
->buffer
, *li
->buffer
, 1);
317 (*rl_startup_hook
)(NULL
, 0);
324 * read one line from input stream and return it, chomping
325 * trailing newline (if there is any)
328 readline(const char *p
)
331 const char * volatile prompt
= p
;
335 static int used_event_hook
;
337 if (e
== NULL
|| h
== NULL
)
342 (void)setjmp(topbuf
);
344 /* update prompt accordingly to what has been passed */
347 if (strcmp(rl_prompt
, prompt
) != 0) {
349 rl_prompt
= strdup(prompt
);
350 if (rl_prompt
== NULL
)
354 if (rl_pre_input_hook
)
355 (*rl_pre_input_hook
)(NULL
, 0);
357 if (rl_event_hook
&& !(e
->el_flags
&NO_TTY
)) {
358 el_set(e
, EL_GETCFN
, _rl_event_read_char
);
362 if (!rl_event_hook
&& used_event_hook
) {
363 el_set(e
, EL_GETCFN
, EL_BUILTIN_GETCFN
);
367 rl_already_prompted
= 0;
369 /* get one line from input stream */
370 ret
= el_gets(e
, &count
);
372 if (ret
&& count
> 0) {
379 if (buf
[lastidx
] == '\n')
384 history(h
, &ev
, H_GETSIZE
);
385 history_length
= ev
.num
;
395 * is normally called before application starts to use
396 * history expansion functions
401 if (h
== NULL
|| e
== NULL
)
407 * substitute ``what'' with ``with'', returning resulting string; if
408 * globally == 1, substitutes all occurrences of what, otherwise only the
412 _rl_compat_sub(const char *str
, const char *what
, const char *with
,
417 size_t len
, with_len
, what_len
;
420 with_len
= strlen(with
);
421 what_len
= strlen(what
);
423 /* calculate length we need for result */
426 if (*s
== *what
&& !strncmp(s
, what
, what_len
)) {
427 len
+= with_len
- what_len
;
434 r
= result
= malloc(len
+ 1);
439 if (*s
== *what
&& !strncmp(s
, what
, what_len
)) {
440 (void)strncpy(r
, with
, with_len
);
454 static char *last_search_pat
; /* last !?pat[?] search pattern */
455 static char *last_search_match
; /* last !?pat[?] that matched */
458 get_history_event(const char *cmd
, int *cindex
, int qchar
)
460 int idx
, sign
, sub
, num
, begin
, ret
;
467 if (cmd
[idx
++] != history_expansion_char
)
470 /* find out which event to take */
471 if (cmd
[idx
] == history_expansion_char
|| cmd
[idx
] == 0) {
472 if (history(h
, &ev
, H_FIRST
) != 0)
474 *cindex
= cmd
[idx
]? (idx
+ 1):idx
;
478 if (cmd
[idx
] == '-') {
483 if ('0' <= cmd
[idx
] && cmd
[idx
] <= '9') {
487 while (cmd
[idx
] && '0' <= cmd
[idx
] && cmd
[idx
] <= '9') {
488 num
= num
* 10 + cmd
[idx
] - '0';
492 num
= history_length
- num
+ 1;
494 if (!(rl_he
= history_get(num
)))
501 if (cmd
[idx
] == '?') {
507 if (cmd
[idx
] == '\n')
509 if (sub
&& cmd
[idx
] == '?')
511 if (!sub
&& (cmd
[idx
] == ':' || cmd
[idx
] == ' '
512 || cmd
[idx
] == '\t' || cmd
[idx
] == qchar
))
517 if (sub
&& cmd
[idx
] == '?')
519 if (sub
&& len
== 0 && last_search_pat
&& *last_search_pat
)
520 pat
= last_search_pat
;
524 if ((pat
= malloc(len
+ 1)) == NULL
)
526 (void)strncpy(pat
, cmd
+ begin
, len
);
530 if (history(h
, &ev
, H_CURR
) != 0) {
531 if (pat
!= last_search_pat
)
538 if (pat
!= last_search_pat
) {
540 free(last_search_pat
);
541 last_search_pat
= pat
;
543 ret
= history_search(pat
, -1);
545 ret
= history_search_prefix(pat
, -1);
548 /* restore to end of list on failed search */
549 history(h
, &ev
, H_FIRST
);
550 (void)fprintf(rl_outstream
, "%s: Event not found\n", pat
);
551 if (pat
!= last_search_pat
)
557 if (last_search_match
&& last_search_match
!= pat
)
558 free(last_search_match
);
559 last_search_match
= pat
;
562 if (pat
!= last_search_pat
)
565 if (history(h
, &ev
, H_CURR
) != 0)
570 /* roll back to original position */
571 (void)history(h
, &ev
, H_SET
, num
);
577 * the real function doing history expansion - takes as argument command
578 * to do and data upon which the command should be executed
579 * does expansion the way I've understood readline documentation
581 * returns 0 if data was not modified, 1 if it was and 2 if the string
582 * should be only printed and not executed; in case of error,
583 * returns -1 and *result points to NULL
584 * it's callers responsibility to free() string returned in *result
587 _history_expand_command(const char *command
, size_t offs
, size_t cmdlen
,
590 char *tmp
, *search
= NULL
, *aptr
;
591 const char *ptr
, *cmd
;
592 static char *from
= NULL
, *to
= NULL
;
593 int start
, end
, idx
, has_mods
= 0;
594 int p_on
= 0, g_on
= 0;
600 /* First get event specifier */
603 if (strchr(":^*$", command
[offs
+ 1])) {
606 * "!:" is shorthand for "!!:".
607 * "!^", "!*" and "!$" are shorthand for
608 * "!!:^", "!!:*" and "!!:$" respectively.
610 str
[0] = str
[1] = '!';
612 ptr
= get_history_event(str
, &idx
, 0);
613 idx
= (command
[offs
+ 1] == ':')? 1:0;
616 if (command
[offs
+ 1] == '#') {
617 /* use command so far */
618 if ((aptr
= malloc(offs
+ 1)) == NULL
)
620 (void)strncpy(aptr
, command
, offs
);
626 qchar
= (offs
> 0 && command
[offs
- 1] == '"')? '"':0;
627 ptr
= get_history_event(command
+ offs
, &idx
, qchar
);
629 has_mods
= command
[offs
+ idx
] == ':';
632 if (ptr
== NULL
&& aptr
== NULL
)
636 *result
= strdup(aptr
? aptr
: ptr
);
642 cmd
= command
+ offs
+ idx
+ 1;
644 /* Now parse any word designators */
646 if (*cmd
== '%') /* last word matched by ?pat? */
647 tmp
= strdup(last_search_match
? last_search_match
:"");
648 else if (strchr("^*$-0123456789", *cmd
)) {
651 start
= end
= 1, cmd
++;
652 else if (*cmd
== '$')
654 else if (*cmd
== '*')
656 else if (*cmd
== '-' || isdigit((unsigned char) *cmd
)) {
658 while (*cmd
&& '0' <= *cmd
&& *cmd
<= '9')
659 start
= start
* 10 + *cmd
++ - '0';
662 if (isdigit((unsigned char) cmd
[1])) {
665 while (*cmd
&& '0' <= *cmd
&& *cmd
<= '9')
666 end
= end
* 10 + *cmd
++ - '0';
667 } else if (cmd
[1] == '$') {
674 } else if (*cmd
== '*')
679 tmp
= history_arg_extract(start
, end
, aptr
? aptr
:ptr
);
681 (void)fprintf(rl_outstream
, "%s: Bad word specifier",
682 command
+ offs
+ idx
);
688 tmp
= strdup(aptr
? aptr
:ptr
);
693 if (*cmd
== 0 || (cmd
- (command
+ offs
) >= cmdlen
)) {
698 for (; *cmd
; cmd
++) {
701 else if (*cmd
== 'h') { /* remove trailing path */
702 if ((aptr
= strrchr(tmp
, '/')) != NULL
)
704 } else if (*cmd
== 't') { /* remove leading path */
705 if ((aptr
= strrchr(tmp
, '/')) != NULL
) {
706 aptr
= strdup(aptr
+ 1);
710 } else if (*cmd
== 'r') { /* remove trailing suffix */
711 if ((aptr
= strrchr(tmp
, '.')) != NULL
)
713 } else if (*cmd
== 'e') { /* remove all but suffix */
714 if ((aptr
= strrchr(tmp
, '.')) != NULL
) {
719 } else if (*cmd
== 'p') /* print only */
721 else if (*cmd
== 'g')
723 else if (*cmd
== 's' || *cmd
== '&') {
724 char *what
, *with
, delim
;
725 size_t len
, from_len
;
728 if (*cmd
== '&' && (from
== NULL
|| to
== NULL
))
730 else if (*cmd
== 's') {
731 delim
= *(++cmd
), cmd
++;
733 what
= realloc(from
, size
);
740 for (; *cmd
&& *cmd
!= delim
; cmd
++) {
741 if (*cmd
== '\\' && cmd
[1] == delim
)
745 nwhat
= realloc(what
,
761 from
= strdup(search
);
772 cmd
++; /* shift after delim */
777 with
= realloc(to
, size
);
784 from_len
= strlen(from
);
785 for (; *cmd
&& *cmd
!= delim
; cmd
++) {
786 if (len
+ from_len
+ 1 >= size
) {
788 size
+= from_len
+ 1;
789 nwith
= realloc(with
, size
);
799 (void)strcpy(&with
[len
], from
);
804 && (*(cmd
+ 1) == delim
805 || *(cmd
+ 1) == '&'))
813 aptr
= _rl_compat_sub(tmp
, from
, to
, g_on
);
827 * csh-style history expansion
830 history_expand(char *str
, char **output
)
836 if (h
== NULL
|| e
== NULL
)
839 if (history_expansion_char
== 0) {
840 *output
= strdup(str
);
845 if (str
[0] == history_subst_char
) {
846 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
847 *output
= malloc(strlen(str
) + 4 + 1);
850 (*output
)[0] = (*output
)[1] = history_expansion_char
;
853 (void)strcpy((*output
) + 4, str
);
856 *output
= strdup(str
);
861 #define ADD_STRING(what, len, fr) \
863 if (idx + len + 1 > size) { \
864 char *nresult = realloc(result, (size += len + 1));\
865 if (nresult == NULL) { \
867 if (/*CONSTCOND*/fr) \
873 (void)strncpy(&result[idx], what, len); \
875 result[idx] = '\0'; \
881 for (i
= 0; str
[i
];) {
882 int qchar
, loop_again
;
883 size_t len
, start
, j
;
889 for (; str
[j
]; j
++) {
890 if (str
[j
] == '\\' &&
891 str
[j
+ 1] == history_expansion_char
) {
892 (void)strcpy(&str
[j
], &str
[j
+ 1]);
896 if (isspace((unsigned char) str
[j
])
900 if (str
[j
] == history_expansion_char
901 && !strchr(history_no_expand_chars
, str
[j
+ 1])
902 && (!history_inhibit_expansion_function
||
903 (*history_inhibit_expansion_function
)(str
,
908 if (str
[j
] && loop_again
) {
910 qchar
= (j
> 0 && str
[j
- 1] == '"' )? '"':0;
912 if (str
[j
] == history_expansion_char
)
918 ADD_STRING(&str
[start
], len
, 0);
920 if (str
[i
] == '\0' || str
[i
] != history_expansion_char
) {
922 ADD_STRING(&str
[i
], len
, 0);
929 ret
= _history_expand_command (str
, i
, (j
- i
), &tmp
);
930 if (ret
> 0 && tmp
) {
932 ADD_STRING(tmp
, len
, 1);
941 /* ret is 2 for "print only" option */
945 /* gdb 4.11 has been shipped with readline, where */
946 /* history_expand() returned -1 when the line */
947 /* should not be executed; in readline 2.1+ */
948 /* it should return 2 in such a case */
959 * Return a string consisting of arguments of "str" from "start" to "end".
962 history_arg_extract(int start
, int end
, const char *str
)
967 arr
= history_tokenize(str
);
970 if (arr
&& *arr
== NULL
) {
975 for (max
= 0; arr
[max
]; max
++)
988 if (start
< 0 || end
< 0 || start
> max
|| end
> max
|| start
> end
)
991 for (i
= start
, len
= 0; i
<= end
; i
++)
992 len
+= strlen(arr
[i
]) + 1;
994 result
= malloc(len
);
998 for (i
= start
, len
= 0; i
<= end
; i
++) {
999 (void)strcpy(result
+ len
, arr
[i
]);
1000 len
+= strlen(arr
[i
]);
1002 result
[len
++] = ' ';
1006 for (i
= 0; arr
[i
]; i
++)
1014 * Parse the string into individual tokens,
1015 * similar to how shell would do it.
1018 history_tokenize(const char *str
)
1020 int size
= 1, idx
= 0, i
, start
;
1022 char **result
= NULL
, *temp
, delim
= '\0';
1024 for (i
= 0; str
[i
];) {
1025 while (isspace((unsigned char) str
[i
]))
1029 if (str
[i
] == '\\') {
1030 if (str
[i
+1] != '\0')
1032 } else if (str
[i
] == delim
)
1035 (isspace((unsigned char) str
[i
]) ||
1036 strchr("()<>;&|$", str
[i
])))
1038 else if (!delim
&& strchr("'`\"", str
[i
]))
1044 if (idx
+ 2 >= size
) {
1047 nresult
= realloc(result
, size
* sizeof(char *));
1048 if (nresult
== NULL
) {
1055 temp
= malloc(len
+ 1);
1057 for (i
= 0; i
< idx
; i
++)
1062 (void)strncpy(temp
, &str
[start
], len
);
1064 result
[idx
++] = temp
;
1074 * limit size of history record to ``max'' events
1077 stifle_history(int max
)
1081 if (h
== NULL
|| e
== NULL
)
1084 if (history(h
, &ev
, H_SETSIZE
, max
) == 0)
1085 max_input_history
= max
;
1090 * "unlimit" size of history - set the limit to maximum allowed int value
1093 unstifle_history(void)
1098 history(h
, &ev
, H_SETSIZE
, INT_MAX
);
1099 omax
= max_input_history
;
1100 max_input_history
= INT_MAX
;
1101 return (omax
); /* some value _must_ be returned */
1106 history_is_stifled(void)
1109 /* cannot return true answer */
1110 return (max_input_history
!= INT_MAX
);
1115 * read history from a file given
1118 read_history(const char *filename
)
1122 if (h
== NULL
|| e
== NULL
)
1124 return (history(h
, &ev
, H_LOAD
, filename
) == -1);
1129 * write history to a file given
1132 write_history(const char *filename
)
1136 if (h
== NULL
|| e
== NULL
)
1138 return (history(h
, &ev
, H_SAVE
, filename
) == -1);
1143 * returns history ``num''th event
1145 * returned pointer points to static variable
1148 history_get(int num
)
1150 static HIST_ENTRY she
;
1154 if (h
== NULL
|| e
== NULL
)
1157 /* save current position */
1158 if (history(h
, &ev
, H_CURR
) != 0)
1162 /* start from most recent */
1163 if (history(h
, &ev
, H_FIRST
) != 0)
1164 return (NULL
); /* error */
1166 /* look backwards for event matching specified offset */
1167 if (history(h
, &ev
, H_NEXT_EVENT
, num
+ 1))
1173 /* restore pointer to where it was */
1174 (void)history(h
, &ev
, H_SET
, curr_num
);
1181 * add the line to history table
1184 add_history(const char *line
)
1188 if (h
== NULL
|| e
== NULL
)
1191 (void)history(h
, &ev
, H_ENTER
, line
);
1192 if (history(h
, &ev
, H_GETSIZE
) == 0)
1193 history_length
= ev
.num
;
1195 return (!(history_length
> 0)); /* return 0 if all is okay */
1200 * remove the specified entry from the history list and return it.
1203 remove_history(int num
)
1208 if (h
== NULL
|| e
== NULL
)
1211 if (history(h
, &ev
, H_DEL
, num
) != 0)
1214 if ((she
= malloc(sizeof(*she
))) == NULL
)
1225 * clear the history list - delete all entries
1232 history(h
, &ev
, H_CLEAR
);
1237 * returns offset of the current history event
1245 if (history(h
, &ev
, H_CURR
) != 0)
1249 history(h
, &ev
, H_FIRST
);
1251 while (ev
.num
!= curr_num
&& history(h
, &ev
, H_NEXT
) == 0)
1259 * returns current history event or NULL if there is no such event
1262 current_history(void)
1265 return (_move_history(H_CURR
));
1270 * returns total number of bytes history events' data are using
1273 history_total_bytes(void)
1278 if (history(h
, &ev
, H_CURR
) != 0)
1282 history(h
, &ev
, H_FIRST
);
1285 size
+= strlen(ev
.str
);
1286 while (history(h
, &ev
, H_NEXT
) == 0);
1288 /* get to the same position as before */
1289 history(h
, &ev
, H_PREV_EVENT
, curr_num
);
1296 * sets the position in the history list to ``pos''
1299 history_set_pos(int pos
)
1304 if (pos
> history_length
|| pos
< 0)
1307 history(h
, &ev
, H_CURR
);
1310 if (history(h
, &ev
, H_SET
, pos
)) {
1311 history(h
, &ev
, H_SET
, curr_num
);
1319 * returns previous event in history and shifts pointer accordingly
1322 previous_history(void)
1325 return (_move_history(H_PREV
));
1330 * returns next event in history and shifts pointer accordingly
1336 return (_move_history(H_NEXT
));
1341 * searches for first history event containing the str
1344 history_search(const char *str
, int direction
)
1350 if (history(h
, &ev
, H_CURR
) != 0)
1355 if ((strp
= strstr(ev
.str
, str
)) != NULL
)
1356 return (int) (strp
- ev
.str
);
1357 if (history(h
, &ev
, direction
< 0 ? H_NEXT
:H_PREV
) != 0)
1360 history(h
, &ev
, H_SET
, curr_num
);
1366 * searches for first history event beginning with str
1369 history_search_prefix(const char *str
, int direction
)
1373 return (history(h
, &ev
, direction
< 0? H_PREV_STR
:H_NEXT_STR
, str
));
1378 * search for event in history containing str, starting at offset
1379 * abs(pos); continue backward, if pos<0, forward otherwise
1383 history_search_pos(const char *str
,
1384 int direction
__attribute__((__unused__
)), int pos
)
1389 off
= (pos
> 0) ? pos
: -pos
;
1390 pos
= (pos
> 0) ? 1 : -1;
1392 if (history(h
, &ev
, H_CURR
) != 0)
1396 if (history_set_pos(off
) != 0 || history(h
, &ev
, H_CURR
) != 0)
1401 if (strstr(ev
.str
, str
))
1403 if (history(h
, &ev
, (pos
< 0) ? H_PREV
: H_NEXT
) != 0)
1407 /* set "current" pointer back to previous state */
1408 history(h
, &ev
, (pos
< 0) ? H_NEXT_EVENT
: H_PREV_EVENT
, curr_num
);
1414 /********************************/
1415 /* completion functions */
1418 tilde_expand(char *name
)
1420 return fn_tilde_expand(name
);
1424 filename_completion_function(const char *name
, int state
)
1426 return fn_filename_completion_function(name
, state
);
1430 * a completion generator for usernames; returns _first_ username
1431 * which starts with supplied text
1432 * text contains a partial username preceded by random character
1433 * (usually '~'); state is ignored
1434 * it's callers responsibility to free returned value
1437 username_completion_function(const char *text
, int state
)
1440 For when getpwent_r is supported:
1441 struct passwd *pwd, pwres;
1445 /* remove the above when getpwend_r is supported */
1447 if (text
[0] == '\0')
1457 For when getpwent_r is supported:
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);
1464 if (pwd
!= NULL
&& text
[0] == pwd
->pw_name
[0] && strcmp(text
, pwd
->pw_name
) == 0)
1468 /* remove the above when getpwend_r is supported */
1474 return (strdup(pwd
->pw_name
));
1479 * el-compatible wrapper to send TSTP on ^Z
1482 static unsigned char
1483 _el_rl_tstp(EditLine
*el
__attribute__((__unused__
)), int ch
__attribute__((__unused__
)))
1485 (void)kill(0, SIGTSTP
);
1490 * Display list of strings in columnar format on readline's output stream.
1491 * 'matches' is list of strings, 'len' is number of strings in 'matches',
1492 * 'max' is maximum length of string in 'matches'.
1495 rl_display_match_list(char **matches
, int len
, int max
)
1498 fn_display_match_list(e
, matches
, len
, max
);
1503 _rl_completion_append_character_function(const char *dummy
1504 __attribute__((__unused__
)))
1507 buf
[1] = rl_completion_append_character
;
1513 * complete word at current point
1517 rl_complete(int ignore
__attribute__((__unused__
)), int invoking_key
)
1519 char *breakchars
= NULL
;
1521 if (h
== NULL
|| e
== NULL
)
1524 if (rl_inhibit_completion
) {
1526 arr
[0] = (char)invoking_key
;
1528 el_insertstr(e
, arr
);
1529 return (CC_REFRESH
);
1532 if (rl_completion_word_break_hook
!= NULL
)
1533 breakchars
= rl_completion_word_break_hook();
1534 if (breakchars
== NULL
)
1535 breakchars
= rl_basic_word_break_characters
;
1537 /* Just look at how many global variables modify this operation! */
1538 return fn_complete(e
,
1539 (CPFunction
*)rl_completion_entry_function
,
1540 rl_attempted_completion_function
,
1541 breakchars
, rl_special_prefixes
,
1542 _rl_completion_append_character_function
, rl_completion_query_items
,
1543 &rl_completion_type
, &rl_attempted_completion_over
,
1544 &rl_point
, &rl_end
);
1549 static unsigned char
1550 _el_rl_complete(EditLine
*el
__attribute__((__unused__
)), int ch
)
1552 return (unsigned char)rl_complete(0, ch
);
1556 * misc other functions
1560 * bind key c to readline-type function func
1563 rl_bind_key(int c
, int func(int, int))
1567 if (h
== NULL
|| e
== NULL
)
1570 if (func
== rl_insert
) {
1571 /* XXX notice there is no range checking of ``c'' */
1572 e
->el_map
.key
[c
] = ED_INSERT
;
1580 * read one key from input - handles chars pushed back
1581 * to input stream also
1586 char fooarr
[2 * sizeof(int)];
1588 if (e
== NULL
|| h
== NULL
)
1591 return (el_getc(e
, fooarr
));
1596 * reset the terminal
1600 rl_reset_terminal(const char *p
__attribute__((__unused__
)))
1603 if (h
== NULL
|| e
== NULL
)
1610 * insert character ``c'' back into input stream, ``count'' times
1613 rl_insert(int count
, int c
)
1617 if (h
== NULL
|| e
== NULL
)
1620 /* XXX - int -> char conversion can lose on multichars */
1624 for (; count
> 0; count
--)
1632 rl_newline(int count
, int c
)
1635 * Readline-4.0 appears to ignore the args.
1637 return rl_insert(1, '\n');
1641 static unsigned char
1642 rl_bind_wrapper(EditLine
*el
, unsigned char c
)
1651 /* If rl_done was set by the above call, deal with it here */
1659 rl_add_defun(const char *name
, Function
*fun
, int c
)
1662 if (c
>= sizeof(map
) / sizeof(map
[0]) || c
< 0)
1664 map
[(unsigned char)c
] = fun
;
1665 el_set(e
, EL_ADDFN
, name
, name
, rl_bind_wrapper
);
1666 vis(dest
, c
, VIS_WHITE
|VIS_NOSLASH
, 0);
1667 el_set(e
, EL_BIND
, dest
, name
);
1672 rl_callback_read_char(void)
1674 int count
= 0, done
= 0;
1675 const char *buf
= el_gets(e
, &count
);
1678 if (buf
== NULL
|| count
-- <= 0)
1680 if (count
== 0 && buf
[0] == e
->el_tty
.t_c
[TS_IO
][C_EOF
])
1682 if (buf
[count
] == '\n' || buf
[count
] == '\r')
1685 if (done
&& rl_linefunc
!= NULL
) {
1686 el_set(e
, EL_UNBUFFERED
, 0);
1688 if ((wbuf
= strdup(buf
)) != NULL
)
1692 (*(void (*)(const char *))rl_linefunc
)(wbuf
);
1693 el_set(e
, EL_UNBUFFERED
, 1);
1698 rl_callback_handler_install(const char *prompt
, VCPFunction
*linefunc
)
1705 rl_prompt
= prompt
? strdup(strchr(prompt
, *prompt
)) : NULL
;
1706 rl_linefunc
= linefunc
;
1707 el_set(e
, EL_UNBUFFERED
, 1);
1711 rl_callback_handler_remove(void)
1713 el_set(e
, EL_UNBUFFERED
, 0);
1721 a
[0] = e
->el_tty
.t_c
[TS_IO
][C_REPRINT
];
1727 rl_get_previous_history(int count
, int key
)
1739 rl_prep_terminal(int meta_flag
)
1741 el_set(e
, EL_PREP_TERM
, 1);
1745 rl_deprep_terminal(void)
1747 el_set(e
, EL_PREP_TERM
, 0);
1751 rl_read_init_file(const char *s
)
1753 return(el_source(e
, s
));
1757 rl_parse_and_bind(const char *line
)
1763 tok
= tok_init(NULL
);
1764 tok_str(tok
, line
, &argc
, &argv
);
1765 argc
= el_parse(e
, argc
, argv
);
1767 return (argc
? 1 : 0);
1771 rl_variable_bind(const char *var
, const char *value
)
1774 * The proper return value is undocument, but this is what the
1775 * readline source seems to do.
1777 return ((el_set(e
, EL_BIND
, "", var
, value
) == -1) ? 1 : 0);
1781 rl_stuff_char(int c
)
1787 el_insertstr(e
, buf
);
1791 _rl_event_read_char(EditLine
*el
, char *cp
)
1793 int n
, num_read
= 0;
1796 while (rl_event_hook
) {
1800 #if defined(FIONREAD)
1801 if (ioctl(el
->el_infd
, FIONREAD
, &n
) < 0)
1804 num_read
= read(el
->el_infd
, cp
, 1);
1807 #elif defined(F_SETFL) && defined(O_NDELAY)
1808 if ((n
= fcntl(el
->el_infd
, F_GETFL
, 0)) < 0)
1810 if (fcntl(el
->el_infd
, F_SETFL
, n
|O_NDELAY
) < 0)
1812 num_read
= read(el
->el_infd
, cp
, 1);
1813 if (fcntl(el
->el_infd
, F_SETFL
, n
))
1816 /* not non-blocking, but what you gonna do? */
1817 num_read
= read(el
->el_infd
, cp
, 1);
1821 if (num_read
< 0 && errno
== EAGAIN
)
1828 el_set(el
, EL_GETCFN
, EL_BUILTIN_GETCFN
);
1833 _rl_update_pos(void)
1835 const LineInfo
*li
= el_line(e
);
1837 rl_point
= li
->cursor
- li
->buffer
;
1838 rl_end
= li
->lastchar
- li
->buffer
;
1842 rl_get_screen_size(int *rows
, int *cols
)
1845 el_get(e
, EL_GETTC
, "li", rows
);
1847 el_get(e
, EL_GETTC
, "co", cols
);
1851 rl_set_screen_size(int rows
, int cols
)
1854 (void)snprintf(buf
, sizeof(buf
), "%d", rows
);
1855 el_set(e
, EL_SETTC
, "li", buf
);
1856 (void)snprintf(buf
, sizeof(buf
), "%d", cols
);
1857 el_set(e
, EL_SETTC
, "co", buf
);
1861 rl_completion_matches(const char *str
, rl_compentry_func_t
*fun
)
1863 size_t len
, max
, i
, j
, min
;
1864 char **list
, *match
, *a
, *b
;
1868 if ((list
= malloc(max
* sizeof(*list
))) == NULL
)
1871 while ((match
= (*fun
)(str
, (int)(len
- 1))) != NULL
) {
1875 if ((nl
= realloc(list
, max
* sizeof(*nl
))) == NULL
)
1879 list
[len
++] = match
;
1885 if ((list
[0] = strdup(list
[1])) == NULL
)
1889 qsort(&list
[1], len
- 1, sizeof(*list
),
1890 (int (*)(const void *, const void *)) strcmp
);
1892 for (i
= 1, a
= list
[i
]; i
< len
- 1; i
++, a
= b
) {
1894 for (j
= 0; a
[j
] && a
[j
] == b
[j
]; j
++)
1899 if (min
== 0 && *str
) {
1900 if ((list
[0] = strdup(str
)) == NULL
)
1903 if ((list
[0] = malloc(min
+ 1)) == NULL
)
1905 (void)memcpy(list
[0], list
[1], min
);
1906 list
[0][min
] = '\0';
1916 rl_filename_completion_function (const char *text
, int state
)
1918 return fn_filename_completion_function(text
, state
);
1922 rl_forced_update_display(void)
1924 el_set(e
, EL_REFRESH
);
1928 _rl_abort_internal(void)
1936 _rl_qsort_string_compare(char **s1
, char **s2
)
1938 return strcoll(*s1
, *s2
);
1943 rl_kill_text(int from
, int to
)
1949 rl_make_bare_keymap(void)
1962 rl_set_keymap(Keymap k
)
1968 rl_generic_bind(int type
, const char * keyseq
, const char * data
, Keymap k
)
1975 rl_bind_key_in_map(int key
, Function
*fun
, Keymap k
)