]> gitweb.michael.orlitzky.com - hath.git/blobdiff - src/Main.hs
Move the is_valid_cidr function in to the Cidr module.
[hath.git] / src / Main.hs
index 3dfa546e374c84b6c86cd2bdce848285d5fd2ea8..7b3bd7b0884847a456545903594130e9a8504539 100644 (file)
@@ -2,10 +2,9 @@ import Data.List (intercalate, intersperse)
 import System.Exit (ExitCode(..), exitWith)
 import System.IO (stderr, hPutStrLn)
 
-import Text.Regex.Posix
-
 import Cidr (Cidr,
              from_string,
+             is_valid_cidr,
              min_first_octet,
              min_second_octet,
              min_third_octet,
@@ -18,7 +17,9 @@ import Cidr (Cidr,
 import CommandLine (help_set,
                     help_text,
                     input_function,
-                    parse_errors)
+                    Mode(..),
+                    parse_errors,
+                    parse_mode)
 
 -- Some exit codes, used in the ExitFailure constructor.
 exit_invalid_cidr :: Int
@@ -68,11 +69,6 @@ cidr_to_regex cidr =
       max4   = max_fourth_octet cidr
 
 
--- Will return True if the passed String is in CIDR notation, False
--- otherwise.
-is_valid_cidr :: String -> Bool
-is_valid_cidr cidr = cidr =~ "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}"
-
 
 -- Take a list of Strings, and return a regular expression matching
 -- any of them.
@@ -93,7 +89,7 @@ numeric_range x y =
 -- 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)
+  if (Cidr.is_valid_cidr cidr)
     then do
       return ()
     else do
@@ -126,8 +122,17 @@ main = do
   -- 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