1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.14 2000/12/12 12:10:08 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"
24 import Directory ( removeFile )
29 -----------------------------------------------------------------------------
32 short_usage = "Usage: For basic information, try the `--help' option."
34 GLOBAL_VAR(v_Path_usage, "", String)
37 usage_path <- readIORef v_Path_usage
38 usage <- readFile usage_path
43 dump ('$':'$':s) = hPutStr stderr prog_name >> dump s
44 dump (c:s) = hPutChar stderr c >> dump s
47 = PhaseFailed String ExitCode
49 | UsageError String -- prints the short usage msg after the error
50 | OtherError String -- just prints the error message
53 prog_name = unsafePerformIO (getProgName)
54 {-# NOINLINE prog_name #-}
56 instance Show BarfKind where
57 showsPrec _ e = showString prog_name . showString ": " . showBarf e
59 showBarf (UsageError str)
60 = showString str . showChar '\n' . showString short_usage
61 showBarf (OtherError str)
63 showBarf (PhaseFailed phase code)
64 = showString phase . showString " failed, code = " . shows code
65 showBarf (Interrupted)
66 = showString "interrupted"
68 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
70 barfKindTc = mkTyCon "BarfKind"
71 {-# NOINLINE barfKindTc #-}
72 instance Typeable BarfKind where
73 typeOf _ = mkAppTy barfKindTc []
75 -----------------------------------------------------------------------------
76 -- Reading OPTIONS pragmas
79 :: String -- input file
80 -> IO [String] -- options, if any
81 getOptionsFromSource file
82 = do h <- openFile file ReadMode
83 catchJust ioErrors (look h)
84 (\e -> if isEOFError e then return [] else ioError e)
90 | prefixMatch "#" l -> look h
91 | prefixMatch "{-# LINE" l -> look h -- -}
92 | Just (opts:_) <- matchRegex optionRegex l
93 -> return (words opts)
94 | otherwise -> return []
96 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
98 -----------------------------------------------------------------------------
101 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
102 my_partition _ [] = ([],[])
103 my_partition p (a:as)
104 = let (bs,cs) = my_partition p as in
107 Just b -> ((a,b):bs,cs)
109 my_prefix_match :: String -> String -> Maybe String
110 my_prefix_match [] rest = Just rest
111 my_prefix_match (_:_) [] = Nothing
112 my_prefix_match (p:pat) (r:rest)
113 | p == r = my_prefix_match pat rest
114 | otherwise = Nothing
118 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
119 handleDyn = flip catchDyn
121 handle :: (Exception -> IO a) -> IO a -> IO a
122 handle = flip Exception.catchAllIO
124 split :: Char -> String -> [String]
125 split c s = case rest of
127 _:rest -> chunk : split c rest
128 where (chunk, rest) = break (==c) s
130 add :: IORef [a] -> a -> IO ()
133 writeIORef var (x:xs)
135 addNoDups :: Eq a => IORef [a] -> a -> IO ()
138 unless (x `elem` xs) $ writeIORef var (x:xs)
140 splitFilename :: String -> (String,String)
141 splitFilename f = split_longest_prefix f '.'
143 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
144 splitFilename3 :: String -> (String,String,String)
146 = let (dir, rest) = split_longest_prefix str '/'
147 (name, ext) = splitFilename rest
148 real_dir | null dir = "."
150 in (real_dir, name, ext)
152 remove_suffix :: Char -> String -> String
154 | null pre = reverse suf
155 | otherwise = reverse pre
156 where (suf,pre) = break (==c) (reverse s)
158 drop_longest_prefix :: String -> Char -> String
159 drop_longest_prefix s c = reverse suf
160 where (suf,_pre) = break (==c) (reverse s)
162 take_longest_prefix :: String -> Char -> String
163 take_longest_prefix s c = reverse pre
164 where (_suf,pre) = break (==c) (reverse s)
166 -- split a string at the last occurence of 'c', returning the two
167 -- parts of the string with the 'c' removed. If the string contains
168 -- no 'c's, the entire string is returned in the second component.
169 split_longest_prefix :: String -> Char -> (String,String)
170 split_longest_prefix s c
172 [] -> ([], reverse suf)
173 (_:pre) -> (reverse pre, reverse suf)
174 where (suf,pre) = break (==c) (reverse s)
176 newsuf :: String -> String -> String
177 newsuf suf s = remove_suffix '.' s ++ suf
179 -- getdir strips the filename off the input string, returning the directory.
180 getdir :: String -> String
181 getdir s = if null dir then "." else init dir
182 where dir = take_longest_prefix s '/'
184 newdir :: String -> String -> String
185 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
187 remove_spaces :: String -> String
188 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
191 -- system that works feasibly under Windows (i.e. passes the command line to sh,
192 -- because system() under Windows doesn't look at SHELL, and always uses CMD.EXE)
193 kludgedSystem cmd phase_name
195 #ifndef mingw32_TARGET_OS
196 exit_code <- system cmd `catchAllIO`
197 (\_ -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
199 pid <- myGetProcessID
200 let tmp = "/tmp/sh" ++ show pid
201 h <- openFile tmp WriteMode
204 exit_code <- system ("sh - " ++ tmp) `catchAllIO`
205 (\_ -> removeFile tmp >>
206 throwDyn (PhaseFailed phase_name (ExitFailure 1)))