1 -----------------------------------------------------------------------------
3 -- Module : System.Environment
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- Miscellaneous information about the system environment.
13 -----------------------------------------------------------------------------
15 module System.Environment
17 getArgs, -- :: IO [String]
18 getProgName, -- :: IO String
19 getEnv, -- :: String -> IO String
24 #ifdef __GLASGOW_HASKELL__
31 #ifdef __GLASGOW_HASKELL__
34 import Control.Exception ( bracket )
51 -- ---------------------------------------------------------------------------
52 -- getArgs, getProgName, getEnv
54 -- | Computation 'getArgs' returns a list of the program's command
55 -- line arguments (not including the program name).
57 #ifdef __GLASGOW_HASKELL__
58 getArgs :: IO [String]
61 alloca $ \ p_argv -> do
62 getProgArgv p_argc p_argv
63 p <- fromIntegral `liftM` peek p_argc
65 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
68 foreign import ccall unsafe "getProgArgv"
69 getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
72 Computation 'getProgName' returns the name of the program as it was
75 However, this is hard-to-impossible to implement on some non-Unix
76 OSes, so instead, for maximum portability, we just return the leafname
77 of the program as invoked. Even then there are some differences
78 between platforms: on Windows, for example, a program invoked as foo
79 is probably really @FOO.EXE@, and that is what 'getProgName' will return.
81 getProgName :: IO String
84 alloca $ \ p_argv -> do
85 getProgArgv p_argc p_argv
89 unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
90 unpackProgName argv = do
91 s <- peekElemOff argv 0 >>= peekCString
94 basename :: String -> String
99 | isPathSeparator x = go xs xs
100 | otherwise = go acc xs
102 isPathSeparator :: Char -> Bool
103 isPathSeparator '/' = True
104 #ifdef mingw32_HOST_OS
105 isPathSeparator '\\' = True
107 isPathSeparator _ = False
110 -- | Computation 'getEnv' @var@ returns the value
111 -- of the environment variable @var@.
113 -- This computation may fail with:
115 -- * 'System.IO.Error.isDoesNotExistError' if the environment variable
118 getEnv :: String -> IO String
120 withCString name $ \s -> do
121 litstring <- c_getenv s
122 if litstring /= nullPtr
123 then peekCString litstring
124 else ioException (IOError Nothing NoSuchThing "getEnv"
125 "no environment variable" (Just name))
127 foreign import ccall unsafe "getenv"
128 c_getenv :: CString -> IO (Ptr CChar)
131 'withArgs' @args act@ - while executing action @act@, have 'getArgs'
134 withArgs :: [String] -> IO a -> IO a
136 p <- System.Environment.getProgName
140 'withProgName' @name act@ - while executing action @act@,
141 have 'getProgName' return @name@.
143 withProgName :: String -> IO a -> IO a
144 withProgName nm act = do
145 xs <- System.Environment.getArgs
148 -- Worker routine which marshals and replaces an argv vector for
149 -- the duration of an action.
151 withArgv :: [String] -> IO a -> IO a
152 withArgv new_args act = do
153 pName <- System.Environment.getProgName
154 existing_args <- System.Environment.getArgs
155 bracket (setArgs new_args)
156 (\argv -> do setArgs (pName:existing_args); freeArgv argv)
159 freeArgv :: Ptr CString -> IO ()
161 size <- lengthArray0 nullPtr argv
162 sequence_ [peek (argv `advancePtr` i) >>= free | i <- [size, size-1 .. 0]]
165 setArgs :: [String] -> IO (Ptr CString)
167 vs <- mapM newCString argv >>= newArray0 nullPtr
168 setArgsPrim (length argv) vs
171 foreign import ccall unsafe "setProgArgv"
172 setArgsPrim :: Int -> Ptr CString -> IO ()
174 -- |'getEnvironment' retrieves the entire environment as a
175 -- list of @(key,value)@ pairs.
177 -- If an environment entry does not contain an @\'=\'@ character,
178 -- the @key@ is the whole entry and the @value@ is the empty string.
180 getEnvironment :: IO [(String, String)]
182 pBlock <- getEnvBlock
183 if pBlock == nullPtr then return []
185 stuff <- peekArray0 nullPtr pBlock >>= mapM peekCString
186 return (map divvy stuff)
189 case break (=='=') str of
190 (xs,[]) -> (xs,[]) -- don't barf (like Posix.getEnvironment)
191 (name,_:value) -> (name,value)
193 foreign import ccall unsafe "__hscore_environ"
194 getEnvBlock :: IO (Ptr CString)
195 #endif /* __GLASGOW_HASKELL__ */