1
/* $NetBSD: readline.c,v 1.78 2009/02/05 19:15:26 christos Exp $ */
4
* Copyright (c) 1997 The NetBSD Foundation, Inc.
7
* This code is derived from software contributed to The NetBSD Foundation
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
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.
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.
33
#if !defined(lint) && !defined(SCCSID)
34
#endif /* not lint && not SCCSID */
36
#include <sys/types.h>
54
#include "readline/readline.h"
56
#include "fcns.h" /* for EL_NUM_FCNS */
58
#include "filecomplete.h"
60
void rl_prep_terminal(int);
61
void rl_deprep_terminal(void);
63
/* for rl_complete() */
66
/* see comment at the #ifdef for sense of this */
67
/* #define GDB_411_HACK */
69
/* readline compatibility stuff - look at readline sources/documentation */
70
/* to see what these variables mean */
71
const char *rl_library_version = "EditLine wrapper";
72
static char empty[] = { '\0' };
73
static char expand_chars[] = { ' ', '\t', '\n', '=', '(', '\0' };
74
static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
75
'>', '<', '=', ';', '|', '&', '{', '(', '\0' };
76
char *rl_readline_name = empty;
77
FILE *rl_instream = NULL;
78
FILE *rl_outstream = NULL;
81
char *rl_line_buffer = NULL;
82
VCPFunction *rl_linefunc = NULL;
84
VFunction *rl_event_hook = NULL;
85
KEYMAP_ENTRY_ARRAY emacs_standard_keymap,
89
int history_base = 1; /* probably never subject to change */
90
int history_length = 0;
91
int max_input_history = 0;
92
char history_expansion_char = '!';
93
char history_subst_char = '^';
94
char *history_no_expand_chars = expand_chars;
95
Function *history_inhibit_expansion_function = NULL;
96
char *history_arg_extract(int start, int end, const char *str);
98
int rl_inhibit_completion = 0;
99
int rl_attempted_completion_over = 0;
100
char *rl_basic_word_break_characters = break_chars;
101
char *rl_completer_word_break_characters = NULL;
102
char *rl_completer_quote_characters = NULL;
103
Function *rl_completion_entry_function = NULL;
104
CPPFunction *rl_attempted_completion_function = NULL;
105
Function *rl_pre_input_hook = NULL;
106
Function *rl_startup1_hook = NULL;
107
int (*rl_getc_function)(FILE *) = NULL;
108
char *rl_terminal_name = NULL;
109
int rl_already_prompted = 0;
110
int rl_filename_completion_desired = 0;
111
int rl_ignore_completion_duplicates = 0;
112
int rl_catch_signals = 1;
113
int readline_echoing_p = 1;
114
int _rl_print_completions_horizontally = 0;
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 = (VFunction *)rl_prep_terminal;
119
VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;
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];
156
static jmp_buf topbuf;
158
/* internal functions */
159
static unsigned char _el_rl_complete(EditLine *, int);
160
static unsigned char _el_rl_tstp(EditLine *, int);
161
static char *_get_prompt(EditLine *);
162
static int _getc_function(EditLine *, char *);
163
static HIST_ENTRY *_move_history(int);
164
static int _history_expand_command(const char *, size_t, size_t,
166
static char *_rl_compat_sub(const char *, const char *,
168
static int _rl_event_read_char(EditLine *, char *);
169
static void _rl_update_pos(void);
174
_get_prompt(EditLine *el __attribute__((__unused__)))
176
rl_already_prompted = 1;
182
* generic function for moving around history
185
_move_history(int op)
188
static HIST_ENTRY rl_he;
190
if (history(h, &ev, op) != 0)
191
return (HIST_ENTRY *) NULL;
201
* read one key from user defined input function
205
_getc_function(EditLine *el, char *c)
209
i = (*rl_getc_function)(NULL);
218
* READLINE compatibility stuff
225
rl_set_prompt(const char *prompt)
229
if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0)
233
rl_prompt = strdup(prompt);
234
return rl_prompt == NULL ? -1 : 0;
238
* initialize rl compat stuff
256
rl_outstream = stdout;
259
* See if we don't really want to run the editor
261
if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)
264
e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
267
el_set(e, EL_EDITMODE, 0);
273
history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */
275
max_input_history = INT_MAX;
276
el_set(e, EL_HIST, history, h);
278
/* setup getc function if valid */
279
if (rl_getc_function)
280
el_set(e, EL_GETCFN, _getc_function);
282
/* for proper prompt printing in readline() */
283
if (rl_set_prompt("") == -1) {
288
el_set(e, EL_PROMPT, _get_prompt);
289
el_set(e, EL_SIGNAL, rl_catch_signals);
291
/* set default mode to "emacs"-style and read setting afterwards */
292
/* so this can be overriden */
293
el_set(e, EL_EDITOR, "emacs");
294
if (rl_terminal_name != NULL)
295
el_set(e, EL_TERMINAL, rl_terminal_name);
297
el_get(e, EL_TERMINAL, &rl_terminal_name);
300
* Word completion - this has to go AFTER rebinding keys
303
el_set(e, EL_ADDFN, "rl_complete",
304
"ReadLine compatible completion function",
306
el_set(e, EL_BIND, "^I", "rl_complete", NULL);
309
* Send TSTP when ^Z is pressed.
311
el_set(e, EL_ADDFN, "rl_tstp",
312
"ReadLine compatible suspend function",
314
el_set(e, EL_BIND, "^Z", "rl_tstp", NULL);
316
/* read settings from configuration file */
320
* Unfortunately, some applications really do use rl_point
321
* and rl_line_buffer directly.
324
/* a cheesy way to get rid of const cast. */
325
rl_line_buffer = memchr(li->buffer, *li->buffer, 1);
329
(*rl_startup_hook)(NULL, 0);
336
* read one line from input stream and return it, chomping
337
* trailing newline (if there is any)
340
readline(const char *p)
343
const char * volatile prompt = p;
347
static int used_event_hook;
349
if (e == NULL || h == NULL)
354
(void)setjmp(topbuf);
356
/* update prompt accordingly to what has been passed */
357
if (rl_set_prompt(prompt) == -1)
360
if (rl_pre_input_hook)
361
(*rl_pre_input_hook)(NULL, 0);
363
if (rl_event_hook && !(e->el_flags&NO_TTY)) {
364
el_set(e, EL_GETCFN, _rl_event_read_char);
368
if (!rl_event_hook && used_event_hook) {
369
el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN);
373
rl_already_prompted = 0;
375
/* get one line from input stream */
376
ret = el_gets(e, &count);
378
if (ret && count > 0) {
385
if (buf[lastidx] == '\n')
390
history(h, &ev, H_GETSIZE);
391
history_length = ev.num;
401
* is normally called before application starts to use
402
* history expansion functions
407
if (h == NULL || e == NULL)
413
* substitute ``what'' with ``with'', returning resulting string; if
414
* globally == 1, substitutes all occurrences of what, otherwise only the
418
_rl_compat_sub(const char *str, const char *what, const char *with,
423
size_t len, with_len, what_len;
426
with_len = strlen(with);
427
what_len = strlen(what);
429
/* calculate length we need for result */
432
if (*s == *what && !strncmp(s, what, what_len)) {
433
len += with_len - what_len;
440
r = result = malloc(len + 1);
445
if (*s == *what && !strncmp(s, what, what_len)) {
446
(void)strncpy(r, with, with_len);
460
static char *last_search_pat; /* last !?pat[?] search pattern */
461
static char *last_search_match; /* last !?pat[?] that matched */
464
get_history_event(const char *cmd, int *cindex, int qchar)
466
int idx, sign, sub, num, begin, ret;
473
if (cmd[idx++] != history_expansion_char)
476
/* find out which event to take */
477
if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') {
478
if (history(h, &ev, H_FIRST) != 0)
480
*cindex = cmd[idx]? (idx + 1):idx;
484
if (cmd[idx] == '-') {
489
if ('0' <= cmd[idx] && cmd[idx] <= '9') {
493
while (cmd[idx] && '0' <= cmd[idx] && cmd[idx] <= '9') {
494
num = num * 10 + cmd[idx] - '0';
498
num = history_length - num + 1;
500
if (!(rl_he = history_get(num)))
507
if (cmd[idx] == '?') {
513
if (cmd[idx] == '\n')
515
if (sub && cmd[idx] == '?')
517
if (!sub && (cmd[idx] == ':' || cmd[idx] == ' '
518
|| cmd[idx] == '\t' || cmd[idx] == qchar))
523
if (sub && cmd[idx] == '?')
525
if (sub && len == 0 && last_search_pat && *last_search_pat)
526
pat = last_search_pat;
530
if ((pat = malloc(len + 1)) == NULL)
532
(void)strncpy(pat, cmd + begin, len);
536
if (history(h, &ev, H_CURR) != 0) {
537
if (pat != last_search_pat)
544
if (pat != last_search_pat) {
546
free(last_search_pat);
547
last_search_pat = pat;
549
ret = history_search(pat, -1);
551
ret = history_search_prefix(pat, -1);
554
/* restore to end of list on failed search */
555
history(h, &ev, H_FIRST);
556
(void)fprintf(rl_outstream, "%s: Event not found\n", pat);
557
if (pat != last_search_pat)
563
if (last_search_match && last_search_match != pat)
564
free(last_search_match);
565
last_search_match = pat;
568
if (pat != last_search_pat)
571
if (history(h, &ev, H_CURR) != 0)
576
/* roll back to original position */
577
(void)history(h, &ev, H_SET, num);
583
* the real function doing history expansion - takes as argument command
584
* to do and data upon which the command should be executed
585
* does expansion the way I've understood readline documentation
587
* returns 0 if data was not modified, 1 if it was and 2 if the string
588
* should be only printed and not executed; in case of error,
589
* returns -1 and *result points to NULL
590
* it's callers responsibility to free() string returned in *result
593
_history_expand_command(const char *command, size_t offs, size_t cmdlen,
596
char *tmp, *search = NULL, *aptr;
597
const char *ptr, *cmd;
598
static char *from = NULL, *to = NULL;
599
int start, end, idx, has_mods = 0;
600
int p_on = 0, g_on = 0;
606
/* First get event specifier */
609
if (strchr(":^*$", command[offs + 1])) {
612
* "!:" is shorthand for "!!:".
613
* "!^", "!*" and "!$" are shorthand for
614
* "!!:^", "!!:*" and "!!:$" respectively.
616
str[0] = str[1] = '!';
618
ptr = get_history_event(str, &idx, 0);
619
idx = (command[offs + 1] == ':')? 1:0;
622
if (command[offs + 1] == '#') {
623
/* use command so far */
624
if ((aptr = malloc(offs + 1)) == NULL)
626
(void)strncpy(aptr, command, offs);
632
qchar = (offs > 0 && command[offs - 1] == '"')? '"':0;
633
ptr = get_history_event(command + offs, &idx, qchar);
635
has_mods = command[offs + idx] == ':';
638
if (ptr == NULL && aptr == NULL)
642
*result = strdup(aptr? aptr : ptr);
648
cmd = command + offs + idx + 1;
650
/* Now parse any word designators */
652
if (*cmd == '%') /* last word matched by ?pat? */
653
tmp = strdup(last_search_match? last_search_match:"");
654
else if (strchr("^*$-0123456789", *cmd)) {
657
start = end = 1, cmd++;
658
else if (*cmd == '$')
660
else if (*cmd == '*')
662
else if (*cmd == '-' || isdigit((unsigned char) *cmd)) {
664
while (*cmd && '0' <= *cmd && *cmd <= '9')
665
start = start * 10 + *cmd++ - '0';
668
if (isdigit((unsigned char) cmd[1])) {
671
while (*cmd && '0' <= *cmd && *cmd <= '9')
672
end = end * 10 + *cmd++ - '0';
673
} else if (cmd[1] == '$') {
680
} else if (*cmd == '*')
685
tmp = history_arg_extract(start, end, aptr? aptr:ptr);
687
(void)fprintf(rl_outstream, "%s: Bad word specifier",
688
command + offs + idx);
694
tmp = strdup(aptr? aptr:ptr);
699
if (*cmd == '\0' || ((size_t)(cmd - (command + offs)) >= cmdlen)) {
704
for (; *cmd; cmd++) {
707
else if (*cmd == 'h') { /* remove trailing path */
708
if ((aptr = strrchr(tmp, '/')) != NULL)
710
} else if (*cmd == 't') { /* remove leading path */
711
if ((aptr = strrchr(tmp, '/')) != NULL) {
712
aptr = strdup(aptr + 1);
716
} else if (*cmd == 'r') { /* remove trailing suffix */
717
if ((aptr = strrchr(tmp, '.')) != NULL)
719
} else if (*cmd == 'e') { /* remove all but suffix */
720
if ((aptr = strrchr(tmp, '.')) != NULL) {
725
} else if (*cmd == 'p') /* print only */
727
else if (*cmd == 'g')
729
else if (*cmd == 's' || *cmd == '&') {
730
char *what, *with, delim;
731
size_t len, from_len;
734
if (*cmd == '&' && (from == NULL || to == NULL))
736
else if (*cmd == 's') {
737
delim = *(++cmd), cmd++;
739
what = realloc(from, size);
746
for (; *cmd && *cmd != delim; cmd++) {
747
if (*cmd == '\\' && cmd[1] == delim)
751
nwhat = realloc(what,
767
from = strdup(search);
778
cmd++; /* shift after delim */
783
with = realloc(to, size);
790
from_len = strlen(from);
791
for (; *cmd && *cmd != delim; cmd++) {
792
if (len + from_len + 1 >= size) {
794
size += from_len + 1;
795
nwith = realloc(with, size);
805
(void)strcpy(&with[len], from);
810
&& (*(cmd + 1) == delim
811
|| *(cmd + 1) == '&'))
819
aptr = _rl_compat_sub(tmp, from, to, g_on);
833
* csh-style history expansion
836
history_expand(char *str, char **output)
842
if (h == NULL || e == NULL)
845
if (history_expansion_char == 0) {
846
*output = strdup(str);
851
if (str[0] == history_subst_char) {
852
/* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
853
*output = malloc(strlen(str) + 4 + 1);
856
(*output)[0] = (*output)[1] = history_expansion_char;
859
(void)strcpy((*output) + 4, str);
862
*output = strdup(str);
867
#define ADD_STRING(what, len, fr) \
869
if (idx + len + 1 > size) { \
870
char *nresult = realloc(result, (size += len + 1));\
871
if (nresult == NULL) { \
873
if (/*CONSTCOND*/fr) \
879
(void)strncpy(&result[idx], what, len); \
881
result[idx] = '\0'; \
887
for (i = 0; str[i];) {
888
int qchar, loop_again;
889
size_t len, start, j;
895
for (; str[j]; j++) {
896
if (str[j] == '\\' &&
897
str[j + 1] == history_expansion_char) {
898
(void)strcpy(&str[j], &str[j + 1]);
902
if (isspace((unsigned char) str[j])
906
if (str[j] == history_expansion_char
907
&& !strchr(history_no_expand_chars, str[j + 1])
908
&& (!history_inhibit_expansion_function ||
909
(*history_inhibit_expansion_function)(str,
914
if (str[j] && loop_again) {
916
qchar = (j > 0 && str[j - 1] == '"' )? '"':0;
918
if (str[j] == history_expansion_char)
924
ADD_STRING(&str[start], len, 0);
926
if (str[i] == '\0' || str[i] != history_expansion_char) {
928
ADD_STRING(&str[i], len, 0);
935
ret = _history_expand_command (str, i, (j - i), &tmp);
936
if (ret > 0 && tmp) {
938
ADD_STRING(tmp, len, 1);
947
/* ret is 2 for "print only" option */
951
/* gdb 4.11 has been shipped with readline, where */
952
/* history_expand() returned -1 when the line */
953
/* should not be executed; in readline 2.1+ */
954
/* it should return 2 in such a case */
965
* Return a string consisting of arguments of "str" from "start" to "end".
968
history_arg_extract(int start, int end, const char *str)
973
arr = history_tokenize(str);
976
if (arr && *arr == NULL) {
981
for (max = 0; arr[max]; max++)
994
if (start < 0 || end < 0 || (size_t)start > max || (size_t)end > max || start > end)
997
for (i = start, len = 0; i <= (size_t)end; i++)
998
len += strlen(arr[i]) + 1;
1000
result = malloc(len);
1004
for (i = start, len = 0; i <= (size_t)end; i++) {
1005
(void)strcpy(result + len, arr[i]);
1006
len += strlen(arr[i]);
1007
if (i < (size_t)end)
1008
result[len++] = ' ';
1012
for (i = 0; arr[i]; i++)
1020
* Parse the string into individual tokens,
1021
* similar to how shell would do it.
1024
history_tokenize(const char *str)
1026
int size = 1, idx = 0, i, start;
1028
char **result = NULL, *temp, delim = '\0';
1030
for (i = 0; str[i];) {
1031
while (isspace((unsigned char) str[i]))
1035
if (str[i] == '\\') {
1036
if (str[i+1] != '\0')
1038
} else if (str[i] == delim)
1041
(isspace((unsigned char) str[i]) ||
1042
strchr("()<>;&|$", str[i])))
1044
else if (!delim && strchr("'`\"", str[i]))
1050
if (idx + 2 >= size) {
1053
nresult = realloc(result, size * sizeof(char *));
1054
if (nresult == NULL) {
1061
temp = malloc(len + 1);
1063
for (i = 0; i < idx; i++)
1068
(void)strncpy(temp, &str[start], len);
1070
result[idx++] = temp;
1080
* limit size of history record to ``max'' events
1083
stifle_history(int max)
1087
if (h == NULL || e == NULL)
1090
if (history(h, &ev, H_SETSIZE, max) == 0)
1091
max_input_history = max;
1096
* "unlimit" size of history - set the limit to maximum allowed int value
1099
unstifle_history(void)
1104
history(h, &ev, H_SETSIZE, INT_MAX);
1105
omax = max_input_history;
1106
max_input_history = INT_MAX;
1107
return (omax); /* some value _must_ be returned */
1112
history_is_stifled(void)
1115
/* cannot return true answer */
1116
return (max_input_history != INT_MAX);
1121
* read history from a file given
1124
read_history(const char *filename)
1128
if (h == NULL || e == NULL)
1130
return (history(h, &ev, H_LOAD, filename) == -1);
1135
* write history to a file given
1138
write_history(const char *filename)
1142
if (h == NULL || e == NULL)
1144
return (history(h, &ev, H_SAVE, filename) == -1);
1149
* returns history ``num''th event
1151
* returned pointer points to static variable
1154
history_get(int num)
1156
static HIST_ENTRY she;
1160
if (h == NULL || e == NULL)
1163
/* save current position */
1164
if (history(h, &ev, H_CURR) != 0)
1168
/* start from most recent */
1169
if (history(h, &ev, H_FIRST) != 0)
1170
return (NULL); /* error */
1172
/* look backwards for event matching specified offset */
1173
if (history(h, &ev, H_NEXT_EVENT, num + 1))
1179
/* restore pointer to where it was */
1180
(void)history(h, &ev, H_SET, curr_num);
1187
* add the line to history table
1190
add_history(const char *line)
1194
if (h == NULL || e == NULL)
1197
(void)history(h, &ev, H_ENTER, line);
1198
if (history(h, &ev, H_GETSIZE) == 0)
1199
history_length = ev.num;
1201
return (!(history_length > 0)); /* return 0 if all is okay */
1206
* remove the specified entry from the history list and return it.
1209
remove_history(int num)
1214
if (h == NULL || e == NULL)
1217
if (history(h, &ev, H_DEL, num) != 0)
1220
if ((she = malloc(sizeof(*she))) == NULL)
1231
* clear the history list - delete all entries
1238
history(h, &ev, H_CLEAR);
1243
* returns offset of the current history event
1251
if (history(h, &ev, H_CURR) != 0)
1255
history(h, &ev, H_FIRST);
1257
while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
1265
* returns current history event or NULL if there is no such event
1268
current_history(void)
1271
return (_move_history(H_CURR));
1276
* returns total number of bytes history events' data are using
1279
history_total_bytes(void)
1284
if (history(h, &ev, H_CURR) != 0)
1288
history(h, &ev, H_FIRST);
1291
size += strlen(ev.str);
1292
while (history(h, &ev, H_NEXT) == 0);
1294
/* get to the same position as before */
1295
history(h, &ev, H_PREV_EVENT, curr_num);
1302
* sets the position in the history list to ``pos''
1305
history_set_pos(int pos)
1310
if (pos > history_length || pos < 0)
1313
history(h, &ev, H_CURR);
1316
if (history(h, &ev, H_SET, pos)) {
1317
history(h, &ev, H_SET, curr_num);
1325
* returns previous event in history and shifts pointer accordingly
1328
previous_history(void)
1331
return (_move_history(H_PREV));
1336
* returns next event in history and shifts pointer accordingly
1342
return (_move_history(H_NEXT));
1347
* searches for first history event containing the str
1350
history_search(const char *str, int direction)
1356
if (history(h, &ev, H_CURR) != 0)
1361
if ((strp = strstr(ev.str, str)) != NULL)
1362
return (int) (strp - ev.str);
1363
if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
1366
history(h, &ev, H_SET, curr_num);
1372
* searches for first history event beginning with str
1375
history_search_prefix(const char *str, int direction)
1379
return (history(h, &ev, direction < 0? H_PREV_STR:H_NEXT_STR, str));
1384
* search for event in history containing str, starting at offset
1385
* abs(pos); continue backward, if pos<0, forward otherwise
1389
history_search_pos(const char *str,
1390
int direction __attribute__((__unused__)), int pos)
1395
off = (pos > 0) ? pos : -pos;
1396
pos = (pos > 0) ? 1 : -1;
1398
if (history(h, &ev, H_CURR) != 0)
1402
if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
1407
if (strstr(ev.str, str))
1409
if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
1413
/* set "current" pointer back to previous state */
1414
history(h, &ev, (pos < 0) ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
1420
/********************************/
1421
/* completion functions */
1424
tilde_expand(char *name)
1426
return fn_tilde_expand(name);
1430
filename_completion_function(const char *name, int state)
1432
return fn_filename_completion_function(name, state);
1436
* a completion generator for usernames; returns _first_ username
1437
* which starts with supplied text
1438
* text contains a partial username preceded by random character
1439
* (usually '~'); state is ignored
1440
* it's callers responsibility to free returned value
1443
username_completion_function(const char *text, int state)
1447
if (text[0] == '\0')
1456
/* XXXMYSQL: just use non-_r functions for now */
1457
while ((pwd = getpwent()) && text[0] == pwd->pw_name[0]
1458
&& strcmp(text, pwd->pw_name) == 0);
1464
return (strdup(pwd->pw_name));
1469
* el-compatible wrapper to send TSTP on ^Z
1472
static unsigned char
1473
_el_rl_tstp(EditLine *el __attribute__((__unused__)), int ch __attribute__((__unused__)))
1475
(void)kill(0, SIGTSTP);
1480
* Display list of strings in columnar format on readline's output stream.
1481
* 'matches' is list of strings, 'len' is number of strings in 'matches',
1482
* 'max' is maximum length of string in 'matches'.
1485
rl_display_match_list(char **matches, int len, int max)
1488
fn_display_match_list(e, matches, len, max);
1493
_rl_completion_append_character_function(const char *dummy
1494
__attribute__((__unused__)))
1497
buf[0] = rl_completion_append_character;
1504
* complete word at current point
1508
rl_complete(int ignore __attribute__((__unused__)), int invoking_key)
1510
if (h == NULL || e == NULL)
1513
if (rl_inhibit_completion) {
1515
arr[0] = (char)invoking_key;
1517
el_insertstr(e, arr);
1518
return (CC_REFRESH);
1521
/* Just look at how many global variables modify this operation! */
1522
return fn_complete(e,
1523
(CPFunction *)rl_completion_entry_function,
1524
rl_attempted_completion_function,
1525
rl_basic_word_break_characters, rl_special_prefixes,
1526
_rl_completion_append_character_function, rl_completion_query_items,
1527
&rl_completion_type, &rl_attempted_completion_over,
1528
&rl_point, &rl_end);
1533
static unsigned char
1534
_el_rl_complete(EditLine *el __attribute__((__unused__)), int ch)
1536
return (unsigned char)rl_complete(0, ch);
1540
* misc other functions
1544
* bind key c to readline-type function func
1547
rl_bind_key(int c, int func(int, int))
1551
if (h == NULL || e == NULL)
1554
if (func == rl_insert) {
1555
/* XXX notice there is no range checking of ``c'' */
1556
e->el_map.key[c] = ED_INSERT;
1564
* read one key from input - handles chars pushed back
1565
* to input stream also
1570
char fooarr[2 * sizeof(int)];
1572
if (e == NULL || h == NULL)
1575
return (el_getc(e, fooarr));
1580
* reset the terminal
1584
rl_reset_terminal(const char *p __attribute__((__unused__)))
1587
if (h == NULL || e == NULL)
1594
* insert character ``c'' back into input stream, ``count'' times
1597
rl_insert(int count, int c)
1601
if (h == NULL || e == NULL)
1604
/* XXX - int -> char conversion can lose on multichars */
1608
for (; count > 0; count--)
1616
rl_newline(int count, int c)
1619
* Readline-4.0 appears to ignore the args.
1621
return rl_insert(1, '\n');
1625
static unsigned char
1626
rl_bind_wrapper(EditLine *el, unsigned char c)
1635
/* If rl_done was set by the above call, deal with it here */
1643
rl_add_defun(const char *name, Function *fun, int c)
1646
if ((size_t)c >= sizeof(map) / sizeof(map[0]) || c < 0)
1648
map[(unsigned char)c] = fun;
1649
el_set(e, EL_ADDFN, name, name, rl_bind_wrapper);
1650
vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0);
1651
el_set(e, EL_BIND, dest, name);
1656
rl_callback_read_char()
1658
int count = 0, done = 0;
1659
const char *buf = el_gets(e, &count);
1662
if (buf == NULL || count-- <= 0)
1664
if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF])
1666
if (buf[count] == '\n' || buf[count] == '\r')
1669
if (done && rl_linefunc != NULL) {
1670
el_set(e, EL_UNBUFFERED, 0);
1672
if ((wbuf = strdup(buf)) != NULL)
1676
(*(void (*)(const char *))rl_linefunc)(wbuf);
1677
el_set(e, EL_UNBUFFERED, 1);
1682
rl_callback_handler_install(const char *prompt, VCPFunction *linefunc)
1687
(void)rl_set_prompt(prompt);
1688
rl_linefunc = linefunc;
1689
el_set(e, EL_UNBUFFERED, 1);
1693
rl_callback_handler_remove(void)
1695
el_set(e, EL_UNBUFFERED, 0);
1703
a[0] = e->el_tty.t_c[TS_IO][C_REPRINT];
1709
rl_get_previous_history(int count, int key)
1721
rl_prep_terminal(int meta_flag)
1723
el_set(e, EL_PREP_TERM, 1);
1727
rl_deprep_terminal(void)
1729
el_set(e, EL_PREP_TERM, 0);
1733
rl_read_init_file(const char *s)
1735
return(el_source(e, s));
1739
rl_parse_and_bind(const char *line)
1745
tok = tok_init(NULL);
1746
tok_str(tok, line, &argc, &argv);
1747
argc = el_parse(e, argc, argv);
1749
return (argc ? 1 : 0);
1753
rl_variable_bind(const char *var, const char *value)
1756
* The proper return value is undocument, but this is what the
1757
* readline source seems to do.
1759
return ((el_set(e, EL_BIND, "", var, value) == -1) ? 1 : 0);
1763
rl_stuff_char(int c)
1769
el_insertstr(e, buf);
1773
_rl_event_read_char(EditLine *el, char *cp)
1775
int n, num_read = 0;
1778
while (rl_event_hook) {
1782
#if defined(FIONREAD)
1783
if (ioctl(el->el_infd, FIONREAD, &n) < 0)
1786
num_read = read(el->el_infd, cp, 1);
1789
#elif defined(F_SETFL) && defined(O_NDELAY)
1790
if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0)
1792
if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0)
1794
num_read = read(el->el_infd, cp, 1);
1795
if (fcntl(el->el_infd, F_SETFL, n))
1798
/* not non-blocking, but what you gonna do? */
1799
num_read = read(el->el_infd, cp, 1);
1803
if (num_read < 0 && errno == EAGAIN)
1810
el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN);
1815
_rl_update_pos(void)
1817
const LineInfo *li = el_line(e);
1819
rl_point = li->cursor - li->buffer;
1820
rl_end = li->lastchar - li->buffer;
1824
rl_get_screen_size(int *rows, int *cols)
1827
el_get(e, EL_GETTC, "li", rows);
1829
el_get(e, EL_GETTC, "co", cols);
1833
rl_set_screen_size(int rows, int cols)
1836
(void)snprintf(buf, sizeof(buf), "%d", rows);
1837
el_set(e, EL_SETTC, "li", buf);
1838
(void)snprintf(buf, sizeof(buf), "%d", cols);
1839
el_set(e, EL_SETTC, "co", buf);
1843
rl_completion_matches(const char *str, rl_compentry_func_t *fun)
1845
size_t len, max, i, j, min;
1846
char **list, *match, *a, *b;
1850
if ((list = malloc(max * sizeof(*list))) == NULL)
1853
while ((match = (*fun)(str, (int)(len - 1))) != NULL) {
1857
if ((nl = realloc(list, max * sizeof(*nl))) == NULL)
1861
list[len++] = match;
1867
if ((list[0] = strdup(list[1])) == NULL)
1871
qsort(&list[1], len - 1, sizeof(*list),
1872
(int (*)(const void *, const void *)) strcmp);
1874
for (i = 1, a = list[i]; i < len - 1; i++, a = b) {
1876
for (j = 0; a[j] && a[j] == b[j]; j++)
1881
if (min == 0 && *str) {
1882
if ((list[0] = strdup(str)) == NULL)
1885
if ((list[0] = malloc(min + 1)) == NULL)
1887
(void)memcpy(list[0], list[1], min);
1888
list[0][min] = '\0';
1898
rl_filename_completion_function (const char *text, int state)
1900
return fn_filename_completion_function(text, state);
1904
rl_forced_update_display(void)
1906
el_set(e, EL_REFRESH);
1910
_rl_abort_internal(void)
1918
_rl_qsort_string_compare(char **s1, char **s2)
1920
return strcoll(*s1, *s2);
1925
rl_kill_text(int from, int to)
1931
rl_make_bare_keymap(void)
1944
rl_set_keymap(Keymap k)
1950
rl_generic_bind(int type, const char * keyseq, const char * data, Keymap k)
1957
rl_bind_key_in_map(int key, Function *fun, Keymap k)