1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.41 2003/11/17 14:41:03 simonmar Exp $
4 -- Utils for the driver
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
11 getOptionsFromSource, softGetDirectoryContents,
12 createDirectoryHierarchy, doesDirNameExist, prefixUnderscore,
13 unknownFlagErr, unknownFlagsErr, missingArgErr,
14 later, handleDyn, handle,
15 split, add, addNoDups,
16 Suffix, splitFilename, getFileSuffix,
17 splitFilename3, remove_suffix, split_longest_prefix,
18 replaceFilenameSuffix, directoryOf, replaceFilenameDirectory,
19 remove_spaces, escapeSpaces,
22 #include "../includes/config.h"
23 #include "HsVersions.h"
27 import Config ( cLeadingUnderscore )
29 import EXCEPTION ( Exception(..), finally, throwDyn, catchDyn, throw )
30 import qualified EXCEPTION as Exception
32 import DATA_IOREF ( IORef, readIORef, writeIORef )
40 -----------------------------------------------------------------------------
41 -- Reading OPTIONS pragmas
44 :: String -- input file
45 -> IO [String] -- options, if any
46 getOptionsFromSource file
47 = do h <- openFile file ReadMode
48 catchJust ioErrors (look h `finally` hClose h)
49 (\e -> if isEOFError e then return [] else ioError e)
54 let l = remove_spaces l'
57 | prefixMatch "#" l -> look h
58 | prefixMatch "{-# LINE" l -> look h -- -}
59 | Just opts <- matchOptions l
61 return (words opts ++ rest)
62 | otherwise -> return []
65 | Just s1 <- maybePrefixMatch "{-#" s, -- -}
66 Just s2 <- maybePrefixMatch "OPTIONS" (remove_spaces s1),
67 Just s3 <- maybePrefixMatch "}-#" (reverse s2)
72 -----------------------------------------------------------------------------
73 -- A version of getDirectoryContents that is non-fatal if the
74 -- directory doesn't exist.
76 softGetDirectoryContents d
77 = IO.catch (getDirectoryContents d)
78 (\_ -> do hPutStrLn stderr
79 ("WARNING: error while reading directory " ++ d)
83 -----------------------------------------------------------------------------
84 -- Create a hierarchy of directories
86 createDirectoryHierarchy :: FilePath -> IO ()
87 createDirectoryHierarchy dir = do
88 b <- doesDirectoryExist dir
90 createDirectoryHierarchy (directoryOf dir)
93 -----------------------------------------------------------------------------
94 -- Verify that the 'dirname' portion of a FilePath exists.
96 doesDirNameExist :: FilePath -> IO Bool
97 doesDirNameExist fpath = doesDirectoryExist (directoryOf fpath)
99 -----------------------------------------------------------------------------
100 -- Prefixing underscore to linker-level names
101 prefixUnderscore :: String -> String
103 | cLeadingUnderscore == "YES" = ('_':)
106 -----------------------------------------------------------------------------
109 unknownFlagErr :: String -> a
110 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
112 unknownFlagsErr :: [String] -> a
113 unknownFlagsErr fs = throwDyn (UsageError ("unrecognised flags: " ++ unwords fs))
115 missingArgErr :: String -> a
116 missingArgErr f = throwDyn (UsageError ("missing argument for flag: " ++ f))
120 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
121 handleDyn = flip catchDyn
123 handle :: (Exception -> IO a) -> IO a -> IO a
124 #if __GLASGOW_HASKELL__ < 501
125 handle = flip Exception.catchAllIO
127 handle h f = f `Exception.catch` \e -> case e of
128 ExitException _ -> throw e
132 split :: Char -> String -> [String]
133 split c s = case rest of
135 _:rest -> chunk : split c rest
136 where (chunk, rest) = break (==c) s
138 add :: IORef [a] -> a -> IO ()
141 writeIORef var (x:xs)
143 addNoDups :: Eq a => IORef [a] -> a -> IO ()
146 unless (x `elem` xs) $ writeIORef var (x:xs)
148 ------------------------------------------------------
149 -- Filename manipulation
150 ------------------------------------------------------
154 splitFilename :: String -> (String,Suffix)
155 splitFilename f = split_longest_prefix f (=='.')
157 getFileSuffix :: String -> Suffix
158 getFileSuffix f = drop_longest_prefix f (=='.')
160 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy.ext")
161 splitFilenameDir :: String -> (String,String)
163 = let (dir, rest) = split_longest_prefix str isPathSeparator
164 real_dir | null dir = "."
168 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
169 splitFilename3 :: String -> (String,String,Suffix)
171 = let (dir, rest) = split_longest_prefix str isPathSeparator
172 (name, ext) = splitFilename rest
173 real_dir | null dir = "."
175 in (real_dir, name, ext)
177 remove_suffix :: Char -> String -> Suffix
179 | null pre = reverse suf
180 | otherwise = reverse pre
181 where (suf,pre) = break (==c) (reverse s)
183 drop_longest_prefix :: String -> (Char -> Bool) -> String
184 drop_longest_prefix s pred = reverse suf
185 where (suf,_pre) = break pred (reverse s)
187 take_longest_prefix :: String -> (Char -> Bool) -> String
188 take_longest_prefix s pred = reverse pre
189 where (_suf,pre) = break pred (reverse s)
191 -- split a string at the last character where 'pred' is True,
192 -- returning a pair of strings. The first component holds the string
193 -- up (but not including) the last character for which 'pred' returned
194 -- True, the second whatever comes after (but also not including the
197 -- If 'pred' returns False for all characters in the string, the original
198 -- string is returned in the second component (and the first one is just
200 split_longest_prefix :: String -> (Char -> Bool) -> (String,String)
201 split_longest_prefix s pred
203 [] -> ([], reverse suf)
204 (_:pre) -> (reverse pre, reverse suf)
205 where (suf,pre) = break pred (reverse s)
207 replaceFilenameSuffix :: FilePath -> Suffix -> FilePath
208 replaceFilenameSuffix s suf = remove_suffix '.' s ++ suf
210 -- directoryOf strips the filename off the input string, returning
212 directoryOf :: FilePath -> String
213 directoryOf = fst . splitFilenameDir
215 replaceFilenameDirectory :: FilePath -> String -> FilePath
216 replaceFilenameDirectory s dir
217 = dir ++ '/':drop_longest_prefix s isPathSeparator
219 remove_spaces :: String -> String
220 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
222 escapeSpaces :: String -> String
223 escapeSpaces = foldr (\c s -> if isSpace c then '\\':c:s else c:s) ""
225 isPathSeparator :: Char -> Bool
227 #ifdef mingw32_TARGET_OS
228 ch == '/' || ch == '\\'