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