/
bifurcate.hs
56 lines (50 loc) · 1.49 KB
/
bifurcate.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import System.Environment
import System.Process
import System.IO
process :: String -> String -> IO Handle
process "a" fname = do
x <- openFile fname AppendMode
return x
process "w" fname = do
x <- openFile fname WriteMode
return x
process "c" cmd = do
x <- createProcess (shell cmd) {std_in = CreatePipe}
let (Just ihandle, _, _, _) = x in
return ihandle
printHelp = do
putStrLn "Usage: bifurcate type targets...\n\
\Types:\n\
\ -a Open the specified file(s) for appending.\n\
\ -w Open the specified file(s) for writing\n\
\ (WARNING: will overwrite file contents).\n\
\ -c Run the specified commands.\n\
\ -h Print this help message.\n\
\\n\
\Note: targets corresponds to possible inputs."
collect :: String -> [String] -> [IO Handle]
collect t [] = []
collect t (('-' : hd) : tl) = parseArgs (('-' : hd) : tl)
collect t (hd : tl) = ((process t hd) : (collect t tl))
parseArgs :: [String] -> [IO Handle]
parseArgs [] = []
parseArgs (('-': hd) : tl) = collect hd tl
multicast :: [IO Handle] -> String -> IO ()
multicast [] c = do
return ()
multicast (x : xs) c = do
hdl <- x
hPutStr hdl c
hFlush hdl
multicast xs c
bifurcate :: [String] -> String -> IO ()
bifurcate args c
| (any (\ x -> x == "-h" || x == "--help") args) = do
printHelp
return ()
| otherwise = multicast (parseArgs args) c
main :: IO ()
main = do
args <- getArgs
c <- getContents
bifurcate args c