]> gitweb.michael.orlitzky.com - hath.git/blob - src/Main.hs
Add more Haddock comments.
[hath.git] / src / Main.hs
1 import Control.Monad (when)
2 import Data.List ((\\), intercalate, intersperse)
3 import Data.Maybe (catMaybes, isNothing)
4 import Data.String.Utils (splitWs)
5 import System.Exit (ExitCode(..), exitWith)
6 import System.IO (stderr, hPutStrLn)
7
8 import Cidr (Cidr(..),
9 cidr_from_string,
10 combine_all,
11 max_octet1,
12 max_octet2,
13 max_octet3,
14 max_octet4,
15 min_octet1,
16 min_octet2,
17 min_octet3,
18 min_octet4 )
19
20 import CommandLine (help_set,
21 help_text,
22 input_function,
23 Mode(..),
24 parse_errors,
25 parse_mode)
26
27 import ExitCodes
28 import Octet
29
30
31 -- | A regular expression that matches a non-address character.
32 non_addr_char :: String
33 non_addr_char = "[^\\.0-9]"
34
35
36 -- | Add non_addr_chars on either side of the given String. This
37 -- prevents (for example) the regex '127.0.0.1' from matching
38 -- '127.0.0.100'.
39 addr_barrier :: String -> String
40 addr_barrier x = non_addr_char ++ x ++ non_addr_char
41
42
43 -- | The magic happens here. We take a CIDR String as an argument, and
44 -- return the equivalent regular expression. We do this as follows:
45 --
46 -- 1. Compute the minimum possible value of each octet.
47 -- 2. Compute the maximum possible value of each octet.
48 -- 3. Generate a regex matching every value between those min and
49 -- max values.
50 -- 4. Join the regexes from step 3 with regexes matching periods.
51 -- 5. Stick an address boundary on either side of the result.
52 cidr_to_regex :: Cidr.Cidr -> String
53 cidr_to_regex cidr =
54 addr_barrier (intercalate "\\." [range1, range2, range3, range4])
55 where
56 range1 = numeric_range min1 max1
57 range2 = numeric_range min2 max2
58 range3 = numeric_range min3 max3
59 range4 = numeric_range min4 max4
60 min1 = octet_to_int (min_octet1 cidr)
61 min2 = octet_to_int (min_octet2 cidr)
62 min3 = octet_to_int (min_octet3 cidr)
63 min4 = octet_to_int (min_octet4 cidr)
64 max1 = octet_to_int (max_octet1 cidr)
65 max2 = octet_to_int (max_octet2 cidr)
66 max3 = octet_to_int (max_octet3 cidr)
67 max4 = octet_to_int (max_octet4 cidr)
68
69
70
71 -- | Take a list of Strings, and return a regular expression matching
72 -- any of them.
73 alternate :: [String] -> String
74 alternate terms = "(" ++ (concat (intersperse "|" terms)) ++ ")"
75
76
77 -- | Take two Ints as parameters, and return a regex matching any
78 -- integer between them (inclusive).
79 numeric_range :: Int -> Int -> String
80 numeric_range x y =
81 alternate (map show [lower..upper])
82 where
83 lower = minimum [x,y]
84 upper = maximum [x,y]
85
86
87 main :: IO ()
88 main = do
89 -- First, check for any errors that occurred while parsing
90 -- the command line options.
91 errors <- CommandLine.parse_errors
92 when ((not . null) errors) $ do
93 hPutStrLn stderr (concat errors)
94 putStrLn CommandLine.help_text
95 exitWith (ExitFailure exit_args_parse_failed)
96
97 -- Next, check to see if the 'help' option was passed to the
98 -- program. If it was, display the help, and exit successfully.
99 help_opt_set <- CommandLine.help_set
100 when help_opt_set $ do
101 putStrLn CommandLine.help_text
102 exitWith ExitSuccess
103
104 -- The input function we receive here should know what to read.
105 inputfunc <- (CommandLine.input_function)
106 input <- inputfunc
107
108 let cidr_strings = splitWs input
109 let cidrs = map cidr_from_string cidr_strings
110
111 when (any isNothing cidrs) $ do
112 putStrLn "Error: not valid CIDR notation."
113 exitWith (ExitFailure exit_invalid_cidr)
114
115 -- Filter out only the valid ones.
116 let valid_cidrs = catMaybes cidrs
117
118 -- Get the mode of operation.
119 mode <- CommandLine.parse_mode
120
121 case mode of
122 Regex -> do
123 let regexes = map cidr_to_regex valid_cidrs
124 putStrLn $ alternate regexes
125 Reduce -> do
126 _ <- mapM (putStrLn . show) (combine_all valid_cidrs)
127 return ()
128 Dupe -> do
129 _ <- mapM (putStrLn . show) dupes
130 return ()
131 where
132 dupes = valid_cidrs \\ (combine_all valid_cidrs)
133 Diff -> do
134 _ <- mapM putStrLn deletions
135 _ <- mapM putStrLn additions
136 return ()
137 where
138 dupes = valid_cidrs \\ (combine_all valid_cidrs)
139 deletions = map (\s -> "-" ++ (show s)) dupes
140 newcidrs = (combine_all valid_cidrs) \\ valid_cidrs
141 additions = map (\s -> "+" ++ (show s)) newcidrs