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