1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.20 2001/04/26 14:33:44 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 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
129 splitFilename3 :: String -> (String,String,String)
131 = let (dir, rest) = split_longest_prefix str '/'
132 (name, ext) = splitFilename rest
133 real_dir | null dir = "."
135 in (real_dir, name, ext)
137 remove_suffix :: Char -> String -> String
139 | null pre = reverse suf
140 | otherwise = reverse pre
141 where (suf,pre) = break (==c) (reverse s)
143 drop_longest_prefix :: String -> Char -> String
144 drop_longest_prefix s c = reverse suf
145 where (suf,_pre) = break (==c) (reverse s)
147 take_longest_prefix :: String -> Char -> String
148 take_longest_prefix s c = reverse pre
149 where (_suf,pre) = break (==c) (reverse s)
151 -- split a string at the last occurence of 'c', returning the two
152 -- parts of the string with the 'c' removed. If the string contains
153 -- no 'c's, the entire string is returned in the second component.
154 split_longest_prefix :: String -> Char -> (String,String)
155 split_longest_prefix s c
157 [] -> ([], reverse suf)
158 (_:pre) -> (reverse pre, reverse suf)
159 where (suf,pre) = break (==c) (reverse s)
161 newsuf :: String -> String -> String
162 newsuf suf s = remove_suffix '.' s ++ suf
164 -- getdir strips the filename off the input string, returning the directory.
165 getdir :: String -> String
166 getdir s = if null dir then "." else init dir
167 where dir = take_longest_prefix s '/'
169 newdir :: String -> String -> String
170 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
172 remove_spaces :: String -> String
173 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace