]> gitweb.michael.orlitzky.com - hath.git/blob - src/Main.hs
Use "words" instead of "splitWs" and drop MissingH dependency.
[hath.git] / src / Main.hs
1 module Main
2 where
3
4 import Control.Monad (when)
5 import Data.List ((\\), intercalate)
6 import Data.Maybe (catMaybes, isNothing)
7 import System.Exit (ExitCode( ExitFailure ), exitWith)
8 import System.IO (stderr, hPutStrLn)
9 import Text.Read (readMaybe)
10
11 import Cidr (
12 Cidr(),
13 combine_all,
14 enumerate,
15 max_octet1,
16 max_octet2,
17 max_octet3,
18 max_octet4,
19 min_octet1,
20 min_octet2,
21 min_octet3,
22 min_octet4 )
23 import CommandLine(
24 Args( Regexed, Reduced, Duped, Diffed, Listed, barriers ),
25 get_args )
26 import ExitCodes ( exit_invalid_cidr )
27 import Octet ()
28
29
30 -- | A regular expression that matches a non-address character.
31 --
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 --
40 add_barriers :: String -> String
41 add_barriers x = non_addr_char ++ x ++ non_addr_char
42
43
44 -- | The magic happens here. We take a CIDR String as an argument, and
45 -- return the equivalent regular expression. We do this as follows:
46 --
47 -- 1. Compute the minimum possible value of each octet.
48 -- 2. Compute the maximum possible value of each octet.
49 -- 3. Generate a regex matching every value between those min and
50 -- max values.
51 -- 4. Join the regexes from step 3 with regexes matching periods.
52 -- 5. Stick an address boundary on either side of the result if
53 -- use_barriers is True.
54 --
55 cidr_to_regex :: Bool -> Cidr -> String
56 cidr_to_regex use_barriers cidr =
57 let f = if use_barriers then add_barriers else id in
58 f (intercalate "\\." [range1, range2, range3, range4])
59 where
60 range1 = numeric_range min1 max1
61 range2 = numeric_range min2 max2
62 range3 = numeric_range min3 max3
63 range4 = numeric_range min4 max4
64 min1 = fromEnum (min_octet1 cidr)
65 min2 = fromEnum (min_octet2 cidr)
66 min3 = fromEnum (min_octet3 cidr)
67 min4 = fromEnum (min_octet4 cidr)
68 max1 = fromEnum (max_octet1 cidr)
69 max2 = fromEnum (max_octet2 cidr)
70 max3 = fromEnum (max_octet3 cidr)
71 max4 = fromEnum (max_octet4 cidr)
72
73
74
75 -- | Take a list of Strings, and return a regular expression matching
76 -- any of them.
77 --
78 alternate :: [String] -> String
79 alternate terms = "(" ++ (intercalate "|" terms) ++ ")"
80
81
82 -- | Take two Ints as parameters, and return a regex matching any
83 -- integer between them (inclusive).
84 --
85 -- IMPORTANT: we match from max to min so that if e.g. the last
86 -- octet is '255', we want '255' to match before '2' in the regex
87 -- (255|254|...|3|2|1) which does not happen if we use
88 -- (1|2|3|...|254|255).
89 --
90 numeric_range :: Int -> Int -> String
91 numeric_range x y =
92 alternate (map show $ reverse [lower..upper])
93 where
94 lower = minimum [x,y]
95 upper = maximum [x,y]
96
97
98 main :: IO ()
99 main = do
100 args <- get_args
101
102 -- This reads stdin.
103 input <- getContents
104
105 let cidr_strings = words input
106 let cidrs = map readMaybe cidr_strings :: [Maybe Cidr]
107
108 when (any isNothing cidrs) $ do
109 hPutStrLn stderr "ERROR: not valid CIDR notation:"
110
111 -- Output the bad lines, safely.
112 let pairs = zip cidr_strings cidrs
113
114 let print_pair :: (String, Maybe Cidr) -> IO ()
115 print_pair (x, Nothing) = hPutStrLn stderr (" * " ++ x)
116 print_pair (_, _) = return ()
117
118 mapM_ print_pair pairs
119 exitWith (ExitFailure exit_invalid_cidr)
120
121 -- Filter out only the valid ones.
122 let valid_cidrs = catMaybes cidrs
123
124 case args of
125 Regexed{} -> do
126 let cidrs' = combine_all valid_cidrs
127 let regexes = map (cidr_to_regex (barriers args)) cidrs'
128 putStrLn $ alternate regexes
129 Reduced{} ->
130 mapM_ print (combine_all valid_cidrs)
131 Duped{} ->
132 mapM_ print dupes
133 where
134 dupes = valid_cidrs \\ (combine_all valid_cidrs)
135 Diffed{} -> do
136 mapM_ putStrLn deletions
137 mapM_ putStrLn additions
138 where
139 dupes = valid_cidrs \\ (combine_all valid_cidrs)
140 deletions = map (\s -> '-' : (show s)) dupes
141 newcidrs = (combine_all valid_cidrs) \\ valid_cidrs
142 additions = map (\s -> '+' : (show s)) newcidrs
143 Listed{} -> do
144 let combined_cidrs = combine_all valid_cidrs
145 let addrs = concatMap enumerate combined_cidrs
146 mapM_ print addrs