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