Imported Upstream version 1.2.2
[platform/upstream/libpipeline.git] / lib / pipeline-private.h
1 /*
2  * Copyright (C) 2001, 2002, 2005, 2007, 2009, 2010 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, ...) PIPELINE_ATTR_FORMAT_PRINTF(1, 2);
38
39 enum pipecmd_tag {
40         PIPECMD_PROCESS,
41         PIPECMD_FUNCTION,
42         PIPECMD_SEQUENCE
43 };
44
45 struct pipecmd_env {
46         char *name;
47         char *value;
48 };
49
50 struct pipecmd {
51         enum pipecmd_tag tag;
52         char *name;
53         int nice;
54         int discard_err;        /* discard stderr? */
55         int nenv;
56         int env_max;            /* size of allocated array */
57         struct pipecmd_env *env;
58         union {
59                 struct pipecmd_process {
60                         int argc;
61                         int argv_max;   /* size of allocated array */
62                         char **argv;
63                 } process;
64                 struct pipecmd_function {
65                         pipecmd_function_type *func;
66                         pipecmd_function_free_type *free_func;
67                         void *data;
68                 } function;
69                 struct pipecmd_sequence {
70                         int ncommands;
71                         int commands_max;
72                         struct pipecmd **commands;
73                 } sequence;
74         } u;
75 };
76
77 enum pipeline_redirect {
78         REDIRECT_NONE,
79         REDIRECT_FD,
80         REDIRECT_FILE_NAME
81 };
82
83 struct pipeline {
84         int ncommands;
85         int commands_max;       /* size of allocated array */
86         pipecmd **commands;
87         pid_t *pids;
88         int *statuses;          /* -1 until command exits */
89
90         /* REDIRECT_NONE for no redirection; REDIRECT_FD for redirection
91          * from/to file descriptor; REDIRECT_FILE_NAME for redirection
92          * from/to file name.
93          */
94         enum pipeline_redirect redirect_in, redirect_out;
95
96         /* If non-negative, these contain caller-supplied file descriptors
97          * for the input and output of the whole pipeline.  If negative,
98          * pipeline_start() will create pipes and store the input writing
99          * half and the output reading half in infd and outfd as
100          * appropriate.
101          */
102         int want_in, want_out;
103
104         /* If non-NULL, these contain files to open and use as the input and
105          * output of the whole pipeline.  These are only used if want_in or
106          * want_out respectively is zero.  The value of using these rather
107          * than simply opening the files before starting the pipeline is
108          * that the files will be opened with the same privileges under
109          * which the pipeline is being run.
110          */
111         const char *want_infile, *want_outfile;
112
113         /* See above. Default to -1. The caller should consider these
114          * read-only.
115          */
116         int infd, outfd;
117
118         /* Set by pipeline_get_infile() and pipeline_get_outfile()
119          * respectively. Default to NULL.
120          */
121         FILE *infile, *outfile;
122
123         /* Set by pipeline_connect() to record that this pipeline reads its
124          * input from another pipeline. Defaults to NULL.
125          */
126         struct pipeline *source;
127
128         /* Private buffer for use by read/peek functions. */
129         char *buffer;
130         size_t buflen, bufmax;
131
132         /* The last line returned by readline/peekline. Private. */
133         char *line_cache;
134
135         /* The amount of data at the end of buffer which has been
136          * read-ahead, either by an explicit peek or by readline/peekline
137          * reading a block at a time to save work. Private.
138          */
139         size_t peek_offset;
140
141         /* If set, ignore SIGINT and SIGQUIT while the pipeline is running,
142          * like system(). Defaults to 1.
143          */
144         int ignore_signals;
145 };
146
147 #endif /* PIPELINE_PRIVATE_H */