+getErrType :: IO Int
+getErrType = _ccall_ getErrType__
+
+slurpFileExpandTabs :: FilePath -> IO (Addr,Int)
+slurpFileExpandTabs fname = do
+ bracket (openFile fname ReadMode) (hClose)
+ (\ handle ->
+ do sz <- hFileSize handle
+ if sz > toInteger (maxBound::Int)
+ then ioError (userError "slurpFile: file too big")
+ else do
+ let sz_i = fromInteger sz
+ if sz_i == 0
+ -- empty file: just allocate a buffer containing '\0'
+ then do chunk <- allocMem 1
+ writeCharOffAddr chunk 0 '\0'
+ return (chunk, 0)
+ else do let sz_i' = (sz_i * 12) `div` 10 -- add 20% for tabs
+ chunk <- allocMem sz_i'
+ trySlurp handle sz_i' chunk
+ )
+
+trySlurp :: Handle -> Int -> Addr -> IO (Addr, Int)
+trySlurp handle sz_i chunk =
+#if __GLASGOW_HASKELL__ < 501
+ wantReadableHandle "hGetChar" handle $ \ handle_ ->
+ let fo = haFO__ handle_ in
+#else
+ wantReadableHandle "hGetChar" handle $
+ \ handle_@Handle__{ haFD=fd, haBuffer=ref, haBufferMode=mode } ->
+#endif
+ let
+ (I# chunk_sz) = sz_i
+
+ tAB_SIZE = 8#
+
+ slurpFile :: Int# -> Int# -> Addr -> Int# -> Int# -> IO (Addr, Int)
+ slurpFile c off chunk chunk_sz max_off = slurp c off
+ where
+
+ slurp :: Int# -> Int# -> IO (Addr, Int)
+ slurp c off | off >=# max_off = do
+ let new_sz = chunk_sz *# 2#
+ chunk' <- reAllocMem chunk (I# new_sz)
+ slurpFile c off chunk' new_sz (new_sz -# (tAB_SIZE +# 1#))
+ slurp c off = do
+#if __GLASGOW_HASKELL__ < 501
+ intc <- mayBlock fo (_ccall_ fileGetc fo)
+ if intc == ((-1)::Int)
+ then do errtype <- getErrType
+ if errtype == (19{-ERR_EOF-} :: Int)
+ then return (chunk, I# off)
+ else constructErrorAndFail "slurpFile"
+ else case chr intc of
+#else
+ buf <- readIORef ref
+ ch <- (if not (bufferEmpty buf)
+ then hGetcBuffered fd ref buf
+ else do
+#if __GLASGOW_HASKELL__ >= 503
+ new_buf <- fillReadBuffer fd True False buf
+#else
+ new_buf <- fillReadBuffer fd True buf
+#endif
+ hGetcBuffered fd ref new_buf)
+ `catch` \e -> if isEOFError e
+ then return '\xFFFF'
+ else ioError e
+ case ch of
+ '\xFFFF' -> return (chunk, I# off)
+#endif
+ '\t' -> tabIt c off
+ ch -> do writeCharOffAddr chunk (I# off) ch
+ let c' | ch == '\n' = 0#
+ | otherwise = c +# 1#
+ slurp c' (off +# 1#)
+
+ tabIt :: Int# -> Int# -> IO (Addr, Int)
+ -- can't run out of buffer in here, because we reserved an
+ -- extra tAB_SIZE bytes at the end earlier.
+ tabIt c off = do
+ writeCharOffAddr chunk (I# off) ' '
+ let c' = c +# 1#
+ off' = off +# 1#
+ if c' `remInt#` tAB_SIZE ==# 0#
+ then slurp c' off'
+ else tabIt c' off'
+ in do
+
+ -- allow space for a full tab at the end of the buffer
+ -- (that's what the max_off thing is for),
+ -- and add 1 to allow room for the final sentinel \NUL at
+ -- the end of the file.
+ (chunk', rc) <- slurpFile 0# 0# chunk chunk_sz (chunk_sz -# (tAB_SIZE +# 1#))
+#if __GLASGOW_HASKELL__ < 404
+ writeHandle handle handle_
+#endif
+ return (chunk', rc+1 {- room for sentinel -})
+
+
+reAllocMem :: Addr -> Int -> IO Addr
+reAllocMem ptr sz = do
+ chunk <- _ccall_ realloc ptr sz
+ if chunk == nullAddr
+ then fail "reAllocMem"
+ else return chunk
+
+allocMem :: Int -> IO Addr
+allocMem sz = do
+ chunk <- _ccall_ malloc sz
+ if chunk == nullAddr
+#if __GLASGOW_HASKELL__ < 501
+ then constructErrorAndFail "allocMem"
+#else
+ then ioException (IOError Nothing ResourceExhausted "malloc"
+ "out of memory" Nothing)
+#endif
+ else return chunk
+\end{code}
+
+Lookup