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