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>
35 #define NORETURN __attribute__ ((noreturn))
40 extern CAMLprim value ocaml_augeas_create (value rootv, value loadpathv, value flagsv);
41 extern CAMLprim value ocaml_augeas_close (value tv);
42 extern CAMLprim value ocaml_augeas_defnode (value tv, value namev, value exprv, value valv);
43 extern CAMLprim value ocaml_augeas_defvar (value tv, value namev, value exprv);
44 extern CAMLprim value ocaml_augeas_get (value tv, value pathv);
45 extern CAMLprim value ocaml_augeas_exists (value tv, value pathv);
46 extern CAMLprim value ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv);
47 extern CAMLprim value ocaml_augeas_label (value tv, value pathv);
48 extern CAMLprim value ocaml_augeas_mv (value tv, value srcv, value destv);
49 extern CAMLprim value ocaml_augeas_rm (value tv, value pathv);
50 extern CAMLprim value ocaml_augeas_match (value tv, value pathv);
51 extern CAMLprim value ocaml_augeas_count_matches (value tv, value pathv);
52 extern CAMLprim value ocaml_augeas_save (value tv);
53 extern CAMLprim value ocaml_augeas_load (value tv);
54 extern CAMLprim value ocaml_augeas_set (value tv, value pathv, value valuev);
55 extern CAMLprim value ocaml_augeas_setm (value tv, value basev, value subv, value valv);
56 extern CAMLprim value ocaml_augeas_transform (value tv, value lensv, value filev, value modev);
57 extern CAMLprim value ocaml_augeas_source (value tv, value pathv)
58 #ifndef HAVE_AUG_SOURCE
63 typedef augeas *augeas_t;
65 /* Map C aug_errcode_t to OCaml error_code. */
66 static const int error_map[] = {
67 /* AugErrInternal */ AUG_EINTERNAL,
68 /* AugErrPathX */ AUG_EPATHX,
69 /* AugErrNoMatch */ AUG_ENOMATCH,
70 /* AugErrMMatch */ AUG_EMMATCH,
71 /* AugErrSyntax */ AUG_ESYNTAX,
72 /* AugErrNoLens */ AUG_ENOLENS,
73 /* AugErrMXfm */ AUG_EMXFM,
74 /* AugErrNoSpan */ AUG_ENOSPAN,
75 /* AugErrMvDesc */ AUG_EMVDESC,
76 /* AugErrCmdRun */ AUG_ECMDRUN,
77 /* AugErrBadArg */ AUG_EBADARG,
78 /* AugErrLabel */ AUG_ELABEL,
79 /* AugErrCpDesc */ AUG_ECPDESC,
81 static const int error_map_len = sizeof error_map / sizeof error_map[0];
83 /* Raise an Augeas.Error exception, and optionally close the
87 raise_error_and_maybe_close (augeas_t t, const char *msg, bool close_handle)
89 value *exn = caml_named_value ("Augeas.Error");
91 const int code = aug_error (t);
92 const char *aug_err_msg;
93 const char *aug_err_minor;
94 const char *aug_err_details;
98 if (code == AUG_ENOMEM) {
101 caml_raise_out_of_memory ();
104 aug_err_msg = aug_error_message (t);
105 aug_err_minor = aug_error_minor_message (t);
106 aug_err_details = aug_error_details (t);
108 for (i = 0; i < error_map_len; ++i)
109 if (error_map[i] == code) {
114 if (ocaml_code != -1)
115 args[0] = Val_int (ocaml_code);
117 args[0] = caml_alloc (1, 0);
118 Store_field (args[0], 0, Val_int (code));
120 args[1] = caml_copy_string (msg);
121 args[2] = caml_copy_string (aug_err_msg);
122 args[3] = caml_copy_string (aug_err_minor ? : "");
123 args[4] = caml_copy_string (aug_err_details ? : "");
128 caml_raise_with_args (*exn, 5, args);
130 #define raise_error(t, msg) raise_error_and_maybe_close(t, msg, false)
133 raise_init_error (const char *msg)
135 value *exn = caml_named_value ("Augeas.Error");
138 args[0] = caml_alloc (1, 0);
139 Store_field (args[0], 0, Val_int (-1));
140 args[1] = caml_copy_string (msg);
141 args[2] = caml_copy_string ("aug_init failed");
142 args[3] = caml_copy_string ("augeas initialization failed");
143 args[4] = caml_copy_string ("");
145 caml_raise_with_args (*exn, 5, args);
149 Optstring_val (value strv)
151 if (strv == Val_int (0)) /* None */
153 else /* Some string */
154 return String_val (Field (strv, 0));
157 /* Map OCaml flags to C flags. */
158 static const int flag_map[] = {
159 /* AugSaveBackup */ AUG_SAVE_BACKUP,
160 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
161 /* AugTypeCheck */ AUG_TYPE_CHECK,
162 /* AugNoStdinc */ AUG_NO_STDINC,
163 /* AugSaveNoop */ AUG_SAVE_NOOP,
164 /* AugNoLoad */ AUG_NO_LOAD,
165 /* AugNoModlAutoload */ AUG_NO_MODL_AUTOLOAD,
166 /* AugEnableSpan */ AUG_ENABLE_SPAN,
167 /* AugNoErrClose */ AUG_NO_ERR_CLOSE,
168 /* AugTraceModuleLoading */ AUG_TRACE_MODULE_LOADING,
171 /* Wrap and unwrap augeas_t handles, with a finalizer. */
172 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
175 augeas_t_finalize (value tv)
177 augeas_t t = Augeas_t_val (tv);
178 if (t) aug_close (t);
181 static struct custom_operations custom_operations = {
182 (char *) "augeas_t_custom_operations",
184 custom_compare_default,
186 custom_serialize_default,
187 custom_deserialize_default,
188 custom_compare_ext_default,
191 static value Val_augeas_t (augeas_t t)
195 /* We could choose these so that the GC can make better decisions.
196 * See 18.9.2 of the OCaml manual.
201 rv = caml_alloc_custom (&custom_operations,
202 sizeof (augeas_t), used, max);
203 Augeas_t_val(rv) = t;
208 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
210 /* val create : string -> string option -> flag list -> t */
212 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
215 const char *root = String_val (rootv);
216 const char *loadpath = Optstring_val (loadpathv);
220 /* Convert list of flags to C. */
221 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
222 i = Int_val (Field (flagsv, 0));
223 flags |= flag_map[i];
226 /* Pass AUG_NO_ERR_CLOSE so we raise a detailed Augeas.Error. */
227 t = aug_init (root, loadpath, flags | AUG_NO_ERR_CLOSE);
230 raise_init_error ("Augeas.create");
232 if (aug_error (t) != AUG_NOERROR) {
233 raise_error_and_maybe_close (t, "Augeas.init", true);
236 CAMLreturn (Val_augeas_t (t));
239 /* val close : t -> unit */
241 ocaml_augeas_close (value tv)
244 augeas_t t = Augeas_t_val (tv);
248 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
251 CAMLreturn (Val_unit);
254 /* val defnode : t -> string -> string -> string option -> int * bool */
256 ocaml_augeas_defnode (value tv, value namev, value exprv, value valv)
258 CAMLparam4 (tv, namev, exprv, valv);
259 CAMLlocal2 (optv, v);
260 augeas_t t = Augeas_t_val (tv);
261 const char *name = String_val (namev);
262 const char *expr = String_val (exprv);
263 const char *val = Optstring_val (valv);
266 r = aug_defnode (t, name, expr, val, &created);
268 raise_error (t, "Augeas.defnode");
271 v = caml_alloc (2, 0);
272 Store_field (v, 0, Val_int (r));
273 Store_field (v, 1, Val_bool (created));
278 /* val defvar : t -> string -> string option -> int option */
280 ocaml_augeas_defvar (value tv, value namev, value exprv)
282 CAMLparam3 (tv, namev, exprv);
283 CAMLlocal2 (optv, v);
284 augeas_t t = Augeas_t_val (tv);
285 const char *name = String_val (namev);
286 const char *expr = Optstring_val (exprv);
289 r = aug_defvar (t, name, expr);
290 if (r > 0) { /* Return Some val */
292 optv = caml_alloc (1, 0);
294 } else if (r == 0) /* Return None */
296 else if (r == -1) /* Error or multiple matches */
297 raise_error (t, "Augeas.defvar");
299 caml_failwith ("Augeas.defvar: bad return value");
304 /* val get : t -> path -> value option */
306 ocaml_augeas_get (value tv, value pathv)
308 CAMLparam2 (tv, pathv);
309 CAMLlocal2 (optv, v);
310 augeas_t t = Augeas_t_val (tv);
311 const char *path = String_val (pathv);
315 r = aug_get (t, path, &val);
316 if (r == 1 && val) { /* Return Some val */
317 v = caml_copy_string (val);
318 optv = caml_alloc (1, 0);
320 } else if (r == 0 || !val) /* Return None */
322 else if (r == -1) /* Error or multiple matches */
323 raise_error (t, "Augeas.get");
325 caml_failwith ("Augeas.get: bad return value");
330 /* val exists : t -> path -> bool */
332 ocaml_augeas_exists (value tv, value pathv)
334 CAMLparam2 (tv, pathv);
336 augeas_t t = Augeas_t_val (tv);
337 const char *path = String_val (pathv);
340 r = aug_get (t, path, NULL);
341 if (r == 1) /* Return true. */
343 else if (r == 0) /* Return false */
345 else if (r == -1) /* Error or multiple matches */
346 raise_error (t, "Augeas.exists");
348 failwith ("Augeas.exists: bad return value");
353 /* val insert : t -> ?before:bool -> path -> string -> unit */
355 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
357 CAMLparam4 (tv, beforev, pathv, labelv);
358 augeas_t t = Augeas_t_val (tv);
359 const char *path = String_val (pathv);
360 const char *label = String_val (labelv);
363 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
365 if (aug_insert (t, path, label, before) == -1)
366 raise_error (t, "Augeas.insert");
368 CAMLreturn (Val_unit);
371 /* val label : t -> path -> string option */
373 ocaml_augeas_label (value tv, value pathv)
375 CAMLparam2 (tv, pathv);
376 CAMLlocal2 (optv, v);
377 augeas_t t = Augeas_t_val (tv);
378 const char *path = String_val (pathv);
382 r = aug_label (t, path, &val);
383 if (r == 1 && val) { /* Return Some val */
384 v = caml_copy_string (val);
385 optv = caml_alloc (1, 0);
387 } else if (r == 0 || !val) /* Return None */
389 else if (r == -1) /* Error or multiple matches */
390 raise_error (t, "Augeas.label");
392 caml_failwith ("Augeas.label: bad return value");
397 /* val mv : t -> path -> path -> unit */
399 ocaml_augeas_mv (value tv, value srcv, value destv)
401 CAMLparam3 (tv, srcv, destv);
402 augeas_t t = Augeas_t_val (tv);
403 const char *src = String_val (srcv);
404 const char *dest = String_val (destv);
406 if (aug_mv (t, src, dest) == -1)
407 raise_error (t, "Augeas.mv");
409 CAMLreturn (Val_unit);
412 /* val rm : t -> path -> int */
414 ocaml_augeas_rm (value tv, value pathv)
416 CAMLparam2 (tv, pathv);
417 augeas_t t = Augeas_t_val (tv);
418 const char *path = String_val (pathv);
421 r = aug_rm (t, path);
423 raise_error (t, "Augeas.rm");
425 CAMLreturn (Val_int (r));
428 /* val matches : t -> path -> path list */
430 ocaml_augeas_match (value tv, value pathv)
432 CAMLparam2 (tv, pathv);
433 CAMLlocal3 (rv, v, cons);
434 augeas_t t = Augeas_t_val (tv);
435 const char *path = String_val (pathv);
439 r = aug_match (t, path, &matches);
441 raise_error (t, "Augeas.matches");
443 /* Copy the paths to a list. */
445 for (i = 0; i < r; ++i) {
446 v = caml_copy_string (matches[i]);
448 cons = caml_alloc (2, 0);
449 Field (cons, 1) = rv;
459 /* val count_matches : t -> path -> int */
461 ocaml_augeas_count_matches (value tv, value pathv)
463 CAMLparam2 (tv, pathv);
464 augeas_t t = Augeas_t_val (tv);
465 const char *path = String_val (pathv);
468 r = aug_match (t, path, NULL);
470 raise_error (t, "Augeas.count_matches");
472 CAMLreturn (Val_int (r));
475 /* val save : t -> unit */
477 ocaml_augeas_save (value tv)
480 augeas_t t = Augeas_t_val (tv);
482 if (aug_save (t) == -1)
483 raise_error (t, "Augeas.save");
485 CAMLreturn (Val_unit);
488 /* val load : t -> unit */
490 ocaml_augeas_load (value tv)
493 augeas_t t = Augeas_t_val (tv);
495 if (aug_load (t) == -1)
496 raise_error (t, "Augeas.load");
498 CAMLreturn (Val_unit);
501 /* val set : t -> -> path -> value option -> unit */
503 ocaml_augeas_set (value tv, value pathv, value valuev)
505 CAMLparam3 (tv, pathv, valuev);
506 augeas_t t = Augeas_t_val (tv);
507 const char *path = String_val (pathv);
508 const char *val = Optstring_val (valuev);
510 if (aug_set (t, path, val) == -1)
511 raise_error (t, "Augeas.set");
513 CAMLreturn (Val_unit);
516 /* val setm : t -> path -> string option -> value option -> int */
518 ocaml_augeas_setm (value tv, value basev, value subv, value valv)
520 CAMLparam4 (tv, basev, subv, valv);
521 augeas_t t = Augeas_t_val (tv);
522 const char *base = String_val (basev);
523 const char *sub = Optstring_val (subv);
524 const char *val = Optstring_val (valv);
527 r = aug_setm (t, base, sub, val);
529 raise_error (t, "Augeas.setm");
531 CAMLreturn (Val_int (r));
534 /* val transform : t -> string -> string -> transform_mode -> unit */
536 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
538 CAMLparam4 (tv, lensv, filev, modev);
539 augeas_t t = Augeas_t_val (tv);
540 const char *lens = String_val (lensv);
541 const char *file = String_val (filev);
542 const int excl = Int_val (modev) == 1 ? 1 : 0;
544 if (aug_transform (t, lens, file, excl) == -1)
545 raise_error (t, "Augeas.transform");
547 CAMLreturn (Val_unit);
550 /* val source : t -> path -> path option */
552 ocaml_augeas_source (value tv, value pathv)
554 #ifdef HAVE_AUG_SOURCE
555 CAMLparam2 (tv, pathv);
556 CAMLlocal2 (optv, v);
557 augeas_t t = Augeas_t_val (tv);
558 const char *path = String_val (pathv);
562 r = aug_source (t, path, &file_path);
564 if (file_path) { /* Return Some file_path */
565 v = caml_copy_string (file_path);
566 optv = caml_alloc (1, 0);
569 } else /* Return None */
573 raise_error (t, "Augeas.source");
577 caml_failwith ("Augeas.source: function not implemented");