NeoMutt  2024-02-01-23-g345d7b
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
filter.h File Reference

Pass files through external commands (filters) More...

#include <stdio.h>
#include <unistd.h>
+ Include dependency graph for filter.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define EXEC_SHELL   "/bin/sh"
 

Functions

pid_t filter_create (const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, char **envlist)
 Set up filter program.
 
pid_t filter_create_fd (const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, int fdin, int fdout, int fderr, char **envlist)
 Run a command on a pipe (optionally connect stdin/stdout)
 
int filter_wait (pid_t pid)
 Wait for the exit of a process and return its status.
 

Detailed Description

Pass files through external commands (filters)

Authors

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file filter.h.

Macro Definition Documentation

◆ EXEC_SHELL

#define EXEC_SHELL   "/bin/sh"

Definition at line 28 of file filter.h.

Function Documentation

◆ filter_create()

pid_t filter_create ( const char *  cmd,
FILE **  fp_in,
FILE **  fp_out,
FILE **  fp_err,
char **  envlist 
)

Set up filter program.

Parameters
[in]cmdCommand string
[out]fp_inFILE pointer of stdin
[out]fp_outFILE pointer of stdout
[out]fp_errFILE pointer of stderr
[in]envlistEnvironment variables
Return values
numPID of filter

Definition at line 208 of file filter.c.

209{
210 return filter_create_fd(cmd, fp_in, fp_out, fp_err, -1, -1, -1, envlist);
211}
pid_t filter_create_fd(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, int fdin, int fdout, int fderr, char **envlist)
Run a command on a pipe (optionally connect stdin/stdout)
Definition: filter.c:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ filter_create_fd()

pid_t filter_create_fd ( const char *  cmd,
FILE **  fp_in,
FILE **  fp_out,
FILE **  fp_err,
int  fdin,
int  fdout,
int  fderr,
char **  envlist 
)

Run a command on a pipe (optionally connect stdin/stdout)

Parameters
[in]cmdCommand line to invoke using sh -c
[out]fp_inFile stream pointing to stdin for the command process, can be NULL
[out]fp_outFile stream pointing to stdout for the command process, can be NULL
[out]fp_errFile stream pointing to stderr for the command process, can be NULL
[in]fdinIf fp_in is NULL and fdin is not -1 then fdin will be used as stdin for the command process
[in]fdoutIf fp_out is NULL and fdout is not -1 then fdout will be used as stdout for the command process
[in]fderrIf fp_err is NULL and fderr is not -1 then fderr will be used as stderr for the command process
[in]envlistEnvironment variables
Return values
numPID of the created process
-1Error creating pipes or forking

This function provides multiple mechanisms to handle IO sharing for the command process. File streams are prioritized over file descriptors if present.

filter_create_fd(commandline, NULL, NULL, NULL, -1, -1, -1);

Additionally, fp_in, fp_out, and fp_err will point to FILE* streams representing the processes stdin, stdout, and stderr.

Definition at line 62 of file filter.c.

64{
65 int pin[2], pout[2], perr[2], pid;
66
67 if (fp_in)
68 {
69 *fp_in = NULL;
70 if (pipe(pin) == -1)
71 return -1;
72 }
73
74 if (fp_out)
75 {
76 *fp_out = NULL;
77 if (pipe(pout) == -1)
78 {
79 if (fp_in)
80 {
81 close(pin[0]);
82 close(pin[1]);
83 }
84 return -1;
85 }
86 }
87
88 if (fp_err)
89 {
90 *fp_err = NULL;
91 if (pipe(perr) == -1)
92 {
93 if (fp_in)
94 {
95 close(pin[0]);
96 close(pin[1]);
97 }
98 if (fp_out)
99 {
100 close(pout[0]);
101 close(pout[1]);
102 }
103 return -1;
104 }
105 }
106
108
109 pid = fork();
110 if (pid == 0)
111 {
113
114 if (fp_in)
115 {
116 close(pin[1]);
117 dup2(pin[0], 0);
118 close(pin[0]);
119 }
120 else if (fdin != -1)
121 {
122 dup2(fdin, 0);
123 close(fdin);
124 }
125
126 if (fp_out)
127 {
128 close(pout[0]);
129 dup2(pout[1], 1);
130 close(pout[1]);
131 }
132 else if (fdout != -1)
133 {
134 dup2(fdout, 1);
135 close(fdout);
136 }
137
138 if (fp_err)
139 {
140 close(perr[0]);
141 dup2(perr[1], 2);
142 close(perr[1]);
143 }
144 else if (fderr != -1)
145 {
146 dup2(fderr, 2);
147 close(fderr);
148 }
149
150 execle(EXEC_SHELL, "sh", "-c", cmd, NULL, envlist);
151 _exit(127);
152 }
153 else if (pid == -1)
154 {
156
157 if (fp_in)
158 {
159 close(pin[0]);
160 close(pin[1]);
161 }
162
163 if (fp_out)
164 {
165 close(pout[0]);
166 close(pout[1]);
167 }
168
169 if (fp_err)
170 {
171 close(perr[0]);
172 close(perr[1]);
173 }
174
175 return -1;
176 }
177
178 if (fp_out)
179 {
180 close(pout[1]);
181 *fp_out = fdopen(pout[0], "r");
182 }
183
184 if (fp_in)
185 {
186 close(pin[0]);
187 *fp_in = fdopen(pin[1], "w");
188 }
189
190 if (fp_err)
191 {
192 close(perr[1]);
193 *fp_err = fdopen(perr[0], "r");
194 }
195
196 return pid;
197}
#define EXEC_SHELL
Definition: filter.h:28
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:199
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:223
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ filter_wait()

int filter_wait ( pid_t  pid)

Wait for the exit of a process and return its status.

Parameters
pidProcess id of the process to wait for
Return values
numExit status of the process identified by pid
-1Error

Definition at line 219 of file filter.c.

220{
221 int rc = 0;
222
223 waitpid(pid, &rc, 0);
225 rc = WIFEXITED(rc) ? WEXITSTATUS(rc) : -1;
226
227 return rc;
228}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: