input.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /* input.c -- functions to perform buffered input with synchronization. */
  2. /* Copyright (C) 1992-2009 Free Software Foundation, Inc.
  3. This file is part of GNU Bash, the Bourne Again SHell.
  4. Bash is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Bash is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bash. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "config.h"
  16. #include "bashtypes.h"
  17. #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
  18. # include <sys/file.h>
  19. #endif
  20. #include "filecntl.h"
  21. #include "posixstat.h"
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #if defined (HAVE_UNISTD_H)
  25. # include <unistd.h>
  26. #endif
  27. #include "bashansi.h"
  28. #include "bashintl.h"
  29. #include "command.h"
  30. #include "general.h"
  31. #include "input.h"
  32. #include "error.h"
  33. #include "externs.h"
  34. #include "quit.h"
  35. #include "trap.h"
  36. #if !defined (errno)
  37. extern int errno;
  38. #endif /* !errno */
  39. #if defined (EAGAIN)
  40. # define X_EAGAIN EAGAIN
  41. #else
  42. # define X_EAGAIN -99
  43. #endif
  44. #if defined (EWOULDBLOCK)
  45. # define X_EWOULDBLOCK EWOULDBLOCK
  46. #else
  47. # define X_EWOULDBLOCK -99
  48. #endif
  49. extern void termsig_handler __P((int));
  50. /* Functions to handle reading input on systems that don't restart read(2)
  51. if a signal is received. */
  52. static char localbuf[128];
  53. static int local_index = 0, local_bufused = 0;
  54. /* Posix and USG systems do not guarantee to restart read () if it is
  55. interrupted by a signal. We do the read ourselves, and restart it
  56. if it returns EINTR. */
  57. int
  58. getc_with_restart (stream)
  59. FILE *stream;
  60. {
  61. unsigned char uc;
  62. CHECK_TERMSIG;
  63. /* Try local buffering to reduce the number of read(2) calls. */
  64. if (local_index == local_bufused || local_bufused == 0)
  65. {
  66. while (1)
  67. {
  68. QUIT;
  69. run_pending_traps ();
  70. local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
  71. if (local_bufused > 0)
  72. break;
  73. else if (local_bufused == 0)
  74. {
  75. local_index = 0;
  76. return EOF;
  77. }
  78. else if (errno == X_EAGAIN || errno == X_EWOULDBLOCK)
  79. {
  80. if (sh_unset_nodelay_mode (fileno (stream)) < 0)
  81. {
  82. sys_error (_("cannot reset nodelay mode for fd %d"), fileno (stream));
  83. local_index = local_bufused = 0;
  84. return EOF;
  85. }
  86. continue;
  87. }
  88. else if (errno != EINTR)
  89. {
  90. local_index = local_bufused = 0;
  91. return EOF;
  92. }
  93. else if (interrupt_state || terminating_signal) /* QUIT; */
  94. local_index = local_bufused = 0;
  95. }
  96. local_index = 0;
  97. }
  98. uc = localbuf[local_index++];
  99. return uc;
  100. }
  101. int
  102. ungetc_with_restart (c, stream)
  103. int c;
  104. FILE *stream;
  105. {
  106. if (local_index == 0 || c == EOF)
  107. return EOF;
  108. localbuf[--local_index] = c;
  109. return c;
  110. }
  111. #if defined (BUFFERED_INPUT)
  112. /* A facility similar to stdio, but input-only. */
  113. #if defined (USING_BASH_MALLOC)
  114. # define MAX_INPUT_BUFFER_SIZE 8176
  115. #else
  116. # define MAX_INPUT_BUFFER_SIZE 8192
  117. #endif
  118. #if !defined (SEEK_CUR)
  119. # define SEEK_CUR 1
  120. #endif /* !SEEK_CUR */
  121. #ifdef max
  122. # undef max
  123. #endif
  124. #define max(a, b) (((a) > (b)) ? (a) : (b))
  125. #ifdef min
  126. # undef min
  127. #endif
  128. #define min(a, b) ((a) > (b) ? (b) : (a))
  129. extern int interactive_shell;
  130. int bash_input_fd_changed;
  131. /* This provides a way to map from a file descriptor to the buffer
  132. associated with that file descriptor, rather than just the other
  133. way around. This is needed so that buffers are managed properly
  134. in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
  135. correspondence is maintained. */
  136. static BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
  137. static int nbuffers;
  138. #define ALLOCATE_BUFFERS(n) \
  139. do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
  140. /* Make sure `buffers' has at least N elements. */
  141. static void
  142. allocate_buffers (n)
  143. int n;
  144. {
  145. register int i, orig_nbuffers;
  146. orig_nbuffers = nbuffers;
  147. nbuffers = n + 20;
  148. buffers = (BUFFERED_STREAM **)xrealloc
  149. (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
  150. /* Zero out the new buffers. */
  151. for (i = orig_nbuffers; i < nbuffers; i++)
  152. buffers[i] = (BUFFERED_STREAM *)NULL;
  153. }
  154. /* Construct and return a BUFFERED_STREAM corresponding to file descriptor
  155. FD, using BUFFER. */
  156. static BUFFERED_STREAM *
  157. make_buffered_stream (fd, buffer, bufsize)
  158. int fd;
  159. char *buffer;
  160. size_t bufsize;
  161. {
  162. BUFFERED_STREAM *bp;
  163. bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
  164. ALLOCATE_BUFFERS (fd);
  165. buffers[fd] = bp;
  166. bp->b_fd = fd;
  167. bp->b_buffer = buffer;
  168. bp->b_size = bufsize;
  169. bp->b_used = bp->b_inputp = bp->b_flag = 0;
  170. if (bufsize == 1)
  171. bp->b_flag |= B_UNBUFF;
  172. if (O_TEXT && (fcntl (fd, F_GETFL) & O_TEXT) != 0)
  173. bp->b_flag |= B_TEXT;
  174. return (bp);
  175. }
  176. /* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
  177. static BUFFERED_STREAM *
  178. copy_buffered_stream (bp)
  179. BUFFERED_STREAM *bp;
  180. {
  181. BUFFERED_STREAM *nbp;
  182. if (!bp)
  183. return ((BUFFERED_STREAM *)NULL);
  184. nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
  185. xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
  186. return (nbp);
  187. }
  188. int
  189. set_bash_input_fd (fd)
  190. int fd;
  191. {
  192. if (bash_input.type == st_bstream)
  193. bash_input.location.buffered_fd = fd;
  194. else if (interactive_shell == 0)
  195. default_buffered_input = fd;
  196. return 0;
  197. }
  198. int
  199. fd_is_bash_input (fd)
  200. int fd;
  201. {
  202. if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
  203. return 1;
  204. else if (interactive_shell == 0 && default_buffered_input == fd)
  205. return 1;
  206. return 0;
  207. }
  208. /* Save the buffered stream corresponding to file descriptor FD (which bash
  209. is using to read input) to a buffered stream associated with NEW_FD. If
  210. NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
  211. file descriptor is returned on success, -1 on error. */
  212. int
  213. save_bash_input (fd, new_fd)
  214. int fd, new_fd;
  215. {
  216. int nfd;
  217. /* Sync the stream so we can re-read from the new file descriptor. We
  218. might be able to avoid this by copying the buffered stream verbatim
  219. to the new file descriptor. */
  220. if (buffers[fd])
  221. sync_buffered_stream (fd);
  222. /* Now take care of duplicating the file descriptor that bash is
  223. using for input, so we can reinitialize it later. */
  224. nfd = (new_fd == -1) ? fcntl (fd, F_DUPFD, 10) : new_fd;
  225. if (nfd == -1)
  226. {
  227. if (fcntl (fd, F_GETFD, 0) == 0)
  228. sys_error (_("cannot allocate new file descriptor for bash input from fd %d"), fd);
  229. return -1;
  230. }
  231. if (nfd < nbuffers && buffers[nfd])
  232. {
  233. /* What's this? A stray buffer without an associated open file
  234. descriptor? Free up the buffer and report the error. */
  235. internal_error (_("save_bash_input: buffer already exists for new fd %d"), nfd);
  236. free_buffered_stream (buffers[nfd]);
  237. }
  238. /* Reinitialize bash_input.location. */
  239. if (bash_input.type == st_bstream)
  240. {
  241. bash_input.location.buffered_fd = nfd;
  242. fd_to_buffered_stream (nfd);
  243. close_buffered_fd (fd); /* XXX */
  244. }
  245. else
  246. /* If the current input type is not a buffered stream, but the shell
  247. is not interactive and therefore using a buffered stream to read
  248. input (e.g. with an `eval exec 3>output' inside a script), note
  249. that the input fd has been changed. pop_stream() looks at this
  250. value and adjusts the input fd to the new value of
  251. default_buffered_input accordingly. */
  252. bash_input_fd_changed++;
  253. if (default_buffered_input == fd)
  254. default_buffered_input = nfd;
  255. SET_CLOSE_ON_EXEC (nfd);
  256. return nfd;
  257. }
  258. /* Check that file descriptor FD is not the one that bash is currently
  259. using to read input from a script. FD is about to be duplicated onto,
  260. which means that the kernel will close it for us. If FD is the bash
  261. input file descriptor, we need to seek backwards in the script (if
  262. possible and necessary -- scripts read from stdin are still unbuffered),
  263. allocate a new file descriptor to use for bash input, and re-initialize
  264. the buffered stream. Make sure the file descriptor used to save bash
  265. input is set close-on-exec. Returns 0 on success, -1 on failure. This
  266. works only if fd is > 0 -- if fd == 0 and bash is reading input from
  267. fd 0, sync_buffered_stream is used instead, to cooperate with input
  268. redirection (look at redir.c:add_undo_redirect()). */
  269. int
  270. check_bash_input (fd)
  271. int fd;
  272. {
  273. if (fd_is_bash_input (fd))
  274. {
  275. if (fd > 0)
  276. return ((save_bash_input (fd, -1) == -1) ? -1 : 0);
  277. else if (fd == 0)
  278. return ((sync_buffered_stream (fd) == -1) ? -1 : 0);
  279. }
  280. return 0;
  281. }
  282. /* This is the buffered stream analogue of dup2(fd1, fd2). The
  283. BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
  284. BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
  285. redirect code for constructs like 4<&0 and 3</etc/rc.local. */
  286. int
  287. duplicate_buffered_stream (fd1, fd2)
  288. int fd1, fd2;
  289. {
  290. int is_bash_input, m;
  291. if (fd1 == fd2)
  292. return 0;
  293. m = max (fd1, fd2);
  294. ALLOCATE_BUFFERS (m);
  295. /* If FD2 is the file descriptor bash is currently using for shell input,
  296. we need to do some extra work to make sure that the buffered stream
  297. actually exists (it might not if fd1 was not active, and the copy
  298. didn't actually do anything). */
  299. is_bash_input = (bash_input.type == st_bstream) &&
  300. (bash_input.location.buffered_fd == fd2);
  301. if (buffers[fd2])
  302. {
  303. /* If the two objects share the same b_buffer, don't free it. */
  304. if (buffers[fd1] && buffers[fd1]->b_buffer && buffers[fd1]->b_buffer == buffers[fd2]->b_buffer)
  305. buffers[fd2] = (BUFFERED_STREAM *)NULL;
  306. else
  307. free_buffered_stream (buffers[fd2]);
  308. }
  309. buffers[fd2] = copy_buffered_stream (buffers[fd1]);
  310. if (buffers[fd2])
  311. buffers[fd2]->b_fd = fd2;
  312. if (is_bash_input)
  313. {
  314. if (!buffers[fd2])
  315. fd_to_buffered_stream (fd2);
  316. buffers[fd2]->b_flag |= B_WASBASHINPUT;
  317. }
  318. return (fd2);
  319. }
  320. /* Return 1 if a seek on FD will succeed. */
  321. #define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
  322. /* Take FD, a file descriptor, and create and return a buffered stream
  323. corresponding to it. If something is wrong and the file descriptor
  324. is invalid, return a NULL stream. */
  325. BUFFERED_STREAM *
  326. fd_to_buffered_stream (fd)
  327. int fd;
  328. {
  329. char *buffer;
  330. size_t size;
  331. struct stat sb;
  332. if (fstat (fd, &sb) < 0)
  333. {
  334. close (fd);
  335. return ((BUFFERED_STREAM *)NULL);
  336. }
  337. size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;
  338. if (size == 0)
  339. size = 1;
  340. buffer = (char *)xmalloc (size);
  341. return (make_buffered_stream (fd, buffer, size));
  342. }
  343. /* Return a buffered stream corresponding to FILE, a file name. */
  344. BUFFERED_STREAM *
  345. open_buffered_stream (file)
  346. char *file;
  347. {
  348. int fd;
  349. fd = open (file, O_RDONLY);
  350. return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
  351. }
  352. /* Deallocate a buffered stream and free up its resources. Make sure we
  353. zero out the slot in BUFFERS that points to BP. */
  354. void
  355. free_buffered_stream (bp)
  356. BUFFERED_STREAM *bp;
  357. {
  358. int n;
  359. if (!bp)
  360. return;
  361. n = bp->b_fd;
  362. if (bp->b_buffer)
  363. free (bp->b_buffer);
  364. free (bp);
  365. buffers[n] = (BUFFERED_STREAM *)NULL;
  366. }
  367. /* Close the file descriptor associated with BP, a buffered stream, and free
  368. up the stream. Return the status of closing BP's file descriptor. */
  369. int
  370. close_buffered_stream (bp)
  371. BUFFERED_STREAM *bp;
  372. {
  373. int fd;
  374. if (!bp)
  375. return (0);
  376. fd = bp->b_fd;
  377. free_buffered_stream (bp);
  378. return (close (fd));
  379. }
  380. /* Deallocate the buffered stream associated with file descriptor FD, and
  381. close FD. Return the status of the close on FD. */
  382. int
  383. close_buffered_fd (fd)
  384. int fd;
  385. {
  386. if (fd < 0)
  387. {
  388. errno = EBADF;
  389. return -1;
  390. }
  391. if (fd >= nbuffers || !buffers || !buffers[fd])
  392. return (close (fd));
  393. return (close_buffered_stream (buffers[fd]));
  394. }
  395. /* Make the BUFFERED_STREAM associated with buffers[FD] be BP, and return
  396. the old BUFFERED_STREAM. */
  397. BUFFERED_STREAM *
  398. set_buffered_stream (fd, bp)
  399. int fd;
  400. BUFFERED_STREAM *bp;
  401. {
  402. BUFFERED_STREAM *ret;
  403. ret = buffers[fd];
  404. buffers[fd] = bp;
  405. return ret;
  406. }
  407. /* Read a buffer full of characters from BP, a buffered stream. */
  408. static int
  409. b_fill_buffer (bp)
  410. BUFFERED_STREAM *bp;
  411. {
  412. ssize_t nr;
  413. off_t o;
  414. CHECK_TERMSIG;
  415. /* In an environment where text and binary files are treated differently,
  416. compensate for lseek() on text files returning an offset different from
  417. the count of characters read() returns. Text-mode streams have to be
  418. treated as unbuffered. */
  419. if ((bp->b_flag & (B_TEXT | B_UNBUFF)) == B_TEXT)
  420. {
  421. o = lseek (bp->b_fd, 0, SEEK_CUR);
  422. nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
  423. if (nr > 0 && nr < lseek (bp->b_fd, 0, SEEK_CUR) - o)
  424. {
  425. lseek (bp->b_fd, o, SEEK_SET);
  426. bp->b_flag |= B_UNBUFF;
  427. bp->b_size = 1;
  428. nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
  429. }
  430. }
  431. else
  432. nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
  433. if (nr <= 0)
  434. {
  435. bp->b_used = 0;
  436. bp->b_buffer[0] = 0;
  437. if (nr == 0)
  438. bp->b_flag |= B_EOF;
  439. else
  440. bp->b_flag |= B_ERROR;
  441. return (EOF);
  442. }
  443. bp->b_used = nr;
  444. bp->b_inputp = 0;
  445. return (bp->b_buffer[bp->b_inputp++] & 0xFF);
  446. }
  447. /* Get a character from buffered stream BP. */
  448. #define bufstream_getc(bp) \
  449. (bp->b_inputp == bp->b_used || !bp->b_used) \
  450. ? b_fill_buffer (bp) \
  451. : bp->b_buffer[bp->b_inputp++] & 0xFF
  452. /* Push C back onto buffered stream BP. */
  453. static int
  454. bufstream_ungetc(c, bp)
  455. int c;
  456. BUFFERED_STREAM *bp;
  457. {
  458. if (c == EOF || bp->b_inputp == 0)
  459. return (EOF);
  460. bp->b_buffer[--bp->b_inputp] = c;
  461. return (c);
  462. }
  463. /* Seek backwards on file BFD to synchronize what we've read so far
  464. with the underlying file pointer. */
  465. int
  466. sync_buffered_stream (bfd)
  467. int bfd;
  468. {
  469. BUFFERED_STREAM *bp;
  470. off_t chars_left;
  471. if (buffers == 0 || (bp = buffers[bfd]) == 0)
  472. return (-1);
  473. chars_left = bp->b_used - bp->b_inputp;
  474. if (chars_left)
  475. lseek (bp->b_fd, -chars_left, SEEK_CUR);
  476. bp->b_used = bp->b_inputp = 0;
  477. return (0);
  478. }
  479. int
  480. buffered_getchar ()
  481. {
  482. CHECK_TERMSIG;
  483. #if !defined (DJGPP)
  484. return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
  485. #else
  486. /* On DJGPP, ignore \r. */
  487. int ch;
  488. while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
  489. ;
  490. return ch;
  491. #endif
  492. }
  493. int
  494. buffered_ungetchar (c)
  495. int c;
  496. {
  497. return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
  498. }
  499. /* Make input come from file descriptor BFD through a buffered stream. */
  500. void
  501. with_input_from_buffered_stream (bfd, name)
  502. int bfd;
  503. char *name;
  504. {
  505. INPUT_STREAM location;
  506. BUFFERED_STREAM *bp;
  507. location.buffered_fd = bfd;
  508. /* Make sure the buffered stream exists. */
  509. bp = fd_to_buffered_stream (bfd);
  510. init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
  511. buffered_ungetchar, st_bstream, name, location);
  512. }
  513. #if defined (TEST)
  514. void *
  515. xmalloc(s)
  516. int s;
  517. {
  518. return (malloc (s));
  519. }
  520. void *
  521. xrealloc(s, size)
  522. char *s;
  523. int size;
  524. {
  525. if (!s)
  526. return(malloc (size));
  527. else
  528. return(realloc (s, size));
  529. }
  530. void
  531. init_yy_io ()
  532. {
  533. }
  534. process(bp)
  535. BUFFERED_STREAM *bp;
  536. {
  537. int c;
  538. while ((c = bufstream_getc(bp)) != EOF)
  539. putchar(c);
  540. }
  541. BASH_INPUT bash_input;
  542. struct stat dsb; /* can be used from gdb */
  543. /* imitate /bin/cat */
  544. main(argc, argv)
  545. int argc;
  546. char **argv;
  547. {
  548. register int i;
  549. BUFFERED_STREAM *bp;
  550. if (argc == 1) {
  551. bp = fd_to_buffered_stream (0);
  552. process(bp);
  553. exit(0);
  554. }
  555. for (i = 1; i < argc; i++) {
  556. if (argv[i][0] == '-' && argv[i][1] == '\0') {
  557. bp = fd_to_buffered_stream (0);
  558. if (!bp)
  559. continue;
  560. process(bp);
  561. free_buffered_stream (bp);
  562. } else {
  563. bp = open_buffered_stream (argv[i]);
  564. if (!bp)
  565. continue;
  566. process(bp);
  567. close_buffered_stream (bp);
  568. }
  569. }
  570. exit(0);
  571. }
  572. #endif /* TEST */
  573. #endif /* BUFFERED_INPUT */