forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
233 lines (208 loc) · 5.05 KB
/
filter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* @file
* Pass files through external commands (filters)
*
* @authors
* Copyright (C) 1996-2000 Michael R. Elkins.
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page filter Pass files through external commands (filters)
*
* Pass files through external commands (filters)
*/
#include "config.h"
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "mutt.h"
#include "filter.h"
#include "mutt_window.h"
/**
* mutt_create_filter_fd - Run a command on a pipe (optionally connect stdin/stdout)
* @param cmd Command line to invoke using `sh -c`
* @param in File stream pointing to stdin for the command process, can be NULL
* @param out File stream pointing to stdout for the command process, can be NULL
* @param err File stream pointing to stderr for the command process, can be NULL
* @param fdin If `in` is NULL and fdin is not -1 then fdin will be used as stdin for the command process
* @param fdout If `out` is NULL and fdout is not -1 then fdout will be used as stdout for the command process
* @param fderr If `error` is NULL and fderr is not -1 then fderr will be used as stderr for the command process
* @retval num PID of the created process
* @retval -1 Error 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.
*
* @code{.c}
* mutt_create_filter_fd(commandline, NULL, NULL, NULL, -1, -1, -1);
* @endcode
*
* Additionally, in, out, and err will point to FILE* streams representing the
* processes stdin, stdout, and stderr.
*/
pid_t mutt_create_filter_fd(const char *cmd, FILE **in, FILE **out, FILE **err,
int fdin, int fdout, int fderr)
{
int pin[2], pout[2], perr[2], thepid;
if (in)
{
*in = 0;
if (pipe(pin) == -1)
return -1;
}
if (out)
{
*out = 0;
if (pipe(pout) == -1)
{
if (in)
{
close(pin[0]);
close(pin[1]);
}
return -1;
}
}
if (err)
{
*err = 0;
if (pipe(perr) == -1)
{
if (in)
{
close(pin[0]);
close(pin[1]);
}
if (out)
{
close(pout[0]);
close(pout[1]);
}
return -1;
}
}
mutt_sig_block_system();
thepid = fork();
if (thepid == 0)
{
mutt_sig_unblock_system(false);
if (in)
{
close(pin[1]);
dup2(pin[0], 0);
close(pin[0]);
}
else if (fdin != -1)
{
dup2(fdin, 0);
close(fdin);
}
if (out)
{
close(pout[0]);
dup2(pout[1], 1);
close(pout[1]);
}
else if (fdout != -1)
{
dup2(fdout, 1);
close(fdout);
}
if (err)
{
close(perr[0]);
dup2(perr[1], 2);
close(perr[1]);
}
else if (fderr != -1)
{
dup2(fderr, 2);
close(fderr);
}
if (MuttIndexWindow && (MuttIndexWindow->cols > 0))
{
char columns[11];
snprintf(columns, sizeof(columns), "%d", MuttIndexWindow->cols);
mutt_envlist_set("COLUMNS", columns, 1);
}
execle(EXECSHELL, "sh", "-c", cmd, NULL, mutt_envlist_getlist());
_exit(127);
}
else if (thepid == -1)
{
mutt_sig_unblock_system(true);
if (in)
{
close(pin[0]);
close(pin[1]);
}
if (out)
{
close(pout[0]);
close(pout[1]);
}
if (err)
{
close(perr[0]);
close(perr[1]);
}
return -1;
}
if (out)
{
close(pout[1]);
*out = fdopen(pout[0], "r");
}
if (in)
{
close(pin[0]);
*in = fdopen(pin[1], "w");
}
if (err)
{
close(perr[1]);
*err = fdopen(perr[0], "r");
}
return thepid;
}
/**
* mutt_create_filter - Set up filter program
* @param[in] s Command string
* @param[out] in FILE pointer of stdin
* @param[out] out FILE pointer of stdout
* @param[out] err FILE pointer of stderr
* @retval num PID of filter
*/
pid_t mutt_create_filter(const char *s, FILE **in, FILE **out, FILE **err)
{
return mutt_create_filter_fd(s, in, out, err, -1, -1, -1);
}
/**
* mutt_wait_filter - Wait for the exit of a process and return its status
* @param pid Process id of the process to wait for
* @retval num Exit status of the process identified by pid
* @retval -1 Error
*/
int mutt_wait_filter(pid_t pid)
{
int rc;
waitpid(pid, &rc, 0);
mutt_sig_unblock_system(true);
rc = WIFEXITED(rc) ? WEXITSTATUS(rc) : -1;
return rc;
}