a7a036607a7683fb1174cb8b82775491ba43d33f
[platform/upstream/libpipeline.git] / lib / pipeline.h
1 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002
2  * Free Software Foundation, Inc.
3  * Copyright (C) 2003, 2004, 2005, 2007, 2008 Colin Watson.
4  *   Written for groff by James Clark (jjc@jclark.com)
5  *   Adapted for man-db by Colin Watson.
6  *
7  * This file is part of libpipeline.
8  *
9  * libpipeline is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or (at
12  * your option) any later version.
13  *
14  * libpipeline is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with libpipeline; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA.
23  */
24
25 #ifndef PIPELINE_H
26 #define PIPELINE_H
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <sys/types.h>
35
36 /* GCC version checking borrowed from glibc. */
37 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
38 #  define PIPELINE_GNUC_PREREQ(maj,min) \
39         ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
40 #else
41 #  define PIPELINE_GNUC_PREREQ(maj,min) 0
42 #endif
43
44 /* Does this compiler support format string checking? */
45 #if PIPELINE_GNUC_PREREQ(2,0)
46 #  define PIPELINE_ATTR_FORMAT_PRINTF(a,b) \
47         __attribute__ ((__format__ (__printf__, a, b)))
48 #else
49 #  define PIPELINE_ATTR_FORMAT_PRINTF(a,b)
50 #endif
51
52 /* Does this compiler support marking variables as unused? */
53 #if PIPELINE_GNUC_PREREQ(2,4)
54 #  define PIPELINE_ATTR_UNUSED __attribute__ ((__unused__))
55 #else
56 #  define PIPELINE_ATTR_UNUSED
57 #endif
58
59 /* Does this compiler support marking functions as non-returning? */
60 #if PIPELINE_GNUC_PREREQ(2,5)
61 #  define PIPELINE_ATTR_NORETURN __attribute__ ((__noreturn__))
62 #else
63 #  define PIPELINE_ATTR_NORETURN
64 #endif
65
66 /* Does this compiler support unused result checking? */
67 #if PIPELINE_GNUC_PREREQ(3,4)
68 #  define PIPELINE_ATTR_WARN_UNUSED_RESULT \
69         __attribute__ ((__warn_unused_result__))
70 #else
71 #  define PIPELINE_ATTR_WARN_UNUSED_RESULT
72 #endif
73
74 /* Does this compiler support sentinel checking? */
75 #if PIPELINE_GNUC_PREREQ(4,0)
76 #  define PIPELINE_ATTR_SENTINEL __attribute__ ((__sentinel__))
77 #else
78 #  define PIPELINE_ATTR_SENTINEL
79 #endif
80
81 typedef void pipecmd_function_type (void *);
82 typedef void pipecmd_function_free_type (void *);
83
84 struct pipecmd;
85 typedef struct pipecmd pipecmd;
86
87 struct pipeline;
88 typedef struct pipeline pipeline;
89
90 /* ---------------------------------------------------------------------- */
91
92 /* Functions to build individual commands. */
93
94 /* Construct a new command. */
95 pipecmd *pipecmd_new (const char *name);
96
97 /* Convenience constructors wrapping pipecmd_new() and pipecmd_arg().
98  * Terminate arguments with NULL.
99  */
100 pipecmd *pipecmd_new_argv (const char *name, va_list argv);
101 pipecmd *pipecmd_new_args (const char *name, ...) PIPELINE_ATTR_SENTINEL;
102
103 /* Split argstr on whitespace to construct a command and arguments,
104  * honouring shell-style single-quoting, double-quoting, and backslashes,
105  * but not other shell evil like wildcards, semicolons, or backquotes. This
106  * is a backward-compatibility hack to support old configuration file
107  * directives; please try to avoid using it in new code.
108  */
109 pipecmd *pipecmd_new_argstr (const char *argstr);
110
111 /* Construct a new command that calls a given function rather than executing
112  * a process. The data argument is passed as the function's only argument,
113  * and will be freed before returning using free_func (if non-NULL).
114  *
115  * pipecmd_* functions that deal with arguments cannot be used with the
116  * command returned by this function.
117  */
118 pipecmd *pipecmd_new_function (const char *name,
119                                pipecmd_function_type *func,
120                                pipecmd_function_free_type *free_func,
121                                void *data);
122
123 /* Construct a new command that runs a sequence of commands. The commands
124  * will be executed in forked children; if any exits non-zero then it will
125  * terminate the sequence, as with "&&" in shell.
126  *
127  * pipecmd_* functions that deal with arguments cannot be used with the
128  * command returned by this function.
129  */
130 pipecmd *pipecmd_new_sequencev (const char *name, va_list cmdv);
131 pipecmd *pipecmd_new_sequence (const char *name, ...) PIPELINE_ATTR_SENTINEL;
132
133 /* Return a new command that just passes data from its input to its output. */
134 pipecmd *pipecmd_new_passthrough (void);
135
136 /* Return a duplicate of a command. */
137 pipecmd *pipecmd_dup (pipecmd *cmd);
138
139 /* Add an argument to a command. */
140 void pipecmd_arg (pipecmd *cmd, const char *arg);
141
142 /* Convenience function to add an argument with printf substitutions. */
143 void pipecmd_argf (pipecmd *cmd, const char *format, ...)
144         PIPELINE_ATTR_FORMAT_PRINTF(2, 3);
145
146 /* Convenience functions wrapping pipecmd_arg().
147  * Terminate arguments with NULL.
148  */
149 void pipecmd_argv (pipecmd *cmd, va_list argv);
150 void pipecmd_args (pipecmd *cmd, ...) PIPELINE_ATTR_SENTINEL;
151
152 /* Split argstr on whitespace to add a list of arguments, honouring
153  * shell-style single-quoting, double-quoting, and backslashes, but not
154  * other shell evil like wildcards, semicolons, or backquotes. This is a
155  * backward-compatibility hack to support old configuration file directives;
156  * please try to avoid using it in new code.
157  */
158 void pipecmd_argstr (pipecmd *cmd, const char *argstr);
159
160 /* Return the number of arguments to this command.  Note that this includes
161  * the command name as the first argument, so the command 'echo foo bar' is
162  * counted as having three arguments.
163  */
164 int pipecmd_get_nargs (pipecmd *cmd);
165
166 /* Set the nice(3) value for this command.  Defaults to 0.  Errors while
167  * attempting to set the nice value are ignored, aside from emitting a debug
168  * message.
169  */
170 void pipecmd_nice (pipecmd *cmd, int nice);
171
172 /* If discard_err is non-zero, redirect this command's standard error to
173  * /dev/null.  Otherwise, and by default, pass it through.
174  */
175 void pipecmd_discard_err (pipecmd *cmd, int discard_err);
176
177 /* Set an environment variable while running this command. */
178 void pipecmd_setenv (pipecmd *cmd, const char *name, const char *value);
179
180 /* Unset an environment variable while running this command. */
181 void pipecmd_unsetenv (pipecmd *cmd, const char *name);
182
183 /* Clear the environment while running this command.  (Note that environment
184  * operations work in sequence; pipecmd_clearenv followed by pipecmd_setenv
185  * causes the command to have just a single environment variable set.)
186  */
187 void pipecmd_clearenv (pipecmd *cmd);
188
189 /* Add a command to a sequence. */
190 void pipecmd_sequence_command (pipecmd *cmd, pipecmd *child);
191
192 /* Dump a string representation of a command to stream. */
193 void pipecmd_dump (pipecmd *cmd, FILE *stream);
194
195 /* Return a string representation of a command. The caller should free the
196  * result.
197  */
198 char *pipecmd_tostring (pipecmd *cmd);
199
200 /* Execute a single command, replacing the current process.  Never returns,
201  * instead exiting non-zero on failure.
202  */
203 void pipecmd_exec (pipecmd *cmd) PIPELINE_ATTR_NORETURN;
204
205 /* Destroy a command. Safely does nothing on NULL. */
206 void pipecmd_free (pipecmd *cmd);
207
208 /* ---------------------------------------------------------------------- */
209
210 /* Functions to build pipelines. */
211
212 /* Construct a new pipeline. */
213 pipeline *pipeline_new (void);
214
215 /* Convenience constructors wrapping pipeline_new() and pipeline_command().
216  * Terminate commands with NULL.
217  */
218 pipeline *pipeline_new_commandv (pipecmd *cmd1, va_list cmdv);
219 pipeline *pipeline_new_commands (pipecmd *cmd1, ...) PIPELINE_ATTR_SENTINEL;
220
221 /* Construct a new pipeline and add a single command to it. */
222 pipeline *pipeline_new_command_argv (const char *name, va_list argv);
223 pipeline *pipeline_new_command_args (const char *name, ...)
224         PIPELINE_ATTR_SENTINEL;
225
226 /* Joins two pipelines, neither of which are allowed to be started. Discards
227  * want_out, want_outfile, and outfd from p1, and want_in, want_infile, and
228  * infd from p2.
229  */
230 pipeline *pipeline_join (pipeline *p1, pipeline *p2);
231
232 /* Connect the input of one or more sink pipelines to the output of a source
233  * pipeline. The source pipeline may be started, but in that case want_out
234  * must be negative; otherwise, discards want_out from source. In any event,
235  * discards want_in from all sinks, none of which are allowed to be started.
236  * Terminate arguments with NULL.
237  *
238  * This is an application-level connection; data may be intercepted between
239  * the pipelines by the program before calling pipeline_pump(), which sets
240  * data flowing from the source to the sinks. It is primarily useful when
241  * more than one sink pipeline is involved, in which case the pipelines
242  * cannot simply be concatenated into one.
243  */
244 void pipeline_connect (pipeline *source, pipeline *sink, ...)
245         PIPELINE_ATTR_SENTINEL;
246
247 /* Add a command to a pipeline. */
248 void pipeline_command (pipeline *p, pipecmd *cmd);
249
250 /* Construct a new command and add it to a pipeline in one go. */
251 void pipeline_command_argv (pipeline *p, const char *name, va_list argv);
252 void pipeline_command_args (pipeline *p, const char *name, ...)
253         PIPELINE_ATTR_SENTINEL;
254
255 /* Construct a new command from a shell-quoted string and add it to a
256  * pipeline in one go. See the comment against pipecmd_new_argstr() above if
257  * you're tempted to use this function.
258  */
259 void pipeline_command_argstr (pipeline *p, const char *argstr);
260
261 /* Convenience functions wrapping pipeline_command().
262  * Terminate commands with NULL.
263  */
264 void pipeline_commandv (pipeline *p, va_list cmdv);
265 void pipeline_commands (pipeline *p, ...) PIPELINE_ATTR_SENTINEL;
266
267 /* Return the number of commands in this pipeline. */
268 int pipeline_get_ncommands (pipeline *p);
269
270 /* Return command number n from this pipeline, counting from zero, or NULL
271  * if n is out of range.
272  */
273 pipecmd *pipeline_get_command (pipeline *p, int n);
274
275 /* Set command number n in this pipeline, counting from zero, to cmd, and
276  * return the previous command in that position.  Do nothing and return NULL
277  * if n is out of range.
278  */
279 pipecmd *pipeline_set_command (pipeline *p, int n, pipecmd *cmd);
280
281 /* Return the process ID of command number n from this pipeline, counting
282  * from zero.  The pipeline must be started.  Return -1 if n is out of range
283  * or if the command has already exited and been reaped.
284  */
285 pid_t pipeline_get_pid (pipeline *p, int n);
286
287 /* Set file descriptors to use as the input and output of the whole
288  * pipeline.  If non-negative, fd is used directly as a file descriptor.  If
289  * negative, pipeline_start will create pipes and store the input writing
290  * half and the output reading half in the pipeline's infd or outfd field as
291  * appropriate.  The default is to leave input and output as stdin and
292  * stdout unless pipeline_want_infile or pipeline_want_outfile respectively
293  * has been called.
294  *
295  * Calling these functions supersedes any previous call to
296  * pipeline_want_infile or pipeline_want_outfile respectively.
297  */
298 void pipeline_want_in (pipeline *p, int fd);
299 void pipeline_want_out (pipeline *p, int fd);
300
301 /* Set file names to open and use as the input and output of the whole
302  * pipeline.  This may be more convenient than supplying file descriptors,
303  * and guarantees that the files are opened with the same privileges under
304  * which the pipeline is run.
305  *
306  * Calling these functions (even with NULL, which returns to the default of
307  * leaving input and output as stdin and stdout) supersedes any previous
308  * call to pipeline_want_in or pipeline_want_outfile respectively.
309  *
310  * The given files will be opened when the pipeline is started.  If an
311  * output file does not already exist, it is created (with mode 0666
312  * modified in the usual way by umask); if it does exist, then it is
313  * truncated.
314  */
315 void pipeline_want_infile (pipeline *p, const char *file);
316 void pipeline_want_outfile (pipeline *p, const char *file);
317
318 /* If ignore_signals is non-zero (which is the default), ignore SIGINT and
319  * SIGQUIT while the pipeline is running, like system().  Otherwise, leave
320  * their dispositions unchanged.
321  */
322 void pipeline_ignore_signals (pipeline *p, int ignore_signals);
323
324 /* Get streams corresponding to infd and outfd respectively. The pipeline
325  * must be started.
326  */
327 FILE *pipeline_get_infile (pipeline *p);
328 FILE *pipeline_get_outfile (pipeline *p);
329
330 /* Dump a string representation of p to stream. */
331 void pipeline_dump (pipeline *p, FILE *stream);
332
333 /* Return a string representation of p. The caller should free the result. */
334 char *pipeline_tostring (pipeline *p);
335
336 /* Destroy a pipeline and all its commands. Safely does nothing on NULL.
337  * May wait for the pipeline to complete if it has not already done so.
338  */
339 void pipeline_free (pipeline *p);
340
341 /* ---------------------------------------------------------------------- */
342
343 /* Functions to run pipelines and handle signals. */
344
345 typedef void pipeline_post_fork_fn (void);
346
347 /* Install a post-fork handler.  This will be run in any child process
348  * immediately after it is forked.  For instance, this may be used for
349  * cleaning up application-specific signal handlers.  Pass NULL to clear any
350  * existing post-fork handler.
351  */
352 void pipeline_install_post_fork (pipeline_post_fork_fn *fn);
353
354 /* Start the processes in a pipeline. Installs this library's SIGCHLD
355  * handler if not already installed. Calls error(FATAL) on error. */
356 void pipeline_start (pipeline *p);
357
358 /* Wait for a pipeline to complete.  Set *statuses to a newly-allocated
359  * array of wait statuses, as returned by waitpid, and *n_statuses to the
360  * length of that array.  The return value is similar to the exit status
361  * that a shell would return, with some modifications.  If the last command
362  * exits with a signal (other than SIGPIPE, which is considered equivalent
363  * to exiting zero), then the return value is 128 plus the signal number; if
364  * the last command exits normally but non-zero, then the return value is
365  * its exit status; if any other command exits non-zero, then the return
366  * value is 127; otherwise, the return value is 0.  This means that the
367  * return value is only 0 if all commands in the pipeline exit successfully.
368  */
369 int pipeline_wait_all (pipeline *p, int **statuses, int *n_statuses);
370
371 /* Wait for a pipeline to complete and return its combined exit status,
372  * calculated as for pipeline_wait_all().
373  */
374 int pipeline_wait (pipeline *p);
375
376 /* Start a pipeline, wait for it to complete, and free it, all in one go. */
377 int pipeline_run (pipeline *p);
378
379 /* Pump data among one or more pipelines connected using pipeline_connect()
380  * until all source pipelines have reached end-of-file and all data has been
381  * written to all sinks (or failed). All relevant pipelines must be
382  * supplied: that is, no pipeline that has been connected to a source
383  * pipeline may be supplied unless that source pipeline is also supplied.
384  * Automatically starts all pipelines if they are not already started, but
385  * does not wait for them. Terminate arguments with NULL.
386  */
387 void pipeline_pump (pipeline *p, ...) PIPELINE_ATTR_SENTINEL;
388
389 /* ---------------------------------------------------------------------- */
390
391 /* Functions to read output from pipelines. */
392
393 /* Read len bytes of data from the pipeline, returning the data block. len
394  * is updated with the number of bytes read.
395  */
396 const char *pipeline_read (pipeline *p, size_t *len);
397
398 /* Look ahead in the pipeline's output for len bytes of data, returning the
399  * data block. len is updated with the number of bytes read. The starting
400  * position of the next read or peek is not affected by this call.
401  */
402 const char *pipeline_peek (pipeline *p, size_t *len);
403
404 /* Return the number of bytes of data that can be read using pipeline_read
405  * or pipeline_peek solely from the peek cache, without having to read from
406  * the pipeline itself (and thus potentially block).
407  */
408 size_t pipeline_peek_size (pipeline *p);
409
410 /* Skip over and discard len bytes of data from the peek cache. Asserts that
411  * enough data is available to skip, so you may want to check using
412  * pipeline_peek_size first.
413  */
414 void pipeline_peek_skip (pipeline *p, size_t len);
415
416 /* Read a line of data from the pipeline, returning it. */
417 const char *pipeline_readline (pipeline *p);
418
419 /* Look ahead in the pipeline's output for a line of data, returning it. The
420  * starting position of the next read or peek is not affected by this call.
421  */
422 const char *pipeline_peekline (pipeline *p);
423
424 #ifdef __cplusplus
425 }
426 #endif
427
428 #endif /* PIPELINE_H */