Imported Upstream version 1.5.7
[platform/upstream/libpipeline.git] / lib / pipeline-private.h
1 /*
2  * Copyright (C) 2001-2017 Colin Watson.
3  *
4  * This file is part of libpipeline.
5  *
6  * libpipeline 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 2 of the License, or (at
9  * your option) any later version.
10  *
11  * libpipeline is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with libpipeline; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA.
20  */
21
22 #ifndef PIPELINE_PRIVATE_H
23 #define PIPELINE_PRIVATE_H
24
25 #include "pipeline.h"
26
27 /* exit codes */
28 #define OK 0    /* success */
29 #define FAIL 1  /* usage or syntax error */
30 #define FATAL 2 /* operational error */
31
32 extern char *appendstr (char *, ...)
33         PIPELINE_ATTR_SENTINEL PIPELINE_ATTR_WARN_UNUSED_RESULT;
34
35 extern void init_debug (void);
36 extern int debug_level;
37 extern void debug (const char *message, ...)
38         PIPELINE_ATTR_FORMAT_PRINTF (1, 2);
39
40 #if defined(HAVE_SETENV) && !defined(HAVE_CLEARENV)
41 extern int clearenv (void);
42 #endif
43
44 enum pipecmd_tag {
45         PIPECMD_PROCESS,
46         PIPECMD_FUNCTION,
47         PIPECMD_SEQUENCE
48 };
49
50 struct pipecmd_env {
51         char *name;
52         char *value;
53 };
54
55 struct pipecmd {
56         enum pipecmd_tag tag;
57         char *name;
58         int nice;
59         int discard_err; /* discard stderr? */
60         int cwd_fd;
61         char *cwd;
62         int nenv;
63         int env_max; /* size of allocated array */
64         struct pipecmd_env *env;
65         pipecmd_function_type *pre_exec_func;
66         pipecmd_function_type *pre_exec_free_func;
67         void *pre_exec_data;
68         union {
69                 struct pipecmd_process {
70                         int argc;
71                         int argv_max; /* size of allocated array */
72                         char **argv;
73                 } process;
74                 struct pipecmd_function {
75                         pipecmd_function_type *func;
76                         pipecmd_function_free_type *free_func;
77                         void *data;
78                 } function;
79                 struct pipecmd_sequence {
80                         int ncommands;
81                         int commands_max;
82                         struct pipecmd **commands;
83                 } sequence;
84         } u;
85 };
86
87 enum pipeline_redirect {
88         REDIRECT_NONE,
89         REDIRECT_FD,
90         REDIRECT_FILE_NAME
91 };
92
93 struct pipeline {
94         int ncommands;
95         int commands_max; /* size of allocated array */
96         pipecmd **commands;
97         pid_t *pids;
98         int *statuses; /* -1 until command exits */
99
100         /* REDIRECT_NONE for no redirection; REDIRECT_FD for redirection
101          * from/to file descriptor; REDIRECT_FILE_NAME for redirection
102          * from/to file name.
103          */
104         enum pipeline_redirect redirect_in, redirect_out;
105
106         /* If non-negative, these contain caller-supplied file descriptors
107          * for the input and output of the whole pipeline.  If negative,
108          * pipeline_start() will create pipes and store the input writing
109          * half and the output reading half in infd and outfd as
110          * appropriate.
111          */
112         int want_in, want_out;
113
114         /* If non-NULL, these contain files to open and use as the input and
115          * output of the whole pipeline.  These are only used if want_in or
116          * want_out respectively is zero.  The value of using these rather
117          * than simply opening the files before starting the pipeline is
118          * that the files will be opened with the same privileges under
119          * which the pipeline is being run.
120          */
121         char *want_infile, *want_outfile;
122
123         /* See above. Default to -1. The caller should consider these
124          * read-only.
125          */
126         int infd, outfd;
127
128         /* Set by pipeline_get_infile() and pipeline_get_outfile()
129          * respectively. Default to NULL.
130          */
131         FILE *infile, *outfile;
132
133         /* Set by pipeline_connect() to record that this pipeline reads its
134          * input from another pipeline. Defaults to NULL.
135          */
136         struct pipeline *source;
137
138         /* Private buffer for use by read/peek functions. */
139         char *buffer;
140         size_t buflen, bufmax;
141
142         /* The last line returned by readline/peekline. Private. */
143         char *line_cache;
144
145         /* The amount of data at the end of buffer which has been
146          * read-ahead, either by an explicit peek or by readline/peekline
147          * reading a block at a time to save work. Private.
148          */
149         size_t peek_offset;
150
151         /* If set, ignore SIGINT and SIGQUIT while the pipeline is running,
152          * like system(). Defaults to 1.
153          */
154         int ignore_signals;
155 };
156
157 #endif /* PIPELINE_PRIVATE_H */