diff options
Diffstat (limited to 'exec')
-rw-r--r-- | exec/ebus-dump-layer2.hs | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/exec/ebus-dump-layer2.hs b/exec/ebus-dump-layer2.hs index 37a7bbe..06ffc6f 100644 --- a/exec/ebus-dump-layer2.hs +++ b/exec/ebus-dump-layer2.hs @@ -1,10 +1,10 @@ module Main(main) where import Control.Exception -import Data.Enumerator (Enumerator, Iteratee, run, ($$)) -import Data.Enumerator.Binary (enumHandle) -import Data.ByteString (ByteString) import Data.Attoparsec.Enumerator (iterParser, ParseError, errorContexts) +import Data.ByteString (ByteString,pack) +import Data.Enumerator (Enumerator, Iteratee, enumLists, run, ($$)) +import Data.Enumerator.Binary (enumHandle) import System.Exit import System.IO (hSetBinaryMode,stdin) @@ -15,24 +15,23 @@ main = do -- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.) hSetBinaryMode stdin True loop 900 + where loop :: Int -> IO() + loop 0 = do + print "finished" + loop n = do + readAndDumpPacket + loop $ n - 1 -loop :: Int -> IO() -loop 0 = do - print "finished" -loop n = do - readAndDumpPacket - loop (n - 1) - - +-- | Read one eBus Packet and dump layer2 to stdout 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 ) + maybePacket <- run( stdinEnumerator $$ parserIteratee) case maybePacket of - Right result -> do { print "Result"; print result} + Right result -> do { print "Result"; print $ result} Left exc -> case fromException (exc::SomeException) of Just e | any (\ctx -> ctx == "demandInput") (errorContexts (e::ParseError)) -> do @@ -41,10 +40,12 @@ readAndDumpPacket = do | otherwise -> do putStr "Other Error: "; print e _ -> putStr "" -enumSource :: Enumerator ByteString IO a -enumSource = enumHandle 1 stdin +stdinEnumerator :: Enumerator ByteString IO a +stdinEnumerator = enumHandle 1 stdin -runParser :: Iteratee ByteString IO EbusPacket -runParser = do +parserIteratee :: Iteratee ByteString IO EbusPacket +parserIteratee = do p <- iterParser ebusParserLayer2 return p + + |