34801ba27c982637aa50ceb79c26f8c81e38c28c
[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[i], 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   NONBLOCKING (r = $c_name (conn, ids, i));
497   CHECK_ERROR (r == -1, \"$c_name\");
498
499   rv = caml_alloc (r, 0);
500   for (i = 0; i < r; ++i)
501     Store_field (rv, i, Val_int (ids[i]));
502
503   CAMLreturn (rv);
504 "
505     } elsif ($sig =~ /^(\w+), int : string array$/) {
506         "\
507   CAMLlocal2 (rv, strv);
508   " . gen_unpack_args ($1) . "
509   int i = Int_val (iv);
510   char *names[i];
511   int r;
512
513   /* Some libvirt List* functions still throw exceptions if i == 0,
514    * so catch that and return an empty array directly.  This changes
515    * the semantics slightly (masking other failures) but it's
516    * unlikely anyone will care.  RWMJ 2008/06/10
517    */
518   if (i == 0) {
519     rv = caml_alloc (0, 0);
520     CAMLreturn (rv);
521   }
522
523   NONBLOCKING (r = $c_name ($1, names, i));
524   CHECK_ERROR (r == -1, \"$c_name\");
525
526   rv = caml_alloc (r, 0);
527   for (i = 0; i < r; ++i) {
528     strv = caml_copy_string (names[i]);
529     Store_field (rv, i, strv);
530     free (names[i]);
531   }
532
533   CAMLreturn (rv);
534 "
535     } elsif ($sig =~ /^(\w+), 0U? : string$/) {
536         "\
537   CAMLlocal1 (rv);
538   " . gen_unpack_args ($1) . "
539   char *r;
540
541   NONBLOCKING (r = $c_name ($1, 0));
542   CHECK_ERROR (!r, \"$c_name\");
543
544   rv = caml_copy_string (r);
545   free (r);
546   CAMLreturn (rv);
547 "
548     } elsif ($sig =~ /^(\w+), 0U? : unit$/) {
549         "\
550   " . gen_unpack_args ($1) . "
551   int r;
552
553   NONBLOCKING (r = $c_name ($1, 0));
554   CHECK_ERROR (r == -1, \"$c_name\");
555
556   CAMLreturn (Val_unit);
557 "
558     } elsif ($sig =~ /^(\w+) : unit$/) {
559         "\
560   " . gen_unpack_args ($1) . "
561   int r;
562
563   NONBLOCKING (r = $c_name ($1));
564   CHECK_ERROR (r == -1, \"$c_name\");
565
566   CAMLreturn (Val_unit);
567 "
568     } elsif ($sig =~ /^(\w+) : free$/) {
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   /* So that we don't double-free in the finalizer: */
577   " . gen_free_arg ($1) . "
578
579   CAMLreturn (Val_unit);
580 "
581     } elsif ($sig =~ /^(\w+), string : unit$/) {
582         "\
583   " . gen_unpack_args ($1) . "
584   char *str = String_val (strv);
585   int r;
586
587   NONBLOCKING (r = $c_name ($1, str));
588   CHECK_ERROR (r == -1, \"$c_name\");
589
590   CAMLreturn (Val_unit);
591 "
592     } elsif ($sig =~ /^(\w+), string, 0U? : unit$/) {
593         "\
594   CAMLlocal1 (rv);
595   " . gen_unpack_args ($1) . "
596   char *str = String_val (strv);
597   int r;
598
599   NONBLOCKING (r = $c_name ($1, str, 0));
600   CHECK_ERROR (!r, \"$c_name\");
601
602   CAMLreturn (Val_unit);
603 "
604     } elsif ($sig =~ /^(\w+), string : (\w+)$/) {
605         my $c_ret_type = short_name_to_c_type ($2);
606         "\
607   CAMLlocal1 (rv);
608   " . gen_unpack_args ($1) . "
609   char *str = String_val (strv);
610   $c_ret_type r;
611
612   NONBLOCKING (r = $c_name ($1, str));
613   CHECK_ERROR (!r, \"$c_name\");
614
615   " . gen_pack_result ($2) . "
616
617   CAMLreturn (rv);
618 "
619     } elsif ($sig =~ /^(\w+), string, 0U? : (\w+)$/) {
620         my $c_ret_type = short_name_to_c_type ($2);
621         "\
622   CAMLlocal1 (rv);
623   " . gen_unpack_args ($1) . "
624   char *str = String_val (strv);
625   $c_ret_type r;
626
627   NONBLOCKING (r = $c_name ($1, str, 0));
628   CHECK_ERROR (!r, \"$c_name\");
629
630   " . gen_pack_result ($2) . "
631
632   CAMLreturn (rv);
633 "
634     } elsif ($sig =~ /^(\w+), string, unsigned : (\w+)$/) {
635         my $c_ret_type = short_name_to_c_type ($2);
636         "\
637   CAMLlocal1 (rv);
638   " . gen_unpack_args ($1) . "
639   char *str = String_val (strv);
640   unsigned int u = Int_val (uv);
641   $c_ret_type r;
642
643   NONBLOCKING (r = $c_name ($1, str, u));
644   CHECK_ERROR (!r, \"$c_name\");
645
646   " . gen_pack_result ($2) . "
647
648   CAMLreturn (rv);
649 "
650     } elsif ($sig =~ /^(\w+), (u?)int : unit$/) {
651         my $unsigned = $2 eq "u" ? "unsigned " : "";
652         "\
653   " . gen_unpack_args ($1) . "
654   ${unsigned}int i = Int_val (iv);
655   int r;
656
657   NONBLOCKING (r = $c_name ($1, i));
658   CHECK_ERROR (r == -1, \"$c_name\");
659
660   CAMLreturn (Val_unit);
661 "
662     } elsif ($sig =~ /^(\w+), (u?)int : (\w+)$/) {
663         my $c_ret_type = short_name_to_c_type ($3);
664         my $unsigned = $2 eq "u" ? "unsigned " : "";
665         "\
666   CAMLlocal1 (rv);
667   " . gen_unpack_args ($1) . "
668   ${unsigned}int i = Int_val (iv);
669   $c_ret_type r;
670
671   NONBLOCKING (r = $c_name ($1, i));
672   CHECK_ERROR (!r, \"$c_name\");
673
674   " . gen_pack_result ($3) . "
675
676   CAMLreturn (rv);
677 "
678     } elsif ($sig =~ /^(\w+), uuid : (\w+)$/) {
679         my $c_ret_type = short_name_to_c_type ($2);
680         "\
681   CAMLlocal1 (rv);
682   " . gen_unpack_args ($1) . "
683   unsigned char *uuid = (unsigned char *) String_val (uuidv);
684   $c_ret_type r;
685
686   NONBLOCKING (r = $c_name ($1, uuid));
687   CHECK_ERROR (!r, \"$c_name\");
688
689   " . gen_pack_result ($2) . "
690
691   CAMLreturn (rv);
692 "
693     } elsif ($sig =~ /^(\w+), 0U? : (\w+)$/) {
694         my $c_ret_type = short_name_to_c_type ($2);
695         "\
696   CAMLlocal1 (rv);
697   " . gen_unpack_args ($1) . "
698   $c_ret_type r;
699
700   NONBLOCKING (r = $c_name ($1, 0));
701   CHECK_ERROR (!r, \"$c_name\");
702
703   " . gen_pack_result ($2) . "
704
705   CAMLreturn (rv);
706 "
707     } elsif ($sig =~ /^(\w+) : (\w+)$/) {
708         my $c_ret_type = short_name_to_c_type ($2);
709         "\
710   CAMLlocal1 (rv);
711   " . gen_unpack_args ($1) . "
712   $c_ret_type r;
713
714   NONBLOCKING (r = $c_name ($1));
715   CHECK_ERROR (!r, \"$c_name\");
716
717   " . gen_pack_result ($2) . "
718
719   CAMLreturn (rv);
720 "
721     } elsif ($sig =~ /^(\w+), string : (\w+) from (\w+)$/) {
722         my $c_ret_type = short_name_to_c_type ($2);
723         "\
724   CAMLlocal2 (rv, connv);
725   " . gen_unpack_args ($1) . "
726   char *str = String_val (strv);
727   $c_ret_type r;
728
729   NONBLOCKING (r = $c_name ($1, str));
730   CHECK_ERROR (!r, \"$c_name\");
731
732   connv = Field ($3v, 1);
733   " . gen_pack_result ($2) . "
734
735   CAMLreturn (rv);
736 "
737     } elsif ($sig =~ /^(\w+), string, 0U? : (\w+) from (\w+)$/) {
738         my $c_ret_type = short_name_to_c_type ($2);
739         "\
740   CAMLlocal2 (rv, connv);
741   " . gen_unpack_args ($1) . "
742   char *str = String_val (strv);
743   $c_ret_type r;
744
745   NONBLOCKING (r = $c_name ($1, str, 0));
746   CHECK_ERROR (!r, \"$c_name\");
747
748   connv = Field ($3v, 1);
749   " . gen_pack_result ($2) . "
750
751   CAMLreturn (rv);
752 "
753     } elsif ($sig =~ /^(\w+), 0U? : (\w+) from (\w+)$/) {
754         my $c_ret_type = short_name_to_c_type ($2);
755         "\
756   CAMLlocal2 (rv, connv);
757   " . gen_unpack_args ($1) . "
758   $c_ret_type r;
759
760   NONBLOCKING (r = $c_name ($1, 0));
761   CHECK_ERROR (!r, \"$c_name\");
762
763   connv = Field ($3v, 1);
764   " . gen_pack_result ($2) . "
765
766   CAMLreturn (rv);
767 "
768     } elsif ($sig =~ /^(\w+) : (\w+) from (\w+)$/) {
769         my $c_ret_type = short_name_to_c_type ($2);
770         "\
771   CAMLlocal2 (rv, connv);
772   " . gen_unpack_args ($1) . "
773   $c_ret_type r;
774
775   NONBLOCKING (r = $c_name ($1));
776   CHECK_ERROR (!r, \"$c_name\");
777
778   connv = Field ($3v, 1);
779   " . gen_pack_result ($2) . "
780
781   CAMLreturn (rv);
782 "
783     } else {
784         die "unknown signature $sig"
785     }
786 }
787
788 # Generate each function.
789
790 foreach my $function (@functions) {
791     my $c_name = $function->{name};
792     my $sig = $function->{sig};
793
794     #print "generating $c_name with sig \"$sig\" ...\n";
795
796     #my $is_pool_func = $c_name =~ /^virStoragePool/;
797     #my $is_vol_func = $c_name =~ /^virStorageVol/;
798
799     # Generate an equivalent C-external name for the function, unless
800     # one is defined already.
801     my $c_external_name;
802     if (exists ($function->{c_external_name})) {
803         $c_external_name = $function->{c_external_name};
804     } elsif ($c_name =~ /^vir/) {
805         $c_external_name = substr $c_name, 3;
806         $c_external_name = camel_case_to_underscores ($c_external_name);
807         $c_external_name = "ocaml_libvirt_" . $c_external_name;
808     } else {
809         die "cannot convert c_name $c_name to c_external_name"
810     }
811
812     print F <<END;
813 /* Automatically generated binding for $c_name.
814  * In generator.pl this function has signature "$sig".
815  */
816
817 END
818
819     my @arg_names = gen_arg_names ($sig);
820     my $nr_arg_names = scalar @arg_names;
821     my $arg_names = join ", ", @arg_names;
822     my $arg_names_as_values = join (", ", map { "value $_" } @arg_names);
823
824     # Generate the start of the function, arguments.
825     print F <<END;
826 CAMLprim value
827 $c_external_name ($arg_names_as_values)
828 {
829   CAMLparam$nr_arg_names ($arg_names);
830 END
831
832     # Generate the internals of the function.
833     print F (gen_c_code ($sig, $c_name));
834
835     # Finish off the function.
836     print F <<END;
837 }
838
839 END
840 }
841
842 #----------------------------------------------------------------------
843
844 # Unimplemented functions.
845
846 if (@unimplemented) {
847     printf "$0: warning: %d unimplemented functions\n", scalar (@unimplemented);
848
849     print F <<'END';
850 /* The following functions are unimplemented and always fail.
851  * See generator.pl '@unimplemented'
852  */
853
854 END
855
856     foreach my $c_external_name (@unimplemented) {
857         print F <<END;
858 CAMLprim value
859 $c_external_name ()
860 {
861   failwith ("$c_external_name is unimplemented");
862 }
863
864 END
865     } # end foreach
866 } # end if @unimplemented
867
868 #----------------------------------------------------------------------
869
870 # Write the epilogue.
871
872 print F <<'END';
873 #include "libvirt_c_epilogue.c"
874
875 /* EOF */
876 END
877
878 close F;
879 print "$0: written $filename\n"
880