1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.31 2002/02/27 16:24:00 simonmar Exp $
4 -- Utils for the driver
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
10 module DriverUtil where
12 #include "../includes/config.h"
13 #include "HsVersions.h"
17 import Config ( cLeadingUnderscore )
23 import Directory ( getDirectoryContents )
29 -----------------------------------------------------------------------------
30 -- Reading OPTIONS pragmas
33 :: String -- input file
34 -> IO [String] -- options, if any
35 getOptionsFromSource file
36 = do h <- openFile file ReadMode
37 catchJust ioErrors (look h `finally` hClose h)
38 (\e -> if isEOFError e then return [] else ioError e)
42 let l = remove_spaces l'
45 | prefixMatch "#" l -> look h
46 | prefixMatch "{-# LINE" l -> look h -- -}
47 | Just opts <- matchOptions l
49 return (words opts ++ rest)
50 | otherwise -> return []
53 | Just s1 <- my_prefix_match "{-#" s,
54 Just s2 <- my_prefix_match "OPTIONS" (remove_spaces s1),
55 Just s3 <- my_prefix_match "}-#" (reverse s2)
60 -----------------------------------------------------------------------------
61 -- A version of getDirectoryContents that is non-fatal if the
62 -- directory doesn't exist.
64 softGetDirectoryContents d
65 = IO.catch (getDirectoryContents d)
66 (\_ -> do hPutStrLn stderr
67 ("WARNING: error while reading directory " ++ d)
71 -----------------------------------------------------------------------------
72 -- Prefixing underscore to linker-level names
73 prefixUnderscore :: String -> String
75 | cLeadingUnderscore == "YES" = ('_':)
78 -----------------------------------------------------------------------------
81 unknownFlagErr :: String -> a
82 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
84 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
85 my_partition _ [] = ([],[])
87 = let (bs,cs) = my_partition p as in
90 Just b -> ((a,b):bs,cs)
92 my_prefix_match :: String -> String -> Maybe String
93 my_prefix_match [] rest = Just rest
94 my_prefix_match (_:_) [] = Nothing
95 my_prefix_match (p:pat) (r:rest)
96 | p == r = my_prefix_match pat rest
101 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
102 handleDyn = flip catchDyn
104 handle :: (Exception -> IO a) -> IO a -> IO a
105 #if __GLASGOW_HASKELL__ < 501
106 handle = flip Exception.catchAllIO
108 handle h f = f `Exception.catch` \e -> case e of
109 ExitException _ -> throw e
113 split :: Char -> String -> [String]
114 split c s = case rest of
116 _:rest -> chunk : split c rest
117 where (chunk, rest) = break (==c) s
119 add :: IORef [a] -> a -> IO ()
122 writeIORef var (x:xs)
124 addNoDups :: Eq a => IORef [a] -> a -> IO ()
127 unless (x `elem` xs) $ writeIORef var (x:xs)
129 ------------------------------------------------------
130 -- Filename manipulation
131 ------------------------------------------------------
135 splitFilename :: String -> (String,Suffix)
136 splitFilename f = split_longest_prefix f (=='.')
138 getFileSuffix :: String -> Suffix
139 getFileSuffix f = drop_longest_prefix f (=='.')
141 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
142 splitFilename3 :: String -> (String,String,Suffix)
144 = let (dir, rest) = split_longest_prefix str isPathSeparator
145 (name, ext) = splitFilename rest
146 real_dir | null dir = "."
148 in (real_dir, name, ext)
150 remove_suffix :: Char -> String -> Suffix
152 | null pre = reverse suf
153 | otherwise = reverse pre
154 where (suf,pre) = break (==c) (reverse s)
156 drop_longest_prefix :: String -> (Char -> Bool) -> String
157 drop_longest_prefix s pred = reverse suf
158 where (suf,_pre) = break pred (reverse s)
160 take_longest_prefix :: String -> (Char -> Bool) -> String
161 take_longest_prefix s pred = reverse pre
162 where (_suf,pre) = break pred (reverse s)
164 -- split a string at the last character where 'pred' is True,
165 -- returning a pair of strings. The first component holds the string
166 -- up (but not including) the last character for which 'pred' returned
167 -- True, the second whatever comes after (but also not including the
170 -- If 'pred' returns False for all characters in the string, the original
171 -- string is returned in the second component (and the first one is just
173 split_longest_prefix :: String -> (Char -> Bool) -> (String,String)
174 split_longest_prefix s pred
176 [] -> ([], reverse suf)
177 (_:pre) -> (reverse pre, reverse suf)
178 where (suf,pre) = break pred (reverse s)
180 newsuf :: String -> Suffix -> String
181 newsuf suf s = remove_suffix '.' s ++ suf
183 -- getdir strips the filename off the input string, returning the directory.
184 getdir :: String -> String
185 getdir s = if null dir then "." else init dir
186 where dir = take_longest_prefix s isPathSeparator
188 newdir :: String -> String -> String
189 newdir dir s = dir ++ '/':drop_longest_prefix s isPathSeparator
191 remove_spaces :: String -> String
192 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
194 escapeSpaces :: String -> String
195 escapeSpaces = foldr (\c s -> if isSpace c then '\\':c:s else c:s) ""
197 isPathSeparator :: Char -> Bool
199 #ifdef mingw32_TARGET_OS
200 ch == '/' || ch == '\\'