1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.16 2000/12/20 15:44:01 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"
25 import Directory ( removeFile )
30 -----------------------------------------------------------------------------
33 GLOBAL_VAR(v_Path_usage, "", String)
36 usage_path <- readIORef v_Path_usage
37 usage <- readFile usage_path
42 dump ('$':'$':s) = hPutStr stderr progName >> dump s
43 dump (c:s) = hPutChar stderr c >> dump s
45 -----------------------------------------------------------------------------
46 -- Reading OPTIONS pragmas
49 :: String -- input file
50 -> IO [String] -- options, if any
51 getOptionsFromSource file
52 = do h <- openFile file ReadMode
53 catchJust ioErrors (look h)
54 (\e -> if isEOFError e then return [] else ioError e)
60 | prefixMatch "#" l -> look h
61 | prefixMatch "{-# LINE" l -> look h -- -}
62 | Just (opts:_) <- matchRegex optionRegex l
64 return (words opts ++ rest)
65 | otherwise -> return []
67 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
69 -----------------------------------------------------------------------------
72 unknownFlagErr :: String -> a
73 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
75 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
76 my_partition _ [] = ([],[])
78 = let (bs,cs) = my_partition p as in
81 Just b -> ((a,b):bs,cs)
83 my_prefix_match :: String -> String -> Maybe String
84 my_prefix_match [] rest = Just rest
85 my_prefix_match (_:_) [] = Nothing
86 my_prefix_match (p:pat) (r:rest)
87 | p == r = my_prefix_match pat rest
92 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
93 handleDyn = flip catchDyn
95 handle :: (Exception -> IO a) -> IO a -> IO a
96 handle = flip Exception.catchAllIO
98 split :: Char -> String -> [String]
99 split c s = case rest of
101 _:rest -> chunk : split c rest
102 where (chunk, rest) = break (==c) s
104 add :: IORef [a] -> a -> IO ()
107 writeIORef var (x:xs)
109 addNoDups :: Eq a => IORef [a] -> a -> IO ()
112 unless (x `elem` xs) $ writeIORef var (x:xs)
114 splitFilename :: String -> (String,String)
115 splitFilename f = split_longest_prefix f '.'
117 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
118 splitFilename3 :: String -> (String,String,String)
120 = let (dir, rest) = split_longest_prefix str '/'
121 (name, ext) = splitFilename rest
122 real_dir | null dir = "."
124 in (real_dir, name, ext)
126 remove_suffix :: Char -> String -> String
128 | null pre = reverse suf
129 | otherwise = reverse pre
130 where (suf,pre) = break (==c) (reverse s)
132 drop_longest_prefix :: String -> Char -> String
133 drop_longest_prefix s c = reverse suf
134 where (suf,_pre) = break (==c) (reverse s)
136 take_longest_prefix :: String -> Char -> String
137 take_longest_prefix s c = reverse pre
138 where (_suf,pre) = break (==c) (reverse s)
140 -- split a string at the last occurence of 'c', returning the two
141 -- parts of the string with the 'c' removed. If the string contains
142 -- no 'c's, the entire string is returned in the second component.
143 split_longest_prefix :: String -> Char -> (String,String)
144 split_longest_prefix s c
146 [] -> ([], reverse suf)
147 (_:pre) -> (reverse pre, reverse suf)
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
165 -- system that works feasibly under Windows (i.e. passes the command line to sh,
166 -- because system() under Windows doesn't look at SHELL, and always uses CMD.EXE)
167 kludgedSystem cmd phase_name
169 #ifndef mingw32_TARGET_OS
170 exit_code <- system cmd `catchAllIO`
171 (\_ -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
173 pid <- myGetProcessID
174 let tmp = "/tmp/sh" ++ show pid
175 h <- openFile tmp WriteMode
178 exit_code <- system ("sh - " ++ tmp) `catchAllIO`
179 (\_ -> removeFile tmp >>
180 throwDyn (PhaseFailed phase_name (ExitFailure 1)))