- -- First, check for any errors that occurred while parsing
- -- the command line options.
- errors <- CommandLine.parse_errors
- if not (null errors)
- then do
- hPutStrLn stderr (concat errors)
- putStrLn CommandLine.help_text
- exitWith (ExitFailure exit_args_parse_failed)
- else do -- Nothing
-
- -- 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
- if help_opt_set
- then do
- putStrLn CommandLine.help_text
- exitWith ExitSuccess
- else do -- Nothing
-
- -- The input function we receive here should know what to read.
- inputfunc <- (CommandLine.input_function)
- input <- inputfunc
-
- let cidr_strings = lines input
- let cidrs = map Cidr.cidr_from_string cidr_strings
-
- if (any (== Cidr.None) cidrs)
- then do
- putStrLn "Error: not valid CIDR notation."
- exitWith (ExitFailure exit_invalid_cidr)
- else do -- Nothing
-
- -- Get the mode of operation.
- mode <- CommandLine.parse_mode
-
- case mode of
- Regex -> do
- let regexes = map cidr_to_regex cidrs
- putStrLn $ alternate regexes
- Reduce -> do
- _ <- mapM (putStrLn . show) (combine_all cidrs)
- return ()
- Dupe -> do
- _ <- mapM (putStrLn . show) dupes
- return ()
- where
- dupes = cidrs \\ (combine_all cidrs)
- Diff -> do
- _ <- mapM putStrLn deletions
- _ <- mapM putStrLn additions
- return ()
- where
- dupes = cidrs \\ (combine_all cidrs)
- deletions = map (\s -> "-" ++ (show s)) dupes
- newcidrs = (combine_all cidrs) \\ cidrs
- additions = map (\s -> "+" ++ (show s)) newcidrs
+ args <- get_args
+
+ -- This reads stdin.
+ input <- getContents
+
+ let cidr_strings = splitWs input
+ let cidrs = map readMaybe cidr_strings
+
+ when (any isNothing cidrs) $ do
+ hPutStrLn stderr "ERROR: not valid CIDR notation:"
+
+ -- Output the bad lines, safely.
+ let pairs = zip cidr_strings cidrs
+ let print_pair (x, Nothing) = hPutStrLn stderr (" * " ++ x)
+ print_pair (_, _) = return ()
+
+ mapM_ print_pair pairs
+ exitWith (ExitFailure exit_invalid_cidr)
+
+ -- Filter out only the valid ones.
+ let valid_cidrs = catMaybes cidrs
+
+ case args of
+ Regexed{} -> do
+ let cidrs' = combine_all valid_cidrs
+ let regexes = map (cidr_to_regex (barriers args)) cidrs'
+ putStrLn $ alternate regexes
+ Reduced{} ->
+ mapM_ print (combine_all valid_cidrs)
+ Duped{} ->
+ mapM_ print dupes
+ where
+ dupes = valid_cidrs \\ (combine_all valid_cidrs)
+ Diffed{} -> 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
+ Listed{} -> do
+ let combined_cidrs = combine_all valid_cidrs
+ let addrs = concatMap enumerate combined_cidrs
+ mapM_ print addrs
+ Reversed{} -> 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
+ -- NameError simply means "not found" so we output nothing.
+ Left NameError -> ""
+ Left err -> "ERROR (" ++ (show err) ++ ")"
+ Right ds -> BS.unpack $ BS.intercalate space ds