.I $ echo x127.0.0.1x | grep -Po $(echo 127.0.0.1/32 | hath -b)
x127.0.0.1x
.fi
-
.IP \fB\-\-normalize\fR,\ \fB\-n\fR
(reduced mode only)
.I $ echo 127.0.0.1/8 | hath reduced --normalize
127.0.0.0/8
.fi
+.IP \fB\-\-sort\fR,\ \fB\-s\fR
+(reduced mode only)
+
+Sort the output CIDRs numerically by octet. The \(dqsort\(dq utility
+doesn't understand IP addresses, so the default pipe-to-sort approach
+fails in some cases:
+
+.nf
+.I $ echo \(dq10.0.121.32/28 10.0.93.248/29\(dq | hath reduced | sort
+10.0.121.32/28
+10.0.93.248/29
+.I $ echo \(dq10.0.121.32/28 10.0.93.248/29\(dq | hath reduced | sort -n
+10.0.121.32/28
+10.0.93.248/29
+.fi
+
+That failure justifies adding the additional option to hath:
+
+.nf
+.I $ echo \(dq10.0.121.32/28 10.0.93.248/29\(dq | hath reduced --sort
+10.0.93.248/29
+10.0.121.32/28
.SH BUGS
Send bugs to michael@orlitzky.com.
normalize_help :: String
normalize_help =
- "(reduced mode) normalize the output CIDRS, replacing any " ++
+ "(reduced mode) normalize the output CIDRs, replacing any " ++
"masked bits by zeros; e.g. '127.0.0.1/8' -> '127.0.0.0/8'"
+sort_help :: String
+sort_help =
+ "(reduced mode) sort the output CIDRs by their octets"
+
+
-- | The Args type represents the possible command-line options. The
-- duplication here seems necessary; CmdArgs' magic requires us to
-- define some things explicitly.
-- same options and arguments.
--
data Args =
- Regexed { barriers :: Bool, normalize :: Bool } |
- Reduced { barriers :: Bool, normalize :: Bool } |
- Duped { barriers :: Bool, normalize :: Bool } |
- Diffed { barriers :: Bool, normalize :: Bool } |
- Listed { barriers :: Bool, normalize :: Bool }
+ Regexed { barriers :: Bool, normalize :: Bool, sort :: Bool } |
+ Reduced { barriers :: Bool, normalize :: Bool, sort :: Bool } |
+ Duped { barriers :: Bool, normalize :: Bool, sort :: Bool } |
+ Diffed { barriers :: Bool, normalize :: Bool, sort :: Bool } |
+ Listed { barriers :: Bool, normalize :: Bool, sort :: Bool }
deriving (Data, Show)
-- | Description of the 'Regexed' mode.
name "v",
groupname "Common flags"]
where
- make_mode :: (Bool -> Bool -> Args) -> String -> (Annotate Ann)
+ make_mode :: (Bool -> Bool -> Bool -> Args) -> String -> (Annotate Ann)
make_mode ctor desc =
- record (ctor def def)
+ record (ctor def def def)
[ barriers := def
+= groupname "Common flags"
+= help barriers_help,
normalize := def
+= groupname "Common flags"
- += help normalize_help ]
+ += help normalize_help,
+ sort := def
+ += groupname "Common flags"
+ += help sort_help ]
+= details [" " ++ desc]
regexed = make_mode Regexed regexed_description
import Control.Monad (when)
import Data.List ((\\), intercalate)
+import qualified Data.List as List (sort)
import Data.Maybe (catMaybes, isNothing)
import System.Exit (ExitCode( ExitFailure ), exitWith)
import System.IO (stderr, hPutStrLn)
min_octet4 )
import qualified Cidr ( normalize )
import CommandLine(
- Args( Regexed, Reduced, Duped, Diffed, Listed, barriers, normalize ),
+ Args( Regexed, Reduced, Duped, Diffed, Listed, barriers, normalize, sort ),
get_args )
import ExitCodes ( exit_invalid_cidr )
import Octet ()
Reduced{} -> do
-- Pre-normalize all CIDRs if the user asked for it.
let nrml_func = if (normalize args) then Cidr.normalize else id
- mapM_ print (combine_all (map nrml_func valid_cidrs))
+ let sort_func = if (sort args) then List.sort else id :: [Cidr] -> [Cidr]
+ mapM_ (print . nrml_func) (sort_func $ combine_all valid_cidrs)
Duped{} ->
mapM_ print dupes
where
-# Test the pipe-to-sort example from the man page.
+# Test --sort example from the man page.
-echo "192.168.0.100/24 192.167.1.200/24" | dist/build/hath/hath reduced | sort
+# The default pipe-to-sort doesn't work.
+echo "10.0.121.32/28 10.0.93.248/29" | dist/build/hath/hath reduced | sort
>>>
-192.167.1.200/24
-192.168.0.100/24
+10.0.121.32/28
+10.0.93.248/29
+>>>= 0
+
+# Neither does a numeric sort.
+echo "10.0.121.32/28 10.0.93.248/29" | dist/build/hath/hath reduced | sort -n
+>>>
+10.0.121.32/28
+10.0.93.248/29
+>>>= 0
+
+# But the CIDR sort order does.
+echo "10.0.121.32/28 10.0.93.248/29" | dist/build/hath/hath reduced --sort
+>>>
+10.0.93.248/29
+10.0.121.32/28
>>>= 0