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