1 import Data.List (intercalate, intersperse)
2 import System.Exit (ExitCode(..), exitWith)
3 import System.IO (stderr, hPutStrLn)
5 import Text.Regex.Posix
18 import CommandLine (help_set,
25 -- Some exit codes, used in the ExitFailure constructor.
26 exit_invalid_cidr :: Int
29 exit_args_parse_failed :: Int
30 exit_args_parse_failed = 2
33 -- A regular expression that matches a non-address character.
34 non_addr_char :: String
35 non_addr_char = "[^\\.0-9]"
38 -- Add non_addr_chars on either side of the given String. This
39 -- prevents (for example) the regex '127.0.0.1' from matching
41 addr_barrier :: String -> String
42 addr_barrier x = non_addr_char ++ x ++ non_addr_char
45 -- The magic happens here. We take a CIDR String as an argument, and
46 -- return the equivalent regular expression. We do this as follows:
48 -- 1. Compute the minimum possible value of each octet.
49 -- 2. Compute the maximum possible value of each octet.
50 -- 3. Generate a regex matching every value between those min and
52 -- 4. Join the regexes from step 3 with regexes matching periods.
53 -- 5. Stick an address boundary on either side of the result.
54 --cidr_to_regex :: String -> String
55 cidr_to_regex :: Cidr.Cidr -> String
57 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
59 range1 = numeric_range min1 max1
60 range2 = numeric_range min2 max2
61 range3 = numeric_range min3 max3
62 range4 = numeric_range min4 max4
63 min1 = min_first_octet cidr
64 min2 = min_second_octet cidr
65 min3 = min_third_octet cidr
66 min4 = min_fourth_octet cidr
67 max1 = max_first_octet cidr
68 max2 = max_second_octet cidr
69 max3 = max_third_octet cidr
70 max4 = max_fourth_octet cidr
73 -- Will return True if the passed String is in CIDR notation, False
75 is_valid_cidr :: String -> Bool
76 is_valid_cidr cidr = cidr =~ "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}"
79 -- Take a list of Strings, and return a regular expression matching
81 alternate :: [String] -> String
82 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
85 -- Take two Ints as parameters, and return a regex matching any
86 -- integer between them (inclusive).
87 numeric_range :: Int -> Int -> String
89 alternate (map show [lower..upper])
95 -- Take a CIDR String, and exit with a failure if it's invalid.
96 validate_or_die :: String -> IO ()
97 validate_or_die cidr = do
98 if (is_valid_cidr cidr)
102 putStrLn "Error: not valid CIDR notation."
103 exitWith (ExitFailure exit_invalid_cidr)
109 -- First, check for any errors that occurred while parsing
110 -- the command line options.
111 errors <- CommandLine.parse_errors
114 hPutStrLn stderr (concat errors)
115 putStrLn CommandLine.help_text
116 exitWith (ExitFailure exit_args_parse_failed)
119 -- Next, check to see if the 'help' option was passed to the
120 -- program. If it was, display the help, and exit successfully.
121 help_opt_set <- CommandLine.help_set
124 putStrLn CommandLine.help_text
128 -- The input function we receive here should know what to read.
129 inputfunc <- (CommandLine.input_function)
132 let cidr_strings = lines input
133 mapM validate_or_die cidr_strings
134 let cidrs = map Cidr.from_string cidr_strings
136 -- Get the mode of operation.
137 mode <- CommandLine.parse_mode
141 let regexes = map cidr_to_regex cidrs
142 putStrLn $ alternate regexes