source: vendor/bash/3.1/builtins/read.def@ 3228

Last change on this file since 3228 was 3228, checked in by bird, 19 years ago

bash 3.1

File size: 18.6 KB
Line 
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
90extern int interrupt_immediately;
91
92#if defined (READLINE)
93static char *edit_line __P((char *));
94static void set_eol_delim __P((int));
95static void reset_eol_delim __P((char *));
96#endif
97static SHELL_VAR *bind_read_variable __P((char *, char *));
98
99static sighandler sigalrm __P((int));
100static void reset_alarm __P((void));
101
102static procenv_t alrmbuf;
103static SigHandler *old_alrm;
104static unsigned char delim;
105
106static sighandler
107sigalrm (s)
108 int s;
109{
110 longjmp (alrmbuf, 1);
111}
112
113static void
114reset_alarm ()
115{
116 set_signal_handler (SIGALRM, old_alrm);
117 alarm (0);
118}
119
120/* Read the value of the shell variables whose names follow.
121 The reading is done from the current input stream, whatever
122 that may be. Successive words of the input line are assigned
123 to the variables mentioned in LIST. The last variable in LIST
124 gets the remainder of the words on the line. If no variables
125 are mentioned in LIST, then the default variable is $REPLY. */
126int
127read_builtin (list)
128 WORD_LIST *list;
129{
130 register char *varname;
131 int size, i, nr, pass_next, saw_escape, eof, opt, retval, code;
132 int input_is_tty, input_is_pipe, unbuffered_read;
133 int raw, edit, nchars, silent, have_timeout, fd;
134 unsigned int tmout;
135 intmax_t intval;
136 char c;
137 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
138 char *e, *t, *t1;
139 struct stat tsb;
140 SHELL_VAR *var;
141#if defined (ARRAY_VARS)
142 WORD_LIST *alist;
143#endif
144#if defined (READLINE)
145 char *rlbuf;
146 int rlind;
147#endif
148
149 USE_VAR(size);
150 USE_VAR(i);
151 USE_VAR(pass_next);
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
168 i = 0; /* Index into the string that we are reading. */
169 raw = edit = 0; /* Not reading raw input by default. */
170 silent = 0;
171 arrayname = prompt = (char *)NULL;
172 fd = 0; /* file descriptor to read from */
173
174#if defined (READLINE)
175 rlbuf = (char *)0;
176 rlind = 0;
177#endif
178
179 tmout = 0; /* no timeout */
180 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
181 delim = '\n'; /* read until newline */
182
183 reset_internal_getopt ();
184 while ((opt = internal_getopt (list, "ersa:d:n:p:t:u:")) != -1)
185 {
186 switch (opt)
187 {
188 case 'r':
189 raw = 1;
190 break;
191 case 'p':
192 prompt = list_optarg;
193 break;
194 case 's':
195 silent = 1;
196 break;
197 case 'e':
198#if defined (READLINE)
199 edit = 1;
200#endif
201 break;
202#if defined (ARRAY_VARS)
203 case 'a':
204 arrayname = list_optarg;
205 break;
206#endif
207 case 't':
208 code = legal_number (list_optarg, &intval);
209 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
210 {
211 builtin_error (_("%s: invalid timeout specification"), list_optarg);
212 return (EXECUTION_FAILURE);
213 }
214 else
215 {
216 have_timeout = 1;
217 tmout = intval;
218 }
219 break;
220 case 'n':
221 code = legal_number (list_optarg, &intval);
222 if (code == 0 || intval < 0 || intval != (int)intval)
223 {
224 sh_invalidnum (list_optarg);
225 return (EXECUTION_FAILURE);
226 }
227 else
228 nchars = intval;
229 break;
230 case 'u':
231 code = legal_number (list_optarg, &intval);
232 if (code == 0 || intval < 0 || intval != (int)intval)
233 {
234 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
235 return (EXECUTION_FAILURE);
236 }
237 else
238 fd = intval;
239 if (sh_validfd (fd) == 0)
240 {
241 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
242 return (EXECUTION_FAILURE);
243 }
244 break;
245 case 'd':
246 delim = *list_optarg;
247 break;
248 default:
249 builtin_usage ();
250 return (EX_USAGE);
251 }
252 }
253 list = loptend;
254
255 /* `read -t 0 var' returns failure immediately. XXX - should it test
256 whether input is available with select/FIONREAD, and fail if those
257 are unavailable? */
258 if (have_timeout && tmout == 0)
259 return (EXECUTION_FAILURE);
260
261 /* IF IFS is unset, we use the default of " \t\n". */
262 ifs_chars = getifs ();
263 if (ifs_chars == 0) /* XXX - shouldn't happen */
264 ifs_chars = "";
265
266 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
267
268 /* $TMOUT, if set, is the default timeout for read. */
269 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
270 {
271 code = legal_number (e, &intval);
272 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
273 tmout = 0;
274 else
275 tmout = intval;
276 }
277
278 begin_unwind_frame ("read_builtin");
279
280#if defined (BUFFERED_INPUT)
281 if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
282 sync_buffered_stream (default_buffered_input);
283#endif
284
285 input_is_tty = isatty (fd);
286 if (input_is_tty == 0)
287#ifndef __CYGWIN__
288 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
289#else
290 input_is_pipe = 1;
291#endif
292
293 /* If the -p, -e or -s flags were given, but input is not coming from the
294 terminal, turn them off. */
295 if ((prompt || edit || silent) && input_is_tty == 0)
296 {
297 prompt = (char *)NULL;
298 edit = silent = 0;
299 }
300
301#if defined (READLINE)
302 if (edit)
303 add_unwind_protect (xfree, rlbuf);
304#endif
305
306 if (prompt && edit == 0)
307 {
308 fprintf (stderr, "%s", prompt);
309 fflush (stderr);
310 }
311
312 pass_next = 0; /* Non-zero signifies last char was backslash. */
313 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
314
315 if (tmout > 0)
316 {
317 /* Turn off the timeout if stdin is a regular file (e.g. from
318 input redirection). */
319 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
320 tmout = 0;
321 }
322
323 if (tmout > 0)
324 {
325 code = setjmp (alrmbuf);
326 if (code)
327 {
328 run_unwind_frame ("read_builtin");
329 return (EXECUTION_FAILURE);
330 }
331 old_alrm = set_signal_handler (SIGALRM, sigalrm);
332 add_unwind_protect (reset_alarm, (char *)NULL);
333 alarm (tmout);
334 }
335
336 /* If we've been asked to read only NCHARS chars, or we're using some
337 character other than newline to terminate the line, do the right
338 thing to readline or the tty. */
339 if (nchars > 0 || delim != '\n')
340 {
341#if defined (READLINE)
342 if (edit)
343 {
344 if (nchars > 0)
345 {
346 unwind_protect_int (rl_num_chars_to_read);
347 rl_num_chars_to_read = nchars;
348 }
349 if (delim != '\n')
350 {
351 set_eol_delim (delim);
352 add_unwind_protect (reset_eol_delim, (char *)NULL);
353 }
354 }
355 else
356#endif
357 if (input_is_tty)
358 {
359 ttsave ();
360 if (silent)
361 ttcbreak ();
362 else
363 ttonechar ();
364 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
365 }
366 }
367 else if (silent) /* turn off echo but leave term in canonical mode */
368 {
369 ttsave ();
370 ttnoecho ();
371 add_unwind_protect ((Function *)ttrestore, (char *)NULL);
372 }
373
374 /* This *must* be the top unwind-protect on the stack, so the manipulation
375 of the unwind-protect stack after the realloc() works right. */
376 add_unwind_protect (xfree, input_string);
377 interrupt_immediately++;
378
379 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
380
381#if defined (__CYGWIN__) && defined (O_TEXT)
382 setmode (0, O_TEXT);
383#endif
384
385 for (eof = retval = 0;;)
386 {
387#if defined (READLINE)
388 if (edit)
389 {
390 if (rlbuf && rlbuf[rlind] == '\0')
391 {
392 xfree (rlbuf);
393 rlbuf = (char *)0;
394 }
395 if (rlbuf == 0)
396 {
397 rlbuf = edit_line (prompt ? prompt : "");
398 rlind = 0;
399 }
400 if (rlbuf == 0)
401 {
402 eof = 1;
403 break;
404 }
405 c = rlbuf[rlind++];
406 }
407 else
408 {
409#endif
410
411 if (unbuffered_read)
412 retval = zread (fd, &c, 1);
413 else
414 retval = zreadc (fd, &c);
415
416 if (retval <= 0)
417 {
418 eof = 1;
419 break;
420 }
421
422#if defined (READLINE)
423 }
424#endif
425
426 if (i + 2 >= size)
427 {
428 input_string = (char *)xrealloc (input_string, size += 128);
429 remove_unwind_protect ();
430 add_unwind_protect (xfree, input_string);
431 }
432
433 /* If the next character is to be accepted verbatim, a backslash
434 newline pair still disappears from the input. */
435 if (pass_next)
436 {
437 pass_next = 0;
438 if (c == '\n')
439 i--; /* back up over the CTLESC */
440 else
441 goto add_char;
442 continue;
443 }
444