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