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