]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/read.def
Bash-4.3 patch 39
[thirdparty/bash.git] / builtins / read.def
CommitLineData
726f6388
JA
1This file is read.def, from which is created read.c.
2It implements the builtin "read" in Bash.
3
ac50fbac 4Copyright (C) 1987-2012 Free Software Foundation, Inc.
726f6388
JA
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
3185942a
JA
8Bash 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.
726f6388 12
3185942a
JA
13Bash 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.
726f6388 17
3185942a
JA
18You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
726f6388
JA
20
21$PRODUCES read.c
22
23$BUILTIN read
24$FUNCTION read_builtin
0001803f 25$SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
3185942a
JA
26Read a line from the standard input and split it into fields.
27
28Reads 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
0001803f
CR
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
3185942a
JA
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
ac50fbac 54 not read within TIMEOUT seconds. The value of the TMOUT
3185942a 55 variable is the default timeout. TIMEOUT may be a
ac50fbac
CR
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
3185942a
JA
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
62Exit Status:
ac50fbac
CR
63The 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,
3185942a 65or an invalid file descriptor is supplied as the argument to -u.
726f6388
JA
66$END
67
ccc6cda3
JA
68#include <config.h>
69
bb70624e
JA
70#include "bashtypes.h"
71#include "posixstat.h"
72
726f6388 73#include <stdio.h>
ccc6cda3 74
3185942a
JA
75#include "bashansi.h"
76
ccc6cda3
JA
77#if defined (HAVE_UNISTD_H)
78# include <unistd.h>
79#endif
80
bb70624e 81#include <signal.h>
e8ce775d
JA
82#include <errno.h>
83
28ef6c31
JA
84#ifdef __CYGWIN__
85# include <fcntl.h>
86# include <io.h>
87#endif
88
b80f6443
JA
89#include "../bashintl.h"
90
726f6388
JA
91#include "../shell.h"
92#include "common.h"
ccc6cda3 93#include "bashgetopt.h"
726f6388 94
bb70624e
JA
95#include <shtty.h>
96
ccc6cda3
JA
97#if defined (READLINE)
98#include "../bashline.h"
99#include <readline/readline.h>
100#endif
726f6388 101
95732b49
JA
102#if defined (BUFFERED_INPUT)
103# include "input.h"
104#endif
105
ac50fbac
CR
106#include "shmbutil.h"
107
e8ce775d
JA
108#if !defined(errno)
109extern int errno;
110#endif
111
ac50fbac
CR
112extern void run_pending_traps __P((void));
113
114extern int posixly_correct;
115extern int trapped_signal_received;
116
3185942a
JA
117struct ttsave
118{
119 int fd;
120 TTYSTRUCT *attrs;
121};
122
ccc6cda3 123#if defined (READLINE)
0628567a 124static void reset_attempted_completion_function __P((char *));
3185942a
JA
125static int set_itext __P((void));
126static char *edit_line __P((char *, char *));
f73dda09
JA
127static void set_eol_delim __P((int));
128static void reset_eol_delim __P((char *));
ccc6cda3 129#endif
f73dda09 130static SHELL_VAR *bind_read_variable __P((char *, char *));
3185942a
JA
131#if defined (HANDLE_MULTIBYTE)
132static int read_mbchar __P((int, char *, int, int, int));
133#endif
134static void ttyrestore __P((struct ttsave *));
f73dda09
JA
135
136static sighandler sigalrm __P((int));
137static void reset_alarm __P((void));
ccc6cda3 138
ac50fbac
CR
139/* Try this to see what the rest of the shell can do with the information. */
140procenv_t alrmbuf;
141int sigalrm_seen;
142
84c617ec 143static int reading, tty_modified;
bb70624e 144static SigHandler *old_alrm;
f73dda09 145static unsigned char delim;
bb70624e 146
84c617ec
CR
147static struct ttsave termsave;
148
ac50fbac
CR
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. */
bb70624e
JA
156static sighandler
157sigalrm (s)
158 int s;
159{
ac50fbac 160 sigalrm_seen = 1;
bb70624e
JA
161}
162
163static void
164reset_alarm ()
165{
166 set_signal_handler (SIGALRM, old_alrm);
3185942a 167 falarm (0, 0);
bb70624e
JA
168}
169
726f6388
JA
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
ccc6cda3
JA
175 are mentioned in LIST, then the default variable is $REPLY. */
176int
726f6388
JA
177read_builtin (list)
178 WORD_LIST *list;
179{
180 register char *varname;
f1be666c
JA
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;
ac50fbac 183 int raw, edit, nchars, silent, have_timeout, ignore_delim, fd, lastsig, t_errno;
3185942a
JA
184 unsigned int tmsec, tmusec;
185 long ival, uval;
7117c2d2 186 intmax_t intval;
ccc6cda3
JA
187 char c;
188 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
f1be666c 189 char *e, *t, *t1, *ps2, *tofree;
bb70624e 190 struct stat tsb;
726f6388 191 SHELL_VAR *var;
3185942a 192 TTYSTRUCT ttattrs, ttset;
ccc6cda3
JA
193#if defined (ARRAY_VARS)
194 WORD_LIST *alist;
195#endif
196#if defined (READLINE)
3185942a 197 char *rlbuf, *itext;
ccc6cda3
JA
198 int rlind;
199#endif
726f6388 200
f73dda09
JA
201 USE_VAR(size);
202 USE_VAR(i);
203 USE_VAR(pass_next);
f1be666c 204 USE_VAR(print_ps2);
f73dda09
JA
205 USE_VAR(saw_escape);
206 USE_VAR(input_is_pipe);
207/* USE_VAR(raw); */
208 USE_VAR(edit);
3185942a
JA
209 USE_VAR(tmsec);
210 USE_VAR(tmusec);
f73dda09
JA
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);
3185942a 219 USE_VAR(itext);
f73dda09
JA
220#endif
221 USE_VAR(list);
f1be666c 222 USE_VAR(ps2);
ac50fbac
CR
223 USE_VAR(lastsig);
224
84c617ec 225 sigalrm_seen = reading = tty_modified = 0;
f73dda09 226
726f6388 227 i = 0; /* Index into the string that we are reading. */
ccc6cda3 228 raw = edit = 0; /* Not reading raw input by default. */
bb70624e 229 silent = 0;
ccc6cda3 230 arrayname = prompt = (char *)NULL;
7117c2d2 231 fd = 0; /* file descriptor to read from */
ccc6cda3
JA
232
233#if defined (READLINE)
3185942a 234 rlbuf = itext = (char *)0;
ccc6cda3
JA
235 rlind = 0;
236#endif
726f6388 237
3185942a 238 tmsec = tmusec = 0; /* no timeout */
95732b49 239 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
bb70624e 240 delim = '\n'; /* read until newline */
0001803f 241 ignore_delim = 0;
bb70624e 242
ccc6cda3 243 reset_internal_getopt ();
0001803f 244 while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1)
726f6388 245 {
ccc6cda3 246 switch (opt)
28ef6c31
JA
247 {
248 case 'r':
ccc6cda3 249 raw = 1;
726f6388 250 break;
ccc6cda3
JA
251 case 'p':
252 prompt = list_optarg;
253 break;
bb70624e
JA
254 case 's':
255 silent = 1;
256 break;
ccc6cda3
JA
257 case 'e':
258#if defined (READLINE)
259 edit = 1;
3185942a
JA
260#endif
261 break;
262 case 'i':
263#if defined (READLINE)
264 itext = list_optarg;
ccc6cda3
JA
265#endif
266 break;
267#if defined (ARRAY_VARS)
268 case 'a':
269 arrayname = list_optarg;
270 break;
271#endif
bb70624e 272 case 't':
3185942a
JA
273 code = uconvert (list_optarg, &ival, &uval);
274 if (code == 0 || ival < 0 || uval < 0)
bb70624e 275 {
b80f6443 276 builtin_error (_("%s: invalid timeout specification"), list_optarg);
bb70624e
JA
277 return (EXECUTION_FAILURE);
278 }
279 else
f73dda09
JA
280 {
281 have_timeout = 1;
3185942a
JA
282 tmsec = ival;
283 tmusec = uval;
f73dda09 284 }
bb70624e 285 break;
0001803f
CR
286 case 'N':
287 ignore_delim = 1;
288 delim = -1;
bb70624e 289 case 'n':
7117c2d2
JA
290 code = legal_number (list_optarg, &intval);
291 if (code == 0 || intval < 0 || intval != (int)intval)
bb70624e 292 {
7117c2d2 293 sh_invalidnum (list_optarg);
bb70624e
JA
294 return (EXECUTION_FAILURE);
295 }
296 else
7117c2d2
JA
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 {
b80f6443 303 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
7117c2d2
JA
304 return (EXECUTION_FAILURE);
305 }
306 else
307 fd = intval;
308 if (sh_validfd (fd) == 0)
309 {
b80f6443 310 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
7117c2d2
JA
311 return (EXECUTION_FAILURE);
312 }
bb70624e
JA
313 break;
314 case 'd':
315 delim = *list_optarg;
316 break;
ccc6cda3
JA
317 default:
318 builtin_usage ();
726f6388
JA
319 return (EX_USAGE);
320 }
726f6388 321 }
ccc6cda3 322 list = loptend;
726f6388 323
17345e5a
JA
324 /* `read -t 0 var' tests whether input is available with select/FIONREAD,
325 and fails if those are unavailable */
3185942a
JA
326 if (have_timeout && tmsec == 0 && tmusec == 0)
327#if 0
bb70624e 328 return (EXECUTION_FAILURE);
3185942a
JA
329#else
330 return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
331#endif
bb70624e 332
ac50fbac
CR
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
0001803f
CR
345 /* If we're asked to ignore the delimiter, make sure we do. */
346 if (ignore_delim)
347 delim = -1;
348
ccc6cda3 349 /* IF IFS is unset, we use the default of " \t\n". */
7117c2d2
JA
350 ifs_chars = getifs ();
351 if (ifs_chars == 0) /* XXX - shouldn't happen */
352 ifs_chars = "";
0001803f
CR
353 /* If we want to read exactly NCHARS chars, don't split on IFS */
354 if (ignore_delim)
355 ifs_chars = "";
3185942a
JA
356 for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++)
357 skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL;
726f6388 358
7117c2d2 359 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
3185942a 360 input_string[0] = '\0';
7117c2d2
JA
361
362 /* $TMOUT, if set, is the default timeout for read. */
363 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
364 {
3185942a
JA
365 code = uconvert (e, &ival, &uval);
366 if (code == 0 || ival < 0 || uval < 0)
367 tmsec = tmusec = 0;
7117c2d2 368 else
3185942a
JA
369 {
370 tmsec = ival;
371 tmusec = uval;
372 }
7117c2d2 373 }
726f6388
JA
374
375 begin_unwind_frame ("read_builtin");
726f6388 376
95732b49
JA
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
7117c2d2 382 input_is_tty = isatty (fd);
bb70624e 383 if (input_is_tty == 0)
28ef6c31 384#ifndef __CYGWIN__
95732b49 385 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
bb70624e
JA
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
ccc6cda3 391 terminal, turn them off. */
bb70624e 392 if ((prompt || edit || silent) && input_is_tty == 0)
ccc6cda3
JA
393 {
394 prompt = (char *)NULL;
3185942a
JA
395#if defined (READLINE)
396 itext = (char *)NULL;
397#endif
bb70624e 398 edit = silent = 0;
ccc6cda3
JA
399 }
400
7117c2d2
JA
401#if defined (READLINE)
402 if (edit)
403 add_unwind_protect (xfree, rlbuf);
404#endif
405
726f6388
JA
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
3185942a 409 if (tmsec > 0 || tmusec > 0)
bb70624e
JA
410 {
411 /* Turn off the timeout if stdin is a regular file (e.g. from
412 input redirection). */
7117c2d2 413 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
3185942a 414 tmsec = tmusec = 0;
bb70624e
JA
415 }
416
3185942a 417 if (tmsec > 0 || tmusec > 0)
bb70624e 418 {
ac50fbac 419 code = setjmp_nosigs (alrmbuf);
bb70624e
JA
420 if (code)
421 {
ac50fbac 422 sigalrm_seen = 0;
89a92869
CR
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,
43aebe92 425 so we have to save input_string temporarily, run the unwind-
ac50fbac
CR
426 protects, then restore input_string so we can use it later */
427 orig_input_string = 0;
3185942a 428 input_string[i] = '\0'; /* make sure it's terminated */
43aebe92
CR
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;
89a92869 439 retval = 128+SIGALRM;
3185942a 440 goto assign_vars;
bb70624e 441 }
84c617ec
CR
442 if (interactive_shell == 0)
443 initialize_terminating_signals ();
bb70624e
JA
444 old_alrm = set_signal_handler (SIGALRM, sigalrm);
445 add_unwind_protect (reset_alarm, (char *)NULL);
0628567a
JA
446#if defined (READLINE)
447 if (edit)
9b86eb22
CR
448 {
449 add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
450 add_unwind_protect (bashline_reset_event_hook, (char *)NULL);
451 }
0628567a 452#endif
3185942a 453 falarm (tmsec, tmusec);
bb70624e
JA
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)
28ef6c31 478 {
3185942a
JA
479 /* ttsave() */
480 termsave.fd = fd;
481 ttgetattr (fd, &ttattrs);
482 termsave.attrs = &ttattrs;
483
484 ttset = ttattrs;
17345e5a
JA
485 i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset);
486 if (i < 0)
487 sh_ttyerror (1);
84c617ec 488 tty_modified = 1;
3185942a 489 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
84c617ec
CR
490 if (interactive_shell == 0)
491 initialize_terminating_signals ();
bb70624e
JA
492 }
493 }
494 else if (silent) /* turn off echo but leave term in canonical mode */
495 {
3185942a
JA
496 /* ttsave (); */
497 termsave.fd = fd;
498 ttgetattr (fd, &ttattrs);
499 termsave.attrs = &ttattrs;
500
501 ttset = ttattrs;
17345e5a
JA
502 i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */
503 if (i < 0)
504 sh_ttyerror (1);
3185942a 505
84c617ec 506 tty_modified = 1;
3185942a 507 add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
84c617ec
CR
508 if (interactive_shell == 0)
509 initialize_terminating_signals ();
bb70624e
JA
510 }
511
28ef6c31
JA
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);
28ef6c31 515
ac50fbac
CR
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;
bb70624e 521
3185942a
JA
522 if (prompt && edit == 0)
523 {
524 fprintf (stderr, "%s", prompt);
525 fflush (stderr);
526 }
527
28ef6c31
JA
528#if defined (__CYGWIN__) && defined (O_TEXT)
529 setmode (0, O_TEXT);
530#endif
531
f1be666c
JA
532 ps2 = 0;
533 for (print_ps2 = eof = retval = 0;;)
726f6388 534 {
ac50fbac
CR
535 CHECK_ALRM;
536
ccc6cda3
JA
537#if defined (READLINE)
538 if (edit)
539 {
540 if (rlbuf && rlbuf[rlind] == '\0')
541 {
d166f048 542 xfree (rlbuf);
ccc6cda3
JA
543 rlbuf = (char *)0;
544 }
545 if (rlbuf == 0)
546 {
ac50fbac 547 reading = 1;
3185942a 548 rlbuf = edit_line (prompt ? prompt : "", itext);
ac50fbac 549 reading = 0;
ccc6cda3
JA
550 rlind = 0;
551 }
552 if (rlbuf == 0)
553 {
554 eof = 1;
555 break;
556 }
557 c = rlbuf[rlind++];
558 }
559 else
b72432fd 560 {
e8ce775d
JA
561#endif
562
f1be666c
JA
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
ac50fbac
CR
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);
bb70624e 581 else
ac50fbac
CR
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
bb70624e 588
e8ce775d 589 if (retval <= 0)
ccc6cda3 590 {
ac50fbac
CR
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;
84c617ec
CR
600 if (terminating_signal && tty_modified)
601 ttyrestore (&termsave); /* fix terminal before exiting */
ac50fbac 602 CHECK_TERMSIG;
ccc6cda3
JA
603 eof = 1;
604 break;
605 }
606
ac50fbac
CR
607 CHECK_ALRM;
608
b72432fd
JA
609#if defined (READLINE)
610 }
611#endif
612
ac50fbac 613 CHECK_ALRM;
3185942a 614 if (i + 4 >= size) /* XXX was i + 2; use i + 4 for multibyte/read_mbchar */
28ef6c31 615 {
ac50fbac
CR
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 }
28ef6c31 626 }
726f6388
JA
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 {
95732b49 632 pass_next = 0;
726f6388 633 if (c == '\n')
f1be666c
JA
634 {
635 i--; /* back up over the CTLESC */
636 if (interactive && input_is_tty && raw == 0)
637 print_ps2 = 1;
638 }
726f6388 639 else
95732b49 640 goto add_char;
726f6388
JA
641 continue;
642 }
643
3185942a 644 /* This may cause problems if IFS contains CTLESC */
ccc6cda3 645 if (c == '\\' && raw == 0)
726f6388
JA
646 {
647 pass_next++;
3185942a
JA
648 if (skip_ctlesc == 0)
649 {
650 saw_escape++;
651 input_string[i++] = CTLESC;
652 }
726f6388
JA
653 continue;
654 }
655
ac50fbac 656 if (ignore_delim == 0 && (unsigned char)c == delim)
726f6388
JA
657 break;
658
ac50fbac
CR
659 if (c == '\0' && delim != '\0')
660 continue; /* skip NUL bytes in input */
661
3185942a 662 if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL))
726f6388
JA
663 {
664 saw_escape++;
665 input_string[i++] = CTLESC;
666 }
667
95732b49 668add_char:
726f6388 669 input_string[i++] = c;
ac50fbac 670 CHECK_ALRM;
3185942a
JA
671
672#if defined (HANDLE_MULTIBYTE)
ac50fbac 673 if (nchars > 0 && MB_CUR_MAX > 1 && is_basic (c) == 0)
3185942a
JA
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
95732b49 680 nr++;
bb70624e 681
95732b49 682 if (nchars > 0 && nr >= nchars)
bb70624e 683 break;
726f6388
JA
684 }
685 input_string[i] = '\0';
ac50fbac 686 CHECK_ALRM;
726f6388 687
7117c2d2
JA
688 if (retval < 0)
689 {
ac50fbac
CR
690 t_errno = errno;
691 if (errno != EINTR)
692 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
0628567a 693 run_unwind_frame ("read_builtin");
ac50fbac 694 return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig);
7117c2d2 695 }
7117c2d2 696
3185942a 697 if (tmsec > 0 || tmusec > 0)
bb70624e
JA
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')
f73dda09 708 reset_eol_delim ((char *)NULL);
bb70624e
JA
709 }
710 else
711#endif
712 if (input_is_tty)
3185942a 713 ttyrestore (&termsave);
bb70624e
JA
714 }
715 else if (silent)
3185942a 716 ttyrestore (&termsave);
bb70624e
JA
717
718 if (unbuffered_read == 0)
7117c2d2 719 zsyncfd (fd);
bb70624e 720
726f6388
JA
721 discard_unwind_frame ("read_builtin");
722
ccc6cda3 723 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
726f6388 724
3185942a
JA
725assign_vars:
726
ccc6cda3
JA
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)
726f6388 731 {
b80f6443
JA
732 if (legal_identifier (arrayname) == 0)
733 {
734 sh_invalidid (arrayname);
735 xfree (input_string);
736 return (EXECUTION_FAILURE);
737 }
738
f73dda09 739 var = find_or_make_array_variable (arrayname, 1);
ccc6cda3 740 if (var == 0)
0628567a
JA
741 {
742 xfree (input_string);
743 return EXECUTION_FAILURE; /* readonly or noassign */
744 }
b4d40164
CR
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 }
ac50fbac
CR
751 else if (invisible_p (var))
752 VUNSETATTR (var, att_invisible);
7117c2d2 753 array_flush (array_cell (var));
ccc6cda3
JA
754
755 alist = list_string (input_string, ifs_chars, 0);
756 if (alist)
757 {
0628567a
JA
758 if (saw_escape)
759 dequote_list (alist);
760 else
761 word_list_remove_quoted_nulls (alist);
95732b49 762 assign_array_var_from_word_list (var, alist, 0);
ccc6cda3
JA
763 dispose_words (alist);
764 }
d166f048 765 xfree (input_string);
ccc6cda3 766 return (retval);
726f6388 767 }
ccc6cda3 768#endif /* ARRAY_VARS */
726f6388 769
d166f048
JA
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
28ef6c31
JA
775 to read a line completely without interpretation or modification
776 unless you mess with $IFS (e.g., setting it to the empty string).
d166f048
JA
777 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
778 if (list == 0)
726f6388 779 {
d166f048
JA
780#if 0
781 orig_input_string = input_string;
7117c2d2 782 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
d166f048
JA
783 ;
784 input_string = t;
785 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
786#endif
787
726f6388
JA
788 if (saw_escape)
789 {
790 t = dequote_string (input_string);
95732b49 791 var = bind_variable ("REPLY", t, 0);
726f6388
JA
792 free (t);
793 }
794 else
95732b49 795 var = bind_variable ("REPLY", input_string, 0);
bb70624e 796 VUNSETATTR (var, att_invisible);
28ef6c31 797
ac50fbac 798 xfree (input_string);
ccc6cda3 799 return (retval);
726f6388 800 }
ccc6cda3
JA
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. */
7117c2d2 808 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
ccc6cda3
JA
809 ;
810 input_string = t;
ccc6cda3 811 for (; list->next; list = list->next)
726f6388 812 {
ccc6cda3
JA
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
726f6388 819 {
7117c2d2 820 sh_invalidid (varname);
f73dda09 821 xfree (orig_input_string);
ccc6cda3
JA
822 return (EXECUTION_FAILURE);
823 }
726f6388 824
ccc6cda3
JA
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)
828 {
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)
726f6388 836 {
ccc6cda3
JA
837 t1 = dequote_string (t);
838 var = bind_read_variable (varname, t1);
f73dda09 839 xfree (t1);
726f6388
JA
840 }
841 else
952a5758 842 var = bind_read_variable (varname, t ? t : "");
ccc6cda3
JA
843 }
844 else
845 {
846 t = (char *)0;
847 var = bind_read_variable (varname, "");
726f6388
JA
848 }
849
ccc6cda3
JA
850 FREE (t);
851 if (var == 0)
726f6388 852 {
f73dda09 853 xfree (orig_input_string);
726f6388
JA
854 return (EXECUTION_FAILURE);
855 }
856
ccc6cda3 857 stupidly_hack_special_variables (varname);
bb70624e 858 VUNSETATTR (var, att_invisible);
ccc6cda3
JA
859 }
860
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 {
7117c2d2 868 sh_invalidid (list->word->word);
f73dda09 869 xfree (orig_input_string);
ccc6cda3 870 return (EXECUTION_FAILURE);
726f6388
JA
871 }
872
95732b49 873#if 0
ccc6cda3
JA
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);
95732b49
JA
878#else
879 /* Check whether or not the number of fields is exactly the same as the
880 number of variables. */
f1be666c 881 tofree = NULL;
95732b49
JA
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)
f1be666c 887 tofree = input_string = t;
95732b49 888 else
89a92869
CR
889 {
890 input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
891 tofree = t;
892 }
95732b49
JA
893 }
894#endif
ccc6cda3 895
0213a83a 896 if (saw_escape && input_string && *input_string)
ccc6cda3
JA
897 {
898 t = dequote_string (input_string);
899 var = bind_read_variable (list->word->word, t);
f73dda09 900 xfree (t);
ccc6cda3
JA
901 }
902 else
952a5758 903 var = bind_read_variable (list->word->word, input_string ? input_string : "");
f1be666c 904
ccc6cda3 905 if (var)
495aee44
CR
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);
f73dda09 914 xfree (orig_input_string);
ccc6cda3 915
726f6388
JA
916 return (retval);
917}
918
ccc6cda3
JA
919static SHELL_VAR *
920bind_read_variable (name, value)
921 char *name, *value;
922{
495aee44 923 SHELL_VAR *v;
ac50fbac 924
ccc6cda3
JA
925#if defined (ARRAY_VARS)
926 if (valid_array_reference (name) == 0)
495aee44 927 v = bind_variable (name, value, 0);
ccc6cda3 928 else
495aee44 929 v = assign_array_element (name, value, 0);
f73dda09 930#else /* !ARRAY_VARS */
495aee44 931 v = bind_variable (name, value, 0);
f73dda09 932#endif /* !ARRAY_VARS */
495aee44
CR
933 return (v == 0 ? v
934 : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v));
ccc6cda3
JA
935}
936
3185942a
JA
937#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;
ac50fbac 963 /* We don't want to be interrupted during a multibyte char read */
3185942a
JA
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
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);
84c617ec
CR
992 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);
3185942a
JA
1006}
1007
ccc6cda3 1008#if defined (READLINE)
0628567a 1009static rl_completion_func_t *old_attempted_completion_function = 0;
3185942a
JA
1010static rl_hook_func_t *old_startup_hook;
1011static char *deftext;
0628567a
JA
1012
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}
7117c2d2 1020
3185942a
JA
1021static 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
ccc6cda3 1039static char *
3185942a 1040edit_line (p, itext)
ccc6cda3 1041 char *p;
3185942a 1042 char *itext;
726f6388 1043{
ccc6cda3
JA
1044 char *ret;
1045 int len;
1046
95732b49 1047 if (bash_readline_initialized == 0)
ccc6cda3 1048 initialize_readline ();
0628567a 1049
7117c2d2
JA
1050 old_attempted_completion_function = rl_attempted_completion_function;
1051 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
9b86eb22 1052 bashline_set_event_hook ();
3185942a
JA
1053 if (itext)
1054 {
1055 old_startup_hook = rl_startup_hook;
1056 rl_startup_hook = set_itext;
1057 deftext = itext;
1058 }
ac50fbac 1059
ccc6cda3 1060 ret = readline (p);
ac50fbac 1061
7117c2d2 1062 rl_attempted_completion_function = old_attempted_completion_function;
0628567a 1063 old_attempted_completion_function = (rl_completion_func_t *)NULL;
9b86eb22 1064 bashline_reset_event_hook ();
0628567a 1065
ccc6cda3
JA
1066 if (ret == 0)
1067 return ret;
1068 len = strlen (ret);
f73dda09 1069 ret = (char *)xrealloc (ret, len + 2);
bb70624e 1070 ret[len++] = delim;
ccc6cda3
JA
1071 ret[len] = '\0';
1072 return ret;
726f6388 1073}
bb70624e
JA
1074
1075static int old_delim_ctype;
f73dda09 1076static rl_command_func_t *old_delim_func;
bb70624e 1077static int old_newline_ctype;
f73dda09 1078static rl_command_func_t *old_newline_func;
bb70624e 1079
f73dda09 1080static unsigned char delim_char;
bb70624e
JA
1081
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
f73dda09
JA
1108reset_eol_delim (cp)
1109 char *cp;
bb70624e
JA
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}
ccc6cda3 1121#endif