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