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