1This file is read.def, from which is created read.c.
2It implements the builtin "read" in Bash.
3
4Copyright (C) 1987-2005 Free Software Foundation, Inc.
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
8Bash is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
12
13Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with Bash; see the file COPYING.  If not, write to the Free Software
20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22$PRODUCES read.c
23
24$BUILTIN read
25$FUNCTION read_builtin
26$SHORT_DOC read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
27One line is read from the standard input, or from file descriptor FD if the
28-u option is supplied, and the first word is assigned to the first NAME,
29the second word to the second NAME, and so on, with leftover words assigned
30to the last NAME.  Only the characters found in $IFS are recognized as word
31delimiters.  If no NAMEs are supplied, the line read is stored in the REPLY
32variable.  If the -r option is given, this signifies `raw' input, and
33backslash escaping is disabled.  The -d option causes read to continue
34until the first character of DELIM is read, rather than newline.  If the -p
35option is supplied, the string PROMPT is output without a trailing newline
36before attempting to read.  If -a is supplied, the words read are assigned
37to sequential indices of ARRAY, starting at zero.  If -e is supplied and
38the shell is interactive, readline is used to obtain the line.  If -n is
39supplied with a non-zero NCHARS argument, read returns after NCHARS
40characters have been read.  The -s option causes input coming from a
41terminal to not be echoed.
42
43The -t option causes read to time out and return failure if a complete line
44of input is not read within TIMEOUT seconds.  If the TMOUT variable is set,
45its value is the default timeout.  The return code is zero, unless end-of-file
46is encountered, read times out, or an invalid file descriptor is supplied as
47the argument to -u.
48$END
49
50#include <config.h>
51
52#include "bashtypes.h"
53#include "posixstat.h"
54
55#include <stdio.h>
56
57#if defined (HAVE_UNISTD_H)
58#  include <unistd.h>
59#endif
60
61#include <signal.h>
62#include <errno.h>
63
64#ifdef __CYGWIN__
65#  include <fcntl.h>
66#  include <io.h>
67#endif
68
69#include "../bashintl.h"
70
71#include "../shell.h"
72#include "common.h"
73#include "bashgetopt.h"
74
75#include <shtty.h>
76
77#if defined (READLINE)
78#include "../bashline.h"
79#include <readline/readline.h>
80#endif
81
82#if defined (BUFFERED_INPUT)
83#  include "input.h"
84#endif
85
86#if !defined(errno)
87extern int errno;
88#endif
89
90#if defined (READLINE)
91static void reset_attempted_completion_function __P((char *));
92static char *edit_line __P((char *));
93static void set_eol_delim __P((int));
94static void reset_eol_delim __P((char *));
95#endif
96static SHELL_VAR *bind_read_variable __P((char *, char *));
97
98static sighandler sigalrm __P((int));
99static void reset_alarm __P((void));
100
101static procenv_t alrmbuf;
102static SigHandler *old_alrm;
103static unsigned char delim;
104
105static sighandler
106sigalrm (s)
107     int s;
108{
109  longjmp (alrmbuf, 1);
110}
111
112static void
113reset_alarm ()
114{
115  set_signal_handler (SIGALRM, old_alrm);
116  alarm (0);
117}
118
119/* Read the value of the shell variables whose names follow.
120   The reading is done from the current input stream, whatever
121   that may be.  Successive words of the input line are assigned
122   to the variables mentioned in LIST.  The last variable in LIST
123   gets the remainder of the words on the line.  If no variables
124   are mentioned in LIST, then the default variable is $REPLY. */
125int
126read_builtin (list)
127     WORD_LIST *list;
128{
129  register char *varname;
130  int size, i, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2;
131  int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul;
132  int raw, edit, nchars, silent, have_timeout, fd;
133  unsigned int tmout;
134  intmax_t intval;
135  char c;
136  char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
137  char *e, *t, *t1, *ps2, *tofree;
138  struct stat tsb;
139  SHELL_VAR *var;
140#if defined (ARRAY_VARS)
141  WORD_LIST *alist;
142#endif
143#if defined (READLINE)
144  char *rlbuf;
145  int rlind;
146#endif
147
148  USE_VAR(size);
149  USE_VAR(i);
150  USE_VAR(pass_next);
151  USE_VAR(print_ps2);
152  USE_VAR(saw_escape);
153  USE_VAR(input_is_pipe);
154/*  USE_VAR(raw); */
155  USE_VAR(edit);
156  USE_VAR(tmout);
157  USE_VAR(nchars);
158  USE_VAR(silent);
159  USE_VAR(ifs_chars);
160  USE_VAR(prompt);
161  USE_VAR(arrayname);
162#if defined (READLINE)
163  USE_VAR(rlbuf);
164  USE_VAR(rlind);
165#endif
166  USE_VAR(list);
167  USE_VAR(ps2);
168
169  i = 0;		/* Index into the string that we are reading. */
170  raw = edit = 0;	/* Not reading raw input by default. */
171  silent = 0;
172  arrayname = prompt = (char *)NULL;
173  fd = 0;		/* file descriptor to read from */
174
175#if defined (READLINE)
176  rlbuf = (char *)0;
177  rlind = 0;
178#endif
179
180  tmout = 0;		/* no timeout */
181  nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
182  delim = '\n';		/* read until newline */
183
184  reset_internal_getopt ();
185  while ((opt = internal_getopt (list, "ersa:d:n:p:t:u:")) != -1)
186    {
187      switch (opt)
188	{
189	case 'r':
190	  raw = 1;
191	  break;
192	case 'p':
193	  prompt = list_optarg;
194	  break;
195	case 's':
196	  silent = 1;
197	  break;
198	case 'e':
199#if defined (READLINE)
200	  edit = 1;
201#endif
202	  break;
203#if defined (ARRAY_VARS)
204	case 'a':
205	  arrayname = list_optarg;
206	  break;
207#endif
208	case 't':
209	  code = legal_number (list_optarg, &intval);
210	  if (code == 0 || intval < 0 || intval != (unsigned int)intval)
211	    {
212	      builtin_error (_("%s: invalid timeout specification"), list_optarg);
213	      return (EXECUTION_FAILURE);
214	    }
215	  else
216	    {
217	      have_timeout = 1;
218	      tmout = intval;
219	    }
220	  break;
221	case 'n':
222	  code = legal_number (list_optarg, &intval);
223	  if (code == 0 || intval < 0 || intval != (int)intval)
224	    {
225	      sh_invalidnum (list_optarg);
226	      return (EXECUTION_FAILURE);
227	    }
228	  else
229	    nchars = intval;
230	  break;
231	case 'u':
232	  code = legal_number (list_optarg, &intval);
233	  if (code == 0 || intval < 0 || intval != (int)intval)
234	    {
235	      builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
236	      return (EXECUTION_FAILURE);
237	    }
238	  else
239	    fd = intval;
240	  if (sh_validfd (fd) == 0)
241	    {
242	      builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
243	      return (EXECUTION_FAILURE);
244	    }
245	  break;
246	case 'd':
247	  delim = *list_optarg;
248	  break;
249	default:
250	  builtin_usage ();
251	  return (EX_USAGE);
252	}
253    }
254  list = loptend;
255
256  /* `read -t 0 var' returns failure immediately.  XXX - should it test
257     whether input is available with select/FIONREAD, and fail if those
258     are unavailable? */
259  if (have_timeout && tmout == 0)
260    return (EXECUTION_FAILURE);
261
262  /* IF IFS is unset, we use the default of " \t\n". */
263  ifs_chars = getifs ();
264  if (ifs_chars == 0)		/* XXX - shouldn't happen */
265    ifs_chars = "";
266
267  input_string = (char *)xmalloc (size = 112);	/* XXX was 128 */
268
269  /* $TMOUT, if set, is the default timeout for read. */
270  if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
271    {
272      code = legal_number (e, &intval);
273      if (code == 0 || intval < 0 || intval != (unsigned int)intval)
274	tmout = 0;
275      else
276	tmout = intval;
277    }
278
279  begin_unwind_frame ("read_builtin");
280
281#if defined (BUFFERED_INPUT)
282  if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
283    sync_buffered_stream (default_buffered_input);
284#endif
285
286  input_is_tty = isatty (fd);
287  if (input_is_tty == 0)
288#ifndef __CYGWIN__
289    input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
290#else
291    input_is_pipe = 1;
292#endif
293
294  /* If the -p, -e or -s flags were given, but input is not coming from the
295     terminal, turn them off. */
296  if ((prompt || edit || silent) && input_is_tty == 0)
297    {
298      prompt = (char *)NULL;
299      edit = silent = 0;
300    }
301
302#if defined (READLINE)
303  if (edit)
304    add_unwind_protect (xfree, rlbuf);
305#endif
306
307  if (prompt && edit == 0)
308    {
309      fprintf (stderr, "%s", prompt);
310      fflush (stderr);
311    }
312
313  pass_next = 0;	/* Non-zero signifies last char was backslash. */
314  saw_escape = 0;	/* Non-zero signifies that we saw an escape char */
315
316  if (tmout > 0)
317    {
318      /* Turn off the timeout if stdin is a regular file (e.g. from
319	 input redirection). */
320      if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
321	tmout = 0;
322    }
323
324  if (tmout > 0)
325    {
326      code = setjmp (alrmbuf);
327      if (code)
328	{
329	  interrupt_immediately--;
330	  terminate_immediately = 0;
331	  run_unwind_frame ("read_builtin");
332	  return (EXECUTION_FAILURE);
333	}
334      old_alrm = set_signal_handler (SIGALRM, sigalrm);
335      add_unwind_protect (reset_alarm, (char *)NULL);
336#if defined (READLINE)
337      if (edit)
338	add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
339#endif
340      alarm (tmout);
341    }
342
343  /* If we've been asked to read only NCHARS chars, or we're using some
344     character other than newline to terminate the line, do the right
345     thing to readline or the tty. */
346  if (nchars > 0 || delim != '\n')
347    {
348#if defined (READLINE)
349      if (edit)
350	{
351	  if (nchars > 0)
352	    {
353	      unwind_protect_int (rl_num_chars_to_read);
354	      rl_num_chars_to_read = nchars;
355	    }
356	  if (delim != '\n')
357	    {
358	      set_eol_delim (delim);
359	      add_unwind_protect (reset_eol_delim, (char *)NULL);
360	    }
361	}
362      else
363#endif
364      if (input_is_tty)
365	{
366	  ttsave ();
367	  if (silent)
368	    ttcbreak ();
369	  else
370	    ttonechar ();
371	  add_unwind_protect ((Function *)ttrestore, (char *)NULL);
372	}
373    }
374  else if (silent)	/* turn off echo but leave term in canonical mode */
375    {
376      ttsave ();
377      ttnoecho ();
378      add_unwind_protect ((Function *)ttrestore, (char *)NULL);
379    }
380
381  /* This *must* be the top unwind-protect on the stack, so the manipulation
382     of the unwind-protect stack after the realloc() works right. */
383  add_unwind_protect (xfree, input_string);
384  interrupt_immediately++;
385  terminate_immediately = 1;
386
387  unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
388
389#if defined (__CYGWIN__) && defined (O_TEXT)
390  setmode (0, O_TEXT);
391#endif
392
393  ps2 = 0;
394  for (print_ps2 = eof = retval = 0;;)
395    {
396#if defined (READLINE)
397      if (edit)
398	{
399	  if (rlbuf && rlbuf[rlind] == '\0')
400	    {
401	      xfree (rlbuf);
402	      rlbuf = (char *)0;
403	    }
404	  if (rlbuf == 0)
405	    {
406	      rlbuf = edit_line (prompt ? prompt : "");
407	      rlind = 0;
408	    }
409	  if (rlbuf == 0)
410	    {
411	      eof = 1;
412	      break;
413	    }
414	  c = rlbuf[rlind++];
415	}
416      else
417	{
418#endif
419
420      if (print_ps2)
421	{
422	  if (ps2 == 0)
423	    ps2 = get_string_value ("PS2");
424	  fprintf (stderr, "%s", ps2 ? ps2 : "");
425	  fflush (stderr);
426	  print_ps2 = 0;
427	}
428
429      if (unbuffered_read)
430	retval = zread (fd, &c, 1);
431      else
432	retval = zreadc (fd, &c);
433
434      if (retval <= 0)
435	{
436	  eof = 1;
437	  break;
438	}
439
440#if defined (READLINE)
441	}
442#endif
443
444      if (i + 2 >= size)
445	{
446	  input_string = (char *)xrealloc (input_string, size += 128);
447	  remove_unwind_protect ();
448	  add_unwind_protect (xfree, input_string);
449	}
450
451      /* If the next character is to be accepted verbatim, a backslash
452	 newline pair still disappears from the input. */
453      if (pass_next)
454	{
455	  pass_next = 0;
456	  if (c == '\n')
457	    {
458	      i--;		/* back up over the CTLESC */
459	      if (interactive && input_is_tty && raw == 0)
460		print_ps2 = 1;
461	    }
462	  else
463	    goto add_char;
464	  continue;
465	}
466
467      if (c == '\\' && raw == 0)
468	{
469	  pass_next++;
470	  saw_escape++;
471	  input_string[i++] = CTLESC;
472	  continue;
473	}
474
475      if ((unsigned char)c == delim)
476	break;
477
478      if (c == CTLESC || c == CTLNUL)
479	{
480	  saw_escape++;
481	  input_string[i++] = CTLESC;
482	}
483
484add_char:
485      input_string[i++] = c;
486      nr++;
487
488      if (nchars > 0 && nr >= nchars)
489	break;
490    }
491  input_string[i] = '\0';
492
493#if 1
494  if (retval < 0)
495    {
496      builtin_error (_("read error: %d: %s"), fd, strerror (errno));
497      run_unwind_frame ("read_builtin");
498      return (EXECUTION_FAILURE);
499    }
500#endif
501
502  if (tmout > 0)
503    reset_alarm ();
504
505  if (nchars > 0 || delim != '\n')
506    {
507#if defined (READLINE)
508      if (edit)
509	{
510	  if (nchars > 0)
511	    rl_num_chars_to_read = 0;
512	  if (delim != '\n')
513	    reset_eol_delim ((char *)NULL);
514	}
515      else
516#endif
517      if (input_is_tty)
518	ttrestore ();
519    }
520  else if (silent)
521    ttrestore ();
522
523  if (unbuffered_read == 0)
524    zsyncfd (fd);
525
526  interrupt_immediately--;
527  terminate_immediately = 0;
528  discard_unwind_frame ("read_builtin");
529
530  retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
531
532#if defined (ARRAY_VARS)
533  /* If -a was given, take the string read, break it into a list of words,
534     an assign them to `arrayname' in turn. */
535  if (arrayname)
536    {
537      if (legal_identifier (arrayname) == 0)
538	{
539	  sh_invalidid (arrayname);
540	  xfree (input_string);
541	  return (EXECUTION_FAILURE);
542	}
543
544      var = find_or_make_array_variable (arrayname, 1);
545      if (var == 0)
546	{
547	  xfree (input_string);
548	  return EXECUTION_FAILURE;	/* readonly or noassign */
549	}
550      array_flush (array_cell (var));
551
552      alist = list_string (input_string, ifs_chars, 0);
553      if (alist)
554	{
555	  if (saw_escape)
556	    dequote_list (alist);
557	  else
558	    word_list_remove_quoted_nulls (alist);
559	  assign_array_var_from_word_list (var, alist, 0);
560	  dispose_words (alist);
561	}
562      xfree (input_string);
563      return (retval);
564    }
565#endif /* ARRAY_VARS */ 
566
567  /* If there are no variables, save the text of the line read to the
568     variable $REPLY.  ksh93 strips leading and trailing IFS whitespace,
569     so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
570     same way, but I believe that the difference in behaviors is useful
571     enough to not do it.  Without the bash behavior, there is no way
572     to read a line completely without interpretation or modification
573     unless you mess with $IFS (e.g., setting it to the empty string).
574     If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
575  if (list == 0)
576    {
577#if 0
578      orig_input_string = input_string;
579      for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
580	;
581      input_string = t;
582      input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
583#endif
584
585      if (saw_escape)
586	{
587	  t = dequote_string (input_string);
588	  var = bind_variable ("REPLY", t, 0);
589	  free (t);
590	}
591      else
592	var = bind_variable ("REPLY", input_string, 0);
593      VUNSETATTR (var, att_invisible);
594
595      free (input_string);
596      return (retval);
597    }
598
599  /* This code implements the Posix.2 spec for splitting the words
600     read and assigning them to variables. */
601  orig_input_string = input_string;
602
603  /* Remove IFS white space at the beginning of the input string.  If
604     $IFS is null, no field splitting is performed. */
605  for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
606    ;
607  input_string = t;
608
609  for (; list->next; list = list->next)
610    {
611      varname = list->word->word;
612#if defined (ARRAY_VARS)
613      if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
614#else
615      if (legal_identifier (varname) == 0)
616#endif
617	{
618	  sh_invalidid (varname);
619	  xfree (orig_input_string);
620	  return (EXECUTION_FAILURE);
621	}
622
623      /* If there are more variables than words read from the input,
624	 the remaining variables are set to the empty string. */
625      if (*input_string)
626	{
627	  /* This call updates INPUT_STRING. */
628	  t = get_word_from_string (&input_string, ifs_chars, &e);
629	  if (t)
630	    *e = '\0';
631	  /* Don't bother to remove the CTLESC unless we added one
632	     somewhere while reading the string. */
633	  if (t && saw_escape)
634	    {
635	      t1 = dequote_string (t);
636	      var = bind_read_variable (varname, t1);
637	      xfree (t1);
638	    }
639	  else
640	    var = bind_read_variable (varname, t);
641	}
642      else
643	{
644	  t = (char *)0;
645	  var = bind_read_variable (varname, "");
646	}
647
648      FREE (t);
649      if (var == 0)
650	{
651	  xfree (orig_input_string);
652	  return (EXECUTION_FAILURE);
653	}
654
655      stupidly_hack_special_variables (varname);
656      VUNSETATTR (var, att_invisible);
657    }
658
659  /* Now assign the rest of the line to the last variable argument. */
660#if defined (ARRAY_VARS)
661  if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
662#else
663  if (legal_identifier (list->word->word) == 0)
664#endif
665    {
666      sh_invalidid (list->word->word);
667      xfree (orig_input_string);
668      return (EXECUTION_FAILURE);
669    }
670
671#if 0
672  /* This has to be done this way rather than using string_list
673     and list_string because Posix.2 says that the last variable gets the
674     remaining words and their intervening separators. */
675  input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
676#else
677  /* Check whether or not the number of fields is exactly the same as the
678     number of variables. */
679  tofree = NULL;
680  if (*input_string)
681    {
682      t1 = input_string;
683      t = get_word_from_string (&input_string, ifs_chars, &e);
684      if (*input_string == 0)
685	tofree = input_string = t;
686      else
687	input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
688    }
689#endif
690
691  if (saw_escape)
692    {
693      t = dequote_string (input_string);
694      var = bind_read_variable (list->word->word, t);
695      xfree (t);
696    }
697  else
698    var = bind_read_variable (list->word->word, input_string);
699  stupidly_hack_special_variables (list->word->word);
700  FREE (tofree);
701
702  if (var)
703    VUNSETATTR (var, att_invisible);
704  xfree (orig_input_string);
705
706  return (retval);
707}
708
709static SHELL_VAR *
710bind_read_variable (name, value)
711     char *name, *value;
712{
713#if defined (ARRAY_VARS)
714  if (valid_array_reference (name) == 0)
715    return (bind_variable (name, value, 0));
716  else
717    return (assign_array_element (name, value, 0));
718#else /* !ARRAY_VARS */
719  return bind_variable (name, value, 0);
720#endif /* !ARRAY_VARS */
721}
722
723#if defined (READLINE)
724static rl_completion_func_t *old_attempted_completion_function = 0;
725
726static void
727reset_attempted_completion_function (cp)
728     char *cp;
729{
730  if (rl_attempted_completion_function == 0 && old_attempted_completion_function)
731    rl_attempted_completion_function = old_attempted_completion_function;
732}
733
734static char *
735edit_line (p)
736     char *p;
737{
738  char *ret;
739  int len;
740
741  if (bash_readline_initialized == 0)
742    initialize_readline ();
743
744  old_attempted_completion_function = rl_attempted_completion_function;
745  rl_attempted_completion_function = (rl_completion_func_t *)NULL;
746  ret = readline (p);
747  rl_attempted_completion_function = old_attempted_completion_function;
748  old_attempted_completion_function = (rl_completion_func_t *)NULL;
749
750  if (ret == 0)
751    return ret;
752  len = strlen (ret);
753  ret = (char *)xrealloc (ret, len + 2);
754  ret[len++] = delim;
755  ret[len] = '\0';
756  return ret;
757}
758
759static int old_delim_ctype;
760static rl_command_func_t *old_delim_func;
761static int old_newline_ctype;
762static rl_command_func_t *old_newline_func;
763
764static unsigned char delim_char;
765
766static void
767set_eol_delim (c)
768     int c;
769{
770  Keymap cmap;
771
772  if (bash_readline_initialized == 0)
773    initialize_readline ();
774  cmap = rl_get_keymap ();
775
776  /* Change newline to self-insert */
777  old_newline_ctype = cmap[RETURN].type;
778  old_newline_func =  cmap[RETURN].function;
779  cmap[RETURN].type = ISFUNC;
780  cmap[RETURN].function = rl_insert;
781
782  /* Bind the delimiter character to accept-line. */
783  old_delim_ctype = cmap[c].type;
784  old_delim_func = cmap[c].function;
785  cmap[c].type = ISFUNC;
786  cmap[c].function = rl_newline;
787
788  delim_char = c;
789}
790
791static void
792reset_eol_delim (cp)
793     char *cp;
794{
795  Keymap cmap;
796
797  cmap = rl_get_keymap ();
798
799  cmap[RETURN].type = old_newline_ctype;
800  cmap[RETURN].function = old_newline_func;
801
802  cmap[delim_char].type = old_delim_ctype;
803  cmap[delim_char].function = old_delim_func;
804}
805#endif
806