]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/read.def
commit bash-20070405 snapshot
[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
e6598ba4 4Copyright (C) 1987-2007 Free Software Foundation, Inc.
726f6388
JA
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
bb70624e 10Software Foundation; either version 2, or (at your option) any later
726f6388
JA
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
bb70624e 20Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
726f6388
JA
21
22$PRODUCES read.c
23
24$BUILTIN read
25$FUNCTION read_builtin
d3ad40de 26$SHORT_DOC read [-ers] [-a array] [-d delim] [-n nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
7117c2d2
JA
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.
bb70624e
JA
42
43The -t option causes read to time out and return failure if a complete line
7117c2d2
JA
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.
726f6388
JA
48$END
49
ccc6cda3
JA
50#include <config.h>
51
bb70624e
JA
52#include "bashtypes.h"
53#include "posixstat.h"
54
726f6388 55#include <stdio.h>
ccc6cda3
JA
56
57#if defined (HAVE_UNISTD_H)
58# include <unistd.h>
59#endif
60
bb70624e 61#include <signal.h>
e8ce775d
JA
62#include <errno.h>
63
28ef6c31
JA
64#ifdef __CYGWIN__
65# include <fcntl.h>
66# include <io.h>
67#endif
68
5e13499c
CR
69#include "../bashintl.h"
70
726f6388
JA
71#include "../shell.h"
72#include "common.h"
ccc6cda3 73#include "bashgetopt.h"
726f6388 74
bb70624e
JA
75#include <shtty.h>
76
ccc6cda3
JA
77#if defined (READLINE)
78#include "../bashline.h"
79#include <readline/readline.h>
80#endif
726f6388 81
cdb32d45
CR
82#if defined (BUFFERED_INPUT)
83# include "input.h"
84#endif
85
e8ce775d
JA
86#if !defined(errno)
87extern int errno;
88#endif
89
ccc6cda3 90#if defined (READLINE)
dc8fbaf9 91static void reset_attempted_completion_function __P((char *));
f73dda09
JA
92static char *edit_line __P((char *));
93static void set_eol_delim __P((int));
94static void reset_eol_delim __P((char *));
ccc6cda3 95#endif
f73dda09
JA
96static SHELL_VAR *bind_read_variable __P((char *, char *));
97
98static sighandler sigalrm __P((int));
99static void reset_alarm __P((void));
ccc6cda3 100
bb70624e
JA
101static procenv_t alrmbuf;
102static SigHandler *old_alrm;
f73dda09 103static unsigned char delim;
bb70624e
JA
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
726f6388
JA
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
ccc6cda3
JA
124 are mentioned in LIST, then the default variable is $REPLY. */
125int
726f6388
JA
126read_builtin (list)
127 WORD_LIST *list;
128{
129 register char *varname;
e6598ba4
CR
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;
7117c2d2 132 int raw, edit, nchars, silent, have_timeout, fd;
f73dda09 133 unsigned int tmout;
7117c2d2 134 intmax_t intval;
ccc6cda3
JA
135 char c;
136 char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname;
e6598ba4 137 char *e, *t, *t1, *ps2;
bb70624e 138 struct stat tsb;
726f6388 139 SHELL_VAR *var;
ccc6cda3
JA
140#if defined (ARRAY_VARS)
141 WORD_LIST *alist;
142#endif
143#if defined (READLINE)
144 char *rlbuf;
145 int rlind;
146#endif
726f6388 147
f73dda09
JA
148 USE_VAR(size);
149 USE_VAR(i);
150 USE_VAR(pass_next);
e6598ba4 151 USE_VAR(print_ps2);
f73dda09
JA
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);
e6598ba4 167 USE_VAR(ps2);
f73dda09 168
726f6388 169 i = 0; /* Index into the string that we are reading. */
ccc6cda3 170 raw = edit = 0; /* Not reading raw input by default. */
bb70624e 171 silent = 0;
ccc6cda3 172 arrayname = prompt = (char *)NULL;
7117c2d2 173 fd = 0; /* file descriptor to read from */
ccc6cda3
JA
174
175#if defined (READLINE)
176 rlbuf = (char *)0;
177 rlind = 0;
178#endif
726f6388 179
f73dda09 180 tmout = 0; /* no timeout */
233564d2 181 nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0;
bb70624e
JA
182 delim = '\n'; /* read until newline */
183
ccc6cda3 184 reset_internal_getopt ();
7117c2d2 185 while ((opt = internal_getopt (list, "ersa:d:n:p:t:u:")) != -1)
726f6388 186 {
ccc6cda3 187 switch (opt)
28ef6c31
JA
188 {
189 case 'r':
ccc6cda3 190 raw = 1;
726f6388 191 break;
ccc6cda3
JA
192 case 'p':
193 prompt = list_optarg;
194 break;
bb70624e
JA
195 case 's':
196 silent = 1;
197 break;
ccc6cda3
JA
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
bb70624e 208 case 't':
7117c2d2
JA
209 code = legal_number (list_optarg, &intval);
210 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
bb70624e 211 {
5e13499c 212 builtin_error (_("%s: invalid timeout specification"), list_optarg);
bb70624e
JA
213 return (EXECUTION_FAILURE);
214 }
215 else
f73dda09
JA
216 {
217 have_timeout = 1;
7117c2d2 218 tmout = intval;
f73dda09 219 }
bb70624e
JA
220 break;
221 case 'n':
7117c2d2
JA
222 code = legal_number (list_optarg, &intval);
223 if (code == 0 || intval < 0 || intval != (int)intval)
bb70624e 224 {
7117c2d2 225 sh_invalidnum (list_optarg);
bb70624e
JA
226 return (EXECUTION_FAILURE);
227 }
228 else
7117c2d2
JA
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 {
5e13499c 235 builtin_error (_("%s: invalid file descriptor specification"), list_optarg);
7117c2d2
JA
236 return (EXECUTION_FAILURE);
237 }
238 else
239 fd = intval;
240 if (sh_validfd (fd) == 0)
241 {
5e13499c 242 builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno));
7117c2d2
JA
243 return (EXECUTION_FAILURE);
244 }
bb70624e
JA
245 break;
246 case 'd':
247 delim = *list_optarg;
248 break;
ccc6cda3
JA
249 default:
250 builtin_usage ();
726f6388
JA
251 return (EX_USAGE);
252 }
726f6388 253 }
ccc6cda3 254 list = loptend;
726f6388 255
7117c2d2
JA
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? */
f73dda09 259 if (have_timeout && tmout == 0)
bb70624e
JA
260 return (EXECUTION_FAILURE);
261
ccc6cda3 262 /* IF IFS is unset, we use the default of " \t\n". */
7117c2d2
JA
263 ifs_chars = getifs ();
264 if (ifs_chars == 0) /* XXX - shouldn't happen */
265 ifs_chars = "";
e6598ba4
CR
266 for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++)
267 skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL;
726f6388 268
7117c2d2
JA
269 input_string = (char *)xmalloc (size = 112); /* XXX was 128 */
270
271 /* $TMOUT, if set, is the default timeout for read. */
272 if (have_timeout == 0 && (e = get_string_value ("TMOUT")))
273 {
274 code = legal_number (e, &intval);
275 if (code == 0 || intval < 0 || intval != (unsigned int)intval)
276 tmout = 0;
277 else
278 tmout = intval;
279 }
726f6388
JA
280
281 begin_unwind_frame ("read_builtin");
726f6388 282
cdb32d45
CR
283#if defined (BUFFERED_INPUT)
284 if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd))
285 sync_buffered_stream (default_buffered_input);
286#endif
287
7117c2d2 288 input_is_tty = isatty (fd);
bb70624e 289 if (input_is_tty == 0)
28ef6c31 290#ifndef __CYGWIN__
af91506a 291 input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);
bb70624e
JA
292#else
293 input_is_pipe = 1;
294#endif
295
296 /* If the -p, -e or -s flags were given, but input is not coming from the
ccc6cda3 297 terminal, turn them off. */
bb70624e 298 if ((prompt || edit || silent) && input_is_tty == 0)
ccc6cda3
JA
299 {
300 prompt = (char *)NULL;
bb70624e 301 edit = silent = 0;
ccc6cda3
JA
302 }
303
7117c2d2
JA
304#if defined (READLINE)
305 if (edit)
306 add_unwind_protect (xfree, rlbuf);
307#endif
308
ccc6cda3
JA
309 if (prompt && edit == 0)
310 {
311 fprintf (stderr, "%s", prompt);
312 fflush (stderr);
313 }
314
726f6388
JA
315 pass_next = 0; /* Non-zero signifies last char was backslash. */
316 saw_escape = 0; /* Non-zero signifies that we saw an escape char */
317
bb70624e
JA
318 if (tmout > 0)
319 {
320 /* Turn off the timeout if stdin is a regular file (e.g. from
321 input redirection). */
7117c2d2 322 if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode))
f73dda09 323 tmout = 0;
bb70624e
JA
324 }
325
326 if (tmout > 0)
327 {
328 code = setjmp (alrmbuf);
329 if (code)
330 {
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);
d3ad40de 336#if defined (READLINE)
dc8fbaf9
CR
337 if (edit)
338 add_unwind_protect (reset_attempted_completion_function, (char *)NULL);
d3ad40de 339#endif
bb70624e
JA
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)
28ef6c31 365 {
bb70624e
JA
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
28ef6c31
JA
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++;
ac18b312 385 terminate_immediately = 1;
28ef6c31 386
bb70624e
JA
387 unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe;
388
28ef6c31
JA
389#if defined (__CYGWIN__) && defined (O_TEXT)
390 setmode (0, O_TEXT);
391#endif
392
e6598ba4
CR
393 ps2 = 0;
394 for (print_ps2 = eof = retval = 0;;)
726f6388 395 {
ccc6cda3
JA
396#if defined (READLINE)
397 if (edit)
398 {
399 if (rlbuf && rlbuf[rlind] == '\0')
400 {
d166f048 401 xfree (rlbuf);
ccc6cda3
JA
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
b72432fd 417 {
e8ce775d
JA
418#endif
419
e6598ba4
CR
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
bb70624e 429 if (unbuffered_read)
7117c2d2 430 retval = zread (fd, &c, 1);
bb70624e 431 else
7117c2d2 432 retval = zreadc (fd, &c);
bb70624e 433
e8ce775d 434 if (retval <= 0)
ccc6cda3
JA
435 {
436 eof = 1;
437 break;
438 }
439
b72432fd
JA
440#if defined (READLINE)
441 }
442#endif
443
726f6388 444 if (i + 2 >= size)
28ef6c31 445 {
f73dda09 446 input_string = (char *)xrealloc (input_string, size += 128);
28ef6c31
JA
447 remove_unwind_protect ();
448 add_unwind_protect (xfree, input_string);
449 }
726f6388
JA
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 {
19e1dd93 455 pass_next = 0;
726f6388 456 if (c == '\n')
e6598ba4
CR
457 {
458 i--; /* back up over the CTLESC */
459 if (interactive && raw == 0)
460 print_ps2 = 1;
461 }
726f6388 462 else
19e1dd93 463 goto add_char;
726f6388
JA
464 continue;
465 }
466
e6598ba4 467 /* This may cause problems if IFS contains CTLESC */
ccc6cda3 468 if (c == '\\' && raw == 0)
726f6388
JA
469 {
470 pass_next++;
e6598ba4
CR
471 if (skip_ctlesc == 0)
472 {
473 saw_escape++;
474 input_string[i++] = CTLESC;
475 }
726f6388
JA
476 continue;
477 }
478
f73dda09 479 if ((unsigned char)c == delim)
726f6388
JA
480 break;
481
e6598ba4 482 if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL))
726f6388
JA
483 {
484 saw_escape++;
485 input_string[i++] = CTLESC;
486 }
487
19e1dd93 488add_char:
726f6388 489 input_string[i++] = c;
233564d2 490 nr++;
bb70624e 491
233564d2 492 if (nchars > 0 && nr >= nchars)
bb70624e 493 break;
726f6388
JA
494 }
495 input_string[i] = '\0';
496
7117c2d2
JA
497#if 1
498 if (retval < 0)
499 {
5e13499c 500 builtin_error (_("read error: %d: %s"), fd, strerror (errno));
af12dacd 501 run_unwind_frame ("read_builtin");
7117c2d2
JA
502 return (EXECUTION_FAILURE);
503 }
504#endif
505
bb70624e
JA
506 if (tmout > 0)
507 reset_alarm ();
508
509 if (nchars > 0 || delim != '\n')
510 {
511#if defined (READLINE)
512 if (edit)
513 {
514 if (nchars > 0)
515 rl_num_chars_to_read = 0;
516 if (delim != '\n')
f73dda09 517 reset_eol_delim ((char *)NULL);
bb70624e
JA
518 }
519 else
520#endif
521 if (input_is_tty)
522 ttrestore ();
523 }
524 else if (silent)
525 ttrestore ();
526
527 if (unbuffered_read == 0)
7117c2d2 528 zsyncfd (fd);
bb70624e 529
726f6388 530 interrupt_immediately--;
ac18b312 531 terminate_immediately = 0;
726f6388
JA
532 discard_unwind_frame ("read_builtin");
533
ccc6cda3 534 retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS;
726f6388 535
ccc6cda3
JA
536#if defined (ARRAY_VARS)
537 /* If -a was given, take the string read, break it into a list of words,
538 an assign them to `arrayname' in turn. */
539 if (arrayname)
726f6388 540 {
12d937f9
CR
541 if (legal_identifier (arrayname) == 0)
542 {
543 sh_invalidid (arrayname);
544 xfree (input_string);
545 return (EXECUTION_FAILURE);
546 }
547
f73dda09 548 var = find_or_make_array_variable (arrayname, 1);
ccc6cda3 549 if (var == 0)
af12dacd
CR
550 {
551 xfree (input_string);
552 return EXECUTION_FAILURE; /* readonly or noassign */
553 }
7117c2d2 554 array_flush (array_cell (var));
ccc6cda3
JA
555
556 alist = list_string (input_string, ifs_chars, 0);
557 if (alist)
558 {
d3ad40de
CR
559 if (saw_escape)
560 dequote_list (alist);
561 else
562 word_list_remove_quoted_nulls (alist);
d11b8b46 563 assign_array_var_from_word_list (var, alist, 0);
ccc6cda3
JA
564 dispose_words (alist);
565 }
d166f048 566 xfree (input_string);
ccc6cda3 567 return (retval);
726f6388 568 }
ccc6cda3 569#endif /* ARRAY_VARS */
726f6388 570
d166f048
JA
571 /* If there are no variables, save the text of the line read to the
572 variable $REPLY. ksh93 strips leading and trailing IFS whitespace,
573 so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the
574 same way, but I believe that the difference in behaviors is useful
575 enough to not do it. Without the bash behavior, there is no way
28ef6c31
JA
576 to read a line completely without interpretation or modification
577 unless you mess with $IFS (e.g., setting it to the empty string).
d166f048
JA
578 If you disagree, change the occurrences of `#if 0' to `#if 1' below. */
579 if (list == 0)
726f6388 580 {
d166f048
JA
581#if 0
582 orig_input_string = input_string;
7117c2d2 583 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
d166f048
JA
584 ;
585 input_string = t;
586 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
587#endif
588
726f6388
JA
589 if (saw_escape)
590 {
591 t = dequote_string (input_string);
d11b8b46 592 var = bind_variable ("REPLY", t, 0);
726f6388
JA
593 free (t);
594 }
595 else
d11b8b46 596 var = bind_variable ("REPLY", input_string, 0);
bb70624e 597 VUNSETATTR (var, att_invisible);
28ef6c31 598
726f6388 599 free (input_string);
ccc6cda3 600 return (retval);
726f6388 601 }
ccc6cda3
JA
602
603 /* This code implements the Posix.2 spec for splitting the words
604 read and assigning them to variables. */
605 orig_input_string = input_string;
606
607 /* Remove IFS white space at the beginning of the input string. If
608 $IFS is null, no field splitting is performed. */
7117c2d2 609 for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++)
ccc6cda3
JA
610 ;
611 input_string = t;
612
613 for (; list->next; list = list->next)
726f6388 614 {
ccc6cda3
JA
615 varname = list->word->word;
616#if defined (ARRAY_VARS)
617 if (legal_identifier (varname) == 0 && valid_array_reference (varname) == 0)
618#else
619 if (legal_identifier (varname) == 0)
620#endif
726f6388 621 {
7117c2d2 622 sh_invalidid (varname);
f73dda09 623 xfree (orig_input_string);
ccc6cda3
JA
624 return (EXECUTION_FAILURE);
625 }
726f6388 626
ccc6cda3
JA
627 /* If there are more variables than words read from the input,
628 the remaining variables are set to the empty string. */
629 if (*input_string)
630 {
631 /* This call updates INPUT_STRING. */
632 t = get_word_from_string (&input_string, ifs_chars, &e);
633 if (t)
634 *e = '\0';
635 /* Don't bother to remove the CTLESC unless we added one
636 somewhere while reading the string. */
637 if (t && saw_escape)
726f6388 638 {
ccc6cda3
JA
639 t1 = dequote_string (t);
640 var = bind_read_variable (varname, t1);
f73dda09 641 xfree (t1);
726f6388
JA
642 }
643 else
ccc6cda3
JA
644 var = bind_read_variable (varname, t);
645 }
646 else
647 {
648 t = (char *)0;
649 var = bind_read_variable (varname, "");
726f6388
JA
650 }
651
ccc6cda3
JA
652 FREE (t);
653 if (var == 0)
726f6388 654 {
f73dda09 655 xfree (orig_input_string);
726f6388
JA
656 return (EXECUTION_FAILURE);
657 }
658
ccc6cda3 659 stupidly_hack_special_variables (varname);
bb70624e 660 VUNSETATTR (var, att_invisible);
ccc6cda3
JA
661 }
662
663 /* Now assign the rest of the line to the last variable argument. */
664#if defined (ARRAY_VARS)
665 if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word) == 0)
666#else
667 if (legal_identifier (list->word->word) == 0)
668#endif
669 {
7117c2d2 670 sh_invalidid (list->word->word);
f73dda09 671 xfree (orig_input_string);
ccc6cda3 672 return (EXECUTION_FAILURE);
726f6388
JA
673 }
674
9d2b70f0 675#if 0
ccc6cda3
JA
676 /* This has to be done this way rather than using string_list
677 and list_string because Posix.2 says that the last variable gets the
678 remaining words and their intervening separators. */
679 input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape);
9d2b70f0
CR
680#else
681 /* Check whether or not the number of fields is exactly the same as the
682 number of variables. */
683 if (*input_string)
684 {
685 t1 = input_string;
686 t = get_word_from_string (&input_string, ifs_chars, &e);
687 if (*input_string == 0)
688 input_string = t;
689 else
690 input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape);
691 }
692#endif
ccc6cda3
JA
693
694 if (saw_escape)
695 {
696 t = dequote_string (input_string);
697 var = bind_read_variable (list->word->word, t);
f73dda09 698 xfree (t);
ccc6cda3
JA
699 }
700 else
701 var = bind_read_variable (list->word->word, input_string);
702 stupidly_hack_special_variables (list->word->word);
703 if (var)
bb70624e 704 VUNSETATTR (var, att_invisible);
f73dda09 705 xfree (orig_input_string);
ccc6cda3 706
726f6388
JA
707 return (retval);
708}
709
ccc6cda3
JA
710static SHELL_VAR *
711bind_read_variable (name, value)
712 char *name, *value;
713{
714#if defined (ARRAY_VARS)
715 if (valid_array_reference (name) == 0)
d11b8b46 716 return (bind_variable (name, value, 0));
ccc6cda3 717 else
d11b8b46 718 return (assign_array_element (name, value, 0));
f73dda09 719#else /* !ARRAY_VARS */
d11b8b46 720 return bind_variable (name, value, 0);
f73dda09 721#endif /* !ARRAY_VARS */
ccc6cda3
JA
722}
723
724#if defined (READLINE)
dc8fbaf9
CR
725static rl_completion_func_t *old_attempted_completion_function = 0;
726
727static void
728reset_attempted_completion_function (cp)
729 char *cp;
730{
731 if (rl_attempted_completion_function == 0 && old_attempted_completion_function)
732 rl_attempted_completion_function = old_attempted_completion_function;
733}
7117c2d2 734
ccc6cda3
JA
735static char *
736edit_line (p)
737 char *p;
726f6388 738{
ccc6cda3
JA
739 char *ret;
740 int len;
741
055a1bf5 742 if (bash_readline_initialized == 0)
ccc6cda3 743 initialize_readline ();
dc8fbaf9 744
7117c2d2
JA
745 old_attempted_completion_function = rl_attempted_completion_function;
746 rl_attempted_completion_function = (rl_completion_func_t *)NULL;
ccc6cda3 747 ret = readline (p);
7117c2d2 748 rl_attempted_completion_function = old_attempted_completion_function;
dc8fbaf9
CR
749 old_attempted_completion_function = (rl_completion_func_t *)NULL;
750
ccc6cda3
JA
751 if (ret == 0)
752 return ret;
753 len = strlen (ret);
f73dda09 754 ret = (char *)xrealloc (ret, len + 2);
bb70624e 755 ret[len++] = delim;
ccc6cda3
JA
756 ret[len] = '\0';
757 return ret;
726f6388 758}
bb70624e
JA
759
760static int old_delim_ctype;
f73dda09 761static rl_command_func_t *old_delim_func;
bb70624e 762static int old_newline_ctype;
f73dda09 763static rl_command_func_t *old_newline_func;
bb70624e 764
f73dda09 765static unsigned char delim_char;
bb70624e
JA
766
767static void
768set_eol_delim (c)
769 int c;
770{
771 Keymap cmap;
772
773 if (bash_readline_initialized == 0)
774 initialize_readline ();
775 cmap = rl_get_keymap ();
776
777 /* Change newline to self-insert */
778 old_newline_ctype = cmap[RETURN].type;
779 old_newline_func = cmap[RETURN].function;
780 cmap[RETURN].type = ISFUNC;
781 cmap[RETURN].function = rl_insert;
782
783 /* Bind the delimiter character to accept-line. */
784 old_delim_ctype = cmap[c].type;
785 old_delim_func = cmap[c].function;
786 cmap[c].type = ISFUNC;
787 cmap[c].function = rl_newline;
788
789 delim_char = c;
790}
791
792static void
f73dda09
JA
793reset_eol_delim (cp)
794 char *cp;
bb70624e
JA
795{
796 Keymap cmap;
797
798 cmap = rl_get_keymap ();
799
800 cmap[RETURN].type = old_newline_ctype;
801 cmap[RETURN].function = old_newline_func;
802
803 cmap[delim_char].type = old_delim_ctype;
804 cmap[delim_char].function = old_delim_func;
805}
ccc6cda3 806#endif