module Posixutil.SafeSystem(
safeSystemGeneral,
safeSystem,
) where
import System.Exit
import Util.WBFiles
import Util.FileNames
import Util.Computation
import Posixutil.ChildProcess
safeSystem :: String -> IO ExitCode
safeSystem command =
let
outputSink "" = done
outputSink str = putStrLn ("SafeSystem output: "++str)
in
safeSystemGeneral command outputSink
safeSystemGeneral :: String -> (String -> IO ()) -> IO ExitCode
safeSystemGeneral command outputSink =
do
top <- getTOP
let
fullName = (trimDir top) `combineNames`
("posixutil" `combineNames` "runCommand")
childProcess <- newChildProcess fullName [
linemode True,
standarderrors True
]
sendMsg childProcess (command++"\n")
let
readOutput =
do
let
notExit str =
do
outputSink str
readOutput
nextLine <- readMsg childProcess
case nextLine of
'E':'X':'I':'T':'C':'O':'D':'E':' ':numberStr ->
case readsPrec 0 numberStr of
[(0,"")] -> return ExitSuccess
[(n,"")] -> return (ExitFailure n)
_ -> notExit nextLine
_ -> notExit nextLine
readOutput