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