1 /* Augeas OCaml bindings
2 * Copyright (C) 2008-2017 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 extern CAMLprim value ocaml_augeas_create (value rootv, value loadpathv, value flagsv);
33 extern CAMLprim value ocaml_augeas_close (value tv);
34 extern CAMLprim value ocaml_augeas_get (value tv, value pathv);
35 extern CAMLprim value ocaml_augeas_exists (value tv, value pathv);
36 extern CAMLprim value ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv);
37 extern CAMLprim value ocaml_augeas_rm (value tv, value pathv);
38 extern CAMLprim value ocaml_augeas_match (value tv, value pathv);
39 extern CAMLprim value ocaml_augeas_count_matches (value tv, value pathv);
40 extern CAMLprim value ocaml_augeas_save (value tv);
41 extern CAMLprim value ocaml_augeas_load (value tv);
42 extern CAMLprim value ocaml_augeas_set (value tv, value pathv, value valuev);
43 extern CAMLprim value ocaml_augeas_transform (value tv, value lensv, value filev, value modev);
44 extern CAMLprim value ocaml_augeas_source (value tv, value pathv);
46 typedef augeas *augeas_t;
48 /* Map C aug_errcode_t to OCaml error_code. */
49 static const int error_map[] = {
50 /* AugErrInternal */ AUG_EINTERNAL,
51 /* AugErrPathX */ AUG_EPATHX,
52 /* AugErrNoMatch */ AUG_ENOMATCH,
53 /* AugErrMMatch */ AUG_EMMATCH,
54 /* AugErrSyntax */ AUG_ESYNTAX,
55 /* AugErrNoLens */ AUG_ENOLENS,
56 /* AugErrMXfm */ AUG_EMXFM,
57 /* AugErrNoSpan */ AUG_ENOSPAN,
58 /* AugErrMvDesc */ AUG_EMVDESC,
59 /* AugErrCmdRun */ AUG_ECMDRUN,
60 /* AugErrBadArg */ AUG_EBADARG,
61 /* AugErrLabel */ AUG_ELABEL,
62 /* AugErrCpDesc */ AUG_ECPDESC,
64 static const int error_map_len = sizeof error_map / sizeof error_map[0];
66 /* Raise an Augeas.Error exception. */
68 raise_error (augeas_t t, const char *msg)
70 value *exn = caml_named_value ("Augeas.Error");
72 const int code = aug_error (t);
73 const char *aug_err_minor;
74 const char *aug_err_details;
78 if (code == AUG_ENOMEM)
79 caml_raise_out_of_memory ();
81 aug_err_minor = aug_error_minor_message (t);
82 aug_err_details = aug_error_details (t);
84 for (i = 0; i < error_map_len; ++i)
85 if (error_map[i] == code) {
91 args[0] = Val_int (ocaml_code);
93 args[0] = caml_alloc (1, 0);
94 Store_field (args[0], 0, Val_int (code));
96 args[1] = caml_copy_string (msg);
97 args[2] = caml_copy_string (aug_err_minor ? : "");
98 args[3] = caml_copy_string (aug_err_details ? : "");
100 caml_raise_with_args (*exn, 4, args);
104 raise_init_error (const char *msg)
106 value *exn = caml_named_value ("Augeas.Error");
109 args[0] = caml_alloc (1, 0);
110 Store_field (args[0], 0, Val_int (-1));
111 args[1] = caml_copy_string (msg);
112 args[2] = caml_copy_string ("augeas initialization failed");
113 args[3] = caml_copy_string ("");
115 caml_raise_with_args (*exn, 4, args);
118 /* Map OCaml flags to C flags. */
119 static const int flag_map[] = {
120 /* AugSaveBackup */ AUG_SAVE_BACKUP,
121 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
122 /* AugTypeCheck */ AUG_TYPE_CHECK,
123 /* AugNoStdinc */ AUG_NO_STDINC,
124 /* AugSaveNoop */ AUG_SAVE_NOOP,
125 /* AugNoLoad */ AUG_NO_LOAD,
128 /* Wrap and unwrap augeas_t handles, with a finalizer. */
129 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
132 augeas_t_finalize (value tv)
134 augeas_t t = Augeas_t_val (tv);
135 if (t) aug_close (t);
138 static struct custom_operations custom_operations = {
139 (char *) "augeas_t_custom_operations",
141 custom_compare_default,
143 custom_serialize_default,
144 custom_deserialize_default,
145 custom_compare_ext_default,
148 static value Val_augeas_t (augeas_t t)
152 /* We could choose these so that the GC can make better decisions.
153 * See 18.9.2 of the OCaml manual.
158 rv = caml_alloc_custom (&custom_operations,
159 sizeof (augeas_t), used, max);
160 Augeas_t_val(rv) = t;
165 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
167 /* val create : string -> string option -> flag list -> t */
169 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
172 const char *root = String_val (rootv);
173 const char *loadpath;
177 /* Optional loadpath. */
179 loadpathv == Val_int (0)
181 : String_val (Field (loadpathv, 0));
183 /* Convert list of flags to C. */
184 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
185 i = Int_val (Field (flagsv, 0));
186 flags |= flag_map[i];
189 t = aug_init (root, loadpath, flags);
192 raise_init_error ("Augeas.create");
194 CAMLreturn (Val_augeas_t (t));
197 /* val close : t -> unit */
199 ocaml_augeas_close (value tv)
202 augeas_t t = Augeas_t_val (tv);
206 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
209 CAMLreturn (Val_unit);
212 /* val get : t -> path -> value option */
214 ocaml_augeas_get (value tv, value pathv)
216 CAMLparam2 (tv, pathv);
217 CAMLlocal2 (optv, v);
218 augeas_t t = Augeas_t_val (tv);
219 const char *path = String_val (pathv);
223 r = aug_get (t, path, &val);
224 if (r == 1 && val) { /* Return Some val */
225 v = caml_copy_string (val);
226 optv = caml_alloc (1, 0);
228 } else if (r == 0 || !val) /* Return None */
230 else if (r == -1) /* Error or multiple matches */
231 raise_error (t, "Augeas.get");
233 caml_failwith ("Augeas.get: bad return value");
238 /* val exists : t -> path -> bool */
240 ocaml_augeas_exists (value tv, value pathv)
242 CAMLparam2 (tv, pathv);
244 augeas_t t = Augeas_t_val (tv);
245 const char *path = String_val (pathv);
248 r = aug_get (t, path, NULL);
249 if (r == 1) /* Return true. */
251 else if (r == 0) /* Return false */
253 else if (r == -1) /* Error or multiple matches */
254 raise_error (t, "Augeas.exists");
256 failwith ("Augeas.exists: bad return value");
261 /* val insert : t -> ?before:bool -> path -> string -> unit */
263 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
265 CAMLparam4 (tv, beforev, pathv, labelv);
266 augeas_t t = Augeas_t_val (tv);
267 const char *path = String_val (pathv);
268 const char *label = String_val (labelv);
271 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
273 if (aug_insert (t, path, label, before) == -1)
274 raise_error (t, "Augeas.insert");
276 CAMLreturn (Val_unit);
279 /* val rm : t -> path -> int */
281 ocaml_augeas_rm (value tv, value pathv)
283 CAMLparam2 (tv, pathv);
284 augeas_t t = Augeas_t_val (tv);
285 const char *path = String_val (pathv);
288 r = aug_rm (t, path);
290 raise_error (t, "Augeas.rm");
292 CAMLreturn (Val_int (r));
295 /* val matches : t -> path -> path list */
297 ocaml_augeas_match (value tv, value pathv)
299 CAMLparam2 (tv, pathv);
300 CAMLlocal3 (rv, v, cons);
301 augeas_t t = Augeas_t_val (tv);
302 const char *path = String_val (pathv);
306 r = aug_match (t, path, &matches);
308 raise_error (t, "Augeas.matches");
310 /* Copy the paths to a list. */
312 for (i = 0; i < r; ++i) {
313 v = caml_copy_string (matches[i]);
315 cons = caml_alloc (2, 0);
316 Field (cons, 1) = rv;
326 /* val count_matches : t -> path -> int */
328 ocaml_augeas_count_matches (value tv, value pathv)
330 CAMLparam2 (tv, pathv);
331 augeas_t t = Augeas_t_val (tv);
332 const char *path = String_val (pathv);
335 r = aug_match (t, path, NULL);
337 raise_error (t, "Augeas.count_matches");
339 CAMLreturn (Val_int (r));
342 /* val save : t -> unit */
344 ocaml_augeas_save (value tv)
347 augeas_t t = Augeas_t_val (tv);
349 if (aug_save (t) == -1)
350 raise_error (t, "Augeas.save");
352 CAMLreturn (Val_unit);
355 /* val load : t -> unit */
357 ocaml_augeas_load (value tv)
360 augeas_t t = Augeas_t_val (tv);
362 if (aug_load (t) == -1)
363 raise_error (t, "Augeas.load");
365 CAMLreturn (Val_unit);
368 /* val set : t -> -> path -> value option -> unit */
370 ocaml_augeas_set (value tv, value pathv, value valuev)
372 CAMLparam3 (tv, pathv, valuev);
373 augeas_t t = Augeas_t_val (tv);
374 const char *path = String_val (pathv);
378 valuev == Val_int (0)
380 : String_val (Field (valuev, 0));
382 if (aug_set (t, path, val) == -1)
383 raise_error (t, "Augeas.set");
385 CAMLreturn (Val_unit);
388 /* val transform : t -> string -> string -> transform_mode -> unit */
390 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
392 CAMLparam4 (tv, lensv, filev, modev);
393 augeas_t t = Augeas_t_val (tv);
394 const char *lens = String_val (lensv);
395 const char *file = String_val (filev);
396 const int excl = Int_val (modev) == 1 ? 1 : 0;
398 if (aug_transform (t, lens, file, excl) == -1)
399 raise_error (t, "Augeas.transform");
401 CAMLreturn (Val_unit);
404 /* val source : t -> path -> path option */
406 ocaml_augeas_source (value tv, value pathv)
408 #ifdef HAVE_AUG_SOURCE
409 CAMLparam2 (tv, pathv);
410 CAMLlocal2 (optv, v);
411 augeas_t t = Augeas_t_val (tv);
412 const char *path = String_val (pathv);
416 r = aug_source (t, path, &file_path);
418 if (file_path) { /* Return Some file_path */
419 v = caml_copy_string (file_path);
420 optv = caml_alloc (1, 0);
423 } else /* Return None */
427 raise_error (t, "Augeas.source");
431 caml_failwith ("Augeas.source: function not implemented");