]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | This file is read.def, from which is created read.c. |
2 | It implements the builtin "read" in Bash. | |
3 | ||
74091dd4 | 4 | Copyright (C) 1987-2021 Free Software Foundation, Inc. |
726f6388 JA |
5 | |
6 | This file is part of GNU Bash, the Bourne Again SHell. | |
7 | ||
3185942a JA |
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. | |
726f6388 | 12 | |
3185942a JA |
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. | |
726f6388 | 17 | |
3185942a JA |
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/>. | |
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 |
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 | |
74091dd4 CR |
33 | delimiters. By default, the backslash character escapes delimiter characters |
34 | and newline. | |
3185942a JA |
35 | |
36 | If no NAMEs are supplied, the line read is stored in the REPLY variable. | |
37 | ||
38 | Options: | |
39 | -a array assign the words read to sequential indices of the array | |
40 | variable ARRAY, starting at zero | |
41 | -d delim continue until the first character of DELIM is read, rather | |
42 | than newline | |
d233b485 | 43 | -e use Readline to obtain the line |
a0c0a00f | 44 | -i text use TEXT as the initial text for Readline |
3185942a | 45 | -n nchars return after reading NCHARS characters rather than waiting |
a0c0a00f CR |
46 | for a newline, but honor a delimiter if fewer than |
47 | NCHARS characters are read before the delimiter | |
0001803f | 48 | -N nchars return only after reading exactly NCHARS characters, unless |
a0c0a00f CR |
49 | EOF is encountered or read times out, ignoring any |
50 | delimiter | |
3185942a JA |
51 | -p prompt output the string PROMPT without a trailing newline before |
52 | attempting to read | |
a0c0a00f CR |
53 | -r do not allow backslashes to escape any characters |
54 | -s do not echo input coming from a terminal | |
55 | -t timeout time out and return failure if a complete line of | |
56 | input is not read within TIMEOUT seconds. The value of the | |
57 | TMOUT variable is the default timeout. TIMEOUT may be a | |
58 | fractional number. If TIMEOUT is 0, read returns | |
59 | immediately, without trying to read any data, returning | |
60 | success only if input is available on the specified | |
61 | file descriptor. The exit status is greater than 128 | |
62 | if the timeout is exceeded | |
63 | -u fd read from file descriptor FD instead of the standard input | |
3185942a JA |
64 | |
65 | Exit Status: | |
ac50fbac CR |
66 | The return code is zero, unless end-of-file is encountered, read times out |
67 | (in which case it's greater than 128), a variable assignment error occurs, | |
3185942a | 68 | or an invalid file descriptor is supplied as the argument to -u. |
726f6388 JA |
69 | $END |
70 | ||
ccc6cda3 JA |
71 | #include <config.h> |
72 | ||
bb70624e JA |
73 | #include "bashtypes.h" |
74 | #include "posixstat.h" | |
75 | ||
726f6388 | 76 | #include <stdio.h> |
ccc6cda3 | 77 | |
3185942a JA |
78 | #include "bashansi.h" |
79 | ||
ccc6cda3 JA |
80 | #if defined (HAVE_UNISTD_H) |
81 | # include <unistd.h> | |
82 | #endif | |
83 | ||
bb70624e | 84 | #include <signal.h> |
e8ce775d JA |
85 | #include <errno.h> |
86 | ||
28ef6c31 JA |
87 | #ifdef __CYGWIN__ |
88 | # include <fcntl.h> | |
89 | # include <io.h> | |
90 | #endif | |
91 | ||
b80f6443 JA |
92 | #include "../bashintl.h" |
93 | ||
726f6388 JA |
94 | #include "../shell.h" |
95 | #include "common.h" | |
ccc6cda3 | 96 | #include "bashgetopt.h" |
d233b485 | 97 | #include "trap.h" |
726f6388 | 98 | |
bb70624e JA |
99 | #include <shtty.h> |
100 | ||
ccc6cda3 JA |
101 | #if defined (READLINE) |
102 | #include "../bashline.h" | |
103 | #include <readline/readline.h> | |
104 | #endif | |
726f6388 | 105 | |
95732b49 JA |
106 | #if defined (BUFFERED_INPUT) |
107 | # include "input.h" | |
108 | #endif | |
109 | ||
ac50fbac | 110 | #include "shmbutil.h" |
74091dd4 | 111 | #include "timer.h" |
ac50fbac | 112 | |
e8ce775d JA |
113 | #if !defined(errno) |
114 | extern int errno; | |
115 | #endif | |
116 | ||
3185942a JA |
117 | struct ttsave |
118 | { | |
119 | int fd; | |
d233b485 | 120 | TTYSTRUCT attrs; |
3185942a JA |
121 | }; |
122 | ||
ccc6cda3 | 123 | #if defined (READLINE) |
8868edaf CR |
124 | static void reset_attempted_completion_function PARAMS((char *)); |
125 | static int set_itext PARAMS((void)); | |
126 | static char *edit_line PARAMS((char *, char *)); | |
127 | static void set_eol_delim PARAMS((int)); | |
128 | static void reset_eol_delim PARAMS((char *)); | |
74091dd4 | 129 | static void set_readline_timeout PARAMS((sh_timer *t, time_t, long)); |
ccc6cda3 | 130 | #endif |
74091dd4 | 131 | static SHELL_VAR *bind_read_variable PARAMS((char *, char *, int)); |
3185942a | 132 | #if defined (HANDLE_MULTIBYTE) |
8868edaf | 133 | static int read_mbchar PARAMS((int, char *, int, int, int)); |
3185942a | 134 | #endif |
8868edaf | 135 | static void ttyrestore PARAMS((struct ttsave *)); |
f73dda09 | 136 | |
8868edaf | 137 | static sighandler sigalrm PARAMS((int)); |
74091dd4 | 138 | static void reset_timeout PARAMS((void)); |
ccc6cda3 | 139 | |
ac50fbac | 140 | /* Try this to see what the rest of the shell can do with the information. */ |
74091dd4 | 141 | sh_timer *read_timeout; |
ac50fbac | 142 | |
84c617ec | 143 | static int reading, tty_modified; |
bb70624e | 144 | static SigHandler *old_alrm; |
f73dda09 | 145 | static unsigned char delim; |
bb70624e | 146 | |
84c617ec CR |
147 | static struct ttsave termsave; |
148 | ||
ac50fbac CR |
149 | /* In all cases, SIGALRM just sets a flag that we check periodically. This |
150 | avoids problems with the semi-tricky stuff we do with the xfree of | |
151 | input_string at the top of the unwind-protect list (see below). */ | |
152 | ||
74091dd4 CR |
153 | /* Set a flag that check_read_timeout can check. This relies on zread or |
154 | read_builtin calling trap.c:check_signals() (which calls check_read_timeout()) */ | |
bb70624e JA |
155 | static sighandler |
156 | sigalrm (s) | |
157 | int s; | |
158 | { | |
74091dd4 CR |
159 | /* Display warning if this is called without read_timeout set? */ |
160 | if (read_timeout) | |
161 | read_timeout->alrmflag = 1; | |
bb70624e JA |
162 | } |
163 | ||
164 | static void | |
74091dd4 | 165 | reset_timeout () |
bb70624e | 166 | { |
a0c0a00f | 167 | /* Cancel alarm before restoring signal handler. */ |
74091dd4 CR |
168 | if (read_timeout) |
169 | shtimer_clear (read_timeout); | |
a6435a3c CR |
170 | #if defined (READLINE) |
171 | rl_clear_timeout (); | |
172 | #endif | |
74091dd4 CR |
173 | read_timeout = 0; |
174 | } | |
175 | ||
176 | void | |
177 | check_read_timeout () | |
178 | { | |
179 | if (read_timeout && shtimer_chktimeout (read_timeout)) | |
180 | sh_longjmp (read_timeout->jmpenv, 1); | |
181 | } | |
182 | ||
183 | int | |
184 | read_builtin_timeout (fd) | |
185 | int fd; | |
186 | { | |
187 | if ((read_timeout == 0) || | |
188 | (read_timeout->fd != fd) || | |
189 | (read_timeout->tmout.tv_sec == 0 && read_timeout->tmout.tv_usec == 0)) | |
190 | return 0; | |
191 | ||
192 | return ((read_timeout->flags & SHTIMER_ALARM) ? shtimer_alrm (read_timeout) | |
193 | : shtimer_select (read_timeout)); | |
bb70624e JA |
194 | } |
195 | ||
726f6388 JA |
196 | /* Read the value of the shell variables whose names follow. |
197 | The reading is done from the current input stream, whatever | |
198 | that may be. Successive words of the input line are assigned | |
199 | to the variables mentioned in LIST. The last variable in LIST | |
200 | gets the remainder of the words on the line. If no variables | |
ccc6cda3 JA |
201 | are mentioned in LIST, then the default variable is $REPLY. */ |
202 | int | |
726f6388 JA |
203 | read_builtin (list) |
204 | WORD_LIST *list; | |
205 | { | |
206 | register char *varname; | |
d233b485 | 207 | int size, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2, nflag; |
5b9762d6 | 208 | volatile int i; |
f1be666c | 209 | int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul; |
d233b485 CR |
210 | int raw, edit, nchars, silent, have_timeout, ignore_delim, fd; |
211 | int lastsig, t_errno; | |
212 | int mb_cur_max; | |
3185942a JA |
213 | unsigned int tmsec, tmusec; |
214 | long ival, uval; | |
7117c2d2 | 215 | intmax_t intval; |
ccc6cda3 JA |
216 | char c; |
217 | char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname; | |
f1be666c | 218 | char *e, *t, *t1, *ps2, *tofree; |
bb70624e | 219 | struct stat tsb; |
726f6388 | 220 | SHELL_VAR *var; |
3185942a | 221 | TTYSTRUCT ttattrs, ttset; |
74091dd4 | 222 | sigset_t chldset, prevset; |
ccc6cda3 JA |
223 | #if defined (ARRAY_VARS) |
224 | WORD_LIST *alist; | |
d233b485 | 225 | int vflags; |
ccc6cda3 | 226 | #endif |
74091dd4 | 227 | int bindflags; |
ccc6cda3 | 228 | #if defined (READLINE) |
3185942a | 229 | char *rlbuf, *itext; |
ccc6cda3 | 230 | int rlind; |
8868edaf | 231 | FILE *save_instream; |
ccc6cda3 | 232 | #endif |
726f6388 | 233 | |
f73dda09 JA |
234 | USE_VAR(size); |
235 | USE_VAR(i); | |
236 | USE_VAR(pass_next); | |
f1be666c | 237 | USE_VAR(print_ps2); |
f73dda09 JA |
238 | USE_VAR(saw_escape); |
239 | USE_VAR(input_is_pipe); | |
240 | /* USE_VAR(raw); */ | |
241 | USE_VAR(edit); | |
3185942a JA |
242 | USE_VAR(tmsec); |
243 | USE_VAR(tmusec); | |
f73dda09 JA |
244 | USE_VAR(nchars); |
245 | USE_VAR(silent); | |
246 | USE_VAR(ifs_chars); | |
247 | USE_VAR(prompt); | |
248 | USE_VAR(arrayname); | |
249 | #if defined (READLINE) | |
250 | USE_VAR(rlbuf); | |
251 | USE_VAR(rlind); | |
3185942a | 252 | USE_VAR(itext); |
f73dda09 JA |
253 | #endif |
254 | USE_VAR(list); | |
f1be666c | 255 | USE_VAR(ps2); |
ac50fbac CR |
256 | USE_VAR(lastsig); |
257 | ||
74091dd4 CR |
258 | reading = tty_modified = 0; |
259 | read_timeout = 0; | |
f73dda09 | 260 | |
726f6388 | 261 | i = 0; /* Index into the string that we are reading. */ |
ccc6cda3 | 262 | raw = edit = 0; /* Not reading raw input by default. */ |
bb70624e | 263 | silent = 0; |
ccc6cda3 | 264 | arrayname = prompt = (char *)NULL; |
7117c2d2 | 265 | fd = 0; /* file descriptor to read from */ |
ccc6cda3 JA |
266 | |
267 | #if defined (READLINE) | |
3185942a | 268 | rlbuf = itext = (char *)0; |
ccc6cda3 JA |
269 | rlind = 0; |
270 | #endif | |
726f6388 | 271 | |
d233b485 | 272 | mb_cur_max = MB_CUR_MAX; |
3185942a | 273 | tmsec = tmusec = 0; /* no timeout */ |
95732b49 | 274 | nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0; |
bb70624e | 275 | delim = '\n'; /* read until newline */ |
d233b485 | 276 | ignore_delim = nflag = 0; |
bb70624e | 277 | |
ccc6cda3 | 278 | reset_internal_getopt (); |
0001803f | 279 | while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1) |
726f6388 | 280 | { |
ccc6cda3 | 281 | switch (opt) |
28ef6c31 JA |
282 | { |
283 | case 'r': | |
ccc6cda3 | 284 | raw = 1; |
726f6388 | 285 | break; |
ccc6cda3 JA |
286 | case 'p': |
287 | prompt = list_optarg; | |
288 | break; | |
bb70624e JA |
289 | case 's': |
290 | silent = 1; | |
291 | break; | |
ccc6cda3 JA |
292 | case 'e': |
293 | #if defined (READLINE) | |
294 | edit = 1; | |
3185942a JA |
295 | #endif |
296 | break; | |
297 | case 'i': | |
298 | #if defined (READLINE) | |
299 | itext = list_optarg; | |
ccc6cda3 JA |
300 | #endif |
301 | break; | |
302 | #if defined (ARRAY_VARS) | |
303 | case 'a': | |
304 | arrayname = list_optarg; | |
305 | break; | |
306 | #endif | |
bb70624e | 307 | case 't': |
8868edaf | 308 | code = uconvert (list_optarg, &ival, &uval, (char **)NULL); |
3185942a | 309 | if (code == 0 || ival < 0 || uval < 0) |
bb70624e | 310 | { |
b80f6443 | 311 | builtin_error (_("%s: invalid timeout specification"), list_optarg); |
bb70624e JA |
312 | return (EXECUTION_FAILURE); |
313 | } | |
314 | else | |
f73dda09 JA |
315 | { |
316 | have_timeout = 1; | |
3185942a JA |
317 | tmsec = ival; |
318 | tmusec = uval; | |
f73dda09 | 319 | } |
bb70624e | 320 | break; |
0001803f CR |
321 | case 'N': |
322 | ignore_delim = 1; | |
323 | delim = -1; | |
bb70624e | 324 | case 'n': |
d233b485 | 325 | nflag = 1; |
7117c2d2 JA |
326 | code = legal_number (list_optarg, &intval); |
327 | if (code == 0 || intval < 0 || intval != (int)intval) | |
bb70624e | 328 | { |
7117c2d2 | 329 | sh_invalidnum (list_optarg); |
bb70624e JA |
330 | return (EXECUTION_FAILURE); |
331 | } | |
332 | else | |
7117c2d2 JA |
333 | nchars = intval; |
334 | break; | |
335 | case 'u': | |
336 | code = legal_number (list_optarg, &intval); | |
337 | if (code == 0 || intval < 0 || intval != (int)intval) | |
338 | { | |
b80f6443 | 339 | builtin_error (_("%s: invalid file descriptor specification"), list_optarg); |
7117c2d2 JA |
340 | return (EXECUTION_FAILURE); |
341 | } | |
342 | else | |
343 | fd = intval; | |
344 | if (sh_validfd (fd) == 0) | |
345 | { | |
b80f6443 | 346 | builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno)); |
7117c2d2 JA |
347 | return (EXECUTION_FAILURE); |
348 | } | |
bb70624e JA |
349 | break; |
350 | case 'd': | |
351 | delim = *list_optarg; | |
352 | break; | |
a0c0a00f | 353 | CASE_HELPOPT; |
ccc6cda3 JA |
354 | default: |
355 | builtin_usage (); | |
726f6388 JA |
356 | return (EX_USAGE); |
357 | } | |
726f6388 | 358 | } |
ccc6cda3 | 359 | list = loptend; |
726f6388 | 360 | |
17345e5a JA |
361 | /* `read -t 0 var' tests whether input is available with select/FIONREAD, |
362 | and fails if those are unavailable */ | |
3185942a | 363 | if (have_timeout && tmsec == 0 && tmusec == 0) |
3185942a | 364 | return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
bb70624e | 365 | |
ac50fbac CR |
366 | /* Convenience: check early whether or not the first of possibly several |
367 | variable names is a valid identifier, and bail early if so. */ | |
368 | #if defined (ARRAY_VARS) | |
74091dd4 CR |
369 | if (list) |
370 | SET_VFLAGS (list->word->flags, vflags, bindflags); | |
d233b485 | 371 | if (list && legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, vflags) == 0) |
ac50fbac | 372 | #else |
74091dd4 | 373 | bindflags = 0; |
ac50fbac CR |
374 | if (list && legal_identifier (list->word->word) == 0) |
375 | #endif | |
376 | { | |
377 | sh_invalidid (list->word->word); | |
378 | return (EXECUTION_FAILURE); | |
379 | } | |
380 | ||
0001803f CR |
381 | /* If we're asked to ignore the delimiter, make sure we do. */ |
382 | if (ignore_delim) | |
383 | delim = -1; | |
384 | ||
ccc6cda3 | 385 | /* IF IFS is unset, we use the default of " \t\n". */ |
7117c2d2 JA |
386 | ifs_chars = getifs (); |
387 | if (ifs_chars == 0) /* XXX - shouldn't happen */ | |
388 | ifs_chars = ""; | |
0001803f CR |
389 | /* If we want to read exactly NCHARS chars, don't split on IFS */ |
390 | if (ignore_delim) | |
391 | ifs_chars = ""; | |
3185942a JA |
392 | for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++) |
393 | skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL; | |
726f6388 | 394 | |
7117c2d2 | 395 | input_string = (char *)xmalloc (size = 112); /* XXX was 128 */ |
3185942a | 396 | input_string[0] = '\0'; |
7117c2d2 | 397 | |
d233b485 CR |
398 | /* More input and options validation */ |
399 | if (nflag == 1 && nchars == 0) | |
400 | { | |
401 | retval = read (fd, &c, 0); | |
402 | retval = (retval >= 0) ? EXECUTION_SUCCESS : EXECUTION_FAILURE; | |
403 | goto assign_vars; /* bail early if asked to read 0 chars */ | |
404 | } | |
405 | ||
7117c2d2 JA |
406 | /* $TMOUT, if set, is the default timeout for read. */ |
407 | if (have_timeout == 0 && (e = get_string_value ("TMOUT"))) | |
408 | { | |
8868edaf | 409 | code = uconvert (e, &ival, &uval, (char **)NULL); |
3185942a JA |
410 | if (code == 0 || ival < 0 || uval < 0) |
411 | tmsec = tmusec = 0; | |
7117c2d2 | 412 | else |
3185942a JA |
413 | { |
414 | tmsec = ival; | |
415 | tmusec = uval; | |
416 | } | |
7117c2d2 | 417 | } |
726f6388 | 418 | |
74091dd4 CR |
419 | #if defined (SIGCHLD) |
420 | sigemptyset (&chldset); | |
421 | sigprocmask (SIG_BLOCK, (sigset_t *)0, &chldset); | |
422 | sigaddset (&chldset, SIGCHLD); | |
423 | #endif | |
424 | ||
726f6388 | 425 | begin_unwind_frame ("read_builtin"); |
726f6388 | 426 | |
95732b49 JA |
427 | #if defined (BUFFERED_INPUT) |
428 | if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd)) | |
429 | sync_buffered_stream (default_buffered_input); | |
430 | #endif | |
431 | ||
d233b485 | 432 | #if 1 |
7117c2d2 | 433 | input_is_tty = isatty (fd); |
d233b485 CR |
434 | #else |
435 | input_is_tty = 1; | |
436 | #endif | |
bb70624e | 437 | if (input_is_tty == 0) |
28ef6c31 | 438 | #ifndef __CYGWIN__ |
95732b49 | 439 | input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE); |
bb70624e JA |
440 | #else |
441 | input_is_pipe = 1; | |
442 | #endif | |
443 | ||
444 | /* If the -p, -e or -s flags were given, but input is not coming from the | |
ccc6cda3 | 445 | terminal, turn them off. */ |
bb70624e | 446 | if ((prompt || edit || silent) && input_is_tty == 0) |
ccc6cda3 JA |
447 | { |
448 | prompt = (char *)NULL; | |
3185942a JA |
449 | #if defined (READLINE) |
450 | itext = (char *)NULL; | |
451 | #endif | |
bb70624e | 452 | edit = silent = 0; |
ccc6cda3 JA |
453 | } |
454 | ||
7117c2d2 JA |
455 | #if defined (READLINE) |
456 | if (edit) | |
457 | add_unwind_protect (xfree, rlbuf); | |
458 | #endif | |
459 | ||
726f6388 JA |
460 | pass_next = 0; /* Non-zero signifies last char was backslash. */ |
461 | saw_escape = 0; /* Non-zero signifies that we saw an escape char */ | |
462 | ||
3185942a | 463 | if (tmsec > 0 || tmusec > 0) |
bb70624e JA |
464 | { |
465 | /* Turn off the timeout if stdin is a regular file (e.g. from | |
466 | input redirection). */ | |
7117c2d2 | 467 | if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode)) |
3185942a | 468 | tmsec = tmusec = 0; |
bb70624e JA |
469 | } |
470 | ||
3185942a | 471 | if (tmsec > 0 || tmusec > 0) |
bb70624e | 472 | { |
74091dd4 CR |
473 | read_timeout = shtimer_alloc (); |
474 | read_timeout->flags = SHTIMER_LONGJMP; | |
475 | ||
476 | #if defined (HAVE_SELECT) | |
477 | read_timeout->flags |= (edit || posixly_correct) ? SHTIMER_ALARM : SHTIMER_SELECT; | |
478 | #else | |
479 | read_timeout->flags |= SHTIMER_ALARM; | |
480 | #endif | |
481 | read_timeout->fd = fd; | |
482 | ||
483 | read_timeout->alrm_handler = sigalrm; | |
484 | } | |
485 | ||
486 | if (tmsec > 0 || tmusec > 0) | |
487 | { | |
488 | code = setjmp_nosigs (read_timeout->jmpenv); | |
bb70624e JA |
489 | if (code) |
490 | { | |
74091dd4 CR |
491 | reset_timeout (); |
492 | sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0); | |
493 | ||
89a92869 CR |
494 | /* Tricky. The top of the unwind-protect stack is the free of |
495 | input_string. We want to run all the rest and use input_string, | |
43aebe92 | 496 | so we have to save input_string temporarily, run the unwind- |
ac50fbac CR |
497 | protects, then restore input_string so we can use it later */ |
498 | orig_input_string = 0; | |
3185942a | 499 | input_string[i] = '\0'; /* make sure it's terminated */ |
43aebe92 CR |
500 | if (i == 0) |
501 | { | |
502 | t = (char *)xmalloc (1); | |
503 | t[0] = 0; | |
504 | } | |
505 | else | |
506 | t = savestring (input_string); | |
507 | ||
508 | run_unwind_frame ("read_builtin"); | |
509 | input_string = t; | |
89a92869 | 510 | retval = 128+SIGALRM; |
3185942a | 511 | goto assign_vars; |
bb70624e | 512 | } |
84c617ec CR |
513 | if (interactive_shell == 0) |
514 | initialize_terminating_signals (); | |
74091dd4 | 515 | add_unwind_protect (reset_timeout, (char *)NULL); |
0628567a JA |
516 | #if defined (READLINE) |
517 | if (edit) | |
9b86eb22 CR |
518 | { |
519 | add_unwind_protect (reset_attempted_completion_function, (char *)NULL); | |
520 | add_unwind_protect (bashline_reset_event_hook, (char *)NULL); | |
74091dd4 | 521 | set_readline_timeout (read_timeout, tmsec, tmusec); |
9b86eb22 | 522 | } |
74091dd4 | 523 | else |
0628567a | 524 | #endif |
74091dd4 | 525 | shtimer_set (read_timeout, tmsec, tmusec); |
bb70624e JA |
526 | } |
527 | ||
528 | /* If we've been asked to read only NCHARS chars, or we're using some | |
529 | character other than newline to terminate the line, do the right | |
530 | thing to readline or the tty. */ | |
531 | if (nchars > 0 || delim != '\n') | |
532 | { | |
533 | #if defined (READLINE) | |
534 | if (edit) | |
535 | { | |
536 | if (nchars > 0) | |
537 | { | |
538 | unwind_protect_int (rl_num_chars_to_read); | |
539 | rl_num_chars_to_read = nchars; | |
540 | } | |
541 | if (delim != '\n') | |
542 | { | |
543 | set_eol_delim (delim); | |
544 | add_unwind_protect (reset_eol_delim, (char *)NULL); | |
545 | } | |
546 | } | |
547 | else | |
548 | #endif | |
549 | if (input_is_tty) | |
28ef6c31 | 550 | { |
3185942a JA |
551 | /* ttsave() */ |
552 | termsave.fd = fd; | |
553 | ttgetattr (fd, &ttattrs); | |
d233b485 | 554 | termsave.attrs = ttattrs; |
3185942a JA |
555 | |
556 | ttset = ttattrs; | |
17345e5a JA |
557 | i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset); |
558 | if (i < 0) | |
559 | sh_ttyerror (1); | |
84c617ec | 560 | tty_modified = 1; |
3185942a | 561 | add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); |
84c617ec CR |
562 | if (interactive_shell == 0) |
563 | initialize_terminating_signals (); | |
bb70624e JA |
564 | } |
565 | } | |
566 | else if (silent) /* turn off echo but leave term in canonical mode */ | |
567 | { | |
3185942a JA |
568 | /* ttsave (); */ |
569 | termsave.fd = fd; | |
570 | ttgetattr (fd, &ttattrs); | |
d233b485 | 571 | termsave.attrs = ttattrs; |
3185942a JA |
572 | |
573 | ttset = ttattrs; | |
17345e5a JA |
574 | i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */ |
575 | if (i < 0) | |
576 | sh_ttyerror (1); | |
3185942a | 577 | |
84c617ec | 578 | tty_modified = 1; |
3185942a | 579 | add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); |
84c617ec CR |
580 | if (interactive_shell == 0) |
581 | initialize_terminating_signals (); | |
bb70624e JA |
582 | } |
583 | ||
8868edaf CR |
584 | #if defined (READLINE) |
585 | save_instream = 0; | |
586 | if (edit && fd != 0) | |
587 | { | |
588 | if (bash_readline_initialized == 0) | |
589 | initialize_readline (); | |
590 | ||
591 | unwind_protect_var (rl_instream); | |
592 | save_instream = rl_instream; | |
593 | rl_instream = fdopen (fd, "r"); | |
594 | } | |
595 | #endif | |
596 | ||
28ef6c31 JA |
597 | /* This *must* be the top unwind-protect on the stack, so the manipulation |
598 | of the unwind-protect stack after the realloc() works right. */ | |
599 | add_unwind_protect (xfree, input_string); | |
28ef6c31 | 600 | |
74091dd4 CR |
601 | check_read_timeout (); |
602 | /* These only matter if edit == 0 */ | |
ac50fbac CR |
603 | if ((nchars > 0) && (input_is_tty == 0) && ignore_delim) /* read -N */ |
604 | unbuffered_read = 2; | |
74091dd4 | 605 | #if 0 |
ac50fbac | 606 | else if ((nchars > 0) || (delim != '\n') || input_is_pipe) |
74091dd4 CR |
607 | #else |
608 | else if (((nchars > 0 || delim != '\n') && input_is_tty) || input_is_pipe) | |
ac50fbac | 609 | unbuffered_read = 1; |
74091dd4 | 610 | #endif |
3185942a JA |
611 | if (prompt && edit == 0) |
612 | { | |
613 | fprintf (stderr, "%s", prompt); | |
614 | fflush (stderr); | |
615 | } | |
616 | ||
28ef6c31 JA |
617 | #if defined (__CYGWIN__) && defined (O_TEXT) |
618 | setmode (0, O_TEXT); | |
619 | #endif | |
620 | ||
f1be666c JA |
621 | ps2 = 0; |
622 | for (print_ps2 = eof = retval = 0;;) | |
726f6388 | 623 | { |
74091dd4 | 624 | check_read_timeout (); |
ac50fbac | 625 | |
ccc6cda3 JA |
626 | #if defined (READLINE) |
627 | if (edit) | |
628 | { | |
8868edaf CR |
629 | /* If we have a null delimiter, don't treat NULL as ending the line */ |
630 | if (rlbuf && rlbuf[rlind] == '\0' && delim != '\0') | |
ccc6cda3 | 631 | { |
d233b485 | 632 | free (rlbuf); |
ccc6cda3 JA |
633 | rlbuf = (char *)0; |
634 | } | |
74091dd4 CR |
635 | #if defined (SIGCHLD) |
636 | if (tmsec > 0 || tmusec > 0) | |
637 | sigprocmask (SIG_SETMASK, &chldset, &prevset); | |
638 | #endif | |
ccc6cda3 JA |
639 | if (rlbuf == 0) |
640 | { | |
ac50fbac | 641 | reading = 1; |
3185942a | 642 | rlbuf = edit_line (prompt ? prompt : "", itext); |
ac50fbac | 643 | reading = 0; |
ccc6cda3 JA |
644 | rlind = 0; |
645 | } | |
74091dd4 CR |
646 | #if defined (SIGCHLD) |
647 | if (tmsec > 0 || tmusec > 0) | |
648 | sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0); | |
649 | #endif | |
ccc6cda3 JA |
650 | if (rlbuf == 0) |
651 | { | |
652 | eof = 1; | |
653 | break; | |
654 | } | |
655 | c = rlbuf[rlind++]; | |
656 | } | |
657 | else | |
b72432fd | 658 | { |
e8ce775d JA |
659 | #endif |
660 | ||
f1be666c JA |
661 | if (print_ps2) |
662 | { | |
663 | if (ps2 == 0) | |
664 | ps2 = get_string_value ("PS2"); | |
665 | fprintf (stderr, "%s", ps2 ? ps2 : ""); | |
666 | fflush (stderr); | |
667 | print_ps2 = 0; | |
668 | } | |
669 | ||
ac50fbac | 670 | reading = 1; |
74091dd4 | 671 | check_read_timeout (); |
8868edaf | 672 | errno = 0; |
74091dd4 CR |
673 | |
674 | #if defined (SIGCHLD) | |
675 | if (tmsec > 0 || tmusec > 0) | |
676 | sigprocmask (SIG_SETMASK, &chldset, &prevset); | |
677 | #endif | |
ac50fbac CR |
678 | if (unbuffered_read == 2) |
679 | retval = posixly_correct ? zreadintr (fd, &c, 1) : zreadn (fd, &c, nchars - nr); | |
680 | else if (unbuffered_read) | |
681 | retval = posixly_correct ? zreadintr (fd, &c, 1) : zread (fd, &c, 1); | |
bb70624e | 682 | else |
ac50fbac | 683 | retval = posixly_correct ? zreadcintr (fd, &c) : zreadc (fd, &c); |
74091dd4 CR |
684 | #if defined (SIGCHLD) |
685 | if (tmsec > 0 || tmusec > 0) | |
686 | sigprocmask (SIG_SETMASK, &prevset, (sigset_t *)0); | |
687 | #endif | |
688 | ||
ac50fbac | 689 | reading = 0; |
bb70624e | 690 | |
e8ce775d | 691 | if (retval <= 0) |
ccc6cda3 | 692 | { |
8868edaf CR |
693 | int t; |
694 | ||
695 | t = errno; | |
ac50fbac CR |
696 | if (retval < 0 && errno == EINTR) |
697 | { | |
d233b485 | 698 | check_signals (); /* in case we didn't call zread via zreadc */ |
ac50fbac CR |
699 | lastsig = LASTSIG(); |
700 | if (lastsig == 0) | |
701 | lastsig = trapped_signal_received; | |
8868edaf | 702 | #if 0 |
ac50fbac | 703 | run_pending_traps (); /* because interrupt_immediately is not set */ |
8868edaf | 704 | #endif |
ac50fbac CR |
705 | } |
706 | else | |
707 | lastsig = 0; | |
84c617ec CR |
708 | if (terminating_signal && tty_modified) |
709 | ttyrestore (&termsave); /* fix terminal before exiting */ | |
ac50fbac | 710 | CHECK_TERMSIG; |
ccc6cda3 | 711 | eof = 1; |
8868edaf | 712 | errno = t; /* preserve it for the error message below */ |
ccc6cda3 JA |
713 | break; |
714 | } | |
715 | ||
eb78197a | 716 | QUIT; /* in case we didn't call check_signals() */ |
b72432fd JA |
717 | #if defined (READLINE) |
718 | } | |
719 | #endif | |
720 | ||
d233b485 | 721 | if (retval <= 0) /* XXX shouldn't happen */ |
74091dd4 | 722 | check_read_timeout (); |
d233b485 CR |
723 | |
724 | /* XXX -- use i + mb_cur_max (at least 4) for multibyte/read_mbchar */ | |
725 | if (i + (mb_cur_max > 4 ? mb_cur_max : 4) >= size) | |
28ef6c31 | 726 | { |
ac50fbac CR |
727 | char *t; |
728 | t = (char *)xrealloc (input_string, size += 128); | |
729 | ||
730 | /* Only need to change unwind-protect if input_string changes */ | |
731 | if (t != input_string) | |
732 | { | |
733 | input_string = t; | |
734 | remove_unwind_protect (); | |
735 | add_unwind_protect (xfree, input_string); | |
736 | } | |
28ef6c31 | 737 | } |
726f6388 JA |
738 | |
739 | /* If the next character is to be accepted verbatim, a backslash | |
740 | newline pair still disappears from the input. */ | |
741 | if (pass_next) | |
742 | { | |
95732b49 | 743 | pass_next = 0; |
726f6388 | 744 | if (c == '\n') |
f1be666c | 745 | { |
d233b485 CR |
746 | if (skip_ctlesc == 0 && i > 0) |
747 | i--; /* back up over the CTLESC */ | |
f1be666c JA |
748 | if (interactive && input_is_tty && raw == 0) |
749 | print_ps2 = 1; | |
750 | } | |
726f6388 | 751 | else |
95732b49 | 752 | goto add_char; |
726f6388 JA |
753 | continue; |
754 | } | |
755 | ||
3185942a | 756 | /* This may cause problems if IFS contains CTLESC */ |
ccc6cda3 | 757 | if (c == '\\' && raw == 0) |
726f6388 JA |
758 | { |
759 | pass_next++; | |
3185942a JA |
760 | if (skip_ctlesc == 0) |
761 | { | |
762 | saw_escape++; | |
763 | input_string[i++] = CTLESC; | |
764 | } | |
726f6388 JA |
765 | continue; |
766 | } | |
767 | ||
ac50fbac | 768 | if (ignore_delim == 0 && (unsigned char)c == delim) |
726f6388 JA |
769 | break; |
770 | ||
ac50fbac CR |
771 | if (c == '\0' && delim != '\0') |
772 | continue; /* skip NUL bytes in input */ | |
773 | ||
3185942a | 774 | if ((skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL)) |
726f6388 JA |
775 | { |
776 | saw_escape++; | |
777 | input_string[i++] = CTLESC; | |
778 | } | |
779 | ||
95732b49 | 780 | add_char: |
726f6388 | 781 | input_string[i++] = c; |
74091dd4 | 782 | check_read_timeout (); |
3185942a JA |
783 | |
784 | #if defined (HANDLE_MULTIBYTE) | |
d233b485 CR |
785 | /* XXX - what if C == 127? Can DEL introduce a multibyte sequence? */ |
786 | if (mb_cur_max > 1 && is_basic (c) == 0) | |
3185942a JA |
787 | { |
788 | input_string[i] = '\0'; /* for simplicity and debugging */ | |
d233b485 CR |
789 | /* If we got input from readline, grab the next multibyte char from |
790 | rlbuf. */ | |
791 | # if defined (READLINE) | |
792 | if (edit) | |
793 | { | |
794 | size_t clen; | |
795 | clen = mbrlen (rlbuf + rlind - 1, mb_cur_max, (mbstate_t *)NULL); | |
796 | /* We only deal with valid multibyte sequences longer than one | |
797 | byte. If we get anything else, we leave the one character | |
798 | copied and move on to the next. */ | |
799 | if ((int)clen > 1) | |
800 | { | |
801 | memcpy (input_string+i, rlbuf+rlind, clen-1); | |
802 | i += clen - 1; | |
803 | rlind += clen - 1; | |
804 | } | |
805 | } | |
806 | else | |
807 | # endif | |
808 | if (locale_utf8locale == 0 || ((c & 0x80) != 0)) | |
809 | i += read_mbchar (fd, input_string, i, c, unbuffered_read); | |
3185942a JA |
810 | } |
811 | #endif | |
812 | ||
95732b49 | 813 | nr++; |
bb70624e | 814 | |
95732b49 | 815 | if (nchars > 0 && nr >= nchars) |
bb70624e | 816 | break; |
726f6388 JA |
817 | } |
818 | input_string[i] = '\0'; | |
74091dd4 | 819 | check_read_timeout (); |
726f6388 | 820 | |
b3a5ec8d CR |
821 | #if defined (READLINE) |
822 | if (edit) | |
823 | free (rlbuf); | |
824 | #endif | |
825 | ||
7117c2d2 JA |
826 | if (retval < 0) |
827 | { | |
ac50fbac CR |
828 | t_errno = errno; |
829 | if (errno != EINTR) | |
830 | builtin_error (_("read error: %d: %s"), fd, strerror (errno)); | |
0628567a | 831 | run_unwind_frame ("read_builtin"); |
ac50fbac | 832 | return ((t_errno != EINTR) ? EXECUTION_FAILURE : 128+lastsig); |
7117c2d2 | 833 | } |
7117c2d2 | 834 | |
3185942a | 835 | if (tmsec > 0 || tmusec > 0) |
74091dd4 | 836 | reset_timeout (); |
bb70624e JA |
837 | |
838 | if (nchars > 0 || delim != '\n') | |
839 | { | |
840 | #if defined (READLINE) | |
841 | if (edit) | |
842 | { | |
843 | if (nchars > 0) | |
844 | rl_num_chars_to_read = 0; | |
845 | if (delim != '\n') | |
f73dda09 | 846 | reset_eol_delim ((char *)NULL); |
bb70624e JA |
847 | } |
848 | else | |
849 | #endif | |
850 | if (input_is_tty) | |
3185942a | 851 | ttyrestore (&termsave); |
bb70624e JA |
852 | } |
853 | else if (silent) | |
3185942a | 854 | ttyrestore (&termsave); |
bb70624e JA |
855 | |
856 | if (unbuffered_read == 0) | |
7117c2d2 | 857 | zsyncfd (fd); |
bb70624e | 858 | |
8868edaf CR |
859 | #if defined (READLINE) |
860 | if (save_instream) | |
861 | rl_instream = save_instream; /* can't portably free it */ | |
862 | #endif | |
863 | ||
726f6388 JA |
864 | discard_unwind_frame ("read_builtin"); |
865 | ||
ccc6cda3 | 866 | retval = eof ? EXECUTION_FAILURE : EXECUTION_SUCCESS; |
726f6388 | 867 | |
3185942a JA |
868 | assign_vars: |
869 | ||
ccc6cda3 JA |
870 | #if defined (ARRAY_VARS) |
871 | /* If -a was given, take the string read, break it into a list of words, | |
872 | an assign them to `arrayname' in turn. */ | |
873 | if (arrayname) | |
726f6388 | 874 | { |
74091dd4 CR |
875 | /* pass 1 for flags arg to clear the existing array + 2 to check for a |
876 | valid identifier. */ | |
877 | var = builtin_find_indexed_array (arrayname, 3); | |
ccc6cda3 | 878 | if (var == 0) |
0628567a | 879 | { |
d233b485 | 880 | free (input_string); |
0628567a JA |
881 | return EXECUTION_FAILURE; /* readonly or noassign */ |
882 | } | |
ccc6cda3 JA |
883 | |
884 | alist = list_string (input_string, ifs_chars, 0); | |
885 | if (alist) | |
886 | { | |
0628567a JA |
887 | if (saw_escape) |
888 | dequote_list (alist); | |
889 | else | |
890 | word_list_remove_quoted_nulls (alist); | |
95732b49 | 891 | assign_array_var_from_word_list (var, alist, 0); |
ccc6cda3 JA |
892 | dispose_words (alist); |
893 | } | |
d233b485 | 894 | free (input_string); |
ccc6cda3 | 895 | return (retval); |
726f6388 | 896 | } |
ccc6cda3 | 897 | #endif /* ARRAY_VARS */ |
726f6388 | 898 | |
d166f048 JA |
899 | /* If there are no variables, save the text of the line read to the |
900 | variable $REPLY. ksh93 strips leading and trailing IFS whitespace, | |
901 | so that `read x ; echo "$x"' and `read ; echo "$REPLY"' behave the | |
902 | same way, but I believe that the difference in behaviors is useful | |
903 | enough to not do it. Without the bash behavior, there is no way | |
28ef6c31 JA |
904 | to read a line completely without interpretation or modification |
905 | unless you mess with $IFS (e.g., setting it to the empty string). | |
d166f048 JA |
906 | If you disagree, change the occurrences of `#if 0' to `#if 1' below. */ |
907 | if (list == 0) | |
726f6388 | 908 | { |
d166f048 JA |
909 | #if 0 |
910 | orig_input_string = input_string; | |
7117c2d2 | 911 | for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++) |
d166f048 JA |
912 | ; |
913 | input_string = t; | |
914 | input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape); | |
915 | #endif | |
916 | ||
726f6388 JA |
917 | if (saw_escape) |
918 | { | |
919 | t = dequote_string (input_string); | |
95732b49 | 920 | var = bind_variable ("REPLY", t, 0); |
726f6388 JA |
921 | free (t); |
922 | } | |
923 | else | |
95732b49 | 924 | var = bind_variable ("REPLY", input_string, 0); |
a0c0a00f CR |
925 | if (var == 0 || readonly_p (var) || noassign_p (var)) |
926 | retval = EXECUTION_FAILURE; | |
927 | else | |
928 | VUNSETATTR (var, att_invisible); | |
28ef6c31 | 929 | |
d233b485 | 930 | free (input_string); |
ccc6cda3 | 931 | return (retval); |
726f6388 | 932 | } |
ccc6cda3 JA |
933 | |
934 | /* This code implements the Posix.2 spec for splitting the words | |
935 | read and assigning them to variables. */ | |
936 | orig_input_string = input_string; | |
937 | ||
938 | /* Remove IFS white space at the beginning of the input string. If | |
939 | $IFS is null, no field splitting is performed. */ | |
7117c2d2 | 940 | for (t = input_string; ifs_chars && *ifs_chars && spctabnl(*t) && isifs(*t); t++) |
ccc6cda3 JA |
941 | ; |
942 | input_string = t; | |
ccc6cda3 | 943 | for (; list->next; list = list->next) |
726f6388 | 944 | { |
ccc6cda3 JA |
945 | varname = list->word->word; |
946 | #if defined (ARRAY_VARS) | |
74091dd4 | 947 | SET_VFLAGS (list->word->flags, vflags, bindflags); |
d233b485 | 948 | if (legal_identifier (varname) == 0 && valid_array_reference (varname, vflags) == 0) |
ccc6cda3 JA |
949 | #else |
950 | if (legal_identifier (varname) == 0) | |
951 | #endif | |
726f6388 | 952 | { |
7117c2d2 | 953 | sh_invalidid (varname); |
d233b485 | 954 | free (orig_input_string); |
ccc6cda3 JA |
955 | return (EXECUTION_FAILURE); |
956 | } | |
726f6388 | 957 | |
ccc6cda3 JA |
958 | /* If there are more variables than words read from the input, |
959 | the remaining variables are set to the empty string. */ | |
960 | if (*input_string) | |
961 | { | |
962 | /* This call updates INPUT_STRING. */ | |
963 | t = get_word_from_string (&input_string, ifs_chars, &e); | |
964 | if (t) | |
965 | *e = '\0'; | |
966 | /* Don't bother to remove the CTLESC unless we added one | |
967 | somewhere while reading the string. */ | |
968 | if (t && saw_escape) | |
726f6388 | 969 | { |
ccc6cda3 | 970 | t1 = dequote_string (t); |
74091dd4 | 971 | var = bind_read_variable (varname, t1, bindflags); |
d233b485 | 972 | free (t1); |
726f6388 JA |
973 | } |
974 | else | |
74091dd4 | 975 | var = bind_read_variable (varname, t ? t : "", bindflags); |
ccc6cda3 JA |
976 | } |
977 | else | |
978 | { | |
979 | t = (char *)0; | |
74091dd4 | 980 | var = bind_read_variable (varname, "", bindflags); |
726f6388 JA |
981 | } |
982 | ||
ccc6cda3 JA |
983 | FREE (t); |
984 | if (var == 0) | |
726f6388 | 985 | { |
d233b485 | 986 | free (orig_input_string); |
726f6388 JA |
987 | return (EXECUTION_FAILURE); |
988 | } | |
989 | ||
ccc6cda3 | 990 | stupidly_hack_special_variables (varname); |
bb70624e | 991 | VUNSETATTR (var, att_invisible); |
ccc6cda3 JA |
992 | } |
993 | ||
994 | /* Now assign the rest of the line to the last variable argument. */ | |
995 | #if defined (ARRAY_VARS) | |
74091dd4 | 996 | SET_VFLAGS (list->word->flags, vflags, bindflags); |
d233b485 | 997 | if (legal_identifier (list->word->word) == 0 && valid_array_reference (list->word->word, vflags) == 0) |
ccc6cda3 JA |
998 | #else |
999 | if (legal_identifier (list->word->word) == 0) | |
1000 | #endif | |
1001 | { | |
7117c2d2 | 1002 | sh_invalidid (list->word->word); |
d233b485 | 1003 | free (orig_input_string); |
ccc6cda3 | 1004 | return (EXECUTION_FAILURE); |
726f6388 JA |
1005 | } |
1006 | ||
95732b49 | 1007 | #if 0 |
ccc6cda3 JA |
1008 | /* This has to be done this way rather than using string_list |
1009 | and list_string because Posix.2 says that the last variable gets the | |
1010 | remaining words and their intervening separators. */ | |
1011 | input_string = strip_trailing_ifs_whitespace (input_string, ifs_chars, saw_escape); | |
95732b49 JA |
1012 | #else |
1013 | /* Check whether or not the number of fields is exactly the same as the | |
1014 | number of variables. */ | |
f1be666c | 1015 | tofree = NULL; |
95732b49 JA |
1016 | if (*input_string) |
1017 | { | |
1018 | t1 = input_string; | |
1019 | t = get_word_from_string (&input_string, ifs_chars, &e); | |
1020 | if (*input_string == 0) | |
f1be666c | 1021 | tofree = input_string = t; |
95732b49 | 1022 | else |
89a92869 CR |
1023 | { |
1024 | input_string = strip_trailing_ifs_whitespace (t1, ifs_chars, saw_escape); | |
1025 | tofree = t; | |
1026 | } | |
95732b49 JA |
1027 | } |
1028 | #endif | |
ccc6cda3 | 1029 | |
0213a83a | 1030 | if (saw_escape && input_string && *input_string) |
ccc6cda3 JA |
1031 | { |
1032 | t = dequote_string (input_string); | |
74091dd4 | 1033 | var = bind_read_variable (list->word->word, t, bindflags); |
d233b485 | 1034 | free (t); |
ccc6cda3 JA |
1035 | } |
1036 | else | |
74091dd4 | 1037 | var = bind_read_variable (list->word->word, input_string ? input_string : "", bindflags); |
f1be666c | 1038 | |
ccc6cda3 | 1039 | if (var) |
495aee44 CR |
1040 | { |
1041 | stupidly_hack_special_variables (list->word->word); | |
1042 | VUNSETATTR (var, att_invisible); | |
1043 | } | |
1044 | else | |
1045 | retval = EXECUTION_FAILURE; | |
1046 | ||
1047 | FREE (tofree); | |
d233b485 | 1048 | free (orig_input_string); |
ccc6cda3 | 1049 | |
726f6388 JA |
1050 | return (retval); |
1051 | } | |
1052 | ||
ccc6cda3 | 1053 | static SHELL_VAR * |
74091dd4 | 1054 | bind_read_variable (name, value, flags) |
ccc6cda3 | 1055 | char *name, *value; |
74091dd4 | 1056 | int flags; |
ccc6cda3 | 1057 | { |
495aee44 | 1058 | SHELL_VAR *v; |
ac50fbac | 1059 | |
74091dd4 | 1060 | v = builtin_bind_variable (name, value, flags); |
495aee44 CR |
1061 | return (v == 0 ? v |
1062 | : ((readonly_p (v) || noassign_p (v)) ? (SHELL_VAR *)NULL : v)); | |
ccc6cda3 JA |
1063 | } |
1064 | ||
3185942a JA |
1065 | #if defined (HANDLE_MULTIBYTE) |
1066 | static int | |
1067 | read_mbchar (fd, string, ind, ch, unbuffered) | |
1068 | int fd; | |
1069 | char *string; | |
1070 | int ind, ch, unbuffered; | |
1071 | { | |
1072 | char mbchar[MB_LEN_MAX + 1]; | |
1073 | int i, n, r; | |
1074 | char c; | |
1075 | size_t ret; | |
1076 | mbstate_t ps, ps_back; | |
1077 | wchar_t wc; | |
1078 | ||
1079 | memset (&ps, '\0', sizeof (mbstate_t)); | |
1080 | memset (&ps_back, '\0', sizeof (mbstate_t)); | |
1081 | ||
1082 | mbchar[0] = ch; | |
1083 | i = 1; | |
1084 | for (n = 0; n <= MB_LEN_MAX; n++) | |
1085 | { | |
1086 | ps_back = ps; | |
1087 | ret = mbrtowc (&wc, mbchar, i, &ps); | |
1088 | if (ret == (size_t)-2) | |
1089 | { | |
1090 | ps = ps_back; | |
d233b485 | 1091 | |
ac50fbac | 1092 | /* We don't want to be interrupted during a multibyte char read */ |
d233b485 CR |
1093 | if (unbuffered == 2) |
1094 | r = zreadn (fd, &c, 1); | |
1095 | else if (unbuffered) | |
3185942a JA |
1096 | r = zread (fd, &c, 1); |
1097 | else | |
1098 | r = zreadc (fd, &c); | |
d233b485 | 1099 | if (r <= 0) |
3185942a JA |
1100 | goto mbchar_return; |
1101 | mbchar[i++] = c; | |
1102 | continue; | |
1103 | } | |
1104 | else if (ret == (size_t)-1 || ret == (size_t)0 || ret > (size_t)0) | |
1105 | break; | |
1106 | } | |
1107 | ||
1108 | mbchar_return: | |
1109 | if (i > 1) /* read a multibyte char */ | |
1110 | /* mbchar[0] is already string[ind-1] */ | |
1111 | for (r = 1; r < i; r++) | |
1112 | string[ind+r-1] = mbchar[r]; | |
1113 | return i - 1; | |
1114 | } | |
1115 | #endif | |
1116 | ||
1117 | ||
1118 | static void | |
1119 | ttyrestore (ttp) | |
1120 | struct ttsave *ttp; | |
1121 | { | |
d233b485 | 1122 | ttsetattr (ttp->fd, &(ttp->attrs)); |
84c617ec CR |
1123 | tty_modified = 0; |
1124 | } | |
1125 | ||
1126 | void | |
1127 | read_tty_cleanup () | |
1128 | { | |
1129 | if (tty_modified) | |
1130 | ttyrestore (&termsave); | |
1131 | } | |
1132 | ||
1133 | int | |
1134 | read_tty_modified () | |
1135 | { | |
1136 | return (tty_modified); | |
3185942a JA |
1137 | } |
1138 | ||
ccc6cda3 | 1139 | #if defined (READLINE) |
0628567a | 1140 | static rl_completion_func_t *old_attempted_completion_function = 0; |
3185942a JA |
1141 | static rl_hook_func_t *old_startup_hook; |
1142 | static char *deftext; | |
0628567a JA |
1143 | |
1144 | static void | |
1145 | reset_attempted_completion_function (cp) | |
1146 | char *cp; | |
1147 | { | |
1148 | if (rl_attempted_completion_function == 0 && old_attempted_completion_function) | |
1149 | rl_attempted_completion_function = old_attempted_completion_function; | |
1150 | } | |
7117c2d2 | 1151 | |
3185942a JA |
1152 | static int |
1153 | set_itext () | |
1154 | { | |
1155 | int r1, r2; | |
1156 | ||
1157 | r1 = r2 = 0; | |
1158 | if (old_startup_hook) | |
1159 | r1 = (*old_startup_hook) (); | |
1160 | if (deftext) | |
1161 | { | |
1162 | r2 = rl_insert_text (deftext); | |
1163 | deftext = (char *)NULL; | |
1164 | rl_startup_hook = old_startup_hook; | |
1165 | old_startup_hook = (rl_hook_func_t *)NULL; | |
1166 | } | |
1167 | return (r1 || r2); | |
1168 | } | |
1169 | ||
ccc6cda3 | 1170 | static char * |
3185942a | 1171 | edit_line (p, itext) |
ccc6cda3 | 1172 | char *p; |
3185942a | 1173 | char *itext; |
726f6388 | 1174 | { |
ccc6cda3 JA |
1175 | char *ret; |
1176 | int len; | |
1177 | ||
95732b49 | 1178 | if (bash_readline_initialized == 0) |
ccc6cda3 | 1179 | initialize_readline (); |
0628567a | 1180 | |
7117c2d2 JA |
1181 | old_attempted_completion_function = rl_attempted_completion_function; |
1182 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; | |
9b86eb22 | 1183 | bashline_set_event_hook (); |
3185942a JA |
1184 | if (itext) |
1185 | { | |
1186 | old_startup_hook = rl_startup_hook; | |
1187 | rl_startup_hook = set_itext; | |
1188 | deftext = itext; | |
1189 | } | |
ac50fbac | 1190 | |
ccc6cda3 | 1191 | ret = readline (p); |
ac50fbac | 1192 | |
7117c2d2 | 1193 | rl_attempted_completion_function = old_attempted_completion_function; |
0628567a | 1194 | old_attempted_completion_function = (rl_completion_func_t *)NULL; |
9b86eb22 | 1195 | bashline_reset_event_hook (); |
0628567a | 1196 | |
ccc6cda3 | 1197 | if (ret == 0) |
74091dd4 CR |
1198 | { |
1199 | if (RL_ISSTATE (RL_STATE_TIMEOUT)) | |
1200 | { | |
1201 | sigalrm (SIGALRM); /* simulate receiving SIGALRM */ | |
1202 | check_read_timeout (); | |
1203 | } | |
1204 | return ret; | |
1205 | } | |
1206 | ||
ccc6cda3 | 1207 | len = strlen (ret); |
f73dda09 | 1208 | ret = (char *)xrealloc (ret, len + 2); |
bb70624e | 1209 | ret[len++] = delim; |
ccc6cda3 JA |
1210 | ret[len] = '\0'; |
1211 | return ret; | |
726f6388 | 1212 | } |
bb70624e | 1213 | |
74091dd4 CR |
1214 | static void |
1215 | set_readline_timeout (t, sec, usec) | |
1216 | sh_timer *t; | |
1217 | time_t sec; | |
1218 | long usec; | |
1219 | { | |
1220 | t->tmout.tv_sec = sec; | |
1221 | t->tmout.tv_usec = usec; | |
1222 | rl_set_timeout (sec, usec); | |
1223 | } | |
1224 | ||
bb70624e | 1225 | static int old_delim_ctype; |
f73dda09 | 1226 | static rl_command_func_t *old_delim_func; |
bb70624e | 1227 | static int old_newline_ctype; |
f73dda09 | 1228 | static rl_command_func_t *old_newline_func; |
bb70624e | 1229 | |
f73dda09 | 1230 | static unsigned char delim_char; |
bb70624e JA |
1231 | |
1232 | static void | |
1233 | set_eol_delim (c) | |
1234 | int c; | |
1235 | { | |
1236 | Keymap cmap; | |
1237 | ||
1238 | if (bash_readline_initialized == 0) | |
1239 | initialize_readline (); | |
1240 | cmap = rl_get_keymap (); | |
1241 | ||
8868edaf | 1242 | /* Save the old delimiter char binding */ |
bb70624e JA |
1243 | old_newline_ctype = cmap[RETURN].type; |
1244 | old_newline_func = cmap[RETURN].function; | |
8868edaf CR |
1245 | old_delim_ctype = cmap[c].type; |
1246 | old_delim_func = cmap[c].function; | |
1247 | ||
1248 | /* Change newline to self-insert */ | |
bb70624e JA |
1249 | cmap[RETURN].type = ISFUNC; |
1250 | cmap[RETURN].function = rl_insert; | |
1251 | ||
1252 | /* Bind the delimiter character to accept-line. */ | |
bb70624e JA |
1253 | cmap[c].type = ISFUNC; |
1254 | cmap[c].function = rl_newline; | |
1255 | ||
1256 | delim_char = c; | |
1257 | } | |
1258 | ||
1259 | static void | |
f73dda09 JA |
1260 | reset_eol_delim (cp) |
1261 | char *cp; | |
bb70624e JA |
1262 | { |
1263 | Keymap cmap; | |
1264 | ||
1265 | cmap = rl_get_keymap (); | |
1266 | ||
1267 | cmap[RETURN].type = old_newline_ctype; | |
1268 | cmap[RETURN].function = old_newline_func; | |
1269 | ||
1270 | cmap[delim_char].type = old_delim_ctype; | |
1271 | cmap[delim_char].function = old_delim_func; | |
1272 | } | |
ccc6cda3 | 1273 | #endif |