[project @ 1998-02-02 17:27:26 by simonm]
[ghc-hetmet.git] / ghc / lib / posix / Posix.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1996
3 %
4 \section[Posix]{Haskell 1.3 POSIX bindings}
5
6 \begin{code}
7 module Posix  (
8     module PosixDB,
9     module PosixErr,
10     module PosixFiles,
11     module PosixIO,
12     module PosixProcEnv,
13     module PosixProcPrim,
14     module PosixTTY,
15
16     runProcess,
17
18     ByteCount,
19     Fd, intToFd,
20     ClockTick,
21     EpochTime,
22     FileOffset,
23     GroupID,
24     Limit,
25     LinkCount,
26     ProcessID,
27     ProcessGroupID,
28     UserID,
29     
30     ExitCode
31
32     )  where
33
34 import PrelBase
35 import PrelIOBase
36 import IO
37 import PrelHandle
38
39 import PosixDB
40 import PosixErr
41 import PosixFiles
42 import PosixIO
43 import PosixProcEnv
44 import PosixProcPrim
45 import PosixTTY
46 import PosixUtil
47
48 -- [OLD COMMENT:]
49 -- runProcess is our candidate for the high-level OS-independent primitive 
50 -- If accepted, it will be moved out of Posix into LibSystem.
51
52 import Directory        ( setCurrentDirectory )
53
54
55 runProcess :: FilePath                      -- Command
56            -> [String]                      -- Arguments
57            -> Maybe [(String, String)]      -- Environment
58            -> Maybe FilePath                -- Working directory    
59            -> Maybe Handle                  -- stdin
60            -> Maybe Handle                  -- stdout
61            -> Maybe Handle                  -- stderr
62            -> IO ()
63 runProcess path args env dir stdin stdout stderr = 
64     forkProcess >>= \ pid ->
65     case pid of
66       Nothing -> doTheBusiness
67       Just x  -> return ()
68   where
69     doTheBusiness :: IO ()
70     doTheBusiness =
71         maybeChangeWorkingDirectory     >>
72         maybeDup2 0 stdin               >>
73         maybeDup2 1 stdout              >>
74         maybeDup2 2 stderr              >>
75         executeFile path True args env  >>
76         syserr "runProcess"
77
78     maybeChangeWorkingDirectory :: IO ()
79     maybeChangeWorkingDirectory =
80         case dir of
81           Nothing -> return ()
82           Just x  -> setCurrentDirectory x
83
84     maybeDup2 :: Int -> Maybe Handle -> IO ()
85     maybeDup2 dest h =
86         case h of Nothing -> return ()
87                   Just x  -> handleToFd x             >>= \ src ->
88                              dupTo src (intToFd dest) >>
89                              return ()
90
91 \end{code}