1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.29 2002/01/22 14:47:52 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 )
24 import Directory ( getDirectoryContents )
31 -----------------------------------------------------------------------------
34 -----------------------------------------------------------------------------
35 -- Reading OPTIONS pragmas
38 :: String -- input file
39 -> IO [String] -- options, if any
40 getOptionsFromSource file
41 = do h <- openFile file ReadMode
42 catchJust ioErrors (look h `finally` hClose h)
43 (\e -> if isEOFError e then return [] else ioError e)
47 let l = remove_spaces l'
50 | prefixMatch "#" l -> look h
51 | prefixMatch "{-# LINE" l -> look h -- -}
52 | Just (opts:_) <- matchRegex optionRegex l
54 return (words opts ++ rest)
55 | otherwise -> return []
57 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
59 -----------------------------------------------------------------------------
60 -- A version of getDirectoryContents that is non-fatal if the
61 -- directory doesn't exist.
63 softGetDirectoryContents d
64 = IO.catch (getDirectoryContents d)
65 (\_ -> do hPutStrLn stderr
66 ("WARNING: error while reading directory " ++ d)
70 -----------------------------------------------------------------------------
71 -- Prefixing underscore to linker-level names
72 prefixUnderscore :: String -> String
74 | cLeadingUnderscore == "YES" = ('_':)
77 -----------------------------------------------------------------------------
80 unknownFlagErr :: String -> a
81 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
83 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
84 my_partition _ [] = ([],[])
86 = let (bs,cs) = my_partition p as in
89 Just b -> ((a,b):bs,cs)
91 my_prefix_match :: String -> String -> Maybe String
92 my_prefix_match [] rest = Just rest
93 my_prefix_match (_:_) [] = Nothing
94 my_prefix_match (p:pat) (r:rest)
95 | p == r = my_prefix_match pat rest
100 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
101 handleDyn = flip catchDyn
103 handle :: (Exception -> IO a) -> IO a -> IO a
104 #if __GLASGOW_HASKELL__ < 501
105 handle = flip Exception.catchAllIO
107 handle h f = f `Exception.catch` \e -> case e of
108 ExitException _ -> throw e
112 split :: Char -> String -> [String]
113 split c s = case rest of
115 _:rest -> chunk : split c rest
116 where (chunk, rest) = break (==c) s
118 add :: IORef [a] -> a -> IO ()
121 writeIORef var (x:xs)
123 addNoDups :: Eq a => IORef [a] -> a -> IO ()
126 unless (x `elem` xs) $ writeIORef var (x:xs)
128 ------------------------------------------------------
129 -- Filename manipulation
130 ------------------------------------------------------
134 splitFilename :: String -> (String,Suffix)
135 splitFilename f = split_longest_prefix f (=='.')
137 getFileSuffix :: String -> Suffix
138 getFileSuffix f = drop_longest_prefix f (=='.')
140 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
141 splitFilename3 :: String -> (String,String,Suffix)
143 = let (dir, rest) = split_longest_prefix str isPathSeparator
144 (name, ext) = splitFilename rest
145 real_dir | null dir = "."
147 in (real_dir, name, ext)
149 remove_suffix :: Char -> String -> Suffix
151 | null pre = reverse suf
152 | otherwise = reverse pre
153 where (suf,pre) = break (==c) (reverse s)
155 drop_longest_prefix :: String -> (Char -> Bool) -> String
156 drop_longest_prefix s pred = reverse suf
157 where (suf,_pre) = break pred (reverse s)
159 take_longest_prefix :: String -> (Char -> Bool) -> String
160 take_longest_prefix s pred = reverse pre
161 where (_suf,pre) = break pred (reverse s)
163 -- split a string at the last character where 'pred' is True,
164 -- returning a pair of strings. The first component holds the string
165 -- up (but not including) the last character for which 'pred' returned
166 -- True, the second whatever comes after (but also not including the
169 -- If 'pred' returns False for all characters in the string, the original
170 -- string is returned in the second component (and the first one is just
172 split_longest_prefix :: String -> (Char -> Bool) -> (String,String)
173 split_longest_prefix s pred
175 [] -> ([], reverse suf)
176 (_:pre) -> (reverse pre, reverse suf)
177 where (suf,pre) = break pred (reverse s)
179 newsuf :: String -> Suffix -> String
180 newsuf suf s = remove_suffix '.' s ++ suf
182 -- getdir strips the filename off the input string, returning the directory.
183 getdir :: String -> String
184 getdir s = if null dir then "." else init dir
185 where dir = take_longest_prefix s isPathSeparator
187 newdir :: String -> String -> String
188 newdir dir s = dir ++ '/':drop_longest_prefix s isPathSeparator
190 remove_spaces :: String -> String
191 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
193 isPathSeparator :: Char -> Bool
195 #ifdef mingw32_TARGET_OS
196 ch == '/' || ch == '\\'