33 #if !defined(lint) && !defined(SCCSID)
36 #include <sys/types.h>
54 #include "editline/readline.h"
58 #include "filecomplete.h"
60 void rl_prep_terminal(
int);
61 void rl_deprep_terminal(
void);
71 const char *rl_library_version =
"EditLine wrapper";
72 int rl_readline_version = RL_READLINE_VERSION;
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;
82 char *rl_line_buffer = NULL;
83 VCPFunction *rl_linefunc = NULL;
85 VFunction *rl_event_hook = NULL;
86 KEYMAP_ENTRY_ARRAY emacs_standard_keymap,
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);
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 KEYMAP_ENTRY_ARRAY emacs_meta_keymap;
126 char *rl_prompt = NULL;
132 int rl_completion_type = 0;
138 int rl_completion_query_items = 100;
145 char *rl_special_prefixes = NULL;
151 int rl_completion_append_character =
' ';
155 static History *h = NULL;
157 static Function *map[256];
158 static jmp_buf topbuf;
161 static unsigned char _el_rl_complete(
EditLine *,
int);
162 static unsigned char _el_rl_tstp(
EditLine *,
int);
163 static char *_get_prompt(
EditLine *);
164 static int _getc_function(
EditLine *,
char *);
166 static int _history_expand_command(
const char *,
size_t,
size_t,
168 static char *_rl_compat_sub(
const char *,
const char *,
170 static int _rl_event_read_char(
EditLine *,
char *);
171 static void _rl_update_pos(
void);
176 _get_prompt(
EditLine *el __attribute__((__unused__)))
178 rl_already_prompted = 1;
187 _move_history(
int op)
192 if (history(h, &ev, op) != 0)
207 _getc_function(
EditLine *el __attribute__((__unused__)),
char *c)
211 i = (*rl_getc_function)(NULL);
226 *ap = memchr(li->buffer, *li->buffer, (
size_t)1);
230 _default_history_file(
void)
233 static char path[PATH_MAX];
237 if ((p = getpwuid(getuid())) == NULL)
239 (void)snprintf(path,
sizeof(path),
"%s/.history", p->pw_dir);
251 rl_set_prompt(
const char *prompt)
257 if (rl_prompt != NULL && strcmp(rl_prompt, prompt) == 0)
261 rl_prompt = strdup(prompt);
262 if (rl_prompt == NULL)
265 while ((p = strchr(rl_prompt, RL_PROMPT_END_IGNORE)) != NULL)
266 *p = RL_PROMPT_START_IGNORE;
289 rl_outstream = stdout;
294 if (tcgetattr(fileno(rl_instream), &t) != -1 && (t.c_lflag & ECHO) == 0)
297 e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
300 el_set(e, EL_EDITMODE, 0);
306 history(h, &ev, H_SETSIZE, INT_MAX);
308 max_input_history = INT_MAX;
309 el_set(e, EL_HIST, history, h);
312 el_set(e, EL_RESIZE, _resize_fun, &rl_line_buffer);
315 if (rl_getc_function)
316 el_set(e, EL_GETCFN, _getc_function);
319 if (rl_set_prompt(
"") == -1) {
324 el_set(e, EL_PROMPT, _get_prompt, RL_PROMPT_START_IGNORE);
325 el_set(e, EL_SIGNAL, rl_catch_signals);
329 el_set(e, EL_EDITOR,
"emacs");
330 if (rl_terminal_name != NULL)
331 el_set(e, EL_TERMINAL, rl_terminal_name);
333 el_get(e, EL_TERMINAL, &rl_terminal_name);
339 el_set(e, EL_ADDFN,
"rl_complete",
340 "ReadLine compatible completion function",
342 el_set(e, EL_BIND,
"^I",
"rl_complete", NULL);
347 el_set(e, EL_ADDFN,
"rl_tstp",
348 "ReadLine compatible suspend function",
350 el_set(e, EL_BIND,
"^Z",
"rl_tstp", NULL);
355 el_set(e, EL_BIND,
"^R",
"em-inc-search-prev", NULL);
360 el_set(e, EL_BIND,
"\\e[1~",
"ed-move-to-beg", NULL);
361 el_set(e, EL_BIND,
"\\e[4~",
"ed-move-to-end", NULL);
362 el_set(e, EL_BIND,
"\\e[7~",
"ed-move-to-beg", NULL);
363 el_set(e, EL_BIND,
"\\e[8~",
"ed-move-to-end", NULL);
364 el_set(e, EL_BIND,
"\\e[H",
"ed-move-to-beg", NULL);
365 el_set(e, EL_BIND,
"\\e[F",
"ed-move-to-end", NULL);
370 el_set(e, EL_BIND,
"\\e[3~",
"ed-delete-next-char", NULL);
371 el_set(e, EL_BIND,
"\\e[2~",
"ed-quoted-insert", NULL);
376 el_set(e, EL_BIND,
"\\e[1;5C",
"em-next-word", NULL);
377 el_set(e, EL_BIND,
"\\e[1;5D",
"ed-prev-word", NULL);
378 el_set(e, EL_BIND,
"\\e[5C",
"em-next-word", NULL);
379 el_set(e, EL_BIND,
"\\e[5D",
"ed-prev-word", NULL);
380 el_set(e, EL_BIND,
"\\e\\e[C",
"em-next-word", NULL);
381 el_set(e, EL_BIND,
"\\e\\e[D",
"ed-prev-word", NULL);
390 _resize_fun(e, &rl_line_buffer);
394 (*rl_startup_hook)(NULL, 0);
405 readline(
const char *p)
408 const char *
volatile prompt = p;
412 static int used_event_hook;
414 if (e == NULL || h == NULL)
419 (void)setjmp(topbuf);
422 if (rl_set_prompt(prompt) == -1)
425 if (rl_pre_input_hook)
426 (*rl_pre_input_hook)(NULL, 0);
428 if (rl_event_hook && !(e->el_flags&NO_TTY)) {
429 el_set(e, EL_GETCFN, _rl_event_read_char);
433 if (!rl_event_hook && used_event_hook) {
434 el_set(e, EL_GETCFN, EL_BUILTIN_GETCFN);
438 rl_already_prompted = 0;
441 ret = el_gets(e, &count);
443 if (ret && count > 0) {
450 if (buf[lastidx] ==
'\n')
455 history(h, &ev, H_GETSIZE);
456 history_length = ev.num;
472 if (h == NULL || e == NULL)
483 _rl_compat_sub(
const char *str,
const char *what,
const char *with,
488 size_t len, with_len, what_len;
491 with_len = strlen(with);
492 what_len = strlen(what);
497 if (*s == *what && !strncmp(s, what, what_len)) {
498 len += with_len - what_len;
505 r = result = el_malloc((len + 1) *
sizeof(*r));
510 if (*s == *what && !strncmp(s, what, what_len)) {
511 (void)strncpy(r, with, with_len);
525 static char *last_search_pat;
526 static char *last_search_match;
529 get_history_event(
const char *cmd,
int *cindex,
int qchar)
531 int idx, sign, sub, num, begin,
ret;
538 if (cmd[idx++] != history_expansion_char)
542 if (cmd[idx] == history_expansion_char || cmd[idx] ==
'\0') {
543 if (history(h, &ev, H_FIRST) != 0)
545 *cindex = cmd[idx]? (idx + 1):idx;
549 if (cmd[idx] ==
'-') {
554 if (
'0' <= cmd[idx] && cmd[idx] <=
'9') {
558 while (cmd[idx] &&
'0' <= cmd[idx] && cmd[idx] <=
'9') {
559 num = num * 10 + cmd[idx] -
'0';
563 num = history_length - num + 1;
565 if (!(rl_he = history_get(num)))
572 if (cmd[idx] ==
'?') {
578 if (cmd[idx] ==
'\n')
580 if (sub && cmd[idx] ==
'?')
582 if (!sub && (cmd[idx] ==
':' || cmd[idx] ==
' '
583 || cmd[idx] ==
'\t' || cmd[idx] == qchar))
587 len = (size_t)idx - (
size_t)begin;
588 if (sub && cmd[idx] ==
'?')
590 if (sub && len == 0 && last_search_pat && *last_search_pat)
591 pat = last_search_pat;
595 if ((pat = el_malloc((len + 1) *
sizeof(*pat))) == NULL)
597 (void)strncpy(pat, cmd + begin, len);
601 if (history(h, &ev, H_CURR) != 0) {
602 if (pat != last_search_pat)
609 if (pat != last_search_pat) {
611 el_free(last_search_pat);
612 last_search_pat = pat;
614 ret = history_search(pat, -1);
616 ret = history_search_prefix(pat, -1);
620 history(h, &ev, H_FIRST);
621 (void)fprintf(rl_outstream,
"%s: Event not found\n", pat);
622 if (pat != last_search_pat)
628 if (last_search_match && last_search_match != pat)
629 el_free(last_search_match);
630 last_search_match = pat;
633 if (pat != last_search_pat)
636 if (history(h, &ev, H_CURR) != 0)
642 (void)history(h, &ev, H_SET, num);
658 _history_expand_command(
const char *command,
size_t offs,
size_t cmdlen,
661 char *tmp, *search = NULL, *aptr;
662 const char *ptr, *cmd;
663 static char *from = NULL, *
to = NULL;
664 int start, end, idx, has_mods = 0;
665 int p_on = 0, g_on = 0;
674 if (strchr(
":^*$", command[offs + 1])) {
681 str[0] = str[1] =
'!';
683 ptr = get_history_event(str, &idx, 0);
684 idx = (command[offs + 1] ==
':')? 1:0;
687 if (command[offs + 1] ==
'#') {
689 if ((aptr = el_malloc((offs + 1) *
sizeof(*aptr)))
692 (void)strncpy(aptr, command, offs);
698 qchar = (offs > 0 && command[offs - 1] ==
'"')?
'"':0;
699 ptr = get_history_event(command + offs, &idx, qchar);
701 has_mods = command[offs + (size_t)idx] ==
':';
704 if (ptr == NULL && aptr == NULL)
708 *result = strdup(aptr ? aptr : ptr);
716 cmd = command + offs + idx + 1;
721 tmp = strdup(last_search_match? last_search_match:
"");
722 else if (strchr(
"^*$-0123456789", *cmd)) {
725 start = end = 1, cmd++;
726 else if (*cmd ==
'$')
728 else if (*cmd ==
'*')
730 else if (*cmd ==
'-' || isdigit((
unsigned char) *cmd)) {
732 while (*cmd &&
'0' <= *cmd && *cmd <=
'9')
733 start = start * 10 + *cmd++ -
'0';
736 if (isdigit((
unsigned char) cmd[1])) {
739 while (*cmd &&
'0' <= *cmd && *cmd <=
'9')
740 end = end * 10 + *cmd++ -
'0';
741 }
else if (cmd[1] ==
'$') {
748 }
else if (*cmd ==
'*')
753 tmp = history_arg_extract(start, end, aptr? aptr:ptr);
755 (void)fprintf(rl_outstream,
"%s: Bad word specifier",
756 command + offs + idx);
762 tmp = strdup(aptr? aptr:ptr);
767 if (*cmd ==
'\0' || ((
size_t)(cmd - (command + offs)) >= cmdlen)) {
772 for (; *cmd; cmd++) {
775 else if (*cmd ==
'h') {
776 if ((aptr = strrchr(tmp,
'/')) != NULL)
778 }
else if (*cmd ==
't') {
779 if ((aptr = strrchr(tmp,
'/')) != NULL) {
780 aptr = strdup(aptr + 1);
784 }
else if (*cmd ==
'r') {
785 if ((aptr = strrchr(tmp,
'.')) != NULL)
787 }
else if (*cmd ==
'e') {
788 if ((aptr = strrchr(tmp,
'.')) != NULL) {
793 }
else if (*cmd ==
'p')
795 else if (*cmd ==
'g')
797 else if (*cmd ==
's' || *cmd ==
'&') {
798 char *what, *with, delim;
799 size_t len, from_len;
802 if (*cmd ==
'&' && (from == NULL || to == NULL))
804 else if (*cmd ==
's') {
805 delim = *(++cmd), cmd++;
807 what = el_realloc(from, size *
sizeof(*what));
814 for (; *cmd && *cmd != delim; cmd++) {
815 if (*cmd ==
'\\' && cmd[1] == delim)
819 nwhat = el_realloc(what,
836 from = strdup(search);
852 with = el_realloc(to, size *
sizeof(*with));
859 from_len = strlen(from);
860 for (; *cmd && *cmd != delim; cmd++) {
861 if (len + from_len + 1 >= size) {
863 size += from_len + 1;
864 nwith = el_realloc(with,
865 size *
sizeof(*nwith));
875 (void)strcpy(&with[len], from);
880 && (*(cmd + 1) == delim
881 || *(cmd + 1) ==
'&'))
889 aptr = _rl_compat_sub(tmp, from, to, g_on);
906 history_expand(
char *str,
char **output)
912 if (h == NULL || e == NULL)
915 if (history_expansion_char == 0) {
916 *output = strdup(str);
921 if (str[0] == history_subst_char) {
923 *output = el_malloc((strlen(str) + 4 + 1) *
sizeof(*output));
926 (*output)[0] = (*output)[1] = history_expansion_char;
929 (void)strcpy((*output) + 4, str);
932 *output = strdup(str);
937 #define ADD_STRING(what, len, fr) \
939 if (idx + len + 1 > size) { \
940 char *nresult = el_realloc(result, \
941 (size += len + 1) * sizeof(*nresult)); \
942 if (nresult == NULL) { \
950 (void)strncpy(&result[idx], what, len); \
952 result[idx] = '\0'; \
958 for (i = 0; str[
i];) {
959 int qchar, loop_again;
960 size_t len, start, j;
966 for (; str[j]; j++) {
967 if (str[j] ==
'\\' &&
968 str[j + 1] == history_expansion_char) {
969 (void)strcpy(&str[j], &str[j + 1]);
973 if (isspace((
unsigned char) str[j])
977 if (str[j] == history_expansion_char
978 && !strchr(history_no_expand_chars, str[j + 1])
979 && (!history_inhibit_expansion_function ||
980 (*history_inhibit_expansion_function)(str,
985 if (str[j] && loop_again) {
987 qchar = (j > 0 && str[j - 1] ==
'"' )?
'"':0;
989 if (str[j] == history_expansion_char)
995 ADD_STRING(&str[start], len, 0);
997 if (str[i] ==
'\0' || str[i] != history_expansion_char) {
999 ADD_STRING(&str[i], len, 0);
1006 ret = _history_expand_command (str, i, (j - i), &tmp);
1007 if (ret > 0 && tmp) {
1009 ADD_STRING(tmp, len, 1);
1020 add_history(result);
1039 history_arg_extract(
int start,
int end,
const char *str)
1042 char **arr, *result = NULL;
1044 arr = history_tokenize(str);
1047 if (arr && *arr == NULL)
1050 for (max = 0; arr[max]; max++)
1059 end = (int)max + end + 1;
1063 if (start < 0 || end < 0 || (
size_t)start > max ||
1064 (size_t)end > max || start > end)
1067 for (i = (
size_t)start, len = 0; i <= (size_t)end; i++)
1068 len += strlen(arr[i]) + 1;
1070 result = el_malloc(len *
sizeof(*result));
1074 for (i = (
size_t)start, len = 0; i <= (size_t)end; i++) {
1075 (void)strcpy(result + len, arr[i]);
1076 len += strlen(arr[i]);
1077 if (i < (
size_t)end)
1078 result[len++] =
' ';
1083 for (i = 0; arr[
i]; i++)
1095 history_tokenize(
const char *str)
1097 int size = 1, idx = 0,
i, start;
1099 char **result = NULL, *
temp, delim =
'\0';
1101 for (i = 0; str[
i];) {
1102 while (isspace((
unsigned char) str[i]))
1106 if (str[i] ==
'\\') {
1107 if (str[i+1] !=
'\0')
1109 }
else if (str[i] == delim)
1112 (isspace((
unsigned char) str[i]) ||
1113 strchr(
"()<>;&|$", str[i])))
1115 else if (!delim && strchr(
"'`\"", str[i]))
1121 if (idx + 2 >= size) {
1124 nresult = el_realloc(result, (
size_t)size *
sizeof(*nresult));
1125 if (nresult == NULL) {
1131 len = (size_t)i - (
size_t)start;
1132 temp = el_malloc((
size_t)(len + 1) *
sizeof(*temp));
1134 for (i = 0; i < idx; i++)
1139 (void)strncpy(temp, &str[start], len);
1141 result[idx++] =
temp;
1154 stifle_history(
int max)
1158 if (h == NULL || e == NULL)
1161 if (history(h, &ev, H_SETSIZE, max) == 0)
1162 max_input_history = max;
1170 unstifle_history(
void)
1175 history(h, &ev, H_SETSIZE, INT_MAX);
1176 omax = max_input_history;
1177 max_input_history = INT_MAX;
1183 history_is_stifled(
void)
1187 return max_input_history != INT_MAX;
1190 static const char _history_tmp_template[] =
"/tmp/.historyXXXXXX";
1193 history_truncate_file (
const char *filename,
int nlines)
1197 char template[
sizeof(_history_tmp_template)];
1205 if (filename == NULL && (filename = _default_history_file()) == NULL)
1207 if ((fp = fopen(filename,
"r+")) == NULL)
1209 strcpy(
template, _history_tmp_template);
1210 if ((fd = mkstemp(
template)) == -1) {
1215 if ((tp = fdopen(fd,
"r+")) == NULL) {
1222 if (fread(buf,
sizeof(buf), (
size_t)1, fp) != 1) {
1227 if (fseeko(fp, (off_t)
sizeof(buf) * count, SEEK_SET) ==
1232 left = (ssize_t)fread(buf, (
size_t)1,
sizeof(
buf), fp);
1240 }
else if (fwrite(buf, (
size_t)left, (size_t)1, tp)
1248 if (fwrite(buf,
sizeof(buf), (
size_t)1, tp) != 1) {
1256 cp = buf + left - 1;
1260 while (--cp >= buf) {
1262 if (--nlines == 0) {
1263 if (++cp >= buf +
sizeof(buf)) {
1271 if (nlines <= 0 || count == 0)
1274 if (fseeko(tp, (off_t)
sizeof(buf) * count, SEEK_SET) < 0) {
1278 if (fread(buf,
sizeof(buf), (
size_t)1, tp) != 1) {
1286 cp = buf +
sizeof(
buf);
1289 if (ret || nlines > 0)
1292 if (fseeko(fp, (off_t)0, SEEK_SET) == (off_t)-1) {
1297 if (fseeko(tp, (off_t)
sizeof(
buf) * count + (cp - buf), SEEK_SET) ==
1304 if ((left = (ssize_t)fread(buf, (
size_t)1,
sizeof(buf), tp)) == 0) {
1309 if (fwrite(buf, (
size_t)left, (size_t)1, fp) != 1) {
1315 if((off = ftello(fp)) > 0) {
1316 if (ftruncate(fileno(fp), off) == -1)
1334 read_history(
const char *filename)
1338 if (h == NULL || e == NULL)
1340 if (filename == NULL && (filename = _default_history_file()) == NULL)
1342 return history(h, &ev, H_LOAD, filename) == -1 ?
1343 (errno ? errno : EINVAL) : 0;
1351 write_history(
const char *filename)
1355 if (h == NULL || e == NULL)
1357 if (filename == NULL && (filename = _default_history_file()) == NULL)
1359 return history(h, &ev, H_SAVE, filename) == -1 ?
1360 (errno ? errno : EINVAL) : 0;
1370 history_get(
int num)
1376 if (h == NULL || e == NULL)
1380 if (history(h, &ev, H_CURR) != 0)
1385 if (history(h, &ev, H_LAST) != 0)
1389 if (history(h, &ev, H_NEXT_EVDATA, num, &she.data))
1395 (void)history(h, &ev, H_SET, curr_num);
1405 add_history(
const char *line)
1409 if (h == NULL || e == NULL)
1412 (void)history(h, &ev, H_ENTER, line);
1413 if (history(h, &ev, H_GETSIZE) == 0)
1414 history_length = ev.num;
1416 return !(history_length > 0);
1424 remove_history(
int num)
1429 if (h == NULL || e == NULL)
1432 if ((he = el_malloc(
sizeof(*he))) == NULL)
1435 if (history(h, &ev, H_DELDATA, num, &he->data) != 0) {
1441 if (history(h, &ev, H_GETSIZE) == 0)
1442 history_length = ev.num;
1452 replace_history_entry(
int num,
const char *line, histdata_t data)
1458 if (h == NULL || e == NULL)
1462 if (history(h, &ev, H_CURR) != 0)
1467 if (history(h, &ev, H_LAST) != 0)
1470 if ((he = el_malloc(
sizeof(*he))) == NULL)
1474 if (history(h, &ev, H_NEXT_EVDATA, num, &he->data))
1477 he->line = strdup(ev.str);
1478 if (he->line == NULL)
1481 if (history(h, &ev, H_REPLACE, line, data))
1485 if (history(h, &ev, H_SET, curr_num))
1502 (void)history(h, &ev, H_CLEAR);
1516 if (history(h, &ev, H_CURR) != 0)
1520 (void)history(h, &ev, H_FIRST);
1522 while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
1533 current_history(
void)
1536 return _move_history(H_CURR);
1544 history_total_bytes(
void)
1550 if (history(h, &ev, H_CURR) != 0)
1554 (void)history(h, &ev, H_FIRST);
1557 size += strlen(ev.str) *
sizeof(*ev.str);
1558 while (history(h, &ev, H_NEXT) == 0);
1561 history(h, &ev, H_PREV_EVENT, curr_num);
1571 history_set_pos(
int pos)
1576 if (pos >= history_length || pos < 0)
1579 (void)history(h, &ev, H_CURR);
1586 if (history(h, &ev, H_DELDATA, pos, (
void **)-1)) {
1587 (void)history(h, &ev, H_SET, curr_num);
1598 previous_history(
void)
1601 return _move_history(H_PREV);
1612 return _move_history(H_NEXT);
1620 history_search(
const char *str,
int direction)
1626 if (history(h, &ev, H_CURR) != 0)
1631 if ((strp = strstr(ev.str, str)) != NULL)
1632 return (
int)(strp - ev.str);
1633 if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
1636 (void)history(h, &ev, H_SET, curr_num);
1645 history_search_prefix(
const char *str,
int direction)
1649 return (history(h, &ev, direction < 0 ?
1650 H_PREV_STR : H_NEXT_STR, str));
1660 history_search_pos(
const char *str,
1661 int direction __attribute__((__unused__)),
int pos)
1666 off = (pos > 0) ? pos : -pos;
1667 pos = (pos > 0) ? 1 : -1;
1669 if (history(h, &ev, H_CURR) != 0)
1673 if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
1677 if (strstr(ev.str, str))
1679 if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
1684 (void)history(h, &ev,
1685 pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
1695 tilde_expand(
char *
name)
1697 return fn_tilde_expand(name);
1701 filename_completion_function(
const char *
name,
int state)
1703 return fn_filename_completion_function(name, state);
1714 username_completion_function(
const char *text,
int state)
1716 #if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
1717 struct passwd pwres;
1720 struct passwd *pass = NULL;
1722 if (text[0] ==
'\0')
1732 #
if defined(HAVE_GETPW_R_POSIX) || defined(HAVE_GETPW_R_DRAFT)
1733 getpwent_r(&pwres, pwbuf,
sizeof(pwbuf), &pass) == 0 && pass != NULL
1735 (pass = getpwent()) != NULL
1737 && text[0] == pass->pw_name[0]
1738 && strcmp(text, pass->pw_name) == 0)
1745 return strdup(pass->pw_name);
1753 static unsigned char
1754 _el_rl_tstp(
EditLine *el __attribute__((__unused__)),
int ch __attribute__((__unused__)))
1756 (void)
kill(0, SIGTSTP);
1766 rl_display_match_list(
char **matches,
int len,
int max)
1769 fn_display_match_list(e, matches, (
size_t)len, (
size_t)max);
1774 _rl_completion_append_character_function(
const char *dummy
1775 __attribute__((__unused__)))
1778 buf[0] = (char)rl_completion_append_character;
1789 rl_complete(
int ignore __attribute__((__unused__)),
int invoking_key)
1792 static ct_buffer_t wbreak_conv, sprefix_conv;
1795 if (h == NULL || e == NULL)
1798 if (rl_inhibit_completion) {
1800 arr[0] = (char)invoking_key;
1802 el_insertstr(e, arr);
1807 return fn_complete(e,
1808 (CPFunction *)rl_completion_entry_function,
1809 rl_attempted_completion_function,
1810 ct_decode_string(rl_basic_word_break_characters, &wbreak_conv),
1811 ct_decode_string(rl_special_prefixes, &sprefix_conv),
1812 _rl_completion_append_character_function,
1813 (
size_t)rl_completion_query_items,
1814 &rl_completion_type, &rl_attempted_completion_over,
1815 &rl_point, &rl_end);
1822 static unsigned char
1823 _el_rl_complete(
EditLine *el __attribute__((__unused__)),
int ch)
1825 return (
unsigned char)rl_complete(0, ch);
1836 rl_bind_key(
int c, rl_command_func_t *func)
1840 if (h == NULL || e == NULL)
1843 if (func == rl_insert) {
1845 e->el_map.key[c] = ED_INSERT;
1859 char fooarr[2 *
sizeof(int)];
1861 if (e == NULL || h == NULL)
1864 return el_getc(e, fooarr);
1873 rl_reset_terminal(
const char *p __attribute__((__unused__)))
1876 if (h == NULL || e == NULL)
1886 rl_insert(
int count,
int c)
1890 if (h == NULL || e == NULL)
1897 for (; count > 0; count--)
1904 rl_insert_text(
const char *text)
1906 if (!text || *text == 0)
1909 if (h == NULL || e == NULL)
1912 if (el_insertstr(e, text) < 0)
1914 return (
int)strlen(text);
1919 rl_newline(
int count __attribute__((__unused__)),
1920 int c __attribute__((__unused__)))
1925 return rl_insert(1,
'\n');
1929 static unsigned char
1930 rl_bind_wrapper(
EditLine *el __attribute__((__unused__)),
unsigned char c)
1947 rl_add_defun(
const char *
name, Function *fun,
int c)
1950 if ((
size_t)c >=
sizeof(map) /
sizeof(map[0]) || c < 0)
1952 map[(
unsigned char)c] = fun;
1953 el_set(e, EL_ADDFN, name, name, rl_bind_wrapper);
1954 vis(dest, c, VIS_WHITE|VIS_NOSLASH, 0);
1955 el_set(e, EL_BIND, dest, name);
1960 rl_callback_read_char()
1962 int count = 0, done = 0;
1963 const char *buf = el_gets(e, &count);
1966 if (buf == NULL || count-- <= 0)
1968 if (count == 0 && buf[0] == e->el_tty.t_c[TS_IO][C_EOF])
1970 if (buf[count] ==
'\n' || buf[count] ==
'\r')
1973 if (done && rl_linefunc != NULL) {
1974 el_set(e, EL_UNBUFFERED, 0);
1976 if ((wbuf = strdup(buf)) != NULL)
1980 (*(void (*)(
const char *))rl_linefunc)(wbuf);
1986 rl_callback_handler_install(
const char *prompt, VCPFunction *linefunc)
1991 (void)rl_set_prompt(prompt);
1992 rl_linefunc = linefunc;
1993 el_set(e, EL_UNBUFFERED, 1);
1997 rl_callback_handler_remove(
void)
1999 el_set(e, EL_UNBUFFERED, 0);
2007 a[0] = (char)e->el_tty.t_c[TS_IO][C_REPRINT];
2013 rl_get_previous_history(
int count,
int key)
2025 rl_prep_terminal(
int meta_flag __attribute__((__unused__)))
2027 el_set(e, EL_PREP_TERM, 1);
2031 rl_deprep_terminal(
void)
2033 el_set(e, EL_PREP_TERM, 0);
2037 rl_read_init_file(
const char *s)
2039 return el_source(e, s);
2043 rl_parse_and_bind(
const char *line)
2049 tok = tok_init(NULL);
2050 tok_str(tok, line, &argc, &argv);
2051 argc = el_parse(e, argc, argv);
2053 return argc ? 1 : 0;
2057 rl_variable_bind(
const char *var,
const char *value)
2063 return el_set(e, EL_BIND,
"", var, value) == -1 ? 1 : 0;
2067 rl_stuff_char(
int c)
2073 el_insertstr(e, buf);
2077 _rl_event_read_char(
EditLine *el,
char *cp)
2080 ssize_t num_read = 0;
2083 while (rl_event_hook) {
2087 #if defined(FIONREAD)
2088 if (ioctl(el->el_infd, FIONREAD, &n) < 0)
2091 num_read = read(el->el_infd, cp, (
size_t)1);
2094 #elif defined(F_SETFL) && defined(O_NDELAY)
2095 if ((n = fcntl(el->el_infd, F_GETFL, 0)) < 0)
2097 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) < 0)
2099 num_read = read(el->el_infd, cp, 1);
2100 if (fcntl(el->el_infd, F_SETFL, n))
2104 num_read = read(el->el_infd, cp, 1);
2108 if (num_read < 0 && errno == EAGAIN)
2115 el_set(el, EL_GETCFN, EL_BUILTIN_GETCFN);
2116 return (
int)num_read;
2120 _rl_update_pos(
void)
2124 rl_point = (int)(li->cursor - li->buffer);
2125 rl_end = (int)(li->lastchar - li->buffer);
2129 rl_get_screen_size(
int *rows,
int *cols)
2132 el_get(e, EL_GETTC,
"li", rows);
2134 el_get(e, EL_GETTC,
"co", cols);
2138 rl_set_screen_size(
int rows,
int cols)
2141 (void)snprintf(buf,
sizeof(buf),
"%d", rows);
2142 el_set(e, EL_SETTC,
"li", buf);
2143 (void)snprintf(buf,
sizeof(buf),
"%d", cols);
2144 el_set(e, EL_SETTC,
"co", buf);
2148 rl_completion_matches(
const char *str, rl_compentry_func_t *fun)
2150 size_t len, max,
i, j, min;
2151 char **list, *
match, *a, *b;
2155 if ((list = el_malloc(max *
sizeof(*list))) == NULL)
2158 while ((match = (*fun)(str, (
int)(len - 1))) != NULL) {
2159 list[len++] = match;
2163 if ((nl = el_realloc(list, max *
sizeof(*nl))) == NULL)
2172 if ((list[0] = strdup(list[1])) == NULL)
2176 qsort(&list[1], len - 1,
sizeof(*list),
2177 (
int (*)(
const void *,
const void *)) strcmp);
2179 for (i = 1, a = list[i]; i < len - 1; i++, a = b) {
2181 for (j = 0; a[j] && a[j] == b[j]; j++)
2186 if (min == 0 && *str) {
2187 if ((list[0] = strdup(str)) == NULL)
2190 if ((list[0] = el_malloc((min + 1) *
sizeof(*list[0]))) == NULL)
2192 (void)memcpy(list[0], list[1], min);
2193 list[0][min] =
'\0';
2203 rl_filename_completion_function (
const char *text,
int state)
2205 return fn_filename_completion_function(text, state);
2209 rl_forced_update_display(
void)
2211 el_set(e, EL_REFRESH);
2215 _rl_abort_internal(
void)
2223 _rl_qsort_string_compare(
char **s1,
char **s2)
2225 return strcoll(*s1, *s2);
2229 history_get_history_state(
void)
2233 if ((hs = el_malloc(
sizeof(*hs))) == NULL)
2235 hs->length = history_length;
2241 rl_kill_text(
int from __attribute__((__unused__)),
2242 int to __attribute__((__unused__)))
2248 rl_make_bare_keymap(
void)
2261 rl_set_keymap(
Keymap k __attribute__((__unused__)))
2267 rl_generic_bind(
int type __attribute__((__unused__)),
2268 const char * keyseq __attribute__((__unused__)),
2269 const char * data __attribute__((__unused__)),
2270 Keymap k __attribute__((__unused__)))
2277 rl_bind_key_in_map(
int key __attribute__((__unused__)),
2278 rl_command_func_t *fun __attribute__((__unused__)),
2279 Keymap k __attribute__((__unused__)))
2286 rl_cleanup_after_signal(
void)
2291 rl_on_new_line(
void)