NeoMutt  2019-12-07
Teaching an old dog new tricks
DOXYGEN
filter.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
34 #include "mutt/mutt.h"
35 #include "mutt.h"
36 #include "filter.h"
37 #ifdef USE_IMAP
38 #include "imap/imap.h"
39 #endif
40 
64 pid_t mutt_create_filter_fd(const char *cmd, FILE **fp_in, FILE **fp_out,
65  FILE **fp_err, int fdin, int fdout, int fderr)
66 {
67  int pin[2], pout[2], perr[2], pid;
68 
69  if (fp_in)
70  {
71  *fp_in = NULL;
72  if (pipe(pin) == -1)
73  return -1;
74  }
75 
76  if (fp_out)
77  {
78  *fp_out = NULL;
79  if (pipe(pout) == -1)
80  {
81  if (fp_in)
82  {
83  close(pin[0]);
84  close(pin[1]);
85  }
86  return -1;
87  }
88  }
89 
90  if (fp_err)
91  {
92  *fp_err = NULL;
93  if (pipe(perr) == -1)
94  {
95  if (fp_in)
96  {
97  close(pin[0]);
98  close(pin[1]);
99  }
100  if (fp_out)
101  {
102  close(pout[0]);
103  close(pout[1]);
104  }
105  return -1;
106  }
107  }
108 
110 
111  pid = fork();
112  if (pid == 0)
113  {
115 
116  if (fp_in)
117  {
118  close(pin[1]);
119  dup2(pin[0], 0);
120  close(pin[0]);
121  }
122  else if (fdin != -1)
123  {
124  dup2(fdin, 0);
125  close(fdin);
126  }
127 
128  if (fp_out)
129  {
130  close(pout[0]);
131  dup2(pout[1], 1);
132  close(pout[1]);
133  }
134  else if (fdout != -1)
135  {
136  dup2(fdout, 1);
137  close(fdout);
138  }
139 
140  if (fp_err)
141  {
142  close(perr[0]);
143  dup2(perr[1], 2);
144  close(perr[1]);
145  }
146  else if (fderr != -1)
147  {
148  dup2(fderr, 2);
149  close(fderr);
150  }
151 
152  execle(EXEC_SHELL, "sh", "-c", cmd, NULL, mutt_envlist_getlist());
153  _exit(127);
154  }
155  else if (pid == -1)
156  {
158 
159  if (fp_in)
160  {
161  close(pin[0]);
162  close(pin[1]);
163  }
164 
165  if (fp_out)
166  {
167  close(pout[0]);
168  close(pout[1]);
169  }
170 
171  if (fp_err)
172  {
173  close(perr[0]);
174  close(perr[1]);
175  }
176 
177  return -1;
178  }
179 
180  if (fp_out)
181  {
182  close(pout[1]);
183  *fp_out = fdopen(pout[0], "r");
184  }
185 
186  if (fp_in)
187  {
188  close(pin[0]);
189  *fp_in = fdopen(pin[1], "w");
190  }
191 
192  if (fp_err)
193  {
194  close(perr[1]);
195  *fp_err = fdopen(perr[0], "r");
196  }
197 
198  return pid;
199 }
200 
209 pid_t mutt_create_filter(const char *s, FILE **fp_in, FILE **fp_out, FILE **fp_err)
210 {
211  return mutt_create_filter_fd(s, fp_in, fp_out, fp_err, -1, -1, -1);
212 }
213 
220 int mutt_wait_filter(pid_t pid)
221 {
222  int rc;
223 
224  waitpid(pid, &rc, 0);
226  rc = WIFEXITED(rc) ? WEXITSTATUS(rc) : -1;
227 
228  return rc;
229 }
230 
245 {
246  int rc;
247 
248 #ifdef USE_IMAP
249  rc = imap_wait_keepalive(pid);
250 #else
251  waitpid(pid, &rc, 0);
252 #endif
254  rc = WIFEXITED(rc) ? WEXITSTATUS(rc) : -1;
255 
256  return rc;
257 }
pid_t mutt_create_filter(const char *s, FILE **fp_in, FILE **fp_out, FILE **fp_err)
Set up filter program.
Definition: filter.c:209
void mutt_sig_block_system(void)
Block signals before calling exec()
Definition: signal.c:183
Pass files through external commands (filters)
void mutt_sig_unblock_system(bool restore)
Restore previously blocked signals.
Definition: signal.c:207
IMAP network mailbox.
#define EXEC_SHELL
Definition: mutt.h:124
pid_t mutt_create_filter_fd(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, int fdin, int fdout, int fderr)
Run a command on a pipe (optionally connect stdin/stdout)
Definition: filter.c:64
int mutt_wait_filter(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
int mutt_wait_interactive_filter(pid_t pid)
Wait after an interactive filter.
Definition: filter.c:244
Convenience wrapper for the library headers.
int imap_wait_keepalive(pid_t pid)
Wait for a process to change state.
Definition: util.c:1087
char ** mutt_envlist_getlist(void)
Get the private environment.
Definition: envlist.c:169