]> gitweb.michael.orlitzky.com - hath.git/blob - src/Main.hs
25413cec9fdf024632911ac8e9a1dd169da83753
[hath.git] / src / Main.hs
1 import Data.List ((\\), intercalate, intersperse)
2 import System.Exit (ExitCode(..), exitWith)
3 import System.IO (stderr, hPutStrLn)
4
5 import Cidr (Cidr(..),
6 cidr_from_string,
7 combine_all,
8 max_octet1,
9 max_octet2,
10 max_octet3,
11 max_octet4,
12 min_octet1,
13 min_octet2,
14 min_octet3,
15 min_octet4 )
16
17 import CommandLine (help_set,
18 help_text,
19 input_function,
20 Mode(..),
21 parse_errors,
22 parse_mode)
23
24 import Octet
25
26 -- Some exit codes, used in the ExitFailure constructor.
27 exit_invalid_cidr :: Int
28 exit_invalid_cidr = 1
29
30 exit_args_parse_failed :: Int
31 exit_args_parse_failed = 2
32
33
34 -- A regular expression that matches a non-address character.
35 non_addr_char :: String
36 non_addr_char = "[^\\.0-9]"
37
38
39 -- Add non_addr_chars on either side of the given String. This
40 -- prevents (for example) the regex '127.0.0.1' from matching
41 -- '127.0.0.100'.
42 addr_barrier :: String -> String
43 addr_barrier 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.
55 --cidr_to_regex :: String -> String
56 cidr_to_regex :: Cidr.Cidr -> String
57 cidr_to_regex cidr =
58 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
59 where
60 range1 = numeric_range min1 max1
61 range2 = numeric_range min2 max2
62 range3 = numeric_range min3 max3
63 range4 = numeric_range min4 max4
64 min1 = octet_to_int (min_octet1 cidr)
65 min2 = octet_to_int (min_octet2 cidr)
66 min3 = octet_to_int (min_octet3 cidr)
67 min4 = octet_to_int (min_octet4 cidr)
68 max1 = octet_to_int (max_octet1 cidr)
69 max2 = octet_to_int (max_octet2 cidr)
70 max3 = octet_to_int (max_octet3 cidr)
71 max4 = octet_to_int (max_octet4 cidr)
72
73
74
75 -- Take a list of Strings, and return a regular expression matching
76 -- any of them.
77 alternate :: [String] -> String
78 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
79
80
81 -- Take two Ints as parameters, and return a regex matching any
82 -- integer between them (inclusive).
83 numeric_range :: Int -> Int -> String
84 numeric_range x y =
85 alternate (map show [lower..upper])
86 where
87 lower = minimum [x,y]
88 upper = maximum [x,y]
89
90
91 main :: IO ()
92 main = do
93 -- First, check for any errors that occurred while parsing
94 -- the command line options.
95 errors <- CommandLine.parse_errors
96 if not (null errors)
97 then do
98 hPutStrLn stderr (concat errors)
99 putStrLn CommandLine.help_text
100 exitWith (ExitFailure exit_args_parse_failed)
101 else do -- Nothing
102
103 -- Next, check to see if the 'help' option was passed to the
104 -- program. If it was, display the help, and exit successfully.
105 help_opt_set <- CommandLine.help_set
106 if help_opt_set
107 then do
108 putStrLn CommandLine.help_text
109 exitWith ExitSuccess
110 else do -- Nothing
111
112 -- The input function we receive here should know what to read.
113 inputfunc <- (CommandLine.input_function)
114 input <- inputfunc
115
116 let cidr_strings = lines input
117 let cidrs = map Cidr.cidr_from_string cidr_strings
118
119 if (any (== Cidr.None) cidrs)
120 then do
121 putStrLn "Error: not valid CIDR notation."
122 exitWith (ExitFailure exit_invalid_cidr)
123 else do -- Nothing
124
125 -- Get the mode of operation.
126 mode <- CommandLine.parse_mode
127
128 case mode of
129 Regex -> do
130 let regexes = map cidr_to_regex cidrs
131 putStrLn $ alternate regexes
132 Reduce -> do
133 mapM (putStrLn . show) (combine_all cidrs)
134 return ()
135 Dupe -> do
136 mapM (putStrLn . show) dupes
137 return ()
138 where
139 dupes = cidrs \\ (combine_all cidrs)
140 Diff -> do
141 mapM putStrLn deletions
142 mapM putStrLn additions
143 return ()
144 where
145 dupes = cidrs \\ (combine_all cidrs)
146 deletions = map (\s -> "-" ++ (show s)) dupes
147 newcidrs = (combine_all cidrs) \\ cidrs
148 additions = map (\s -> "+" ++ (show s)) newcidrs