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_get (value tv, value pathv);
43 extern CAMLprim value ocaml_augeas_exists (value tv, value pathv);
44 extern CAMLprim value ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv);
45 extern CAMLprim value ocaml_augeas_rm (value tv, value pathv);
46 extern CAMLprim value ocaml_augeas_match (value tv, value pathv);
47 extern CAMLprim value ocaml_augeas_count_matches (value tv, value pathv);
48 extern CAMLprim value ocaml_augeas_save (value tv);
49 extern CAMLprim value ocaml_augeas_load (value tv);
50 extern CAMLprim value ocaml_augeas_set (value tv, value pathv, value valuev);
51 extern CAMLprim value ocaml_augeas_transform (value tv, value lensv, value filev, value modev);
52 extern CAMLprim value ocaml_augeas_source (value tv, value pathv)
53 #ifndef HAVE_AUG_SOURCE
58 typedef augeas *augeas_t;
60 /* Map C aug_errcode_t to OCaml error_code. */
61 static const int error_map[] = {
62 /* AugErrInternal */ AUG_EINTERNAL,
63 /* AugErrPathX */ AUG_EPATHX,
64 /* AugErrNoMatch */ AUG_ENOMATCH,
65 /* AugErrMMatch */ AUG_EMMATCH,
66 /* AugErrSyntax */ AUG_ESYNTAX,
67 /* AugErrNoLens */ AUG_ENOLENS,
68 /* AugErrMXfm */ AUG_EMXFM,
69 /* AugErrNoSpan */ AUG_ENOSPAN,
70 /* AugErrMvDesc */ AUG_EMVDESC,
71 /* AugErrCmdRun */ AUG_ECMDRUN,
72 /* AugErrBadArg */ AUG_EBADARG,
73 /* AugErrLabel */ AUG_ELABEL,
74 /* AugErrCpDesc */ AUG_ECPDESC,
76 static const int error_map_len = sizeof error_map / sizeof error_map[0];
78 /* Raise an Augeas.Error exception, and optionally close the
82 raise_error_and_maybe_close (augeas_t t, const char *msg, bool close_handle)
84 value *exn = caml_named_value ("Augeas.Error");
86 const int code = aug_error (t);
87 const char *aug_err_minor;
88 const char *aug_err_details;
92 if (code == AUG_ENOMEM) {
95 caml_raise_out_of_memory ();
98 aug_err_minor = aug_error_minor_message (t);
99 aug_err_details = aug_error_details (t);
101 for (i = 0; i < error_map_len; ++i)
102 if (error_map[i] == code) {
107 if (ocaml_code != -1)
108 args[0] = Val_int (ocaml_code);
110 args[0] = caml_alloc (1, 0);
111 Store_field (args[0], 0, Val_int (code));
113 args[1] = caml_copy_string (msg);
114 args[2] = caml_copy_string (aug_err_minor ? : "");
115 args[3] = caml_copy_string (aug_err_details ? : "");
120 caml_raise_with_args (*exn, 4, args);
122 #define raise_error(t, msg) raise_error_and_maybe_close(t, msg, false)
125 raise_init_error (const char *msg)
127 value *exn = caml_named_value ("Augeas.Error");
130 args[0] = caml_alloc (1, 0);
131 Store_field (args[0], 0, Val_int (-1));
132 args[1] = caml_copy_string (msg);
133 args[2] = caml_copy_string ("augeas initialization failed");
134 args[3] = caml_copy_string ("");
136 caml_raise_with_args (*exn, 4, args);
139 /* Map OCaml flags to C flags. */
140 static const int flag_map[] = {
141 /* AugSaveBackup */ AUG_SAVE_BACKUP,
142 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
143 /* AugTypeCheck */ AUG_TYPE_CHECK,
144 /* AugNoStdinc */ AUG_NO_STDINC,
145 /* AugSaveNoop */ AUG_SAVE_NOOP,
146 /* AugNoLoad */ AUG_NO_LOAD,
147 /* AugNoModlAutoload */ AUG_NO_MODL_AUTOLOAD,
148 /* AugEnableSpan */ AUG_ENABLE_SPAN,
149 /* AugNoErrClose */ AUG_NO_ERR_CLOSE,
150 /* AugTraceModuleLoading */ AUG_TRACE_MODULE_LOADING,
153 /* Wrap and unwrap augeas_t handles, with a finalizer. */
154 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
157 augeas_t_finalize (value tv)
159 augeas_t t = Augeas_t_val (tv);
160 if (t) aug_close (t);
163 static struct custom_operations custom_operations = {
164 (char *) "augeas_t_custom_operations",
166 custom_compare_default,
168 custom_serialize_default,
169 custom_deserialize_default,
170 custom_compare_ext_default,
173 static value Val_augeas_t (augeas_t t)
177 /* We could choose these so that the GC can make better decisions.
178 * See 18.9.2 of the OCaml manual.
183 rv = caml_alloc_custom (&custom_operations,
184 sizeof (augeas_t), used, max);
185 Augeas_t_val(rv) = t;
190 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
192 /* val create : string -> string option -> flag list -> t */
194 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
197 const char *root = String_val (rootv);
198 const char *loadpath;
202 /* Optional loadpath. */
204 loadpathv == Val_int (0)
206 : String_val (Field (loadpathv, 0));
208 /* Convert list of flags to C. */
209 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
210 i = Int_val (Field (flagsv, 0));
211 flags |= flag_map[i];
214 t = aug_init (root, loadpath, flags);
217 raise_init_error ("Augeas.create");
219 CAMLreturn (Val_augeas_t (t));
222 /* val close : t -> unit */
224 ocaml_augeas_close (value tv)
227 augeas_t t = Augeas_t_val (tv);
231 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
234 CAMLreturn (Val_unit);
237 /* val get : t -> path -> value option */
239 ocaml_augeas_get (value tv, value pathv)
241 CAMLparam2 (tv, pathv);
242 CAMLlocal2 (optv, v);
243 augeas_t t = Augeas_t_val (tv);
244 const char *path = String_val (pathv);
248 r = aug_get (t, path, &val);
249 if (r == 1 && val) { /* Return Some val */
250 v = caml_copy_string (val);
251 optv = caml_alloc (1, 0);
253 } else if (r == 0 || !val) /* Return None */
255 else if (r == -1) /* Error or multiple matches */
256 raise_error (t, "Augeas.get");
258 caml_failwith ("Augeas.get: bad return value");
263 /* val exists : t -> path -> bool */
265 ocaml_augeas_exists (value tv, value pathv)
267 CAMLparam2 (tv, pathv);
269 augeas_t t = Augeas_t_val (tv);
270 const char *path = String_val (pathv);
273 r = aug_get (t, path, NULL);
274 if (r == 1) /* Return true. */
276 else if (r == 0) /* Return false */
278 else if (r == -1) /* Error or multiple matches */
279 raise_error (t, "Augeas.exists");
281 failwith ("Augeas.exists: bad return value");
286 /* val insert : t -> ?before:bool -> path -> string -> unit */
288 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
290 CAMLparam4 (tv, beforev, pathv, labelv);
291 augeas_t t = Augeas_t_val (tv);
292 const char *path = String_val (pathv);
293 const char *label = String_val (labelv);
296 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
298 if (aug_insert (t, path, label, before) == -1)
299 raise_error (t, "Augeas.insert");
301 CAMLreturn (Val_unit);
304 /* val rm : t -> path -> int */
306 ocaml_augeas_rm (value tv, value pathv)
308 CAMLparam2 (tv, pathv);
309 augeas_t t = Augeas_t_val (tv);
310 const char *path = String_val (pathv);
313 r = aug_rm (t, path);
315 raise_error (t, "Augeas.rm");
317 CAMLreturn (Val_int (r));
320 /* val matches : t -> path -> path list */
322 ocaml_augeas_match (value tv, value pathv)
324 CAMLparam2 (tv, pathv);
325 CAMLlocal3 (rv, v, cons);
326 augeas_t t = Augeas_t_val (tv);
327 const char *path = String_val (pathv);
331 r = aug_match (t, path, &matches);
333 raise_error (t, "Augeas.matches");
335 /* Copy the paths to a list. */
337 for (i = 0; i < r; ++i) {
338 v = caml_copy_string (matches[i]);
340 cons = caml_alloc (2, 0);
341 Field (cons, 1) = rv;
351 /* val count_matches : t -> path -> int */
353 ocaml_augeas_count_matches (value tv, value pathv)
355 CAMLparam2 (tv, pathv);
356 augeas_t t = Augeas_t_val (tv);
357 const char *path = String_val (pathv);
360 r = aug_match (t, path, NULL);
362 raise_error (t, "Augeas.count_matches");
364 CAMLreturn (Val_int (r));
367 /* val save : t -> unit */
369 ocaml_augeas_save (value tv)
372 augeas_t t = Augeas_t_val (tv);
374 if (aug_save (t) == -1)
375 raise_error (t, "Augeas.save");
377 CAMLreturn (Val_unit);
380 /* val load : t -> unit */
382 ocaml_augeas_load (value tv)
385 augeas_t t = Augeas_t_val (tv);
387 if (aug_load (t) == -1)
388 raise_error (t, "Augeas.load");
390 CAMLreturn (Val_unit);
393 /* val set : t -> -> path -> value option -> unit */
395 ocaml_augeas_set (value tv, value pathv, value valuev)
397 CAMLparam3 (tv, pathv, valuev);
398 augeas_t t = Augeas_t_val (tv);
399 const char *path = String_val (pathv);
403 valuev == Val_int (0)
405 : String_val (Field (valuev, 0));
407 if (aug_set (t, path, val) == -1)
408 raise_error (t, "Augeas.set");
410 CAMLreturn (Val_unit);
413 /* val transform : t -> string -> string -> transform_mode -> unit */
415 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
417 CAMLparam4 (tv, lensv, filev, modev);
418 augeas_t t = Augeas_t_val (tv);
419 const char *lens = String_val (lensv);
420 const char *file = String_val (filev);
421 const int excl = Int_val (modev) == 1 ? 1 : 0;
423 if (aug_transform (t, lens, file, excl) == -1)
424 raise_error (t, "Augeas.transform");
426 CAMLreturn (Val_unit);
429 /* val source : t -> path -> path option */
431 ocaml_augeas_source (value tv, value pathv)
433 #ifdef HAVE_AUG_SOURCE
434 CAMLparam2 (tv, pathv);
435 CAMLlocal2 (optv, v);
436 augeas_t t = Augeas_t_val (tv);
437 const char *path = String_val (pathv);
441 r = aug_source (t, path, &file_path);
443 if (file_path) { /* Return Some file_path */
444 v = caml_copy_string (file_path);
445 optv = caml_alloc (1, 0);
448 } else /* Return None */
452 raise_error (t, "Augeas.source");
456 caml_failwith ("Augeas.source: function not implemented");