81f87f92fc0bda3e22072e15b8a824ce49ac9880
[ocaml-augeas.git] / augeas-c.c
1 /* Augeas OCaml bindings
2  * Copyright (C) 2008-2017 Red Hat Inc., Richard W.M. Jones
3  *
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.
8  *
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.
13  *
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
17  *
18  * $Id: augeas_c.c,v 1.1 2008/05/06 10:48:20 rjones Exp $
19  */
20
21 #include "config.h"
22
23 #include <augeas.h>
24
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>
31
32 #include <stdbool.h>
33
34 #ifdef __GNUC__
35   #define NORETURN __attribute__ ((noreturn))
36 #else
37   #define NORETURN
38 #endif
39
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
54   NORETURN
55 #endif
56 ;
57
58 typedef augeas *augeas_t;
59
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,
75 };
76 static const int error_map_len = sizeof error_map / sizeof error_map[0];
77
78 /* Raise an Augeas.Error exception, and optionally close the
79  * specified handle.
80  */
81 static void
82 raise_error_and_maybe_close (augeas_t t, const char *msg, bool close_handle)
83 {
84   value *exn = caml_named_value ("Augeas.Error");
85   value args[4];
86   const int code = aug_error (t);
87   const char *aug_err_minor;
88   const char *aug_err_details;
89   int ocaml_code = -1;
90   int i;
91
92   if (code == AUG_ENOMEM) {
93     if (close_handle)
94       aug_close (t);
95     caml_raise_out_of_memory ();
96   }
97
98   aug_err_minor = aug_error_minor_message (t);
99   aug_err_details = aug_error_details (t);
100
101   for (i = 0; i < error_map_len; ++i)
102     if (error_map[i] == code) {
103       ocaml_code = i;
104       break;
105     }
106
107   if (ocaml_code != -1)
108     args[0] = Val_int (ocaml_code);
109   else {
110     args[0] = caml_alloc (1, 0);
111     Store_field (args[0], 0, Val_int (code));
112   }
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 ? : "");
116
117   if (close_handle)
118     aug_close (t);
119
120   caml_raise_with_args (*exn, 4, args);
121 }
122 #define raise_error(t, msg) raise_error_and_maybe_close(t, msg, false)
123
124 static void
125 raise_init_error (const char *msg)
126 {
127   value *exn = caml_named_value ("Augeas.Error");
128   value args[4];
129
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 ("");
135
136   caml_raise_with_args (*exn, 4, args);
137 }
138
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,
151 };
152
153 /* Wrap and unwrap augeas_t handles, with a finalizer. */
154 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
155
156 static void
157 augeas_t_finalize (value tv)
158 {
159   augeas_t t = Augeas_t_val (tv);
160   if (t) aug_close (t);
161 }
162
163 static struct custom_operations custom_operations = {
164   (char *) "augeas_t_custom_operations",
165   augeas_t_finalize,
166   custom_compare_default,
167   custom_hash_default,
168   custom_serialize_default,
169   custom_deserialize_default,
170   custom_compare_ext_default,
171 };
172
173 static value Val_augeas_t (augeas_t t)
174 {
175   CAMLparam0 ();
176   CAMLlocal1 (rv);
177   /* We could choose these so that the GC can make better decisions.
178    * See 18.9.2 of the OCaml manual.
179    */
180   const int used = 0;
181   const int max = 1;
182
183   rv = caml_alloc_custom (&custom_operations,
184                           sizeof (augeas_t), used, max);
185   Augeas_t_val(rv) = t;
186
187   CAMLreturn (rv);
188 }
189
190 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
191
192 /* val create : string -> string option -> flag list -> t */
193 CAMLprim value
194 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
195 {
196   CAMLparam1 (rootv);
197   const char *root = String_val (rootv);
198   const char *loadpath;
199   int flags = 0, i;
200   augeas_t t;
201
202   /* Optional loadpath. */
203   loadpath =
204     loadpathv == Val_int (0)
205     ? NULL
206     : String_val (Field (loadpathv, 0));
207
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];
212   }
213
214   t = aug_init (root, loadpath, flags);
215
216   if (t == NULL)
217     raise_init_error ("Augeas.create");
218
219   CAMLreturn (Val_augeas_t (t));
220 }
221
222 /* val close : t -> unit */
223 CAMLprim value
224 ocaml_augeas_close (value tv)
225 {
226   CAMLparam1 (tv);
227   augeas_t t = Augeas_t_val (tv);
228
229   if (t) {
230     aug_close (t);
231     Augeas_t_val(tv) = NULL;    /* So the finalizer doesn't double-free. */
232   }
233
234   CAMLreturn (Val_unit);
235 }
236
237 /* val get : t -> path -> value option */
238 CAMLprim value
239 ocaml_augeas_get (value tv, value pathv)
240 {
241   CAMLparam2 (tv, pathv);
242   CAMLlocal2 (optv, v);
243   augeas_t t = Augeas_t_val (tv);
244   const char *path = String_val (pathv);
245   const char *val;
246   int r;
247
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);
252     Field (optv, 0) = v;
253   } else if (r == 0 || !val)    /* Return None */
254     optv = Val_int (0);
255   else if (r == -1)             /* Error or multiple matches */
256     raise_error (t, "Augeas.get");
257   else
258     caml_failwith ("Augeas.get: bad return value");
259
260   CAMLreturn (optv);
261 }
262
263 /* val exists : t -> path -> bool */
264 CAMLprim value
265 ocaml_augeas_exists (value tv, value pathv)
266 {
267   CAMLparam2 (tv, pathv);
268   CAMLlocal1 (v);
269   augeas_t t = Augeas_t_val (tv);
270   const char *path = String_val (pathv);
271   int r;
272
273   r = aug_get (t, path, NULL);
274   if (r == 1)                   /* Return true. */
275     v = Val_int (1);
276   else if (r == 0)              /* Return false */
277     v = Val_int (0);
278   else if (r == -1)             /* Error or multiple matches */
279     raise_error (t, "Augeas.exists");
280   else
281     failwith ("Augeas.exists: bad return value");
282
283   CAMLreturn (v);
284 }
285
286 /* val insert : t -> ?before:bool -> path -> string -> unit */
287 CAMLprim value
288 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
289 {
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);
294   int before;
295
296   before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
297
298   if (aug_insert (t, path, label, before) == -1)
299     raise_error (t, "Augeas.insert");
300
301   CAMLreturn (Val_unit);
302 }
303
304 /* val rm : t -> path -> int */
305 CAMLprim value
306 ocaml_augeas_rm (value tv, value pathv)
307 {
308   CAMLparam2 (tv, pathv);
309   augeas_t t = Augeas_t_val (tv);
310   const char *path = String_val (pathv);
311   int r;
312
313   r = aug_rm (t, path);
314   if (r == -1)
315     raise_error (t, "Augeas.rm");
316
317   CAMLreturn (Val_int (r));
318 }
319
320 /* val matches : t -> path -> path list */
321 CAMLprim value
322 ocaml_augeas_match (value tv, value pathv)
323 {
324   CAMLparam2 (tv, pathv);
325   CAMLlocal3 (rv, v, cons);
326   augeas_t t = Augeas_t_val (tv);
327   const char *path = String_val (pathv);
328   char **matches;
329   int r, i;
330
331   r = aug_match (t, path, &matches);
332   if (r == -1)
333     raise_error (t, "Augeas.matches");
334
335   /* Copy the paths to a list. */
336   rv = Val_int (0);
337   for (i = 0; i < r; ++i) {
338     v = caml_copy_string (matches[i]);
339     free (matches[i]);
340     cons = caml_alloc (2, 0);
341     Field (cons, 1) = rv;
342     Field (cons, 0) = v;
343     rv = cons;
344   }
345
346   free (matches);
347
348   CAMLreturn (rv);
349 }
350
351 /* val count_matches : t -> path -> int */
352 CAMLprim value
353 ocaml_augeas_count_matches (value tv, value pathv)
354 {
355   CAMLparam2 (tv, pathv);
356   augeas_t t = Augeas_t_val (tv);
357   const char *path = String_val (pathv);
358   int r;
359
360   r = aug_match (t, path, NULL);
361   if (r == -1)
362     raise_error (t, "Augeas.count_matches");
363
364   CAMLreturn (Val_int (r));
365 }
366
367 /* val save : t -> unit */
368 CAMLprim value
369 ocaml_augeas_save (value tv)
370 {
371   CAMLparam1 (tv);
372   augeas_t t = Augeas_t_val (tv);
373
374   if (aug_save (t) == -1)
375     raise_error (t, "Augeas.save");
376
377   CAMLreturn (Val_unit);
378 }
379
380 /* val load : t -> unit */
381 CAMLprim value
382 ocaml_augeas_load (value tv)
383 {
384   CAMLparam1 (tv);
385   augeas_t t = Augeas_t_val (tv);
386
387   if (aug_load (t) == -1)
388     raise_error (t, "Augeas.load");
389
390   CAMLreturn (Val_unit);
391 }
392
393 /* val set : t -> -> path -> value option -> unit */
394 CAMLprim value
395 ocaml_augeas_set (value tv, value pathv, value valuev)
396 {
397   CAMLparam3 (tv, pathv, valuev);
398   augeas_t t = Augeas_t_val (tv);
399   const char *path = String_val (pathv);
400   const char *val;
401
402   val =
403     valuev == Val_int (0)
404     ? NULL
405     : String_val (Field (valuev, 0));
406
407   if (aug_set (t, path, val) == -1)
408     raise_error (t, "Augeas.set");
409
410   CAMLreturn (Val_unit);
411 }
412
413 /* val transform : t -> string -> string -> transform_mode -> unit */
414 CAMLprim value
415 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
416 {
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;
422
423   if (aug_transform (t, lens, file, excl) == -1)
424     raise_error (t, "Augeas.transform");
425
426   CAMLreturn (Val_unit);
427 }
428
429 /* val source : t -> path -> path option */
430 CAMLprim value
431 ocaml_augeas_source (value tv, value pathv)
432 {
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);
438   char *file_path;
439   int r;
440
441   r = aug_source (t, path, &file_path);
442   if (r == 0) {
443     if (file_path) {    /* Return Some file_path */
444       v = caml_copy_string (file_path);
445       optv = caml_alloc (1, 0);
446       Field (optv, 0) = v;
447       free (file_path);
448     } else              /* Return None */
449       optv = Val_int (0);
450   }
451   else                  /* Error */
452     raise_error (t, "Augeas.source");
453
454   CAMLreturn (optv);
455 #else
456   caml_failwith ("Augeas.source: function not implemented");
457 #endif
458 }