Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is read.def, from which is created read.c. |
| 2 | It implements the builtin "read" in Bash. |
| 3 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2012 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES read.c |
| 22 | |
| 23 | $BUILTIN read |
| 24 | $FUNCTION read_builtin |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 25 | $SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Read a line from the standard input and split it into fields. |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 27 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
| 34 | |
| 35 | If no NAMEs are supplied, the line read is stored in the REPLY variable. |
| 36 | |
| 37 | Options: |
| 38 | -a array assign the words read to sequential indices of the array |
| 39 | variable ARRAY, starting at zero |
| 40 | -d delim continue until the first character of DELIM is read, rather |
| 41 | than newline |
| 42 | -e use Readline to obtain the line in an interactive shell |
| 43 | -i text Use TEXT as the initial text for Readline |
| 44 | -n nchars return after reading NCHARS characters rather than waiting |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 45 | for a newline, but honor a delimiter if fewer than NCHARS |
| 46 | characters are read before the delimiter |
| 47 | -N nchars return only after reading exactly NCHARS characters, unless |
| 48 | EOF is encountered or read times out, ignoring any delimiter |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 49 | -p prompt output the string PROMPT without a trailing newline before |
| 50 | attempting to read |
| 51 | -r do not allow backslashes to escape any characters |
| 52 | -s do not echo input coming from a terminal |
| 53 | -t timeout time out and return failure if a complete line of input is |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 54 | not read within TIMEOUT seconds. The value of the TMOUT |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 55 | variable is the default timeout. TIMEOUT may be a |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 56 | fractional number. If TIMEOUT is 0, read returns immediately, |
| 57 | without trying to read any data, returning success only if |
| 58 | input is available on the specified file descriptor. The |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 59 | exit status is greater than 128 if the timeout is exceeded |
| 60 | -u fd read from file descriptor FD instead of the standard input |
| 61 | |
| 62 | Exit Status: |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 63 | The return code is zero, unless end-of-file is encountered, read times out |
| 64 | (in which case it's greater than 128), a variable assignment error occurs, |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 65 | or an invalid file descriptor is supplied as the argument to -u. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 66 | $END |
| 67 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 68 | #include <config.h> |
| 69 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 70 | #include "bashtypes.h" |
| 71 | #include "posixstat.h" |
| 72 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 73 | #include <stdio.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 74 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 75 | #include "bashansi.h" |
| 76 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 77 | #if defined (HAVE_UNISTD_H) |
| 78 | # include <unistd.h> |
| 79 | #endif |
| 80 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 81 | #include <signal.h> |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 82 | #include <errno.h> |
| 83 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 84 | #ifdef __CYGWIN__ |
| 85 | # include <fcntl.h> |
| 86 | # include <io.h> |
| 87 | #endif |
| 88 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 89 | #include "../bashintl.h" |
| 90 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | #include "../shell.h" |
| 92 | #include "common.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 93 | #include "bashgetopt.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 94 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 95 | #include <shtty.h> |
| 96 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 97 | #if defined (READLINE) |
| 98 | #include "../bashline.h" |
| 99 | #include <readline/readline.h> |
| 100 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 101 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 102 | #if defined (BUFFERED_INPUT) |
| 103 | # include "input.h" |
| 104 | #endif |
| 105 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 106 | #include "shmbutil.h" |
| 107 | |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 108 | #if !defined(errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 109 | #include <errno.h> |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 112 | extern void run_pending_traps __P((void)); |
| 113 | |
| 114 | extern int posixly_correct; |
| 115 | extern int trapped_signal_received; |
| 116 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 117 | struct ttsave |
| 118 | { |
| 119 | int fd; |
| 120 | TTYSTRUCT *attrs; |
| 121 | }; |
| 122 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 123 | #if defined (READLINE) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 124 | static void reset_attempted_completion_function __P((char *)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 125 | static int set_itext __P((void)); |
| 126 | static char *edit_line __P((char *, char *)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 127 | static void set_eol_delim __P((int)); |
| 128 | static void reset_eol_delim __P((char *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 129 | #endif |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 130 | static SHELL_VAR *bind_read_variable __P((char *, char *)); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 131 | #if defined (HANDLE_MULTIBYTE) |
| 132 | static int read_mbchar __P((int, char *, int, int, int)); |
| 133 | #endif |
| 134 | static void ttyrestore __P((struct ttsave *)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 135 | |
| 136 | static sighandler sigalrm __P((int)); |
| 137 | static void reset_alarm __P((void)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 138 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 139 | /* Try this to see what the rest of the shell can do with the information. */ |
| 140 | procenv_t alrmbuf; |
| 141 | int sigalrm_seen; |
| 142 | |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 143 | static int reading, tty_modified; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 144 | static SigHandler *old_alrm; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 145 | static unsigned char delim; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 146 | |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 147 | static struct ttsave termsave; |
| 148 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 149 | /* In all cases, SIGALRM just sets a flag that we check periodically. This |
| 150 | avoids problems with the semi-tricky stuff we do with the xfree of |
| 151 | input_string at the top of the unwind-protect list (see below). */ |
| 152 | |
| 153 | /* Set a flag that CHECK_ALRM can check. This relies on zread calling |
| 154 | trap.c:check_signals_and_traps(), which knows about sigalrm_seen and |
| 155 | alrmbuf. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 156 | static sighandler |
| 157 | sigalrm (s) |
| 158 | int s; |
| 159 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 160 | sigalrm_seen = 1; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void |
| 164 | reset_alarm () |
| 165 | { |
| 166 | set_signal_handler (SIGALRM, old_alrm); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 167 | falarm (0, 0); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 170 | /* Read the value of the shell variables whose names follow. |
| 171 | The reading is done from the current input stream, whatever |
| 172 | that may be. Successive words of the input line are assigned |
| 173 | to the variables mentioned in LIST. The last variable in LIST |
| 174 | gets the remainder of the words on the line. If no variables |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 175 | are mentioned in LIST, then the default variable is $REPLY. */ |
| 176 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 177 | read_builtin (list) |
| 178 | WORD_LIST *list; |
| 179 | { |
| 180 | register char *varname; |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 181 | int size, i, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2; |
| 182 | int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 183 | int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 184 | unsigned int tmsec, tmusec; |
| 185 | long ival, uval; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 186 | intmax_t intval; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 187 | char c; |
| 188 | char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname; |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 189 | char *e, *t, *t1, *ps2, *tofree; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 190 | struct stat tsb; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 191 | SHELL_VAR *var; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 192 | TTYSTRUCT ttattrs, ttset; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 193 | #if defined (ARRAY_VARS) |
| 194 | WORD_LIST *alist; |
| 195 | #endif |
| 196 | #if defined (READLINE) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 197 | char *rlbuf, *itext; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 198 | int rlind; |
| 199 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 200 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 201 | USE_VAR(size); |
| 202 | USE_VAR(i); |
| 203 | USE_VAR(pass_next); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 204 | USE_VAR(print_ps2); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 205 | USE_VAR(saw_escape); |
| 206 | USE_VAR(input_is_pipe); |
| 207 | /* USE_VAR(raw); */ |
| 208 | USE_VAR(edit); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 209 | USE_VAR(tmsec); |
| 210 | USE_VAR(tmusec); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 211 | USE_VAR(nchars); |
| 212 | USE_VAR(silent); |
| 213 | USE_VAR(ifs_chars); |
| 214 | USE_VAR(prompt); |
| 215 | USE_VAR(arrayname); |
| 216 | #if defined (READLINE) |
| 217 | USE_VAR(rlbuf); |
| 218 | USE_VAR(rlind); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 219 | USE_VAR(itext); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 220 | #endif |
| 221 | USE_VAR(list); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 222 | USE_VAR(ps2); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 223 | USE_VAR(lastsig); |
| 224 | |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 225 | sigalrm_seen = reading = tty_modified = 0; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 226 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 227 | i = 0; /* Index into the string that we are reading. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 228 | raw = edit = 0; /* Not reading raw input by default. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 229 | silent = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 230 | arrayname = prompt = (char *)NULL; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 231 | fd = 0; /* file descriptor to read from */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 232 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 233 | #if defined (READLINE) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 234 | rlbuf = itext = (char *)0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 235 | rlind = 0; |
| 236 | #endif |
| 237 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 238 | tmsec = tmusec = 0; /* no timeout */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 239 | nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 240 | delim = '\n'; /* read until newline */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 241 | ignore_delim = 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 242 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 243 | reset_internal_getopt (); |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 244 | while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 245 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 246 | switch (opt) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 247 | { |
| 248 | case 'r': |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 249 | raw = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 250 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 251 | case 'p': |
| 252 | prompt = list_optarg; |
| 253 | break; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 254 | case 's': |
| 255 | silent = 1; |
| 256 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 257 | case 'e': |
| 258 | #if defined (READLINE) |
| 259 | edit = 1; |
| 260 | #endif |
| 261 | break; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 262 | case 'i': |
| 263 | #if defined (READLINE) |
| 264 | itext = list_optarg; |
| 265 | #endif |
| 266 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 267 | #if defined (ARRAY_VARS) |
| 268 | case 'a': |
| 269 | arrayname = list_optarg; |
| 270 | break; |
| 271 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 272 | case 't': |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 273 | code = uconvert (list_optarg, &ival, &uval); |
| 274 | if (code == 0 || ival < 0 || uval < 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 275 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 276 | builtin_error (_("%s: invalid timeout specification"), list_optarg); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 277 | return (EXECUTION_FAILURE); |
| 278 | } |
| 279 | else |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 280 | { |
| 281 | have_timeout = 1; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 282 | tmsec = ival; |
| 283 | tmusec = uval; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 284 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 285 | break; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 286 | case 'N': |
| 287 | ignore_delim = 1; |
| 288 | delim = -1; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 289 | case 'n': |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 290 | code = legal_number (list_optarg, &intval); |
| 291 | if (code == 0 || intval < 0 || intval != (int)intval) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 292 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 293 | sh_invalidnum (list_optarg); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 294 | return (EXECUTION_FAILURE); |
| 295 | } |
| 296 | else |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 297 | nchars = intval; |
| 298 | break; |
| 299 | case 'u': |
| 300 | code = legal_number (list_optarg, &intval); |
| 301 | if (code == 0 || intval < 0 || intval != (int)intval) |
| 302 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 303 | builtin_error (_("%s: invalid file descriptor specification"), list_optarg); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 304 | return (EXECUTION_FAILURE); |
| 305 | } |
| 306 | else |
| 307 | fd = intval; |
| 308 | if (sh_validfd (fd) == 0) |
| 309 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 310 | builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 311 | return (EXECUTION_FAILURE); |
| 312 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 313 | break; |
| 314 | case 'd': |
| 315 | delim = *list_optarg; |
| 316 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 317 | default: |
| 318 | builtin_usage (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 319 | return (EX_USAGE); |
| 320 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 321 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 322 | list = loptend; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 323 | |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 324 | /* `read -t 0 var' tests whether input is available with select/FIONREAD, |
| 325 | and fails if those are unavailable */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 326 | if (have_timeout && tmsec == 0 && tmusec == 0) |
| 327 | #if 0 |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 328 | return (EXECUTION_FAILURE); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 329 | #else |
| 330 | return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
| 331 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 332 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 333 | /* Convenience: check early whether or not the first of possibly several |
| 334 | variable names is a valid identifier, and bail early if so. */ |
| 335 | #if defined (ARRAY_VARS) |
| 336 | if (list && legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0) |
| 337 | #else |
| 338 | if (list && legal_identifier (list->word->word) == 0) |
| 339 | #endif |
| 340 | { |
| 341 | sh_invalidid (list->word->word); |
| 342 | return (EXECUTION_FAILURE); |
| 343 | } |
| 344 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 345 | /* If we're asked to ignore the delimiter, make sure we do. */ |
| 346 | if (ignore_delim) |
| 347 | delim = -1; |
| 348 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 349 | /* IF IFS is unset, we use the default of " \t\n". */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 350 | ifs_chars = getifs (); |
| 351 | if (ifs_chars == 0) /* XXX - shouldn't happen */ |
| 352 | ifs_chars = ""; |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 353 | /* If we want to read exactly NCHARS chars, don't split on IFS */ |
| 354 | if (ignore_delim) |
| 355 | ifs_chars = ""; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 356 | for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++) |
| 357 | skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 358 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 359 | input_string = (char *)xmalloc (size = 112); /* XXX was 128 */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 360 | input_string[0] = '\0'; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 361 | |
| 362 | /* $TMOUT, if set, is the default timeout for read. */ |
| 363 | if (have_timeout == 0 && (e = get_string_value ("TMOUT"))) |
| 364 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 365 | code = uconvert (e, &ival, &uval); |
| 366 | if (code == 0 || ival < 0 || uval < 0) |
| 367 | tmsec = tmusec = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 368 | else |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 369 | { |
| 370 | tmsec = ival; |
| 371 | tmusec = uval; |
| 372 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 373 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 374 | |
| 375 | begin_unwind_frame ("read_builtin"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 376 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 377 | #if defined (BUFFERED_INPUT) |
| 378 | if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd)) |
| 379 | sync_buffered_stream (default_buffered_input); |
| 380 | #endif |
| 381 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 382 | input_is_tty = isatty (fd); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 383 | if (input_is_tty == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 384 | #ifndef __CYGWIN__ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 385 | input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 386 | #else |
| 387 | input_is_pipe = 1; |
| 388 | #endif |
| 389 | |
| 390 | /* If the -p, -e or -s flags were given, but input is not coming from the |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 391 | terminal, turn them off. */ |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 392 | if ((prompt || edit || silent) && input_is_tty == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 393 | { |
| 394 | prompt = (char *)NULL; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 395 | #if defined (READLINE) |
| 396 | itext = (char *)NULL; |
| 397 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 398 | edit = silent = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 401 | #if defined (READLINE) |
| 402 | if (edit) |
| 403 | add_unwind_protect (xfree, rlbuf); |
| 404 | #endif |
| 405 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 406 | pass_next = 0; /* Non-zero signifies last char was backslash. */ |
| 407 | saw_escape = 0; /* Non-zero signifies that we saw an escape char */ |
| 408 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 409 | if (tmsec > 0 || tmusec > 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 410 | { |
| 411 | /* Turn off the timeout if stdin is a regular file (e.g. from |
| 412 | input redirection). */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 413 | if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode)) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 414 | tmsec = tmusec = 0; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 417 | if (tmsec > 0 || tmusec > 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 418 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 419 | code = setjmp_nosigs (alrmbuf); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 420 | if (code) |
| 421 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 422 | sigalrm_seen = 0; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 423 | /* Tricky. The top of the unwind-protect stack is the free of |
| 424 | input_string. We want to run all the rest and use input_string, |
Chet Ramey | 43aebe9 | 2013-03-07 15:20:58 -0500 | [diff] [blame] | 425 | so we have to save input_string temporarily, run the unwind- |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 426 | protects, then restore input_string so we can use it later */ |
| 427 | orig_input_string = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 428 | input_string[i] = '\0'; /* make sure it's terminated */ |
Chet Ramey | 43aebe9 | 2013-03-07 15:20:58 -0500 | [diff] [blame] | 429 | if (i == 0) |
| 430 | { |
| 431 | t = (char *)xmalloc (1); |
| 432 | t[0] = 0; |
| 433 | } |
| 434 | else |
| 435 | t = savestring (input_string); |
| 436 | |
| 437 | run_unwind_frame ("read_builtin"); |
| 438 | input_string = t; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 439 | retval = 128+SIGALRM; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 440 | goto assign_vars; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 441 | } |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 442 | if (interactive_shell == 0) |
| 443 | initialize_terminating_signals (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 444 | old_alrm = set_signal_handler (SIGALRM, sigalrm); |
| 445 | add_unwind_protect (reset_alarm, (char *)NULL); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 446 | #if defined (READLINE) |
| 447 | if (edit) |
Chet Ramey | 9b86eb2 | 2014-08-01 15:30:33 -0400 | [diff] [blame] | 448 | { |
| 449 | add_unwind_protect (reset_attempted_completion_function, (char *)NULL); |
| 450 | add_unwind_protect (bashline_reset_event_hook, (char *)NULL); |
| 451 | } |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 452 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 453 | falarm (tmsec, tmusec); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* If we've been asked to read only NCHARS chars, or we're using some |
| 457 | character other than newline to terminate the line, do the right |
| 458 | thing to readline or the tty. */ |
| 459 | if (nchars > 0 || delim != '\n') |
| 460 | { |
| 461 | #if defined (READLINE) |
| 462 | if (edit) |
| 463 | { |
| 464 | if (nchars > 0) |
| 465 | { |
| 466 | unwind_protect_int (rl_num_chars_to_read); |
| 467 | rl_num_chars_to_read = nchars; |
| 468 | } |
| 469 | if (delim != '\n') |
| 470 | { |
| 471 | set_eol_delim (delim); |
| 472 | add_unwind_protect (reset_eol_delim, (char *)NULL); |
| 473 | } |
| 474 | } |
| 475 | else |
| 476 | #endif |
| 477 | if (input_is_tty) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 478 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 479 | /* ttsave() */ |
| 480 | termsave.fd = fd; |
| 481 | ttgetattr (fd, &ttattrs); |
| 482 | termsave.attrs = &ttattrs; |
| 483 | |
| 484 | ttset = ttattrs; |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 485 | i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset); |
| 486 | if (i < 0) |
| 487 | sh_ttyerror (1); |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 488 | tty_modified = 1; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 489 | add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 490 | if (interactive_shell == 0) |
| 491 | initialize_terminating_signals (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | else if (silent) /* turn off echo but leave term in canonical mode */ |
| 495 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 496 | /* ttsave (); */ |
| 497 | termsave.fd = fd; |
| 498 | ttgetattr (fd, &ttattrs); |
| 499 | termsave.attrs = &ttattrs; |
| 500 | |
| 501 | ttset = ttattrs; |
Jari Aalto | 17345e5 | 2009-02-19 22:21:29 +0000 | [diff] [blame] | 502 | i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */ |
| 503 | if (i < 0) |
| 504 | sh_ttyerror (1); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 505 | |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 506 | tty_modified = 1; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 507 | add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 508 | if (interactive_shell == 0) |
| 509 | initialize_terminating_signals (); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 512 | /* This *must* be the top unwind-protect on the stack, so the manipulation |
| 513 | of the unwind-protect stack after the realloc() works right. */ |
| 514 | add_unwind_protect (xfree, input_string); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 515 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 516 | CHECK_ALRM; |
| 517 | if ((nchars > 0) && (input_is_tty == 0) && ignore_delim) /* read -N */ |
| 518 | unbuffered_read = 2; |
| 519 | else if ((nchars > 0) || (delim != '\n') || input_is_pipe) |
| 520 | unbuffered_read = 1; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 521 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 522 | if (prompt && edit == 0) |
| 523 | { |
| 524 | fprintf (stderr, "%s", prompt); |
| 525 | fflush (stderr); |
| 526 | } |
| 527 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 528 | #if defined (__CYGWIN__) && defined (O_TEXT) |
| 529 | setmode (0, O_TEXT); |
| 530 | #endif |
| 531 | |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 532 | ps2 = 0; |
| 533 | for (print_ps2 = eof = retval = 0;;) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 534 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 535 | CHECK_ALRM; |
| 536 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 537 | #if defined (READLINE) |
| 538 | if (edit) |
| 539 | { |
| 540 | if (rlbuf && rlbuf[rlind] == '\0') |
| 541 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 542 | xfree (rlbuf); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 543 | rlbuf = (char *)0; |
| 544 | } |
| 545 | if (rlbuf == 0) |
| 546 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 547 | reading = 1; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 548 | rlbuf = edit_line (prompt ? prompt : "", itext); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 549 | reading = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 550 | rlind = 0; |
| 551 | } |
| 552 | if (rlbuf == 0) |
| 553 | { |
| 554 | eof = 1; |
| 555 | break; |
| 556 | } |
| 557 | c = rlbuf[rlind++]; |
| 558 | } |
| 559 | else |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 560 | { |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 561 | #endif |
| 562 | |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 563 | if (print_ps2) |
| 564 | { |
| 565 | if (ps2 == 0) |
| 566 | ps2 = get_string_value ("PS2"); |
| 567 | fprintf (stderr, "%s", ps2 ? ps2 : ""); |
| 568 | fflush (stderr); |
| 569 | print_ps2 = 0; |
| 570 | } |
| 571 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 572 | #if 0 |
| 573 | if (posixly_correct == 0) |
| 574 | interrupt_immediately++; |
| 575 | #endif |
| 576 | reading = 1; |
| 577 | if (unbuffered_read == 2) |
| 578 | retval = posixly_correct ? zreadintr (fd, &c, 1) : zreadn (fd, &c, nchars - nr); |
| 579 | else if (unbuffered_read) |
| 580 | retval = posixly_correct ? zreadintr (fd, &c, 1) : zread (fd, &c, 1); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 581 | else |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 582 | retval = posixly_correct ? zreadcintr (fd, &c) : zreadc (fd, &c); |
| 583 | reading = 0; |
| 584 | #if 0 |
| 585 | if (posixly_correct == 0) |
| 586 | interrupt_immediately--; |
| 587 | #endif |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 588 | |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 589 | if (retval <= 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 590 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 591 | if (retval < 0 && errno == EINTR) |
| 592 | { |
| 593 | lastsig = LASTSIG(); |
| 594 | if (lastsig == 0) |
| 595 | lastsig = trapped_signal_received; |
| 596 | run_pending_traps (); /* because interrupt_immediately is not set */ |
| 597 | } |
| 598 | else |
| 599 | lastsig = 0; |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 600 | if (terminating_signal && tty_modified) |
| 601 | ttyrestore (&termsave); /* fix terminal before exiting */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 602 | CHECK_TERMSIG; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 603 | eof = 1; |
| 604 | break; |
| 605 | } |
| 606 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 607 | CHECK_ALRM; |
| 608 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 609 | #if defined (READLINE) |
| 610 | } |
| 611 | #endif |
| 612 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 613 | CHECK_ALRM; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 614 | if (i + 4 >= size) /* XXX was i + 2; use i + 4 for multibyte/read_mbchar */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 615 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 616 | char *t; |
| 617 | t = (char *)xrealloc (input_string, size += 128); |
| 618 | |
| 619 | /* Only need to change unwind-protect if input_string changes */ |
| 620 | if (t != input_string) |
| 621 | { |
| 622 | input_string = t; |
| 623 | remove_unwind_protect (); |
| 624 | add_unwind_protect (xfree, input_string); |
| 625 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 626 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 627 | |
| 628 | /* If the next character is to be accepted verbatim, a backslash |
| 629 | newline pair still disappears from the input. */ |
| 630 | if (pass_next) |
| 631 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 632 | pass_next = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 633 | if (c == '\n') |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 634 | { |
| 635 | i--; /* back up over the CTLESC */ |
| 636 | if (interactive && input_is_tty && raw == 0) |
| 637 | print_ps2 = 1; |
| 638 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 639 | else |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 640 | goto add_char; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 641 | continue; |
| 642 | } |
| 643 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 644 | /* This may cause problems if IFS contains CTLESC */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 645 | if (c == '\\' && raw == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 646 | { |
| 647 | pass_next++; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 648 | if (skip_ctlesc == 0) |
| 649 | { |
| 650 | saw_escape++; |
| 651 | input_string[i++] = CTLESC; |
| 652 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 653 | continue; |
| 654 | } |
| 655 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 656 | if (ignore_delim == 0 && (unsigned char)c == delim) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 657 | break; |
| 658 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 659 | if (c == '\0' && delim != '\0') |
| 660 | continue; /* skip NUL bytes in input */ |
| 661 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 662 | if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 663 | { |
| 664 | saw_escape++; |
| 665 | input_string[i++] = CTLESC; |
| 666 | } |
| 667 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 668 | add_char: |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 669 | input_string[i++] = c; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 670 | CHECK_ALRM; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 671 | |
| 672 | #if defined (HANDLE_MULTIBYTE) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 673 | if (nchars > 0 && MB_CUR_MAX > 1 && is_basic (c) == 0) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 674 | { |
| 675 | input_string[i] = '\0'; /* for simplicity and debugging */ |
| 676 | i += read_mbchar (fd, input_string, i, c, unbuffered_read); |
| 677 | } |
| 678 | #endif |
| 679 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 680 | nr++; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 681 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 682 | if (nchars > 0 && nr >= nchars) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 683 | break; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 684 | } |
| 685 | input_string[i] = '\0'; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 686 | CHECK_ALRM; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 687 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 688 | if (retval < 0) |
| 689 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 690 | t_errno = errno; |
| 691 | if (errno != EINTR) |
| 692 | builtin_error (_("read error: %d: %s"), fd, strerror (errno)); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 693 | run_unwind_frame ("read_builtin"); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 694 | return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 695 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 696 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 697 | if (tmsec > 0 || tmusec > 0) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 698 | reset_alarm (); |
| 699 | |
| 700 | if (nchars > 0 || delim != '\n') |
| 701 | { |
| 702 | #if defined (READLINE) |
| 703 | if (edit) |
| 704 | { |
| 705 | if (nchars > 0) |
| 706 | rl_num_chars_to_read = 0; |
| 707 | if (delim != '\n') |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 708 | reset_eol_delim ((char *)NULL); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 709 | } |
| 710 | else |
| 711 | #endif |
| 712 | if (input_is_tty) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 713 | ttyrestore (&termsave); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 714 | } |
| 715 | else if (silent) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 716 | ttyrestore (&termsave); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 717 | |
| 718 | if (unbuffered_read == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 719 | zsyncfd (fd); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 720 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 721 | discard_unwind_frame ("read_builtin"); |
| 722 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 723 | retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 724 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 725 | assign_vars: |
| 726 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 727 | #if defined (ARRAY_VARS) |
| 728 | /* If -a was given, take the string read, break it into a list of words, |
| 729 | an assign them to `arrayname' in turn. */ |
| 730 | if (arrayname) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 731 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 732 | if (legal_identifier (arrayname) == 0) |
| 733 | { |
| 734 | sh_invalidid (arrayname); |
| 735 | xfree (input_string); |
| 736 | return (EXECUTION_FAILURE); |
| 737 | } |
| 738 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 739 | var = find_or_make_array_variable (arrayname, 1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 740 | if (var == 0) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 741 | { |
| 742 | xfree (input_string); |
| 743 | return EXECUTION_FAILURE; /* readonly or noassign */ |
| 744 | } |
Chet Ramey | b4d4016 | 2011-11-22 20:03:09 -0500 | [diff] [blame] | 745 | if (assoc_p (var)) |
| 746 | { |
| 747 | builtin_error (_("%s: cannot convert associative to indexed array"), arrayname); |
| 748 | xfree (input_string); |
| 749 | return EXECUTION_FAILURE; /* existing associative array */ |
| 750 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 751 | else if (invisible_p (var)) |
| 752 | VUNSETATTR (var, att_invisible); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 753 | array_flush (array_cell (var)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 754 | |
| 755 | alist = list_string (input_string, ifs_chars, 0); |
| 756 | if (alist) |
| 757 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 758 | if (saw_escape) |
| 759 | dequote_list (alist); |
| 760 | else |
| 761 | word_list_remove_quoted_nulls (alist); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 762 | assign_array_var_from_word_list (var, alist, 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 763 | dispose_words (alist); |
| 764 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 765 | xfree (input_string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 766 | return (retval); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 767 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 768 | #endif /* ARRAY_VARS */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 769 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 770 | /* If there are no variables, save the text of the line read to the |
| 771 | variable $REPLY. ksh93 strips leading and trailing IFS whitespace, |
| 772 | so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the |
| 773 | same way, but I believe that the difference in behaviors is useful |
| 774 | enough to not do it. Without the bash behavior, there is no way |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 775 | to read a line completely without interpretation or modification |
| 776 | unless you mess with $IFS (e.g., setting it to the empty string). |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 777 | If you disagree, change the occurrences of `#if 0' to `#if 1' below. */ |
| 778 | if (list == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 779 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 780 | #if 0 |
| 781 | orig_input_string = input_string; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 782 | for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 783 | ; |
| 784 | input_string = t; |
| 785 | input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape); |
| 786 | #endif |
| 787 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 788 | if (saw_escape) |
| 789 | { |
| 790 | t = dequote_string (input_string); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 791 | var = bind_variable ("REPLY", t, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 792 | free (t); |
| 793 | } |
| 794 | else |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 795 | var = bind_variable ("REPLY", input_string, 0); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 796 | VUNSETATTR (var, att_invisible); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 797 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 798 | xfree (input_string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 799 | return (retval); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 800 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 801 | |
| 802 | /* This code implements the Posix.2 spec for splitting the words |
| 803 | read and assigning them to variables. */ |
| 804 | orig_input_string = input_string; |
| 805 | |
| 806 | /* Remove IFS white space at the beginning of the input string. If |
| 807 | $IFS is null, no field splitting is performed. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 808 | for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 809 | ; |
| 810 | input_string = t; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 811 | for (; list->next; list = list->next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 812 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 813 | varname = list->word->word; |
| 814 | #if defined (ARRAY_VARS) |
| 815 | if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0) |
| 816 | #else |
| 817 | if (legal_identifier (varname) == 0) |
| 818 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 819 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 820 | sh_invalidid (varname); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 821 | xfree (orig_input_string); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 822 | return (EXECUTION_FAILURE); |
| 823 | } |
| 824 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 825 | /* If there are more variables than words read from the input, |
| 826 | the remaining variables are set to the empty string. */ |
| 827 | if (*input_string) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 828 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 829 | /* This call updates INPUT_STRING. */ |
| 830 | t = get_word_from_string (&input_string, ifs_chars, &e); |
| 831 | if (t) |
| 832 | *e = '\0'; |
| 833 | /* Don't bother to remove the CTLESC unless we added one |
| 834 | somewhere while reading the string. */ |
| 835 | if (t && saw_escape) |
| 836 | { |
| 837 | t1 = dequote_string (t); |
| 838 | var = bind_read_variable (varname, t1); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 839 | xfree (t1); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 840 | } |
| 841 | else |
Chet Ramey | 952a575 | 2012-03-13 15:11:02 -0400 | [diff] [blame] | 842 | var = bind_read_variable (varname, t ? t : ""); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 843 | } |
| 844 | else |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 845 | { |
| 846 | t = (char *)0; |
| 847 | var = bind_read_variable (varname, ""); |
| 848 | } |
| 849 | |
| 850 | FREE (t); |
| 851 | if (var == 0) |
| 852 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 853 | xfree (orig_input_string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 854 | return (EXECUTION_FAILURE); |
| 855 | } |
| 856 | |
| 857 | stupidly_hack_special_variables (varname); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 858 | VUNSETATTR (var, att_invisible); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 861 | /* Now assign the rest of the line to the last variable argument. */ |
| 862 | #if defined (ARRAY_VARS) |
| 863 | if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0) |
| 864 | #else |
| 865 | if (legal_identifier (list->word->word) == 0) |
| 866 | #endif |
| 867 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 868 | sh_invalidid (list->word->word); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 869 | xfree (orig_input_string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 870 | return (EXECUTION_FAILURE); |
| 871 | } |
| 872 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 873 | #if 0 |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 874 | /* This has to be done this way rather than using string_list |
| 875 | and list_string because Posix.2 says that the last variable gets the |
| 876 | remaining words and their intervening separators. */ |
| 877 | input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 878 | #else |
| 879 | /* Check whether or not the number of fields is exactly the same as the |
| 880 | number of variables. */ |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 881 | tofree = NULL; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 882 | if (*input_string) |
| 883 | { |
| 884 | t1 = input_string; |
| 885 | t = get_word_from_string (&input_string, ifs_chars, &e); |
| 886 | if (*input_string == 0) |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 887 | tofree = input_string = t; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 888 | else |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 889 | { |
| 890 | input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape); |
| 891 | tofree = t; |
| 892 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 893 | } |
| 894 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 895 | |
Chet Ramey | 0213a83 | 2012-11-02 09:58:59 -0400 | [diff] [blame] | 896 | if (saw_escape && input_string && *input_string) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 897 | { |
| 898 | t = dequote_string (input_string); |
| 899 | var = bind_read_variable (list->word->word, t); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 900 | xfree (t); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 901 | } |
| 902 | else |
Chet Ramey | 952a575 | 2012-03-13 15:11:02 -0400 | [diff] [blame] | 903 | var = bind_read_variable (list->word->word, input_string ? input_string : ""); |
Jari Aalto | f1be666 | 2008-11-18 13:15:12 +0000 | [diff] [blame] | 904 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 905 | if (var) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 906 | { |
| 907 | stupidly_hack_special_variables (list->word->word); |
| 908 | VUNSETATTR (var, att_invisible); |
| 909 | } |
| 910 | else |
| 911 | retval = EXECUTION_FAILURE; |
| 912 | |
| 913 | FREE (tofree); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 914 | xfree (orig_input_string); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 915 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 916 | return (retval); |
| 917 | } |
| 918 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 919 | static SHELL_VAR * |
| 920 | bind_read_variable (name, value) |
| 921 | char *name, *value; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 922 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 923 | SHELL_VAR *v; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 924 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 925 | #if defined (ARRAY_VARS) |
| 926 | if (valid_array_reference (name) == 0) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 927 | v = bind_variable (name, value, 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 928 | else |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 929 | v = assign_array_element (name, value, 0); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 930 | #else /* !ARRAY_VARS */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 931 | v = bind_variable (name, value, 0); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 932 | #endif /* !ARRAY_VARS */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 933 | return (v == 0 ? v |
| 934 | : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 935 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 936 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 937 | #if defined (HANDLE_MULTIBYTE) |
| 938 | static int |
| 939 | read_mbchar (fd, string, ind, ch, unbuffered) |
| 940 | int fd; |
| 941 | char *string; |
| 942 | int ind, ch, unbuffered; |
| 943 | { |
| 944 | char mbchar[MB_LEN_MAX + 1]; |
| 945 | int i, n, r; |
| 946 | char c; |
| 947 | size_t ret; |
| 948 | mbstate_t ps, ps_back; |
| 949 | wchar_t wc; |
| 950 | |
| 951 | memset (&ps, '\0', sizeof (mbstate_t)); |
| 952 | memset (&ps_back, '\0', sizeof (mbstate_t)); |
| 953 | |
| 954 | mbchar[0] = ch; |
| 955 | i = 1; |
| 956 | for (n = 0; n <= MB_LEN_MAX; n++) |
| 957 | { |
| 958 | ps_back = ps; |
| 959 | ret = mbrtowc (&wc, mbchar, i, &ps); |
| 960 | if (ret == (size_t)-2) |
| 961 | { |
| 962 | ps = ps_back; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 963 | /* We don't want to be interrupted during a multibyte char read */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 964 | if (unbuffered) |
| 965 | r = zread (fd, &c, 1); |
| 966 | else |
| 967 | r = zreadc (fd, &c); |
| 968 | if (r < 0) |
| 969 | goto mbchar_return; |
| 970 | mbchar[i++] = c; |
| 971 | continue; |
| 972 | } |
| 973 | else if (ret == (size_t)-1 || ret == (size_t)0 || ret > (size_t)0) |
| 974 | break; |
| 975 | } |
| 976 | |
| 977 | mbchar_return: |
| 978 | if (i > 1) /* read a multibyte char */ |
| 979 | /* mbchar[0] is already string[ind-1] */ |
| 980 | for (r = 1; r < i; r++) |
| 981 | string[ind+r-1] = mbchar[r]; |
| 982 | return i - 1; |
| 983 | } |
| 984 | #endif |
| 985 | |
| 986 | |
| 987 | static void |
| 988 | ttyrestore (ttp) |
| 989 | struct ttsave *ttp; |
| 990 | { |
| 991 | ttsetattr (ttp->fd, ttp->attrs); |
Chet Ramey | bbc8b48 | 2015-01-15 10:21:08 -0500 | [diff] [blame] | 992 | tty_modified = 0; |
| 993 | } |
| 994 | |
| 995 | void |
| 996 | read_tty_cleanup () |
| 997 | { |
| 998 | if (tty_modified) |
| 999 | ttyrestore (&termsave); |
| 1000 | } |
| 1001 | |
| 1002 | int |
| 1003 | read_tty_modified () |
| 1004 | { |
| 1005 | return (tty_modified); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1008 | #if defined (READLINE) |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1009 | static rl_completion_func_t *old_attempted_completion_function = 0; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1010 | static rl_hook_func_t *old_startup_hook; |
| 1011 | static char *deftext; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1012 | |
| 1013 | static void |
| 1014 | reset_attempted_completion_function (cp) |
| 1015 | char *cp; |
| 1016 | { |
| 1017 | if (rl_attempted_completion_function == 0 && old_attempted_completion_function) |
| 1018 | rl_attempted_completion_function = old_attempted_completion_function; |
| 1019 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1020 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1021 | static int |
| 1022 | set_itext () |
| 1023 | { |
| 1024 | int r1, r2; |
| 1025 | |
| 1026 | r1 = r2 = 0; |
| 1027 | if (old_startup_hook) |
| 1028 | r1 = (*old_startup_hook) (); |
| 1029 | if (deftext) |
| 1030 | { |
| 1031 | r2 = rl_insert_text (deftext); |
| 1032 | deftext = (char *)NULL; |
| 1033 | rl_startup_hook = old_startup_hook; |
| 1034 | old_startup_hook = (rl_hook_func_t *)NULL; |
| 1035 | } |
| 1036 | return (r1 || r2); |
| 1037 | } |
| 1038 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1039 | static char * |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1040 | edit_line (p, itext) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1041 | char *p; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1042 | char *itext; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1043 | { |
| 1044 | char *ret; |
| 1045 | int len; |
| 1046 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 1047 | if (bash_readline_initialized == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1048 | initialize_readline (); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1049 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1050 | old_attempted_completion_function = rl_attempted_completion_function; |
| 1051 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
Chet Ramey | 9b86eb2 | 2014-08-01 15:30:33 -0400 | [diff] [blame] | 1052 | bashline_set_event_hook (); |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1053 | if (itext) |
| 1054 | { |
| 1055 | old_startup_hook = rl_startup_hook; |
| 1056 | rl_startup_hook = set_itext; |
| 1057 | deftext = itext; |
| 1058 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1059 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1060 | ret = readline (p); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1061 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 1062 | rl_attempted_completion_function = old_attempted_completion_function; |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1063 | old_attempted_completion_function = (rl_completion_func_t *)NULL; |
Chet Ramey | 9b86eb2 | 2014-08-01 15:30:33 -0400 | [diff] [blame] | 1064 | bashline_reset_event_hook (); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 1065 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1066 | if (ret == 0) |
| 1067 | return ret; |
| 1068 | len = strlen (ret); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1069 | ret = (char *)xrealloc (ret, len + 2); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1070 | ret[len++] = delim; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1071 | ret[len] = '\0'; |
| 1072 | return ret; |
| 1073 | } |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1074 | |
| 1075 | static int old_delim_ctype; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1076 | static rl_command_func_t *old_delim_func; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1077 | static int old_newline_ctype; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1078 | static rl_command_func_t *old_newline_func; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1079 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1080 | static unsigned char delim_char; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1081 | |
| 1082 | static void |
| 1083 | set_eol_delim (c) |
| 1084 | int c; |
| 1085 | { |
| 1086 | Keymap cmap; |
| 1087 | |
| 1088 | if (bash_readline_initialized == 0) |
| 1089 | initialize_readline (); |
| 1090 | cmap = rl_get_keymap (); |
| 1091 | |
| 1092 | /* Change newline to self-insert */ |
| 1093 | old_newline_ctype = cmap[RETURN].type; |
| 1094 | old_newline_func = cmap[RETURN].function; |
| 1095 | cmap[RETURN].type = ISFUNC; |
| 1096 | cmap[RETURN].function = rl_insert; |
| 1097 | |
| 1098 | /* Bind the delimiter character to accept-line. */ |
| 1099 | old_delim_ctype = cmap[c].type; |
| 1100 | old_delim_func = cmap[c].function; |
| 1101 | cmap[c].type = ISFUNC; |
| 1102 | cmap[c].function = rl_newline; |
| 1103 | |
| 1104 | delim_char = c; |
| 1105 | } |
| 1106 | |
| 1107 | static void |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 1108 | reset_eol_delim (cp) |
| 1109 | char *cp; |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 1110 | { |
| 1111 | Keymap cmap; |
| 1112 | |
| 1113 | cmap = rl_get_keymap (); |
| 1114 | |
| 1115 | cmap[RETURN].type = old_newline_ctype; |
| 1116 | cmap[RETURN].function = old_newline_func; |
| 1117 | |
| 1118 | cmap[delim_char].type = old_delim_ctype; |
| 1119 | cmap[delim_char].function = old_delim_func; |
| 1120 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1121 | #endif |