diff options
author | Yves Fischer <yvesf-git@xapek.org> | 2012-03-26 21:59:05 +0200 |
---|---|---|
committer | Yves Fischer <yvesf-git@xapek.org> | 2012-03-26 21:59:05 +0200 |
commit | 1270abb4ce6842d3b66f792d23358b97a530c4d5 (patch) | |
tree | 4600b6c6ffb819a115e70e9f93196fe394c4c150 | |
parent | 3ca3be30a4593eeeef4fefd7053eb7c4b9396fcd (diff) | |
download | haskell-ebus-1270abb4ce6842d3b66f792d23358b97a530c4d5.tar.gz haskell-ebus-1270abb4ce6842d3b66f792d23358b97a530c4d5.zip |
dump layer2 cli util
-rw-r--r-- | exec/ebus-dump-layer2.hs | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/exec/ebus-dump-layer2.hs b/exec/ebus-dump-layer2.hs index c8fd1b5..37a7bbe 100644 --- a/exec/ebus-dump-layer2.hs +++ b/exec/ebus-dump-layer2.hs @@ -1,21 +1,20 @@ module Main(main) where -import Network.EBus.Layer2 +import Control.Exception import Data.Enumerator (Enumerator, Iteratee, run, ($$)) import Data.Enumerator.Binary (enumHandle) -import System.IO (hSetBinaryMode,stdin) import Data.ByteString (ByteString) -import Data.Attoparsec.Enumerator (iterParser) +import Data.Attoparsec.Enumerator (iterParser, ParseError, errorContexts) +import System.Exit +import System.IO (hSetBinaryMode,stdin) + +import Network.EBus.Layer2 +main :: IO () main = do -- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.) hSetBinaryMode stdin True - -- * run - -- Run an iteratee until it finishes, and return either the final value (if it succeeded) or the error (if it failed). - -- * run_ - -- Like run, except errors are converted to exceptions and thrown. Primarily useful for small scripts or other simple cases. - - loop 100 + loop 900 loop :: Int -> IO() loop 0 = do @@ -24,13 +23,23 @@ loop n = do readAndDumpPacket loop (n - 1) + readAndDumpPacket :: IO() readAndDumpPacket = do + -- * run + -- Run an iteratee until it finishes, and return either the final value (if it succeeded) or the error (if it failed). + -- * run_ + -- Like run, except errors are converted to exceptions and thrown. Primarily useful for small scripts or other simple cases. maybePacket <- run( enumSource $$ runParser ) case maybePacket of - Right result -> print result - Left error -> print error - + Right result -> do { print "Result"; print result} + Left exc -> case fromException (exc::SomeException) of + Just e + | any (\ctx -> ctx == "demandInput") (errorContexts (e::ParseError)) -> do + putStrLn "EOF reached" + exitWith $ ExitFailure 1 + | otherwise -> do putStr "Other Error: "; print e + _ -> putStr "" enumSource :: Enumerator ByteString IO a enumSource = enumHandle 1 stdin |