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