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.
10 #include <lib-lib/lib-lib.h>
11 #include <lib-ui/curses.h>
13 #include "mutt_signal.h"
16 /* Invokes a commmand on a pipe and optionally connects its stdin and stdout
17 * to the specified handles.
20 mutt_create_filter_fd (const char *cmd, FILE ** in, FILE ** out, FILE ** err,
21 int fdin, int fdout, int fderr)
23 int pin[2], pout[2], perr[2], thepid;
33 if (pipe (pout) == -1) {
44 if (pipe (perr) == -1) {
57 mutt_block_signals_system ();
59 if ((thepid = fork ()) == 0) {
60 mutt_unblock_signals_system (0);
67 else if (fdin != -1) {
77 else if (fdout != -1) {
87 else if (fderr != -1) {
92 execl ("/bin/sh", "sh", "-c", cmd, NULL);
95 else if (thepid == -1) {
96 mutt_unblock_signals_system (1);
118 *out = fdopen (pout[0], "r");
123 *in = fdopen (pin[1], "w");
128 *err = fdopen (perr[0], "r");
134 pid_t mutt_create_filter (const char *s, FILE ** in, FILE ** out, FILE ** err)
136 return (mutt_create_filter_fd (s, in, out, err, -1, -1, -1));
139 int mutt_wait_filter (pid_t pid)
143 waitpid (pid, &rc, 0);
144 mutt_unblock_signals_system (1);
145 rc = WIFEXITED (rc) ? WEXITSTATUS (rc) : -1;
150 /* This function allows the user to specify a command to read stdout from in
151 place of a normal file. If the last character in the string is a pipe (|),
152 then we assume it is a commmand to run instead of a normal file. */
153 FILE *mutt_open_read (const char *path, pid_t * thepid)
155 int len = m_strlen(path);
158 if (path[len - 1] == '|') {
159 char *s = m_strdup(path);
161 /* read from a pipe */
165 *thepid = mutt_create_filter (s, NULL, &f, NULL);
168 f = fopen (path, "r");