summaryrefslogtreecommitdiff
path: root/Hosts.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Hosts.hs')
-rw-r--r--Hosts.hs38
1 files changed, 26 insertions, 12 deletions
diff --git a/Hosts.hs b/Hosts.hs
index 4f8788f..669fd09 100644
--- a/Hosts.hs
+++ b/Hosts.hs
@@ -9,6 +9,9 @@ module Hosts
9 , empty 9 , empty
10 , hasName 10 , hasName
11 , hasAddr 11 , hasAddr
12 , encode
13 , decode
14 , diff
12 ) where 15 ) where
13 16
14import Data.Maybe 17import Data.Maybe
@@ -22,6 +25,7 @@ import qualified Data.ByteString.Lazy.Char8 as L
22import System.IO.Unsafe (unsafePerformIO) 25import System.IO.Unsafe (unsafePerformIO)
23import Control.Exception as Exception (IOException(..),catch) 26import Control.Exception as Exception (IOException(..),catch)
24import Control.Applicative ( (<$>), (<*>) ) 27import Control.Applicative ( (<$>), (<*>) )
28import Control.Monad (mplus)
25import Network.Socket 29import Network.Socket
26 30
27handleIO_ h a = Exception.catch a (\(_ :: IOException) -> h) 31handleIO_ h a = Exception.catch a (\(_ :: IOException) -> h)
@@ -51,7 +55,9 @@ data Hosts = Hosts
51 } 55 }
52 56
53instance Show Hosts where 57instance Show Hosts where
54 show hosts = L.unpack . L.unlines . map snd . Map.assocs $ numline hosts 58 show = L.unpack . encode
59
60encode = L.unlines . map snd . Map.assocs . numline
55 61
56parseLine s = (addr,names) 62parseLine s = (addr,names)
57 where 63 where
@@ -71,6 +77,9 @@ empty = Hosts { lineCount = 0
71 77
72parseHosts fname = do 78parseHosts fname = do
73 input <- L.readFile fname 79 input <- L.readFile fname
80 decode input
81
82decode input = do
74 let ls = L.lines input 83 let ls = L.lines input
75 ans = map (\l->(parseLine l,l)) ls 84 ans = map (\l->(parseLine l,l)) ls
76 hosts = foldl' upd empty ans 85 hosts = foldl' upd empty ans
@@ -150,14 +159,19 @@ assignNewName addr name hosts =
150 if hasName name hosts then hosts 159 if hasName name hosts then hosts
151 else assignName0 True addr name hosts 160 else assignName0 True addr name hosts
152 161
153{- 162-- Returns a list of bytestrings intended to show the
154main = do 163-- differences between the two host databases. It is
155 args <- getArgs 164-- assumed that no lines are deleted, only altered or
156 let fname = args !! 0 165-- appended.
157 p <- parseHosts fname 166diff :: Hosts -> Hosts -> [L.ByteString]
158 let addr = (fromJust $ inet_pton "fdee:0abe:1f80:31c7:d1af:bce0:0f6c:91d2") 167diff as bs = cs
159 p' = assignName addr "bigshift" p 168 where
160 p'' = assignNewName addr "poopy" p' 169 [as',bs'] = map (L.lines . Hosts.encode) [as,bs]
161 putStr $ show p'' 170 ext xs = map Just xs ++ repeat Nothing
162 return () 171 ds = takeWhile (isJust . uncurry mplus) $ zip (ext as') (ext bs')
163-} 172 es = filter (uncurry (/=)) ds
173 cs = do
174 (a,b) <- es
175 [a,b] <- return $ map maybeToList [a,b]
176 fmap ("- " <>) a ++ fmap ("+ " <>) b
177