
I am trying to implement a function which takes a string, pipes it through an external command and returns the stdout of teh command utilizing the Posix library and ghc. first of all, if someone has this handy code tidbit already written then feel free to send it to me. the other thing is that I cannot figure out how to get data into and out of the process. I can create a pipe which returns a pair of Fd's but then i cannot pass these into runProcess since it expects handles. also I have no ability to wait for the termination of the command that runProcess starts... it is always possible i am just overlooking a few key functions in Posix... John -- --------------------------------------------------------------------------- John Meacham - California Institute of Technology, Alum. - john@repetae.net ---------------------------------------------------------------------------

Hi, The POSIX functions you may want to try are open2 and open3. Much trouble come from the Unix buffering : as you can't force autoflushed output from the command, you'll simply have a deadlock 99% of the time. A solution (used in scsh, I think) is to send the stdout in a file and then to read the file. HTH jb John Meacham wrote:
I am trying to implement a function which takes a string, pipes it through an external command and returns the stdout of teh command utilizing the Posix library and ghc.
first of all, if someone has this handy code tidbit already written then feel free to send it to me. the other thing is that I cannot figure out how to get data into and out of the process. I can create a pipe which returns a pair of Fd's but then i cannot pass these into runProcess since it expects handles. also I have no ability to wait for the termination of the command that runProcess starts...
it is always possible i am just overlooking a few key functions in Posix...
John -- --------------------------------------------------------------------------- John Meacham - California Institute of Technology, Alum. - john@repetae.net ---------------------------------------------------------------------------
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
--
Jean-Baptiste Robertson

Hi, On Mon, 16 Jul 2001, John Meacham wrote:
I am trying to implement a function which takes a string, pipes it through an external command and returns the stdout of teh command utilizing the Posix library and ghc.
first of all, if someone has this handy code tidbit already written then feel free to send it to me. the other thing is that I cannot figure out how to get data into and out of the process. I can create a pipe which returns a pair of Fd's but then i cannot pass these into runProcess since it expects handles. also I have no ability to wait for the termination of the command that runProcess starts...
Use forkProcess/executeFile instead. Below is some code I hacked together a while back to execute shell commands and read the output (lazily or strictly). It could easily be adapted to write something to the child (use fdWrite in the parent branch to write to writep; dup readp into stdInput in the child branch), but there is a risk of deadlock. module Main where { import System; import IOExts; import Posix; -- "shell' lazy command" executes the specified command in a child process -- (by specifying it as an argument to "/bin/sh -c") and returns the data -- written to standard output in a string. If "lazy" is True, the data is -- read lazily, i.e., on demand; the child will block if the pipe is full. -- Otherwise, the data is read at once. shell' :: Bool -> String -> IO String; shell' lazy command = do { -- Create a pipe. (readp, writep) <- createPipe; -- Fork. child <- forkProcess; case child of { -- The parent goes here. Just pid -> do { fdClose writep; readFD lazy readp; }; -- The child goes here. Nothing -> do { fdClose readp; dupTo writep stdOutput; catch (executeFile "/bin/sh" False [ "-c", command ] Nothing) (\_ -> exitImmediately (ExitFailure 1)); return undefined; }; }; }; -- Read this many bytes at a time. granularity = 1024 :: Int; readFD :: Bool -> Fd -> IO String; readFD lazy fd = (if lazy then unsafeInterleaveIO else id) $ do { (s, bytes) <- catch (fdRead fd granularity) (\_ -> return ("", 0)) ; if bytes > 0 then do { t <- readFD lazy fd; return $ s ++ t; } else do { fdClose fd; return s; } }; shell = shell' False; shellLazy = shell' True; main = do { s <- shellLazy "find /"; putStr $ unlines $ take 10 $ lines s; }; } Regards, Eelco.

John Meacham
I am trying to implement a function which takes a string, pipes it through an external command and returns the stdout of teh command utilizing the Posix library and ghc.
first of all, if someone has this handy code tidbit already written then feel free to send it to me. the other thing is that I cannot figure out how to get data into and out of the process. I can create a pipe which returns a pair of Fd's but then i cannot pass these into runProcess since it expects handles. also I have no ability to wait for the termination of the command that runProcess starts...
I have all this stuff ready to use in a library in my CVS repository (but it is not very heavily tested yet). I plan to release it soon, but just returned from a trip and so may take a couple of more days. Is it very urgent for you? Manuel

It is not time-critical, but it would be nice to know how to do it properly :). is this a part of the whole IO system rewrite? also, is withStdin broken? every time i try to call it the program aborts with an hFlush - cannot flush, Handle is not writeable error. John On Tue, Jul 17, 2001 at 10:39:08PM +1000, Manuel M. T. Chakravarty wrote:
John Meacham
wrote, I am trying to implement a function which takes a string, pipes it through an external command and returns the stdout of teh command utilizing the Posix library and ghc.
first of all, if someone has this handy code tidbit already written then feel free to send it to me. the other thing is that I cannot figure out how to get data into and out of the process. I can create a pipe which returns a pair of Fd's but then i cannot pass these into runProcess since it expects handles. also I have no ability to wait for the termination of the command that runProcess starts...
I have all this stuff ready to use in a library in my CVS repository (but it is not very heavily tested yet). I plan to release it soon, but just returned from a trip and so may take a couple of more days. Is it very urgent for you?
Manuel
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- --------------------------------------------------------------------------- John Meacham - California Institute of Technology, Alum. - john@repetae.net ---------------------------------------------------------------------------

John Meacham
It is not time-critical, but it would be nice to know how to do it properly :). is this a part of the whole IO system rewrite? also, is withStdin broken? every time i try to call it the program aborts with an hFlush - cannot flush, Handle is not writeable error.
No, it doesn't have anything to do with the IO rewrite, but is a separate library. Manuel
participants (4)
-
Eelco Dolstra
-
Jean-Baptiste Robertson
-
John Meacham
-
Manuel M. T. Chakravarty