This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
/
Options.hs
79 lines (69 loc) · 2.34 KB
/
Options.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{-# LANGUAGE CPP, DeriveDataTypeable #-}
-- | Command line options for the @scion-server@ binary.
module Scion.Server.Options
( ProxyMode(..), WireFormat(..),
parseArgs
)
where
import Data.Data
import Network.Socket ( PortNumber(..) )
import System.Console.CmdArgs
import System.IO ( stderr, hPutStrLn )
import System.Exit ( exitFailure )
data ProxyMode
= StdIO { wire_format :: WireFormat
, worker_path :: FilePath }
| Socket { port :: Int
, scan_port :: Bool
, wire_format :: WireFormat
, worker_path :: FilePath }
#ifndef mingw32_HOST_OS
| SocketFile { path :: FilePath
, wire_format :: WireFormat
, worker_path :: FilePath }
#endif
deriving (Eq, Show, Data, Typeable)
data WireFormat
= Json
| Lisp
| Vim
deriving (Eq, Show, Data, Typeable)
defaultPort :: Int
defaultPort = 4040 -- unassigned according to Wikipedia
stdioMode =
mode $ StdIO
{ wire_format = enum Json
[ Json &= text "Use JSON as wire format (default)"
, Lisp &= text "Use Lisp S-expressions as wire format"
, Vim &= text "Use a wire format readable by Vimscript"
]
, worker_path = def &= typFile & empty "scion-worker"
& text "Path to the scion-worker executable."
} &= prog "scion-server " & text "Communicate with server via stdio"
& defMode
socketMode =
mode $ Socket
{ port = defaultPort &= typ "PORT" & text "Use this port"
, scan_port = False &= text "Automatically scan for a free port"
, wire_format = enum Json
[ Json &= text "Use JSON as wire format (default)"
, Lisp &= text "Use Lisp S-expressions as wire format"
, Vim &= text "Use a wire format readable by Vimscript"
]
, worker_path = def &= typFile & empty "scion-worker"
& text "Path to the scion-worker executable."
} &= text "Communicate with server via TCP/IP."
modes = [stdioMode, socketMode]
verifyMode :: ProxyMode -> Maybe String
verifyMode Socket{ port = p }
| p >= 1 && p < 65535 = Nothing
| otherwise = Just "Invalid port number. Must be within 1..65535."
verifyMode _ = Nothing
parseArgs :: IO ProxyMode
parseArgs = do
mode <- cmdArgs "Scion Server v0.2" modes
case verifyMode mode of
Just msg -> do
hPutStrLn stderr msg
exitFailure
_ -> return mode