summaryrefslogtreecommitdiff
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
parent77f46d6c08671d5e038e327cc5cd333df3aaf696 (diff)
Fixed -c option to hosts utility
-rw-r--r--Hosts.hs21
-rw-r--r--hosts.hs4
2 files changed, 17 insertions, 8 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
diff --git a/hosts.hs b/hosts.hs
index afd60fc..6c93b10 100644
--- a/hosts.hs
+++ b/hosts.hs
@@ -64,10 +64,8 @@ crap strs = do
64grokPairing :: Bool -> Hosts -> (String,Either String SockAddr) -> Hosts 64grokPairing :: Bool -> Hosts -> (String,Either String SockAddr) -> Hosts
65grokPairing bCanon hosts (name,eaddr) = 65grokPairing bCanon hosts (name,eaddr) =
66 case eaddr of 66 case eaddr of
67 Right addr -> assign addr (L.pack name) hosts 67 Right addr -> assignName' bCanon addr (L.pack name) hosts
68 Left "delete" -> error "deletion not implemented." 68 Left "delete" -> error "deletion not implemented."
69 where
70 assign = if bCanon then assignNewName else assignName
71 69
72main = do 70main = do
73 args <- getArgs 71 args <- getArgs