import System.Exit (exitFailure)
import Text.Regex.Posix
-
--- Stolen from ByteString. Splits a list at each element satisfying
--- the predicate p.
-splitWith :: (a -> Bool) -> [a] -> [[a]]
-splitWith p xs =
- ys : case zs of
- [] -> []
- _:ws -> splitWith p ws
- where (ys,zs) = break p xs
-
+import ListUtils
-- Takes an IP address in CIDR notation, and returns a list of its
-- octets (converted to Int).
maskbits cidr = read ((splitWith (`elem` "/") cidr) !! 1)
--- Pads a list (on the left) to length len by prepending pad_elem.
-pad_left_to :: Int -> a -> [a] -> [a]
-pad_left_to len pad_elem xs =
- if (length xs) >= len then
- xs
- else
- (replicate padcount pad_elem) ++ xs
- where
- padcount = len - (length xs)
-
-
--- Pads a list (on the right) to length len by appending pad_elem.
-pad_right_to :: Int -> a -> [a] -> [a]
-pad_right_to len pad_elem xs =
- if (length xs) >= len then
- xs
- else
- xs ++ (replicate padcount pad_elem)
- where
- padcount = len - (length xs)
-
-
-- Takes an Int, and returns its base-two representation as a String.
base_two :: Int -> String
base_two n = N.showIntAtBase 2 DC.intToDigit n ""
upper = maximum [x,y]
-main :: IO ()
-main = do
- line <- getLine
-
- if (is_valid_cidr line)
+-- Take a CIDR String, and exitFailure if it's invalid.
+validate_or_die :: String -> IO ()
+validate_or_die cidr = do
+ if (is_valid_cidr cidr)
then do
- putStrLn (cidr_to_regex line)
+ return ()
else do
putStrLn "Error: not valid CIDR notation."
exitFailure
+
+main :: IO ()
+main = do
+ input <- getContents
+ let cidrs = lines input
+ mapM validate_or_die cidrs
+ let regexes = map cidr_to_regex cidrs
+ putStrLn $ alternate regexes
+