Add Augeas.source
[ocaml-augeas.git] / augeas-c.c
1 /* Augeas OCaml bindings
2  * Copyright (C) 2008-2012 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 typedef augeas *augeas_t;
33
34 /* Map C aug_errcode_t to OCaml error_code. */
35 static const int error_map[] = {
36   /* AugErrInternal */ AUG_EINTERNAL,
37   /* AugErrPathX */    AUG_EPATHX,
38   /* AugErrNoMatch */  AUG_ENOMATCH,
39   /* AugErrMMatch */   AUG_EMMATCH,
40   /* AugErrSyntax */   AUG_ESYNTAX,
41   /* AugErrNoLens */   AUG_ENOLENS,
42   /* AugErrMXfm */     AUG_EMXFM,
43   /* AugErrNoSpan */   AUG_ENOSPAN,
44   /* AugErrMvDesc */   AUG_EMVDESC,
45   /* AugErrCmdRun */   AUG_ECMDRUN,
46   /* AugErrBadArg */   AUG_EBADARG,
47   /* AugErrLabel */    AUG_ELABEL,
48   /* AugErrCpDesc */   AUG_ECPDESC,
49 };
50 static const int error_map_len = sizeof error_map / sizeof error_map[0];
51
52 /* Raise an Augeas.Error exception. */
53 static void
54 raise_error (augeas_t t, const char *msg)
55 {
56   value *exn = caml_named_value ("Augeas.Error");
57   value args[4];
58   const int code = aug_error (t);
59   const char *aug_err_minor;
60   const char *aug_err_details;
61   int ocaml_code = -1;
62   int i;
63
64   if (code == AUG_ENOMEM)
65     caml_raise_out_of_memory ();
66
67   aug_err_minor = aug_error_minor_message (t);
68   aug_err_details = aug_error_details (t);
69
70   for (i = 0; i < error_map_len; ++i)
71     if (error_map[i] == code) {
72       ocaml_code = i;
73       break;
74     }
75
76   if (ocaml_code != -1)
77     args[0] = Val_int (ocaml_code);
78   else {
79     args[0] = caml_alloc (1, 0);
80     Store_field (args[0], 0, Val_int (code));
81   }
82   args[1] = caml_copy_string (msg);
83   args[2] = caml_copy_string (aug_err_minor ? : "");
84   args[3] = caml_copy_string (aug_err_details ? : "");
85
86   caml_raise_with_args (*exn, 4, args);
87 }
88
89 static void
90 raise_init_error (const char *msg)
91 {
92   value *exn = caml_named_value ("Augeas.Error");
93   value args[4];
94
95   args[0] = caml_alloc (1, 0);
96   Store_field (args[0], 0, Val_int (-1));
97   args[1] = caml_copy_string (msg);
98   args[2] = caml_copy_string ("augeas initialization failed");
99   args[3] = caml_copy_string ("");
100
101   caml_raise_with_args (*exn, 4, args);
102 }
103
104 /* Map OCaml flags to C flags. */
105 static const int flag_map[] = {
106   /* AugSaveBackup */  AUG_SAVE_BACKUP,
107   /* AugSaveNewFile */ AUG_SAVE_NEWFILE,
108   /* AugTypeCheck */   AUG_TYPE_CHECK,
109   /* AugNoStdinc */    AUG_NO_STDINC,
110   /* AugSaveNoop */    AUG_SAVE_NOOP,
111   /* AugNoLoad */      AUG_NO_LOAD,
112 };
113
114 /* Wrap and unwrap augeas_t handles, with a finalizer. */
115 #define Augeas_t_val(rv) (*(augeas_t *)Data_custom_val(rv))
116
117 static void
118 augeas_t_finalize (value tv)
119 {
120   augeas_t t = Augeas_t_val (tv);
121   if (t) aug_close (t);
122 }
123
124 static struct custom_operations custom_operations = {
125   (char *) "augeas_t_custom_operations",
126   augeas_t_finalize,
127   custom_compare_default,
128   custom_hash_default,
129   custom_serialize_default,
130   custom_deserialize_default
131 };
132
133 static value Val_augeas_t (augeas_t t)
134 {
135   CAMLparam0 ();
136   CAMLlocal1 (rv);
137   /* We could choose these so that the GC can make better decisions.
138    * See 18.9.2 of the OCaml manual.
139    */
140   const int used = 0;
141   const int max = 1;
142
143   rv = caml_alloc_custom (&custom_operations,
144                           sizeof (augeas_t), used, max);
145   Augeas_t_val(rv) = t;
146
147   CAMLreturn (rv);
148 }
149
150 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
151
152 /* val create : string -> string option -> flag list -> t */
153 CAMLprim value
154 ocaml_augeas_create (value rootv, value loadpathv, value flagsv)
155 {
156   CAMLparam1 (rootv);
157   const char *root = String_val (rootv);
158   const char *loadpath;
159   int flags = 0, i;
160   augeas_t t;
161
162   /* Optional loadpath. */
163   loadpath =
164     loadpathv == Val_int (0)
165     ? NULL
166     : String_val (Field (loadpathv, 0));
167
168   /* Convert list of flags to C. */
169   for (; flagsv != Val_int (0); flagsv = Field (flagsv, 1)) {
170     i = Int_val (Field (flagsv, 0));
171     flags |= flag_map[i];
172   }
173
174   t = aug_init (root, loadpath, flags);
175
176   if (t == NULL)
177     raise_init_error ("Augeas.create");
178
179   CAMLreturn (Val_augeas_t (t));
180 }
181
182 /* val close : t -> unit */
183 CAMLprim value
184 ocaml_augeas_close (value tv)
185 {
186   CAMLparam1 (tv);
187   augeas_t t = Augeas_t_val (tv);
188
189   if (t) {
190     aug_close (t);
191     Augeas_t_val(tv) = NULL;    /* So the finalizer doesn't double-free. */
192   }
193
194   CAMLreturn (Val_unit);
195 }
196
197 /* val get : t -> path -> value option */
198 CAMLprim value
199 ocaml_augeas_get (value tv, value pathv)
200 {
201   CAMLparam2 (tv, pathv);
202   CAMLlocal2 (optv, v);
203   augeas_t t = Augeas_t_val (tv);
204   const char *path = String_val (pathv);
205   const char *val;
206   int r;
207
208   r = aug_get (t, path, &val);
209   if (r == 1 && val) {          /* Return Some val */
210     v = caml_copy_string (val);
211     optv = caml_alloc (1, 0);
212     Field (optv, 0) = v;
213   } else if (r == 0 || !val)    /* Return None */
214     optv = Val_int (0);
215   else if (r == -1)             /* Error or multiple matches */
216     raise_error (t, "Augeas.get");
217   else
218     failwith ("Augeas.get: bad return value");
219
220   CAMLreturn (optv);
221 }
222
223 /* val exists : t -> path -> bool */
224 CAMLprim value
225 ocaml_augeas_exists (value tv, value pathv)
226 {
227   CAMLparam2 (tv, pathv);
228   CAMLlocal1 (v);
229   augeas_t t = Augeas_t_val (tv);
230   const char *path = String_val (pathv);
231   int r;
232
233   r = aug_get (t, path, NULL);
234   if (r == 1)                   /* Return true. */
235     v = Val_int (1);
236   else if (r == 0)              /* Return false */
237     v = Val_int (0);
238   else if (r == -1)             /* Error or multiple matches */
239     raise_error (t, "Augeas.exists");
240   else
241     failwith ("Augeas.exists: bad return value");
242
243   CAMLreturn (v);
244 }
245
246 /* val insert : t -> ?before:bool -> path -> string -> unit */
247 CAMLprim value
248 ocaml_augeas_insert (value tv, value beforev, value pathv, value labelv)
249 {
250   CAMLparam4 (tv, beforev, pathv, labelv);
251   augeas_t t = Augeas_t_val (tv);
252   const char *path = String_val (pathv);
253   const char *label = String_val (labelv);
254   int before;
255
256   before = beforev == Val_int (0) ? 0 : Int_val (Field (beforev, 0));
257
258   if (aug_insert (t, path, label, before) == -1)
259     raise_error (t, "Augeas.insert");
260
261   CAMLreturn (Val_unit);
262 }
263
264 /* val rm : t -> path -> int */
265 CAMLprim value
266 ocaml_augeas_rm (value tv, value pathv)
267 {
268   CAMLparam2 (tv, pathv);
269   augeas_t t = Augeas_t_val (tv);
270   const char *path = String_val (pathv);
271   int r;
272
273   r = aug_rm (t, path);
274   if (r == -1)
275     raise_error (t, "Augeas.rm");
276
277   CAMLreturn (Val_int (r));
278 }
279
280 /* val matches : t -> path -> path list */
281 CAMLprim value
282 ocaml_augeas_match (value tv, value pathv)
283 {
284   CAMLparam2 (tv, pathv);
285   CAMLlocal3 (rv, v, cons);
286   augeas_t t = Augeas_t_val (tv);
287   const char *path = String_val (pathv);
288   char **matches;
289   int r, i;
290
291   r = aug_match (t, path, &matches);
292   if (r == -1)
293     raise_error (t, "Augeas.matches");
294
295   /* Copy the paths to a list. */
296   rv = Val_int (0);
297   for (i = 0; i < r; ++i) {
298     v = caml_copy_string (matches[i]);
299     free (matches[i]);
300     cons = caml_alloc (2, 0);
301     Field (cons, 1) = rv;
302     Field (cons, 0) = v;
303     rv = cons;
304   }
305
306   free (matches);
307
308   CAMLreturn (rv);
309 }
310
311 /* val count_matches : t -> path -> int */
312 CAMLprim value
313 ocaml_augeas_count_matches (value tv, value pathv)
314 {
315   CAMLparam2 (tv, pathv);
316   augeas_t t = Augeas_t_val (tv);
317   const char *path = String_val (pathv);
318   int r;
319
320   r = aug_match (t, path, NULL);
321   if (r == -1)
322     raise_error (t, "Augeas.count_matches");
323
324   CAMLreturn (Val_int (r));
325 }
326
327 /* val save : t -> unit */
328 CAMLprim value
329 ocaml_augeas_save (value tv)
330 {
331   CAMLparam1 (tv);
332   augeas_t t = Augeas_t_val (tv);
333
334   if (aug_save (t) == -1)
335     raise_error (t, "Augeas.save");
336
337   CAMLreturn (Val_unit);
338 }
339
340 /* val load : t -> unit */
341 CAMLprim value
342 ocaml_augeas_load (value tv)
343 {
344   CAMLparam1 (tv);
345   augeas_t t = Augeas_t_val (tv);
346
347   if (aug_load (t) == -1)
348     raise_error (t, "Augeas.load");
349
350   CAMLreturn (Val_unit);
351 }
352
353 /* val set : t -> -> path -> value option -> unit */
354 CAMLprim value
355 ocaml_augeas_set (value tv, value pathv, value valuev)
356 {
357   CAMLparam3 (tv, pathv, valuev);
358   augeas_t t = Augeas_t_val (tv);
359   const char *path = String_val (pathv);
360   const char *val;
361
362   val =
363     valuev == Val_int (0)
364     ? NULL
365     : String_val (Field (valuev, 0));
366
367   if (aug_set (t, path, val) == -1)
368     raise_error (t, "Augeas.set");
369
370   CAMLreturn (Val_unit);
371 }
372
373 /* val transform : t -> string -> string -> transform_mode -> unit */
374 CAMLprim value
375 ocaml_augeas_transform (value tv, value lensv, value filev, value modev)
376 {
377   CAMLparam4 (tv, lensv, filev, modev);
378   augeas_t t = Augeas_t_val (tv);
379   const char *lens = String_val (lensv);
380   const char *file = String_val (filev);
381   const int excl = Int_val (modev) == 1 ? 1 : 0;
382
383   if (aug_transform (t, lens, file, excl) == -1)
384     raise_error (t, "Augeas.transform");
385
386   CAMLreturn (Val_unit);
387 }
388
389 /* val source : t -> path -> path option */
390 CAMLprim value
391 ocaml_augeas_source (value tv, value pathv)
392 {
393   CAMLparam2 (tv, pathv);
394   CAMLlocal2 (optv, v);
395   augeas_t t = Augeas_t_val (tv);
396   const char *path = String_val (pathv);
397   char *file_path;
398   int r;
399
400   r = aug_source (t, path, &file_path);
401   if (r == 0) {
402     if (file_path) {    /* Return Some file_path */
403       v = caml_copy_string (file_path);
404       optv = caml_alloc (1, 0);
405       Field (optv, 0) = v;
406       free (file_path);
407     } else              /* Return None */
408       optv = Val_int (0);
409   }
410   else                  /* Error */
411     raise_error (t, "Augeas.source");
412
413   CAMLreturn (optv);
414 }