build: enable gcc warnings in capitests/ and ocaml/
[libguestfs.git] / ocaml / guestfs_c.c
1 /* libguestfs
2  * Copyright (C) 2009 Red Hat Inc.
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
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <guestfs.h>
25
26 #include <caml/config.h>
27 #include <caml/alloc.h>
28 #include <caml/callback.h>
29 #include <caml/custom.h>
30 #include <caml/fail.h>
31 #include <caml/memory.h>
32 #include <caml/mlvalues.h>
33
34 #include "guestfs_c.h"
35
36 /* This macro was added in OCaml 3.10.  Backport for earlier versions. */
37 #ifndef CAMLreturnT
38 #define CAMLreturnT(type, result) do{ \
39   type caml__temp_result = (result); \
40   caml_local_roots = caml__frame; \
41   return (caml__temp_result); \
42 }while(0)
43 #endif
44
45 /* These prototypes are solely to quiet gcc warning.  */
46 CAMLprim value ocaml_guestfs_create (void);
47 CAMLprim value ocaml_guestfs_close (value gv);
48
49 /* Allocate handles and deal with finalization. */
50 static void
51 guestfs_finalize (value gv)
52 {
53   guestfs_h *g = Guestfs_val (gv);
54   if (g) guestfs_close (g);
55 }
56
57 static struct custom_operations guestfs_custom_operations = {
58   (char *) "guestfs_custom_operations",
59   guestfs_finalize,
60   custom_compare_default,
61   custom_hash_default,
62   custom_serialize_default,
63   custom_deserialize_default
64 };
65
66 static value
67 Val_guestfs (guestfs_h *g)
68 {
69   CAMLparam0 ();
70   CAMLlocal1 (rv);
71
72   rv = caml_alloc_custom (&guestfs_custom_operations,
73                           sizeof (guestfs_h *), 0, 1);
74   Guestfs_val (rv) = g;
75
76   CAMLreturn (rv);
77 }
78
79 void
80 ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
81 {
82   CAMLparam0 ();
83   CAMLlocal1 (v);
84   const char *msg;
85
86   msg = guestfs_last_error (g);
87
88   if (msg)
89     v = caml_copy_string (msg);
90   else
91     v = caml_copy_string (func);
92   caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_error"), v);
93   CAMLnoreturn;
94 }
95
96 /* Guestfs.create */
97 CAMLprim value
98 ocaml_guestfs_create (void)
99 {
100   CAMLparam0 ();
101   CAMLlocal1 (gv);
102   guestfs_h *g;
103
104   g = guestfs_create ();
105   if (g == NULL)
106     caml_failwith ("failed to create guestfs handle");
107
108   guestfs_set_error_handler (g, NULL, NULL);
109
110   gv = Val_guestfs (g);
111   CAMLreturn (gv);
112 }
113
114 /* Guestfs.close */
115 CAMLprim value
116 ocaml_guestfs_close (value gv)
117 {
118   CAMLparam1 (gv);
119
120   guestfs_finalize (gv);
121
122   /* So we don't double-free in the finalizer. */
123   Guestfs_val (gv) = NULL;
124
125   CAMLreturn (Val_unit);
126 }
127
128 /* Copy string array value.
129  * The return value is only 'safe' provided we don't allocate anything
130  * further on the OCaml heap (ie. cannot trigger the OCaml GC) because
131  * that could move the strings around.
132  */
133 char **
134 ocaml_guestfs_strings_val (guestfs_h *g, value sv)
135 {
136   CAMLparam1 (sv);
137   char **r;
138   unsigned int i;
139
140   r = guestfs_safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1));
141   for (i = 0; i < Wosize_val (sv); ++i)
142     r[i] = String_val (Field (sv, i));
143   r[i] = NULL;
144
145   CAMLreturnT (char **, r);
146 }
147
148 /* Free array of strings. */
149 void
150 ocaml_guestfs_free_strings (char **argv)
151 {
152   /* Don't free the actual strings - they are String_vals on
153    * the OCaml heap.
154    */
155   free (argv);
156 }