Mark ocaml_augeas_source w/o aug_source as noreturn
[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 #ifdef __GNUC__
33   #define NORETURN __attribute__ ((noreturn))
34 #else
35   #define NORETURN
36 #endif
37
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
52   NORETURN
53 #endif
54 ;
55
56 typedef augeas *augeas_t;
57
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,
73 };
74 static const int error_map_len = sizeof error_map / sizeof error_map[0];
75
76 /* Raise an Augeas.Error exception. */
77 static void
78 raise_error (augeas_t t, const char *msg)
79 {
80   value *exn = caml_named_value ("Augeas.Error");
81   value args[4];
82   const int code = aug_error (t);
83   const char *aug_err_minor;
84   const char *aug_err_details;
85   int ocaml_code = -1;
86   int i;
87
88   if (code == AUG_ENOMEM)
89     caml_raise_out_of_memory ();
90
91   aug_err_minor = aug_error_minor_message (t);
92   aug_err_details = aug_error_details (t);
93
94   for (i = 0; i < error_map_len; ++i)
95     if (error_map[i] == code) {
96       ocaml_code = i;
97       break;
98     }
99
100   if (ocaml_code != -1)
101     args[0] = Val_int (ocaml_code);
102   else {
103     args[0] = caml_alloc (1, 0);
104     Store_field (args[0], 0, Val_int (code));
105   }
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 ? : "");
109
110   caml_raise_with_args (*exn, 4, args);
111 }
112
113 static void
114 raise_init_error (const char *msg)
115 {
116   value *exn = caml_named_value ("Augeas.Error");
117   value args[4];
118
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 ("");
124
125   caml_raise_with_args (*exn, 4, args);
126 }
127
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 };
137
138 /* Wrap and unwrap augeas_t handles, with a finalizer. */
139 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
140
141 static void
142 augeas_t_finalize (value tv)
143 {
144   augeas_t t = Augeas_t_val (tv);
145   if (t) aug_close (t);
146 }
147
148 static struct custom_operations custom_operations = {
149   (char *) "augeas_t_custom_operations",
150   augeas_t_finalize,
151   custom_compare_default,
152   custom_hash_default,
153   custom_serialize_default,
154   custom_deserialize_default,
155   custom_compare_ext_default,
156 };
157
158 static value Val_augeas_t (augeas_t t)
159 {
160   CAMLparam0 ();
161   CAMLlocal1 (rv);
162   /* We could choose these so that the GC can make better decisions.
163    * See 18.9.2 of the OCaml manual.
164    */
165   const int used = 0;
166   const int max = 1;
167
168   rv = caml_alloc_custom (&custom_operations,
169                           sizeof (augeas_t), used, max);
170   Augeas_t_val(rv) = t;
171
172   CAMLreturn (rv);
173 }
174
175 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
176
177 /* val create : string -> string option -> flag list -> t */
178 CAMLprim value
179 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
180 {
181   CAMLparam1 (rootv);
182   const char *root = String_val (rootv);
183   const char *loadpath;
184   int flags = 0, i;
185   augeas_t t;
186
187   /* Optional loadpath. */
188   loadpath =
189     loadpathv == Val_int (0)
190     ? NULL
191     : String_val (Field (loadpathv, 0));
192
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];
197   }
198
199   t = aug_init (root, loadpath, flags);
200
201   if (t == NULL)
202     raise_init_error ("Augeas.create");
203
204   CAMLreturn (Val_augeas_t (t));
205 }
206
207 /* val close : t -> unit */
208 CAMLprim value
209 ocaml_augeas_close (value tv)
210 {
211   CAMLparam1 (tv);
212   augeas_t t = Augeas_t_val (tv);
213
214   if (t) {
215     aug_close (t);
216     Augeas_t_val(tv) = NULL;    /* So the finalizer doesn't double-free. */
217   }
218
219   CAMLreturn (Val_unit);
220 }
221
222 /* val get : t -> path -> value option */
223 CAMLprim value
224 ocaml_augeas_get (value tv, value pathv)
225 {
226   CAMLparam2 (tv, pathv);
227   CAMLlocal2 (optv, v);
228   augeas_t t = Augeas_t_val (tv);
229   const char *path = String_val (pathv);
230   const char *val;
231   int r;
232
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);
237     Field (optv, 0) = v;
238   } else if (r == 0 || !val)    /* Return None */
239     optv = Val_int (0);
240   else if (r == -1)             /* Error or multiple matches */
241     raise_error (t, "Augeas.get");
242   else
243     caml_failwith ("Augeas.get: bad return value");
244
245   CAMLreturn (optv);
246 }
247
248 /* val exists : t -> path -> bool */
249 CAMLprim value
250 ocaml_augeas_exists (value tv, value pathv)
251 {
252   CAMLparam2 (tv, pathv);
253   CAMLlocal1 (v);
254   augeas_t t = Augeas_t_val (tv);
255   const char *path = String_val (pathv);
256   int r;
257
258   r = aug_get (t, path, NULL);
259   if (r == 1)                   /* Return true. */
260     v = Val_int (1);
261   else if (r == 0)              /* Return false */
262     v = Val_int (0);
263   else if (r == -1)             /* Error or multiple matches */
264     raise_error (t, "Augeas.exists");
265   else
266     failwith ("Augeas.exists: bad return value");
267
268   CAMLreturn (v);
269 }
270
271 /* val insert : t -> ?before:bool -> path -> string -> unit */
272 CAMLprim value
273 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
274 {
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);
279   int before;
280
281   before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
282
283   if (aug_insert (t, path, label, before) == -1)
284     raise_error (t, "Augeas.insert");
285
286   CAMLreturn (Val_unit);
287 }
288
289 /* val rm : t -> path -> int */
290 CAMLprim value
291 ocaml_augeas_rm (value tv, value pathv)
292 {
293   CAMLparam2 (tv, pathv);
294   augeas_t t = Augeas_t_val (tv);
295   const char *path = String_val (pathv);
296   int r;
297
298   r = aug_rm (t, path);
299   if (r == -1)
300     raise_error (t, "Augeas.rm");
301
302   CAMLreturn (Val_int (r));
303 }
304
305 /* val matches : t -> path -> path list */
306 CAMLprim value
307 ocaml_augeas_match (value tv, value pathv)
308 {
309   CAMLparam2 (tv, pathv);
310   CAMLlocal3 (rv, v, cons);
311   augeas_t t = Augeas_t_val (tv);
312   const char *path = String_val (pathv);
313   char **matches;
314   int r, i;
315
316   r = aug_match (t, path, &matches);
317   if (r == -1)
318     raise_error (t, "Augeas.matches");
319
320   /* Copy the paths to a list. */
321   rv = Val_int (0);
322   for (i = 0; i < r; ++i) {
323     v = caml_copy_string (matches[i]);
324     free (matches[i]);
325     cons = caml_alloc (2, 0);
326     Field (cons, 1) = rv;
327     Field (cons, 0) = v;
328     rv = cons;
329   }
330
331   free (matches);
332
333   CAMLreturn (rv);
334 }
335
336 /* val count_matches : t -> path -> int */
337 CAMLprim value
338 ocaml_augeas_count_matches (value tv, value pathv)
339 {
340   CAMLparam2 (tv, pathv);
341   augeas_t t = Augeas_t_val (tv);
342   const char *path = String_val (pathv);
343   int r;
344
345   r = aug_match (t, path, NULL);
346   if (r == -1)
347     raise_error (t, "Augeas.count_matches");
348
349   CAMLreturn (Val_int (r));
350 }
351
352 /* val save : t -> unit */
353 CAMLprim value
354 ocaml_augeas_save (value tv)
355 {
356   CAMLparam1 (tv);
357   augeas_t t = Augeas_t_val (tv);
358
359   if (aug_save (t) == -1)
360     raise_error (t, "Augeas.save");
361
362   CAMLreturn (Val_unit);
363 }
364
365 /* val load : t -> unit */
366 CAMLprim value
367 ocaml_augeas_load (value tv)
368 {
369   CAMLparam1 (tv);
370   augeas_t t = Augeas_t_val (tv);
371
372   if (aug_load (t) == -1)
373     raise_error (t, "Augeas.load");
374
375   CAMLreturn (Val_unit);
376 }
377
378 /* val set : t -> -> path -> value option -> unit */
379 CAMLprim value
380 ocaml_augeas_set (value tv, value pathv, value valuev)
381 {
382   CAMLparam3 (tv, pathv, valuev);
383   augeas_t t = Augeas_t_val (tv);
384   const char *path = String_val (pathv);
385   const char *val;
386
387   val =
388     valuev == Val_int (0)
389     ? NULL
390     : String_val (Field (valuev, 0));
391
392   if (aug_set (t, path, val) == -1)
393     raise_error (t, "Augeas.set");
394
395   CAMLreturn (Val_unit);
396 }
397
398 /* val transform : t -> string -> string -> transform_mode -> unit */
399 CAMLprim value
400 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
401 {
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;
407
408   if (aug_transform (t, lens, file, excl) == -1)
409     raise_error (t, "Augeas.transform");
410
411   CAMLreturn (Val_unit);
412 }
413
414 /* val source : t -> path -> path option */
415 CAMLprim value
416 ocaml_augeas_source (value tv, value pathv)
417 {
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);
423   char *file_path;
424   int r;
425
426   r = aug_source (t, path, &file_path);
427   if (r == 0) {
428     if (file_path) {    /* Return Some file_path */
429       v = caml_copy_string (file_path);
430       optv = caml_alloc (1, 0);
431       Field (optv, 0) = v;
432       free (file_path);
433     } else              /* Return None */
434       optv = Val_int (0);
435   }
436   else                  /* Error */
437     raise_error (t, "Augeas.source");
438
439   CAMLreturn (optv);
440 #else
441   caml_failwith ("Augeas.source: function not implemented");
442 #endif
443 }