1 import Data.List ((\\), intercalate, intersperse)
2 import System.Exit (ExitCode(..), exitWith)
3 import System.IO (stderr, hPutStrLn)
17 import CommandLine (help_set,
26 -- Some exit codes, used in the ExitFailure constructor.
27 exit_invalid_cidr :: Int
30 exit_args_parse_failed :: Int
31 exit_args_parse_failed = 2
34 -- A regular expression that matches a non-address character.
35 non_addr_char :: String
36 non_addr_char = "[^\\.0-9]"
39 -- Add non_addr_chars on either side of the given String. This
40 -- prevents (for example) the regex '127.0.0.1' from matching
42 addr_barrier :: String -> String
43 addr_barrier x = non_addr_char ++ x ++ non_addr_char
46 -- The magic happens here. We take a CIDR String as an argument, and
47 -- return the equivalent regular expression. We do this as follows:
49 -- 1. Compute the minimum possible value of each octet.
50 -- 2. Compute the maximum possible value of each octet.
51 -- 3. Generate a regex matching every value between those min and
53 -- 4. Join the regexes from step 3 with regexes matching periods.
54 -- 5. Stick an address boundary on either side of the result.
55 --cidr_to_regex :: String -> String
56 cidr_to_regex :: Cidr.Cidr -> String
58 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
60 range1 = numeric_range min1 max1
61 range2 = numeric_range min2 max2
62 range3 = numeric_range min3 max3
63 range4 = numeric_range min4 max4
64 min1 = octet_to_int (min_octet1 cidr)
65 min2 = octet_to_int (min_octet2 cidr)
66 min3 = octet_to_int (min_octet3 cidr)
67 min4 = octet_to_int (min_octet4 cidr)
68 max1 = octet_to_int (max_octet1 cidr)
69 max2 = octet_to_int (max_octet2 cidr)
70 max3 = octet_to_int (max_octet3 cidr)
71 max4 = octet_to_int (max_octet4 cidr)
75 -- Take a list of Strings, and return a regular expression matching
77 alternate :: [String] -> String
78 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
81 -- Take two Ints as parameters, and return a regex matching any
82 -- integer between them (inclusive).
83 numeric_range :: Int -> Int -> String
85 alternate (map show [lower..upper])
93 -- First, check for any errors that occurred while parsing
94 -- the command line options.
95 errors <- CommandLine.parse_errors
98 hPutStrLn stderr (concat errors)
99 putStrLn CommandLine.help_text
100 exitWith (ExitFailure exit_args_parse_failed)
103 -- Next, check to see if the 'help' option was passed to the
104 -- program. If it was, display the help, and exit successfully.
105 help_opt_set <- CommandLine.help_set
108 putStrLn CommandLine.help_text
112 -- The input function we receive here should know what to read.
113 inputfunc <- (CommandLine.input_function)
116 let cidr_strings = lines input
117 let cidrs = map Cidr.cidr_from_string cidr_strings
119 if (any (== Cidr.None) cidrs)
121 putStrLn "Error: not valid CIDR notation."
122 exitWith (ExitFailure exit_invalid_cidr)
125 -- Get the mode of operation.
126 mode <- CommandLine.parse_mode
130 let regexes = map cidr_to_regex cidrs
131 putStrLn $ alternate regexes
133 mapM (putStrLn . show) (combine_all cidrs)
136 mapM (putStrLn . show) dupes
139 dupes = cidrs \\ (combine_all cidrs)