2014-04-05 17:29:28 +00:00
|
|
|
{- Simple line-based protocols.
|
|
|
|
-
|
2016-12-09 17:34:00 +00:00
|
|
|
- Copyright 2013-2016 Joey Hess <id@joeyh.name>
|
2014-04-05 17:29:28 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2014-04-05 17:29:28 +00:00
|
|
|
-}
|
|
|
|
|
2016-11-17 21:19:04 +00:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2014-04-05 17:29:28 +00:00
|
|
|
module Utility.SimpleProtocol (
|
|
|
|
Sendable(..),
|
|
|
|
Receivable(..),
|
|
|
|
parseMessage,
|
|
|
|
Serializable(..),
|
|
|
|
Parser,
|
|
|
|
parseFail,
|
|
|
|
parse0,
|
|
|
|
parse1,
|
|
|
|
parse2,
|
|
|
|
parse3,
|
2015-04-03 19:33:28 +00:00
|
|
|
dupIoHandles,
|
2016-12-09 17:34:00 +00:00
|
|
|
getProtocolLine,
|
2014-04-05 17:29:28 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.Char
|
2014-04-08 18:02:25 +00:00
|
|
|
import GHC.IO.Handle
|
2016-11-19 20:30:57 +00:00
|
|
|
import System.Exit (ExitCode(..))
|
2014-04-05 17:29:28 +00:00
|
|
|
|
2014-04-08 18:02:25 +00:00
|
|
|
import Common
|
2014-04-05 17:29:28 +00:00
|
|
|
|
|
|
|
-- Messages that can be sent.
|
|
|
|
class Sendable m where
|
|
|
|
formatMessage :: m -> [String]
|
|
|
|
|
|
|
|
-- Messages that can be received.
|
|
|
|
class Receivable m where
|
|
|
|
-- Passed the first word of the message, returns
|
|
|
|
-- a Parser that can be be fed the rest of the message to generate
|
|
|
|
-- the value.
|
|
|
|
parseCommand :: String -> Parser m
|
|
|
|
|
|
|
|
parseMessage :: (Receivable m) => String -> Maybe m
|
|
|
|
parseMessage s = parseCommand command rest
|
|
|
|
where
|
|
|
|
(command, rest) = splitWord s
|
|
|
|
|
|
|
|
class Serializable a where
|
|
|
|
serialize :: a -> String
|
|
|
|
deserialize :: String -> Maybe a
|
|
|
|
|
2016-12-09 17:34:00 +00:00
|
|
|
instance Serializable [Char] where
|
|
|
|
serialize = id
|
|
|
|
deserialize = Just
|
|
|
|
|
|
|
|
instance Serializable ExitCode where
|
|
|
|
serialize ExitSuccess = "0"
|
|
|
|
serialize (ExitFailure n) = show n
|
|
|
|
deserialize "0" = Just ExitSuccess
|
|
|
|
deserialize s = ExitFailure <$> readish s
|
|
|
|
|
2014-04-05 17:29:28 +00:00
|
|
|
{- Parsing the parameters of messages. Using the right parseN ensures
|
|
|
|
- that the string is split into exactly the requested number of words,
|
|
|
|
- which allows the last parameter of a message to contain arbitrary
|
|
|
|
- whitespace, etc, without needing any special quoting.
|
|
|
|
-}
|
|
|
|
type Parser a = String -> Maybe a
|
|
|
|
|
|
|
|
parseFail :: Parser a
|
|
|
|
parseFail _ = Nothing
|
|
|
|
|
|
|
|
parse0 :: a -> Parser a
|
|
|
|
parse0 mk "" = Just mk
|
|
|
|
parse0 _ _ = Nothing
|
|
|
|
|
|
|
|
parse1 :: Serializable p1 => (p1 -> a) -> Parser a
|
|
|
|
parse1 mk p1 = mk <$> deserialize p1
|
|
|
|
|
|
|
|
parse2 :: (Serializable p1, Serializable p2) => (p1 -> p2 -> a) -> Parser a
|
|
|
|
parse2 mk s = mk <$> deserialize p1 <*> deserialize p2
|
|
|
|
where
|
|
|
|
(p1, p2) = splitWord s
|
|
|
|
|
|
|
|
parse3 :: (Serializable p1, Serializable p2, Serializable p3) => (p1 -> p2 -> p3 -> a) -> Parser a
|
|
|
|
parse3 mk s = mk <$> deserialize p1 <*> deserialize p2 <*> deserialize p3
|
|
|
|
where
|
|
|
|
(p1, rest) = splitWord s
|
|
|
|
(p2, p3) = splitWord rest
|
|
|
|
|
|
|
|
splitWord :: String -> (String, String)
|
|
|
|
splitWord = separate isSpace
|
2014-04-08 18:02:25 +00:00
|
|
|
|
|
|
|
{- When a program speaks a simple protocol over stdio, any other output
|
|
|
|
- to stdout (or anything that attempts to read from stdin)
|
2015-10-12 20:32:52 +00:00
|
|
|
- will mess up the protocol. To avoid that, close stdin,
|
2014-04-08 18:02:25 +00:00
|
|
|
- and duplicate stderr to stdout. Return two new handles
|
|
|
|
- that are duplicates of the original (stdin, stdout). -}
|
2015-04-03 19:33:28 +00:00
|
|
|
dupIoHandles :: IO (Handle, Handle)
|
2015-04-03 20:48:30 +00:00
|
|
|
dupIoHandles = do
|
2014-04-08 18:02:25 +00:00
|
|
|
readh <- hDuplicate stdin
|
|
|
|
writeh <- hDuplicate stdout
|
|
|
|
nullh <- openFile devNull ReadMode
|
|
|
|
nullh `hDuplicateTo` stdin
|
|
|
|
stderr `hDuplicateTo` stdout
|
|
|
|
return (readh, writeh)
|
2016-11-17 21:19:04 +00:00
|
|
|
|
2016-12-09 17:34:00 +00:00
|
|
|
{- Reads a line, but to avoid super-long lines eating memory, returns
|
|
|
|
- Nothing if 32 kb have been read without seeing a '\n'
|
|
|
|
-
|
|
|
|
- If there is a '\r' before the '\n', it is removed, to support
|
|
|
|
- systems using "\r\n" at ends of lines
|
|
|
|
-
|
|
|
|
- This implementation is not super efficient, but as long as the Handle
|
|
|
|
- supports buffering, it avoids reading a character at a time at the
|
|
|
|
- syscall level.
|
|
|
|
-}
|
|
|
|
getProtocolLine :: Handle -> IO (Maybe String)
|
|
|
|
getProtocolLine h = go (32768 :: Int) []
|
|
|
|
where
|
|
|
|
go 0 _ = return Nothing
|
|
|
|
go n l = do
|
|
|
|
c <- hGetChar h
|
|
|
|
if c == '\n'
|
|
|
|
then return $ Just $ reverse $
|
|
|
|
case l of
|
|
|
|
('\r':rest) -> rest
|
|
|
|
_ -> l
|
|
|
|
else go (n-1) (c:l)
|