Blame lib/pipeline.h

Packit Service 51e54d
/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002
Packit Service 51e54d
 * Free Software Foundation, Inc.
Packit Service 51e54d
 * Copyright (C) 2003-2017 Colin Watson.
Packit Service 51e54d
 *   Written for groff by James Clark (jjc@jclark.com)
Packit Service 51e54d
 *   Adapted for man-db by Colin Watson.
Packit Service 51e54d
 *
Packit Service 51e54d
 * This file is part of libpipeline.
Packit Service 51e54d
 *
Packit Service 51e54d
 * libpipeline is free software; you can redistribute it and/or modify
Packit Service 51e54d
 * it under the terms of the GNU General Public License as published by
Packit Service 51e54d
 * the Free Software Foundation; either version 2 of the License, or (at
Packit Service 51e54d
 * your option) any later version.
Packit Service 51e54d
 *
Packit Service 51e54d
 * libpipeline is distributed in the hope that it will be useful, but
Packit Service 51e54d
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 51e54d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 51e54d
 * General Public License for more details.
Packit Service 51e54d
 *
Packit Service 51e54d
 * You should have received a copy of the GNU General Public License
Packit Service 51e54d
 * along with libpipeline; if not, write to the Free Software
Packit Service 51e54d
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
Packit Service 51e54d
 * USA.
Packit Service 51e54d
 */
Packit Service 51e54d
Packit Service 51e54d
#ifndef PIPELINE_H
Packit Service 51e54d
#define PIPELINE_H
Packit Service 51e54d
Packit Service 51e54d
#ifdef __cplusplus
Packit Service 51e54d
extern "C" {
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
#include <stdio.h>
Packit Service 51e54d
#include <stdarg.h>
Packit Service 51e54d
#include <sys/types.h>
Packit Service 51e54d
Packit Service 51e54d
/* GCC version checking borrowed from glibc. */
Packit Service 51e54d
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
Packit Service 51e54d
#  define PIPELINE_GNUC_PREREQ(maj,min) \
Packit Service 51e54d
	((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
Packit Service 51e54d
#else
Packit Service 51e54d
#  define PIPELINE_GNUC_PREREQ(maj,min) 0
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
/* Does this compiler support format string checking? */
Packit Service 51e54d
#if PIPELINE_GNUC_PREREQ(2,0)
Packit Service 51e54d
#  define PIPELINE_ATTR_FORMAT_PRINTF(a,b) \
Packit Service 51e54d
	__attribute__ ((__format__ (__printf__, a, b)))
Packit Service 51e54d
#else
Packit Service 51e54d
#  define PIPELINE_ATTR_FORMAT_PRINTF(a,b)
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
/* Does this compiler support marking variables as unused? */
Packit Service 51e54d
#if PIPELINE_GNUC_PREREQ(2,4)
Packit Service 51e54d
#  define PIPELINE_ATTR_UNUSED __attribute__ ((__unused__))
Packit Service 51e54d
#else
Packit Service 51e54d
#  define PIPELINE_ATTR_UNUSED
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
/* Does this compiler support marking functions as non-returning? */
Packit Service 51e54d
#if PIPELINE_GNUC_PREREQ(2,5)
Packit Service 51e54d
#  define PIPELINE_ATTR_NORETURN __attribute__ ((__noreturn__))
Packit Service 51e54d
#else
Packit Service 51e54d
#  define PIPELINE_ATTR_NORETURN
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
/* Does this compiler support unused result checking? */
Packit Service 51e54d
#if PIPELINE_GNUC_PREREQ(3,4)
Packit Service 51e54d
#  define PIPELINE_ATTR_WARN_UNUSED_RESULT \
Packit Service 51e54d
	__attribute__ ((__warn_unused_result__))
Packit Service 51e54d
#else
Packit Service 51e54d
#  define PIPELINE_ATTR_WARN_UNUSED_RESULT
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
/* Does this compiler support sentinel checking? */
Packit Service 51e54d
#if PIPELINE_GNUC_PREREQ(4,0)
Packit Service 51e54d
#  define PIPELINE_ATTR_SENTINEL __attribute__ ((__sentinel__))
Packit Service 51e54d
#else
Packit Service 51e54d
#  define PIPELINE_ATTR_SENTINEL
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
typedef void pipecmd_function_type (void *);
Packit Service 51e54d
typedef void pipecmd_function_free_type (void *);
Packit Service 51e54d
Packit Service 51e54d
struct pipecmd;
Packit Service 51e54d
typedef struct pipecmd pipecmd;
Packit Service 51e54d
Packit Service 51e54d
struct pipeline;
Packit Service 51e54d
typedef struct pipeline pipeline;
Packit Service 51e54d
Packit Service 51e54d
/* ---------------------------------------------------------------------- */
Packit Service 51e54d
Packit Service 51e54d
/* Functions to build individual commands. */
Packit Service 51e54d
Packit Service 51e54d
/* Construct a new command. */
Packit Service 51e54d
pipecmd *pipecmd_new (const char *name);
Packit Service 51e54d
Packit Service 51e54d
/* Convenience constructors wrapping pipecmd_new() and pipecmd_arg().
Packit Service 51e54d
 * Terminate arguments with NULL.
Packit Service 51e54d
 */
Packit Service 51e54d
pipecmd *pipecmd_new_argv (const char *name, va_list argv);
Packit Service 51e54d
pipecmd *pipecmd_new_args (const char *name, ...) PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Split argstr on whitespace to construct a command and arguments,
Packit Service 51e54d
 * honouring shell-style single-quoting, double-quoting, and backslashes,
Packit Service 51e54d
 * but not other shell evil like wildcards, semicolons, or backquotes. This
Packit Service 51e54d
 * is a backward-compatibility hack to support old configuration file
Packit Service 51e54d
 * directives; please try to avoid using it in new code.
Packit Service 51e54d
 */
Packit Service 51e54d
pipecmd *pipecmd_new_argstr (const char *argstr);
Packit Service 51e54d
Packit Service 51e54d
/* Construct a new command that calls a given function rather than executing
Packit Service 51e54d
 * a process. The data argument is passed as the function's only argument,
Packit Service 51e54d
 * and will be freed before returning using free_func (if non-NULL).
Packit Service 51e54d
 *
Packit Service 51e54d
 * pipecmd_* functions that deal with arguments cannot be used with the
Packit Service 51e54d
 * command returned by this function.
Packit Service 51e54d
 */
Packit Service 51e54d
pipecmd *pipecmd_new_function (const char *name,
Packit Service 51e54d
			       pipecmd_function_type *func,
Packit Service 51e54d
			       pipecmd_function_free_type *free_func,
Packit Service 51e54d
			       void *data);
Packit Service 51e54d
Packit Service 51e54d
/* Construct a new command that runs a sequence of commands. The commands
Packit Service 51e54d
 * will be executed in forked children; if any exits non-zero then it will
Packit Service 51e54d
 * terminate the sequence, as with "&&" in shell.
Packit Service 51e54d
 *
Packit Service 51e54d
 * pipecmd_* functions that deal with arguments cannot be used with the
Packit Service 51e54d
 * command returned by this function.
Packit Service 51e54d
 */
Packit Service 51e54d
pipecmd *pipecmd_new_sequencev (const char *name, va_list cmdv);
Packit Service 51e54d
pipecmd *pipecmd_new_sequence (const char *name, ...) PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Return a new command that just passes data from its input to its output. */
Packit Service 51e54d
pipecmd *pipecmd_new_passthrough (void);
Packit Service 51e54d
Packit Service 51e54d
/* Return a duplicate of a command. */
Packit Service 51e54d
pipecmd *pipecmd_dup (pipecmd *cmd);
Packit Service 51e54d
Packit Service 51e54d
/* Add an argument to a command. */
Packit Service 51e54d
void pipecmd_arg (pipecmd *cmd, const char *arg);
Packit Service 51e54d
Packit Service 51e54d
/* Convenience function to add an argument with printf substitutions. */
Packit Service 51e54d
void pipecmd_argf (pipecmd *cmd, const char *format, ...)
Packit Service 51e54d
	PIPELINE_ATTR_FORMAT_PRINTF(2, 3);
Packit Service 51e54d
Packit Service 51e54d
/* Convenience functions wrapping pipecmd_arg().
Packit Service 51e54d
 * Terminate arguments with NULL.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipecmd_argv (pipecmd *cmd, va_list argv);
Packit Service 51e54d
void pipecmd_args (pipecmd *cmd, ...) PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Split argstr on whitespace to add a list of arguments, honouring
Packit Service 51e54d
 * shell-style single-quoting, double-quoting, and backslashes, but not
Packit Service 51e54d
 * other shell evil like wildcards, semicolons, or backquotes. This is a
Packit Service 51e54d
 * backward-compatibility hack to support old configuration file directives;
Packit Service 51e54d
 * please try to avoid using it in new code.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipecmd_argstr (pipecmd *cmd, const char *argstr);
Packit Service 51e54d
Packit Service 51e54d
/* Return the number of arguments to this command.  Note that this includes
Packit Service 51e54d
 * the command name as the first argument, so the command 'echo foo bar' is
Packit Service 51e54d
 * counted as having three arguments.
Packit Service 51e54d
 */
Packit Service 51e54d
int pipecmd_get_nargs (pipecmd *cmd);
Packit Service 51e54d
Packit Service 51e54d
/* Set the nice(3) value for this command.  Defaults to 0.  Errors while
Packit Service 51e54d
 * attempting to set the nice value are ignored, aside from emitting a debug
Packit Service 51e54d
 * message.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipecmd_nice (pipecmd *cmd, int nice);
Packit Service 51e54d
Packit Service 51e54d
/* If discard_err is non-zero, redirect this command's standard error to
Packit Service 51e54d
 * /dev/null.  Otherwise, and by default, pass it through.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipecmd_discard_err (pipecmd *cmd, int discard_err);
Packit Service 51e54d
Packit Service 51e54d
/* Change to this directory while running this command. */
Packit Service 51e54d
void pipecmd_chdir (pipecmd *cmd, const char *directory);
Packit Service 51e54d
Packit Service 51e54d
/* Change to the directory given by this open file descriptor while running
Packit Service 51e54d
 * this command. */
Packit Service 51e54d
void pipecmd_fchdir (pipecmd *cmd, int directory_fd);
Packit Service 51e54d
Packit Service 51e54d
/* Set an environment variable while running this command. */
Packit Service 51e54d
void pipecmd_setenv (pipecmd *cmd, const char *name, const char *value);
Packit Service 51e54d
Packit Service 51e54d
/* Unset an environment variable while running this command. */
Packit Service 51e54d
void pipecmd_unsetenv (pipecmd *cmd, const char *name);
Packit Service 51e54d
Packit Service 51e54d
/* Clear the environment while running this command.  (Note that environment
Packit Service 51e54d
 * operations work in sequence; pipecmd_clearenv followed by pipecmd_setenv
Packit Service 51e54d
 * causes the command to have just a single environment variable set.)
Packit Service 51e54d
 * Beware that this may cause unexpected failures, for example if some of
Packit Service 51e54d
 * the contents of the environment are necessary to execute programs at all
Packit Service 51e54d
 * (say, PATH).
Packit Service 51e54d
 */
Packit Service 51e54d
void pipecmd_clearenv (pipecmd *cmd);
Packit Service 51e54d
Packit Service 51e54d
/* Install a pre-exec handler.  This will be run immediately before
Packit Service 51e54d
 * executing the command's payload (process or function).  Pass NULL to
Packit Service 51e54d
 * clear any existing pre-exec handler.  The data argument is passed as the
Packit Service 51e54d
 * function's only argument, and will be freed before returning using
Packit Service 51e54d
 * free_func (if non-NULL).
Packit Service 51e54d
 *
Packit Service 51e54d
 * This is similar to pipeline_install_post_fork, except that is specific to
Packit Service 51e54d
 * a single command rather than installing a global handler, and it runs
Packit Service 51e54d
 * slightly later (immediately before exec rather than immediately after
Packit Service 51e54d
 * fork).
Packit Service 51e54d
 */
Packit Service 51e54d
void pipecmd_pre_exec (pipecmd *cmd,
Packit Service 51e54d
		       pipecmd_function_type *func,
Packit Service 51e54d
		       pipecmd_function_free_type *free_func,
Packit Service 51e54d
		       void *data);
Packit Service 51e54d
Packit Service 51e54d
/* Add a command to a sequence. */
Packit Service 51e54d
void pipecmd_sequence_command (pipecmd *cmd, pipecmd *child);
Packit Service 51e54d
Packit Service 51e54d
/* Dump a string representation of a command to stream. */
Packit Service 51e54d
void pipecmd_dump (pipecmd *cmd, FILE *stream);
Packit Service 51e54d
Packit Service 51e54d
/* Return a string representation of a command. The caller should free the
Packit Service 51e54d
 * result.
Packit Service 51e54d
 */
Packit Service 51e54d
char *pipecmd_tostring (pipecmd *cmd);
Packit Service 51e54d
Packit Service 51e54d
/* Execute a single command, replacing the current process.  Never returns,
Packit Service 51e54d
 * instead exiting non-zero on failure.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipecmd_exec (pipecmd *cmd) PIPELINE_ATTR_NORETURN;
Packit Service 51e54d
Packit Service 51e54d
/* Destroy a command. Safely does nothing on NULL. */
Packit Service 51e54d
void pipecmd_free (pipecmd *cmd);
Packit Service 51e54d
Packit Service 51e54d
/* ---------------------------------------------------------------------- */
Packit Service 51e54d
Packit Service 51e54d
/* Functions to build pipelines. */
Packit Service 51e54d
Packit Service 51e54d
/* Construct a new pipeline. */
Packit Service 51e54d
pipeline *pipeline_new (void);
Packit Service 51e54d
Packit Service 51e54d
/* Convenience constructors wrapping pipeline_new() and pipeline_command().
Packit Service 51e54d
 * Terminate commands with NULL.
Packit Service 51e54d
 */
Packit Service 51e54d
pipeline *pipeline_new_commandv (pipecmd *cmd1, va_list cmdv);
Packit Service 51e54d
pipeline *pipeline_new_commands (pipecmd *cmd1, ...) PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Construct a new pipeline and add a single command to it. */
Packit Service 51e54d
pipeline *pipeline_new_command_argv (const char *name, va_list argv);
Packit Service 51e54d
pipeline *pipeline_new_command_args (const char *name, ...)
Packit Service 51e54d
	PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Joins two pipelines, neither of which are allowed to be started. Discards
Packit Service 51e54d
 * want_out, want_outfile, and outfd from p1, and want_in, want_infile, and
Packit Service 51e54d
 * infd from p2.
Packit Service 51e54d
 */
Packit Service 51e54d
pipeline *pipeline_join (pipeline *p1, pipeline *p2);
Packit Service 51e54d
Packit Service 51e54d
/* Connect the input of one or more sink pipelines to the output of a source
Packit Service 51e54d
 * pipeline. The source pipeline may be started, but in that case want_out
Packit Service 51e54d
 * must be negative; otherwise, discards want_out from source. In any event,
Packit Service 51e54d
 * discards want_in from all sinks, none of which are allowed to be started.
Packit Service 51e54d
 * Terminate arguments with NULL.
Packit Service 51e54d
 *
Packit Service 51e54d
 * This is an application-level connection; data may be intercepted between
Packit Service 51e54d
 * the pipelines by the program before calling pipeline_pump(), which sets
Packit Service 51e54d
 * data flowing from the source to the sinks. It is primarily useful when
Packit Service 51e54d
 * more than one sink pipeline is involved, in which case the pipelines
Packit Service 51e54d
 * cannot simply be concatenated into one.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_connect (pipeline *source, pipeline *sink, ...)
Packit Service 51e54d
	PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Add a command to a pipeline. */
Packit Service 51e54d
void pipeline_command (pipeline *p, pipecmd *cmd);
Packit Service 51e54d
Packit Service 51e54d
/* Construct a new command and add it to a pipeline in one go. */
Packit Service 51e54d
void pipeline_command_argv (pipeline *p, const char *name, va_list argv);
Packit Service 51e54d
void pipeline_command_args (pipeline *p, const char *name, ...)
Packit Service 51e54d
	PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Construct a new command from a shell-quoted string and add it to a
Packit Service 51e54d
 * pipeline in one go. See the comment against pipecmd_new_argstr() above if
Packit Service 51e54d
 * you're tempted to use this function.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_command_argstr (pipeline *p, const char *argstr);
Packit Service 51e54d
Packit Service 51e54d
/* Convenience functions wrapping pipeline_command().
Packit Service 51e54d
 * Terminate commands with NULL.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_commandv (pipeline *p, va_list cmdv);
Packit Service 51e54d
void pipeline_commands (pipeline *p, ...) PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* Return the number of commands in this pipeline. */
Packit Service 51e54d
int pipeline_get_ncommands (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Return command number n from this pipeline, counting from zero, or NULL
Packit Service 51e54d
 * if n is out of range.
Packit Service 51e54d
 */
Packit Service 51e54d
pipecmd *pipeline_get_command (pipeline *p, int n);
Packit Service 51e54d
Packit Service 51e54d
/* Set command number n in this pipeline, counting from zero, to cmd, and
Packit Service 51e54d
 * return the previous command in that position.  Do nothing and return NULL
Packit Service 51e54d
 * if n is out of range.
Packit Service 51e54d
 */
Packit Service 51e54d
pipecmd *pipeline_set_command (pipeline *p, int n, pipecmd *cmd);
Packit Service 51e54d
Packit Service 51e54d
/* Return the process ID of command number n from this pipeline, counting
Packit Service 51e54d
 * from zero.  The pipeline must be started.  Return -1 if n is out of range
Packit Service 51e54d
 * or if the command has already exited and been reaped.
Packit Service 51e54d
 */
Packit Service 51e54d
pid_t pipeline_get_pid (pipeline *p, int n);
Packit Service 51e54d
Packit Service 51e54d
/* Set file descriptors to use as the input and output of the whole
Packit Service 51e54d
 * pipeline.  If non-negative, fd is used directly as a file descriptor.  If
Packit Service 51e54d
 * negative, pipeline_start will create pipes and store the input writing
Packit Service 51e54d
 * half and the output reading half in the pipeline's infd or outfd field as
Packit Service 51e54d
 * appropriate.  The default is to leave input and output as stdin and
Packit Service 51e54d
 * stdout unless pipeline_want_infile or pipeline_want_outfile respectively
Packit Service 51e54d
 * has been called.
Packit Service 51e54d
 *
Packit Service 51e54d
 * Calling these functions supersedes any previous call to
Packit Service 51e54d
 * pipeline_want_infile or pipeline_want_outfile respectively.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_want_in (pipeline *p, int fd);
Packit Service 51e54d
void pipeline_want_out (pipeline *p, int fd);
Packit Service 51e54d
Packit Service 51e54d
/* Set file names to open and use as the input and output of the whole
Packit Service 51e54d
 * pipeline.  This may be more convenient than supplying file descriptors,
Packit Service 51e54d
 * and guarantees that the files are opened with the same privileges under
Packit Service 51e54d
 * which the pipeline is run.
Packit Service 51e54d
 *
Packit Service 51e54d
 * Calling these functions (even with NULL, which returns to the default of
Packit Service 51e54d
 * leaving input and output as stdin and stdout) supersedes any previous
Packit Service 51e54d
 * call to pipeline_want_in or pipeline_want_outfile respectively.
Packit Service 51e54d
 *
Packit Service 51e54d
 * The given files will be opened when the pipeline is started.  If an
Packit Service 51e54d
 * output file does not already exist, it is created (with mode 0666
Packit Service 51e54d
 * modified in the usual way by umask); if it does exist, then it is
Packit Service 51e54d
 * truncated.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_want_infile (pipeline *p, const char *file);
Packit Service 51e54d
void pipeline_want_outfile (pipeline *p, const char *file);
Packit Service 51e54d
Packit Service 51e54d
/* If ignore_signals is non-zero (which is the default), ignore SIGINT and
Packit Service 51e54d
 * SIGQUIT while the pipeline is running, like system().  Otherwise, leave
Packit Service 51e54d
 * their dispositions unchanged.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_ignore_signals (pipeline *p, int ignore_signals);
Packit Service 51e54d
Packit Service 51e54d
/* Get streams corresponding to infd and outfd respectively. The pipeline
Packit Service 51e54d
 * must be started.
Packit Service 51e54d
 */
Packit Service 51e54d
FILE *pipeline_get_infile (pipeline *p);
Packit Service 51e54d
FILE *pipeline_get_outfile (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Dump a string representation of p to stream. */
Packit Service 51e54d
void pipeline_dump (pipeline *p, FILE *stream);
Packit Service 51e54d
Packit Service 51e54d
/* Return a string representation of p. The caller should free the result. */
Packit Service 51e54d
char *pipeline_tostring (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Destroy a pipeline and all its commands. Safely does nothing on NULL.
Packit Service 51e54d
 * May wait for the pipeline to complete if it has not already done so.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_free (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* ---------------------------------------------------------------------- */
Packit Service 51e54d
Packit Service 51e54d
/* Functions to run pipelines and handle signals. */
Packit Service 51e54d
Packit Service 51e54d
typedef void pipeline_post_fork_fn (void);
Packit Service 51e54d
Packit Service 51e54d
/* Install a post-fork handler.  This will be run in any child process
Packit Service 51e54d
 * immediately after it is forked.  For instance, this may be used for
Packit Service 51e54d
 * cleaning up application-specific signal handlers.  Pass NULL to clear any
Packit Service 51e54d
 * existing post-fork handler.
Packit Service 51e54d
 *
Packit Service 51e54d
 * See pipecmd_pre_exec for a similar facility limited to a single command
Packit Service 51e54d
 * rather than global to the calling process.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_install_post_fork (pipeline_post_fork_fn *fn);
Packit Service 51e54d
Packit Service 51e54d
/* Start the processes in a pipeline. Installs this library's SIGCHLD
Packit Service 51e54d
 * handler if not already installed. Calls error(FATAL) on error. */
Packit Service 51e54d
void pipeline_start (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Wait for a pipeline to complete.  Set *statuses to a newly-allocated
Packit Service 51e54d
 * array of wait statuses, as returned by waitpid, and *n_statuses to the
Packit Service 51e54d
 * length of that array.  The return value is similar to the exit status
Packit Service 51e54d
 * that a shell would return, with some modifications.  If the last command
Packit Service 51e54d
 * exits with a signal (other than SIGPIPE, which is considered equivalent
Packit Service 51e54d
 * to exiting zero), then the return value is 128 plus the signal number; if
Packit Service 51e54d
 * the last command exits normally but non-zero, then the return value is
Packit Service 51e54d
 * its exit status; if any other command exits non-zero, then the return
Packit Service 51e54d
 * value is 127; otherwise, the return value is 0.  This means that the
Packit Service 51e54d
 * return value is only 0 if all commands in the pipeline exit successfully.
Packit Service 51e54d
 */
Packit Service 51e54d
int pipeline_wait_all (pipeline *p, int **statuses, int *n_statuses);
Packit Service 51e54d
Packit Service 51e54d
/* Wait for a pipeline to complete and return its combined exit status,
Packit Service 51e54d
 * calculated as for pipeline_wait_all().
Packit Service 51e54d
 */
Packit Service 51e54d
int pipeline_wait (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Start a pipeline, wait for it to complete, and free it, all in one go. */
Packit Service 51e54d
int pipeline_run (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Pump data among one or more pipelines connected using pipeline_connect()
Packit Service 51e54d
 * until all source pipelines have reached end-of-file and all data has been
Packit Service 51e54d
 * written to all sinks (or failed). All relevant pipelines must be
Packit Service 51e54d
 * supplied: that is, no pipeline that has been connected to a source
Packit Service 51e54d
 * pipeline may be supplied unless that source pipeline is also supplied.
Packit Service 51e54d
 * Automatically starts all pipelines if they are not already started, but
Packit Service 51e54d
 * does not wait for them. Terminate arguments with NULL.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_pump (pipeline *p, ...) PIPELINE_ATTR_SENTINEL;
Packit Service 51e54d
Packit Service 51e54d
/* ---------------------------------------------------------------------- */
Packit Service 51e54d
Packit Service 51e54d
/* Functions to read output from pipelines. */
Packit Service 51e54d
Packit Service 51e54d
/* Read len bytes of data from the pipeline, returning the data block. len
Packit Service 51e54d
 * is updated with the number of bytes read.
Packit Service 51e54d
 */
Packit Service 51e54d
const char *pipeline_read (pipeline *p, size_t *len);
Packit Service 51e54d
Packit Service 51e54d
/* Look ahead in the pipeline's output for len bytes of data, returning the
Packit Service 51e54d
 * data block. len is updated with the number of bytes read. The starting
Packit Service 51e54d
 * position of the next read or peek is not affected by this call.
Packit Service 51e54d
 */
Packit Service 51e54d
const char *pipeline_peek (pipeline *p, size_t *len);
Packit Service 51e54d
Packit Service 51e54d
/* Return the number of bytes of data that can be read using pipeline_read
Packit Service 51e54d
 * or pipeline_peek solely from the peek cache, without having to read from
Packit Service 51e54d
 * the pipeline itself (and thus potentially block).
Packit Service 51e54d
 */
Packit Service 51e54d
size_t pipeline_peek_size (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Skip over and discard len bytes of data from the peek cache. Asserts that
Packit Service 51e54d
 * enough data is available to skip, so you may want to check using
Packit Service 51e54d
 * pipeline_peek_size first.
Packit Service 51e54d
 */
Packit Service 51e54d
void pipeline_peek_skip (pipeline *p, size_t len);
Packit Service 51e54d
Packit Service 51e54d
/* Read a line of data from the pipeline, returning it. */
Packit Service 51e54d
const char *pipeline_readline (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
/* Look ahead in the pipeline's output for a line of data, returning it. The
Packit Service 51e54d
 * starting position of the next read or peek is not affected by this call.
Packit Service 51e54d
 */
Packit Service 51e54d
const char *pipeline_peekline (pipeline *p);
Packit Service 51e54d
Packit Service 51e54d
#ifdef __cplusplus
Packit Service 51e54d
}
Packit Service 51e54d
#endif
Packit Service 51e54d
Packit Service 51e54d
#endif /* PIPELINE_H */