1 -----------------------------------------------------------------------------
2 -- $Id: DriverUtil.hs,v 1.15 2000/12/12 14:35: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"
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
63 -> return (words opts)
64 | otherwise -> return []
66 optionRegex = mkRegex "\\{-#[ \t]+OPTIONS[ \t]+(.*)#-\\}" -- -}
68 -----------------------------------------------------------------------------
71 unknownFlagErr :: String -> a
72 unknownFlagErr f = throwDyn (UsageError ("unrecognised flag: " ++ f))
74 my_partition :: (a -> Maybe b) -> [a] -> ([(a,b)],[a])
75 my_partition _ [] = ([],[])
77 = let (bs,cs) = my_partition p as in
80 Just b -> ((a,b):bs,cs)
82 my_prefix_match :: String -> String -> Maybe String
83 my_prefix_match [] rest = Just rest
84 my_prefix_match (_:_) [] = Nothing
85 my_prefix_match (p:pat) (r:rest)
86 | p == r = my_prefix_match pat rest
91 handleDyn :: Typeable ex => (ex -> IO a) -> IO a -> IO a
92 handleDyn = flip catchDyn
94 handle :: (Exception -> IO a) -> IO a -> IO a
95 handle = flip Exception.catchAllIO
97 split :: Char -> String -> [String]
98 split c s = case rest of
100 _:rest -> chunk : split c rest
101 where (chunk, rest) = break (==c) s
103 add :: IORef [a] -> a -> IO ()
106 writeIORef var (x:xs)
108 addNoDups :: Eq a => IORef [a] -> a -> IO ()
111 unless (x `elem` xs) $ writeIORef var (x:xs)
113 splitFilename :: String -> (String,String)
114 splitFilename f = split_longest_prefix f '.'
116 -- "foo/bar/xyzzy.ext" -> ("foo/bar", "xyzzy", ".ext")
117 splitFilename3 :: String -> (String,String,String)
119 = let (dir, rest) = split_longest_prefix str '/'
120 (name, ext) = splitFilename rest
121 real_dir | null dir = "."
123 in (real_dir, name, ext)
125 remove_suffix :: Char -> String -> String
127 | null pre = reverse suf
128 | otherwise = reverse pre
129 where (suf,pre) = break (==c) (reverse s)
131 drop_longest_prefix :: String -> Char -> String
132 drop_longest_prefix s c = reverse suf
133 where (suf,_pre) = break (==c) (reverse s)
135 take_longest_prefix :: String -> Char -> String
136 take_longest_prefix s c = reverse pre
137 where (_suf,pre) = break (==c) (reverse s)
139 -- split a string at the last occurence of 'c', returning the two
140 -- parts of the string with the 'c' removed. If the string contains
141 -- no 'c's, the entire string is returned in the second component.
142 split_longest_prefix :: String -> Char -> (String,String)
143 split_longest_prefix s c
145 [] -> ([], reverse suf)
146 (_:pre) -> (reverse pre, reverse suf)
147 where (suf,pre) = break (==c) (reverse s)
149 newsuf :: String -> String -> String
150 newsuf suf s = remove_suffix '.' s ++ suf
152 -- getdir strips the filename off the input string, returning the directory.
153 getdir :: String -> String
154 getdir s = if null dir then "." else init dir
155 where dir = take_longest_prefix s '/'
157 newdir :: String -> String -> String
158 newdir dir s = dir ++ '/':drop_longest_prefix s '/'
160 remove_spaces :: String -> String
161 remove_spaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace
164 -- system that works feasibly under Windows (i.e. passes the command line to sh,
165 -- because system() under Windows doesn't look at SHELL, and always uses CMD.EXE)
166 kludgedSystem cmd phase_name
168 #ifndef mingw32_TARGET_OS
169 exit_code <- system cmd `catchAllIO`
170 (\_ -> throwDyn (PhaseFailed phase_name (ExitFailure 1)))
172 pid <- myGetProcessID
173 let tmp = "/tmp/sh" ++ show pid
174 h <- openFile tmp WriteMode
177 exit_code <- system ("sh - " ++ tmp) `catchAllIO`
178 (\_ -> removeFile tmp >>
179 throwDyn (PhaseFailed phase_name (ExitFailure 1)))