2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins.
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
19 #include <lib-lib/lib-lib.h>
21 #include "mutt_signal.h"
24 /* Invokes a commmand on a pipe and optionally connects its stdin and stdout
25 * to the specified handles.
28 mutt_create_filter_fd (const char *cmd, FILE ** in, FILE ** out, FILE ** err,
29 int fdin, int fdout, int fderr)
31 int pin[2], pout[2], perr[2], thepid;
41 if (pipe (pout) == -1) {
52 if (pipe (perr) == -1) {
65 mutt_block_signals_system ();
67 if ((thepid = fork ()) == 0) {
68 mutt_unblock_signals_system (0);
75 else if (fdin != -1) {
85 else if (fdout != -1) {
95 else if (fderr != -1) {
100 execl ("/bin/sh", "sh", "-c", cmd, NULL);
103 else if (thepid == -1) {
104 mutt_unblock_signals_system (1);
126 *out = fdopen (pout[0], "r");
131 *in = fdopen (pin[1], "w");
136 *err = fdopen (perr[0], "r");
142 pid_t mutt_create_filter (const char *s, FILE ** in, FILE ** out, FILE ** err)
144 return (mutt_create_filter_fd (s, in, out, err, -1, -1, -1));
147 int mutt_wait_filter (pid_t pid)
151 waitpid (pid, &rc, 0);
152 mutt_unblock_signals_system (1);
153 rc = WIFEXITED (rc) ? WEXITSTATUS (rc) : -1;
158 /* This function allows the user to specify a command to read stdout from in
159 place of a normal file. If the last character in the string is a pipe (|),
160 then we assume it is a commmand to run instead of a normal file. */
161 FILE *mutt_open_read (const char *path, pid_t * thepid)
163 int len = m_strlen(path);
166 if (path[len - 1] == '|') {
167 char *s = m_strdup(path);
169 /* read from a pipe */
173 *thepid = mutt_create_filter (s, NULL, &f, NULL);
176 f = fopen (path, "r");