Avoid VLAs with size depending on user input
[ocaml-libvirt.git] / libvirt / generator.pl
1 #!/usr/bin/perl -w
2 #
3 # OCaml bindings for libvirt.
4 # (C) Copyright 2007-2015 Richard W.M. Jones, Red Hat Inc.
5 # http://libvirt.org/
6 #
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.
12 #
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.
17 #
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
21
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).
25 #
26 # Please read libvirt/README.
27
28 use strict;
29
30 #----------------------------------------------------------------------
31
32 # The functions in the libvirt API that we can generate.
33
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
37 # binding.
38
39 my @functions = (
40     { name => "virConnectClose", sig => "conn : free" },
41     { name => "virConnectGetHostname", sig => "conn : string" },
42     { name => "virConnectGetURI", sig => "conn : string" },
43     { name => "virConnectGetType", sig => "conn : static string" },
44     { name => "virConnectNumOfDomains", sig => "conn : int" },
45     { name => "virConnectListDomains", sig => "conn, int : int array" },
46     { name => "virConnectNumOfDefinedDomains", sig => "conn : int" },
47     { name => "virConnectListDefinedDomains",
48       sig => "conn, int : string array" },
49     { name => "virConnectNumOfNetworks", sig => "conn : int" },
50     { name => "virConnectListNetworks", sig => "conn, int : string array" },
51     { name => "virConnectNumOfDefinedNetworks", sig => "conn : int" },
52     { name => "virConnectListDefinedNetworks",
53       sig => "conn, int : string array" },
54     { name => "virConnectNumOfStoragePools", sig => "conn : int" },
55     { name => "virConnectListStoragePools",
56       sig => "conn, int : string array" },
57     { name => "virConnectNumOfDefinedStoragePools",
58       sig => "conn : int" },
59     { name => "virConnectListDefinedStoragePools",
60       sig => "conn, int : string array" },
61     { name => "virConnectGetCapabilities", sig => "conn : string" },
62     { name => "virConnectDomainEventDeregisterAny",
63       sig => "conn, int : unit" },
64
65     { name => "virDomainCreateLinux", sig => "conn, string, 0U : dom" },
66     { name => "virDomainCreateXML", sig => "conn, string, unsigned : dom" },
67     { name => "virDomainFree", sig => "dom : free" },
68     { name => "virDomainDestroy", sig => "dom : free" },
69     { name => "virDomainLookupByName", sig => "conn, string : dom" },
70     { name => "virDomainLookupByID", sig => "conn, int : dom" },
71     { name => "virDomainLookupByUUID", sig => "conn, uuid : dom" },
72     { name => "virDomainLookupByUUIDString", sig => "conn, string : dom" },
73     { name => "virDomainGetName", sig => "dom : static string" },
74     { name => "virDomainGetOSType", sig => "dom : string" },
75     { name => "virDomainGetXMLDesc", sig => "dom, 0 : string" },
76     { name => "virDomainGetUUID", sig => "dom : uuid" },
77     { name => "virDomainGetUUIDString", sig => "dom : uuid string" },
78     { name => "virDomainGetMaxVcpus", sig => "dom : int" },
79     { name => "virDomainSave", sig => "dom, string : unit" },
80     { name => "virDomainRestore", sig => "conn, string : unit" },
81     { name => "virDomainCoreDump", sig => "dom, string, 0 : unit" },
82     { name => "virDomainSuspend", sig => "dom : unit" },
83     { name => "virDomainResume", sig => "dom : unit" },
84     { name => "virDomainShutdown", sig => "dom : unit" },
85     { name => "virDomainReboot", sig => "dom, 0 : unit" },
86     { name => "virDomainDefineXML", sig => "conn, string : dom" },
87     { name => "virDomainUndefine", sig => "dom : unit" },
88     { name => "virDomainCreate", sig => "dom : unit" },
89     { name => "virDomainAttachDevice", sig => "dom, string : unit" },
90     { name => "virDomainDetachDevice", sig => "dom, string : unit" },
91     { name => "virDomainGetAutostart", sig => "dom : bool" },
92     { name => "virDomainSetAutostart", sig => "dom, bool : unit" },
93
94     { name => "virNetworkFree", sig => "net : free" },
95     { name => "virNetworkDestroy", sig => "net : free" },
96     { name => "virNetworkLookupByName", sig => "conn, string : net" },
97     { name => "virNetworkLookupByUUID", sig => "conn, uuid : net" },
98     { name => "virNetworkLookupByUUIDString", sig => "conn, string : net" },
99     { name => "virNetworkGetName", sig => "net : static string" },
100     { name => "virNetworkGetXMLDesc", sig => "net, 0 : string" },
101     { name => "virNetworkGetBridgeName", sig => "net : string" },
102     { name => "virNetworkGetUUID", sig => "net : uuid" },
103     { name => "virNetworkGetUUIDString", sig => "net : uuid string" },
104     { name => "virNetworkUndefine", sig => "net : unit" },
105     { name => "virNetworkCreateXML", sig => "conn, string : net" },
106     { name => "virNetworkDefineXML", sig => "conn, string : net" },
107     { name => "virNetworkCreate", sig => "net : unit" },
108     { name => "virNetworkGetAutostart", sig => "net : bool" },
109     { name => "virNetworkSetAutostart", sig => "net, bool : unit" },
110
111     { name => "virStoragePoolFree", sig => "pool : free" },
112     { name => "virStoragePoolDestroy", sig => "pool : free" },
113     { name => "virStoragePoolLookupByName",
114       sig => "conn, string : pool" },
115     { name => "virStoragePoolLookupByUUID",
116       sig => "conn, uuid : pool" },
117     { name => "virStoragePoolLookupByUUIDString",
118       sig => "conn, string : pool" },
119     { name => "virStoragePoolGetName",
120       sig => "pool : static string" },
121     { name => "virStoragePoolGetXMLDesc",
122       sig => "pool, 0U : string" },
123     { name => "virStoragePoolGetUUID",
124       sig => "pool : uuid" },
125     { name => "virStoragePoolGetUUIDString",
126       sig => "pool : uuid string" },
127     { name => "virStoragePoolCreateXML",
128       sig => "conn, string, 0U : pool" },
129     { name => "virStoragePoolDefineXML",
130       sig => "conn, string, 0U : pool" },
131     { name => "virStoragePoolBuild",
132       sig => "pool, uint : unit" },
133     { name => "virStoragePoolUndefine",
134       sig => "pool : unit" },
135     { name => "virStoragePoolCreate",
136       sig => "pool, 0U : unit" },
137     { name => "virStoragePoolDelete",
138       sig => "pool, uint : unit" },
139     { name => "virStoragePoolRefresh",
140       sig => "pool, 0U : unit" },
141     { name => "virStoragePoolGetAutostart",
142       sig => "pool : bool" },
143     { name => "virStoragePoolSetAutostart",
144       sig => "pool, bool : unit" },
145     { name => "virStoragePoolNumOfVolumes",
146       sig => "pool : int" },
147     { name => "virStoragePoolListVolumes",
148       sig => "pool, int : string array" },
149
150     { name => "virStorageVolFree", sig => "vol : free" },
151     { name => "virStorageVolDelete",
152       sig => "vol, uint : unit" },
153     { name => "virStorageVolLookupByName",
154       sig => "pool, string : vol from pool" },
155     { name => "virStorageVolLookupByKey",
156       sig => "conn, string : vol" },
157     { name => "virStorageVolLookupByPath",
158       sig => "conn, string : vol" },
159     { name => "virStorageVolCreateXML",
160       sig => "pool, string, 0U : vol from pool" },
161     { name => "virStorageVolGetXMLDesc",
162       sig => "vol, 0U : string" },
163     { name => "virStorageVolGetPath",
164       sig => "vol : string" },
165     { name => "virStorageVolGetKey",
166       sig => "vol : static string" },
167     { name => "virStorageVolGetName",
168       sig => "vol : static string" },
169     { name => "virStoragePoolLookupByVolume",
170       sig => "vol : pool from vol" },
171
172     );
173
174 # Functions we haven't implemented anywhere yet but which are mentioned
175 # in 'libvirt.ml'.
176 #
177 # We create stubs for these, but eventually they need to either be
178 # moved ^^^ so they are auto-generated, or implementations of them
179 # written in 'libvirt_c_oneoffs.c'.
180
181 my @unimplemented = (
182     );
183
184 #----------------------------------------------------------------------
185
186 # Open the output file.
187
188 my $filename = "libvirt_c.c";
189 open F, ">$filename" or die "$filename: $!";
190
191 # Write the prologue.
192
193 print F <<'END';
194 /* !!! WARNING WARNING WARNING WARNING WARNING WARNING WARNING !!!
195  *
196  * THIS FILE IS AUTOMATICALLY GENERATED BY 'generator.pl'.
197  *
198  * Any changes you make to this file may be overwritten.
199  */
200
201 /* OCaml bindings for libvirt.
202  * (C) Copyright 2007-2015 Richard W.M. Jones, Red Hat Inc.
203  * http://libvirt.org/
204  *
205  * This library is free software; you can redistribute it and/or
206  * modify it under the terms of the GNU Lesser General Public
207  * License as published by the Free Software Foundation; either
208  * version 2 of the License, or (at your option) any later version,
209  * with the OCaml linking exception described in ../COPYING.LIB.
210  *
211  * This library is distributed in the hope that it will be useful,
212  * but WITHOUT ANY WARRANTY; without even the implied warranty of
213  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
214  * Lesser General Public License for more details.
215  *
216  * You should have received a copy of the GNU Lesser General Public
217  * License along with this library; if not, write to the Free Software
218  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
219  */
220
221 #include "config.h"
222
223 #include <stdio.h>
224 #include <stdlib.h>
225 #include <string.h>
226
227 #include <libvirt/libvirt.h>
228 #include <libvirt/virterror.h>
229
230 #include <caml/config.h>
231 #include <caml/alloc.h>
232 #include <caml/callback.h>
233 #include <caml/custom.h>
234 #include <caml/fail.h>
235 #include <caml/memory.h>
236 #include <caml/misc.h>
237 #include <caml/mlvalues.h>
238 #include <caml/signals.h>
239
240 #include "libvirt_c_prologue.c"
241
242 #include "libvirt_c_oneoffs.c"
243
244 END
245
246 #----------------------------------------------------------------------
247
248 sub camel_case_to_underscores
249 {
250     my $name = shift;
251
252     $name =~ s/([A-Z][a-z]+|XML|URI|OS|UUID)/$1,/g;
253     my @subs = split (/,/, $name);
254     @subs = map { lc($_) } @subs;
255     join "_", @subs
256 }
257
258 # Helper functions dealing with signatures.
259
260 sub short_name_to_c_type
261 {
262     local $_ = shift;
263
264     if ($_ eq "conn") { "virConnectPtr" }
265     elsif ($_ eq "dom") { "virDomainPtr" }
266     elsif ($_ eq "net") { "virNetworkPtr" }
267     elsif ($_ eq "pool") { "virStoragePoolPtr" }
268     elsif ($_ eq "vol") { "virStorageVolPtr" }
269     else {
270         die "unknown short name $_"
271     }
272 }
273
274 # OCaml argument names.
275
276 sub gen_arg_names
277 {
278     my $sig = shift;
279
280     if ($sig =~ /^(\w+) : string$/) {
281         ( "$1v" )
282     } elsif ($sig =~ /^(\w+) : static string$/) {
283         ( "$1v" )
284     } elsif ($sig =~ /^(\w+) : int$/) {
285         ( "$1v" )
286     } elsif ($sig =~ /^(\w+) : uuid$/) {
287         ( "$1v" )
288     } elsif ($sig =~ /^(\w+) : uuid string$/) {
289         ( "$1v" )
290     } elsif ($sig =~ /^(\w+) : bool$/) {
291         ( "$1v" )
292     } elsif ($sig =~ /^(\w+), bool : unit$/) {
293         ( "$1v", "bv" )
294     } elsif ($sig eq "conn, int : int array") {
295         ( "connv", "iv" )
296     } elsif ($sig =~ /^(\w+), int : string array$/) {
297         ( "$1v", "iv" )
298     } elsif ($sig =~ /^(\w+), 0U? : string$/) {
299         ( "$1v" )
300     } elsif ($sig =~ /^(\w+), 0U? : unit$/) {
301         ( "$1v" )
302     } elsif ($sig =~ /^(\w+) : unit$/) {
303         ( "$1v" )
304     } elsif ($sig =~ /^(\w+) : free$/) {
305         ( "$1v" )
306     } elsif ($sig =~ /^(\w+), string : unit$/) {
307         ( "$1v", "strv" )
308     } elsif ($sig =~ /^(\w+), string, 0U? : unit$/) {
309         ( "$1v", "strv" )
310     } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
311         ( "$1v", "strv" )
312     } elsif ($sig =~ /^(\w+), string, 0U? : (\w+)$/) {
313         ( "$1v", "strv" )
314     } elsif ($sig =~ /^(\w+), string, unsigned : (\w+)$/) {
315         ( "$1v", "strv", "uv" )
316     } elsif ($sig =~ /^(\w+), u?int : (\w+)$/) {
317         ( "$1v", "iv" )
318     } elsif ($sig =~ /^(\w+), uuid : (\w+)$/) {
319         ( "$1v", "uuidv" )
320     } elsif ($sig =~ /^(\w+), 0U? : (\w+)$/) {
321         ( "$1v" )
322     } elsif ($sig =~ /^(\w+) : (\w+)$/) {
323         ( "$1v" )
324     } elsif ($sig =~ /^(\w+), string : (\w+) from \w+$/) {
325         ( "$1v", "strv" )
326     } elsif ($sig =~ /^(\w+), string, 0U? : (\w+) from \w+$/) {
327         ( "$1v", "strv" )
328     } elsif ($sig =~ /^(\w+), 0U? : (\w+) from \w+$/) {
329         ( "$1v" )
330     } elsif ($sig =~ /^(\w+) : (\w+) from \w+$/) {
331         ( "$1v" )
332     } else {
333         die "unknown signature $sig"
334     }
335 }
336
337 # Unpack the first (object) argument.
338
339 sub gen_unpack_args
340 {
341     local $_ = shift;
342
343     if ($_ eq "conn") {
344         "virConnectPtr conn = Connect_val (connv);"
345     } elsif ($_ eq "dom") {
346         "virDomainPtr dom = Domain_val (domv);"
347     } elsif ($_ eq "net") {
348         "virNetworkPtr net = Network_val (netv);"
349     } elsif ($_ eq "pool") {
350         "virStoragePoolPtr pool = Pool_val (poolv);"
351     } elsif ($_ eq "vol") {
352         "virStorageVolPtr vol = Volume_val (volv);"
353     } else {
354         die "unknown short name $_"
355     }
356 }
357
358 # Pack the result if it's an object.
359
360 sub gen_pack_result
361 {
362     local $_ = shift;
363
364     if ($_ eq "dom") {     "rv = Val_domain (r, connv);" }
365     elsif ($_ eq "net") {  "rv = Val_network (r, connv);" }
366     elsif ($_ eq "pool") { "rv = Val_pool (r, connv);" }
367     elsif ($_ eq "vol") {  "rv = Val_volume (r, connv);" }
368     else {
369         die "unknown short name $_"
370     }
371 }
372
373 sub gen_free_arg
374 {
375     local $_ = shift;
376
377     if ($_ eq "conn") {     "Connect_val (connv) = NULL;" }
378     elsif ($_ eq "dom") {   "Domain_val (domv) = NULL;" }
379     elsif ($_ eq "net") {   "Network_val (netv) = NULL;" }
380     elsif ($_ eq "pool") {  "Pool_val (poolv) = NULL;" }
381     elsif ($_ eq "vol") {   "Volume_val (volv) = NULL;" }
382     else {
383         die "unknown short name $_"
384     }
385 }
386
387 # Generate the C body for each signature (class of function).
388
389 sub gen_c_code
390 {
391     my $sig = shift;
392     my $c_name = shift;
393
394     if ($sig =~ /^(\w+) : string$/) {
395         "\
396   CAMLlocal1 (rv);
397   " . gen_unpack_args ($1) . "
398   char *r;
399
400   NONBLOCKING (r = $c_name ($1));
401   CHECK_ERROR (!r, \"$c_name\");
402
403   rv = caml_copy_string (r);
404   free (r);
405   CAMLreturn (rv);
406 "
407     } elsif ($sig =~ /^(\w+) : static string$/) {
408         "\
409   CAMLlocal1 (rv);
410   " . gen_unpack_args ($1) . "
411   const char *r;
412
413   NONBLOCKING (r = $c_name ($1));
414   CHECK_ERROR (!r, \"$c_name\");
415
416   rv = caml_copy_string (r);
417   CAMLreturn (rv);
418 "
419     } elsif ($sig =~ /^(\w+) : int$/) {
420         "\
421   " . gen_unpack_args ($1) . "
422   int r;
423
424   NONBLOCKING (r = $c_name ($1));
425   CHECK_ERROR (r == -1, \"$c_name\");
426
427   CAMLreturn (Val_int (r));
428 "
429     } elsif ($sig =~ /^(\w+) : uuid$/) {
430         "\
431   CAMLlocal1 (rv);
432   " . gen_unpack_args ($1) . "
433   unsigned char uuid[VIR_UUID_BUFLEN];
434   int r;
435
436   NONBLOCKING (r = $c_name ($1, uuid));
437   CHECK_ERROR (r == -1, \"$c_name\");
438
439   /* UUIDs are byte arrays with a fixed length. */
440   rv = caml_alloc_string (VIR_UUID_BUFLEN);
441   memcpy (String_val (rv), uuid, VIR_UUID_BUFLEN);
442   CAMLreturn (rv);
443 "
444     } elsif ($sig =~ /^(\w+) : uuid string$/) {
445         "\
446   CAMLlocal1 (rv);
447   " . gen_unpack_args ($1) . "
448   char uuid[VIR_UUID_STRING_BUFLEN];
449   int r;
450
451   NONBLOCKING (r = $c_name ($1, uuid));
452   CHECK_ERROR (r == -1, \"$c_name\");
453
454   rv = caml_copy_string (uuid);
455   CAMLreturn (rv);
456 "
457     } elsif ($sig =~ /^(\w+) : bool$/) {
458         "\
459   " . gen_unpack_args ($1) . "
460   int r, b;
461
462   NONBLOCKING (r = $c_name ($1, &b));
463   CHECK_ERROR (r == -1, \"$c_name\");
464
465   CAMLreturn (b ? Val_true : Val_false);
466 "
467     } elsif ($sig =~ /^(\w+), bool : unit$/) {
468         "\
469   " . gen_unpack_args ($1) . "
470   int r, b;
471
472   b = bv == Val_true ? 1 : 0;
473
474   NONBLOCKING (r = $c_name ($1, b));
475   CHECK_ERROR (r == -1, \"$c_name\");
476
477   CAMLreturn (Val_unit);
478 "
479     } elsif ($sig eq "conn, int : int array") {
480         "\
481   CAMLlocal1 (rv);
482   virConnectPtr conn = Connect_val (connv);
483   int i = Int_val (iv);
484   int *ids, r;
485
486   /* Some libvirt List* functions still throw exceptions if i == 0,
487    * so catch that and return an empty array directly.  This changes
488    * the semantics slightly (masking other failures) but it's
489    * unlikely anyone will care.  RWMJ 2008/06/10
490    */
491   if (i == 0) {
492     rv = caml_alloc (0, 0);
493     CAMLreturn (rv);
494   }
495
496   ids = malloc (sizeof (*ids) * i);
497   if (ids == NULL)
498     caml_raise_out_of_memory ();
499
500   NONBLOCKING (r = $c_name (conn, ids, i));
501   CHECK_ERROR_CLEANUP (r == -1, free (ids), \"$c_name\");
502
503   rv = caml_alloc (r, 0);
504   for (i = 0; i < r; ++i)
505     Store_field (rv, i, Val_int (ids[i]));
506   free (ids);
507
508   CAMLreturn (rv);
509 "
510     } elsif ($sig =~ /^(\w+), int : string array$/) {
511         "\
512   CAMLlocal2 (rv, strv);
513   " . gen_unpack_args ($1) . "
514   int i = Int_val (iv);
515   char **names;
516   int r;
517
518   /* Some libvirt List* functions still throw exceptions if i == 0,
519    * so catch that and return an empty array directly.  This changes
520    * the semantics slightly (masking other failures) but it's
521    * unlikely anyone will care.  RWMJ 2008/06/10
522    */
523   if (i == 0) {
524     rv = caml_alloc (0, 0);
525     CAMLreturn (rv);
526   }
527
528   names = malloc (sizeof (*names) * i);
529   if (names == NULL)
530     caml_raise_out_of_memory ();
531
532   NONBLOCKING (r = $c_name ($1, names, i));
533   CHECK_ERROR_CLEANUP (r == -1, free (names), \"$c_name\");
534
535   rv = caml_alloc (r, 0);
536   for (i = 0; i < r; ++i) {
537     strv = caml_copy_string (names[i]);
538     Store_field (rv, i, strv);
539     free (names[i]);
540   }
541   free (names);
542
543   CAMLreturn (rv);
544 "
545     } elsif ($sig =~ /^(\w+), 0U? : string$/) {
546         "\
547   CAMLlocal1 (rv);
548   " . gen_unpack_args ($1) . "
549   char *r;
550
551   NONBLOCKING (r = $c_name ($1, 0));
552   CHECK_ERROR (!r, \"$c_name\");
553
554   rv = caml_copy_string (r);
555   free (r);
556   CAMLreturn (rv);
557 "
558     } elsif ($sig =~ /^(\w+), 0U? : unit$/) {
559         "\
560   " . gen_unpack_args ($1) . "
561   int r;
562
563   NONBLOCKING (r = $c_name ($1, 0));
564   CHECK_ERROR (r == -1, \"$c_name\");
565
566   CAMLreturn (Val_unit);
567 "
568     } elsif ($sig =~ /^(\w+) : unit$/) {
569         "\
570   " . gen_unpack_args ($1) . "
571   int r;
572
573   NONBLOCKING (r = $c_name ($1));
574   CHECK_ERROR (r == -1, \"$c_name\");
575
576   CAMLreturn (Val_unit);
577 "
578     } elsif ($sig =~ /^(\w+) : free$/) {
579         "\
580   " . gen_unpack_args ($1) . "
581   int r;
582
583   NONBLOCKING (r = $c_name ($1));
584   CHECK_ERROR (r == -1, \"$c_name\");
585
586   /* So that we don't double-free in the finalizer: */
587   " . gen_free_arg ($1) . "
588
589   CAMLreturn (Val_unit);
590 "
591     } elsif ($sig =~ /^(\w+), string : unit$/) {
592         "\
593   " . gen_unpack_args ($1) . "
594   char *str = String_val (strv);
595   int r;
596
597   NONBLOCKING (r = $c_name ($1, str));
598   CHECK_ERROR (r == -1, \"$c_name\");
599
600   CAMLreturn (Val_unit);
601 "
602     } elsif ($sig =~ /^(\w+), string, 0U? : unit$/) {
603         "\
604   CAMLlocal1 (rv);
605   " . gen_unpack_args ($1) . "
606   char *str = String_val (strv);
607   int r;
608
609   NONBLOCKING (r = $c_name ($1, str, 0));
610   CHECK_ERROR (!r, \"$c_name\");
611
612   CAMLreturn (Val_unit);
613 "
614     } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
615         my $c_ret_type = short_name_to_c_type ($2);
616         "\
617   CAMLlocal1 (rv);
618   " . gen_unpack_args ($1) . "
619   char *str = String_val (strv);
620   $c_ret_type r;
621
622   NONBLOCKING (r = $c_name ($1, str));
623   CHECK_ERROR (!r, \"$c_name\");
624
625   " . gen_pack_result ($2) . "
626
627   CAMLreturn (rv);
628 "
629     } elsif ($sig =~ /^(\w+), string, 0U? : (\w+)$/) {
630         my $c_ret_type = short_name_to_c_type ($2);
631         "\
632   CAMLlocal1 (rv);
633   " . gen_unpack_args ($1) . "
634   char *str = String_val (strv);
635   $c_ret_type r;
636
637   NONBLOCKING (r = $c_name ($1, str, 0));
638   CHECK_ERROR (!r, \"$c_name\");
639
640   " . gen_pack_result ($2) . "
641
642   CAMLreturn (rv);
643 "
644     } elsif ($sig =~ /^(\w+), string, unsigned : (\w+)$/) {
645         my $c_ret_type = short_name_to_c_type ($2);
646         "\
647   CAMLlocal1 (rv);
648   " . gen_unpack_args ($1) . "
649   char *str = String_val (strv);
650   unsigned int u = Int_val (uv);
651   $c_ret_type r;
652
653   NONBLOCKING (r = $c_name ($1, str, u));
654   CHECK_ERROR (!r, \"$c_name\");
655
656   " . gen_pack_result ($2) . "
657
658   CAMLreturn (rv);
659 "
660     } elsif ($sig =~ /^(\w+), (u?)int : unit$/) {
661         my $unsigned = $2 eq "u" ? "unsigned " : "";
662         "\
663   " . gen_unpack_args ($1) . "
664   ${unsigned}int i = Int_val (iv);
665   int r;
666
667   NONBLOCKING (r = $c_name ($1, i));
668   CHECK_ERROR (r == -1, \"$c_name\");
669
670   CAMLreturn (Val_unit);
671 "
672     } elsif ($sig =~ /^(\w+), (u?)int : (\w+)$/) {
673         my $c_ret_type = short_name_to_c_type ($3);
674         my $unsigned = $2 eq "u" ? "unsigned " : "";
675         "\
676   CAMLlocal1 (rv);
677   " . gen_unpack_args ($1) . "
678   ${unsigned}int i = Int_val (iv);
679   $c_ret_type r;
680
681   NONBLOCKING (r = $c_name ($1, i));
682   CHECK_ERROR (!r, \"$c_name\");
683
684   " . gen_pack_result ($3) . "
685
686   CAMLreturn (rv);
687 "
688     } elsif ($sig =~ /^(\w+), uuid : (\w+)$/) {
689         my $c_ret_type = short_name_to_c_type ($2);
690         "\
691   CAMLlocal1 (rv);
692   " . gen_unpack_args ($1) . "
693   unsigned char *uuid = (unsigned char *) String_val (uuidv);
694   $c_ret_type r;
695
696   NONBLOCKING (r = $c_name ($1, uuid));
697   CHECK_ERROR (!r, \"$c_name\");
698
699   " . gen_pack_result ($2) . "
700
701   CAMLreturn (rv);
702 "
703     } elsif ($sig =~ /^(\w+), 0U? : (\w+)$/) {
704         my $c_ret_type = short_name_to_c_type ($2);
705         "\
706   CAMLlocal1 (rv);
707   " . gen_unpack_args ($1) . "
708   $c_ret_type r;
709
710   NONBLOCKING (r = $c_name ($1, 0));
711   CHECK_ERROR (!r, \"$c_name\");
712
713   " . gen_pack_result ($2) . "
714
715   CAMLreturn (rv);
716 "
717     } elsif ($sig =~ /^(\w+) : (\w+)$/) {
718         my $c_ret_type = short_name_to_c_type ($2);
719         "\
720   CAMLlocal1 (rv);
721   " . gen_unpack_args ($1) . "
722   $c_ret_type r;
723
724   NONBLOCKING (r = $c_name ($1));
725   CHECK_ERROR (!r, \"$c_name\");
726
727   " . gen_pack_result ($2) . "
728
729   CAMLreturn (rv);
730 "
731     } elsif ($sig =~ /^(\w+), string : (\w+) from (\w+)$/) {
732         my $c_ret_type = short_name_to_c_type ($2);
733         "\
734   CAMLlocal2 (rv, connv);
735   " . gen_unpack_args ($1) . "
736   char *str = String_val (strv);
737   $c_ret_type r;
738
739   NONBLOCKING (r = $c_name ($1, str));
740   CHECK_ERROR (!r, \"$c_name\");
741
742   connv = Field ($3v, 1);
743   " . gen_pack_result ($2) . "
744
745   CAMLreturn (rv);
746 "
747     } elsif ($sig =~ /^(\w+), string, 0U? : (\w+) from (\w+)$/) {
748         my $c_ret_type = short_name_to_c_type ($2);
749         "\
750   CAMLlocal2 (rv, connv);
751   " . gen_unpack_args ($1) . "
752   char *str = String_val (strv);
753   $c_ret_type r;
754
755   NONBLOCKING (r = $c_name ($1, str, 0));
756   CHECK_ERROR (!r, \"$c_name\");
757
758   connv = Field ($3v, 1);
759   " . gen_pack_result ($2) . "
760
761   CAMLreturn (rv);
762 "
763     } elsif ($sig =~ /^(\w+), 0U? : (\w+) from (\w+)$/) {
764         my $c_ret_type = short_name_to_c_type ($2);
765         "\
766   CAMLlocal2 (rv, connv);
767   " . gen_unpack_args ($1) . "
768   $c_ret_type r;
769
770   NONBLOCKING (r = $c_name ($1, 0));
771   CHECK_ERROR (!r, \"$c_name\");
772
773   connv = Field ($3v, 1);
774   " . gen_pack_result ($2) . "
775
776   CAMLreturn (rv);
777 "
778     } elsif ($sig =~ /^(\w+) : (\w+) from (\w+)$/) {
779         my $c_ret_type = short_name_to_c_type ($2);
780         "\
781   CAMLlocal2 (rv, connv);
782   " . gen_unpack_args ($1) . "
783   $c_ret_type r;
784
785   NONBLOCKING (r = $c_name ($1));
786   CHECK_ERROR (!r, \"$c_name\");
787
788   connv = Field ($3v, 1);
789   " . gen_pack_result ($2) . "
790
791   CAMLreturn (rv);
792 "
793     } else {
794         die "unknown signature $sig"
795     }
796 }
797
798 # Generate each function.
799
800 foreach my $function (@functions) {
801     my $c_name = $function->{name};
802     my $sig = $function->{sig};
803
804     #print "generating $c_name with sig \"$sig\" ...\n";
805
806     #my $is_pool_func = $c_name =~ /^virStoragePool/;
807     #my $is_vol_func = $c_name =~ /^virStorageVol/;
808
809     # Generate an equivalent C-external name for the function, unless
810     # one is defined already.
811     my $c_external_name;
812     if (exists ($function->{c_external_name})) {
813         $c_external_name = $function->{c_external_name};
814     } elsif ($c_name =~ /^vir/) {
815         $c_external_name = substr $c_name, 3;
816         $c_external_name = camel_case_to_underscores ($c_external_name);
817         $c_external_name = "ocaml_libvirt_" . $c_external_name;
818     } else {
819         die "cannot convert c_name $c_name to c_external_name"
820     }
821
822     print F <<END;
823 /* Automatically generated binding for $c_name.
824  * In generator.pl this function has signature "$sig".
825  */
826
827 END
828
829     my @arg_names = gen_arg_names ($sig);
830     my $nr_arg_names = scalar @arg_names;
831     my $arg_names = join ", ", @arg_names;
832     my $arg_names_as_values = join (", ", map { "value $_" } @arg_names);
833
834     # Generate the start of the function, arguments.
835     print F <<END;
836 CAMLprim value
837 $c_external_name ($arg_names_as_values)
838 {
839   CAMLparam$nr_arg_names ($arg_names);
840 END
841
842     # Generate the internals of the function.
843     print F (gen_c_code ($sig, $c_name));
844
845     # Finish off the function.
846     print F <<END;
847 }
848
849 END
850 }
851
852 #----------------------------------------------------------------------
853
854 # Unimplemented functions.
855
856 if (@unimplemented) {
857     printf "$0: warning: %d unimplemented functions\n", scalar (@unimplemented);
858
859     print F <<'END';
860 /* The following functions are unimplemented and always fail.
861  * See generator.pl '@unimplemented'
862  */
863
864 END
865
866     foreach my $c_external_name (@unimplemented) {
867         print F <<END;
868 CAMLprim value
869 $c_external_name ()
870 {
871   failwith ("$c_external_name is unimplemented");
872 }
873
874 END
875     } # end foreach
876 } # end if @unimplemented
877
878 #----------------------------------------------------------------------
879
880 # Write the epilogue.
881
882 print F <<'END';
883 #include "libvirt_c_epilogue.c"
884
885 /* EOF */
886 END
887
888 close F;
889 print "$0: written $filename\n"
890