1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.21 2001/05/08 10:58:48 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"
23 import Directory ( getDirectoryContents )
30 -----------------------------------------------------------------------------
33 GLOBAL_VAR(v_Path_usage, "", String)
36 usage_path <- readIORef v_Path_usage
37 usage <- readFile usage_path
42 dump ('$':'$':s) = hPutStr stderr progName >> dump s
43 dump (c:s) = hPutChar stderr c >> dump s
45 -----------------------------------------------------------------------------
46 -- Reading OPTIONS pragmas
49 :: String -- input file
50 -> IO [String] -- options, if any
51 getOptionsFromSource file
52 = do h <- openFile file ReadMode
53 catchJust ioErrors (look h)
54 (\e -> if isEOFError e then return [] else ioError e)
60 | prefixMatch "#" l -> look h
61 | prefixMatch "{-# LINE" l -> look h -- -}
62 | Just (opts:_) <- matchRegex optionRegex l
64 return (words opts ++ rest)
65 | otherwise -> return []
67 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
69 -----------------------------------------------------------------------------
70 -- A version of getDirectoryContents that is non-fatal if the
71 -- directory doesn't exist.
73 softGetDirectoryContents d
74 = IO.catch (getDirectoryContents d)
75 (\_ -> do hPutStr stderr
76 ("WARNING: error while reading directory " ++ d)
80 -----------------------------------------------------------------------------
83 unknownFlagErr :: String -> a
84 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
86 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
87 my_partition _ [] = ([],[])
89 = let (bs,cs) = my_partition p as in
92 Just b -> ((a,b):bs,cs)
94 my_prefix_match :: String -> String -> Maybe String
95 my_prefix_match [] rest = Just rest
96 my_prefix_match (_:_) [] = Nothing
97 my_prefix_match (p:pat) (r:rest)
98 | p == r = my_prefix_match pat rest
103 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
104 handleDyn = flip catchDyn
106 handle :: (Exception -> IO a) -> IO a -> IO a
107 handle = flip Exception.catchAllIO
109 split :: Char -> String -> [String]
110 split c s = case rest of
112 _:rest -> chunk : split c rest
113 where (chunk, rest) = break (==c) s
115 add :: IORef [a] -> a -> IO ()
118 writeIORef var (x:xs)
120 addNoDups :: Eq a => IORef [a] -> a -> IO ()
123 unless (x `elem` xs) $ writeIORef var (x:xs)
125 splitFilename :: String -> (String,String)
126 splitFilename f = split_longest_prefix f '.'
128 getFileSuffix :: String -> String
129 getFileSuffix f = drop_longest_prefix f '.'
131 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
132 splitFilename3 :: String -> (String,String,String)
134 = let (dir, rest) = split_longest_prefix str '/'
135 (name, ext) = splitFilename rest
136 real_dir | null dir = "."
138 in (real_dir, name, ext)
140 remove_suffix :: Char -> String -> String
142 | null pre = reverse suf
143 | otherwise = reverse pre
144 where (suf,pre) = break (==c) (reverse s)
146 drop_longest_prefix :: String -> Char -> String
147 drop_longest_prefix s c = reverse suf
148 where (suf,_pre) = break (==c) (reverse s)
150 take_longest_prefix :: String -> Char -> String
151 take_longest_prefix s c = reverse pre
152 where (_suf,pre) = break (==c) (reverse s)
154 -- split a string at the last occurence of 'c', returning the two
155 -- parts of the string with the 'c' removed. If the string contains
156 -- no 'c's, the entire string is returned in the second component.
157 split_longest_prefix :: String -> Char -> (String,String)
158 split_longest_prefix s c
160 [] -> ([], reverse suf)
161 (_:pre) -> (reverse pre, reverse suf)
162 where (suf,pre) = break (==c) (reverse s)
164 newsuf :: String -> String -> String
165 newsuf suf s = remove_suffix '.' s ++ suf
167 -- getdir strips the filename off the input string, returning the directory.
168 getdir :: String -> String
169 getdir s = if null dir then "." else init dir
170 where dir = take_longest_prefix s '/'
172 newdir :: String -> String -> String
173 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
175 remove_spaces :: String -> String
176 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace