build: reenable "syntax-check" rule: sc_const_long_option
[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 /* Allocate handles and deal with finalization. */
46 static void
47 guestfs_finalize (value gv)
48 {
49   guestfs_h *g = Guestfs_val (gv);
50   if (g) guestfs_close (g);
51 }
52
53 static struct custom_operations guestfs_custom_operations = {
54   "guestfs_custom_operations",
55   guestfs_finalize,
56   custom_compare_default,
57   custom_hash_default,
58   custom_serialize_default,
59   custom_deserialize_default
60 };
61
62 static value
63 Val_guestfs (guestfs_h *g)
64 {
65   CAMLparam0 ();
66   CAMLlocal1 (rv);
67
68   rv = caml_alloc_custom (&guestfs_custom_operations,
69                           sizeof (guestfs_h *), 0, 1);
70   Guestfs_val (rv) = g;
71
72   CAMLreturn (rv);
73 }
74
75 void
76 ocaml_guestfs_raise_error (guestfs_h *g, const char *func)
77 {
78   CAMLparam0 ();
79   CAMLlocal1 (v);
80   const char *msg;
81
82   msg = guestfs_last_error (g);
83
84   if (msg)
85     v = caml_copy_string (msg);
86   else
87     v = caml_copy_string (func);
88   caml_raise_with_arg (*caml_named_value ("ocaml_guestfs_error"), v);
89   CAMLnoreturn;
90 }
91
92 /* Guestfs.create */
93 CAMLprim value
94 ocaml_guestfs_create (void)
95 {
96   CAMLparam0 ();
97   CAMLlocal1 (gv);
98   guestfs_h *g;
99
100   g = guestfs_create ();
101   if (g == NULL)
102     caml_failwith ("failed to create guestfs handle");
103
104   guestfs_set_error_handler (g, NULL, NULL);
105
106   gv = Val_guestfs (g);
107   CAMLreturn (gv);
108 }
109
110 /* Guestfs.close */
111 CAMLprim value
112 ocaml_guestfs_close (value gv)
113 {
114   CAMLparam1 (gv);
115
116   guestfs_finalize (gv);
117
118   /* So we don't double-free in the finalizer. */
119   Guestfs_val (gv) = NULL;
120
121   CAMLreturn (Val_unit);
122 }
123
124 /* Copy string array value.
125  * The return value is only 'safe' provided we don't allocate anything
126  * further on the OCaml heap (ie. cannot trigger the OCaml GC) because
127  * that could move the strings around.
128  */
129 char **
130 ocaml_guestfs_strings_val (guestfs_h *g, value sv)
131 {
132   CAMLparam1 (sv);
133   char **r;
134   int i;
135
136   r = guestfs_safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1));
137   for (i = 0; i < Wosize_val (sv); ++i)
138     r[i] = String_val (Field (sv, i));
139   r[i] = NULL;
140
141   CAMLreturnT (char **, r);
142 }
143
144 /* Free array of strings. */
145 void
146 ocaml_guestfs_free_strings (char **argv)
147 {
148   /* Don't free the actual strings - they are String_vals on
149    * the OCaml heap.
150    */
151   free (argv);
152 }