1 import Data.List (intercalate, intersperse)
2 import System.Exit (ExitCode(..), exitWith)
3 import System.IO (stderr, hPutStrLn)
8 import CommandLine (help_set,
18 -- Some exit codes, used in the ExitFailure constructor.
19 exit_invalid_cidr :: Int
22 exit_args_parse_failed :: Int
23 exit_args_parse_failed = 2
26 -- A regular expression that matches a non-address character.
27 non_addr_char :: String
28 non_addr_char = "[^\\.0-9]"
31 -- Add non_addr_chars on either side of the given String. This
32 -- prevents (for example) the regex '127.0.0.1' from matching
34 addr_barrier :: String -> String
35 addr_barrier x = non_addr_char ++ x ++ non_addr_char
38 -- The magic happens here. We take a CIDR String as an argument, and
39 -- return the equivalent regular expression. We do this as follows:
41 -- 1. Compute the minimum possible value of each octet.
42 -- 2. Compute the maximum possible value of each octet.
43 -- 3. Generate a regex matching every value between those min and
45 -- 4. Join the regexes from step 3 with regexes matching periods.
46 -- 5. Stick an address boundary on either side of the result.
47 --cidr_to_regex :: String -> String
48 cidr_to_regex :: Cidr.Cidr -> String
50 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
52 range1 = numeric_range min1 max1
53 range2 = numeric_range min2 max2
54 range3 = numeric_range min3 max3
55 range4 = numeric_range min4 max4
56 min1 = octet_to_int (min_octet1 (ipv4address cidr) (maskbits cidr))
57 min2 = octet_to_int (min_octet2 (ipv4address cidr) (maskbits cidr))
58 min3 = octet_to_int (min_octet3 (ipv4address cidr) (maskbits cidr))
59 min4 = octet_to_int (min_octet4 (ipv4address cidr) (maskbits cidr))
60 max1 = octet_to_int (max_octet1 (ipv4address cidr) (maskbits cidr))
61 max2 = octet_to_int (max_octet2 (ipv4address cidr) (maskbits cidr))
62 max3 = octet_to_int (max_octet3 (ipv4address cidr) (maskbits cidr))
63 max4 = octet_to_int (max_octet4 (ipv4address cidr) (maskbits cidr))
67 -- Take a list of Strings, and return a regular expression matching
69 alternate :: [String] -> String
70 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
73 -- Take two Ints as parameters, and return a regex matching any
74 -- integer between them (inclusive).
75 numeric_range :: Int -> Int -> String
77 alternate (map show [lower..upper])
85 -- First, check for any errors that occurred while parsing
86 -- the command line options.
87 errors <- CommandLine.parse_errors
90 hPutStrLn stderr (concat errors)
91 putStrLn CommandLine.help_text
92 exitWith (ExitFailure exit_args_parse_failed)
95 -- Next, check to see if the 'help' option was passed to the
96 -- program. If it was, display the help, and exit successfully.
97 help_opt_set <- CommandLine.help_set
100 putStrLn CommandLine.help_text
104 -- The input function we receive here should know what to read.
105 inputfunc <- (CommandLine.input_function)
108 let cidr_strings = lines input
109 let cidrs = map Cidr.cidr_from_string cidr_strings
111 if (any (== Cidr.None) cidrs)
113 putStrLn "Error: not valid CIDR notation."
114 exitWith (ExitFailure exit_invalid_cidr)
117 -- Get the mode of operation.
118 mode <- CommandLine.parse_mode
122 let regexes = map cidr_to_regex cidrs
123 putStrLn $ alternate regexes