X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FMain.hs;h=7686ae30fb989e0cd3ee51d2dca2ac9e6980629b;hb=3296c2f6578001f6a40de1b1e1f79b25c987d3af;hp=c87520951088c46f095f334bd90e78587b98c53d;hpb=364735d8f7cbc78528517faeccac606813ef9998;p=hath.git diff --git a/src/Main.hs b/src/Main.hs index c875209..7686ae3 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,5 +1,7 @@ import Data.List (intercalate, intersperse) -import System.Exit (exitFailure) +import System.Exit (ExitCode(..), exitWith) +import System.IO (stderr, hPutStrLn) + import Text.Regex.Posix import Cidr (Cidr, @@ -13,6 +15,20 @@ import Cidr (Cidr, max_third_octet, max_fourth_octet) +import CommandLine (help_set, + help_text, + input_function, + Mode(..), + parse_errors, + parse_mode) + +-- Some exit codes, used in the ExitFailure constructor. +exit_invalid_cidr :: Int +exit_invalid_cidr = 1 + +exit_args_parse_failed :: Int +exit_args_parse_failed = 2 + -- A regular expression that matches a non-address character. non_addr_char :: String @@ -76,7 +92,7 @@ numeric_range x y = upper = maximum [x,y] --- Take a CIDR String, and exitFailure if it's invalid. +-- Take a CIDR String, and exit with a failure if it's invalid. validate_or_die :: String -> IO () validate_or_die cidr = do if (is_valid_cidr cidr) @@ -84,15 +100,45 @@ validate_or_die cidr = do return () else do putStrLn "Error: not valid CIDR notation." - exitFailure + exitWith (ExitFailure exit_invalid_cidr) + main :: IO () main = do - input <- getContents + -- 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 mapM validate_or_die cidr_strings let cidrs = map Cidr.from_string cidr_strings - let regexes = map cidr_to_regex cidrs - putStrLn $ alternate regexes + -- 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 + putStr input