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_mv (value tv, value srcv, value destv);
48 extern CAMLprim value ocaml_augeas_rm (value tv, value pathv);
49 extern CAMLprim value ocaml_augeas_match (value tv, value pathv);
50 extern CAMLprim value ocaml_augeas_count_matches (value tv, value pathv);
51 extern CAMLprim value ocaml_augeas_save (value tv);
52 extern CAMLprim value ocaml_augeas_load (value tv);
53 extern CAMLprim value ocaml_augeas_set (value tv, value pathv, value valuev);
54 extern CAMLprim value ocaml_augeas_setm (value tv, value basev, value subv, value valv);
55 extern CAMLprim value ocaml_augeas_transform (value tv, value lensv, value filev, value modev);
56 extern CAMLprim value ocaml_augeas_source (value tv, value pathv)
57 #ifndef HAVE_AUG_SOURCE
62 typedef augeas *augeas_t;
64 /* Map C aug_errcode_t to OCaml error_code. */
65 static const int error_map[] = {
66 /* AugErrInternal */ AUG_EINTERNAL,
67 /* AugErrPathX */ AUG_EPATHX,
68 /* AugErrNoMatch */ AUG_ENOMATCH,
69 /* AugErrMMatch */ AUG_EMMATCH,
70 /* AugErrSyntax */ AUG_ESYNTAX,
71 /* AugErrNoLens */ AUG_ENOLENS,
72 /* AugErrMXfm */ AUG_EMXFM,
73 /* AugErrNoSpan */ AUG_ENOSPAN,
74 /* AugErrMvDesc */ AUG_EMVDESC,
75 /* AugErrCmdRun */ AUG_ECMDRUN,
76 /* AugErrBadArg */ AUG_EBADARG,
77 /* AugErrLabel */ AUG_ELABEL,
78 /* AugErrCpDesc */ AUG_ECPDESC,
80 static const int error_map_len = sizeof error_map / sizeof error_map[0];
82 /* Raise an Augeas.Error exception, and optionally close the
86 raise_error_and_maybe_close (augeas_t t, const char *msg, bool close_handle)
88 value *exn = caml_named_value ("Augeas.Error");
90 const int code = aug_error (t);
91 const char *aug_err_minor;
92 const char *aug_err_details;
96 if (code == AUG_ENOMEM) {
99 caml_raise_out_of_memory ();
102 aug_err_minor = aug_error_minor_message (t);
103 aug_err_details = aug_error_details (t);
105 for (i = 0; i < error_map_len; ++i)
106 if (error_map[i] == code) {
111 if (ocaml_code != -1)
112 args[0] = Val_int (ocaml_code);
114 args[0] = caml_alloc (1, 0);
115 Store_field (args[0], 0, Val_int (code));
117 args[1] = caml_copy_string (msg);
118 args[2] = caml_copy_string (aug_err_minor ? : "");
119 args[3] = caml_copy_string (aug_err_details ? : "");
124 caml_raise_with_args (*exn, 4, args);
126 #define raise_error(t, msg) raise_error_and_maybe_close(t, msg, false)
129 raise_init_error (const char *msg)
131 value *exn = caml_named_value ("Augeas.Error");
134 args[0] = caml_alloc (1, 0);
135 Store_field (args[0], 0, Val_int (-1));
136 args[1] = caml_copy_string (msg);
137 args[2] = caml_copy_string ("augeas initialization failed");
138 args[3] = caml_copy_string ("");
140 caml_raise_with_args (*exn, 4, args);
144 Optstring_val (value strv)
146 if (strv == Val_int (0)) /* None */
148 else /* Some string */
149 return String_val (Field (strv, 0));
152 /* Map OCaml flags to C flags. */
153 static const int flag_map[] = {
154 /* AugSaveBackup */ AUG_SAVE_BACKUP,
155 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
156 /* AugTypeCheck */ AUG_TYPE_CHECK,
157 /* AugNoStdinc */ AUG_NO_STDINC,
158 /* AugSaveNoop */ AUG_SAVE_NOOP,
159 /* AugNoLoad */ AUG_NO_LOAD,
160 /* AugNoModlAutoload */ AUG_NO_MODL_AUTOLOAD,
161 /* AugEnableSpan */ AUG_ENABLE_SPAN,
162 /* AugNoErrClose */ AUG_NO_ERR_CLOSE,
163 /* AugTraceModuleLoading */ AUG_TRACE_MODULE_LOADING,
166 /* Wrap and unwrap augeas_t handles, with a finalizer. */
167 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
170 augeas_t_finalize (value tv)
172 augeas_t t = Augeas_t_val (tv);
173 if (t) aug_close (t);
176 static struct custom_operations custom_operations = {
177 (char *) "augeas_t_custom_operations",
179 custom_compare_default,
181 custom_serialize_default,
182 custom_deserialize_default,
183 custom_compare_ext_default,
186 static value Val_augeas_t (augeas_t t)
190 /* We could choose these so that the GC can make better decisions.
191 * See 18.9.2 of the OCaml manual.
196 rv = caml_alloc_custom (&custom_operations,
197 sizeof (augeas_t), used, max);
198 Augeas_t_val(rv) = t;
203 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
205 /* val create : string -> string option -> flag list -> t */
207 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
210 const char *root = String_val (rootv);
211 const char *loadpath = Optstring_val (loadpathv);
215 /* Convert list of flags to C. */
216 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
217 i = Int_val (Field (flagsv, 0));
218 flags |= flag_map[i];
221 /* Pass AUG_NO_ERR_CLOSE so we raise a detailed Augeas.Error. */
222 t = aug_init (root, loadpath, flags | AUG_NO_ERR_CLOSE);
225 raise_init_error ("Augeas.create");
227 if (aug_error (t) != AUG_NOERROR) {
228 raise_error_and_maybe_close (t, "Augeas.init", true);
231 CAMLreturn (Val_augeas_t (t));
234 /* val close : t -> unit */
236 ocaml_augeas_close (value tv)
239 augeas_t t = Augeas_t_val (tv);
243 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
246 CAMLreturn (Val_unit);
249 /* val defnode : t -> string -> string -> string option -> int * bool */
251 ocaml_augeas_defnode (value tv, value namev, value exprv, value valv)
253 CAMLparam4 (tv, namev, exprv, valv);
254 CAMLlocal2 (optv, v);
255 augeas_t t = Augeas_t_val (tv);
256 const char *name = String_val (namev);
257 const char *expr = String_val (exprv);
258 const char *val = Optstring_val (valv);
261 r = aug_defnode (t, name, expr, val, &created);
263 raise_error (t, "Augeas.defnode");
266 v = caml_alloc (2, 0);
267 Store_field (v, 0, Val_int (r));
268 Store_field (v, 1, Val_bool (created));
273 /* val defvar : t -> string -> string option -> int option */
275 ocaml_augeas_defvar (value tv, value namev, value exprv)
277 CAMLparam3 (tv, namev, exprv);
278 CAMLlocal2 (optv, v);
279 augeas_t t = Augeas_t_val (tv);
280 const char *name = String_val (namev);
281 const char *expr = Optstring_val (exprv);
284 r = aug_defvar (t, name, expr);
285 if (r > 0) { /* Return Some val */
287 optv = caml_alloc (1, 0);
289 } else if (r == 0) /* Return None */
291 else if (r == -1) /* Error or multiple matches */
292 raise_error (t, "Augeas.defvar");
294 caml_failwith ("Augeas.defvar: bad return value");
299 /* val get : t -> path -> value option */
301 ocaml_augeas_get (value tv, value pathv)
303 CAMLparam2 (tv, pathv);
304 CAMLlocal2 (optv, v);
305 augeas_t t = Augeas_t_val (tv);
306 const char *path = String_val (pathv);
310 r = aug_get (t, path, &val);
311 if (r == 1 && val) { /* Return Some val */
312 v = caml_copy_string (val);
313 optv = caml_alloc (1, 0);
315 } else if (r == 0 || !val) /* Return None */
317 else if (r == -1) /* Error or multiple matches */
318 raise_error (t, "Augeas.get");
320 caml_failwith ("Augeas.get: bad return value");
325 /* val exists : t -> path -> bool */
327 ocaml_augeas_exists (value tv, value pathv)
329 CAMLparam2 (tv, pathv);
331 augeas_t t = Augeas_t_val (tv);
332 const char *path = String_val (pathv);
335 r = aug_get (t, path, NULL);
336 if (r == 1) /* Return true. */
338 else if (r == 0) /* Return false */
340 else if (r == -1) /* Error or multiple matches */
341 raise_error (t, "Augeas.exists");
343 failwith ("Augeas.exists: bad return value");
348 /* val insert : t -> ?before:bool -> path -> string -> unit */
350 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
352 CAMLparam4 (tv, beforev, pathv, labelv);
353 augeas_t t = Augeas_t_val (tv);
354 const char *path = String_val (pathv);
355 const char *label = String_val (labelv);
358 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
360 if (aug_insert (t, path, label, before) == -1)
361 raise_error (t, "Augeas.insert");
363 CAMLreturn (Val_unit);
366 /* val mv : t -> path -> path -> unit */
368 ocaml_augeas_mv (value tv, value srcv, value destv)
370 CAMLparam3 (tv, srcv, destv);
371 augeas_t t = Augeas_t_val (tv);
372 const char *src = String_val (srcv);
373 const char *dest = String_val (destv);
375 if (aug_mv (t, src, dest) == -1)
376 raise_error (t, "Augeas.mv");
378 CAMLreturn (Val_unit);
381 /* val rm : t -> path -> int */
383 ocaml_augeas_rm (value tv, value pathv)
385 CAMLparam2 (tv, pathv);
386 augeas_t t = Augeas_t_val (tv);
387 const char *path = String_val (pathv);
390 r = aug_rm (t, path);
392 raise_error (t, "Augeas.rm");
394 CAMLreturn (Val_int (r));
397 /* val matches : t -> path -> path list */
399 ocaml_augeas_match (value tv, value pathv)
401 CAMLparam2 (tv, pathv);
402 CAMLlocal3 (rv, v, cons);
403 augeas_t t = Augeas_t_val (tv);
404 const char *path = String_val (pathv);
408 r = aug_match (t, path, &matches);
410 raise_error (t, "Augeas.matches");
412 /* Copy the paths to a list. */
414 for (i = 0; i < r; ++i) {
415 v = caml_copy_string (matches[i]);
417 cons = caml_alloc (2, 0);
418 Field (cons, 1) = rv;
428 /* val count_matches : t -> path -> int */
430 ocaml_augeas_count_matches (value tv, value pathv)
432 CAMLparam2 (tv, pathv);
433 augeas_t t = Augeas_t_val (tv);
434 const char *path = String_val (pathv);
437 r = aug_match (t, path, NULL);
439 raise_error (t, "Augeas.count_matches");
441 CAMLreturn (Val_int (r));
444 /* val save : t -> unit */
446 ocaml_augeas_save (value tv)
449 augeas_t t = Augeas_t_val (tv);
451 if (aug_save (t) == -1)
452 raise_error (t, "Augeas.save");
454 CAMLreturn (Val_unit);
457 /* val load : t -> unit */
459 ocaml_augeas_load (value tv)
462 augeas_t t = Augeas_t_val (tv);
464 if (aug_load (t) == -1)
465 raise_error (t, "Augeas.load");
467 CAMLreturn (Val_unit);
470 /* val set : t -> -> path -> value option -> unit */
472 ocaml_augeas_set (value tv, value pathv, value valuev)
474 CAMLparam3 (tv, pathv, valuev);
475 augeas_t t = Augeas_t_val (tv);
476 const char *path = String_val (pathv);
477 const char *val = Optstring_val (valuev);
479 if (aug_set (t, path, val) == -1)
480 raise_error (t, "Augeas.set");
482 CAMLreturn (Val_unit);
485 /* val setm : t -> path -> string option -> value option -> int */
487 ocaml_augeas_setm (value tv, value basev, value subv, value valv)
489 CAMLparam4 (tv, basev, subv, valv);
490 augeas_t t = Augeas_t_val (tv);
491 const char *base = String_val (basev);
492 const char *sub = Optstring_val (subv);
493 const char *val = Optstring_val (valv);
496 r = aug_setm (t, base, sub, val);
498 raise_error (t, "Augeas.setm");
500 CAMLreturn (Val_int (r));
503 /* val transform : t -> string -> string -> transform_mode -> unit */
505 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
507 CAMLparam4 (tv, lensv, filev, modev);
508 augeas_t t = Augeas_t_val (tv);
509 const char *lens = String_val (lensv);
510 const char *file = String_val (filev);
511 const int excl = Int_val (modev) == 1 ? 1 : 0;
513 if (aug_transform (t, lens, file, excl) == -1)
514 raise_error (t, "Augeas.transform");
516 CAMLreturn (Val_unit);
519 /* val source : t -> path -> path option */
521 ocaml_augeas_source (value tv, value pathv)
523 #ifdef HAVE_AUG_SOURCE
524 CAMLparam2 (tv, pathv);
525 CAMLlocal2 (optv, v);
526 augeas_t t = Augeas_t_val (tv);
527 const char *path = String_val (pathv);
531 r = aug_source (t, path, &file_path);
533 if (file_path) { /* Return Some file_path */
534 v = caml_copy_string (file_path);
535 optv = caml_alloc (1, 0);
538 } else /* Return None */
542 raise_error (t, "Augeas.source");
546 caml_failwith ("Augeas.source: function not implemented");