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