1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.9 2000/11/19 19:40:08 simonmar Exp $
4 -- Utils for the driver
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
10 module DriverUtil where
12 #include "HsVersions.h"
28 -----------------------------------------------------------------------------
31 short_usage = "Usage: For basic information, try the `--help' option."
33 GLOBAL_VAR(v_Path_usage, "", String)
36 usage_path <- readIORef v_Path_usage
37 usage <- readFile usage_path
42 dump ('$':'$':s) = hPutStr stderr prog_name >> dump s
43 dump (c:s) = hPutChar stderr c >> dump s
46 = PhaseFailed String ExitCode
48 | UsageError String -- prints the short usage msg after the error
49 | OtherError String -- just prints the error message
52 prog_name = unsafePerformIO (getProgName)
53 {-# NOINLINE prog_name #-}
55 instance Show BarfKind where
56 showsPrec _ e = showString prog_name . showString ": " . showBarf e
58 showBarf (UsageError str)
59 = showString str . showChar '\n' . showString short_usage
60 showBarf (OtherError str)
62 showBarf (PhaseFailed phase code)
63 = showString phase . showString " failed, code = " . shows code
64 showBarf (Interrupted)
65 = showString "interrupted"
67 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
69 barfKindTc = mkTyCon "BarfKind"
70 {-# NOINLINE barfKindTc #-}
71 instance Typeable BarfKind where
72 typeOf _ = mkAppTy barfKindTc []
74 -----------------------------------------------------------------------------
75 -- Reading OPTIONS pragmas
78 :: String -- input file
79 -> IO [String] -- options, if any
80 getOptionsFromSource file
81 = do h <- openFile file ReadMode
82 catchJust ioErrors (look h)
83 (\e -> if isEOFError e then return [] else ioError e)
89 | prefixMatch "#" l -> look h
90 | prefixMatch "{-# LINE" l -> look h -- -}
91 | Just (opts:_) <- matchRegex optionRegex l
92 -> return (words opts)
93 | otherwise -> return []
95 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
97 -----------------------------------------------------------------------------
100 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
101 my_partition _ [] = ([],[])
102 my_partition p (a:as)
103 = let (bs,cs) = my_partition p as in
106 Just b -> ((a,b):bs,cs)
108 my_prefix_match :: String -> String -> Maybe String
109 my_prefix_match [] rest = Just rest
110 my_prefix_match (_:_) [] = Nothing
111 my_prefix_match (p:pat) (r:rest)
112 | p == r = my_prefix_match pat rest
113 | otherwise = Nothing
117 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
118 handleDyn = flip catchDyn
120 split :: Char -> String -> [String]
121 split c s = case rest of
123 _:rest -> chunk : split c rest
124 where (chunk, rest) = break (==c) s
126 add :: IORef [a] -> a -> IO ()
129 writeIORef var (x:xs)
131 addNoDups :: Eq a => IORef [a] -> a -> IO ()
134 unless (x `elem` xs) $ writeIORef var (x:xs)
136 splitFilename :: String -> (String,String)
137 splitFilename f = (reverse (stripDot rev_basename), reverse rev_ext)
138 where (rev_ext, rev_basename) = span ('.' /=) (reverse f)
139 stripDot ('.':xs) = xs
142 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
143 splitFilename3 :: String -> (String,String,String)
145 = let dir = getdir str
146 (name, ext) = splitFilename (drop (length dir) str)
149 remove_suffix :: Char -> String -> String
151 | null pre = reverse suf
152 | otherwise = reverse pre
153 where (suf,pre) = break (==c) (reverse s)
155 drop_longest_prefix :: String -> Char -> String
156 drop_longest_prefix s c = reverse suf
157 where (suf,_pre) = break (==c) (reverse s)
159 take_longest_prefix :: String -> Char -> String
160 take_longest_prefix s c = reverse pre
161 where (_suf,pre) = break (==c) (reverse s)
163 newsuf :: String -> String -> String
164 newsuf suf s = remove_suffix '.' s ++ suf
166 -- getdir strips the filename off the input string, returning the directory.
167 getdir :: String -> String
168 getdir s = if null dir then "." else init dir
169 where dir = take_longest_prefix s '/'
171 newdir :: String -> String -> String
172 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
174 remove_spaces :: String -> String
175 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace