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,
138 /* Wrap and unwrap augeas_t handles, with a finalizer. */
139 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
142 augeas_t_finalize (value tv)
144 augeas_t t = Augeas_t_val (tv);
145 if (t) aug_close (t);
148 static struct custom_operations custom_operations = {
149 (char *) "augeas_t_custom_operations",
151 custom_compare_default,
153 custom_serialize_default,
154 custom_deserialize_default,
155 custom_compare_ext_default,
158 static value Val_augeas_t (augeas_t t)
162 /* We could choose these so that the GC can make better decisions.
163 * See 18.9.2 of the OCaml manual.
168 rv = caml_alloc_custom (&custom_operations,
169 sizeof (augeas_t), used, max);
170 Augeas_t_val(rv) = t;
175 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
177 /* val create : string -> string option -> flag list -> t */
179 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
182 const char *root = String_val (rootv);
183 const char *loadpath;
187 /* Optional loadpath. */
189 loadpathv == Val_int (0)
191 : String_val (Field (loadpathv, 0));
193 /* Convert list of flags to C. */
194 for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
195 i = Int_val (Field (flagsv, 0));
196 flags |= flag_map[i];
199 t = aug_init (root, loadpath, flags);
202 raise_init_error ("Augeas.create");
204 CAMLreturn (Val_augeas_t (t));
207 /* val close : t -> unit */
209 ocaml_augeas_close (value tv)
212 augeas_t t = Augeas_t_val (tv);
216 Augeas_t_val(tv) = NULL; /* So the finalizer doesn't double-free. */
219 CAMLreturn (Val_unit);
222 /* val get : t -> path -> value option */
224 ocaml_augeas_get (value tv, value pathv)
226 CAMLparam2 (tv, pathv);
227 CAMLlocal2 (optv, v);
228 augeas_t t = Augeas_t_val (tv);
229 const char *path = String_val (pathv);
233 r = aug_get (t, path, &val);
234 if (r == 1 && val) { /* Return Some val */
235 v = caml_copy_string (val);
236 optv = caml_alloc (1, 0);
238 } else if (r == 0 || !val) /* Return None */
240 else if (r == -1) /* Error or multiple matches */
241 raise_error (t, "Augeas.get");
243 caml_failwith ("Augeas.get: bad return value");
248 /* val exists : t -> path -> bool */
250 ocaml_augeas_exists (value tv, value pathv)
252 CAMLparam2 (tv, pathv);
254 augeas_t t = Augeas_t_val (tv);
255 const char *path = String_val (pathv);
258 r = aug_get (t, path, NULL);
259 if (r == 1) /* Return true. */
261 else if (r == 0) /* Return false */
263 else if (r == -1) /* Error or multiple matches */
264 raise_error (t, "Augeas.exists");
266 failwith ("Augeas.exists: bad return value");
271 /* val insert : t -> ?before:bool -> path -> string -> unit */
273 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
275 CAMLparam4 (tv, beforev, pathv, labelv);
276 augeas_t t = Augeas_t_val (tv);
277 const char *path = String_val (pathv);
278 const char *label = String_val (labelv);
281 before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
283 if (aug_insert (t, path, label, before) == -1)
284 raise_error (t, "Augeas.insert");
286 CAMLreturn (Val_unit);
289 /* val rm : t -> path -> int */
291 ocaml_augeas_rm (value tv, value pathv)
293 CAMLparam2 (tv, pathv);
294 augeas_t t = Augeas_t_val (tv);
295 const char *path = String_val (pathv);
298 r = aug_rm (t, path);
300 raise_error (t, "Augeas.rm");
302 CAMLreturn (Val_int (r));
305 /* val matches : t -> path -> path list */
307 ocaml_augeas_match (value tv, value pathv)
309 CAMLparam2 (tv, pathv);
310 CAMLlocal3 (rv, v, cons);
311 augeas_t t = Augeas_t_val (tv);
312 const char *path = String_val (pathv);
316 r = aug_match (t, path, &matches);
318 raise_error (t, "Augeas.matches");
320 /* Copy the paths to a list. */
322 for (i = 0; i < r; ++i) {
323 v = caml_copy_string (matches[i]);
325 cons = caml_alloc (2, 0);
326 Field (cons, 1) = rv;
336 /* val count_matches : t -> path -> int */
338 ocaml_augeas_count_matches (value tv, value pathv)
340 CAMLparam2 (tv, pathv);
341 augeas_t t = Augeas_t_val (tv);
342 const char *path = String_val (pathv);
345 r = aug_match (t, path, NULL);
347 raise_error (t, "Augeas.count_matches");
349 CAMLreturn (Val_int (r));
352 /* val save : t -> unit */
354 ocaml_augeas_save (value tv)
357 augeas_t t = Augeas_t_val (tv);
359 if (aug_save (t) == -1)
360 raise_error (t, "Augeas.save");
362 CAMLreturn (Val_unit);
365 /* val load : t -> unit */
367 ocaml_augeas_load (value tv)
370 augeas_t t = Augeas_t_val (tv);
372 if (aug_load (t) == -1)
373 raise_error (t, "Augeas.load");
375 CAMLreturn (Val_unit);
378 /* val set : t -> -> path -> value option -> unit */
380 ocaml_augeas_set (value tv, value pathv, value valuev)
382 CAMLparam3 (tv, pathv, valuev);
383 augeas_t t = Augeas_t_val (tv);
384 const char *path = String_val (pathv);
388 valuev == Val_int (0)
390 : String_val (Field (valuev, 0));
392 if (aug_set (t, path, val) == -1)
393 raise_error (t, "Augeas.set");
395 CAMLreturn (Val_unit);
398 /* val transform : t -> string -> string -> transform_mode -> unit */
400 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
402 CAMLparam4 (tv, lensv, filev, modev);
403 augeas_t t = Augeas_t_val (tv);
404 const char *lens = String_val (lensv);
405 const char *file = String_val (filev);
406 const int excl = Int_val (modev) == 1 ? 1 : 0;
408 if (aug_transform (t, lens, file, excl) == -1)
409 raise_error (t, "Augeas.transform");
411 CAMLreturn (Val_unit);
414 /* val source : t -> path -> path option */
416 ocaml_augeas_source (value tv, value pathv)
418 #ifdef HAVE_AUG_SOURCE
419 CAMLparam2 (tv, pathv);
420 CAMLlocal2 (optv, v);
421 augeas_t t = Augeas_t_val (tv);
422 const char *path = String_val (pathv);
426 r = aug_source (t, path, &file_path);
428 if (file_path) { /* Return Some file_path */
429 v = caml_copy_string (file_path);
430 optv = caml_alloc (1, 0);
433 } else /* Return None */
437 raise_error (t, "Augeas.source");
441 caml_failwith ("Augeas.source: function not implemented");