1 This file is read.def, from which is created read.c.
2 It implements the builtin "read" in Bash.
4 Copyright (C) 1987-2022 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $FUNCTION read_builtin
25 $SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
26 Read a line from the standard input and split it into fields.
28 Reads a single line from the standard input, or from file descriptor FD
29 if the -u option is supplied. The line is split into fields as with word
30 splitting, and the first word is assigned to the first NAME, the second
31 word to the second NAME, and so on, with any leftover words assigned to
32 the last NAME. Only the characters found in $IFS are recognized as word
33 delimiters. By default, the backslash character escapes delimiter characters
36 If no NAMEs are supplied, the line read is stored in the REPLY variable.
39 -a array assign the words read to sequential indices of the array
40 variable ARRAY, starting at zero
41 -d delim continue until the first character of DELIM is read, rather
43 -e use Readline to obtain the line
44 -i text use TEXT as the initial text for Readline
45 -n nchars return after reading NCHARS characters rather than waiting
46 for a newline, but honor a delimiter if fewer than
47 NCHARS characters are read before the delimiter
48 -N nchars return only after reading exactly NCHARS characters, unless
49 EOF is encountered or read times out, ignoring any
51 -p prompt output the string PROMPT without a trailing newline before
53 -r do not allow backslashes to escape any characters
54 -s do not echo input coming from a terminal
55 -t timeout time out and return failure if a complete line of
56 input is not read within TIMEOUT seconds. The value of the
57 TMOUT variable is the default timeout. TIMEOUT may be a
58 fractional number. If TIMEOUT is 0, read returns
59 immediately, without trying to read any data, returning
60 success only if input is available on the specified
61 file descriptor. The exit status is greater than 128
62 if the timeout is exceeded
63 -u fd read from file descriptor FD instead of the standard input
66 The return code is zero, unless end-of-file is encountered, read times out
67 (in which case it's greater than 128), a variable assignment error occurs,
68 or an invalid file descriptor is supplied as the argument to -u.
73 #include "bashtypes.h"
74 #include "posixstat.h"
80 #if defined (HAVE_UNISTD_H)
92 #include "../bashintl.h"
96 #include "bashgetopt.h"
101 #if defined (READLINE)
102 #include "../bashline.h"
103 #include <readline/readline.h>
106 #if defined (BUFFERED_INPUT)
110 #include "shmbutil.h"
123 #if defined (READLINE)
124 static void reset_attempted_completion_function (char *);
125 static int set_itext (void);
126 static char *edit_line (char *, char *);
127 static void set_eol_delim (int);
128 static void reset_eol_delim (char *);
129 static void set_readline_timeout (sh_timer *t, time_t, long);
131 static SHELL_VAR *bind_read_variable (char *, char *, int);
132 #if defined (HANDLE_MULTIBYTE)
133 static int read_mbchar (int, char *, int, int, int);
135 static void ttyrestore (struct ttsave *);
137 static sighandler sigalrm (int);
138 static void reset_timeout (void);
140 /* Try this to see what the rest of the shell can do with the information. */
141 sh_timer *read_timeout;
143 static int reading, tty_modified;
144 static unsigned char delim;
146 static struct ttsave termsave;
148 /* In all cases, SIGALRM just sets a flag that we check periodically. This
149 avoids problems with the semi-tricky stuff we do with the xfree of
150 input_string at the top of the unwind-protect list (see below). */
152 /* Set a flag that check_read_timeout can check. This relies on zread or
153 read_builtin calling trap.c:check_signals() (which calls check_read_timeout()) */
157 /* Display warning if this is called without read_timeout set? */
159 read_timeout->alrmflag = 1;
165 /* Cancel alarm before restoring signal handler. */
167 shtimer_clear (read_timeout);
168 #if defined (READLINE)
175 check_read_timeout (void)
177 if (read_timeout && shtimer_chktimeout (read_timeout))
178 sh_longjmp (read_timeout->jmpenv, 1);
182 read_builtin_timeout (int fd)
184 if ((read_timeout == 0) ||
185 (read_timeout->fd != fd) ||
186 (read_timeout->tmout.tv_sec == 0 && read_timeout->tmout.tv_usec == 0))
189 #if defined (HAVE_SELECT)
190 return ((read_timeout->flags & SHTIMER_ALARM) ? shtimer_alrm (read_timeout)
191 : shtimer_select (read_timeout));
193 return (shtimer_alrm (read_timeout));
197 /* Read the value of the shell variables whose names follow.
198 The reading is done from the current input stream, whatever
199 that may be. Successive words of the input line are assigned
200 to the variables mentioned in LIST. The last variable in LIST
201 gets the remainder of the words on the line. If no variables
202 are mentioned in LIST, then the default variable is $REPLY. */
204 read_builtin (WORD_LIST *list)
207 int nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2, nflag;
210 int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul;
211 int raw, edit, nchars, silent, have_timeout, ignore_delim, fd;
212 int lastsig, t_errno;
214 unsigned int tmsec, tmusec;
218 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
219 char *e, *t, *t1, *ps2, *tofree;
222 TTYSTRUCT ttattrs, ttset;
223 sigset_t chldset, prevset;
224 #if defined (ARRAY_VARS)
229 #if defined (READLINE)
240 USE_VAR(input_is_pipe);
250 #if defined (READLINE)
259 reading = tty_modified = 0;
262 i = 0; /* Index into the string that we are reading. */
263 raw = edit = 0; /* Not reading raw input by default. */
265 arrayname = prompt = (char *)NULL;
266 fd = 0; /* file descriptor to read from */
268 #if defined (READLINE)
269 rlbuf = itext = (char *)0;
273 mb_cur_max = MB_CUR_MAX;
274 tmsec = tmusec = 0; /* no timeout */
275 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
276 delim = '\n'; /* read until newline */
277 ignore_delim = nflag = 0;
279 reset_internal_getopt ();
280 while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1)
288 prompt = list_optarg;
294 #if defined (READLINE)
299 #if defined (READLINE)
303 #if defined (ARRAY_VARS)
305 arrayname = list_optarg;
309 code = uconvert (list_optarg, &ival, &uval, (char **)NULL);
310 if (code == 0 || ival < 0 || uval < 0)
312 builtin_error (_("%s: invalid timeout specification"), list_optarg);
313 return (EXECUTION_FAILURE);
327 code = legal_number (list_optarg, &intval);
328 if (code == 0 || intval < 0 || intval != (int)intval)
330 sh_invalidnum (list_optarg);
331 return (EXECUTION_FAILURE);
337 code = legal_number (list_optarg, &intval);
338 if (code == 0 || intval < 0 || intval != (int)intval)
340 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
341 return (EXECUTION_FAILURE);
345 if (sh_validfd (fd) == 0)
347 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
348 return (EXECUTION_FAILURE);
352 delim = *list_optarg;
362 /* `read -t 0 var' tests whether input is available with select/FIONREAD,
363 and fails if those are unavailable */
364 if (have_timeout && tmsec == 0 && tmusec == 0)
365 return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
367 /* Convenience: check early whether or not the first of possibly several
368 variable names is a valid identifier, and bail early if so. */
369 #if defined (ARRAY_VARS)
371 SET_VFLAGS (list->word->flags, vflags, bindflags);
372 if (list && legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, vflags) == 0)
375 if (list && legal_identifier (list->word->word) == 0)
378 sh_invalidid (list->word->word);
379 return (EXECUTION_FAILURE);
382 /* If we're asked to ignore the delimiter, make sure we do. */
386 /* IF IFS is unset, we use the default of " \t\n". */
387 ifs_chars = getifs ();
388 if (ifs_chars == 0) /* XXX - shouldn't happen */
390 /* If we want to read exactly NCHARS chars, don't split on IFS */
393 for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++)
394 skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL;
396 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
397 input_string[0] = '\0';
399 /* More input and options validation */
400 if (nflag == 1 && nchars == 0)
402 retval = read (fd, &c, 0);
403 retval = (retval >= 0) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
404 goto assign_vars; /* bail early if asked to read 0 chars */
407 /* $TMOUT, if set, is the default timeout for read. */
408 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
410 code = uconvert (e, &ival, &uval, (char **)NULL);
411 if (code == 0 || ival < 0 || uval < 0)
420 #if defined (SIGCHLD)
421 sigemptyset (&chldset);
422 sigprocmask (SIG_BLOCK, (sigset_t *)0, &chldset);
423 sigaddset (&chldset, SIGCHLD);
426 begin_unwind_frame ("read_builtin");
428 #if defined (BUFFERED_INPUT)
429 if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
430 sync_buffered_stream (default_buffered_input);
434 input_is_tty = isatty (fd);
438 if (input_is_tty == 0)
440 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
445 /* If the -p, -e or -s flags were given, but input is not coming from the
446 terminal, turn them off. */
447 if ((prompt || edit || silent) && input_is_tty == 0)
449 prompt = (char *)NULL;
450 #if defined (READLINE)
451 itext = (char *)NULL;
456 #if defined (READLINE)
458 add_unwind_protect (xfree, rlbuf);
461 pass_next = 0; /* Non-zero signifies last char was backslash. */
462 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
464 if (tmsec > 0 || tmusec > 0)
466 /* Turn off the timeout if stdin is a regular file (e.g. from
467 input redirection). */
468 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
472 if (tmsec > 0 || tmusec > 0)
474 read_timeout = shtimer_alloc ();
475 read_timeout->flags = SHTIMER_LONGJMP;
477 #if defined (HAVE_SELECT)
478 read_timeout->flags |= (edit || posixly_correct) ? SHTIMER_ALARM : SHTIMER_SELECT;
480 read_timeout->flags |= SHTIMER_ALARM;
482 read_timeout->fd = fd;
484 read_timeout->alrm_handler = sigalrm;
487 if (tmsec > 0 || tmusec > 0)
489 code = setjmp_nosigs (read_timeout->jmpenv);
493 sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0);
495 /* Tricky. The top of the unwind-protect stack is the free of
496 input_string. We want to run all the rest and use input_string,
497 so we have to save input_string temporarily, run the unwind-
498 protects, then restore input_string so we can use it later */
499 orig_input_string = 0;
500 input_string[i] = '\0'; /* make sure it's terminated */
503 t = (char *)xmalloc (1);
507 t = savestring (input_string);
509 run_unwind_frame ("read_builtin");
511 retval = 128+SIGALRM;
514 if (interactive_shell == 0)
515 initialize_terminating_signals ();
516 add_unwind_protect (reset_timeout, (char *)NULL);
517 #if defined (READLINE)
520 add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
521 add_unwind_protect (bashline_reset_event_hook, (char *)NULL);
522 set_readline_timeout (read_timeout, tmsec, tmusec);
526 shtimer_set (read_timeout, tmsec, tmusec);
529 /* If we've been asked to read only NCHARS chars, or we're using some
530 character other than newline to terminate the line, do the right
531 thing to readline or the tty. */
532 if (nchars > 0 || delim != '\n')
534 #if defined (READLINE)
539 unwind_protect_int (rl_num_chars_to_read);
540 rl_num_chars_to_read = nchars;
544 set_eol_delim (delim);
545 add_unwind_protect (reset_eol_delim, (char *)NULL);
554 ttgetattr (fd, &ttattrs);
555 termsave.attrs = ttattrs;
558 i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset);
562 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
563 if (interactive_shell == 0)
564 initialize_terminating_signals ();
567 else if (silent) /* turn off echo but leave term in canonical mode */
571 ttgetattr (fd, &ttattrs);
572 termsave.attrs = ttattrs;
575 i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */
580 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
581 if (interactive_shell == 0)
582 initialize_terminating_signals ();
585 #if defined (READLINE)
589 if (bash_readline_initialized == 0)
590 initialize_readline ();
592 unwind_protect_var (rl_instream);
593 save_instream = rl_instream;
594 rl_instream = fdopen (fd, "r");
598 /* This *must* be the top unwind-protect on the stack, so the manipulation
599 of the unwind-protect stack after the realloc() works right. */
600 add_unwind_protect (xfree, input_string);
602 check_read_timeout ();
603 /* These only matter if edit == 0 */
604 if ((nchars > 0) && (input_is_tty == 0) && ignore_delim) /* read -N */
607 else if ((nchars > 0) || (delim != '\n') || input_is_pipe)
609 else if (((nchars > 0 || delim != '\n') && input_is_tty) || input_is_pipe)
612 if (prompt && edit == 0)
614 fprintf (stderr, "%s", prompt);
618 #if defined (__CYGWIN__) && defined (O_TEXT)
623 for (print_ps2 = eof = retval = 0;;)
625 check_read_timeout ();
627 #if defined (READLINE)
630 /* If we have a null delimiter, don't treat NULL as ending the line */
631 if (rlbuf && rlbuf[rlind] == '\0' && delim != '\0')
636 #if defined (SIGCHLD)
637 if (tmsec > 0 || tmusec > 0)
638 sigprocmask (SIG_SETMASK, &chldset, &prevset);
643 rlbuf = edit_line (prompt ? prompt : "", itext);
647 #if defined (SIGCHLD)
648 if (tmsec > 0 || tmusec > 0)
649 sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0);
665 ps2 = get_string_value ("PS2");
666 fprintf (stderr, "%s", ps2 ? ps2 : "");
672 check_read_timeout ();
675 #if defined (SIGCHLD)
676 if (tmsec > 0 || tmusec > 0)
677 sigprocmask (SIG_SETMASK, &chldset, &prevset);
679 if (unbuffered_read == 2)
680 retval = posixly_correct ? zreadintr (fd, &c, 1) : zreadn (fd, &c, nchars - nr);
681 else if (unbuffered_read)
682 retval = posixly_correct ? zreadintr (fd, &c, 1) : zread (fd, &c, 1);
684 retval = posixly_correct ? zreadcintr (fd, &c) : zreadc (fd, &c);
685 #if defined (SIGCHLD)
686 if (tmsec > 0 || tmusec > 0)
687 sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0);
697 if (retval < 0 && errno == EINTR)
699 check_signals (); /* in case we didn't call zread via zreadc */
702 lastsig = trapped_signal_received;
704 run_pending_traps (); /* because interrupt_immediately is not set */
709 if (terminating_signal && tty_modified)
710 ttyrestore (&termsave); /* fix terminal before exiting */
713 errno = x; /* preserve it for the error message below */
717 QUIT; /* in case we didn't call check_signals() */
718 #if defined (READLINE)
722 if (retval <= 0) /* XXX shouldn't happen */
723 check_read_timeout ();
725 /* XXX -- use i + mb_cur_max (at least 4) for multibyte/read_mbchar */
726 if (i + (mb_cur_max > 4 ? mb_cur_max : 4) >= size)
729 x = (char *)xrealloc (input_string, size += 128);
731 /* Only need to change unwind-protect if input_string changes */
732 if (x != input_string)
735 remove_unwind_protect ();
736 add_unwind_protect (xfree, input_string);
740 /* If the next character is to be accepted verbatim, a backslash
741 newline pair still disappears from the input. */
747 if (skip_ctlesc == 0 && i > 0)
748 i--; /* back up over the CTLESC */
749 if (interactive && input_is_tty && raw == 0)
757 /* This may cause problems if IFS contains CTLESC */
758 if (c == '\\' && raw == 0)
761 if (skip_ctlesc == 0)
764 input_string[i++] = CTLESC;
769 if (ignore_delim == 0 && (unsigned char)c == delim)
772 if (c == '\0' && delim != '\0')
773 continue; /* skip NUL bytes in input */
775 if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL))
778 input_string[i++] = CTLESC;
782 input_string[i++] = c;
783 check_read_timeout ();
785 #if defined (HANDLE_MULTIBYTE)
786 /* XXX - what if C == 127? Can DEL introduce a multibyte sequence? */
787 if (mb_cur_max > 1 && is_basic (c) == 0)
789 input_string[i] = '\0'; /* for simplicity and debugging */
790 /* If we got input from readline, grab the next multibyte char from
792 # if defined (READLINE)
796 clen = mbrlen (rlbuf + rlind - 1, mb_cur_max, (mbstate_t *)NULL);
797 /* We only deal with valid multibyte sequences longer than one
798 byte. If we get anything else, we leave the one character
799 copied and move on to the next. */
802 memcpy (input_string+i, rlbuf+rlind, clen-1);
809 if (locale_utf8locale == 0 || ((c & 0x80) != 0))
810 i += read_mbchar (fd, input_string, i, c, unbuffered_read);
816 if (nchars > 0 && nr >= nchars)
819 input_string[i] = '\0';
820 check_read_timeout ();
822 #if defined (READLINE)
831 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
832 run_unwind_frame ("read_builtin");
833 return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig);
836 if (tmsec > 0 || tmusec > 0)
839 if (nchars > 0 || delim != '\n')
841 #if defined (READLINE)
845 rl_num_chars_to_read = 0;
847 reset_eol_delim ((char *)NULL);
852 ttyrestore (&termsave);
855 ttyrestore (&termsave);
857 if (unbuffered_read == 0)
860 #if defined (READLINE)
862 rl_instream = save_instream; /* can't portably free it */
865 discard_unwind_frame ("read_builtin");
867 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
871 #if defined (ARRAY_VARS)
872 /* If -a was given, take the string read, break it into a list of words,
873 an assign them to `arrayname' in turn. */
876 /* pass 1 for flags arg to clear the existing array + 2 to check for a
878 var = builtin_find_indexed_array (arrayname, 3);
882 return EXECUTION_FAILURE; /* readonly or noassign */
885 alist = list_string (input_string, ifs_chars, 0);
889 dequote_list (alist);
891 word_list_remove_quoted_nulls (alist);
892 assign_array_var_from_word_list (var, alist, 0);
893 dispose_words (alist);
898 #endif /* ARRAY_VARS */
900 /* If there are no variables, save the text of the line read to the
901 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
902 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
903 same way, but I believe that the difference in behaviors is useful
904 enough to not do it. Without the bash behavior, there is no way
905 to read a line completely without interpretation or modification
906 unless you mess with $IFS (e.g., setting it to the empty string).
907 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
911 orig_input_string = input_string;
912 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
915 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
920 t = dequote_string (input_string);
921 var = bind_variable ("REPLY", t, 0);
925 var = bind_variable ("REPLY", input_string, 0);
926 if (var == 0 || readonly_p (var) || noassign_p (var))
927 retval = EXECUTION_FAILURE;
929 VUNSETATTR (var, att_invisible);
935 /* This code implements the Posix.2 spec for splitting the words
936 read and assigning them to variables. */
937 orig_input_string = input_string;
939 /* Remove IFS white space at the beginning of the input string. If
940 $IFS is null, no field splitting is performed. */
941 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
944 for (; list->next; list = list->next)
946 varname = list->word->word;
947 #if defined (ARRAY_VARS)
948 SET_VFLAGS (list->word->flags, vflags, bindflags);
949 if (legal_identifier (varname) == 0 && valid_array_reference (varname, vflags) == 0)
951 if (legal_identifier (varname) == 0)
954 sh_invalidid (varname);
955 free (orig_input_string);
956 return (EXECUTION_FAILURE);
959 /* If there are more variables than words read from the input,
960 the remaining variables are set to the empty string. */
963 /* This call updates INPUT_STRING. */
964 t = get_word_from_string (&input_string, ifs_chars, &e);
967 /* Don't bother to remove the CTLESC unless we added one
968 somewhere while reading the string. */
971 t1 = dequote_string (t);
972 var = bind_read_variable (varname, t1, bindflags);
976 var = bind_read_variable (varname, t ? t : "", bindflags);
981 var = bind_read_variable (varname, "", bindflags);
987 free (orig_input_string);
988 return (EXECUTION_FAILURE);
991 stupidly_hack_special_variables (varname);
992 VUNSETATTR (var, att_invisible);
995 /* Now assign the rest of the line to the last variable argument. */
996 #if defined (ARRAY_VARS)
997 SET_VFLAGS (list->word->flags, vflags, bindflags);
998 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, vflags) == 0)
1000 if (legal_identifier (list->word->word) == 0)
1003 sh_invalidid (list->word->word);
1004 free (orig_input_string);
1005 return (EXECUTION_FAILURE);
1009 /* This has to be done this way rather than using string_list
1010 and list_string because Posix.2 says that the last variable gets the
1011 remaining words and their intervening separators. */
1012 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
1014 /* Check whether or not the number of fields is exactly the same as the
1015 number of variables. */
1020 t = get_word_from_string (&input_string, ifs_chars, &e);
1021 if (*input_string == 0)
1022 tofree = input_string = t;
1025 input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
1031 if (saw_escape && input_string && *input_string)
1033 t = dequote_string (input_string);
1034 var = bind_read_variable (list->word->word, t, bindflags);
1038 var = bind_read_variable (list->word->word, input_string ? input_string : "", bindflags);
1042 stupidly_hack_special_variables (list->word->word);
1043 VUNSETATTR (var, att_invisible);
1046 retval = EXECUTION_FAILURE;
1049 free (orig_input_string);
1055 bind_read_variable (char *name, char *value, int flags)
1059 v = builtin_bind_variable (name, value, flags);
1061 : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v));
1064 #if defined (HANDLE_MULTIBYTE)
1066 read_mbchar (int fd, char *string, int ind, int ch, int unbuffered)
1068 char mbchar[MB_LEN_MAX + 1];
1072 mbstate_t ps, ps_back;
1075 memset (&ps, '\0', sizeof (mbstate_t));
1076 memset (&ps_back, '\0', sizeof (mbstate_t));
1080 for (n = 0; n <= MB_LEN_MAX; n++)
1083 ret = mbrtowc (&wc, mbchar, i, &ps);
1084 if (ret == (size_t)-2)
1088 /* We don't want to be interrupted during a multibyte char read */
1089 if (unbuffered == 2)
1090 r = zreadn (fd, &c, 1);
1091 else if (unbuffered)
1092 r = zread (fd, &c, 1);
1094 r = zreadc (fd, &c);
1100 else if (ret == (size_t)-1 || ret == (size_t)0 || ret > (size_t)0)
1105 if (i > 1) /* read a multibyte char */
1106 /* mbchar[0] is already string[ind-1] */
1107 for (r = 1; r < i; r++)
1108 string[ind+r-1] = mbchar[r];
1115 ttyrestore (struct ttsave *ttp)
1117 ttsetattr (ttp->fd, &(ttp->attrs));
1122 read_tty_cleanup (void)
1125 ttyrestore (&termsave);
1129 read_tty_modified (void)
1131 return (tty_modified);
1134 #if defined (READLINE)
1135 static rl_completion_func_t *old_attempted_completion_function = 0;
1136 static rl_hook_func_t *old_startup_hook;
1137 static char *deftext;
1140 reset_attempted_completion_function (char *cp)
1142 if (rl_attempted_completion_function == 0 && old_attempted_completion_function)
1143 rl_attempted_completion_function = old_attempted_completion_function;
1152 if (old_startup_hook)
1153 r1 = (*old_startup_hook) ();
1156 r2 = rl_insert_text (deftext);
1157 deftext = (char *)NULL;
1158 rl_startup_hook = old_startup_hook;
1159 old_startup_hook = (rl_hook_func_t *)NULL;
1165 edit_line (char *p, char *itext)
1170 if (bash_readline_initialized == 0)
1171 initialize_readline ();
1173 old_attempted_completion_function = rl_attempted_completion_function;
1174 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
1175 bashline_set_event_hook ();
1178 old_startup_hook = rl_startup_hook;
1179 rl_startup_hook = set_itext;
1185 rl_attempted_completion_function = old_attempted_completion_function;
1186 old_attempted_completion_function = (rl_completion_func_t *)NULL;
1187 bashline_reset_event_hook ();
1191 if (RL_ISSTATE (RL_STATE_TIMEOUT))
1193 sigalrm (SIGALRM); /* simulate receiving SIGALRM */
1194 check_read_timeout ();
1200 ret = (char *)xrealloc (ret, len + 2);
1207 set_readline_timeout (sh_timer *t, time_t sec, long usec)
1209 t->tmout.tv_sec = sec;
1210 t->tmout.tv_usec = usec;
1211 rl_set_timeout (sec, usec);
1214 static int old_delim_ctype;
1215 static rl_command_func_t *old_delim_func;
1216 static int old_newline_ctype;
1217 static rl_command_func_t *old_newline_func;
1219 static unsigned char delim_char;
1222 set_eol_delim (int c)
1226 if (bash_readline_initialized == 0)
1227 initialize_readline ();
1228 cmap = rl_get_keymap ();
1230 /* Save the old delimiter char binding */
1231 old_newline_ctype = cmap[RETURN].type;
1232 old_newline_func = cmap[RETURN].function;
1233 old_delim_ctype = cmap[c].type;
1234 old_delim_func = cmap[c].function;
1236 /* Change newline to self-insert */
1237 cmap[RETURN].type = ISFUNC;
1238 cmap[RETURN].function = rl_insert;
1240 /* Bind the delimiter character to accept-line. */
1241 cmap[c].type = ISFUNC;
1242 cmap[c].function = rl_newline;
1248 reset_eol_delim (char *cp)
1252 cmap = rl_get_keymap ();
1254 cmap[RETURN].type = old_newline_ctype;
1255 cmap[RETURN].function = old_newline_func;
1257 cmap[delim_char].type = old_delim_ctype;
1258 cmap[delim_char].function = old_delim_func;