builtin_read.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Adapted from ash applet code
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Kenneth Almquist.
  7. *
  8. * Copyright (c) 1989, 1991, 1993, 1994
  9. * The Regents of the University of California. All rights reserved.
  10. *
  11. * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
  12. * was re-ported from NetBSD and debianized.
  13. *
  14. * Copyright (c) 2010 Denys Vlasenko
  15. * Split from ash.c
  16. *
  17. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  18. */
  19. #include "libbb.h"
  20. #include "shell_common.h"
  21. #include "builtin_read.h"
  22. //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
  23. //string. hush naturally has it, and ash has setvareq().
  24. //Here we can simply store "VAR=" at buffer start and store read data directly
  25. //after "=", then pass buffer to setvar() to consume.
  26. const char* FAST_FUNC
  27. shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
  28. char **argv,
  29. const char *ifs,
  30. int read_flags,
  31. const char *opt_n,
  32. const char *opt_p,
  33. const char *opt_t,
  34. const char *opt_u
  35. )
  36. {
  37. unsigned end_ms; /* -t TIMEOUT */
  38. int fd; /* -u FD */
  39. int nchars; /* -n NUM */
  40. char **pp;
  41. char *buffer;
  42. struct termios tty, old_tty;
  43. const char *retval;
  44. int bufpos; /* need to be able to hold -1 */
  45. int startword;
  46. smallint backslash;
  47. pp = argv;
  48. while (*pp) {
  49. if (!is_well_formed_var_name(*pp, '\0')) {
  50. /* Mimic bash message */
  51. bb_error_msg("read: '%s': not a valid identifier", *pp);
  52. return (const char *)(uintptr_t)1;
  53. }
  54. pp++;
  55. }
  56. nchars = 0; /* if != 0, -n is in effect */
  57. if (opt_n) {
  58. nchars = bb_strtou(opt_n, NULL, 10);
  59. if (nchars < 0 || errno)
  60. return "invalid count";
  61. /* note: "-n 0": off (bash 3.2 does this too) */
  62. }
  63. end_ms = 0;
  64. if (opt_t) {
  65. end_ms = bb_strtou(opt_t, NULL, 10);
  66. if (errno || end_ms > UINT_MAX / 2048)
  67. return "invalid timeout";
  68. end_ms *= 1000;
  69. #if 0 /* even bash has no -t N.NNN support */
  70. ts.tv_sec = bb_strtou(opt_t, &p, 10);
  71. ts.tv_usec = 0;
  72. /* EINVAL means number is ok, but not terminated by NUL */
  73. if (*p == '.' && errno == EINVAL) {
  74. char *p2;
  75. if (*++p) {
  76. int scale;
  77. ts.tv_usec = bb_strtou(p, &p2, 10);
  78. if (errno)
  79. return "invalid timeout";
  80. scale = p2 - p;
  81. /* normalize to usec */
  82. if (scale > 6)
  83. return "invalid timeout";
  84. while (scale++ < 6)
  85. ts.tv_usec *= 10;
  86. }
  87. } else if (ts.tv_sec < 0 || errno) {
  88. return "invalid timeout";
  89. }
  90. if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
  91. return "invalid timeout";
  92. }
  93. #endif /* if 0 */
  94. }
  95. fd = STDIN_FILENO;
  96. if (opt_u) {
  97. fd = bb_strtou(opt_u, NULL, 10);
  98. if (fd < 0 || errno)
  99. return "invalid file descriptor";
  100. }
  101. if (opt_p && isatty(fd)) {
  102. fputs(opt_p, stderr);
  103. fflush_all();
  104. }
  105. if (ifs == NULL)
  106. ifs = defifs;
  107. if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
  108. tcgetattr(fd, &tty);
  109. old_tty = tty;
  110. if (nchars) {
  111. tty.c_lflag &= ~ICANON;
  112. tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
  113. }
  114. if (read_flags & BUILTIN_READ_SILENT) {
  115. tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
  116. }
  117. /* This forces execution of "restoring" tcgetattr later */
  118. read_flags |= BUILTIN_READ_SILENT;
  119. /* if tcgetattr failed, tcsetattr will fail too.
  120. * Ignoring, it's harmless. */
  121. tcsetattr(fd, TCSANOW, &tty);
  122. }
  123. retval = (const char *)(uintptr_t)0;
  124. startword = 1;
  125. backslash = 0;
  126. if (end_ms) /* NB: end_ms stays nonzero: */
  127. end_ms = ((unsigned)monotonic_ms() + end_ms) | 1;
  128. buffer = NULL;
  129. bufpos = 0;
  130. do {
  131. char c;
  132. if (end_ms) {
  133. int timeout;
  134. struct pollfd pfd[1];
  135. pfd[0].fd = fd;
  136. pfd[0].events = POLLIN;
  137. timeout = end_ms - (unsigned)monotonic_ms();
  138. if (timeout <= 0 /* already late? */
  139. || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
  140. ) { /* timed out! */
  141. retval = (const char *)(uintptr_t)1;
  142. goto ret;
  143. }
  144. }
  145. if ((bufpos & 0xff) == 0)
  146. buffer = xrealloc(buffer, bufpos + 0x100);
  147. if (nonblock_safe_read(fd, &buffer[bufpos], 1) != 1) {
  148. retval = (const char *)(uintptr_t)1;
  149. break;
  150. }
  151. c = buffer[bufpos];
  152. if (c == '\0')
  153. continue;
  154. if (backslash) {
  155. backslash = 0;
  156. if (c != '\n')
  157. goto put;
  158. continue;
  159. }
  160. if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
  161. backslash = 1;
  162. continue;
  163. }
  164. if (c == '\n')
  165. break;
  166. /* $IFS splitting. NOT done if we run "read"
  167. * without variable names (bash compat).
  168. * Thus, "read" and "read REPLY" are not the same.
  169. */
  170. if (argv[0]) {
  171. /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
  172. const char *is_ifs = strchr(ifs, c);
  173. if (startword && is_ifs) {
  174. if (isspace(c))
  175. continue;
  176. /* it is a non-space ifs char */
  177. startword--;
  178. if (startword == 1) /* first one? */
  179. continue; /* yes, it is not next word yet */
  180. }
  181. startword = 0;
  182. if (argv[1] != NULL && is_ifs) {
  183. buffer[bufpos] = '\0';
  184. bufpos = 0;
  185. setvar(*argv, buffer);
  186. argv++;
  187. /* can we skip one non-space ifs char? (2: yes) */
  188. startword = isspace(c) ? 2 : 1;
  189. continue;
  190. }
  191. }
  192. put:
  193. bufpos++;
  194. } while (--nchars);
  195. if (argv[0]) {
  196. /* Remove trailing space $IFS chars */
  197. while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
  198. continue;
  199. buffer[bufpos + 1] = '\0';
  200. /* Use the remainder as a value for the next variable */
  201. setvar(*argv, buffer);
  202. /* Set the rest to "" */
  203. while (*++argv)
  204. setvar(*argv, "");
  205. } else {
  206. /* Note: no $IFS removal */
  207. buffer[bufpos] = '\0';
  208. setvar("REPLY", buffer);
  209. }
  210. ret:
  211. free(buffer);
  212. if (read_flags & BUILTIN_READ_SILENT)
  213. tcsetattr(fd, TCSANOW, &old_tty);
  214. return retval;
  215. }