From fa436b19b25a6a908a78bc6874f1adf3a70ea8a9 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 31 Jul 2014 03:22:49 -0400 Subject: Fixed -c option to hosts utility --- Hosts.hs | 21 ++++++++++++++++----- hosts.hs | 4 +--- 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 @@ module Hosts ( Hosts , assignName + , assignName' , assignNewName , inet_pton , inet_ntop @@ -145,7 +146,7 @@ scrubName f line = line' in L.concat (a'++ws'') <> vs''' assignName :: SockAddr -> L.ByteString -> Hosts -> Hosts -assignName addr name hosts = assignName0 False addr name hosts +assignName addr name hosts = assignName' False addr name hosts chaddr :: Int -> SockAddr -> Hosts -> Hosts chaddr n addr hosts = @@ -177,14 +178,24 @@ scrubTrailingEmpties hosts = $ [cnt,cnt-1..] cnt' = cnt - length es -assignName0 :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts -assignName0 iscannon addr name hosts = hosts' +cannonizeName :: L.ByteString -> L.ByteString -> L.ByteString +cannonizeName name line = scrubName f line + where + f ws = [name," "] ++ pre ++ drop 2 rst + where + (pre,rst) = break (==name) ws + + +assignName' :: Bool -> SockAddr -> L.ByteString -> Hosts -> Hosts +assignName' iscannon addr name hosts = hosts' where ns = Map.lookup name (namenum hosts) a = Map.lookup addr (addrnum hosts) + canonize numline n = Map.adjust (cannonizeName name) n numline hosts' = do if (== Just True) $ elem <$> a <*> ns - then hosts -- address already has name, nothing to do + then if not iscannon then hosts -- address already has name, nothing to do + else hosts { numline = foldl' canonize (numline hosts) $ fromJust ns} else let hosts0 = -- remove name if it's present scrubTrailingEmpties $ maybe hosts (removeName hosts) ns @@ -219,7 +230,7 @@ assignName0 iscannon addr name hosts = hosts' assignNewName :: SockAddr -> L.ByteString -> Hosts -> Hosts assignNewName addr name hosts = if hasName name hosts then hosts - else assignName0 True addr name hosts + else assignName' True addr name hosts appendName :: Bool -> L.ByteString -> Hosts -> Int -> Hosts appendName 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 grokPairing :: Bool -> Hosts -> (String,Either String SockAddr) -> Hosts grokPairing bCanon hosts (name,eaddr) = case eaddr of - Right addr -> assign addr (L.pack name) hosts + Right addr -> assignName' bCanon addr (L.pack name) hosts Left "delete" -> error "deletion not implemented." - where - assign = if bCanon then assignNewName else assignName main = do args <- getArgs -- cgit v1.2.3