timespec_get: New module.
[gnulib.git] / lib / pipe-filter.h
blob614b9e0cba23d90b0d83bf34d6a6f47dd1ff2ada
1 /* Filtering of data through a subprocess. -*- coding: utf-8 -*-
2 Copyright (C) 2009-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2009,
4 and Paolo Bonzini <bonzini@gnu.org>, 2009.
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #ifndef _PIPE_FILTER_H
20 #define _PIPE_FILTER_H
22 #include <stdbool.h>
23 #include <stddef.h>
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
31 /* Piping data through a subprocess in the naïve way - write data to the
32 subprocess and read from the subprocess when you expect it to have
33 produced results - is subject to two kinds of deadlocks:
34 1) If you write more than PIPE_MAX bytes or, more generally, if you write
35 more bytes than the subprocess can handle at once, the subprocess
36 may write its data and wait on you to read it, but you are currently
37 busy writing.
38 2) When you don't know ahead of time how many bytes the subprocess
39 will produce, the usual technique of calling read (fd, buf, BUFSIZ)
40 with a fixed BUFSIZ will, on Linux 2.2.17 and on BSD systems, cause
41 the read() call to block until *all* of the buffer has been filled.
42 But the subprocess cannot produce more data until you gave it more
43 input. But you are currently busy reading from it.
45 This header file declares four set of functions that pipes data through
46 the subprocess, without risking these deadlocks.
48 The side that writes data to the subprocess can be seen as a "generator",
49 that is, as a subroutine that produces and writes a piece of data here and
50 there, see <https://en.wikipedia.org/wiki/Generator_(computer_science)>.
51 But often, it can be written in the form of an "iterator", that is, as a
52 function that, each time it is invoked, produces and writes one more piece
53 of data.
55 Similarly, the side that reads data from the subprocess can be seen as
56 a "generator", that is, as a subroutine that consumes a piece of data here
57 and there. Often, it can be written in the form of an "iterator", that
58 is, as a function that, each time it is invoked, consumes one more piece
59 of data.
61 This header file declares four set of functions:
63 | writer | reader |
64 ----------------+------------+------------+
65 pipe_filter_ii | iterator | iterator |
66 pipe_filter_ig | iterator | generator |
67 pipe_filter_gi | generator | iterator |
68 pipe_filter_gg | generator | generator |
69 ----------------+------------+------------+
71 The last one uses threads in order to implement two generators running at
72 the same time. (For the relation between generators, coroutines, and
73 threads, see <https://en.wikipedia.org/wiki/Generator_(computer_science)>
74 and <https://en.wikipedia.org/wiki/Coroutine>.) It is therefore only
75 portable to platforms with kernel-based POSIX threads. */
77 /* These two functions together describe the side that writes data to the
78 subprocess when it has the form of an iterator.
79 - prepare_write (&num_bytes, p) must either return a pointer to data that
80 is ready to be written and set num_bytes to the number of bytes ready to
81 be written, or return NULL when no more bytes are to be written.
82 - done_write (data_written, num_bytes_written) is called after
83 num_bytes_written bytes were written. It is guaranteed that
84 num_bytes_written > 0.
85 Here p is always the private_data argument passed to the main function. */
86 typedef const void * (*prepare_write_fn) (size_t *num_bytes_p,
87 void *private_data);
88 typedef void (*done_write_fn) (void *data_written, size_t num_bytes_written,
89 void *private_data);
91 /* These two functions together describe the side that reads data from the
92 subprocess when it has the form of an iterator.
93 - prepare_read (&num_bytes, p) must return a pointer to a buffer for data
94 that can be read and set num_bytes to the size of that buffer
95 (must be > 0).
96 - done_read (data_read, num_bytes_read, p) is called after num_bytes_read
97 bytes were read into the buffer.
98 Here p is always the private_data argument passed to the main function. */
99 typedef void * (*prepare_read_fn) (size_t *num_bytes_p,
100 void *private_data);
101 typedef void (*done_read_fn) (void *data_read, size_t num_bytes_read,
102 void *private_data);
105 /* ============================ pipe_filter_ii ============================ */
107 /* Create a subprocess and pipe some data through it.
108 Arguments:
109 - progname is the program name used in error messages.
110 - prog_path is the file name of the program to invoke.
111 - prog_argv is a NULL terminated argument list, starting with prog_path as
112 first element.
113 - If null_stderr is true, the subprocess' stderr will be redirected to
114 /dev/null, and the usual error message to stderr will be omitted.
115 This is suitable when the subprocess does not fulfill an important task.
116 - If exit_on_error is true, any error will cause the main process to exit
117 with an error status.
118 If the subprocess does not terminate correctly, exit if exit_on_error is
119 true, otherwise return 127.
120 Callback arguments are as described above.
122 Data is alternately written to the subprocess, through the functions
123 prepare_write and done_write, and read from the subprocess, through the
124 functions prepare_read and done_read.
126 Note that the prepare_write/done_write functions and the
127 prepare_read/done_read functions may be called in different threads than
128 the current thread (depending on the platform). But they will not be
129 called after the pipe_filter_ii_execute function has returned.
131 Return 0 upon success, or (only if exit_on_error is false):
132 - -1 with errno set upon failure,
133 - the positive exit code of the subprocess if that failed. */
134 extern int
135 pipe_filter_ii_execute (const char *progname,
136 const char *prog_path,
137 const char * const *prog_argv,
138 bool null_stderr, bool exit_on_error,
139 prepare_write_fn prepare_write,
140 done_write_fn done_write,
141 prepare_read_fn prepare_read,
142 done_read_fn done_read,
143 void *private_data);
146 /* ============================ pipe_filter_ig ============================ */
148 struct pipe_filter_ig;
151 /* ============================ pipe_filter_gi ============================ */
153 struct pipe_filter_gi;
155 /* Create a subprocess and pipe some data through it.
156 Arguments:
157 - progname is the program name used in error messages.
158 - prog_path is the file name of the program to invoke.
159 - prog_argv is a NULL terminated argument list, starting with
160 prog_path as first element.
161 - If null_stderr is true, the subprocess' stderr will be redirected
162 to /dev/null, and the usual error message to stderr will be
163 omitted. This is suitable when the subprocess does not fulfill an
164 important task.
165 - If exit_on_error is true, any error will cause the main process to
166 exit with an error status.
167 If the subprocess does not start correctly, exit if exit_on_error is
168 true, otherwise return NULL and set errno.
170 The caller will write to the subprocess through pipe_filter_gi_write
171 and finally call pipe_filter_gi_close. During such calls, the
172 prepare_read and done_read function may be called to process any data
173 that the subprocess has written.
175 Note that the prepare_read/done_read functions may be called in a
176 different thread than the current thread (depending on the platform).
177 But they will not be called after the pipe_filter_gi_close function has
178 returned.
180 Return the freshly created 'struct pipe_filter_gi'. */
181 extern struct pipe_filter_gi *
182 pipe_filter_gi_create (const char *progname,
183 const char *prog_path,
184 const char * const *prog_argv,
185 bool null_stderr, bool exit_on_error,
186 prepare_read_fn prepare_read,
187 done_read_fn done_read,
188 void *private_data);
190 /* Write size bytes starting at buf into the pipe and in the meanwhile
191 possibly call the prepare_read and done_read functions specified to
192 pipe_filter_gi_create.
194 Note that the prepare_read/done_read functions may be called in a
195 different thread than the current thread (depending on the platform).
196 However, they will always be called before pipe_filter_gi_write has
197 returned, or otherwise not sooner than the next call to
198 pipe_filter_gi_write or pipe_filter_gi_close.
200 Return only after all the entire buffer has been written to the pipe or
201 the subprocess has exited.
203 Return 0 upon success, or (only if exit_on_error is false):
204 - -1 with errno set upon failure,
205 - the positive exit code of the subprocess if that failed. */
206 extern int
207 pipe_filter_gi_write (struct pipe_filter_gi *filter,
208 const void *buf, size_t size);
210 /* Finish reading the output via the prepare_read/done_read functions
211 specified to pipe_filter_gi_create.
213 Note that the prepare_read/done_read functions may be called in a
214 different thread than the current thread (depending on the platform).
215 However, they will always be called before pipe_filter_gi_close has
216 returned.
218 The write side of the pipe is closed as soon as pipe_filter_gi_close
219 starts, while the read side will be closed just before it finishes.
221 Return 0 upon success, or (only if exit_on_error is false):
222 - -1 with errno set upon failure,
223 - the positive exit code of the subprocess if that failed. */
224 extern int
225 pipe_filter_gi_close (struct pipe_filter_gi *filter);
228 /* ============================ pipe_filter_gg ============================ */
231 /* ======================================================================== */
234 #ifdef __cplusplus
236 #endif
239 #endif /* _PIPE_FILTER_H */