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.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
36 * $NetBSD: readline.c,v 1.57 2005/06/11 18:18:59 christos Exp $
37 * $DragonFly: src/lib/libedit/readline.c,v 1.1 2005/11/13 11:58:30 corecode Exp $
42 #include <sys/types.h>
63 #include "fcns.h" /* for EL_NUM_FCNS */
65 #include "readline/readline.h"
66 #include "filecomplete.h"
68 /* for rl_complete() */
71 /* see comment at the #ifdef for sense of this */
72 /* #define GDB_411_HACK */
74 /* readline compatibility stuff - look at readline sources/documentation */
75 /* to see what these variables mean */
76 const char *rl_library_version
= "EditLine wrapper";
77 static char empty
[] = { '\0' };
78 static char expand_chars
[] = { ' ', '\t', '\n', '=', '(', '\0' };
79 static char break_chars
[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
80 '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
81 char *rl_readline_name
= empty
;
82 FILE *rl_instream
= NULL
;
83 FILE *rl_outstream
= NULL
;
86 char *rl_line_buffer
= NULL
;
87 VCPFunction
*rl_linefunc
= NULL
;
89 VFunction
*rl_event_hook
= NULL
;
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 Function
*rl_completion_entry_function
= NULL
;
106 CPPFunction
*rl_attempted_completion_function
= NULL
;
107 Function
*rl_pre_input_hook
= NULL
;
108 Function
*rl_startup1_hook
= NULL
;
109 Function
*rl_getc_function
= NULL
;
110 char *rl_terminal_name
= NULL
;
111 int rl_already_prompted
= 0;
112 int rl_filename_completion_desired
= 0;
113 int rl_ignore_completion_duplicates
= 0;
114 int rl_catch_signals
= 1;
115 VFunction
*rl_redisplay_function
= NULL
;
116 Function
*rl_startup_hook
= NULL
;
117 VFunction
*rl_completion_display_matches_hook
= NULL
;
118 VFunction
*rl_prep_term_function
= NULL
;
119 VFunction
*rl_deprep_term_function
= NULL
;
122 * The current prompt string.
124 char *rl_prompt
= NULL
;
126 * This is set to character indicating type of completion being done by
127 * rl_complete_internal(); this is available for application completion
130 int rl_completion_type
= 0;
133 * If more than this number of items results from query for possible
134 * completions, we ask user if they are sure to really display the list.
136 int rl_completion_query_items
= 100;
139 * List of characters which are word break characters, but should be left
140 * in the parsed text when it is passed to the completion function.
141 * Shell uses this to help determine what kind of completing to do.
143 char *rl_special_prefixes
= NULL
;
146 * This is the character appended to the completed words if at the end of
147 * the line. Default is ' ' (a space).
149 int rl_completion_append_character
= ' ';
151 /* stuff below is used internally by libedit for readline emulation */
153 static History
*h
= NULL
;
154 static EditLine
*e
= NULL
;
155 static Function
*map
[256];
157 /* internal functions */
158 static unsigned char _el_rl_complete(EditLine
*, int);
159 static unsigned char _el_rl_tstp(EditLine
*, int);
160 static char *_get_prompt(EditLine
*);
161 static HIST_ENTRY
*_move_history(int);
162 static int _history_expand_command(const char *, size_t, size_t,
164 static char *_rl_compat_sub(const char *, const char *,
166 static int _rl_event_read_char(EditLine
*, char *);
167 static void _rl_update_pos(void);
172 _get_prompt(EditLine
*el
__attribute__((__unused__
)))
174 rl_already_prompted
= 1;
180 * generic function for moving around history
183 _move_history(int op
)
186 static HIST_ENTRY rl_he
;
188 if (history(h
, &ev
, op
) != 0)
189 return (HIST_ENTRY
*) NULL
;
199 * READLINE compatibility stuff
203 * initialize rl compat stuff
221 rl_outstream
= stdout
;
224 * See if we don't really want to run the editor
226 if (tcgetattr(fileno(rl_instream
), &t
) != -1 && (t
.c_lflag
& ECHO
) == 0)
229 e
= el_init(rl_readline_name
, rl_instream
, rl_outstream
, stderr
);
232 el_set(e
, EL_EDITMODE
, 0);
238 history(h
, &ev
, H_SETSIZE
, INT_MAX
); /* unlimited */
240 max_input_history
= INT_MAX
;
241 el_set(e
, EL_HIST
, history
, h
);
243 /* for proper prompt printing in readline() */
244 rl_prompt
= strdup("");
245 if (rl_prompt
== NULL
) {
250 el_set(e
, EL_PROMPT
, _get_prompt
);
251 el_set(e
, EL_SIGNAL
, rl_catch_signals
);
253 /* set default mode to "emacs"-style and read setting afterwards */
254 /* so this can be overriden */
255 el_set(e
, EL_EDITOR
, "emacs");
256 if (rl_terminal_name
!= NULL
)
257 el_set(e
, EL_TERMINAL
, rl_terminal_name
);
259 el_get(e
, EL_TERMINAL
, &rl_terminal_name
);
262 * Word completion - this has to go AFTER rebinding keys
265 el_set(e
, EL_ADDFN
, "rl_complete",
266 "ReadLine compatible completion function",
268 el_set(e
, EL_BIND
, "^I", "rl_complete", NULL
);
271 * Send TSTP when ^Z is pressed.
273 el_set(e
, EL_ADDFN
, "rl_tstp",
274 "ReadLine compatible suspend function",
276 el_set(e
, EL_BIND
, "^Z", "rl_tstp", NULL
);
278 /* read settings from configuration file */
282 * Unfortunately, some applications really do use rl_point
283 * and rl_line_buffer directly.
286 /* a cheesy way to get rid of const cast. */
287 rl_line_buffer
= memchr(li
->buffer
, *li
->buffer
, 1);
291 (*rl_startup_hook
)(NULL
, 0);
298 * read one line from input stream and return it, chomping
299 * trailing newline (if there is any)
302 readline(const char *prompt
)
308 static int used_event_hook
;
310 if (e
== NULL
|| h
== NULL
)
315 /* update prompt accordingly to what has been passed */
318 if (strcmp(rl_prompt
, prompt
) != 0) {
320 rl_prompt
= strdup(prompt
);
321 if (rl_prompt
== NULL
)
325 if (rl_pre_input_hook
)
326 (*rl_pre_input_hook
)(NULL
, 0);
328 if (rl_event_hook
&& !(e
->el_flags
&NO_TTY
)) {
329 el_set(e
, EL_GETCFN
, _rl_event_read_char
);
333 if (!rl_event_hook
&& used_event_hook
) {
334 el_set(e
, EL_GETCFN
, EL_BUILTIN_GETCFN
);
338 rl_already_prompted
= 0;
340 /* get one line from input stream */
341 ret
= el_gets(e
, &count
);
343 if (ret
&& count
> 0) {
350 if (buf
[lastidx
] == '\n')
355 history(h
, &ev
, H_GETSIZE
);
356 history_length
= ev
.num
;
366 * is normally called before application starts to use
367 * history expansion functions
372 if (h
== NULL
|| e
== NULL
)
378 * substitute ``what'' with ``with'', returning resulting string; if
379 * globally == 1, substitutes all occurrences of what, otherwise only the
383 _rl_compat_sub(const char *str
, const char *what
, const char *with
,
388 size_t len
, with_len
, what_len
;
391 with_len
= strlen(with
);
392 what_len
= strlen(what
);
394 /* calculate length we need for result */
397 if (*s
== *what
&& !strncmp(s
, what
, what_len
)) {
398 len
+= with_len
- what_len
;
405 r
= result
= malloc(len
+ 1);
410 if (*s
== *what
&& !strncmp(s
, what
, what_len
)) {
411 (void)strncpy(r
, with
, with_len
);
425 static char *last_search_pat
; /* last !?pat[?] search pattern */
426 static char *last_search_match
; /* last !?pat[?] that matched */
429 get_history_event(const char *cmd
, int *cindex
, int qchar
)
431 int idx
, sign
, sub
, num
, begin
, ret
;
438 if (cmd
[idx
++] != history_expansion_char
)
441 /* find out which event to take */
442 if (cmd
[idx
] == history_expansion_char
|| cmd
[idx
] == 0) {
443 if (history(h
, &ev
, H_FIRST
) != 0)
445 *cindex
= cmd
[idx
]? (idx
+ 1):idx
;
449 if (cmd
[idx
] == '-') {
454 if ('0' <= cmd
[idx
] && cmd
[idx
] <= '9') {
458 while (cmd
[idx
] && '0' <= cmd
[idx
] && cmd
[idx
] <= '9') {
459 num
= num
* 10 + cmd
[idx
] - '0';
463 num
= history_length
- num
+ 1;
465 if (!(rl_he
= history_get(num
)))
472 if (cmd
[idx
] == '?') {
478 if (cmd
[idx
] == '\n')
480 if (sub
&& cmd
[idx
] == '?')
482 if (!sub
&& (cmd
[idx
] == ':' || cmd
[idx
] == ' '
483 || cmd
[idx
] == '\t' || cmd
[idx
] == qchar
))
488 if (sub
&& cmd
[idx
] == '?')
490 if (sub
&& len
== 0 && last_search_pat
&& *last_search_pat
)
491 pat
= last_search_pat
;
495 if ((pat
= malloc(len
+ 1)) == NULL
)
497 (void)strncpy(pat
, cmd
+ begin
, len
);
501 if (history(h
, &ev
, H_CURR
) != 0) {
502 if (pat
!= last_search_pat
)
509 if (pat
!= last_search_pat
) {
511 free(last_search_pat
);
512 last_search_pat
= pat
;
514 ret
= history_search(pat
, -1);
516 ret
= history_search_prefix(pat
, -1);
519 /* restore to end of list on failed search */
520 history(h
, &ev
, H_FIRST
);
521 (void)fprintf(rl_outstream
, "%s: Event not found\n", pat
);
522 if (pat
!= last_search_pat
)
528 if (last_search_match
&& last_search_match
!= pat
)
529 free(last_search_match
);
530 last_search_match
= pat
;
533 if (pat
!= last_search_pat
)
536 if (history(h
, &ev
, H_CURR
) != 0)
541 /* roll back to original position */
542 (void)history(h
, &ev
, H_SET
, num
);
548 * the real function doing history expansion - takes as argument command
549 * to do and data upon which the command should be executed
550 * does expansion the way I've understood readline documentation
552 * returns 0 if data was not modified, 1 if it was and 2 if the string
553 * should be only printed and not executed; in case of error,
554 * returns -1 and *result points to NULL
555 * it's callers responsibility to free() string returned in *result
558 _history_expand_command(const char *command
, size_t offs
, size_t cmdlen
,
561 char *tmp
, *search
= NULL
, *aptr
;
562 const char *ptr
, *cmd
;
563 static char *from
= NULL
, *to
= NULL
;
564 int start
, end
, idx
, has_mods
= 0;
565 int p_on
= 0, g_on
= 0;
571 /* First get event specifier */
574 if (strchr(":^*$", command
[offs
+ 1])) {
577 * "!:" is shorthand for "!!:".
578 * "!^", "!*" and "!$" are shorthand for
579 * "!!:^", "!!:*" and "!!:$" respectively.
581 str
[0] = str
[1] = '!';
583 ptr
= get_history_event(str
, &idx
, 0);
584 idx
= (command
[offs
+ 1] == ':')? 1:0;
587 if (command
[offs
+ 1] == '#') {
588 /* use command so far */
589 if ((aptr
= malloc(offs
+ 1)) == NULL
)
591 (void)strncpy(aptr
, command
, offs
);
597 qchar
= (offs
> 0 && command
[offs
- 1] == '"')? '"':0;
598 ptr
= get_history_event(command
+ offs
, &idx
, qchar
);
600 has_mods
= command
[offs
+ idx
] == ':';
603 if (ptr
== NULL
&& aptr
== NULL
)
607 *result
= strdup(aptr
? aptr
: ptr
);
613 cmd
= command
+ offs
+ idx
+ 1;
615 /* Now parse any word designators */
617 if (*cmd
== '%') /* last word matched by ?pat? */
618 tmp
= strdup(last_search_match
? last_search_match
:"");
619 else if (strchr("^*$-0123456789", *cmd
)) {
622 start
= end
= 1, cmd
++;
623 else if (*cmd
== '$')
625 else if (*cmd
== '*')
627 else if (*cmd
== '-' || isdigit((unsigned char) *cmd
)) {
629 while (*cmd
&& '0' <= *cmd
&& *cmd
<= '9')
630 start
= start
* 10 + *cmd
++ - '0';
633 if (isdigit((unsigned char) cmd
[1])) {
636 while (*cmd
&& '0' <= *cmd
&& *cmd
<= '9')
637 end
= end
* 10 + *cmd
++ - '0';
638 } else if (cmd
[1] == '$') {
645 } else if (*cmd
== '*')
650 tmp
= history_arg_extract(start
, end
, aptr
? aptr
:ptr
);
652 (void)fprintf(rl_outstream
, "%s: Bad word specifier",
653 command
+ offs
+ idx
);
659 tmp
= strdup(aptr
? aptr
:ptr
);
664 if (*cmd
== 0 || (cmd
- (command
+ offs
) >= cmdlen
)) {
669 for (; *cmd
; cmd
++) {
672 else if (*cmd
== 'h') { /* remove trailing path */
673 if ((aptr
= strrchr(tmp
, '/')) != NULL
)
675 } else if (*cmd
== 't') { /* remove leading path */
676 if ((aptr
= strrchr(tmp
, '/')) != NULL
) {
677 aptr
= strdup(aptr
+ 1);
681 } else if (*cmd
== 'r') { /* remove trailing suffix */
682 if ((aptr
= strrchr(tmp
, '.')) != NULL
)
684 } else if (*cmd
== 'e') { /* remove all but suffix */
685 if ((aptr
= strrchr(tmp
, '.')) != NULL
) {
690 } else if (*cmd
== 'p') /* print only */
692 else if (*cmd
== 'g')
694 else if (*cmd
== 's' || *cmd
== '&') {
695 char *what
, *with
, delim
;
696 size_t len
, from_len
;
699 if (*cmd
== '&' && (from
== NULL
|| to
== NULL
))
701 else if (*cmd
== 's') {
702 delim
= *(++cmd
), cmd
++;
704 what
= realloc(from
, size
);
710 for (; *cmd
&& *cmd
!= delim
; cmd
++) {
711 if (*cmd
== '\\' && cmd
[1] == delim
)
715 nwhat
= realloc(what
,
730 from
= strdup(search
);
738 cmd
++; /* shift after delim */
743 with
= realloc(to
, size
);
749 from_len
= strlen(from
);
750 for (; *cmd
&& *cmd
!= delim
; cmd
++) {
751 if (len
+ from_len
+ 1 >= size
) {
753 size
+= from_len
+ 1;
754 nwith
= realloc(with
, size
);
763 (void)strcpy(&with
[len
], from
);
768 && (*(cmd
+ 1) == delim
769 || *(cmd
+ 1) == '&'))
777 aptr
= _rl_compat_sub(tmp
, from
, to
, g_on
);
791 * csh-style history expansion
794 history_expand(char *str
, char **output
)
800 if (h
== NULL
|| e
== NULL
)
803 if (history_expansion_char
== 0) {
804 *output
= strdup(str
);
809 if (str
[0] == history_subst_char
) {
810 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
811 *output
= malloc(strlen(str
) + 4 + 1);
814 (*output
)[0] = (*output
)[1] = history_expansion_char
;
817 (void)strcpy((*output
) + 4, str
);
820 *output
= strdup(str
);
825 #define ADD_STRING(what, len) \
827 if (idx + len + 1 > size) { \
828 char *nresult = realloc(result, (size += len + 1));\
829 if (nresult == NULL) { \
835 (void)strncpy(&result[idx], what, len); \
837 result[idx] = '\0'; \
842 for (i
= 0; str
[i
];) {
843 int qchar
, loop_again
;
844 size_t len
, start
, j
;
850 for (; str
[j
]; j
++) {
851 if (str
[j
] == '\\' &&
852 str
[j
+ 1] == history_expansion_char
) {
853 (void)strcpy(&str
[j
], &str
[j
+ 1]);
857 if (isspace((unsigned char) str
[j
])
861 if (str
[j
] == history_expansion_char
862 && !strchr(history_no_expand_chars
, str
[j
+ 1])
863 && (!history_inhibit_expansion_function
||
864 (*history_inhibit_expansion_function
)(str
,
869 if (str
[j
] && loop_again
) {
871 qchar
= (j
> 0 && str
[j
- 1] == '"' )? '"':0;
873 if (str
[j
] == history_expansion_char
)
880 ADD_STRING(tmp
, len
);
882 if (str
[i
] == '\0' || str
[i
] != history_expansion_char
) {
885 ADD_STRING(tmp
, len
);
892 ret
= _history_expand_command (str
, i
, (j
- i
), &tmp
);
893 if (ret
> 0 && tmp
) {
895 ADD_STRING(tmp
, len
);
901 /* ret is 2 for "print only" option */
905 /* gdb 4.11 has been shipped with readline, where */
906 /* history_expand() returned -1 when the line */
907 /* should not be executed; in readline 2.1+ */
908 /* it should return 2 in such a case */
919 * Return a string consisting of arguments of "str" from "start" to "end".
922 history_arg_extract(int start
, int end
, const char *str
)
927 arr
= history_tokenize(str
);
930 if (arr
&& *arr
== NULL
) {
935 for (max
= 0; arr
[max
]; max
++)
948 if (start
< 0 || end
< 0 || start
> max
|| end
> max
|| start
> end
)
951 for (i
= start
, len
= 0; i
<= end
; i
++)
952 len
+= strlen(arr
[i
]) + 1;
954 result
= malloc(len
);
958 for (i
= start
, len
= 0; i
<= end
; i
++) {
959 (void)strcpy(result
+ len
, arr
[i
]);
960 len
+= strlen(arr
[i
]);
966 for (i
= 0; arr
[i
]; i
++)
974 * Parse the string into individual tokens,
975 * similar to how shell would do it.
978 history_tokenize(const char *str
)
980 int size
= 1, idx
= 0, i
, start
;
982 char **result
= NULL
, *temp
, delim
= '\0';
984 for (i
= 0; str
[i
];) {
985 while (isspace((unsigned char) str
[i
]))
989 if (str
[i
] == '\\') {
990 if (str
[i
+1] != '\0')
992 } else if (str
[i
] == delim
)
995 (isspace((unsigned char) str
[i
]) ||
996 strchr("()<>;&|$", str
[i
])))
998 else if (!delim
&& strchr("'`\"", str
[i
]))
1004 if (idx
+ 2 >= size
) {
1007 nresult
= realloc(result
, size
* sizeof(char *));
1008 if (nresult
== NULL
) {
1015 temp
= malloc(len
+ 1);
1017 for (i
= 0; i
< idx
; i
++)
1022 (void)strncpy(temp
, &str
[start
], len
);
1024 result
[idx
++] = temp
;
1034 * limit size of history record to ``max'' events
1037 stifle_history(int max
)
1041 if (h
== NULL
|| e
== NULL
)
1044 if (history(h
, &ev
, H_SETSIZE
, max
) == 0)
1045 max_input_history
= max
;
1050 * "unlimit" size of history - set the limit to maximum allowed int value
1053 unstifle_history(void)
1058 history(h
, &ev
, H_SETSIZE
, INT_MAX
);
1059 omax
= max_input_history
;
1060 max_input_history
= INT_MAX
;
1061 return (omax
); /* some value _must_ be returned */
1066 history_is_stifled(void)
1069 /* cannot return true answer */
1070 return (max_input_history
!= INT_MAX
);
1075 * read history from a file given
1078 read_history(const char *filename
)
1082 if (h
== NULL
|| e
== NULL
)
1084 return (history(h
, &ev
, H_LOAD
, filename
));
1089 * write history to a file given
1092 write_history(const char *filename
)
1096 if (h
== NULL
|| e
== NULL
)
1098 return (history(h
, &ev
, H_SAVE
, filename
));
1103 * returns history ``num''th event
1105 * returned pointer points to static variable
1108 history_get(int num
)
1110 static HIST_ENTRY she
;
1114 if (h
== NULL
|| e
== NULL
)
1117 /* save current position */
1118 if (history(h
, &ev
, H_CURR
) != 0)
1122 /* start from most recent */
1123 if (history(h
, &ev
, H_FIRST
) != 0)
1124 return (NULL
); /* error */
1126 /* look backwards for event matching specified offset */
1127 if (history(h
, &ev
, H_NEXT_EVENT
, num
))
1133 /* restore pointer to where it was */
1134 (void)history(h
, &ev
, H_SET
, curr_num
);
1141 * add the line to history table
1144 add_history(const char *line
)
1148 if (h
== NULL
|| e
== NULL
)
1151 (void)history(h
, &ev
, H_ENTER
, line
);
1152 if (history(h
, &ev
, H_GETSIZE
) == 0)
1153 history_length
= ev
.num
;
1155 return (!(history_length
> 0)); /* return 0 if all is okay */
1160 * clear the history list - delete all entries
1167 history(h
, &ev
, H_CLEAR
);
1172 * returns offset of the current history event
1180 if (history(h
, &ev
, H_CURR
) != 0)
1184 history(h
, &ev
, H_FIRST
);
1186 while (ev
.num
!= curr_num
&& history(h
, &ev
, H_NEXT
) == 0)
1194 * returns current history event or NULL if there is no such event
1197 current_history(void)
1200 return (_move_history(H_CURR
));
1205 * returns total number of bytes history events' data are using
1208 history_total_bytes(void)
1213 if (history(h
, &ev
, H_CURR
) != 0)
1217 history(h
, &ev
, H_FIRST
);
1220 size
+= strlen(ev
.str
);
1221 while (history(h
, &ev
, H_NEXT
) == 0);
1223 /* get to the same position as before */
1224 history(h
, &ev
, H_PREV_EVENT
, curr_num
);
1231 * sets the position in the history list to ``pos''
1234 history_set_pos(int pos
)
1239 if (pos
> history_length
|| pos
< 0)
1242 history(h
, &ev
, H_CURR
);
1245 if (history(h
, &ev
, H_SET
, pos
)) {
1246 history(h
, &ev
, H_SET
, curr_num
);
1254 * returns previous event in history and shifts pointer accordingly
1257 previous_history(void)
1260 return (_move_history(H_PREV
));
1265 * returns next event in history and shifts pointer accordingly
1271 return (_move_history(H_NEXT
));
1276 * searches for first history event containing the str
1279 history_search(const char *str
, int direction
)
1285 if (history(h
, &ev
, H_CURR
) != 0)
1290 if ((strp
= strstr(ev
.str
, str
)) != NULL
)
1291 return (int) (strp
- ev
.str
);
1292 if (history(h
, &ev
, direction
< 0 ? H_NEXT
:H_PREV
) != 0)
1295 history(h
, &ev
, H_SET
, curr_num
);
1301 * searches for first history event beginning with str
1304 history_search_prefix(const char *str
, int direction
)
1308 return (history(h
, &ev
, direction
< 0? H_PREV_STR
:H_NEXT_STR
, str
));
1313 * search for event in history containing str, starting at offset
1314 * abs(pos); continue backward, if pos<0, forward otherwise
1318 history_search_pos(const char *str
,
1319 int direction
__attribute__((__unused__
)), int pos
)
1324 off
= (pos
> 0) ? pos
: -pos
;
1325 pos
= (pos
> 0) ? 1 : -1;
1327 if (history(h
, &ev
, H_CURR
) != 0)
1331 if (history_set_pos(off
) != 0 || history(h
, &ev
, H_CURR
) != 0)
1336 if (strstr(ev
.str
, str
))
1338 if (history(h
, &ev
, (pos
< 0) ? H_PREV
: H_NEXT
) != 0)
1342 /* set "current" pointer back to previous state */
1343 history(h
, &ev
, (pos
< 0) ? H_NEXT_EVENT
: H_PREV_EVENT
, curr_num
);
1349 /********************************/
1350 /* completion functions */
1353 tilde_expand(char *name
)
1355 return fn_tilde_expand(name
);
1359 filename_completion_function(const char *name
, int state
)
1361 return fn_filename_completion_function(name
, state
);
1365 * a completion generator for usernames; returns _first_ username
1366 * which starts with supplied text
1367 * text contains a partial username preceded by random character
1368 * (usually '~'); state is ignored
1369 * it's callers responsibility to free returned value
1372 username_completion_function(const char *text
, int state
)
1375 For when getpwent_r is supported:
1376 struct passwd *pwd, pwres;
1380 /* remove the above when getpwend_r is supported */
1382 if (text
[0] == '\0')
1392 For when getpwent_r is supported:
1393 while (getpwent_r(&pwres, pwbuf, sizeof(pwbuf), &pwd) == 0
1394 && pwd != NULL && text[0] == pwd->pw_name[0]
1395 && strcmp(text, pwd->pw_name) == 0);
1399 if (pwd
!= NULL
&& text
[0] == pwd
->pw_name
[0] && strcmp(text
, pwd
->pw_name
) == 0)
1403 /* remove the above when getpwend_r is supported */
1409 return (strdup(pwd
->pw_name
));
1414 * el-compatible wrapper to send TSTP on ^Z
1417 static unsigned char
1418 _el_rl_tstp(EditLine
*el
__attribute__((__unused__
)), int ch
__attribute__((__unused__
)))
1420 (void)kill(0, SIGTSTP
);
1425 * Display list of strings in columnar format on readline's output stream.
1426 * 'matches' is list of strings, 'len' is number of strings in 'matches',
1427 * 'max' is maximum length of string in 'matches'.
1430 rl_display_match_list(char **matches
, int len
, int max
)
1433 fn_display_match_list(e
, matches
, len
, max
);
1438 _rl_completion_append_character_function(const char *dummy
1439 __attribute__((__unused__
)))
1442 buf
[1] = rl_completion_append_character
;
1448 * complete word at current point
1452 rl_complete(int ignore
__attribute__((__unused__
)), int invoking_key
)
1454 if (h
== NULL
|| e
== NULL
)
1457 if (rl_inhibit_completion
) {
1459 arr
[0] = (char)invoking_key
;
1461 el_insertstr(e
, arr
);
1462 return (CC_REFRESH
);
1465 /* Just look at how many global variables modify this operation! */
1466 return fn_complete(e
,
1467 (CPFunction
*)rl_completion_entry_function
,
1468 rl_attempted_completion_function
,
1469 rl_basic_word_break_characters
, rl_special_prefixes
,
1470 _rl_completion_append_character_function
, rl_completion_query_items
,
1471 &rl_completion_type
, &rl_attempted_completion_over
,
1472 &rl_point
, &rl_end
);
1477 static unsigned char
1478 _el_rl_complete(EditLine
*el
__attribute__((__unused__
)), int ch
)
1480 return (unsigned char)rl_complete(0, ch
);
1484 * misc other functions
1488 * bind key c to readline-type function func
1491 rl_bind_key(int c
, int func(int, int))
1495 if (h
== NULL
|| e
== NULL
)
1498 if (func
== rl_insert
) {
1499 /* XXX notice there is no range checking of ``c'' */
1500 e
->el_map
.key
[c
] = ED_INSERT
;
1508 * read one key from input - handles chars pushed back
1509 * to input stream also
1514 char fooarr
[2 * sizeof(int)];
1516 if (e
== NULL
|| h
== NULL
)
1519 return (el_getc(e
, fooarr
));
1524 * reset the terminal
1528 rl_reset_terminal(const char *p
__attribute__((__unused__
)))
1531 if (h
== NULL
|| e
== NULL
)
1538 * insert character ``c'' back into input stream, ``count'' times
1541 rl_insert(int count
, int c
)
1545 if (h
== NULL
|| e
== NULL
)
1548 /* XXX - int -> char conversion can lose on multichars */
1552 for (; count
> 0; count
--)
1560 rl_newline(int count
, int c
)
1563 * Readline-4.0 appears to ignore the args.
1565 return rl_insert(1, '\n');
1569 static unsigned char
1570 rl_bind_wrapper(EditLine
*el
, unsigned char c
)
1579 /* If rl_done was set by the above call, deal with it here */
1587 rl_add_defun(const char *name
, Function
*fun
, int c
)
1590 if (c
>= sizeof(map
) / sizeof(map
[0]) || c
< 0)
1592 map
[(unsigned char)c
] = fun
;
1593 el_set(e
, EL_ADDFN
, name
, name
, rl_bind_wrapper
);
1594 vis(dest
, c
, VIS_WHITE
|VIS_NOSLASH
, 0);
1595 el_set(e
, EL_BIND
, dest
, name
);
1600 rl_callback_read_char()
1602 int count
= 0, done
= 0;
1603 const char *buf
= el_gets(e
, &count
);
1606 if (buf
== NULL
|| count
-- <= 0)
1608 if (count
== 0 && buf
[0] == CTRL('d'))
1610 if (buf
[count
] == '\n' || buf
[count
] == '\r')
1613 if (done
&& rl_linefunc
!= NULL
) {
1614 el_set(e
, EL_UNBUFFERED
, 0);
1616 if ((wbuf
= strdup(buf
)) != NULL
)
1620 (*(void (*)(const char *))rl_linefunc
)(wbuf
);
1621 el_set(e
, EL_UNBUFFERED
, 1);
1626 rl_callback_handler_install (const char *prompt
, VCPFunction
*linefunc
)
1633 rl_prompt
= prompt
? strdup(strchr(prompt
, *prompt
)) : NULL
;
1634 rl_linefunc
= linefunc
;
1635 el_set(e
, EL_UNBUFFERED
, 1);
1639 rl_callback_handler_remove(void)
1641 el_set(e
, EL_UNBUFFERED
, 0);
1654 rl_get_previous_history(int count
, int key
)
1666 rl_prep_terminal(int meta_flag
)
1668 el_set(e
, EL_PREP_TERM
, 1);
1672 rl_deprep_terminal()
1674 el_set(e
, EL_PREP_TERM
, 0);
1678 rl_read_init_file(const char *s
)
1680 return(el_source(e
, s
));
1684 rl_parse_and_bind(const char *line
)
1690 tok
= tok_init(NULL
);
1691 tok_str(tok
, line
, &argc
, &argv
);
1692 argc
= el_parse(e
, argc
, argv
);
1694 return (argc
? 1 : 0);
1698 rl_variable_bind(const char *var
, const char *value
)
1701 * The proper return value is undocument, but this is what the
1702 * readline source seems to do.
1704 return ((el_set(e
, EL_BIND
, "", var
, value
) == -1) ? 1 : 0);
1708 rl_stuff_char(int c
)
1714 el_insertstr(e
, buf
);
1718 _rl_event_read_char(EditLine
*el
, char *cp
)
1720 int n
, num_read
= 0;
1723 while (rl_event_hook
) {
1727 #if defined(FIONREAD)
1728 if (ioctl(el
->el_infd
, FIONREAD
, &n
) < 0)
1731 num_read
= read(el
->el_infd
, cp
, 1);
1734 #elif defined(F_SETFL) && defined(O_NDELAY)
1735 if ((n
= fcntl(el
->el_infd
, F_GETFL
, 0)) < 0)
1737 if (fcntl(el
->el_infd
, F_SETFL
, n
|O_NDELAY
) < 0)
1739 num_read
= read(el
->el_infd
, cp
, 1);
1740 if (fcntl(el
->el_infd
, F_SETFL
, n
))
1743 /* not non-blocking, but what you gonna do? */
1744 num_read
= read(el
->el_infd
, cp
, 1);
1748 if (num_read
< 0 && errno
== EAGAIN
)
1755 el_set(el
, EL_GETCFN
, EL_BUILTIN_GETCFN
);
1760 _rl_update_pos(void)
1762 const LineInfo
*li
= el_line(e
);
1764 rl_point
= li
->cursor
- li
->buffer
;
1765 rl_end
= li
->lastchar
- li
->buffer
;