-
-
-static RETSIGTYPE
-pipeser(int i)
-{
-#ifndef RELIABLE_SIGNALS
- SIGNAL(SIGPIPE, pipeser);
-#endif
-
- done(NOTOK);
-}
-
-
-static RETSIGTYPE
-quitser(int i)
-{
-#ifndef RELIABLE_SIGNALS
- SIGNAL(SIGQUIT, quitser);
-#endif
-
- putchar('\n');
- fflush(stdout);
- done(NOTOK);
-}
-
-
-#undef adios
-#undef done
-
-static void
-mhladios(char *what, char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt);
- advertise(what, NULL, fmt, ap);
- va_end(ap);
- mhldone(1);
-}
-
-
-static void
-mhldone(int status)
-{
- exitstat = status;
- if (mhl_action)
- longjmp(mhlenv, DONE);
- else
- done(exitstat);
-}
-
-
-static int m_pid = NOTOK;
-static int sd = NOTOK;
-
-static void
-m_popen(char *name)
-{
- int pd[2];
-
- if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
- adios("standard output", "unable to dup()");
-
- if (pipe(pd) == NOTOK)
- adios("pipe", "unable to");
-
- switch (m_pid = fork()) {
- case NOTOK:
- adios("fork", "unable to");
-
- case OK:
- SIGNAL(SIGINT, SIG_DFL);
- SIGNAL(SIGQUIT, SIG_DFL);
-
- close(pd[1]);
- if (pd[0] != fileno(stdin)) {
- dup2(pd[0], fileno(stdin));
- close(pd[0]);
- }
- execlp(name, mhbasename(name), NULL);
- fprintf(stderr, "unable to exec ");
- perror(name);
- _exit(-1);
-
- default:
- close(pd[0]);
- if (pd[1] != fileno(stdout)) {
- dup2(pd[1], fileno(stdout));
- close(pd[1]);
- }
- }
-}
-
-
-void
-m_pclose(void)
-{
- if (m_pid == NOTOK)
- return;
-
- if (sd != NOTOK) {
- fflush(stdout);
- if (dup2(sd, fileno(stdout)) == NOTOK)
- adios("standard output", "unable to dup2()");
-
- clearerr(stdout);
- close(sd);
- sd = NOTOK;
- }
- else
- fclose(stdout);
-
- pidwait(m_pid, OK);
- m_pid = NOTOK;
-}