3 # OCaml bindings for libvirt.
4 # (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version,
11 # with the OCaml linking exception described in ../COPYING.LIB.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # This generates libvirt_c.c (the core of the bindings). You don't
23 # need to run this program unless you are extending the bindings
24 # themselves (eg. because libvirt has been extended).
26 # Please read libvirt/README.
30 #----------------------------------------------------------------------
32 # The functions in the libvirt API that we can generate.
34 # The 'sig' (signature) doesn't have a meaning or any internal structure.
35 # It is interpreted by the generation functions below to indicate what
36 # "class" the function falls into, and to generate the right class of
39 # Any function added since libvirt 0.2.1 must be marked weak.
42 { name => "virConnectClose", sig => "conn : free" },
43 { name => "virConnectGetHostname", sig => "conn : string", weak => 1 },
44 { name => "virConnectGetURI", sig => "conn : string", weak => 1 },
45 { name => "virConnectGetType", sig => "conn : static string" },
46 { name => "virConnectNumOfDomains", sig => "conn : int" },
47 { name => "virConnectListDomains", sig => "conn, int : int array" },
48 { name => "virConnectNumOfDefinedDomains", sig => "conn : int" },
49 { name => "virConnectListDefinedDomains",
50 sig => "conn, int : string array" },
51 { name => "virConnectNumOfNetworks", sig => "conn : int" },
52 { name => "virConnectListNetworks", sig => "conn, int : string array" },
53 { name => "virConnectNumOfDefinedNetworks", sig => "conn : int" },
54 { name => "virConnectListDefinedNetworks",
55 sig => "conn, int : string array" },
56 { name => "virConnectNumOfStoragePools", sig => "conn : int", weak => 1 },
57 { name => "virConnectListStoragePools",
58 sig => "conn, int : string array", weak => 1 },
59 { name => "virConnectNumOfDefinedStoragePools",
60 sig => "conn : int", weak => 1 },
61 { name => "virConnectListDefinedStoragePools",
62 sig => "conn, int : string array", weak => 1 },
63 { name => "virConnectGetCapabilities", sig => "conn : string" },
65 { name => "virDomainCreateLinux", sig => "conn, string, 0U : dom" },
66 { name => "virDomainFree", sig => "dom : free" },
67 { name => "virDomainDestroy", sig => "dom : free" },
68 { name => "virDomainLookupByName", sig => "conn, string : dom" },
69 { name => "virDomainLookupByID", sig => "conn, int : dom" },
70 { name => "virDomainLookupByUUID", sig => "conn, uuid : dom" },
71 { name => "virDomainLookupByUUIDString", sig => "conn, string : dom" },
72 { name => "virDomainGetName", sig => "dom : static string" },
73 { name => "virDomainGetOSType", sig => "dom : string" },
74 { name => "virDomainGetXMLDesc", sig => "dom, 0 : string" },
75 { name => "virDomainGetUUID", sig => "dom : uuid" },
76 { name => "virDomainGetUUIDString", sig => "dom : uuid string" },
77 { name => "virDomainGetMaxVcpus", sig => "dom : int" },
78 { name => "virDomainSave", sig => "dom, string : unit" },
79 { name => "virDomainRestore", sig => "conn, string : unit" },
80 { name => "virDomainCoreDump", sig => "dom, string, 0 : unit" },
81 { name => "virDomainSuspend", sig => "dom : unit" },
82 { name => "virDomainResume", sig => "dom : unit" },
83 { name => "virDomainShutdown", sig => "dom : unit" },
84 { name => "virDomainReboot", sig => "dom, 0 : unit" },
85 { name => "virDomainDefineXML", sig => "conn, string : dom" },
86 { name => "virDomainUndefine", sig => "dom : unit" },
87 { name => "virDomainCreate", sig => "dom : unit" },
88 { name => "virDomainAttachDevice", sig => "dom, string : unit" },
89 { name => "virDomainDetachDevice", sig => "dom, string : unit" },
90 { name => "virDomainGetAutostart", sig => "dom : bool" },
91 { name => "virDomainSetAutostart", sig => "dom, bool : unit" },
93 { name => "virNetworkFree", sig => "net : free" },
94 { name => "virNetworkDestroy", sig => "net : free" },
95 { name => "virNetworkLookupByName", sig => "conn, string : net" },
96 { name => "virNetworkLookupByUUID", sig => "conn, uuid : net" },
97 { name => "virNetworkLookupByUUIDString", sig => "conn, string : net" },
98 { name => "virNetworkGetName", sig => "net : static string" },
99 { name => "virNetworkGetXMLDesc", sig => "net, 0 : string" },
100 { name => "virNetworkGetBridgeName", sig => "net : string" },
101 { name => "virNetworkGetUUID", sig => "net : uuid" },
102 { name => "virNetworkGetUUIDString", sig => "net : uuid string" },
103 { name => "virNetworkUndefine", sig => "net : unit" },
104 { name => "virNetworkCreateXML", sig => "conn, string : net" },
105 { name => "virNetworkDefineXML", sig => "conn, string : net" },
106 { name => "virNetworkCreate", sig => "net : unit" },
107 { name => "virNetworkGetAutostart", sig => "net : bool" },
108 { name => "virNetworkSetAutostart", sig => "net, bool : unit" },
110 { name => "virStoragePoolFree", sig => "pool : free", weak => 1 },
111 { name => "virStoragePoolDestroy", sig => "pool : free", weak => 1 },
112 { name => "virStoragePoolLookupByName",
113 sig => "conn, string : pool", weak => 1 },
114 { name => "virStoragePoolLookupByUUID",
115 sig => "conn, uuid : pool", weak => 1 },
116 { name => "virStoragePoolLookupByUUIDString",
117 sig => "conn, string : pool", weak => 1 },
118 { name => "virStoragePoolGetName",
119 sig => "pool : static string", weak => 1 },
120 { name => "virStoragePoolGetXMLDesc",
121 sig => "pool, 0U : string", weak => 1 },
122 { name => "virStoragePoolGetUUID",
123 sig => "pool : uuid", weak => 1 },
124 { name => "virStoragePoolGetUUIDString",
125 sig => "pool : uuid string", weak => 1 },
126 { name => "virStoragePoolCreateXML",
127 sig => "conn, string, 0U : pool", weak => 1 },
128 { name => "virStoragePoolDefineXML",
129 sig => "conn, string, 0U : pool", weak => 1 },
130 { name => "virStoragePoolBuild",
131 sig => "pool, uint : unit", weak => 1 },
132 { name => "virStoragePoolUndefine",
133 sig => "pool : unit", weak => 1 },
134 { name => "virStoragePoolCreate",
135 sig => "pool, 0U : unit", weak => 1 },
136 { name => "virStoragePoolDelete",
137 sig => "pool, uint : unit", weak => 1 },
138 { name => "virStoragePoolRefresh",
139 sig => "pool, 0U : unit", weak => 1 },
140 { name => "virStoragePoolGetAutostart",
141 sig => "pool : bool", weak => 1 },
142 { name => "virStoragePoolSetAutostart",
143 sig => "pool, bool : unit", weak => 1 },
144 { name => "virStoragePoolNumOfVolumes",
145 sig => "pool : int", weak => 1 },
146 { name => "virStoragePoolListVolumes",
147 sig => "pool, int : string array", weak => 1 },
149 { name => "virStorageVolFree", sig => "vol : free", weak => 1 },
150 { name => "virStorageVolDelete",
151 sig => "vol, uint : unit", weak => 1 },
152 { name => "virStorageVolLookupByName",
153 sig => "pool, string : vol from pool", weak => 1 },
154 { name => "virStorageVolLookupByKey",
155 sig => "conn, string : vol", weak => 1 },
156 { name => "virStorageVolLookupByPath",
157 sig => "conn, string : vol", weak => 1 },
158 { name => "virStorageVolCreateXML",
159 sig => "pool, string, 0U : vol from pool", weak => 1 },
160 { name => "virStorageVolGetXMLDesc",
161 sig => "vol, 0U : string", weak => 1 },
162 { name => "virStorageVolGetPath",
163 sig => "vol : string", weak => 1 },
164 { name => "virStorageVolGetKey",
165 sig => "vol : static string", weak => 1 },
166 { name => "virStorageVolGetName",
167 sig => "vol : static string", weak => 1 },
168 { name => "virStoragePoolLookupByVolume",
169 sig => "vol : pool from vol", weak => 1 },
173 # Functions we haven't implemented anywhere yet but which are mentioned
176 # We create stubs for these, but eventually they need to either be
177 # moved ^^^ so they are auto-generated, or implementations of them
178 # written in 'libvirt_c_oneoffs.c'.
180 my @unimplemented = (
183 #----------------------------------------------------------------------
185 # Open the output file.
187 my $filename = "libvirt_c.c";
188 open F, ">$filename" or die "$filename: $!";
190 # Write the prologue.
193 /* !!! WARNING WARNING WARNING WARNING WARNING WARNING WARNING !!!
195 * THIS FILE IS AUTOMATICALLY GENERATED BY 'generator.pl'.
197 * Any changes you make to this file may be overwritten.
200 /* OCaml bindings for libvirt.
201 * (C) Copyright 2007-2008 Richard W.M. Jones, Red Hat Inc.
202 * http://libvirt.org/
204 * This library is free software; you can redistribute it and/or
205 * modify it under the terms of the GNU Lesser General Public
206 * License as published by the Free Software Foundation; either
207 * version 2 of the License, or (at your option) any later version,
208 * with the OCaml linking exception described in ../COPYING.LIB.
210 * This library is distributed in the hope that it will be useful,
211 * but WITHOUT ANY WARRANTY; without even the implied warranty of
212 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
213 * Lesser General Public License for more details.
215 * You should have received a copy of the GNU Lesser General Public
216 * License along with this library; if not, write to the Free Software
217 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
226 #include <libvirt/libvirt.h>
227 #include <libvirt/virterror.h>
229 #include <caml/config.h>
230 #include <caml/alloc.h>
231 #include <caml/callback.h>
232 #include <caml/custom.h>
233 #include <caml/fail.h>
234 #include <caml/memory.h>
235 #include <caml/misc.h>
236 #include <caml/mlvalues.h>
237 #include <caml/signals.h>
239 #include "libvirt_c_prologue.c"
241 #include "libvirt_c_oneoffs.c"
245 #----------------------------------------------------------------------
247 sub camel_case_to_underscores
251 $name =~ s/([A-Z][a-z]+|XML|URI|OS|UUID)/$1,/g;
252 my @subs = split (/,/, $name);
253 @subs = map { lc($_) } @subs;
257 # Helper functions dealing with signatures.
259 sub short_name_to_c_type
263 if ($_ eq "conn") { "virConnectPtr" }
264 elsif ($_ eq "dom") { "virDomainPtr" }
265 elsif ($_ eq "net") { "virNetworkPtr" }
266 elsif ($_ eq "pool") { "virStoragePoolPtr" }
267 elsif ($_ eq "vol") { "virStorageVolPtr" }
269 die "unknown short name $_"
273 # Generate a C signature for the original function. Used when building
281 if ($sig =~ /^(\w+) : string$/) {
282 my $c_type = short_name_to_c_type ($1);
283 "char *$c_name ($c_type $1)"
284 } elsif ($sig =~ /^(\w+) : static string$/) {
285 my $c_type = short_name_to_c_type ($1);
286 "const char *$c_name ($c_type $1)"
287 } elsif ($sig =~ /^(\w+) : int$/) {
288 my $c_type = short_name_to_c_type ($1);
289 "int $c_name ($c_type $1)"
290 } elsif ($sig =~ /^(\w+) : uuid$/) {
291 my $c_type = short_name_to_c_type ($1);
292 "int $c_name ($c_type $1, unsigned char *)"
293 } elsif ($sig =~ /^(\w+) : uuid string$/) {
294 my $c_type = short_name_to_c_type ($1);
295 "int $c_name ($c_type $1, char *)"
296 } elsif ($sig =~ /^(\w+) : bool$/) {
297 my $c_type = short_name_to_c_type ($1);
298 "int $c_name ($c_type $1, int *r)"
299 } elsif ($sig =~ /^(\w+), bool : unit$/) {
300 my $c_type = short_name_to_c_type ($1);
301 "int $c_name ($c_type $1, int b)"
302 } elsif ($sig eq "conn, int : int array") {
303 "int $c_name (virConnectPtr conn, int *ids, int maxids)"
304 } elsif ($sig =~ /^(\w+), int : string array$/) {
305 my $c_type = short_name_to_c_type ($1);
306 "int $c_name ($c_type $1, char **const names, int maxnames)"
307 } elsif ($sig =~ /^(\w+), 0(U?) : string$/) {
308 my $c_type = short_name_to_c_type ($1);
309 my $unsigned = $2 eq "U" ? "unsigned " : "";
310 "char *$c_name ($c_type $1, $unsigned int flags)"
311 } elsif ($sig =~ /^(\w+), 0(U?) : unit$/) {
312 my $c_type = short_name_to_c_type ($1);
313 my $unsigned = $2 eq "U" ? "unsigned " : "";
314 "int $c_name ($c_type $1, $unsigned int flags)"
315 } elsif ($sig =~ /^(\w+) : unit$/) {
316 my $c_type = short_name_to_c_type ($1);
317 "int $c_name ($c_type $1)"
318 } elsif ($sig =~ /^(\w+) : free$/) {
319 my $c_type = short_name_to_c_type ($1);
320 "int $c_name ($c_type $1)"
321 } elsif ($sig =~ /^(\w+), string : unit$/) {
322 my $c_type = short_name_to_c_type ($1);
323 "int $c_name ($c_type $1, const char *str)"
324 } elsif ($sig =~ /^(\w+), string, 0(U?) : unit$/) {
325 my $c_type = short_name_to_c_type ($1);
326 my $unsigned = $2 eq "U" ? "unsigned " : "";
327 "int $c_name ($c_type $1, const char *str, ${unsigned}int flags)"
328 } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
329 my $c_type = short_name_to_c_type ($1);
330 my $c_ret_type = short_name_to_c_type ($2);
331 "$c_ret_type $c_name ($c_type $1, const char *str)"
332 } elsif ($sig =~ /^(\w+), string, 0(U?) : (\w+)$/) {
333 my $c_type = short_name_to_c_type ($1);
334 my $unsigned = $2 eq "U" ? "unsigned " : "";
335 my $c_ret_type = short_name_to_c_type ($3);
336 "$c_ret_type $c_name ($c_type $1, const char *str, ${unsigned}int flags)"
337 } elsif ($sig =~ /^(\w+), (u?)int : unit$/) {
338 my $c_type = short_name_to_c_type ($1);
339 my $unsigned = $2 eq "u" ? "unsigned " : "";
340 "int $c_name ($c_type $1, ${unsigned}int i)"
341 } elsif ($sig =~ /^(\w+), (u?)int : (\w+)$/) {
342 my $c_type = short_name_to_c_type ($1);
343 my $unsigned = $2 eq "u" ? "unsigned " : "";
344 my $c_ret_type = short_name_to_c_type ($3);
345 "$c_ret_type $c_name ($c_type $1, ${unsigned}int i)"
346 } elsif ($sig =~ /^(\w+), uuid : (\w+)$/) {
347 my $c_type = short_name_to_c_type ($1);
348 my $c_ret_type = short_name_to_c_type ($2);
349 "$c_ret_type $c_name ($c_type $1, const unsigned char *str)"
350 } elsif ($sig =~ /^(\w+), 0(U?) : (\w+)$/) {
351 my $c_type = short_name_to_c_type ($1);
352 my $unsigned = $2 eq "U" ? "unsigned " : "";
353 my $c_ret_type = short_name_to_c_type ($3);
354 "$c_ret_type $c_name ($c_type $1, $unsigned int flags)"
355 } elsif ($sig =~ /^(\w+) : (\w+)$/) {
356 my $c_type = short_name_to_c_type ($1);
357 my $c_ret_type = short_name_to_c_type ($2);
358 "$c_ret_type $c_name ($c_type $1)"
359 } elsif ($sig =~ /^(\w+), string : (\w+) from \w+$/) {
360 my $c_type = short_name_to_c_type ($1);
361 my $c_ret_type = short_name_to_c_type ($2);
362 "$c_ret_type $c_name ($c_type $1, const char *str)"
363 } elsif ($sig =~ /^(\w+), string, 0(U?) : (\w+) from \w+$/) {
364 my $c_type = short_name_to_c_type ($1);
365 my $unsigned = $2 eq "U" ? "unsigned " : "";
366 my $c_ret_type = short_name_to_c_type ($3);
367 "$c_ret_type $c_name ($c_type $1, const char *str, $unsigned int flags)"
368 } elsif ($sig =~ /^(\w+), 0(U?) : (\w+) from \w+$/) {
369 my $c_type = short_name_to_c_type ($1);
370 my $unsigned = $2 eq "U" ? "unsigned " : "";
371 my $c_ret_type = short_name_to_c_type ($3);
372 "$c_ret_type $c_name ($c_type $1, $unsigned int flags)"
373 } elsif ($sig =~ /^(\w+) : (\w+) from \w+$/) {
374 my $c_type = short_name_to_c_type ($1);
375 my $c_ret_type = short_name_to_c_type ($2);
376 "$c_ret_type $c_name ($c_type $1)"
378 die "unknown signature $sig"
382 # OCaml argument names.
388 if ($sig =~ /^(\w+) : string$/) {
390 } elsif ($sig =~ /^(\w+) : static string$/) {
392 } elsif ($sig =~ /^(\w+) : int$/) {
394 } elsif ($sig =~ /^(\w+) : uuid$/) {
396 } elsif ($sig =~ /^(\w+) : uuid string$/) {
398 } elsif ($sig =~ /^(\w+) : bool$/) {
400 } elsif ($sig =~ /^(\w+), bool : unit$/) {
402 } elsif ($sig eq "conn, int : int array") {
404 } elsif ($sig =~ /^(\w+), int : string array$/) {
406 } elsif ($sig =~ /^(\w+), 0U? : string$/) {
408 } elsif ($sig =~ /^(\w+), 0U? : unit$/) {
410 } elsif ($sig =~ /^(\w+) : unit$/) {
412 } elsif ($sig =~ /^(\w+) : free$/) {
414 } elsif ($sig =~ /^(\w+), string : unit$/) {
416 } elsif ($sig =~ /^(\w+), string, 0U? : unit$/) {
418 } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
420 } elsif ($sig =~ /^(\w+), string, 0U? : (\w+)$/) {
422 } elsif ($sig =~ /^(\w+), u?int : (\w+)$/) {
424 } elsif ($sig =~ /^(\w+), uuid : (\w+)$/) {
426 } elsif ($sig =~ /^(\w+), 0U? : (\w+)$/) {
428 } elsif ($sig =~ /^(\w+) : (\w+)$/) {
430 } elsif ($sig =~ /^(\w+), string : (\w+) from \w+$/) {
432 } elsif ($sig =~ /^(\w+), string, 0U? : (\w+) from \w+$/) {
434 } elsif ($sig =~ /^(\w+), 0U? : (\w+) from \w+$/) {
436 } elsif ($sig =~ /^(\w+) : (\w+) from \w+$/) {
439 die "unknown signature $sig"
443 # Unpack the first (object) argument.
450 "virConnectPtr conn = Connect_val (connv);"
451 } elsif ($_ eq "dom") {
452 "virDomainPtr dom = Domain_val (domv);\n".
453 " virConnectPtr conn = Connect_domv (domv);"
454 } elsif ($_ eq "net") {
455 "virNetworkPtr net = Network_val (netv);\n".
456 " virConnectPtr conn = Connect_netv (netv);"
457 } elsif ($_ eq "pool") {
458 "virStoragePoolPtr pool = Pool_val (poolv);\n".
459 " virConnectPtr conn = Connect_polv (poolv);"
460 } elsif ($_ eq "vol") {
461 "virStorageVolPtr vol = Volume_val (volv);\n".
462 " virConnectPtr conn = Connect_volv (volv);"
464 die "unknown short name $_"
468 # Pack the result if it's an object.
474 if ($_ eq "dom") { "rv = Val_domain (r, connv);" }
475 elsif ($_ eq "net") { "rv = Val_network (r, connv);" }
476 elsif ($_ eq "pool") { "rv = Val_pool (r, connv);" }
477 elsif ($_ eq "vol") { "rv = Val_volume (r, connv);" }
479 die "unknown short name $_"
487 if ($_ eq "conn") { "Connect_val (connv) = NULL;" }
488 elsif ($_ eq "dom") { "Domain_val (domv) = NULL;" }
489 elsif ($_ eq "net") { "Network_val (netv) = NULL;" }
490 elsif ($_ eq "pool") { "Pool_val (poolv) = NULL;" }
491 elsif ($_ eq "vol") { "Volume_val (volv) = NULL;" }
493 die "unknown short name $_"
497 # Generate the C body for each signature (class of function).
504 if ($sig =~ /^(\w+) : string$/) {
507 " . gen_unpack_args ($1) . "
510 NONBLOCKING (r = $c_name ($1));
511 CHECK_ERROR (!r, conn, \"$c_name\");
513 rv = caml_copy_string (r);
517 } elsif ($sig =~ /^(\w+) : static string$/) {
520 " . gen_unpack_args ($1) . "
523 NONBLOCKING (r = $c_name ($1));
524 CHECK_ERROR (!r, conn, \"$c_name\");
526 rv = caml_copy_string (r);
529 } elsif ($sig =~ /^(\w+) : int$/) {
531 " . gen_unpack_args ($1) . "
534 NONBLOCKING (r = $c_name ($1));
535 CHECK_ERROR (r == -1, conn, \"$c_name\");
537 CAMLreturn (Val_int (r));
539 } elsif ($sig =~ /^(\w+) : uuid$/) {
542 " . gen_unpack_args ($1) . "
543 unsigned char uuid[VIR_UUID_BUFLEN];
546 NONBLOCKING (r = $c_name ($1, uuid));
547 CHECK_ERROR (r == -1, conn, \"$c_name\");
549 /* UUIDs are byte arrays with a fixed length. */
550 rv = caml_alloc_string (VIR_UUID_BUFLEN);
551 memcpy (String_val (rv), uuid, VIR_UUID_BUFLEN);
554 } elsif ($sig =~ /^(\w+) : uuid string$/) {
557 " . gen_unpack_args ($1) . "
558 char uuid[VIR_UUID_STRING_BUFLEN];
561 NONBLOCKING (r = $c_name ($1, uuid));
562 CHECK_ERROR (r == -1, conn, \"$c_name\");
564 rv = caml_copy_string (uuid);
567 } elsif ($sig =~ /^(\w+) : bool$/) {
569 " . gen_unpack_args ($1) . "
572 NONBLOCKING (r = $c_name ($1, &b));
573 CHECK_ERROR (r == -1, conn, \"$c_name\");
575 CAMLreturn (b ? Val_true : Val_false);
577 } elsif ($sig =~ /^(\w+), bool : unit$/) {
579 " . gen_unpack_args ($1) . "
582 b = bv == Val_true ? 1 : 0;
584 NONBLOCKING (r = $c_name ($1, b));
585 CHECK_ERROR (r == -1, conn, \"$c_name\");
587 CAMLreturn (Val_unit);
589 } elsif ($sig eq "conn, int : int array") {
592 virConnectPtr conn = Connect_val (connv);
593 int i = Int_val (iv);
596 /* Some libvirt List* functions still throw exceptions if i == 0,
597 * so catch that and return an empty array directly. This changes
598 * the semantics slightly (masking other failures) but it's
599 * unlikely anyone will care. RWMJ 2008/06/10
602 rv = caml_alloc (0, 0);
606 NONBLOCKING (r = $c_name (conn, ids, i));
607 CHECK_ERROR (r == -1, conn, \"$c_name\");
609 rv = caml_alloc (r, 0);
610 for (i = 0; i < r; ++i)
611 Store_field (rv, i, Val_int (ids[i]));
615 } elsif ($sig =~ /^(\w+), int : string array$/) {
617 CAMLlocal2 (rv, strv);
618 " . gen_unpack_args ($1) . "
619 int i = Int_val (iv);
623 /* Some libvirt List* functions still throw exceptions if i == 0,
624 * so catch that and return an empty array directly. This changes
625 * the semantics slightly (masking other failures) but it's
626 * unlikely anyone will care. RWMJ 2008/06/10
629 rv = caml_alloc (0, 0);
633 NONBLOCKING (r = $c_name ($1, names, i));
634 CHECK_ERROR (r == -1, conn, \"$c_name\");
636 rv = caml_alloc (r, 0);
637 for (i = 0; i < r; ++i) {
638 strv = caml_copy_string (names[i]);
639 Store_field (rv, i, strv);
645 } elsif ($sig =~ /^(\w+), 0U? : string$/) {
648 " . gen_unpack_args ($1) . "
651 NONBLOCKING (r = $c_name ($1, 0));
652 CHECK_ERROR (!r, conn, \"$c_name\");
654 rv = caml_copy_string (r);
658 } elsif ($sig =~ /^(\w+), 0U? : unit$/) {
660 " . gen_unpack_args ($1) . "
663 NONBLOCKING (r = $c_name ($1, 0));
664 CHECK_ERROR (r == -1, conn, \"$c_name\");
666 CAMLreturn (Val_unit);
668 } elsif ($sig =~ /^(\w+) : unit$/) {
670 " . gen_unpack_args ($1) . "
673 NONBLOCKING (r = $c_name ($1));
674 CHECK_ERROR (r == -1, conn, \"$c_name\");
676 CAMLreturn (Val_unit);
678 } elsif ($sig =~ /^(\w+) : free$/) {
680 " . gen_unpack_args ($1) . "
683 NONBLOCKING (r = $c_name ($1));
684 CHECK_ERROR (r == -1, conn, \"$c_name\");
686 /* So that we don't double-free in the finalizer: */
687 " . gen_free_arg ($1) . "
689 CAMLreturn (Val_unit);
691 } elsif ($sig =~ /^(\w+), string : unit$/) {
693 " . gen_unpack_args ($1) . "
694 char *str = String_val (strv);
697 NONBLOCKING (r = $c_name ($1, str));
698 CHECK_ERROR (r == -1, conn, \"$c_name\");
700 CAMLreturn (Val_unit);
702 } elsif ($sig =~ /^(\w+), string, 0U? : unit$/) {
705 " . gen_unpack_args ($1) . "
706 char *str = String_val (strv);
709 NONBLOCKING (r = $c_name ($1, str, 0));
710 CHECK_ERROR (!r, conn, \"$c_name\");
712 CAMLreturn (Val_unit);
714 } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
715 my $c_ret_type = short_name_to_c_type ($2);
718 " . gen_unpack_args ($1) . "
719 char *str = String_val (strv);
722 NONBLOCKING (r = $c_name ($1, str));
723 CHECK_ERROR (!r, conn, \"$c_name\");
725 " . gen_pack_result ($2) . "
729 } elsif ($sig =~ /^(\w+), string, 0U? : (\w+)$/) {
730 my $c_ret_type = short_name_to_c_type ($2);
733 " . gen_unpack_args ($1) . "
734 char *str = String_val (strv);
737 NONBLOCKING (r = $c_name ($1, str, 0));
738 CHECK_ERROR (!r, conn, \"$c_name\");
740 " . gen_pack_result ($2) . "
744 } elsif ($sig =~ /^(\w+), (u?)int : unit$/) {
745 my $unsigned = $2 eq "u" ? "unsigned " : "";
747 " . gen_unpack_args ($1) . "
748 ${unsigned}int i = Int_val (iv);
751 NONBLOCKING (r = $c_name ($1, i));
752 CHECK_ERROR (!r, conn, \"$c_name\");
754 CAMLreturn (Val_unit);
756 } elsif ($sig =~ /^(\w+), (u?)int : (\w+)$/) {
757 my $c_ret_type = short_name_to_c_type ($3);
758 my $unsigned = $2 eq "u" ? "unsigned " : "";
761 " . gen_unpack_args ($1) . "
762 ${unsigned}int i = Int_val (iv);
765 NONBLOCKING (r = $c_name ($1, i));
766 CHECK_ERROR (!r, conn, \"$c_name\");
768 " . gen_pack_result ($3) . "
772 } elsif ($sig =~ /^(\w+), uuid : (\w+)$/) {
773 my $c_ret_type = short_name_to_c_type ($2);
776 " . gen_unpack_args ($1) . "
777 unsigned char *uuid = (unsigned char *) String_val (uuidv);
780 NONBLOCKING (r = $c_name ($1, uuid));
781 CHECK_ERROR (!r, conn, \"$c_name\");
783 " . gen_pack_result ($2) . "
787 } elsif ($sig =~ /^(\w+), 0U? : (\w+)$/) {
788 my $c_ret_type = short_name_to_c_type ($2);
791 " . gen_unpack_args ($1) . "
794 NONBLOCKING (r = $c_name ($1, 0));
795 CHECK_ERROR (!r, conn, \"$c_name\");
797 " . gen_pack_result ($2) . "
801 } elsif ($sig =~ /^(\w+) : (\w+)$/) {
802 my $c_ret_type = short_name_to_c_type ($2);
805 " . gen_unpack_args ($1) . "
808 NONBLOCKING (r = $c_name ($1));
809 CHECK_ERROR (!r, conn, \"$c_name\");
811 " . gen_pack_result ($2) . "
815 } elsif ($sig =~ /^(\w+), string : (\w+) from (\w+)$/) {
816 my $c_ret_type = short_name_to_c_type ($2);
818 CAMLlocal2 (rv, connv);
819 " . gen_unpack_args ($1) . "
820 char *str = String_val (strv);
823 NONBLOCKING (r = $c_name ($1, str));
824 CHECK_ERROR (!r, conn, \"$c_name\");
826 connv = Field ($3v, 1);
827 " . gen_pack_result ($2) . "
831 } elsif ($sig =~ /^(\w+), string, 0U? : (\w+) from (\w+)$/) {
832 my $c_ret_type = short_name_to_c_type ($2);
834 CAMLlocal2 (rv, connv);
835 " . gen_unpack_args ($1) . "
836 char *str = String_val (strv);
839 NONBLOCKING (r = $c_name ($1, str, 0));
840 CHECK_ERROR (!r, conn, \"$c_name\");
842 connv = Field ($3v, 1);
843 " . gen_pack_result ($2) . "
847 } elsif ($sig =~ /^(\w+), 0U? : (\w+) from (\w+)$/) {
848 my $c_ret_type = short_name_to_c_type ($2);
850 CAMLlocal2 (rv, connv);
851 " . gen_unpack_args ($1) . "
854 NONBLOCKING (r = $c_name ($1, 0));
855 CHECK_ERROR (!r, conn, \"$c_name\");
857 connv = Field ($3v, 1);
858 " . gen_pack_result ($2) . "
862 } elsif ($sig =~ /^(\w+) : (\w+) from (\w+)$/) {
863 my $c_ret_type = short_name_to_c_type ($2);
865 CAMLlocal2 (rv, connv);
866 " . gen_unpack_args ($1) . "
869 NONBLOCKING (r = $c_name ($1));
870 CHECK_ERROR (!r, conn, \"$c_name\");
872 connv = Field ($3v, 1);
873 " . gen_pack_result ($2) . "
878 die "unknown signature $sig"
882 # Generate each function.
884 foreach my $function (@functions) {
885 my $c_name = $function->{name};
886 my $is_weak = $function->{weak};
887 my $sig = $function->{sig};
889 #print "generating $c_name with sig \"$sig\" ...\n";
891 #my $is_pool_func = $c_name =~ /^virStoragePool/;
892 #my $is_vol_func = $c_name =~ /^virStorageVol/;
894 # Generate an equivalent C-external name for the function, unless
895 # one is defined already.
897 if (exists ($function->{c_external_name})) {
898 $c_external_name = $function->{c_external_name};
899 } elsif ($c_name =~ /^vir/) {
900 $c_external_name = substr $c_name, 3;
901 $c_external_name = camel_case_to_underscores ($c_external_name);
902 $c_external_name = "ocaml_libvirt_" . $c_external_name;
904 die "cannot convert c_name $c_name to c_external_name"
908 /* Automatically generated binding for $c_name.
909 * In generator.pl this function has signature "$sig".
914 # Generate a full function prototype if the function is weak.
915 my $have_name = "HAVE_" . uc ($c_name);
917 my $c_sig = gen_c_signature ($sig, $c_name);
919 #ifdef HAVE_WEAK_SYMBOLS
921 extern $c_sig __attribute__((weak));
928 my @arg_names = gen_arg_names ($sig);
929 my $nr_arg_names = scalar @arg_names;
930 my $arg_names = join ", ", @arg_names;
931 my $arg_names_as_values = join (", ", map { "value $_" } @arg_names);
933 # Generate the start of the function, arguments.
936 $c_external_name ($arg_names_as_values)
938 CAMLparam$nr_arg_names ($arg_names);
941 # If weak, check the function exists at compile time or runtime.
945 /* Symbol $c_name not found at compile time. */
946 not_supported ("$c_name");
949 /* Check that the symbol $c_name
950 * is in runtime version of libvirt.
952 WEAK_SYMBOL_CHECK ($c_name);
956 # Generate the internals of the function.
957 print F (gen_c_code ($sig, $c_name));
959 # Finish off weak #ifdef.
966 # Finish off the function.
973 #----------------------------------------------------------------------
975 # Unimplemented functions.
977 if (@unimplemented) {
978 printf "$0: warning: %d unimplemented functions\n", scalar (@unimplemented);
981 /* The following functions are unimplemented and always fail.
982 * See generator.pl '@unimplemented'
987 foreach my $c_external_name (@unimplemented) {
992 failwith ("$c_external_name is unimplemented");
997 } # end if @unimplemented
999 #----------------------------------------------------------------------
1001 # Write the epilogue.
1004 #include "libvirt_c_epilogue.c"
1010 print "$0: written $filename\n"