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_minor;
93 const char *aug_err_details;
97 if (code == AUG_ENOMEM) {
100 caml_raise_out_of_memory ();
103 aug_err_minor = aug_error_minor_message (t);
104 aug_err_details = aug_error_details (t);
106 for (i = 0; i < error_map_len; ++i)
107 if (error_map[i] == code) {
112 if (ocaml_code != -1)
113 args[0] = Val_int (ocaml_code);
115 args[0] = caml_alloc (1, 0);
116 Store_field (args[0], 0, Val_int (code));
118 args[1] = caml_copy_string (msg);
119 args[2] = caml_copy_string (aug_err_minor ? : "");
120 args[3] = caml_copy_string (aug_err_details ? : "");
125 caml_raise_with_args (*exn, 4, args);
127 #define raise_error(t, msg) raise_error_and_maybe_close(t, msg, false)
130 raise_init_error (const char *msg)
132 value *exn = caml_named_value ("Augeas.Error");
135 args[0] = caml_alloc (1, 0);
136 Store_field (args[0], 0, Val_int (-1));
137 args[1] = caml_copy_string (msg);
138 args[2] = caml_copy_string ("augeas initialization failed");
139 args[3] = caml_copy_string ("");
141 caml_raise_with_args (*exn, 4, args);
145 Optstring_val (value strv)
147 if (strv == Val_int (0)) /* None */
149 else /* Some string */
150 return String_val (Field (strv, 0));
153 /* Map OCaml flags to C flags. */
154 static const int flag_map[] = {
155 /* AugSaveBackup */ AUG_SAVE_BACKUP,
156 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
157 /* AugTypeCheck */ AUG_TYPE_CHECK,
158 /* AugNoStdinc */ AUG_NO_STDINC,
159 /* AugSaveNoop */ AUG_SAVE_NOOP,
160 /* AugNoLoad */ AUG_NO_LOAD,
161 /* AugNoModlAutoload */ AUG_NO_MODL_AUTOLOAD,
162 /* AugEnableSpan */ AUG_ENABLE_SPAN,
163 /* AugNoErrClose */ AUG_NO_ERR_CLOSE,
164 /* AugTraceModuleLoading */ AUG_TRACE_MODULE_LOADING,
167 /* Wrap and unwrap augeas_t handles, with a finalizer. */
168 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
171 augeas_t_finalize (value tv)
173 augeas_t t = Augeas_t_val (tv);
174 if (t) aug_close (t);
177 static struct custom_operations custom_operations = {
178 (char *) "augeas_t_custom_operations",
180 custom_compare_default,
182 custom_serialize_default,
183 custom_deserialize_default,
184 custom_compare_ext_default,
187 static value Val_augeas_t (augeas_t t)
191 /* We could choose these so that the GC can make better decisions.
192 * See 18.9.2 of the OCaml manual.
197 rv = caml_alloc_custom (&custom_operations,
198 sizeof (augeas_t), used, max);
199 Augeas_t_val(rv) = t;
204 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
206 /* val create : string -> string option -> flag list -> t */
208 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
211 const char *root = String_val (rootv);
212 const char *loadpath = Optstring_val (loadpathv);
216 /* Convert list of flags to C. */
217 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
218 i = Int_val (Field (flagsv, 0));
219 flags |= flag_map[i];
222 /* Pass AUG_NO_ERR_CLOSE so we raise a detailed Augeas.Error. */
223 t = aug_init (root, loadpath, flags | AUG_NO_ERR_CLOSE);
226 raise_init_error ("Augeas.create");
228 if (aug_error (t) != AUG_NOERROR) {
229 raise_error_and_maybe_close (t, "Augeas.init", true);
232 CAMLreturn (Val_augeas_t (t));
235 /* val close : t -> unit */
237 ocaml_augeas_close (value tv)
240 augeas_t t = Augeas_t_val (tv);
244 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
247 CAMLreturn (Val_unit);
250 /* val defnode : t -> string -> string -> string option -> int * bool */
252 ocaml_augeas_defnode (value tv, value namev, value exprv, value valv)
254 CAMLparam4 (tv, namev, exprv, valv);
255 CAMLlocal2 (optv, v);
256 augeas_t t = Augeas_t_val (tv);
257 const char *name = String_val (namev);
258 const char *expr = String_val (exprv);
259 const char *val = Optstring_val (valv);
262 r = aug_defnode (t, name, expr, val, &created);
264 raise_error (t, "Augeas.defnode");
267 v = caml_alloc (2, 0);
268 Store_field (v, 0, Val_int (r));
269 Store_field (v, 1, Val_bool (created));
274 /* val defvar : t -> string -> string option -> int option */
276 ocaml_augeas_defvar (value tv, value namev, value exprv)
278 CAMLparam3 (tv, namev, exprv);
279 CAMLlocal2 (optv, v);
280 augeas_t t = Augeas_t_val (tv);
281 const char *name = String_val (namev);
282 const char *expr = Optstring_val (exprv);
285 r = aug_defvar (t, name, expr);
286 if (r > 0) { /* Return Some val */
288 optv = caml_alloc (1, 0);
290 } else if (r == 0) /* Return None */
292 else if (r == -1) /* Error or multiple matches */
293 raise_error (t, "Augeas.defvar");
295 caml_failwith ("Augeas.defvar: bad return value");
300 /* val get : t -> path -> value option */
302 ocaml_augeas_get (value tv, value pathv)
304 CAMLparam2 (tv, pathv);
305 CAMLlocal2 (optv, v);
306 augeas_t t = Augeas_t_val (tv);
307 const char *path = String_val (pathv);
311 r = aug_get (t, path, &val);
312 if (r == 1 && val) { /* Return Some val */
313 v = caml_copy_string (val);
314 optv = caml_alloc (1, 0);
316 } else if (r == 0 || !val) /* Return None */
318 else if (r == -1) /* Error or multiple matches */
319 raise_error (t, "Augeas.get");
321 caml_failwith ("Augeas.get: bad return value");
326 /* val exists : t -> path -> bool */
328 ocaml_augeas_exists (value tv, value pathv)
330 CAMLparam2 (tv, pathv);
332 augeas_t t = Augeas_t_val (tv);
333 const char *path = String_val (pathv);
336 r = aug_get (t, path, NULL);
337 if (r == 1) /* Return true. */
339 else if (r == 0) /* Return false */
341 else if (r == -1) /* Error or multiple matches */
342 raise_error (t, "Augeas.exists");
344 failwith ("Augeas.exists: bad return value");
349 /* val insert : t -> ?before:bool -> path -> string -> unit */
351 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
353 CAMLparam4 (tv, beforev, pathv, labelv);
354 augeas_t t = Augeas_t_val (tv);
355 const char *path = String_val (pathv);
356 const char *label = String_val (labelv);
359 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
361 if (aug_insert (t, path, label, before) == -1)
362 raise_error (t, "Augeas.insert");
364 CAMLreturn (Val_unit);
367 /* val label : t -> path -> string option */
369 ocaml_augeas_label (value tv, value pathv)
371 CAMLparam2 (tv, pathv);
372 CAMLlocal2 (optv, v);
373 augeas_t t = Augeas_t_val (tv);
374 const char *path = String_val (pathv);
378 r = aug_label (t, path, &val);
379 if (r == 1 && val) { /* Return Some val */
380 v = caml_copy_string (val);
381 optv = caml_alloc (1, 0);
383 } else if (r == 0 || !val) /* Return None */
385 else if (r == -1) /* Error or multiple matches */
386 raise_error (t, "Augeas.label");
388 caml_failwith ("Augeas.label: bad return value");
393 /* val mv : t -> path -> path -> unit */
395 ocaml_augeas_mv (value tv, value srcv, value destv)
397 CAMLparam3 (tv, srcv, destv);
398 augeas_t t = Augeas_t_val (tv);
399 const char *src = String_val (srcv);
400 const char *dest = String_val (destv);
402 if (aug_mv (t, src, dest) == -1)
403 raise_error (t, "Augeas.mv");
405 CAMLreturn (Val_unit);
408 /* val rm : t -> path -> int */
410 ocaml_augeas_rm (value tv, value pathv)
412 CAMLparam2 (tv, pathv);
413 augeas_t t = Augeas_t_val (tv);
414 const char *path = String_val (pathv);
417 r = aug_rm (t, path);
419 raise_error (t, "Augeas.rm");
421 CAMLreturn (Val_int (r));
424 /* val matches : t -> path -> path list */
426 ocaml_augeas_match (value tv, value pathv)
428 CAMLparam2 (tv, pathv);
429 CAMLlocal3 (rv, v, cons);
430 augeas_t t = Augeas_t_val (tv);
431 const char *path = String_val (pathv);
435 r = aug_match (t, path, &matches);
437 raise_error (t, "Augeas.matches");
439 /* Copy the paths to a list. */
441 for (i = 0; i < r; ++i) {
442 v = caml_copy_string (matches[i]);
444 cons = caml_alloc (2, 0);
445 Field (cons, 1) = rv;
455 /* val count_matches : t -> path -> int */
457 ocaml_augeas_count_matches (value tv, value pathv)
459 CAMLparam2 (tv, pathv);
460 augeas_t t = Augeas_t_val (tv);
461 const char *path = String_val (pathv);
464 r = aug_match (t, path, NULL);
466 raise_error (t, "Augeas.count_matches");
468 CAMLreturn (Val_int (r));
471 /* val save : t -> unit */
473 ocaml_augeas_save (value tv)
476 augeas_t t = Augeas_t_val (tv);
478 if (aug_save (t) == -1)
479 raise_error (t, "Augeas.save");
481 CAMLreturn (Val_unit);
484 /* val load : t -> unit */
486 ocaml_augeas_load (value tv)
489 augeas_t t = Augeas_t_val (tv);
491 if (aug_load (t) == -1)
492 raise_error (t, "Augeas.load");
494 CAMLreturn (Val_unit);
497 /* val set : t -> -> path -> value option -> unit */
499 ocaml_augeas_set (value tv, value pathv, value valuev)
501 CAMLparam3 (tv, pathv, valuev);
502 augeas_t t = Augeas_t_val (tv);
503 const char *path = String_val (pathv);
504 const char *val = Optstring_val (valuev);
506 if (aug_set (t, path, val) == -1)
507 raise_error (t, "Augeas.set");
509 CAMLreturn (Val_unit);
512 /* val setm : t -> path -> string option -> value option -> int */
514 ocaml_augeas_setm (value tv, value basev, value subv, value valv)
516 CAMLparam4 (tv, basev, subv, valv);
517 augeas_t t = Augeas_t_val (tv);
518 const char *base = String_val (basev);
519 const char *sub = Optstring_val (subv);
520 const char *val = Optstring_val (valv);
523 r = aug_setm (t, base, sub, val);
525 raise_error (t, "Augeas.setm");
527 CAMLreturn (Val_int (r));
530 /* val transform : t -> string -> string -> transform_mode -> unit */
532 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
534 CAMLparam4 (tv, lensv, filev, modev);
535 augeas_t t = Augeas_t_val (tv);
536 const char *lens = String_val (lensv);
537 const char *file = String_val (filev);
538 const int excl = Int_val (modev) == 1 ? 1 : 0;
540 if (aug_transform (t, lens, file, excl) == -1)
541 raise_error (t, "Augeas.transform");
543 CAMLreturn (Val_unit);
546 /* val source : t -> path -> path option */
548 ocaml_augeas_source (value tv, value pathv)
550 #ifdef HAVE_AUG_SOURCE
551 CAMLparam2 (tv, pathv);
552 CAMLlocal2 (optv, v);
553 augeas_t t = Augeas_t_val (tv);
554 const char *path = String_val (pathv);
558 r = aug_source (t, path, &file_path);
560 if (file_path) { /* Return Some file_path */
561 v = caml_copy_string (file_path);
562 optv = caml_alloc (1, 0);
565 } else /* Return None */
569 raise_error (t, "Augeas.source");
573 caml_failwith ("Augeas.source: function not implemented");