summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-04-21 22:09:55 -0400
committerjoe <joe@jerkface.net>2014-04-21 22:09:55 -0400
commitbbb38d5e54664ced895a6e4dcfcc289f2a9afef4 (patch)
tree06ebb65e70d8b93002f230f4a2cd521a0e1625e4
parenta3dae3710e1d7578301b0abbb1a0fd9db4476f7e (diff)
type signatures for Hosts module
-rw-r--r--Hosts.hs16
1 files changed, 13 insertions, 3 deletions
diff --git a/Hosts.hs b/Hosts.hs
index ee4d154..5b117c7 100644
--- a/Hosts.hs
+++ b/Hosts.hs
@@ -30,17 +30,15 @@ import qualified Data.Map as Map
30import Data.Map (Map) 30import Data.Map (Map)
31import qualified Data.ByteString.Lazy.Char8 as L 31import qualified Data.ByteString.Lazy.Char8 as L
32import System.IO.Unsafe (unsafePerformIO) 32import System.IO.Unsafe (unsafePerformIO)
33import Control.Exception as Exception (IOException(..),catch)
34import Control.Applicative ( (<$>), (<*>) ) 33import Control.Applicative ( (<$>), (<*>) )
35import Control.Monad (mplus) 34import Control.Monad (mplus)
36import Network.Socket 35import Network.Socket
36import ControlMaybe ( handleIO_ )
37 37
38#if ! MIN_VERSION_network(2,4,0) 38#if ! MIN_VERSION_network(2,4,0)
39deriving instance Ord SockAddr 39deriving instance Ord SockAddr
40#endif 40#endif
41 41
42handleIO_ h a = Exception.catch a (\(_ :: IOException) -> h)
43
44inet_pton :: String -> Maybe SockAddr 42inet_pton :: String -> Maybe SockAddr
45inet_pton p = n 43inet_pton p = n
46 where 44 where
@@ -68,8 +66,10 @@ data Hosts = Hosts
68instance Show Hosts where 66instance Show Hosts where
69 show = L.unpack . encode 67 show = L.unpack . encode
70 68
69encode :: Hosts -> L.ByteString
71encode = L.unlines . map snd . Map.assocs . numline 70encode = L.unlines . map snd . Map.assocs . numline
72 71
72parseLine :: L.ByteString -> (Maybe SockAddr, [L.ByteString])
73parseLine s = (addr,names) 73parseLine s = (addr,names)
74 where 74 where
75 (addr0,names) = splitAt 1 $ L.words (uncom s) 75 (addr0,names) = splitAt 1 $ L.words (uncom s)
@@ -80,6 +80,7 @@ parseLine s = (addr,names)
80 80
81 uncom s = fst $ L.break (=='#') s 81 uncom s = fst $ L.break (=='#') s
82 82
83empty :: Hosts
83empty = Hosts { lineCount = 0 84empty = Hosts { lineCount = 0
84 , numline = Map.empty 85 , numline = Map.empty
85 , addrnum = Map.empty 86 , addrnum = Map.empty
@@ -92,6 +93,7 @@ parseHosts fname = do
92 return $ decode input 93 return $ decode input
93-} 94-}
94 95
96decode :: L.ByteString -> Hosts
95decode input = 97decode input =
96 let ls = L.lines input 98 let ls = L.lines input
97 ans = map (\l->(parseLine l,l)) ls 99 ans = map (\l->(parseLine l,l)) ls
@@ -118,6 +120,8 @@ hasName name hosts = Map.member name $ namenum hosts
118hasAddr :: SockAddr -> Hosts -> Bool 120hasAddr :: SockAddr -> Hosts -> Bool
119hasAddr addr hosts = Map.member addr $ addrnum hosts 121hasAddr addr hosts = Map.member addr $ addrnum hosts
120 122
123scrubName ::
124 ([L.ByteString] -> [L.ByteString]) -> L.ByteString -> L.ByteString
121scrubName f line = line' 125scrubName f line = line'
122 where 126 where
123 (x,ign) = L.break (=='#') line 127 (x,ign) = L.break (=='#') line
@@ -140,8 +144,10 @@ scrubName f line = line'
140 else "# " <> vs'' 144 else "# " <> vs''
141 in L.concat (a'++ws'') <> vs''' 145 in L.concat (a'++ws'') <> vs'''
142 146
147assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts
143assignName addr name hosts = assignName0 False addr name hosts 148assignName addr name hosts = assignName0 False addr name hosts
144 149
150assignName0 :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts
145assignName0 iscannon addr name hosts = hosts' 151assignName0 iscannon addr name hosts = hosts'
146 where 152 where
147 ns = Map.lookup name (namenum hosts) 153 ns = Map.lookup name (namenum hosts)
@@ -170,10 +176,12 @@ assignName0 iscannon addr name hosts = hosts'
170 line = L.pack (inet_ntop addr) <> " " <> name 176 line = L.pack (inet_ntop addr) <> " " <> name
171 cons v xs = Just $ maybe [v] (v:) xs 177 cons v xs = Just $ maybe [v] (v:) xs
172 178
179assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts
173assignNewName addr name hosts = 180assignNewName addr name hosts =
174 if hasName name hosts then hosts 181 if hasName name hosts then hosts
175 else assignName0 True addr name hosts 182 else assignName0 True addr name hosts
176 183
184appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts
177appendName iscannon name hosts num = hosts 185appendName iscannon name hosts num = hosts
178 { numline = Map.adjust (scrubName f) num (numline hosts) 186 { numline = Map.adjust (scrubName f) num (numline hosts)
179 , namenum = Map.alter (cons num) name (namenum hosts) 187 , namenum = Map.alter (cons num) name (namenum hosts)
@@ -204,6 +212,7 @@ diff as bs = cs
204namesForAddress :: SockAddr -> Hosts -> [L.ByteString] 212namesForAddress :: SockAddr -> Hosts -> [L.ByteString]
205namesForAddress addr hosts = snd $ _namesForAddress addr hosts 213namesForAddress addr hosts = snd $ _namesForAddress addr hosts
206 214
215_namesForAddress :: SockAddr -> Hosts -> (Int, [L.ByteString])
207_namesForAddress addr (Hosts {numline=numline, addrnum=addrnum}) = ns 216_namesForAddress addr (Hosts {numline=numline, addrnum=addrnum}) = ns
208 where 217 where
209 ns = maybe (-1,[]) id $ do 218 ns = maybe (-1,[]) id $ do
@@ -212,6 +221,7 @@ _namesForAddress addr (Hosts {numline=numline, addrnum=addrnum}) = ns
212 return (n, snd $ parseLine line) 221 return (n, snd $ parseLine line)
213 222
214 223
224plus :: Hosts -> Hosts -> Hosts
215plus a b = Map.foldlWithKey' mergeAddr a (addrnum b) 225plus a b = Map.foldlWithKey' mergeAddr a (addrnum b)
216 where 226 where
217 mergeAddr a addr bnum = a' 227 mergeAddr a addr bnum = a'