
Hi, assume I have a program taking input from stdin. How do I call it from Haskell while feeding to it a string as input. That is, I want a function like system' :: String -> String -> IO ExitCode such that system' cmd inp would be equivalent to first writing inp to a file, say "temp", and then calling system (cmd ++ " < temp") Of course, without actually creating that temporary file and having to clean it up afterwards. Does such a system' exist? Thanks, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

On Tue, Sep 23, 2008 at 01:37:56PM +0200, Janis Voigtlaender wrote:
Hi,
assume I have a program taking input from stdin. How do I call it from Haskell while feeding to it a string as input.
Sure, have a look at http://hackage.haskell.org/packages/archive/process/1.0.0.0/doc/html/System-... rnuInteractiveCommand does would you want (in,out,err,pId) <- runInteractiveProcess "cat" ... hPutStrLn "test" in hClose in outContents <- hGetContents out print outContents or such Marc

Marc Weber wrote:
On Tue, Sep 23, 2008 at 01:37:56PM +0200, Janis Voigtlaender wrote:
Hi,
assume I have a program taking input from stdin. How do I call it from Haskell while feeding to it a string as input.
Sure, have a look at http://hackage.haskell.org/packages/archive/process/1.0.0.0/doc/html/System-...
Ah, great. Just what I need! -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

Marc Weber wrote:
On Tue, Sep 23, 2008 at 01:37:56PM +0200, Janis Voigtlaender wrote:
Hi,
assume I have a program taking input from stdin. How do I call it from Haskell while feeding to it a string as input.
Sure, have a look at http://hackage.haskell.org/packages/archive/process/1.0.0.0/doc/html/System-...
rnuInteractiveCommand does would you want (in,out,err,pId) <- runInteractiveProcess "cat" ... hPutStrLn "test" in hClose in outContents <- hGetContents out print outContents
Beware. The above code is broken. Whether it appears to work depends on your OS, buffering settings, and the size of any underlying buffers. The simplest safe way to do this is to fork a separate thread for one side or the other of the handle. Jules

Jules Bean wrote:
Marc Weber wrote:
On Tue, Sep 23, 2008 at 01:37:56PM +0200, Janis Voigtlaender wrote:
Hi,
assume I have a program taking input from stdin. How do I call it from Haskell while feeding to it a string as input.
Sure, have a look at http://hackage.haskell.org/packages/archive/process/1.0.0.0/doc/html/System-...
rnuInteractiveCommand does would you want (in,out,err,pId) <- runInteractiveProcess "cat" ... hPutStrLn "test" in hClose in outContents <- hGetContents out print outContents
Beware. The above code is broken.
Whether it appears to work depends on your OS, buffering settings, and the size of any underlying buffers.
Thanks for the warning. I needed it only for a small scripting task, and it seems to work well enough. Actually, I am not even interested in the output, so I just went for waitForProcess. This comes with additional warnings in the docs, but I did not experience any problems in my concrete setting so far. Ciao, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de

Janis Voigtlaender wrote:
Jules Bean wrote:
Marc Weber wrote:
On Tue, Sep 23, 2008 at 01:37:56PM +0200, Janis Voigtlaender wrote:
Hi,
assume I have a program taking input from stdin. How do I call it from Haskell while feeding to it a string as input.
Sure, have a look at http://hackage.haskell.org/packages/archive/process/1.0.0.0/doc/html/System-...
rnuInteractiveCommand does would you want (in,out,err,pId) <- runInteractiveProcess "cat" ... hPutStrLn "test" in hClose in outContents <- hGetContents out print outContents
Beware. The above code is broken.
Whether it appears to work depends on your OS, buffering settings, and the size of any underlying buffers.
Thanks for the warning. I needed it only for a small scripting task, and it seems to work well enough. Actually, I am not even interested in the output, so I just went for waitForProcess. This comes with additional warnings in the docs, but I did not experience any problems in my concrete setting so far.
If you don't care about the output, it's fine if you send the output to /dev/null. The deadlock is when you try to read the output and send the input from the same thread. However if the output is to "out" and you never read it then that *will* deadlock, but probably only when some OS buffer gets full (e.g. after 8k of output) Jules

On 2008 Sep 23, at 7:55, Marc Weber wrote:
On Tue, Sep 23, 2008 at 01:37:56PM +0200, Janis Voigtlaender wrote:
assume I have a program taking input from stdin. How do I call it from Haskell while feeding to it a string as input.
Sure, have a look at http://hackage.haskell.org/packages/archive/process/1.0.0.0/doc/html/System-... :runInteractiveProcess
rnuInteractiveCommand does would you want (in,out,err,pId) <- runInteractiveProcess "cat" ... hPutStrLn "test" in hClose in outContents <- hGetContents out print outContents
Note that this will deadlock if either the input or the output is too long; you should forkIO a thread to do either the input or the output. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Em Ter, 2008-09-23 às 13:37 +0200, Janis Voigtlaender escreveu: (...)
That is, I want a function like
system' :: String -> String -> IO ExitCode
such that
system' cmd inp
would be equivalent to first writing inp to a file, say "temp", and then calling
system (cmd ++ " < temp") (...) Does such a system' exist?
I think you should take a look at HSH[0]. Greetings. 0: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HSH -- marcot Página: http://marcotmarcot.iaaeee.org/ Blog: http://marcotmarcot.blogspot.com/ Correio: marcot@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil
participants (5)
-
Brandon S. Allbery KF8NH
-
Janis Voigtlaender
-
Jules Bean
-
Marc Weber
-
Marco Túlio Gontijo e Silva