1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.18 2001/03/07 10:28:40 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"
17 import TmpFiles ( v_TmpDir )
26 import Directory ( removeFile )
31 -----------------------------------------------------------------------------
34 GLOBAL_VAR(v_Path_usage, "", String)
37 usage_path <- readIORef v_Path_usage
38 usage <- readFile usage_path
43 dump ('$':'$':s) = hPutStr stderr progName >> dump s
44 dump (c:s) = hPutChar stderr c >> dump s
46 -----------------------------------------------------------------------------
47 -- Reading OPTIONS pragmas
50 :: String -- input file
51 -> IO [String] -- options, if any
52 getOptionsFromSource file
53 = do h <- openFile file ReadMode
54 catchJust ioErrors (look h)
55 (\e -> if isEOFError e then return [] else ioError e)
61 | prefixMatch "#" l -> look h
62 | prefixMatch "{-# LINE" l -> look h -- -}
63 | Just (opts:_) <- matchRegex optionRegex l
65 return (words opts ++ rest)
66 | otherwise -> return []
68 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
70 -----------------------------------------------------------------------------
73 unknownFlagErr :: String -> a
74 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
76 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
77 my_partition _ [] = ([],[])
79 = let (bs,cs) = my_partition p as in
82 Just b -> ((a,b):bs,cs)
84 my_prefix_match :: String -> String -> Maybe String
85 my_prefix_match [] rest = Just rest
86 my_prefix_match (_:_) [] = Nothing
87 my_prefix_match (p:pat) (r:rest)
88 | p == r = my_prefix_match pat rest
93 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
94 handleDyn = flip catchDyn
96 handle :: (Exception -> IO a) -> IO a -> IO a
97 handle = flip Exception.catchAllIO
99 split :: Char -> String -> [String]
100 split c s = case rest of
102 _:rest -> chunk : split c rest
103 where (chunk, rest) = break (==c) s
105 add :: IORef [a] -> a -> IO ()
108 writeIORef var (x:xs)
110 addNoDups :: Eq a => IORef [a] -> a -> IO ()
113 unless (x `elem` xs) $ writeIORef var (x:xs)
115 splitFilename :: String -> (String,String)
116 splitFilename f = split_longest_prefix f '.'
118 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
119 splitFilename3 :: String -> (String,String,String)
121 = let (dir, rest) = split_longest_prefix str '/'
122 (name, ext) = splitFilename rest
123 real_dir | null dir = "."
125 in (real_dir, name, ext)
127 remove_suffix :: Char -> String -> String
129 | null pre = reverse suf
130 | otherwise = reverse pre
131 where (suf,pre) = break (==c) (reverse s)
133 drop_longest_prefix :: String -> Char -> String
134 drop_longest_prefix s c = reverse suf
135 where (suf,_pre) = break (==c) (reverse s)
137 take_longest_prefix :: String -> Char -> String
138 take_longest_prefix s c = reverse pre
139 where (_suf,pre) = break (==c) (reverse s)
141 -- split a string at the last occurence of 'c', returning the two
142 -- parts of the string with the 'c' removed. If the string contains
143 -- no 'c's, the entire string is returned in the second component.
144 split_longest_prefix :: String -> Char -> (String,String)
145 split_longest_prefix s c
147 [] -> ([], reverse suf)
148 (_:pre) -> (reverse pre, reverse suf)
149 where (suf,pre) = break (==c) (reverse s)
151 newsuf :: String -> String -> String
152 newsuf suf s = remove_suffix '.' s ++ suf
154 -- getdir strips the filename off the input string, returning the directory.
155 getdir :: String -> String
156 getdir s = if null dir then "." else init dir
157 where dir = take_longest_prefix s '/'
159 newdir :: String -> String -> String
160 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
162 remove_spaces :: String -> String
163 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
166 -- system that works feasibly under Windows (i.e. passes the command line to sh,
167 -- because system() under Windows doesn't look at SHELL, and always uses CMD.EXE)
168 kludgedSystem cmd phase_name
170 #ifndef mingw32_TARGET_OS
171 exit_code <- system cmd `catchAllIO`
172 (\_ -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
174 pid <- myGetProcessID
175 tmp_dir <- readIORef v_TmpDir
176 let tmp = tmp_dir++"/sh"++show pid
177 h <- openFile tmp WriteMode
180 exit_code <- system ("sh - " ++ tmp) `catchAllIO`
181 (\_ -> removeFile tmp >>
182 throwDyn (PhaseFailed phase_name (ExitFailure 1)))