summaryrefslogtreecommitdiff
path: root/src/compiler/GF/System/Process.hs
diff options
context:
space:
mode:
authorhallgren <hallgren@chalmers.se>2013-11-19 15:18:58 +0000
committerhallgren <hallgren@chalmers.se>2013-11-19 15:18:58 +0000
commitddac5f9e5aa935f4c154253831a36e49a48cdc8d (patch)
treea58846318676f398077d4779fd15d3a589eed2ec /src/compiler/GF/System/Process.hs
parent5b83da558327b53e857ba88f91cba76821ecf763 (diff)
GF shell: improved system_pipe (aka "?") command
1. No temporary files are created. 2. The output of a system command is read lazily, making it feasible to process large or even infinite output, e.g. the following works as expected: ? "yes" | ? "head -5" | ps -lextext
Diffstat (limited to 'src/compiler/GF/System/Process.hs')
-rw-r--r--src/compiler/GF/System/Process.hs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/compiler/GF/System/Process.hs b/src/compiler/GF/System/Process.hs
new file mode 100644
index 000000000..415f8a50a
--- /dev/null
+++ b/src/compiler/GF/System/Process.hs
@@ -0,0 +1,18 @@
+module GF.System.Process where
+import System.Process
+import System.IO(hGetContents,hClose,hPutStr)
+import Control.Concurrent(forkIO)
+import GF.System.Catch(try)
+
+-- | Feed some input to a shell process and read the output lazily
+readShellProcess :: String -- ^ shell command
+ -> String -- ^ input to shell command
+ -> IO String -- ^ output from shell command
+readShellProcess cmd input =
+ do (Just stdin,Just stdout,Nothing,ph) <-
+ createProcess (shell cmd){std_in=CreatePipe,std_out=CreatePipe}
+ forkIO $ do try $ hPutStr stdin input
+ try $ hClose stdin
+ waitForProcess ph
+ return ()
+ hGetContents stdout