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