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