1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.19 2001/03/08 09:50:18 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"
29 -----------------------------------------------------------------------------
32 GLOBAL_VAR(v_Path_usage, "", String)
35 usage_path <- readIORef v_Path_usage
36 usage <- readFile usage_path
41 dump ('$':'$':s) = hPutStr stderr progName >> dump s
42 dump (c:s) = hPutChar stderr c >> dump s
44 -----------------------------------------------------------------------------
45 -- Reading OPTIONS pragmas
48 :: String -- input file
49 -> IO [String] -- options, if any
50 getOptionsFromSource file
51 = do h <- openFile file ReadMode
52 catchJust ioErrors (look h)
53 (\e -> if isEOFError e then return [] else ioError e)
59 | prefixMatch "#" l -> look h
60 | prefixMatch "{-# LINE" l -> look h -- -}
61 | Just (opts:_) <- matchRegex optionRegex l
63 return (words opts ++ rest)
64 | otherwise -> return []
66 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
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 handle = flip Exception.catchAllIO
97 split :: Char -> String -> [String]
98 split c s = case rest of
100 _:rest -> chunk : split c rest
101 where (chunk, rest) = break (==c) s
103 add :: IORef [a] -> a -> IO ()
106 writeIORef var (x:xs)
108 addNoDups :: Eq a => IORef [a] -> a -> IO ()
111 unless (x `elem` xs) $ writeIORef var (x:xs)
113 splitFilename :: String -> (String,String)
114 splitFilename f = split_longest_prefix f '.'
116 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
117 splitFilename3 :: String -> (String,String,String)
119 = let (dir, rest) = split_longest_prefix str '/'
120 (name, ext) = splitFilename rest
121 real_dir | null dir = "."
123 in (real_dir, name, ext)
125 remove_suffix :: Char -> String -> String
127 | null pre = reverse suf
128 | otherwise = reverse pre
129 where (suf,pre) = break (==c) (reverse s)
131 drop_longest_prefix :: String -> Char -> String
132 drop_longest_prefix s c = reverse suf
133 where (suf,_pre) = break (==c) (reverse s)
135 take_longest_prefix :: String -> Char -> String
136 take_longest_prefix s c = reverse pre
137 where (_suf,pre) = break (==c) (reverse s)
139 -- split a string at the last occurence of 'c', returning the two
140 -- parts of the string with the 'c' removed. If the string contains
141 -- no 'c's, the entire string is returned in the second component.
142 split_longest_prefix :: String -> Char -> (String,String)
143 split_longest_prefix s c
145 [] -> ([], reverse suf)
146 (_:pre) -> (reverse pre, reverse suf)
147 where (suf,pre) = break (==c) (reverse s)
149 newsuf :: String -> String -> String
150 newsuf suf s = remove_suffix '.' s ++ suf
152 -- getdir strips the filename off the input string, returning the directory.
153 getdir :: String -> String
154 getdir s = if null dir then "." else init dir
155 where dir = take_longest_prefix s '/'
157 newdir :: String -> String -> String
158 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
160 remove_spaces :: String -> String
161 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace