diff options
author | joe <joe@jerkface.net> | 2014-07-31 03:22:49 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-07-31 03:22:49 -0400 |
commit | fa436b19b25a6a908a78bc6874f1adf3a70ea8a9 (patch) | |
tree | abad65f8d0cb24d483762a0f7ae3ae8acda244a9 | |
parent | 77f46d6c08671d5e038e327cc5cd333df3aaf696 (diff) |
Fixed -c option to hosts utility
-rw-r--r-- | Hosts.hs | 21 | ||||
-rw-r--r-- | hosts.hs | 4 |
2 files changed, 17 insertions, 8 deletions
@@ -7,6 +7,7 @@ | |||
7 | module Hosts | 7 | module 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 | ||
147 | assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts | 148 | assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts |
148 | assignName addr name hosts = assignName0 False addr name hosts | 149 | assignName addr name hosts = assignName' False addr name hosts |
149 | 150 | ||
150 | chaddr :: Int -> SockAddr -> Hosts -> Hosts | 151 | chaddr :: Int -> SockAddr -> Hosts -> Hosts |
151 | chaddr n addr hosts = | 152 | chaddr 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 | ||
180 | assignName0 :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts | 181 | cannonizeName :: L.ByteString -> L.ByteString -> L.ByteString |
181 | assignName0 iscannon addr name hosts = hosts' | 182 | cannonizeName 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 | |||
189 | assignName' :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts | ||
190 | assignName' 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' | |||
219 | assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts | 230 | assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts |
220 | assignNewName addr name hosts = | 231 | assignNewName 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 | ||
224 | appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts | 235 | appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts |
225 | appendName iscannon name hosts num = hosts | 236 | appendName iscannon name hosts num = hosts |
@@ -64,10 +64,8 @@ crap strs = do | |||
64 | grokPairing :: Bool -> Hosts -> (String,Either String SockAddr) -> Hosts | 64 | grokPairing :: Bool -> Hosts -> (String,Either String SockAddr) -> Hosts |
65 | grokPairing bCanon hosts (name,eaddr) = | 65 | grokPairing 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 | ||
72 | main = do | 70 | main = do |
73 | args <- getArgs | 71 | args <- getArgs |