From 775f9763a4ff5422fca837c2fb213273adb772c9 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Wed, 29 May 2019 11:48:44 +0200 Subject: [PATCH] Add Augeas.label Simple binding for aug_label. --- augeas-c.c | 27 +++++++++++++++++++++++++++ augeas.ml | 2 ++ augeas.mli | 6 ++++++ 3 files changed, 35 insertions(+) diff --git a/augeas-c.c b/augeas-c.c index 5dbaa83..ae22dff 100644 --- a/augeas-c.c +++ b/augeas-c.c @@ -44,6 +44,7 @@ extern CAMLprim value ocaml_augeas_defvar (value tv, value namev, value exprv); extern CAMLprim value ocaml_augeas_get (value tv, value pathv); extern CAMLprim value ocaml_augeas_exists (value tv, value pathv); extern CAMLprim value ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv); +extern CAMLprim value ocaml_augeas_label (value tv, value pathv); extern CAMLprim value ocaml_augeas_mv (value tv, value srcv, value destv); extern CAMLprim value ocaml_augeas_rm (value tv, value pathv); extern CAMLprim value ocaml_augeas_match (value tv, value pathv); @@ -363,6 +364,32 @@ ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv) CAMLreturn (Val_unit); } +/* val label : t -> path -> string option */ +CAMLprim value +ocaml_augeas_label (value tv, value pathv) +{ + CAMLparam2 (tv, pathv); + CAMLlocal2 (optv, v); + augeas_t t = Augeas_t_val (tv); + const char *path = String_val (pathv); + const char *val; + int r; + + r = aug_label (t, path, &val); + if (r == 1 && val) { /* Return Some val */ + v = caml_copy_string (val); + optv = caml_alloc (1, 0); + Field (optv, 0) = v; + } else if (r == 0 || !val) /* Return None */ + optv = Val_int (0); + else if (r == -1) /* Error or multiple matches */ + raise_error (t, "Augeas.label"); + else + caml_failwith ("Augeas.label: bad return value"); + + CAMLreturn (optv); +} + /* val mv : t -> path -> path -> unit */ CAMLprim value ocaml_augeas_mv (value tv, value srcv, value destv) diff --git a/augeas.ml b/augeas.ml index 63379a2..006b76b 100644 --- a/augeas.ml +++ b/augeas.ml @@ -72,6 +72,8 @@ external exists : t -> path -> bool = "ocaml_augeas_exists" external insert : t -> ?before:bool -> path -> string -> unit = "ocaml_augeas_insert" +external label : t -> path -> string option + = "ocaml_augeas_label" external rm : t -> path -> int = "ocaml_augeas_rm" external matches : t -> path -> path list diff --git a/augeas.mli b/augeas.mli index c1862f1..dc98c62 100644 --- a/augeas.mli +++ b/augeas.mli @@ -115,6 +115,12 @@ val insert : t -> ?before:bool -> path -> string -> unit of [path]. By default it is inserted after [path], unless [~before:true] is specified. *) +val label : t -> path -> string option + (** [label t path] gets the label of [path]. + + Returns [Some value] when [path] matches only one node, and + that has an associated label. *) + val rm : t -> path -> int (** [rm t path] removes all nodes matching [path]. -- 1.8.3.1