📄 read.def
字号:
This file is read.def, from which is created read.c.It implements the builtin "read" in Bash.Copyright (C) 1987-2010 Free Software Foundation, Inc.This file is part of GNU Bash, the Bourne Again SHell.Bash is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.Bash is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Bash. If not, see <http://www.gnu.org/licenses/>.$PRODUCES read.c$BUILTIN read$FUNCTION read_builtin$SHORT_DOC read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]Read a line from the standard input and split it into fields.Reads a single line from the standard input, or from file descriptor FDif the -u option is supplied. The line is split into fields as with wordsplitting, and the first word is assigned to the first NAME, the secondword to the second NAME, and so on, with any leftover words assigned tothe last NAME. Only the characters found in $IFS are recognized as worddelimiters.If no NAMEs are supplied, the line read is stored in the REPLY variable.Options: -a array assign the words read to sequential indices of the array variable ARRAY, starting at zero -d delim continue until the first character of DELIM is read, rather than newline -e use Readline to obtain the line in an interactive shell -i text Use TEXT as the initial text for Readline -n nchars return after reading NCHARS characters rather than waiting for a newline, but honor a delimiter if fewer than NCHARS characters are read before the delimiter -N nchars return only after reading exactly NCHARS characters, unless EOF is encountered or read times out, ignoring any delimiter -p prompt output the string PROMPT without a trailing newline before attempting to read -r do not allow backslashes to escape any characters -s do not echo input coming from a terminal -t timeout time out and return failure if a complete line of input is not read withint TIMEOUT seconds. The value of the TMOUT variable is the default timeout. TIMEOUT may be a fractional number. If TIMEOUT is 0, read returns success only if input is available on the specified file descriptor. The exit status is greater than 128 if the timeout is exceeded -u fd read from file descriptor FD instead of the standard inputExit Status:The return code is zero, unless end-of-file is encountered, read times out,or an invalid file descriptor is supplied as the argument to -u.$END#include <config.h>#include "bashtypes.h"#include "posixstat.h"#include <stdio.h>#include "bashansi.h"#if defined (HAVE_UNISTD_H)# include <unistd.h>#endif#include <signal.h>#include <errno.h>#ifdef __CYGWIN__# include <fcntl.h># include <io.h>#endif#include "../bashintl.h"#include "../shell.h"#include "common.h"#include "bashgetopt.h"#include <shtty.h>#if defined (READLINE)#include "../bashline.h"#include <readline/readline.h>#endif#if defined (BUFFERED_INPUT)# include "input.h"#endif#if !defined(errno)extern int errno;#endifstruct ttsave{ int fd; TTYSTRUCT *attrs;};#if defined (READLINE)static void reset_attempted_completion_function __P((char *));static int set_itext __P((void));static char *edit_line __P((char *, char *));static void set_eol_delim __P((int));static void reset_eol_delim __P((char *));#endifstatic SHELL_VAR *bind_read_variable __P((char *, char *));#if defined (HANDLE_MULTIBYTE)static int read_mbchar __P((int, char *, int, int, int));#endifstatic void ttyrestore __P((struct ttsave *));static sighandler sigalrm __P((int));static void reset_alarm __P((void));static procenv_t alrmbuf;static SigHandler *old_alrm;static unsigned char delim;static sighandlersigalrm (s) int s;{ longjmp (alrmbuf, 1);}static voidreset_alarm (){ set_signal_handler (SIGALRM, old_alrm); falarm (0, 0);}/* Read the value of the shell variables whose names follow. The reading is done from the current input stream, whatever that may be. Successive words of the input line are assigned to the variables mentioned in LIST. The last variable in LIST gets the remainder of the words on the line. If no variables are mentioned in LIST, then the default variable is $REPLY. */intread_builtin (list) WORD_LIST *list;{ register char *varname; int size, i, nr, pass_next, saw_escape, eof, opt, retval, code, print_ps2; int input_is_tty, input_is_pipe, unbuffered_read, skip_ctlesc, skip_ctlnul; int raw, edit, nchars, silent, have_timeout, ignore_delim, fd; unsigned int tmsec, tmusec; long ival, uval; intmax_t intval; char c; char *input_string, *orig_input_string, *ifs_chars, *prompt, *arrayname; char *e, *t, *t1, *ps2, *tofree; struct stat tsb; SHELL_VAR *var; TTYSTRUCT ttattrs, ttset; struct ttsave termsave;#if defined (ARRAY_VARS) WORD_LIST *alist;#endif#if defined (READLINE) char *rlbuf, *itext; int rlind;#endif USE_VAR(size); USE_VAR(i); USE_VAR(pass_next); USE_VAR(print_ps2); USE_VAR(saw_escape); USE_VAR(input_is_pipe);/* USE_VAR(raw); */ USE_VAR(edit); USE_VAR(tmsec); USE_VAR(tmusec); USE_VAR(nchars); USE_VAR(silent); USE_VAR(ifs_chars); USE_VAR(prompt); USE_VAR(arrayname);#if defined (READLINE) USE_VAR(rlbuf); USE_VAR(rlind); USE_VAR(itext);#endif USE_VAR(list); USE_VAR(ps2); i = 0; /* Index into the string that we are reading. */ raw = edit = 0; /* Not reading raw input by default. */ silent = 0; arrayname = prompt = (char *)NULL; fd = 0; /* file descriptor to read from */#if defined (READLINE) rlbuf = itext = (char *)0; rlind = 0;#endif tmsec = tmusec = 0; /* no timeout */ nr = nchars = input_is_tty = input_is_pipe = unbuffered_read = have_timeout = 0; delim = '\n'; /* read until newline */ ignore_delim = 0; reset_internal_getopt (); while ((opt = internal_getopt (list, "ersa:d:i:n:p:t:u:N:")) != -1) { switch (opt) { case 'r': raw = 1; break; case 'p': prompt = list_optarg; break; case 's': silent = 1; break; case 'e':#if defined (READLINE) edit = 1;#endif break; case 'i':#if defined (READLINE) itext = list_optarg;#endif break;#if defined (ARRAY_VARS) case 'a': arrayname = list_optarg; break;#endif case 't': code = uconvert (list_optarg, &ival, &uval); if (code == 0 || ival < 0 || uval < 0) { builtin_error (_("%s: invalid timeout specification"), list_optarg); return (EXECUTION_FAILURE); } else { have_timeout = 1; tmsec = ival; tmusec = uval; } break; case 'N': ignore_delim = 1; delim = -1; case 'n': code = legal_number (list_optarg, &intval); if (code == 0 || intval < 0 || intval != (int)intval) { sh_invalidnum (list_optarg); return (EXECUTION_FAILURE); } else nchars = intval; break; case 'u': code = legal_number (list_optarg, &intval); if (code == 0 || intval < 0 || intval != (int)intval) { builtin_error (_("%s: invalid file descriptor specification"), list_optarg); return (EXECUTION_FAILURE); } else fd = intval; if (sh_validfd (fd) == 0) { builtin_error (_("%d: invalid file descriptor: %s"), fd, strerror (errno)); return (EXECUTION_FAILURE); } break; case 'd': delim = *list_optarg; break; default: builtin_usage (); return (EX_USAGE); } } list = loptend; /* `read -t 0 var' tests whether input is available with select/FIONREAD, and fails if those are unavailable */ if (have_timeout && tmsec == 0 && tmusec == 0)#if 0 return (EXECUTION_FAILURE);#else return (input_avail (fd) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);#endif /* If we're asked to ignore the delimiter, make sure we do. */ if (ignore_delim) delim = -1; /* IF IFS is unset, we use the default of " \t\n". */ ifs_chars = getifs (); if (ifs_chars == 0) /* XXX - shouldn't happen */ ifs_chars = ""; /* If we want to read exactly NCHARS chars, don't split on IFS */ if (ignore_delim) ifs_chars = ""; for (skip_ctlesc = skip_ctlnul = 0, e = ifs_chars; *e; e++) skip_ctlesc |= *e == CTLESC, skip_ctlnul |= *e == CTLNUL; input_string = (char *)xmalloc (size = 112); /* XXX was 128 */ input_string[0] = '\0'; /* $TMOUT, if set, is the default timeout for read. */ if (have_timeout == 0 && (e = get_string_value ("TMOUT"))) { code = uconvert (e, &ival, &uval); if (code == 0 || ival < 0 || uval < 0) tmsec = tmusec = 0; else { tmsec = ival; tmusec = uval; } } begin_unwind_frame ("read_builtin");#if defined (BUFFERED_INPUT) if (interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input (fd)) sync_buffered_stream (default_buffered_input);#endif input_is_tty = isatty (fd); if (input_is_tty == 0)#ifndef __CYGWIN__ input_is_pipe = (lseek (fd, 0L, SEEK_CUR) < 0) && (errno == ESPIPE);#else input_is_pipe = 1;#endif /* If the -p, -e or -s flags were given, but input is not coming from the terminal, turn them off. */ if ((prompt || edit || silent) && input_is_tty == 0) { prompt = (char *)NULL;#if defined (READLINE) itext = (char *)NULL;#endif edit = silent = 0; }#if defined (READLINE) if (edit) add_unwind_protect (xfree, rlbuf);#endif pass_next = 0; /* Non-zero signifies last char was backslash. */ saw_escape = 0; /* Non-zero signifies that we saw an escape char */ if (tmsec > 0 || tmusec > 0) { /* Turn off the timeout if stdin is a regular file (e.g. from input redirection). */ if ((fstat (fd, &tsb) < 0) || S_ISREG (tsb.st_mode)) tmsec = tmusec = 0; } if (tmsec > 0 || tmusec > 0) { code = setjmp (alrmbuf); if (code) { /* Tricky. The top of the unwind-protect stack is the free of input_string. We want to run all the rest and use input_string, so we have to remove it from the stack. */ remove_unwind_protect (); run_unwind_frame ("read_builtin"); input_string[i] = '\0'; /* make sure it's terminated */ retval = 128+SIGALRM; goto assign_vars; } old_alrm = set_signal_handler (SIGALRM, sigalrm); add_unwind_protect (reset_alarm, (char *)NULL);#if defined (READLINE) if (edit) add_unwind_protect (reset_attempted_completion_function, (char *)NULL);#endif falarm (tmsec, tmusec); } /* If we've been asked to read only NCHARS chars, or we're using some character other than newline to terminate the line, do the right thing to readline or the tty. */ if (nchars > 0 || delim != '\n') {#if defined (READLINE) if (edit) { if (nchars > 0) { unwind_protect_int (rl_num_chars_to_read); rl_num_chars_to_read = nchars; } if (delim != '\n') { set_eol_delim (delim); add_unwind_protect (reset_eol_delim, (char *)NULL); } } else#endif if (input_is_tty) { /* ttsave() */ termsave.fd = fd; ttgetattr (fd, &ttattrs); termsave.attrs = &ttattrs; ttset = ttattrs; i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset); if (i < 0) sh_ttyerror (1); add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); } } else if (silent) /* turn off echo but leave term in canonical mode */ { /* ttsave (); */ termsave.fd = fd; ttgetattr (fd, &ttattrs); termsave.attrs = &ttattrs; ttset = ttattrs; i = ttfd_noecho (fd, &ttset); /* ttnoecho (); */ if (i < 0) sh_ttyerror (1); add_unwind_protect ((Function *)ttyrestore, (char *)&termsave); } /* This *must* be the top unwind-protect on the stack, so the manipulation of the unwind-protect stack after the realloc() works right. */ add_unwind_protect (xfree, input_string); interrupt_immediately++; terminate_immediately++; unbuffered_read = (nchars > 0) || (delim != '\n') || input_is_pipe; if (prompt && edit == 0) { fprintf (stderr, "%s", prompt); fflush (stderr); }#if defined (__CYGWIN__) && defined (O_TEXT) setmode (0, O_TEXT);#endif ps2 = 0; for (print_ps2 = eof = retval = 0;;) {#if defined (READLINE) if (edit) { if (rlbuf && rlbuf[rlind] == '\0') { xfree (rlbuf); rlbuf = (char *)0; } if (rlbuf == 0) { rlbuf = edit_line (prompt ? prompt : "", itext); rlind = 0; } if (rlbuf == 0) { eof = 1; break; } c = rlbuf[rlind++]; } else {#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -