1 import Data.List (intercalate, intersperse)
2 import System.Exit (ExitCode(..), exitWith)
3 import System.IO (stderr, hPutStrLn)
17 import CommandLine (help_set,
24 -- Some exit codes, used in the ExitFailure constructor.
25 exit_invalid_cidr :: Int
28 exit_args_parse_failed :: Int
29 exit_args_parse_failed = 2
32 -- A regular expression that matches a non-address character.
33 non_addr_char :: String
34 non_addr_char = "[^\\.0-9]"
37 -- Add non_addr_chars on either side of the given String. This
38 -- prevents (for example) the regex '127.0.0.1' from matching
40 addr_barrier :: String -> String
41 addr_barrier x = non_addr_char ++ x ++ non_addr_char
44 -- The magic happens here. We take a CIDR String as an argument, and
45 -- return the equivalent regular expression. We do this as follows:
47 -- 1. Compute the minimum possible value of each octet.
48 -- 2. Compute the maximum possible value of each octet.
49 -- 3. Generate a regex matching every value between those min and
51 -- 4. Join the regexes from step 3 with regexes matching periods.
52 -- 5. Stick an address boundary on either side of the result.
53 --cidr_to_regex :: String -> String
54 cidr_to_regex :: Cidr.Cidr -> String
56 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
58 range1 = numeric_range min1 max1
59 range2 = numeric_range min2 max2
60 range3 = numeric_range min3 max3
61 range4 = numeric_range min4 max4
62 min1 = min_first_octet cidr
63 min2 = min_second_octet cidr
64 min3 = min_third_octet cidr
65 min4 = min_fourth_octet cidr
66 max1 = max_first_octet cidr
67 max2 = max_second_octet cidr
68 max3 = max_third_octet cidr
69 max4 = max_fourth_octet cidr
73 -- Take a list of Strings, and return a regular expression matching
75 alternate :: [String] -> String
76 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
79 -- Take two Ints as parameters, and return a regex matching any
80 -- integer between them (inclusive).
81 numeric_range :: Int -> Int -> String
83 alternate (map show [lower..upper])
89 -- Take a CIDR String, and exit with a failure if it's invalid.
90 validate_or_die :: String -> IO ()
91 validate_or_die cidr = do
92 if (Cidr.is_valid_cidr cidr)
96 putStrLn "Error: not valid CIDR notation."
97 exitWith (ExitFailure exit_invalid_cidr)
103 -- First, check for any errors that occurred while parsing
104 -- the command line options.
105 errors <- CommandLine.parse_errors
108 hPutStrLn stderr (concat errors)
109 putStrLn CommandLine.help_text
110 exitWith (ExitFailure exit_args_parse_failed)
113 -- Next, check to see if the 'help' option was passed to the
114 -- program. If it was, display the help, and exit successfully.
115 help_opt_set <- CommandLine.help_set
118 putStrLn CommandLine.help_text
122 -- The input function we receive here should know what to read.
123 inputfunc <- (CommandLine.input_function)
126 let cidr_strings = lines input
127 mapM validate_or_die cidr_strings
128 let cidrs = map Cidr.from_string cidr_strings
130 -- Get the mode of operation.
131 mode <- CommandLine.parse_mode
135 let regexes = map cidr_to_regex cidrs
136 putStrLn $ alternate regexes