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,
23 -- Some exit codes, used in the ExitFailure constructor.
24 exit_invalid_cidr :: Int
27 exit_args_parse_failed :: Int
28 exit_args_parse_failed = 2
31 -- A regular expression that matches a non-address character.
32 non_addr_char :: String
33 non_addr_char = "[^\\.0-9]"
36 -- Add non_addr_chars on either side of the given String. This
37 -- prevents (for example) the regex '127.0.0.1' from matching
39 addr_barrier :: String -> String
40 addr_barrier x = non_addr_char ++ x ++ non_addr_char
43 -- The magic happens here. We take a CIDR String as an argument, and
44 -- return the equivalent regular expression. We do this as follows:
46 -- 1. Compute the minimum possible value of each octet.
47 -- 2. Compute the maximum possible value of each octet.
48 -- 3. Generate a regex matching every value between those min and
50 -- 4. Join the regexes from step 3 with regexes matching periods.
51 -- 5. Stick an address boundary on either side of the result.
52 --cidr_to_regex :: String -> String
53 cidr_to_regex :: Cidr.Cidr -> String
55 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
57 range1 = numeric_range min1 max1
58 range2 = numeric_range min2 max2
59 range3 = numeric_range min3 max3
60 range4 = numeric_range min4 max4
61 min1 = min_first_octet cidr
62 min2 = min_second_octet cidr
63 min3 = min_third_octet cidr
64 min4 = min_fourth_octet cidr
65 max1 = max_first_octet cidr
66 max2 = max_second_octet cidr
67 max3 = max_third_octet cidr
68 max4 = max_fourth_octet cidr
71 -- Will return True if the passed String is in CIDR notation, False
73 is_valid_cidr :: String -> Bool
74 is_valid_cidr cidr = cidr =~ "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}"
77 -- Take a list of Strings, and return a regular expression matching
79 alternate :: [String] -> String
80 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
83 -- Take two Ints as parameters, and return a regex matching any
84 -- integer between them (inclusive).
85 numeric_range :: Int -> Int -> String
87 alternate (map show [lower..upper])
93 -- Take a CIDR String, and exit with a failure if it's invalid.
94 validate_or_die :: String -> IO ()
95 validate_or_die cidr = do
96 if (is_valid_cidr cidr)
100 putStrLn "Error: not valid CIDR notation."
101 exitWith (ExitFailure exit_invalid_cidr)
107 -- First, check for any errors that occurred while parsing
108 -- the command line options.
109 errors <- CommandLine.parse_errors
112 hPutStrLn stderr (concat errors)
113 putStrLn CommandLine.help_text
114 exitWith (ExitFailure exit_args_parse_failed)
117 -- Next, check to see if the 'help' option was passed to the
118 -- program. If it was, display the help, and exit successfully.
119 help_opt_set <- CommandLine.help_set
122 putStrLn CommandLine.help_text
126 -- The input function we receive here should know what to read.
127 inputfunc <- (CommandLine.input_function)
129 let cidr_strings = lines input
130 mapM validate_or_die cidr_strings
131 let cidrs = map Cidr.from_string cidr_strings
132 let regexes = map cidr_to_regex cidrs
133 putStrLn $ alternate regexes