Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / gnulib-lib / pipe-filter-aux.h
1 /* Auxiliary code for filtering of data through a subprocess.
2    Copyright (C) 2001-2003, 2008-2015 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2009.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _GL_INLINE_HEADER_BEGIN
19  #error "Please include config.h first."
20 #endif
21 _GL_INLINE_HEADER_BEGIN
22 #ifndef PIPE_FILTER_AUX_INLINE
23 # define PIPE_FILTER_AUX_INLINE _GL_INLINE
24 #endif
25
26 #ifndef SSIZE_MAX
27 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
28 #endif
29
30 /* We use a child process, and communicate through a bidirectional pipe.
31    To avoid deadlocks, let the child process decide when it wants to read
32    or to write, and let the parent behave accordingly.  The parent uses
33    select() to know whether it must write or read.  On platforms without
34    select(), we use non-blocking I/O.  (This means the parent is busy
35    looping while waiting for the child.  Not good.  But hardly any platform
36    lacks select() nowadays.)  */
37
38 /* On BeOS select() works only on sockets, not on normal file descriptors.  */
39 #ifdef __BEOS__
40 # undef HAVE_SELECT
41 #endif
42
43 #ifdef EINTR
44
45 /* EINTR handling for close(), read(), write(), select().
46    These functions can return -1/EINTR even though we don't have any
47    signal handlers set up, namely when we get interrupted via SIGSTOP.  */
48
49 PIPE_FILTER_AUX_INLINE int
50 nonintr_close (int fd)
51 {
52   int retval;
53
54   do
55     retval = close (fd);
56   while (retval < 0 && errno == EINTR);
57
58   return retval;
59 }
60 #undef close /* avoid warning related to gnulib module unistd */
61 #define close nonintr_close
62
63 PIPE_FILTER_AUX_INLINE ssize_t
64 nonintr_read (int fd, void *buf, size_t count)
65 {
66   ssize_t retval;
67
68   do
69     retval = read (fd, buf, count);
70   while (retval < 0 && errno == EINTR);
71
72   return retval;
73 }
74 #define read nonintr_read
75
76 PIPE_FILTER_AUX_INLINE ssize_t
77 nonintr_write (int fd, const void *buf, size_t count)
78 {
79   ssize_t retval;
80
81   do
82     retval = write (fd, buf, count);
83   while (retval < 0 && errno == EINTR);
84
85   return retval;
86 }
87 #undef write /* avoid warning on VMS */
88 #define write nonintr_write
89
90 #endif
91
92 /* Non-blocking I/O.  */
93 #if HAVE_SELECT
94 # define IS_EAGAIN(errcode) 0
95 #else
96 # ifdef EWOULDBLOCK
97 #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN || (errcode) == EWOULDBLOCK)
98 # else
99 #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN)
100 # endif
101 #endif
102
103 _GL_INLINE_HEADER_END