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