1 /* vi: set sw=4 ts=4: */
3 * Adapted from ash applet code
5 * This code is derived from software contributed to Berkeley by
8 * Copyright (c) 1989, 1991, 1993, 1994
9 * The Regents of the University of California. All rights reserved.
11 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
12 * was re-ported from NetBSD and debianized.
14 * Copyright (c) 2010 Denys Vlasenko
17 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
20 #include "shell_common.h"
21 #include "builtin_read.h"
23 //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
24 //string. hush naturally has it, and ash has setvareq().
25 //Here we can simply store "VAR=" at buffer start and store read data directly
26 //after "=", then pass buffer to setvar() to consume.
29 shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
39 unsigned end_ms; /* -t TIMEOUT */
41 int nchars; /* -n NUM */
44 struct termios tty, old_tty;
46 int bufpos; /* need to be able to hold -1 */
52 if (!is_well_formed_var_name(*pp, '\0')) {
53 /* Mimic bash message */
54 bb_error_msg("read: '%s': not a valid identifier", *pp);
55 return (const char *)(uintptr_t)1;
60 nchars = 0; /* if != 0, -n is in effect */
62 nchars = bb_strtou(opt_n, NULL, 10);
63 if (nchars < 0 || errno)
64 return "invalid count";
65 /* note: "-n 0": off (bash 3.2 does this too) */
69 end_ms = bb_strtou(opt_t, NULL, 10);
70 if (errno || end_ms > UINT_MAX / 2048)
71 return "invalid timeout";
73 #if 0 /* even bash has no -t N.NNN support */
74 ts.tv_sec = bb_strtou(opt_t, &p, 10);
76 /* EINVAL means number is ok, but not terminated by NUL */
77 if (*p == '.' && errno == EINVAL) {
81 ts.tv_usec = bb_strtou(p, &p2, 10);
83 return "invalid timeout";
85 /* normalize to usec */
87 return "invalid timeout";
91 } else if (ts.tv_sec < 0 || errno) {
92 return "invalid timeout";
94 if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
95 return "invalid timeout";
101 fd = bb_strtou(opt_u, NULL, 10);
103 return "invalid file descriptor";
106 if (opt_p && isatty(fd)) {
107 fputs(opt_p, stderr);
114 if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
118 tty.c_lflag &= ~ICANON;
119 tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
121 if (read_flags & BUILTIN_READ_SILENT) {
122 tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
124 /* This forces execution of "restoring" tcgetattr later */
125 read_flags |= BUILTIN_READ_SILENT;
126 /* if tcgetattr failed, tcsetattr will fail too.
127 * Ignoring, it's harmless. */
128 tcsetattr(fd, TCSANOW, &tty);
131 retval = (const char *)(uintptr_t)0;
134 if (end_ms) /* NB: end_ms stays nonzero: */
135 end_ms = ((unsigned)monotonic_ms() + end_ms) | 1;
143 struct pollfd pfd[1];
146 pfd[0].events = POLLIN;
147 timeout = end_ms - (unsigned)monotonic_ms();
148 if (timeout <= 0 /* already late? */
149 || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
151 retval = (const char *)(uintptr_t)1;
156 if ((bufpos & 0xff) == 0)
157 buffer = xrealloc(buffer, bufpos + 0x100);
158 if (nonblock_safe_read(fd, &buffer[bufpos], 1) != 1) {
159 retval = (const char *)(uintptr_t)1;
171 if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
178 /* $IFS splitting. NOT done if we run "read"
179 * without variable names (bash compat).
180 * Thus, "read" and "read REPLY" are not the same.
183 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
184 const char *is_ifs = strchr(ifs, c);
185 if (startword && is_ifs) {
188 /* it is a non-space ifs char */
190 if (startword == 1) /* first one? */
191 continue; /* yes, it is not next word yet */
194 if (argv[1] != NULL && is_ifs) {
195 buffer[bufpos] = '\0';
197 setvar(*argv, buffer);
199 /* can we skip one non-space ifs char? (2: yes) */
200 startword = isspace(c) ? 2 : 1;
209 /* Remove trailing space $IFS chars */
210 while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
212 buffer[bufpos + 1] = '\0';
213 /* Use the remainder as a value for the next variable */
214 setvar(*argv, buffer);
215 /* Set the rest to "" */
219 /* Note: no $IFS removal */
220 buffer[bufpos] = '\0';
221 setvar("REPLY", buffer);
226 if (read_flags & BUILTIN_READ_SILENT)
227 tcsetattr(fd, TCSANOW, &old_tty);