1 /* Augeas OCaml bindings
2 * Copyright (C) 2008 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 (const char *msg)
38 caml_raise_with_string (*caml_named_value ("Augeas.Error"), msg);
41 /* Map OCaml flags to C flags. */
42 static int flag_map[] = {
43 /* AugSaveBackup */ AUG_SAVE_BACKUP,
44 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
45 /* AugTypeCheck */ AUG_TYPE_CHECK
48 /* Wrap and unwrap augeas_t handles, with a finalizer. */
49 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
52 augeas_t_finalize (value tv)
54 augeas_t t = Augeas_t_val (tv);
58 static struct custom_operations custom_operations = {
59 "augeas_t_custom_operations",
61 custom_compare_default,
63 custom_serialize_default,
64 custom_deserialize_default
67 static value Val_augeas_t (augeas_t t)
71 /* We could choose these so that the GC can make better decisions.
72 * See 18.9.2 of the OCaml manual.
77 rv = caml_alloc_custom (&custom_operations,
78 sizeof (augeas_t), used, max);
84 /* val create : string -> string option -> flag list -> t */
86 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
89 char *root = String_val (rootv);
94 /* Optional loadpath. */
96 loadpathv == Val_int (0)
98 : String_val (Field (loadpathv, 0));
100 /* Convert list of flags to C. */
101 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
102 i = Int_val (Field (flagsv, 0));
103 flags |= flag_map[i];
106 t = aug_init (root, loadpath, flags);
109 raise_error ("Augeas.create");
111 CAMLreturn (Val_augeas_t (t));
114 /* val close : t -> unit */
116 ocaml_augeas_close (value tv)
119 augeas_t t = Augeas_t_val (tv);
123 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
126 CAMLreturn (Val_unit);
129 /* val get : t -> path -> value option */
131 ocaml_augeas_get (value tv, value pathv)
133 CAMLparam2 (tv, pathv);
134 CAMLlocal2 (optv, v);
135 augeas_t t = Augeas_t_val (tv);
136 char *path = String_val (pathv);
140 r = aug_get (t, path, &val);
141 if (r == 1) { /* Return Some val */
142 v = caml_copy_string (val);
143 optv = caml_alloc (1, 0);
145 } else if (r == 0) /* Return None */
147 else if (r == -1) /* Error or multiple matches */
148 raise_error ("Augeas.get");
150 failwith ("Augeas.get: bad return value");
155 /* val exists : t -> path -> bool */
157 ocaml_augeas_exists (value tv, value pathv)
159 CAMLparam2 (tv, pathv);
161 augeas_t t = Augeas_t_val (tv);
162 char *path = String_val (pathv);
165 r = aug_get (t, path, NULL);
166 if (r == 1) /* Return true. */
168 else if (r == 0) /* Return false */
170 else if (r == -1) /* Error or multiple matches */
171 raise_error ("Augeas.exists");
173 failwith ("Augeas.exists: bad return value");
178 /* val insert : t -> ?before:bool -> path -> string -> unit */
180 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
182 CAMLparam4 (tv, beforev, pathv, labelv);
183 augeas_t t = Augeas_t_val (tv);
184 char *path = String_val (pathv);
185 char *label = String_val (labelv);
188 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
190 if (aug_insert (t, path, label, before) == -1)
191 raise_error ("Augeas.insert");
193 CAMLreturn (Val_unit);
196 /* val rm : t -> path -> int */
198 ocaml_augeas_rm (value tv, value pathv)
200 CAMLparam2 (tv, pathv);
201 augeas_t t = Augeas_t_val (tv);
202 char *path = String_val (path);
205 r = aug_rm (t, path);
207 raise_error ("Augeas.rm");
209 CAMLreturn (Val_int (r));
212 /* val matches : t -> path -> path list */
214 ocaml_augeas_match (value tv, value pathv)
216 CAMLparam2 (tv, pathv);
217 CAMLlocal3 (rv, v, cons);
218 augeas_t t = Augeas_t_val (tv);
219 char *path = String_val (path);
223 r = aug_match (t, path, &matches);
225 raise_error ("Augeas.match");
227 /* Copy the paths to a list. */
229 for (i = 0; i < r; ++i) {
230 v = caml_copy_string (matches[i]);
232 cons = caml_alloc (2, 0);
233 Field (cons, 1) = rv;
243 /* val save : t -> unit */
245 ocaml_augeas_save (value tv)
248 augeas_t t = Augeas_t_val (tv);
250 if (aug_save (t) == -1)
251 raise_error ("Augeas.save");
253 CAMLreturn (Val_unit);