1 import Data.List ((\\), intercalate, intersperse)
2 import Data.Maybe (catMaybes, isNothing)
3 import System.Exit (ExitCode(..), exitWith)
4 import System.IO (stderr, hPutStrLn)
18 import CommandLine (help_set,
27 -- Some exit codes, used in the ExitFailure constructor.
28 exit_invalid_cidr :: Int
31 exit_args_parse_failed :: Int
32 exit_args_parse_failed = 2
35 -- A regular expression that matches a non-address character.
36 non_addr_char :: String
37 non_addr_char = "[^\\.0-9]"
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
43 addr_barrier :: String -> String
44 addr_barrier x = non_addr_char ++ x ++ non_addr_char
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:
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
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
59 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
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)
76 -- Take a list of Strings, and return a regular expression matching
78 alternate :: [String] -> String
79 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
82 -- Take two Ints as parameters, and return a regex matching any
83 -- integer between them (inclusive).
84 numeric_range :: Int -> Int -> String
86 alternate (map show [lower..upper])
94 -- First, check for any errors that occurred while parsing
95 -- the command line options.
96 errors <- CommandLine.parse_errors
99 hPutStrLn stderr (concat errors)
100 putStrLn CommandLine.help_text
101 exitWith (ExitFailure exit_args_parse_failed)
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
109 putStrLn CommandLine.help_text
113 -- The input function we receive here should know what to read.
114 inputfunc <- (CommandLine.input_function)
117 let cidr_strings = lines input
118 let cidrs = map cidr_from_string cidr_strings
120 if (any isNothing cidrs)
122 putStrLn "Error: not valid CIDR notation."
123 exitWith (ExitFailure exit_invalid_cidr)
126 -- Filter out only the valid ones.
127 let valid_cidrs = catMaybes cidrs
129 -- Get the mode of operation.
130 mode <- CommandLine.parse_mode
134 let regexes = map cidr_to_regex valid_cidrs
135 putStrLn $ alternate regexes
137 _ <- mapM (putStrLn . show) (combine_all valid_cidrs)
140 _ <- mapM (putStrLn . show) dupes
143 dupes = valid_cidrs \\ (combine_all valid_cidrs)
145 _ <- mapM putStrLn deletions
146 _ <- mapM putStrLn additions
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