blob: 84cb218df4c4dc674b5ba55fcf3e19a2a8812a45 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001This file is read.def, from which is created read.c.
2It implements the builtin "read" in Bash.
3
Chet Rameyac50fba2014-02-26 09:36:43 -05004Copyright (C) 1987-2012 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
6This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
Jari Aalto726f6381996-08-26 18:22:31 +000020
21$PRODUCES read.c
22
23$BUILTIN read
24$FUNCTION read_builtin
Chet Ramey00018032011-11-21 20:51:19 -050025$SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
Jari Aalto31859422009-01-12 13:36:28 +000026Read a line from the standard input and split it into fields.
Jari Aaltobb706242000-03-17 21:46:59 +000027
Jari Aalto31859422009-01-12 13:36:28 +000028Reads a single line from the standard input, or from file descriptor FD
29if the -u option is supplied. The line is split into fields as with word
30splitting, and the first word is assigned to the first NAME, the second
31word to the second NAME, and so on, with any leftover words assigned to
32the last NAME. Only the characters found in $IFS are recognized as word
33delimiters.
34
35If no NAMEs are supplied, the line read is stored in the REPLY variable.
36
37Options:
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 Ramey00018032011-11-21 20:51:19 -050045 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 Aalto31859422009-01-12 13:36:28 +000049 -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 Rameyac50fba2014-02-26 09:36:43 -050054 not read within TIMEOUT seconds. The value of the TMOUT
Jari Aalto31859422009-01-12 13:36:28 +000055 variable is the default timeout. TIMEOUT may be a
Chet Rameyac50fba2014-02-26 09:36:43 -050056 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 Aalto31859422009-01-12 13:36:28 +000059 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
62Exit Status:
Chet Rameyac50fba2014-02-26 09:36:43 -050063The 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 Aalto31859422009-01-12 13:36:28 +000065or an invalid file descriptor is supplied as the argument to -u.
Jari Aalto726f6381996-08-26 18:22:31 +000066$END
67
Jari Aaltoccc6cda1996-12-23 17:02:34 +000068#include <config.h>
69
Jari Aaltobb706242000-03-17 21:46:59 +000070#include "bashtypes.h"
71#include "posixstat.h"
72
Jari Aalto726f6381996-08-26 18:22:31 +000073#include <stdio.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000074
Jari Aalto31859422009-01-12 13:36:28 +000075#include "bashansi.h"
76
Jari Aaltoccc6cda1996-12-23 17:02:34 +000077#if defined (HAVE_UNISTD_H)
78# include <unistd.h>
79#endif
80
Jari Aaltobb706242000-03-17 21:46:59 +000081#include <signal.h>
Jari Aaltoe8ce7751997-09-22 20:22:27 +000082#include <errno.h>
83
Jari Aalto28ef6c32001-04-06 19:14:31 +000084#ifdef __CYGWIN__
85# include <fcntl.h>
86# include <io.h>
87#endif
88
Jari Aaltob80f6442004-07-27 13:29:18 +000089#include "../bashintl.h"
90
Jari Aalto726f6381996-08-26 18:22:31 +000091#include "../shell.h"
92#include "common.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000093#include "bashgetopt.h"
Jari Aalto726f6381996-08-26 18:22:31 +000094
Jari Aaltobb706242000-03-17 21:46:59 +000095#include <shtty.h>
96
Jari Aaltoccc6cda1996-12-23 17:02:34 +000097#if defined (READLINE)
98#include "../bashline.h"
99#include <readline/readline.h>
100#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000101
Jari Aalto95732b42005-12-07 14:08:12 +0000102#if defined (BUFFERED_INPUT)
103# include "input.h"
104#endif
105
Chet Rameyac50fba2014-02-26 09:36:43 -0500106#include "shmbutil.h"
107
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000108#if !defined(errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +0100109#include <errno.h>
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000110#endif
111
Chet Rameyac50fba2014-02-26 09:36:43 -0500112extern void run_pending_traps __P((void));
113
114extern int posixly_correct;
115extern int trapped_signal_received;
116
Jari Aalto31859422009-01-12 13:36:28 +0000117struct ttsave
118{
119 int fd;
120 TTYSTRUCT *attrs;
121};
122
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000123#if defined (READLINE)
Jari Aalto06285672006-10-10 14:15:34 +0000124static void reset_attempted_completion_function __P((char *));
Jari Aalto31859422009-01-12 13:36:28 +0000125static int set_itext __P((void));
126static char *edit_line __P((char *, char *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000127static void set_eol_delim __P((int));
128static void reset_eol_delim __P((char *));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000129#endif
Jari Aaltof73dda02001-11-13 17:56:06 +0000130static SHELL_VAR *bind_read_variable __P((char *, char *));
Jari Aalto31859422009-01-12 13:36:28 +0000131#if defined (HANDLE_MULTIBYTE)
132static int read_mbchar __P((int, char *, int, int, int));
133#endif
134static void ttyrestore __P((struct ttsave *));
Jari Aaltof73dda02001-11-13 17:56:06 +0000135
136static sighandler sigalrm __P((int));
137static void reset_alarm __P((void));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000138
Chet Rameyac50fba2014-02-26 09:36:43 -0500139/* Try this to see what the rest of the shell can do with the information. */
140procenv_t alrmbuf;
141int sigalrm_seen;
142
Chet Rameybbc8b482015-01-15 10:21:08 -0500143static int reading, tty_modified;
Jari Aaltobb706242000-03-17 21:46:59 +0000144static SigHandler *old_alrm;
Jari Aaltof73dda02001-11-13 17:56:06 +0000145static unsigned char delim;
Jari Aaltobb706242000-03-17 21:46:59 +0000146
Chet Rameybbc8b482015-01-15 10:21:08 -0500147static struct ttsave termsave;
148
Chet Rameyac50fba2014-02-26 09:36:43 -0500149/* 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 Aaltobb706242000-03-17 21:46:59 +0000156static sighandler
157sigalrm (s)
158 int s;
159{
Chet Rameyac50fba2014-02-26 09:36:43 -0500160 sigalrm_seen = 1;
Jari Aaltobb706242000-03-17 21:46:59 +0000161}
162
163static void
164reset_alarm ()
165{
166 set_signal_handler (SIGALRM, old_alrm);
Jari Aalto31859422009-01-12 13:36:28 +0000167 falarm (0, 0);
Jari Aaltobb706242000-03-17 21:46:59 +0000168}
169
Jari Aalto726f6381996-08-26 18:22:31 +0000170/* 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 Aaltoccc6cda1996-12-23 17:02:34 +0000175 are mentioned in LIST, then the default variable is $REPLY. */
176int
Jari Aalto726f6381996-08-26 18:22:31 +0000177read_builtin (list)
178 WORD_LIST *list;
179{
180 register char *varname;
Jari Aaltof1be6662008-11-18 13:15:12 +0000181 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 Rameyac50fba2014-02-26 09:36:43 -0500183 int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno;
Jari Aalto31859422009-01-12 13:36:28 +0000184 unsigned int tmsec, tmusec;
185 long ival, uval;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000186 intmax_t intval;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000187 char c;
188 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
Jari Aaltof1be6662008-11-18 13:15:12 +0000189 char *e, *t, *t1, *ps2, *tofree;
Jari Aaltobb706242000-03-17 21:46:59 +0000190 struct stat tsb;
Jari Aalto726f6381996-08-26 18:22:31 +0000191 SHELL_VAR *var;
Jari Aalto31859422009-01-12 13:36:28 +0000192 TTYSTRUCT ttattrs, ttset;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000193#if defined (ARRAY_VARS)
194 WORD_LIST *alist;
195#endif
196#if defined (READLINE)
Jari Aalto31859422009-01-12 13:36:28 +0000197 char *rlbuf, *itext;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000198 int rlind;
199#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000200
Jari Aaltof73dda02001-11-13 17:56:06 +0000201 USE_VAR(size);
202 USE_VAR(i);
203 USE_VAR(pass_next);
Jari Aaltof1be6662008-11-18 13:15:12 +0000204 USE_VAR(print_ps2);
Jari Aaltof73dda02001-11-13 17:56:06 +0000205 USE_VAR(saw_escape);
206 USE_VAR(input_is_pipe);
207/* USE_VAR(raw); */
208 USE_VAR(edit);
Jari Aalto31859422009-01-12 13:36:28 +0000209 USE_VAR(tmsec);
210 USE_VAR(tmusec);
Jari Aaltof73dda02001-11-13 17:56:06 +0000211 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 Aalto31859422009-01-12 13:36:28 +0000219 USE_VAR(itext);
Jari Aaltof73dda02001-11-13 17:56:06 +0000220#endif
221 USE_VAR(list);
Jari Aaltof1be6662008-11-18 13:15:12 +0000222 USE_VAR(ps2);
Chet Rameyac50fba2014-02-26 09:36:43 -0500223 USE_VAR(lastsig);
224
Chet Rameybbc8b482015-01-15 10:21:08 -0500225 sigalrm_seen = reading = tty_modified = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +0000226
Jari Aalto726f6381996-08-26 18:22:31 +0000227 i = 0; /* Index into the string that we are reading. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000228 raw = edit = 0; /* Not reading raw input by default. */
Jari Aaltobb706242000-03-17 21:46:59 +0000229 silent = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000230 arrayname = prompt = (char *)NULL;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000231 fd = 0; /* file descriptor to read from */
Jari Aalto726f6381996-08-26 18:22:31 +0000232
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000233#if defined (READLINE)
Jari Aalto31859422009-01-12 13:36:28 +0000234 rlbuf = itext = (char *)0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000235 rlind = 0;
236#endif
237
Jari Aalto31859422009-01-12 13:36:28 +0000238 tmsec = tmusec = 0; /* no timeout */
Jari Aalto95732b42005-12-07 14:08:12 +0000239 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000240 delim = '\n'; /* read until newline */
Chet Ramey00018032011-11-21 20:51:19 -0500241 ignore_delim = 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000242
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000243 reset_internal_getopt ();
Chet Ramey00018032011-11-21 20:51:19 -0500244 while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1)
Jari Aalto726f6381996-08-26 18:22:31 +0000245 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000246 switch (opt)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000247 {
248 case 'r':
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000249 raw = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000250 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000251 case 'p':
252 prompt = list_optarg;
253 break;
Jari Aaltobb706242000-03-17 21:46:59 +0000254 case 's':
255 silent = 1;
256 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000257 case 'e':
258#if defined (READLINE)
259 edit = 1;
260#endif
261 break;
Jari Aalto31859422009-01-12 13:36:28 +0000262 case 'i':
263#if defined (READLINE)
264 itext = list_optarg;
265#endif
266 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000267#if defined (ARRAY_VARS)
268 case 'a':
269 arrayname = list_optarg;
270 break;
271#endif
Jari Aaltobb706242000-03-17 21:46:59 +0000272 case 't':
Jari Aalto31859422009-01-12 13:36:28 +0000273 code = uconvert (list_optarg, &ival, &uval);
274 if (code == 0 || ival < 0 || uval < 0)
Jari Aaltobb706242000-03-17 21:46:59 +0000275 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000276 builtin_error (_("%s: invalid timeout specification"), list_optarg);
Jari Aaltobb706242000-03-17 21:46:59 +0000277 return (EXECUTION_FAILURE);
278 }
279 else
Jari Aaltof73dda02001-11-13 17:56:06 +0000280 {
281 have_timeout = 1;
Jari Aalto31859422009-01-12 13:36:28 +0000282 tmsec = ival;
283 tmusec = uval;
Jari Aaltof73dda02001-11-13 17:56:06 +0000284 }
Jari Aaltobb706242000-03-17 21:46:59 +0000285 break;
Chet Ramey00018032011-11-21 20:51:19 -0500286 case 'N':
287 ignore_delim = 1;
288 delim = -1;
Jari Aaltobb706242000-03-17 21:46:59 +0000289 case 'n':
Jari Aalto7117c2d2002-07-17 14:10:11 +0000290 code = legal_number (list_optarg, &intval);
291 if (code == 0 || intval < 0 || intval != (int)intval)
Jari Aaltobb706242000-03-17 21:46:59 +0000292 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000293 sh_invalidnum (list_optarg);
Jari Aaltobb706242000-03-17 21:46:59 +0000294 return (EXECUTION_FAILURE);
295 }
296 else
Jari Aalto7117c2d2002-07-17 14:10:11 +0000297 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 Aaltob80f6442004-07-27 13:29:18 +0000303 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000304 return (EXECUTION_FAILURE);
305 }
306 else
307 fd = intval;
308 if (sh_validfd (fd) == 0)
309 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000310 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000311 return (EXECUTION_FAILURE);
312 }
Jari Aaltobb706242000-03-17 21:46:59 +0000313 break;
314 case 'd':
315 delim = *list_optarg;
316 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000317 default:
318 builtin_usage ();
Jari Aalto726f6381996-08-26 18:22:31 +0000319 return (EX_USAGE);
320 }
Jari Aalto726f6381996-08-26 18:22:31 +0000321 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000322 list = loptend;
Jari Aalto726f6381996-08-26 18:22:31 +0000323
Jari Aalto17345e52009-02-19 22:21:29 +0000324 /* `read -t 0 var' tests whether input is available with select/FIONREAD,
325 and fails if those are unavailable */
Jari Aalto31859422009-01-12 13:36:28 +0000326 if (have_timeout && tmsec == 0 && tmusec == 0)
327#if 0
Jari Aaltobb706242000-03-17 21:46:59 +0000328 return (EXECUTION_FAILURE);
Jari Aalto31859422009-01-12 13:36:28 +0000329#else
330 return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
331#endif
Jari Aaltobb706242000-03-17 21:46:59 +0000332
Chet Rameyac50fba2014-02-26 09:36:43 -0500333 /* 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 Ramey00018032011-11-21 20:51:19 -0500345 /* If we're asked to ignore the delimiter, make sure we do. */
346 if (ignore_delim)
347 delim = -1;
348
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000349 /* IF IFS is unset, we use the default of " \t\n". */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000350 ifs_chars = getifs ();
351 if (ifs_chars == 0) /* XXX - shouldn't happen */
352 ifs_chars = "";
Chet Ramey00018032011-11-21 20:51:19 -0500353 /* If we want to read exactly NCHARS chars, don't split on IFS */
354 if (ignore_delim)
355 ifs_chars = "";
Jari Aalto31859422009-01-12 13:36:28 +0000356 for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++)
357 skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL;
Jari Aalto726f6381996-08-26 18:22:31 +0000358
Jari Aalto7117c2d2002-07-17 14:10:11 +0000359 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
Jari Aalto31859422009-01-12 13:36:28 +0000360 input_string[0] = '\0';
Jari Aalto7117c2d2002-07-17 14:10:11 +0000361
362 /* $TMOUT, if set, is the default timeout for read. */
363 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
364 {
Jari Aalto31859422009-01-12 13:36:28 +0000365 code = uconvert (e, &ival, &uval);
366 if (code == 0 || ival < 0 || uval < 0)
367 tmsec = tmusec = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000368 else
Jari Aalto31859422009-01-12 13:36:28 +0000369 {
370 tmsec = ival;
371 tmusec = uval;
372 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000373 }
Jari Aalto726f6381996-08-26 18:22:31 +0000374
375 begin_unwind_frame ("read_builtin");
Jari Aalto726f6381996-08-26 18:22:31 +0000376
Jari Aalto95732b42005-12-07 14:08:12 +0000377#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 Aalto7117c2d2002-07-17 14:10:11 +0000382 input_is_tty = isatty (fd);
Jari Aaltobb706242000-03-17 21:46:59 +0000383 if (input_is_tty == 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000384#ifndef __CYGWIN__
Jari Aalto95732b42005-12-07 14:08:12 +0000385 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
Jari Aaltobb706242000-03-17 21:46:59 +0000386#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 Aaltoccc6cda1996-12-23 17:02:34 +0000391 terminal, turn them off. */
Jari Aaltobb706242000-03-17 21:46:59 +0000392 if ((prompt || edit || silent) && input_is_tty == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000393 {
394 prompt = (char *)NULL;
Jari Aalto31859422009-01-12 13:36:28 +0000395#if defined (READLINE)
396 itext = (char *)NULL;
397#endif
Jari Aaltobb706242000-03-17 21:46:59 +0000398 edit = silent = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000399 }
400
Jari Aalto7117c2d2002-07-17 14:10:11 +0000401#if defined (READLINE)
402 if (edit)
403 add_unwind_protect (xfree, rlbuf);
404#endif
405
Jari Aalto726f6381996-08-26 18:22:31 +0000406 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 Aalto31859422009-01-12 13:36:28 +0000409 if (tmsec > 0 || tmusec > 0)
Jari Aaltobb706242000-03-17 21:46:59 +0000410 {
411 /* Turn off the timeout if stdin is a regular file (e.g. from
412 input redirection). */
Jari Aalto7117c2d2002-07-17 14:10:11 +0000413 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
Jari Aalto31859422009-01-12 13:36:28 +0000414 tmsec = tmusec = 0;
Jari Aaltobb706242000-03-17 21:46:59 +0000415 }
416
Jari Aalto31859422009-01-12 13:36:28 +0000417 if (tmsec > 0 || tmusec > 0)
Jari Aaltobb706242000-03-17 21:46:59 +0000418 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500419 code = setjmp_nosigs (alrmbuf);
Jari Aaltobb706242000-03-17 21:46:59 +0000420 if (code)
421 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500422 sigalrm_seen = 0;
Chet Ramey89a92862011-11-21 20:49:12 -0500423 /* 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 Ramey43aebe92013-03-07 15:20:58 -0500425 so we have to save input_string temporarily, run the unwind-
Chet Rameyac50fba2014-02-26 09:36:43 -0500426 protects, then restore input_string so we can use it later */
427 orig_input_string = 0;
Jari Aalto31859422009-01-12 13:36:28 +0000428 input_string[i] = '\0'; /* make sure it's terminated */
Chet Ramey43aebe92013-03-07 15:20:58 -0500429 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 Ramey89a92862011-11-21 20:49:12 -0500439 retval = 128+SIGALRM;
Jari Aalto31859422009-01-12 13:36:28 +0000440 goto assign_vars;
Jari Aaltobb706242000-03-17 21:46:59 +0000441 }
Chet Rameybbc8b482015-01-15 10:21:08 -0500442 if (interactive_shell == 0)
443 initialize_terminating_signals ();
Jari Aaltobb706242000-03-17 21:46:59 +0000444 old_alrm = set_signal_handler (SIGALRM, sigalrm);
445 add_unwind_protect (reset_alarm, (char *)NULL);
Jari Aalto06285672006-10-10 14:15:34 +0000446#if defined (READLINE)
447 if (edit)
Chet Ramey9b86eb22014-08-01 15:30:33 -0400448 {
449 add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
450 add_unwind_protect (bashline_reset_event_hook, (char *)NULL);
451 }
Jari Aalto06285672006-10-10 14:15:34 +0000452#endif
Jari Aalto31859422009-01-12 13:36:28 +0000453 falarm (tmsec, tmusec);
Jari Aaltobb706242000-03-17 21:46:59 +0000454 }
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 Aalto28ef6c32001-04-06 19:14:31 +0000478 {
Jari Aalto31859422009-01-12 13:36:28 +0000479 /* ttsave() */
480 termsave.fd = fd;
481 ttgetattr (fd, &ttattrs);
482 termsave.attrs = &ttattrs;
483
484 ttset = ttattrs;
Jari Aalto17345e52009-02-19 22:21:29 +0000485 i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset);
486 if (i < 0)
487 sh_ttyerror (1);
Chet Rameybbc8b482015-01-15 10:21:08 -0500488 tty_modified = 1;
Jari Aalto31859422009-01-12 13:36:28 +0000489 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
Chet Rameybbc8b482015-01-15 10:21:08 -0500490 if (interactive_shell == 0)
491 initialize_terminating_signals ();
Jari Aaltobb706242000-03-17 21:46:59 +0000492 }
493 }
494 else if (silent) /* turn off echo but leave term in canonical mode */
495 {
Jari Aalto31859422009-01-12 13:36:28 +0000496 /* ttsave (); */
497 termsave.fd = fd;
498 ttgetattr (fd, &ttattrs);
499 termsave.attrs = &ttattrs;
500
501 ttset = ttattrs;
Jari Aalto17345e52009-02-19 22:21:29 +0000502 i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */
503 if (i < 0)
504 sh_ttyerror (1);
Jari Aalto31859422009-01-12 13:36:28 +0000505
Chet Rameybbc8b482015-01-15 10:21:08 -0500506 tty_modified = 1;
Jari Aalto31859422009-01-12 13:36:28 +0000507 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
Chet Rameybbc8b482015-01-15 10:21:08 -0500508 if (interactive_shell == 0)
509 initialize_terminating_signals ();
Jari Aaltobb706242000-03-17 21:46:59 +0000510 }
511
Jari Aalto28ef6c32001-04-06 19:14:31 +0000512 /* 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 Aalto28ef6c32001-04-06 19:14:31 +0000515
Chet Rameyac50fba2014-02-26 09:36:43 -0500516 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 Aaltobb706242000-03-17 21:46:59 +0000521
Jari Aalto31859422009-01-12 13:36:28 +0000522 if (prompt && edit == 0)
523 {
524 fprintf (stderr, "%s", prompt);
525 fflush (stderr);
526 }
527
Jari Aalto28ef6c32001-04-06 19:14:31 +0000528#if defined (__CYGWIN__) && defined (O_TEXT)
529 setmode (0, O_TEXT);
530#endif
531
Jari Aaltof1be6662008-11-18 13:15:12 +0000532 ps2 = 0;
533 for (print_ps2 = eof = retval = 0;;)
Jari Aalto726f6381996-08-26 18:22:31 +0000534 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500535 CHECK_ALRM;
536
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000537#if defined (READLINE)
538 if (edit)
539 {
540 if (rlbuf && rlbuf[rlind] == '\0')
541 {
Jari Aaltod166f041997-06-05 14:59:13 +0000542 xfree (rlbuf);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000543 rlbuf = (char *)0;
544 }
545 if (rlbuf == 0)
546 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500547 reading = 1;
Jari Aalto31859422009-01-12 13:36:28 +0000548 rlbuf = edit_line (prompt ? prompt : "", itext);
Chet Rameyac50fba2014-02-26 09:36:43 -0500549 reading = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000550 rlind = 0;
551 }
552 if (rlbuf == 0)
553 {
554 eof = 1;
555 break;
556 }
557 c = rlbuf[rlind++];
558 }
559 else
Jari Aaltob72432f1999-02-19 17:11:39 +0000560 {
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000561#endif
562
Jari Aaltof1be6662008-11-18 13:15:12 +0000563 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 Rameyac50fba2014-02-26 09:36:43 -0500572#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 Aaltobb706242000-03-17 21:46:59 +0000581 else
Chet Rameyac50fba2014-02-26 09:36:43 -0500582 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 Aaltobb706242000-03-17 21:46:59 +0000588
Jari Aaltoe8ce7751997-09-22 20:22:27 +0000589 if (retval <= 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000590 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500591 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 Rameybbc8b482015-01-15 10:21:08 -0500600 if (terminating_signal && tty_modified)
601 ttyrestore (&termsave); /* fix terminal before exiting */
Chet Rameyac50fba2014-02-26 09:36:43 -0500602 CHECK_TERMSIG;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000603 eof = 1;
604 break;
605 }
606
Chet Rameyac50fba2014-02-26 09:36:43 -0500607 CHECK_ALRM;
608
Jari Aaltob72432f1999-02-19 17:11:39 +0000609#if defined (READLINE)
610 }
611#endif
612
Chet Rameyac50fba2014-02-26 09:36:43 -0500613 CHECK_ALRM;
Jari Aalto31859422009-01-12 13:36:28 +0000614 if (i + 4 >= size) /* XXX was i + 2; use i + 4 for multibyte/read_mbchar */
Jari Aalto28ef6c32001-04-06 19:14:31 +0000615 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500616 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 Aalto28ef6c32001-04-06 19:14:31 +0000626 }
Jari Aalto726f6381996-08-26 18:22:31 +0000627
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 Aalto95732b42005-12-07 14:08:12 +0000632 pass_next = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000633 if (c == '\n')
Jari Aaltof1be6662008-11-18 13:15:12 +0000634 {
635 i--; /* back up over the CTLESC */
636 if (interactive && input_is_tty && raw == 0)
637 print_ps2 = 1;
638 }
Jari Aalto726f6381996-08-26 18:22:31 +0000639 else
Jari Aalto95732b42005-12-07 14:08:12 +0000640 goto add_char;
Jari Aalto726f6381996-08-26 18:22:31 +0000641 continue;
642 }
643
Jari Aalto31859422009-01-12 13:36:28 +0000644 /* This may cause problems if IFS contains CTLESC */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000645 if (c == '\\' && raw == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000646 {
647 pass_next++;
Jari Aalto31859422009-01-12 13:36:28 +0000648 if (skip_ctlesc == 0)
649 {
650 saw_escape++;
651 input_string[i++] = CTLESC;
652 }
Jari Aalto726f6381996-08-26 18:22:31 +0000653 continue;
654 }
655
Chet Rameyac50fba2014-02-26 09:36:43 -0500656 if (ignore_delim == 0 && (unsigned char)c == delim)
Jari Aalto726f6381996-08-26 18:22:31 +0000657 break;
658
Chet Rameyac50fba2014-02-26 09:36:43 -0500659 if (c == '\0' && delim != '\0')
660 continue; /* skip NUL bytes in input */
661
Jari Aalto31859422009-01-12 13:36:28 +0000662 if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL))
Jari Aalto726f6381996-08-26 18:22:31 +0000663 {
664 saw_escape++;
665 input_string[i++] = CTLESC;
666 }
667
Jari Aalto95732b42005-12-07 14:08:12 +0000668add_char:
Jari Aalto726f6381996-08-26 18:22:31 +0000669 input_string[i++] = c;
Chet Rameyac50fba2014-02-26 09:36:43 -0500670 CHECK_ALRM;
Jari Aalto31859422009-01-12 13:36:28 +0000671
672#if defined (HANDLE_MULTIBYTE)
Chet Rameyac50fba2014-02-26 09:36:43 -0500673 if (nchars > 0 && MB_CUR_MAX > 1 && is_basic (c) == 0)
Jari Aalto31859422009-01-12 13:36:28 +0000674 {
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 Aalto95732b42005-12-07 14:08:12 +0000680 nr++;
Jari Aaltobb706242000-03-17 21:46:59 +0000681
Jari Aalto95732b42005-12-07 14:08:12 +0000682 if (nchars > 0 && nr >= nchars)
Jari Aaltobb706242000-03-17 21:46:59 +0000683 break;
Jari Aalto726f6381996-08-26 18:22:31 +0000684 }
685 input_string[i] = '\0';
Chet Rameyac50fba2014-02-26 09:36:43 -0500686 CHECK_ALRM;
Jari Aalto726f6381996-08-26 18:22:31 +0000687
Jari Aalto7117c2d2002-07-17 14:10:11 +0000688 if (retval < 0)
689 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500690 t_errno = errno;
691 if (errno != EINTR)
692 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
Jari Aalto06285672006-10-10 14:15:34 +0000693 run_unwind_frame ("read_builtin");
Chet Rameyac50fba2014-02-26 09:36:43 -0500694 return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000695 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000696
Jari Aalto31859422009-01-12 13:36:28 +0000697 if (tmsec > 0 || tmusec > 0)
Jari Aaltobb706242000-03-17 21:46:59 +0000698 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 Aaltof73dda02001-11-13 17:56:06 +0000708 reset_eol_delim ((char *)NULL);
Jari Aaltobb706242000-03-17 21:46:59 +0000709 }
710 else
711#endif
712 if (input_is_tty)
Jari Aalto31859422009-01-12 13:36:28 +0000713 ttyrestore (&termsave);
Jari Aaltobb706242000-03-17 21:46:59 +0000714 }
715 else if (silent)
Jari Aalto31859422009-01-12 13:36:28 +0000716 ttyrestore (&termsave);
Jari Aaltobb706242000-03-17 21:46:59 +0000717
718 if (unbuffered_read == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000719 zsyncfd (fd);
Jari Aaltobb706242000-03-17 21:46:59 +0000720
Jari Aalto726f6381996-08-26 18:22:31 +0000721 discard_unwind_frame ("read_builtin");
722
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000723 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
Jari Aalto726f6381996-08-26 18:22:31 +0000724
Jari Aalto31859422009-01-12 13:36:28 +0000725assign_vars:
726
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000727#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 Aalto726f6381996-08-26 18:22:31 +0000731 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000732 if (legal_identifier (arrayname) == 0)
733 {
734 sh_invalidid (arrayname);
735 xfree (input_string);
736 return (EXECUTION_FAILURE);
737 }
738
Jari Aaltof73dda02001-11-13 17:56:06 +0000739 var = find_or_make_array_variable (arrayname, 1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000740 if (var == 0)
Jari Aalto06285672006-10-10 14:15:34 +0000741 {
742 xfree (input_string);
743 return EXECUTION_FAILURE; /* readonly or noassign */
744 }
Chet Rameyb4d40162011-11-22 20:03:09 -0500745 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 Rameyac50fba2014-02-26 09:36:43 -0500751 else if (invisible_p (var))
752 VUNSETATTR (var, att_invisible);
Jari Aalto7117c2d2002-07-17 14:10:11 +0000753 array_flush (array_cell (var));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000754
755 alist = list_string (input_string, ifs_chars, 0);
756 if (alist)
757 {
Jari Aalto06285672006-10-10 14:15:34 +0000758 if (saw_escape)
759 dequote_list (alist);
760 else
761 word_list_remove_quoted_nulls (alist);
Jari Aalto95732b42005-12-07 14:08:12 +0000762 assign_array_var_from_word_list (var, alist, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000763 dispose_words (alist);
764 }
Jari Aaltod166f041997-06-05 14:59:13 +0000765 xfree (input_string);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000766 return (retval);
Jari Aalto726f6381996-08-26 18:22:31 +0000767 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000768#endif /* ARRAY_VARS */
Jari Aalto726f6381996-08-26 18:22:31 +0000769
Jari Aaltod166f041997-06-05 14:59:13 +0000770 /* 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 Aalto28ef6c32001-04-06 19:14:31 +0000775 to read a line completely without interpretation or modification
776 unless you mess with $IFS (e.g., setting it to the empty string).
Jari Aaltod166f041997-06-05 14:59:13 +0000777 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
778 if (list == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000779 {
Jari Aaltod166f041997-06-05 14:59:13 +0000780#if 0
781 orig_input_string = input_string;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000782 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
Jari Aaltod166f041997-06-05 14:59:13 +0000783 ;
784 input_string = t;
785 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
786#endif
787
Jari Aalto726f6381996-08-26 18:22:31 +0000788 if (saw_escape)
789 {
790 t = dequote_string (input_string);
Jari Aalto95732b42005-12-07 14:08:12 +0000791 var = bind_variable ("REPLY", t, 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000792 free (t);
793 }
794 else
Jari Aalto95732b42005-12-07 14:08:12 +0000795 var = bind_variable ("REPLY", input_string, 0);
Jari Aaltobb706242000-03-17 21:46:59 +0000796 VUNSETATTR (var, att_invisible);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000797
Chet Rameyac50fba2014-02-26 09:36:43 -0500798 xfree (input_string);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000799 return (retval);
Jari Aalto726f6381996-08-26 18:22:31 +0000800 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000801
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 Aalto7117c2d2002-07-17 14:10:11 +0000808 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000809 ;
810 input_string = t;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000811 for (; list->next; list = list->next)
Jari Aalto726f6381996-08-26 18:22:31 +0000812 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000813 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 Aalto726f6381996-08-26 18:22:31 +0000819 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000820 sh_invalidid (varname);
Jari Aaltof73dda02001-11-13 17:56:06 +0000821 xfree (orig_input_string);
Jari Aalto726f6381996-08-26 18:22:31 +0000822 return (EXECUTION_FAILURE);
823 }
824
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000825 /* 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 Aalto726f6381996-08-26 18:22:31 +0000828 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000829 /* 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 Aaltof73dda02001-11-13 17:56:06 +0000839 xfree (t1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000840 }
841 else
Chet Ramey952a5752012-03-13 15:11:02 -0400842 var = bind_read_variable (varname, t ? t : "");
Jari Aalto726f6381996-08-26 18:22:31 +0000843 }
844 else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000845 {
846 t = (char *)0;
847 var = bind_read_variable (varname, "");
848 }
849
850 FREE (t);
851 if (var == 0)
852 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000853 xfree (orig_input_string);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000854 return (EXECUTION_FAILURE);
855 }
856
857 stupidly_hack_special_variables (varname);
Jari Aaltobb706242000-03-17 21:46:59 +0000858 VUNSETATTR (var, att_invisible);
Jari Aalto726f6381996-08-26 18:22:31 +0000859 }
860
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000861 /* 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 Aalto7117c2d2002-07-17 14:10:11 +0000868 sh_invalidid (list->word->word);
Jari Aaltof73dda02001-11-13 17:56:06 +0000869 xfree (orig_input_string);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000870 return (EXECUTION_FAILURE);
871 }
872
Jari Aalto95732b42005-12-07 14:08:12 +0000873#if 0
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000874 /* 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 Aalto95732b42005-12-07 14:08:12 +0000878#else
879 /* Check whether or not the number of fields is exactly the same as the
880 number of variables. */
Jari Aaltof1be6662008-11-18 13:15:12 +0000881 tofree = NULL;
Jari Aalto95732b42005-12-07 14:08:12 +0000882 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 Aaltof1be6662008-11-18 13:15:12 +0000887 tofree = input_string = t;
Jari Aalto95732b42005-12-07 14:08:12 +0000888 else
Chet Ramey89a92862011-11-21 20:49:12 -0500889 {
890 input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
891 tofree = t;
892 }
Jari Aalto95732b42005-12-07 14:08:12 +0000893 }
894#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000895
Chet Ramey0213a832012-11-02 09:58:59 -0400896 if (saw_escape && input_string && *input_string)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000897 {
898 t = dequote_string (input_string);
899 var = bind_read_variable (list->word->word, t);
Jari Aaltof73dda02001-11-13 17:56:06 +0000900 xfree (t);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000901 }
902 else
Chet Ramey952a5752012-03-13 15:11:02 -0400903 var = bind_read_variable (list->word->word, input_string ? input_string : "");
Jari Aaltof1be6662008-11-18 13:15:12 +0000904
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000905 if (var)
Chet Ramey495aee42011-11-22 19:11:26 -0500906 {
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 Aaltof73dda02001-11-13 17:56:06 +0000914 xfree (orig_input_string);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000915
Jari Aalto726f6381996-08-26 18:22:31 +0000916 return (retval);
917}
918
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000919static SHELL_VAR *
920bind_read_variable (name, value)
921 char *name, *value;
Jari Aalto726f6381996-08-26 18:22:31 +0000922{
Chet Ramey495aee42011-11-22 19:11:26 -0500923 SHELL_VAR *v;
Chet Rameyac50fba2014-02-26 09:36:43 -0500924
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000925#if defined (ARRAY_VARS)
926 if (valid_array_reference (name) == 0)
Chet Ramey495aee42011-11-22 19:11:26 -0500927 v = bind_variable (name, value, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000928 else
Chet Ramey495aee42011-11-22 19:11:26 -0500929 v = assign_array_element (name, value, 0);
Jari Aaltof73dda02001-11-13 17:56:06 +0000930#else /* !ARRAY_VARS */
Chet Ramey495aee42011-11-22 19:11:26 -0500931 v = bind_variable (name, value, 0);
Jari Aaltof73dda02001-11-13 17:56:06 +0000932#endif /* !ARRAY_VARS */
Chet Ramey495aee42011-11-22 19:11:26 -0500933 return (v == 0 ? v
934 : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v));
Jari Aalto726f6381996-08-26 18:22:31 +0000935}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000936
Jari Aalto31859422009-01-12 13:36:28 +0000937#if defined (HANDLE_MULTIBYTE)
938static int
939read_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 Rameyac50fba2014-02-26 09:36:43 -0500963 /* We don't want to be interrupted during a multibyte char read */
Jari Aalto31859422009-01-12 13:36:28 +0000964 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
977mbchar_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
987static void
988ttyrestore (ttp)
989 struct ttsave *ttp;
990{
991 ttsetattr (ttp->fd, ttp->attrs);
Chet Rameybbc8b482015-01-15 10:21:08 -0500992 tty_modified = 0;
993}
994
995void
996read_tty_cleanup ()
997{
998 if (tty_modified)
999 ttyrestore (&termsave);
1000}
1001
1002int
1003read_tty_modified ()
1004{
1005 return (tty_modified);
Jari Aalto31859422009-01-12 13:36:28 +00001006}
1007
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001008#if defined (READLINE)
Jari Aalto06285672006-10-10 14:15:34 +00001009static rl_completion_func_t *old_attempted_completion_function = 0;
Jari Aalto31859422009-01-12 13:36:28 +00001010static rl_hook_func_t *old_startup_hook;
1011static char *deftext;
Jari Aalto06285672006-10-10 14:15:34 +00001012
1013static void
1014reset_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 Aalto7117c2d2002-07-17 14:10:11 +00001020
Jari Aalto31859422009-01-12 13:36:28 +00001021static int
1022set_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 Aaltoccc6cda1996-12-23 17:02:34 +00001039static char *
Jari Aalto31859422009-01-12 13:36:28 +00001040edit_line (p, itext)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001041 char *p;
Jari Aalto31859422009-01-12 13:36:28 +00001042 char *itext;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001043{
1044 char *ret;
1045 int len;
1046
Jari Aalto95732b42005-12-07 14:08:12 +00001047 if (bash_readline_initialized == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001048 initialize_readline ();
Jari Aalto06285672006-10-10 14:15:34 +00001049
Jari Aalto7117c2d2002-07-17 14:10:11 +00001050 old_attempted_completion_function = rl_attempted_completion_function;
1051 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
Chet Ramey9b86eb22014-08-01 15:30:33 -04001052 bashline_set_event_hook ();
Jari Aalto31859422009-01-12 13:36:28 +00001053 if (itext)
1054 {
1055 old_startup_hook = rl_startup_hook;
1056 rl_startup_hook = set_itext;
1057 deftext = itext;
1058 }
Chet Rameyac50fba2014-02-26 09:36:43 -05001059
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001060 ret = readline (p);
Chet Rameyac50fba2014-02-26 09:36:43 -05001061
Jari Aalto7117c2d2002-07-17 14:10:11 +00001062 rl_attempted_completion_function = old_attempted_completion_function;
Jari Aalto06285672006-10-10 14:15:34 +00001063 old_attempted_completion_function = (rl_completion_func_t *)NULL;
Chet Ramey9b86eb22014-08-01 15:30:33 -04001064 bashline_reset_event_hook ();
Jari Aalto06285672006-10-10 14:15:34 +00001065
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001066 if (ret == 0)
1067 return ret;
1068 len = strlen (ret);
Jari Aaltof73dda02001-11-13 17:56:06 +00001069 ret = (char *)xrealloc (ret, len + 2);
Jari Aaltobb706242000-03-17 21:46:59 +00001070 ret[len++] = delim;
Jari Aaltoccc6cda1996-12-23 17:02:34 +00001071 ret[len] = '\0';
1072 return ret;
1073}
Jari Aaltobb706242000-03-17 21:46:59 +00001074
1075static int old_delim_ctype;
Jari Aaltof73dda02001-11-13 17:56:06 +00001076static rl_command_func_t *old_delim_func;
Jari Aaltobb706242000-03-17 21:46:59 +00001077static int old_newline_ctype;
Jari Aaltof73dda02001-11-13 17:56:06 +00001078static rl_command_func_t *old_newline_func;
Jari Aaltobb706242000-03-17 21:46:59 +00001079
Jari Aaltof73dda02001-11-13 17:56:06 +00001080static unsigned char delim_char;
Jari Aaltobb706242000-03-17 21:46:59 +00001081
1082static void
1083set_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
1107static void
Jari Aaltof73dda02001-11-13 17:56:06 +00001108reset_eol_delim (cp)
1109 char *cp;
Jari Aaltobb706242000-03-17 21:46:59 +00001110{
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 Aaltoccc6cda1996-12-23 17:02:34 +00001121#endif