summaryrefslogtreecommitdiff
path: root/Hosts.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-07-31 03:22:49 -0400
committerjoe <joe@jerkface.net>2014-07-31 03:22:49 -0400
commitfa436b19b25a6a908a78bc6874f1adf3a70ea8a9 (patch)
treeabad65f8d0cb24d483762a0f7ae3ae8acda244a9 /Hosts.hs
parent77f46d6c08671d5e038e327cc5cd333df3aaf696 (diff)
Fixed -c option to hosts utility
Diffstat (limited to 'Hosts.hs')
-rw-r--r--Hosts.hs21
1 files changed, 16 insertions, 5 deletions
diff --git a/Hosts.hs b/Hosts.hs
index 29e54be..8d02cc3 100644
--- a/Hosts.hs
+++ b/Hosts.hs
@@ -7,6 +7,7 @@
7module Hosts 7module Hosts
8 ( Hosts 8 ( Hosts
9 , assignName 9 , assignName
10 , assignName'
10 , assignNewName 11 , assignNewName
11 , inet_pton 12 , inet_pton
12 , inet_ntop 13 , inet_ntop
@@ -145,7 +146,7 @@ scrubName f line = line'
145 in L.concat (a'++ws'') <> vs''' 146 in L.concat (a'++ws'') <> vs'''
146 147
147assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts 148assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts
148assignName addr name hosts = assignName0 False addr name hosts 149assignName addr name hosts = assignName' False addr name hosts
149 150
150chaddr :: Int -> SockAddr -> Hosts -> Hosts 151chaddr :: Int -> SockAddr -> Hosts -> Hosts
151chaddr n addr hosts = 152chaddr n addr hosts =
@@ -177,14 +178,24 @@ scrubTrailingEmpties hosts =
177 $ [cnt,cnt-1..] 178 $ [cnt,cnt-1..]
178 cnt' = cnt - length es 179 cnt' = cnt - length es
179 180
180assignName0 :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts 181cannonizeName :: L.ByteString -> L.ByteString -> L.ByteString
181assignName0 iscannon addr name hosts = hosts' 182cannonizeName name line = scrubName f line
183 where
184 f ws = [name," "] ++ pre ++ drop 2 rst
185 where
186 (pre,rst) = break (==name) ws
187
188
189assignName' :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts
190assignName' iscannon addr name hosts = hosts'
182 where 191 where
183 ns = Map.lookup name (namenum hosts) 192 ns = Map.lookup name (namenum hosts)
184 a = Map.lookup addr (addrnum hosts) 193 a = Map.lookup addr (addrnum hosts)
194 canonize numline n = Map.adjust (cannonizeName name) n numline
185 hosts' = do 195 hosts' = do
186 if (== Just True) $ elem <$> a <*> ns 196 if (== Just True) $ elem <$> a <*> ns
187 then hosts -- address already has name, nothing to do 197 then if not iscannon then hosts -- address already has name, nothing to do
198 else hosts { numline = foldl' canonize (numline hosts) $ fromJust ns}
188 else 199 else
189 let hosts0 = -- remove name if it's present 200 let hosts0 = -- remove name if it's present
190 scrubTrailingEmpties $ maybe hosts (removeName hosts) ns 201 scrubTrailingEmpties $ maybe hosts (removeName hosts) ns
@@ -219,7 +230,7 @@ assignName0 iscannon addr name hosts = hosts'
219assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts 230assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts
220assignNewName addr name hosts = 231assignNewName addr name hosts =
221 if hasName name hosts then hosts 232 if hasName name hosts then hosts
222 else assignName0 True addr name hosts 233 else assignName' True addr name hosts
223 234
224appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts 235appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts
225appendName iscannon name hosts num = hosts 236appendName iscannon name hosts num = hosts