1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.25 2001/06/27 10:14:13 rrt 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 -----------------------------------------------------------------------------
34 -- Reading OPTIONS pragmas
37 :: String -- input file
38 -> IO [String] -- options, if any
39 getOptionsFromSource file
40 = do h <- openFile file ReadMode
41 catchJust ioErrors (look h `finally` hClose h)
42 (\e -> if isEOFError e then return [] else ioError e)
48 | prefixMatch "#" l -> look h
49 | prefixMatch "{-# LINE" l -> look h -- -}
50 | Just (opts:_) <- matchRegex optionRegex l
52 return (words opts ++ rest)
53 | otherwise -> return []
55 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
57 -----------------------------------------------------------------------------
58 -- A version of getDirectoryContents that is non-fatal if the
59 -- directory doesn't exist.
61 softGetDirectoryContents d
62 = IO.catch (getDirectoryContents d)
63 (\_ -> do hPutStr stderr
64 ("WARNING: error while reading directory " ++ d)
68 -----------------------------------------------------------------------------
71 unknownFlagErr :: String -> a
72 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
74 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
75 my_partition _ [] = ([],[])
77 = let (bs,cs) = my_partition p as in
80 Just b -> ((a,b):bs,cs)
82 my_prefix_match :: String -> String -> Maybe String
83 my_prefix_match [] rest = Just rest
84 my_prefix_match (_:_) [] = Nothing
85 my_prefix_match (p:pat) (r:rest)
86 | p == r = my_prefix_match pat rest
91 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
92 handleDyn = flip catchDyn
94 handle :: (Exception -> IO a) -> IO a -> IO a
95 #if __GLASGOW_HASKELL__ < 501
96 handle = flip Exception.catchAllIO
98 handle h f = f `Exception.catch` \e -> case e of
99 ExitException _ -> throw e
103 split :: Char -> String -> [String]
104 split c s = case rest of
106 _:rest -> chunk : split c rest
107 where (chunk, rest) = break (==c) s
109 add :: IORef [a] -> a -> IO ()
112 writeIORef var (x:xs)
114 addNoDups :: Eq a => IORef [a] -> a -> IO ()
117 unless (x `elem` xs) $ writeIORef var (x:xs)
119 ------------------------------------------------------
120 -- Filename manipulation
121 ------------------------------------------------------
125 splitFilename :: String -> (String,Suffix)
126 splitFilename f = split_longest_prefix f '.'
128 getFileSuffix :: String -> Suffix
129 getFileSuffix f = drop_longest_prefix f '.'
131 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
132 splitFilename3 :: String -> (String,String,Suffix)
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 -> Suffix
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 -> Suffix -> 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