-- These take advantage of the fact that the port is always the last argument
--
-- withInput does an EOF check as well
withInput, withOutput :: Int
                      -> String
                      -> (Handle -> [ScmValue] -> IO (ErrOr ScmValue))
                      -> [ScmValue] -> IO (ErrOr ScmValue)
withInput idx s f = withPort stdin apply idx s f
 where
   apply (ScmInput h) xs = do
      eof <- hIsEOF h
      if eof
         then return$ Right ScmEOF
         else f h xs
   apply _            _  = return$ notInput s
withInput, withOutput :: Int
                      -> String
                      -> (Handle -> [ScmValue] -> IO (ErrOr ScmValue))
                      -> [ScmValue] -> IO (ErrOr ScmValue)
withInput idx s f = withPort stdin apply idx s f
 where
   apply (ScmInput h) xs = do
      eof <- hIsEOF h
      if eof
         then return$ Right ScmEOF
         else f h xs
   apply _            _  = return$ notInput s
withInput idx s f = withPort stdin apply idx s f
 where
   apply (ScmInput h) xs = do
      eof <- hIsEOF h
      if eof
         then return$ Right ScmEOF
         else f h xs
   apply _            _  = return$ notInput s