+ -- First, check for any errors that occurred while parsing
+ -- the command line options.
+ errors <- CommandLine.parse_errors
+ unless (null errors) $ do
+ hPutStrLn stderr (concat errors)
+ putStrLn CommandLine.help_text
+ exitWith (ExitFailure exit_args_parse_failed)
+
+ -- Next, check to see if the 'help' option was passed to the
+ -- program. If it was, display the help, and exit successfully.
+ help_opt_set <- CommandLine.help_set
+ when help_opt_set $ do
+ putStrLn CommandLine.help_text
+ exitSuccess
+
+ -- The input function we receive here should know what to read.
+ inputfunc <- (CommandLine.input_function)
+ input <- inputfunc
+
+ let cidr_strings = splitWs input
+ let cidrs = map readMaybe cidr_strings
+
+ when (any isNothing cidrs) $ do
+ putStrLn "Error: not valid CIDR notation."
+ exitWith (ExitFailure exit_invalid_cidr)
+
+ -- Filter out only the valid ones.
+ let valid_cidrs = catMaybes cidrs
+
+ -- Get the mode of operation.
+ mode <- CommandLine.parse_mode
+
+ case mode of
+ Regex -> do
+ let regexes = map cidr_to_regex valid_cidrs
+ putStrLn $ alternate regexes
+ Reduce ->
+ mapM_ print (combine_all valid_cidrs)
+ Dupe ->
+ mapM_ print dupes
+ where
+ dupes = valid_cidrs \\ (combine_all valid_cidrs)
+ Diff -> do
+ mapM_ putStrLn deletions
+ mapM_ putStrLn additions
+ where
+ dupes = valid_cidrs \\ (combine_all valid_cidrs)
+ deletions = map (\s -> '-' : (show s)) dupes
+ newcidrs = (combine_all valid_cidrs) \\ valid_cidrs
+ additions = map (\s -> '+' : (show s)) newcidrs
+ List -> do
+ let combined_cidrs = combine_all valid_cidrs
+ let addrs = concatMap enumerate combined_cidrs
+ mapM_ print addrs
+ Reverse -> do
+ let combined_cidrs = combine_all valid_cidrs
+ let addrs = concatMap enumerate combined_cidrs
+ let addr_bytestrings = map (BS.pack . show) addrs
+ ptrs <- lookup_ptrs addr_bytestrings
+ let pairs = zip addr_bytestrings ptrs
+ mapM_ (putStrLn . show_pair) pairs
+
+ stopGlobalPool
+
+ where
+ show_pair :: (Domain, PTRResult) -> String
+ show_pair (s, eds) =
+ (BS.unpack s) ++ ": " ++ results
+ where
+ space = BS.pack " "
+ results =
+ case eds of
+ Left err -> "ERROR (" ++ (show err) ++ ")"
+ Right ds -> BS.unpack $ BS.intercalate space ds