1 /* Augeas OCaml bindings
2 * Copyright (C) 2008-2012 Red Hat Inc., Richard W.M. Jones
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 * $Id: augeas_c.c,v 1.1 2008/05/06 10:48:20 rjones Exp $
25 #include <caml/alloc.h>
26 #include <caml/memory.h>
27 #include <caml/mlvalues.h>
28 #include <caml/fail.h>
29 #include <caml/callback.h>
30 #include <caml/custom.h>
32 typedef augeas *augeas_t;
34 /* Raise an Augeas.Error exception. */
36 raise_error (augeas_t t, const char *msg)
38 const int code = aug_error (t);
40 if (code == AUG_ENOMEM)
41 caml_raise_out_of_memory ();
43 caml_raise_with_string (*caml_named_value ("Augeas.Error"), msg);
47 raise_init_error (const char *msg)
49 caml_raise_with_string (*caml_named_value ("Augeas.Error"), msg);
52 /* Map OCaml flags to C flags. */
53 static int flag_map[] = {
54 /* AugSaveBackup */ AUG_SAVE_BACKUP,
55 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
56 /* AugTypeCheck */ AUG_TYPE_CHECK,
57 /* AugNoStdinc */ AUG_NO_STDINC,
58 /* AugSaveNoop */ AUG_SAVE_NOOP,
59 /* AugNoLoad */ AUG_NO_LOAD,
62 /* Wrap and unwrap augeas_t handles, with a finalizer. */
63 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
66 augeas_t_finalize (value tv)
68 augeas_t t = Augeas_t_val (tv);
72 static struct custom_operations custom_operations = {
73 (char *) "augeas_t_custom_operations",
75 custom_compare_default,
77 custom_serialize_default,
78 custom_deserialize_default
81 static value Val_augeas_t (augeas_t t)
85 /* We could choose these so that the GC can make better decisions.
86 * See 18.9.2 of the OCaml manual.
91 rv = caml_alloc_custom (&custom_operations,
92 sizeof (augeas_t), used, max);
98 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
100 /* val create : string -> string option -> flag list -> t */
102 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
105 char *root = String_val (rootv);
110 /* Optional loadpath. */
112 loadpathv == Val_int (0)
114 : String_val (Field (loadpathv, 0));
116 /* Convert list of flags to C. */
117 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
118 i = Int_val (Field (flagsv, 0));
119 flags |= flag_map[i];
122 t = aug_init (root, loadpath, flags);
125 raise_init_error ("Augeas.create");
127 CAMLreturn (Val_augeas_t (t));
130 /* val close : t -> unit */
132 ocaml_augeas_close (value tv)
135 augeas_t t = Augeas_t_val (tv);
139 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
142 CAMLreturn (Val_unit);
145 /* val get : t -> path -> value option */
147 ocaml_augeas_get (value tv, value pathv)
149 CAMLparam2 (tv, pathv);
150 CAMLlocal2 (optv, v);
151 augeas_t t = Augeas_t_val (tv);
152 char *path = String_val (pathv);
156 r = aug_get (t, path, &val);
157 if (r == 1) { /* Return Some val */
158 v = caml_copy_string (val);
159 optv = caml_alloc (1, 0);
161 } else if (r == 0) /* Return None */
163 else if (r == -1) /* Error or multiple matches */
164 raise_error (t, "Augeas.get");
166 failwith ("Augeas.get: bad return value");
171 /* val exists : t -> path -> bool */
173 ocaml_augeas_exists (value tv, value pathv)
175 CAMLparam2 (tv, pathv);
177 augeas_t t = Augeas_t_val (tv);
178 char *path = String_val (pathv);
181 r = aug_get (t, path, NULL);
182 if (r == 1) /* Return true. */
184 else if (r == 0) /* Return false */
186 else if (r == -1) /* Error or multiple matches */
187 raise_error (t, "Augeas.exists");
189 failwith ("Augeas.exists: bad return value");
194 /* val insert : t -> ?before:bool -> path -> string -> unit */
196 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
198 CAMLparam4 (tv, beforev, pathv, labelv);
199 augeas_t t = Augeas_t_val (tv);
200 char *path = String_val (pathv);
201 char *label = String_val (labelv);
204 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
206 if (aug_insert (t, path, label, before) == -1)
207 raise_error (t, "Augeas.insert");
209 CAMLreturn (Val_unit);
212 /* val rm : t -> path -> int */
214 ocaml_augeas_rm (value tv, value pathv)
216 CAMLparam2 (tv, pathv);
217 augeas_t t = Augeas_t_val (tv);
218 char *path = String_val (pathv);
221 r = aug_rm (t, path);
223 raise_error (t, "Augeas.rm");
225 CAMLreturn (Val_int (r));
228 /* val matches : t -> path -> path list */
230 ocaml_augeas_match (value tv, value pathv)
232 CAMLparam2 (tv, pathv);
233 CAMLlocal3 (rv, v, cons);
234 augeas_t t = Augeas_t_val (tv);
235 char *path = String_val (pathv);
239 r = aug_match (t, path, &matches);
241 raise_error (t, "Augeas.matches");
243 /* Copy the paths to a list. */
245 for (i = 0; i < r; ++i) {
246 v = caml_copy_string (matches[i]);
248 cons = caml_alloc (2, 0);
249 Field (cons, 1) = rv;
259 /* val count_matches : t -> path -> int */
261 ocaml_augeas_count_matches (value tv, value pathv)
263 CAMLparam2 (tv, pathv);
264 augeas_t t = Augeas_t_val (tv);
265 char *path = String_val (pathv);
268 r = aug_match (t, path, NULL);
270 raise_error (t, "Augeas.count_matches");
272 CAMLreturn (Val_int (r));
275 /* val save : t -> unit */
277 ocaml_augeas_save (value tv)
280 augeas_t t = Augeas_t_val (tv);
282 if (aug_save (t) == -1)
283 raise_error (t, "Augeas.save");
285 CAMLreturn (Val_unit);
288 /* val load : t -> unit */
290 ocaml_augeas_load (value tv)
293 augeas_t t = Augeas_t_val (tv);
295 if (aug_load (t) == -1)
296 raise_error (t, "Augeas.load");
298 CAMLreturn (Val_unit);