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