]> gitweb.michael.orlitzky.com - hath.git/blob - src/Main.hs
Add full lists for every existing import.
[hath.git] / src / Main.hs
1 module Main
2 where
3
4 import Control.Monad (when)
5 import Data.List ((\\), intercalate)
6 import Data.Maybe (catMaybes, isNothing)
7 import Data.String.Utils (splitWs)
8 import System.Exit (ExitCode( ExitFailure ), exitWith)
9 import System.IO (stderr, hPutStrLn)
10 import Text.Read (readMaybe)
11
12 import Cidr (
13 Cidr(),
14 combine_all,
15 enumerate,
16 max_octet1,
17 max_octet2,
18 max_octet3,
19 max_octet4,
20 min_octet1,
21 min_octet2,
22 min_octet3,
23 min_octet4 )
24 import CommandLine(
25 Args( Regexed, Reduced, Duped, Diffed, Listed, barriers ),
26 get_args )
27 import ExitCodes ( exit_invalid_cidr )
28 import Octet ()
29
30
31 -- | A regular expression that matches a non-address character.
32 --
33 non_addr_char :: String
34 non_addr_char = "[^\\.0-9]"
35
36
37 -- | Add non_addr_chars on either side of the given String. This
38 -- prevents (for example) the regex '127.0.0.1' from matching
39 -- '127.0.0.100'.
40 --
41 add_barriers :: String -> String
42 add_barriers x = non_addr_char ++ x ++ non_addr_char
43
44
45 -- | The magic happens here. We take a CIDR String as an argument, and
46 -- return the equivalent regular expression. We do this as follows:
47 --
48 -- 1. Compute the minimum possible value of each octet.
49 -- 2. Compute the maximum possible value of each octet.
50 -- 3. Generate a regex matching every value between those min and
51 -- max values.
52 -- 4. Join the regexes from step 3 with regexes matching periods.
53 -- 5. Stick an address boundary on either side of the result if
54 -- use_barriers is True.
55 --
56 cidr_to_regex :: Bool -> Cidr -> String
57 cidr_to_regex use_barriers cidr =
58 let f = if use_barriers then add_barriers else id in
59 f (intercalate "\\." [range1, range2, range3, range4])
60 where
61 range1 = numeric_range min1 max1
62 range2 = numeric_range min2 max2
63 range3 = numeric_range min3 max3
64 range4 = numeric_range min4 max4
65 min1 = fromEnum (min_octet1 cidr)
66 min2 = fromEnum (min_octet2 cidr)
67 min3 = fromEnum (min_octet3 cidr)
68 min4 = fromEnum (min_octet4 cidr)
69 max1 = fromEnum (max_octet1 cidr)
70 max2 = fromEnum (max_octet2 cidr)
71 max3 = fromEnum (max_octet3 cidr)
72 max4 = fromEnum (max_octet4 cidr)
73
74
75
76 -- | Take a list of Strings, and return a regular expression matching
77 -- any of them.
78 --
79 alternate :: [String] -> String
80 alternate terms = "(" ++ (intercalate "|" terms) ++ ")"
81
82
83 -- | Take two Ints as parameters, and return a regex matching any
84 -- integer between them (inclusive).
85 --
86 -- IMPORTANT: we match from max to min so that if e.g. the last
87 -- octet is '255', we want '255' to match before '2' in the regex
88 -- (255|254|...|3|2|1) which does not happen if we use
89 -- (1|2|3|...|254|255).
90 --
91 numeric_range :: Int -> Int -> String
92 numeric_range x y =
93 alternate (map show $ reverse [lower..upper])
94 where
95 lower = minimum [x,y]
96 upper = maximum [x,y]
97
98
99 main :: IO ()
100 main = do
101 args <- get_args
102
103 -- This reads stdin.
104 input <- getContents
105
106 let cidr_strings = splitWs input
107 let cidrs = map readMaybe cidr_strings :: [Maybe Cidr]
108
109 when (any isNothing cidrs) $ do
110 hPutStrLn stderr "ERROR: not valid CIDR notation:"
111
112 -- Output the bad lines, safely.
113 let pairs = zip cidr_strings cidrs
114
115 let print_pair :: (String, Maybe Cidr) -> IO ()
116 print_pair (x, Nothing) = hPutStrLn stderr (" * " ++ x)
117 print_pair (_, _) = return ()
118
119 mapM_ print_pair pairs
120 exitWith (ExitFailure exit_invalid_cidr)
121
122 -- Filter out only the valid ones.
123 let valid_cidrs = catMaybes cidrs
124
125 case args of
126 Regexed{} -> do
127 let cidrs' = combine_all valid_cidrs
128 let regexes = map (cidr_to_regex (barriers args)) cidrs'
129 putStrLn $ alternate regexes
130 Reduced{} ->
131 mapM_ print (combine_all valid_cidrs)
132 Duped{} ->
133 mapM_ print dupes
134 where
135 dupes = valid_cidrs \\ (combine_all valid_cidrs)
136 Diffed{} -> do
137 mapM_ putStrLn deletions
138 mapM_ putStrLn additions
139 where
140 dupes = valid_cidrs \\ (combine_all valid_cidrs)
141 deletions = map (\s -> '-' : (show s)) dupes
142 newcidrs = (combine_all valid_cidrs) \\ valid_cidrs
143 additions = map (\s -> '+' : (show s)) newcidrs
144 Listed{} -> do
145 let combined_cidrs = combine_all valid_cidrs
146 let addrs = concatMap enumerate combined_cidrs
147 mapM_ print addrs