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>
33 #define NORETURN __attribute__ ((noreturn))
38 extern CAMLprim value ocaml_augeas_create (value rootv, value loadpathv, value flagsv);
39 extern CAMLprim value ocaml_augeas_close (value tv);
40 extern CAMLprim value ocaml_augeas_get (value tv, value pathv);
41 extern CAMLprim value ocaml_augeas_exists (value tv, value pathv);
42 extern CAMLprim value ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv);
43 extern CAMLprim value ocaml_augeas_rm (value tv, value pathv);
44 extern CAMLprim value ocaml_augeas_match (value tv, value pathv);
45 extern CAMLprim value ocaml_augeas_count_matches (value tv, value pathv);
46 extern CAMLprim value ocaml_augeas_save (value tv);
47 extern CAMLprim value ocaml_augeas_load (value tv);
48 extern CAMLprim value ocaml_augeas_set (value tv, value pathv, value valuev);
49 extern CAMLprim value ocaml_augeas_transform (value tv, value lensv, value filev, value modev);
50 extern CAMLprim value ocaml_augeas_source (value tv, value pathv)
51 #ifndef HAVE_AUG_SOURCE
56 typedef augeas *augeas_t;
58 /* Map C aug_errcode_t to OCaml error_code. */
59 static const int error_map[] = {
60 /* AugErrInternal */ AUG_EINTERNAL,
61 /* AugErrPathX */ AUG_EPATHX,
62 /* AugErrNoMatch */ AUG_ENOMATCH,
63 /* AugErrMMatch */ AUG_EMMATCH,
64 /* AugErrSyntax */ AUG_ESYNTAX,
65 /* AugErrNoLens */ AUG_ENOLENS,
66 /* AugErrMXfm */ AUG_EMXFM,
67 /* AugErrNoSpan */ AUG_ENOSPAN,
68 /* AugErrMvDesc */ AUG_EMVDESC,
69 /* AugErrCmdRun */ AUG_ECMDRUN,
70 /* AugErrBadArg */ AUG_EBADARG,
71 /* AugErrLabel */ AUG_ELABEL,
72 /* AugErrCpDesc */ AUG_ECPDESC,
74 static const int error_map_len = sizeof error_map / sizeof error_map[0];
76 /* Raise an Augeas.Error exception. */
78 raise_error (augeas_t t, const char *msg)
80 value *exn = caml_named_value ("Augeas.Error");
82 const int code = aug_error (t);
83 const char *aug_err_minor;
84 const char *aug_err_details;
88 if (code == AUG_ENOMEM)
89 caml_raise_out_of_memory ();
91 aug_err_minor = aug_error_minor_message (t);
92 aug_err_details = aug_error_details (t);
94 for (i = 0; i < error_map_len; ++i)
95 if (error_map[i] == code) {
100 if (ocaml_code != -1)
101 args[0] = Val_int (ocaml_code);
103 args[0] = caml_alloc (1, 0);
104 Store_field (args[0], 0, Val_int (code));
106 args[1] = caml_copy_string (msg);
107 args[2] = caml_copy_string (aug_err_minor ? : "");
108 args[3] = caml_copy_string (aug_err_details ? : "");
110 caml_raise_with_args (*exn, 4, args);
114 raise_init_error (const char *msg)
116 value *exn = caml_named_value ("Augeas.Error");
119 args[0] = caml_alloc (1, 0);
120 Store_field (args[0], 0, Val_int (-1));
121 args[1] = caml_copy_string (msg);
122 args[2] = caml_copy_string ("augeas initialization failed");
123 args[3] = caml_copy_string ("");
125 caml_raise_with_args (*exn, 4, args);
128 /* Map OCaml flags to C flags. */
129 static const int flag_map[] = {
130 /* AugSaveBackup */ AUG_SAVE_BACKUP,
131 /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
132 /* AugTypeCheck */ AUG_TYPE_CHECK,
133 /* AugNoStdinc */ AUG_NO_STDINC,
134 /* AugSaveNoop */ AUG_SAVE_NOOP,
135 /* AugNoLoad */ AUG_NO_LOAD,
136 /* AugNoModlAutoload */ AUG_NO_MODL_AUTOLOAD,
137 /* AugEnableSpan */ AUG_ENABLE_SPAN,
138 /* AugNoErrClose */ AUG_NO_ERR_CLOSE,
139 /* AugTraceModuleLoading */ AUG_TRACE_MODULE_LOADING,
142 /* Wrap and unwrap augeas_t handles, with a finalizer. */
143 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
146 augeas_t_finalize (value tv)
148 augeas_t t = Augeas_t_val (tv);
149 if (t) aug_close (t);
152 static struct custom_operations custom_operations = {
153 (char *) "augeas_t_custom_operations",
155 custom_compare_default,
157 custom_serialize_default,
158 custom_deserialize_default,
159 custom_compare_ext_default,
162 static value Val_augeas_t (augeas_t t)
166 /* We could choose these so that the GC can make better decisions.
167 * See 18.9.2 of the OCaml manual.
172 rv = caml_alloc_custom (&custom_operations,
173 sizeof (augeas_t), used, max);
174 Augeas_t_val(rv) = t;
179 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
181 /* val create : string -> string option -> flag list -> t */
183 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
186 const char *root = String_val (rootv);
187 const char *loadpath;
191 /* Optional loadpath. */
193 loadpathv == Val_int (0)
195 : String_val (Field (loadpathv, 0));
197 /* Convert list of flags to C. */
198 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
199 i = Int_val (Field (flagsv, 0));
200 flags |= flag_map[i];
203 t = aug_init (root, loadpath, flags);
206 raise_init_error ("Augeas.create");
208 CAMLreturn (Val_augeas_t (t));
211 /* val close : t -> unit */
213 ocaml_augeas_close (value tv)
216 augeas_t t = Augeas_t_val (tv);
220 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
223 CAMLreturn (Val_unit);
226 /* val get : t -> path -> value option */
228 ocaml_augeas_get (value tv, value pathv)
230 CAMLparam2 (tv, pathv);
231 CAMLlocal2 (optv, v);
232 augeas_t t = Augeas_t_val (tv);
233 const char *path = String_val (pathv);
237 r = aug_get (t, path, &val);
238 if (r == 1 && val) { /* Return Some val */
239 v = caml_copy_string (val);
240 optv = caml_alloc (1, 0);
242 } else if (r == 0 || !val) /* Return None */
244 else if (r == -1) /* Error or multiple matches */
245 raise_error (t, "Augeas.get");
247 caml_failwith ("Augeas.get: bad return value");
252 /* val exists : t -> path -> bool */
254 ocaml_augeas_exists (value tv, value pathv)
256 CAMLparam2 (tv, pathv);
258 augeas_t t = Augeas_t_val (tv);
259 const char *path = String_val (pathv);
262 r = aug_get (t, path, NULL);
263 if (r == 1) /* Return true. */
265 else if (r == 0) /* Return false */
267 else if (r == -1) /* Error or multiple matches */
268 raise_error (t, "Augeas.exists");
270 failwith ("Augeas.exists: bad return value");
275 /* val insert : t -> ?before:bool -> path -> string -> unit */
277 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
279 CAMLparam4 (tv, beforev, pathv, labelv);
280 augeas_t t = Augeas_t_val (tv);
281 const char *path = String_val (pathv);
282 const char *label = String_val (labelv);
285 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
287 if (aug_insert (t, path, label, before) == -1)
288 raise_error (t, "Augeas.insert");
290 CAMLreturn (Val_unit);
293 /* val rm : t -> path -> int */
295 ocaml_augeas_rm (value tv, value pathv)
297 CAMLparam2 (tv, pathv);
298 augeas_t t = Augeas_t_val (tv);
299 const char *path = String_val (pathv);
302 r = aug_rm (t, path);
304 raise_error (t, "Augeas.rm");
306 CAMLreturn (Val_int (r));
309 /* val matches : t -> path -> path list */
311 ocaml_augeas_match (value tv, value pathv)
313 CAMLparam2 (tv, pathv);
314 CAMLlocal3 (rv, v, cons);
315 augeas_t t = Augeas_t_val (tv);
316 const char *path = String_val (pathv);
320 r = aug_match (t, path, &matches);
322 raise_error (t, "Augeas.matches");
324 /* Copy the paths to a list. */
326 for (i = 0; i < r; ++i) {
327 v = caml_copy_string (matches[i]);
329 cons = caml_alloc (2, 0);
330 Field (cons, 1) = rv;
340 /* val count_matches : t -> path -> int */
342 ocaml_augeas_count_matches (value tv, value pathv)
344 CAMLparam2 (tv, pathv);
345 augeas_t t = Augeas_t_val (tv);
346 const char *path = String_val (pathv);
349 r = aug_match (t, path, NULL);
351 raise_error (t, "Augeas.count_matches");
353 CAMLreturn (Val_int (r));
356 /* val save : t -> unit */
358 ocaml_augeas_save (value tv)
361 augeas_t t = Augeas_t_val (tv);
363 if (aug_save (t) == -1)
364 raise_error (t, "Augeas.save");
366 CAMLreturn (Val_unit);
369 /* val load : t -> unit */
371 ocaml_augeas_load (value tv)
374 augeas_t t = Augeas_t_val (tv);
376 if (aug_load (t) == -1)
377 raise_error (t, "Augeas.load");
379 CAMLreturn (Val_unit);
382 /* val set : t -> -> path -> value option -> unit */
384 ocaml_augeas_set (value tv, value pathv, value valuev)
386 CAMLparam3 (tv, pathv, valuev);
387 augeas_t t = Augeas_t_val (tv);
388 const char *path = String_val (pathv);
392 valuev == Val_int (0)
394 : String_val (Field (valuev, 0));
396 if (aug_set (t, path, val) == -1)
397 raise_error (t, "Augeas.set");
399 CAMLreturn (Val_unit);
402 /* val transform : t -> string -> string -> transform_mode -> unit */
404 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
406 CAMLparam4 (tv, lensv, filev, modev);
407 augeas_t t = Augeas_t_val (tv);
408 const char *lens = String_val (lensv);
409 const char *file = String_val (filev);
410 const int excl = Int_val (modev) == 1 ? 1 : 0;
412 if (aug_transform (t, lens, file, excl) == -1)
413 raise_error (t, "Augeas.transform");
415 CAMLreturn (Val_unit);
418 /* val source : t -> path -> path option */
420 ocaml_augeas_source (value tv, value pathv)
422 #ifdef HAVE_AUG_SOURCE
423 CAMLparam2 (tv, pathv);
424 CAMLlocal2 (optv, v);
425 augeas_t t = Augeas_t_val (tv);
426 const char *path = String_val (pathv);
430 r = aug_source (t, path, &file_path);
432 if (file_path) { /* Return Some file_path */
433 v = caml_copy_string (file_path);
434 optv = caml_alloc (1, 0);
437 } else /* Return None */
441 raise_error (t, "Augeas.source");
445 caml_failwith ("Augeas.source: function not implemented");