summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2021-05-21 15:22:46 -0500
committerJohn Estabrook <jestabro@vyos.io>2021-06-08 15:58:54 -0500
commitcf462028d5921efd53996cee0c340e8d7f402784 (patch)
tree08898f6608d219eea8c88bac31a2b2acd71e33f2
parented9a41190997d4a46c1e8efd21ac06aeb0965965 (diff)
downloadvyos-utils-cf462028d5921efd53996cee0c340e8d7f402784.tar.gz
vyos-utils-cf462028d5921efd53996cee0c340e8d7f402784.zip
T3574: add support for --grp argument
Add support for --grp argument, which joins the arguments following (up until the next --grp) with logical and; arguments preceding a group, and other groups following, are joined by logical or.
-rw-r--r--src/validate_value.ml55
1 files changed, 45 insertions, 10 deletions
diff --git a/src/validate_value.ml b/src/validate_value.ml
index 0b4148f..6d52618 100644
--- a/src/validate_value.ml
+++ b/src/validate_value.ml
@@ -1,7 +1,30 @@
-type check = Regex of string | Exec of string
+type check = Regex of string | Exec of string | Grp | Faux
-let validate_value buf value_constraint value =
+let checks = ref []
+
+let find_next_grp n l =
+ let rec aux i = function
+ | [] -> List.length l
+ | h::t ->
+ if i > n && h = Grp then i
+ else aux (i+1) t
+ in aux 0 l
+
+let get_next_range =
+ let n = ref (-1) in
+ let f () =
+ let i = !n and j = (n := find_next_grp !n !checks; !n) in
+ (i, j) in
+ f
+
+let rec validate_value buf value_constraint value =
match value_constraint with
+ | Faux -> false
+ | Grp ->
+ let (k, j) = get_next_range () in
+ let in_grp i = if i > k && i < j then true else false in
+ let conj_checks = List.filteri (fun i _ -> in_grp i) !checks in
+ List.fold_left (&&) true (List.map (fun e -> validate_value buf e value) conj_checks)
| Regex s ->
(try let _ = Pcre.exec ~pat:s value in true
with Not_found -> false)
@@ -24,25 +47,37 @@ let validate_value buf value_constraint value =
let value = ref ""
-let checks = ref []
-
let args = [
("--regex", Arg.String (fun s -> checks := (Regex s) :: !checks), "Check the value against a regex");
("--exec", Arg.String (fun s -> checks := (Exec s) :: !checks), "Check the value against an external command");
+ ("--grp", Arg.Unit (fun () -> checks := (Grp) :: !checks), "Group following arguments, joining results with logical and");
("--value", Arg.String (fun s -> value := s), "Value to check");
]
let usage = Printf.sprintf "Usage: %s [OPTIONS] <number>" Sys.argv.(0)
let () = Arg.parse args (fun _ -> ()) usage
-let _ =
- let buf = Buffer.create 4096 in
+let buf = Buffer.create 4096
+
+let mask l =
+ let (k, j) = get_next_range () in
+ let imask i e =
+ match e with
+ | Grp -> Grp
+ | _ -> if i >= k && i <= j then e else Faux in
+ List.mapi (fun i e -> imask i e) l
+
+let validate =
+ checks := List.rev(!checks);
let value = !value in
- let checks = !checks in
- match checks with
- | [] -> exit 0
+ let disj_checks = mask !checks in
+ match disj_checks with
+ | [] -> false
| _ ->
- List.iter (fun c -> if (validate_value buf c value) then exit 0 else ()) checks;
+ List.fold_left (||) false (List.map (fun c -> validate_value buf c value) disj_checks)
+
+let _ =
+ if validate then exit 0 else
(* If we got this far, value validation failed.
Show the user output from the validators.
*)