0
votes

Is there a Haskell conduit that can execute a process and capture both its stderr and stdout streams (separately)? The ability to pass in a stdin to the process would be perfect as a conduit too would be perfect, but not a requirement (I can use a file for that).

1

1 Answers

4
votes

Here is an example derived from the School of Haskell article Data.Conduit.Process:

{-# LANGUAGE OverloadedStrings #-}
import           Control.Applicative      ((*>))
import           Control.Concurrent.Async (Concurrently (..))
import           Data.Conduit             (await, yield, ($$), (=$))
import qualified Data.Conduit.Binary      as CB
import qualified Data.Conduit.List        as CL
import           Data.Conduit.Process     (ClosedStream (..), streamingProcess,
                                           proc, waitForStreamingProcess)
import           System.IO                (stdin)

main :: IO ()
main = do
    putStrLn "Enter lines of data. I'll run ./base64-perl on it."
    putStrLn "Enter \"quit\" to exit."

    ((toProcess, close), fromProcess, fromStderr, cph) <-
        streamingProcess (proc "./base64-perl" [])

    let input = CB.sourceHandle stdin
             $$ CB.lines
             =$ inputLoop
             =$ toProcess

        inputLoop = do
            mbs <- await
            case mbs of
                Nothing -> close
                Just "quit" -> close
                Just bs -> do
                    yield bs
                    inputLoop

        output = fromProcess $$ CL.mapM_
            (\bs -> putStrLn $ "from process: " ++ show bs)

        errout = fromStderr $$ CL.mapM_
            (\bs -> putStrLn $ "from stderr: " ++ show bs)

    ec <- runConcurrently $
        Concurrently input *>
        Concurrently output *>
        Concurrently errout *>
        Concurrently (waitForStreamingProcess cph)

    putStrLn $ "Process exit code: " ++ show ec

It's basically the example in the article with a thread for processing stderr added.

It calls this perl program which emits output to both stdout and stderr:

#!/usr/bin/env perl

use strict;
use warnings;
use MIME::Base64;

$| = 1;

my $timeout = 3;
my $buf = "";
while (1) {
  my $rin = '';
  vec($rin, fileno(STDIN), 1) = 1;
  my ($nfound) = select($rin, undef, undef, $timeout);
  if ($nfound) {
    my $nread = sysread(STDIN, $buf, 4096, length($buf));
    last if $nread <= 0;
    print encode_base64($buf);
    $buf = "";
  } else {
    print STDERR "this is from stderr\n";
  }
}