1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.2 2000/10/11 14:08:52 simonmar Exp $
4 -- Utils for the driver
6 -- (c) The University of Glasgow 2000
8 -----------------------------------------------------------------------------
10 module DriverUtil where
12 #include "HsVersions.h"
29 -----------------------------------------------------------------------------
32 short_usage = "Usage: For basic information, try the `--help' option."
34 GLOBAL_VAR(path_usage, "", String)
37 usage_path <- readIORef path_usage
38 usage <- readFile usage_path
43 dump ('$':'$':s) = hPutStr stderr get_prog_name >> dump s
44 dump (c:s) = hPutChar stderr c >> dump s
46 version_str = cProjectVersion
49 = PhaseFailed String ExitCode
51 | UsageError String -- prints the short usage msg after the error
52 | OtherError String -- just prints the error message
55 GLOBAL_VAR(prog_name, "ghc", String)
57 get_prog_name = unsafePerformIO (readIORef prog_name) -- urk!
59 instance Show BarfKind where
60 showsPrec _ e = showString get_prog_name . showString ": " . showBarf e
62 showBarf (UsageError str) = showString str . showChar '\n' . showString short_usage
63 showBarf (OtherError str) = showString str
64 showBarf (PhaseFailed phase code) =
65 showString phase . showString " failed, code = " . shows code
66 showBarf (Interrupted) = showString "interrupted"
68 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
70 barfKindTc = mkTyCon "BarfKind"
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 remove_suffix :: Char -> String -> String
138 | null pre = reverse suf
139 | otherwise = reverse pre
140 where (suf,pre) = break (==c) (reverse s)
142 drop_longest_prefix :: String -> Char -> String
143 drop_longest_prefix s c = reverse suf
144 where (suf,_pre) = break (==c) (reverse s)
146 take_longest_prefix :: String -> Char -> String
147 take_longest_prefix s c = reverse pre
148 where (_suf,pre) = break (==c) (reverse s)
150 newsuf :: String -> String -> String
151 newsuf suf s = remove_suffix '.' s ++ suf
153 -- getdir strips the filename off the input string, returning the directory.
154 getdir :: String -> String
155 getdir s = if null dir then "." else init dir
156 where dir = take_longest_prefix s '/'
158 newdir :: String -> String -> String
159 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
161 remove_spaces :: String -> String
162 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
166 \ __GLASGOW_HASKELL__" of