gendispatch.pl 66.7 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
E
Eric Blake 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (C) 2010-2013 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see
# <http://www.gnu.org/licenses/>.
#
19 20 21 22 23 24 25 26 27
# This script parses remote_protocol.x or qemu_protocol.x and produces lots of
# boilerplate code for both ends of the remote connection.
#
# The first non-option argument specifies the prefix to be searched for, and
# output to, the boilerplate code.  The second non-option argument is the
# file you want to operate on.  For instance, to generate the dispatch table
# for both remote_protocol.x and qemu_protocol.x, you would run the
# following:
#
28
# gendispatch.pl -t remote ../src/remote/remote_protocol.x
29
# gendispatch.pl -t qemu ../src/remote/qemu_protocol.x
30 31
#
# By Richard Jones <rjones@redhat.com>
32
# Extended by Matthias Bolte <matthias.bolte@googlemail.com>
33 34 35

use strict;

36
use Getopt::Long;
37

38 39 40 41 42 43 44
my $mode = "debug";
my $res = GetOptions("mode=s" => \$mode);

die "cannot parse command line options" unless $res;

die "unknown mode '$mode', expecting 'client', 'server' or 'debug'"
    unless $mode =~ /^(client|server|debug)$/;
45

46 47
my $structprefix = shift or die "missing struct prefix argument";
my $procprefix = shift or die "missing procedure prefix argument";
48 49 50
my $protocol = shift or die "missing protocol argument";
my @autogen;

51 52 53 54 55 56 57 58 59 60 61 62 63 64

sub fixup_name {
    my $name = shift;

    $name =~ s/Nwfilter/NWFilter/;
    $name =~ s/Xml$/XML/;
    $name =~ s/Uri$/URI/;
    $name =~ s/Uuid$/UUID/;
    $name =~ s/Id$/ID/;
    $name =~ s/Mac$/MAC/;
    $name =~ s/Cpu$/CPU/;
    $name =~ s/Os$/OS/;
    $name =~ s/Nmi$/NMI/;
    $name =~ s/Pm/PM/;
65
    $name =~ s/Fstrim$/FSTrim/;
O
Osier Yang 已提交
66 67
    $name =~ s/Scsi/SCSI/;
    $name =~ s/Wwn$/WWN/;
68 69 70

    return $name;
}
71 72 73 74

# Convert name_of_call to NameOfCall.
sub name_to_ProcName {
    my $name = shift;
75 76 77

    my @elems;
    if ($name =~ /_/ || (lc $name) eq "open" || (lc $name) eq "close") {
78 79 80
        @elems = split /_/, $name;
        @elems = map lc, @elems;
        @elems = map ucfirst, @elems;
81
    } else {
82
        @elems = $name;
83 84 85 86 87 88 89 90 91 92
    }
    @elems = map { fixup_name($_) } @elems;
    my $procname = join "", @elems;

    return $procname;
}

sub name_to_TypeName {
    my $name = shift;

93
    my @elems = split /_/, $name;
94
    @elems = map lc, @elems;
95
    @elems = map ucfirst, @elems;
96 97 98
    @elems = map { fixup_name($_) } @elems;
    my $typename = join "", @elems;
    return $typename;
99 100 101 102
}

# Read the input file (usually remote_protocol.x) and form an
# opinion about the name, args and return type of each RPC.
103
my ($name, $ProcName, $id, $flags, %calls, @calls, %opts);
104

105
my $collect_args_members = 0;
106
my $collect_ret_members = 0;
107
my $collect_opts = 0;
108 109
my $last_name;

110 111 112
open PROTOCOL, "<$protocol" or die "cannot open $protocol: $!";

while (<PROTOCOL>) {
113 114 115 116 117 118
    if ($collect_args_members) {
        if (/^};/) {
            $collect_args_members = 0;
        } elsif ($_ =~ m/^\s*(.*\S)\s*$/) {
            push(@{$calls{$name}->{args_members}}, $1);
        }
119 120 121 122 123 124
    } elsif ($collect_ret_members) {
        if (/^};/) {
            $collect_ret_members = 0;
        } elsif ($_ =~ m/^\s*(.*\S)\s*$/) {
            push(@{$calls{$name}->{ret_members}}, $1);
        }
125 126 127 128 129 130 131 132 133 134 135 136 137
    } elsif ($collect_opts) {
        if (m,^\s*\*\s*\@(\w+)\s*:\s*(\w+)\s*$,) {
            $opts{$1} = $2;
        } elsif (m,^\s*\*/\s*$,) {
            $collect_opts = 0;
        } elsif (m,^\s*\*\s*$,) {
            # pass
        } else {
            die "cannot parse $_";
        }
    } elsif (m,/\*\*,) {
        %opts = ();
        $collect_opts = 1;
138 139 140 141
    } elsif (/^struct (${structprefix}_(.*)_args)/ ||
             /^struct (${structprefix}(.*)Args)/) {
        my $structname = $1;
        $name = $2;
142
        $ProcName = name_to_ProcName ($name);
143 144 145
        $name = lc $name;
        $name =~ s/_//g;
        die "duplicate definition of $_"
146 147 148 149 150
            if exists $calls{$name};

        $calls{$name} = {
            name => $name,
            ProcName => $ProcName,
151
            args => $structname,
152 153
            args_members => [],
            ret => "void"
154 155
        };

156
        $collect_args_members = 1;
157
        $collect_ret_members = 0;
158
        $last_name = $name;
159 160 161 162 163
    } elsif (/^struct (${structprefix}_(.*)_ret)\s+{(.*)$/ ||
             /^struct (${structprefix}(.*)Ret)\s+{(.*)$/) {
        my $structname = $1;
        $name = $2;
        $flags = $3;
164
        $ProcName = name_to_ProcName ($name);
165 166
        $name = lc $name;
        $name =~ s/_//g;
167 168

        if (exists $calls{$name}) {
169
            $calls{$name}->{ret} = $structname;
170 171 172 173 174
        } else {
            $calls{$name} = {
                name => $name,
                ProcName => $ProcName,
                args => "void",
175
                ret => $structname,
176
                ret_members => []
177 178
            }
        }
179

180
        if ($flags ne "") {
181 182 183 184 185 186 187
            if (!($flags =~ m/^\s*\/\*\s*insert@(\d+)\s*\*\/\s*$/)) {
                die "invalid generator flags for $calls{$name}->{ret}";
            }

            $calls{$name}->{ret_offset} = int($1);
        }

188
        $collect_args_members = 0;
189 190
        $collect_ret_members = 1;
        $last_name = $name;
191 192 193 194
    } elsif (/^struct (${structprefix}_(.*)_msg)/ ||
             /^struct (${structprefix}(.*)Msg)/) {
        my $structname = $1;
        $name = $2;
195
        $ProcName = name_to_ProcName ($name);
196 197
        $name = lc $name;
        $name =~ s/_//g;
198 199 200
        $calls{$name} = {
            name => $name,
            ProcName => $ProcName,
201
            msg => $structname,
202 203 204
        };

        $collect_args_members = 0;
205
        $collect_ret_members = 0;
206
    } elsif (/^\s*(${procprefix}_PROC_(.*?))\s*=\s*(\d+)\s*,?\s*$/) {
207 208 209
        my $constname = $1;
        $name = $2;
        $id = $3;
210
        $ProcName = name_to_ProcName ($name);
211 212
        $name = lc $name;
        $name =~ s/_//g;
213

214 215 216 217 218 219 220 221 222 223 224 225
        if (!exists $calls{$name}) {
            # that the argument and return value cases have not yet added
            # this procedure to the calls hash means that it has no arguments
            # and no return value. add it to the calls hash now because all
            # procedures have to be listed in the calls hash
            $calls{$name} = {
                name => $name,
                ProcName => $ProcName,
                args => "void",
                ret => "void"
            }
        }
226
        $calls{$name}->{constname} = $constname;
227

228 229 230
        if (!exists $opts{generate}) {
            die "'\@generate' annotation missing for $constname";
        }
231

232 233 234
        if ($opts{generate} !~ /^(both|server|client|none)$/) {
            die "'\@generate' annotation value '$opts{generate}' invalid";
        }
235

236 237 238 239 240 241 242
        if ($opts{generate} eq "both") {
            push(@autogen, $ProcName);
        } elsif ($mode eq "server" && ($opts{generate} eq "server")) {
            push(@autogen, $ProcName);
        } elsif ($mode eq "client" && ($opts{generate} eq "client")) {
            push(@autogen, $ProcName);
        }
243

244 245 246 247 248 249 250 251 252
        if (exists $opts{readstream}) {
            $calls{$name}->{streamflag} = "read";
            $calls{$name}->{streamoffset} = int($opts{readstream});
        } elsif (exists $opts{writestream}) {
            $calls{$name}->{streamflag} = "write";
            $calls{$name}->{streamoffset} = int($opts{writestream});
        } else {
            $calls{$name}->{streamflag} = "none";
        }
253

254

255 256 257 258 259 260
        # for now, we distinguish only two levels of priority:
        # low (0) and high (1)
        if (exists $opts{priority}) {
            if ($opts{priority} eq "high") {
                $calls{$name}->{priority} = 1;
            } elsif ($opts{priority} eq "low") {
261
                $calls{$name}->{priority} = 0;
262 263
            } else {
                die "\@priority annotation value '$opts{priority}' invalid for $constname"
264
            }
265 266
        } else {
            $calls{$name}->{priority} = 0;
267 268
        }

269
        $calls[$id] = $calls{$name};
270 271

        $collect_args_members = 0;
272
        $collect_ret_members = 0;
273 274
    } else {
        $collect_args_members = 0;
275
        $collect_ret_members = 0;
276 277 278
    }
}

279 280
close(PROTOCOL);

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
# this hash contains the procedures that are allowed to map [unsigned] hyper
# to [unsigned] long for legacy reasons in their signature and return type.
# this list is fixed. new procedures and public APIs have to map [unsigned]
# hyper to [unsigned] long long
my $long_legacy = {
    DomainGetInfo               => { ret => { maxMem => 1, memory => 1 } },
    DomainMigrate               => { arg => { flags => 1, resource => 1 } },
    DomainMigrate2              => { arg => { flags => 1, resource => 1 } },
    DomainMigrateBegin3         => { arg => { flags => 1, resource => 1 } },
    DomainMigrateConfirm3       => { arg => { flags => 1, resource => 1 } },
    DomainMigrateDirect         => { arg => { flags => 1, resource => 1 } },
    DomainMigrateFinish         => { arg => { flags => 1 } },
    DomainMigrateFinish2        => { arg => { flags => 1 } },
    DomainMigrateFinish3        => { arg => { flags => 1 } },
    DomainMigratePeer2Peer      => { arg => { flags => 1, resource => 1 } },
    DomainMigratePerform        => { arg => { flags => 1, resource => 1 } },
    DomainMigratePerform3       => { arg => { flags => 1, resource => 1 } },
    DomainMigratePrepare        => { arg => { flags => 1, resource => 1 } },
    DomainMigratePrepare2       => { arg => { flags => 1, resource => 1 } },
    DomainMigratePrepare3       => { arg => { flags => 1, resource => 1 } },
    DomainMigratePrepareTunnel  => { arg => { flags => 1, resource => 1 } },
    DomainMigratePrepareTunnel3 => { arg => { flags => 1, resource => 1 } },
    DomainMigrateToURI          => { arg => { flags => 1, resource => 1 } },
    DomainMigrateToURI2         => { arg => { flags => 1, resource => 1 } },
    DomainMigrateVersion1       => { arg => { flags => 1, resource => 1 } },
    DomainMigrateVersion2       => { arg => { flags => 1, resource => 1 } },
    DomainMigrateVersion3       => { arg => { flags => 1, resource => 1 } },
    DomainMigrateSetMaxSpeed    => { arg => { bandwidth => 1 } },
    DomainSetMaxMemory          => { arg => { memory => 1 } },
    DomainSetMemory             => { arg => { memory => 1 } },
    DomainSetMemoryFlags        => { arg => { memory => 1 } },
312 313
    ConnectGetLibVersion        => { ret => { lib_ver => 1 } },
    ConnectGetVersion           => { ret => { hv_ver => 1 } },
314
    NodeGetInfo                 => { ret => { memory => 1 } },
315
    DomainBlockCommit           => { arg => { bandwidth => 1 } },
316
    DomainBlockPull             => { arg => { bandwidth => 1 } },
317
    DomainBlockRebase           => { arg => { bandwidth => 1 } },
318
    DomainBlockJobSetSpeed      => { arg => { bandwidth => 1 } },
319
    DomainMigrateGetMaxSpeed    => { ret => { bandwidth => 1 } },
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
};

sub hyper_to_long
{
    my $proc_name = shift;
    my $ret_or_arg = shift;
    my $member = shift;

    if ($long_legacy->{$proc_name} and
        $long_legacy->{$proc_name}->{$ret_or_arg} and
        $long_legacy->{$proc_name}->{$ret_or_arg}->{$member}) {
        return 1;
    } else {
        return 0
    }
}

337 338 339 340
#----------------------------------------------------------------------
# Output

print <<__EOF__;
341
/* Automatically generated by gendispatch.pl.
342 343 344 345 346
 * Do not edit this file.  Any changes you make will be lost.
 */
__EOF__

# Debugging.
347
if ($mode eq "debug") {
348 349 350 351 352
    my @keys = sort (keys %calls);
    foreach (@keys) {
        print "$_:\n";
        print "        name $calls{$_}->{name} ($calls{$_}->{ProcName})\n";
        print "        $calls{$_}->{args} -> $calls{$_}->{ret}\n";
353
        print "        priority -> $calls{$_}->{priority}\n";
354 355 356
    }
}

357
# Bodies for dispatch functions ("remote_dispatch_bodies.h").
358
elsif ($mode eq "server") {
359
    my %generate = map { $_ => 1 } @autogen;
360 361 362
    my @keys = sort (keys %calls);

    foreach (@keys) {
363 364
        my $call = $calls{$_};

365
        # skip things which are REMOTE_MESSAGE
366
        next if $call->{msg};
367

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
        my $name = $structprefix . "Dispatch" . $call->{ProcName};
        my $argtype = $call->{args};
        my $rettype = $call->{ret};

        my $argann = $argtype ne "void" ? "" : " ATTRIBUTE_UNUSED";
        my $retann = $rettype ne "void" ? "" : " ATTRIBUTE_UNUSED";

        # First we print out a function declaration for the
        # real dispatcher body
        print "static int ${name}(\n";
        print "    virNetServerPtr server,\n";
        print "    virNetServerClientPtr client,\n";
        print "    virNetMessagePtr msg,\n";
        print "    virNetMessageErrorPtr rerr";
        if ($argtype ne "void") {
            print ",\n    $argtype *args";
        }
        if ($rettype ne "void") {
            print ",\n    $rettype *ret";
        }
        print ");\n";


        # Next we print out a generic wrapper method which has
        # fixed function signature, for use in the dispatcher
        # table. This simply callers the real dispatcher method
        print "static int ${name}Helper(\n";
        print "    virNetServerPtr server,\n";
        print "    virNetServerClientPtr client,\n";
        print "    virNetMessagePtr msg,\n";
        print "    virNetMessageErrorPtr rerr,\n";
        print "    void *args$argann,\n";
        print "    void *ret$retann)\n";
        print "{\n";
        print "  VIR_DEBUG(\"server=%p client=%p msg=%p rerr=%p args=%p ret=%p\", server, client, msg, rerr, args, ret);\n";
        print "  return $name(server, client, msg, rerr";
        if ($argtype ne "void") {
            print ", args";
        }
        if ($rettype ne "void") {
            print ", ret";
        }
        print ");\n";
        print "}\n";

        # Finally we print out the dispatcher method body impl
        # (if possible)
415 416 417 418 419
        if (!exists($generate{$call->{ProcName}})) {
            print "/* ${structprefix}Dispatch$call->{ProcName} body has " .
                  "to be implemented manually */\n\n\n\n";
            next;
        }
420 421 422

        my $has_node_device = 0;
        my @vars_list = ();
423
        my @optionals_list = ();
424 425
        my @getters_list = ();
        my @args_list = ();
426
        my @prepare_ret_list = ();
427
        my @ret_list = ();
428
        my @free_list = ();
429
        my @free_list_on_error = ("virNetMessageSaveError(rerr);");
430

431
        # handle arguments to the function
432
        if ($argtype ne "void") {
433
            # node device is special, as it's identified by name
434 435
            if ($argtype =~ m/^remote_node_device_/ and
                !($argtype =~ m/^remote_node_device_lookup_by_name_/) and
O
Osier Yang 已提交
436 437
                !($argtype =~ m/^remote_node_device_create_xml_/) and
                !($argtype =~ m/^remote_node_device_lookup_scsi_host_by_wwn_/)) {
438 439 440
                $has_node_device = 1;
                push(@vars_list, "virNodeDevicePtr dev = NULL");
                push(@getters_list,
441
                     "    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))\n" .
442 443 444 445 446 447 448
                     "        goto cleanup;\n");
                push(@args_list, "dev");
                push(@free_list,
                     "    if (dev)\n" .
                     "        virNodeDeviceFree(dev);");
            }

449
            foreach my $args_member (@{$call->{args_members}}) {
450 451 452
                if ($args_member =~ m/^remote_nonnull_string name;/ and $has_node_device) {
                    # ignore the name arg for node devices
                    next
453
                } elsif ($args_member =~ m/^remote_nonnull_(domain|network|storage_pool|storage_vol|interface|secret|nwfilter) (\S+);/) {
454
                    my $type_name = name_to_TypeName($1);
455 456

                    push(@vars_list, "vir${type_name}Ptr $2 = NULL");
457
                    push(@getters_list,
458
                         "    if (!($2 = get_nonnull_$1(priv->conn, args->$2)))\n" .
459
                         "        goto cleanup;\n");
460
                    push(@args_list, "$2");
461
                    push(@free_list,
462 463
                         "    if ($2)\n" .
                         "        vir${type_name}Free($2);");
464 465 466 467
                } elsif ($args_member =~ m/^remote_nonnull_domain_snapshot /) {
                    push(@vars_list, "virDomainPtr dom = NULL");
                    push(@vars_list, "virDomainSnapshotPtr snapshot = NULL");
                    push(@getters_list,
468
                         "    if (!(dom = get_nonnull_domain(priv->conn, args->snap.dom)))\n" .
469 470 471 472 473 474 475 476 477 478
                         "        goto cleanup;\n" .
                         "\n" .
                         "    if (!(snapshot = get_nonnull_domain_snapshot(dom, args->snap)))\n" .
                         "        goto cleanup;\n");
                    push(@args_list, "snapshot");
                    push(@free_list,
                         "    if (snapshot)\n" .
                         "        virDomainSnapshotFree(snapshot);\n" .
                         "    if (dom)\n" .
                         "        virDomainFree(dom);");
479
                } elsif ($args_member =~ m/^(?:remote_string|remote_uuid) (\S+)<\S+>;/) {
480
                    if (! @args_list) {
481
                        push(@args_list, "priv->conn");
482 483
                    }

484 485 486 487
                    push(@args_list, "args->$1.$1_val");
                    push(@args_list, "args->$1.$1_len");
                } elsif ($args_member =~ m/^(?:opaque|remote_nonnull_string) (\S+)<\S+>;(.*)$/) {
                    if (! @args_list) {
488
                        push(@args_list, "priv->conn");
489 490
                    }

491 492 493 494 495 496 497 498 499 500 501 502 503 504
                    my $cast = "";
                    my $arg_name = $1;
                    my $annotation = $2;

                    if ($annotation ne "") {
                        if ($annotation =~ m/\s*\/\*\s*(.*)\s*\*\//) {
                            $cast = $1;
                        } else {
                            die "malformed cast annotation for argument: $args_member";
                        }
                    }

                    push(@args_list, "${cast}args->$arg_name.${arg_name}_val");
                    push(@args_list, "args->$arg_name.${arg_name}_len");
505 506
                } elsif ($args_member =~ m/^(?:unsigned )?int (\S+)<\S+>;/) {
                    if (! @args_list) {
507
                        push(@args_list, "priv->conn");
508 509 510 511
                    }

                    push(@args_list, "args->$1.$1_val");
                    push(@args_list, "args->$1.$1_len");
512 513
                } elsif ($args_member =~ m/^remote_typed_param (\S+)<(\S+)>;/) {
                    push(@vars_list, "virTypedParameterPtr $1 = NULL");
514
                    push(@vars_list, "int n$1 = 0;");
515 516 517
                    if ($call->{ProcName} eq "NodeSetMemoryParameters") {
                        push(@args_list, "priv->conn");
                    }
518 519 520 521 522 523 524
                    push(@args_list, "$1");
                    push(@args_list, "n$1");
                    push(@getters_list, "    if (($1 = remoteDeserializeTypedParameters(args->$1.$1_val,\n" .
                                        "                                                   args->$1.$1_len,\n" .
                                        "                                                   $2,\n" .
                                        "                                                   &n$1)) == NULL)\n" .
                                        "        goto cleanup;\n");
525
                    push(@free_list, "    virTypedParamsFree($1, n$1);");
526 527 528 529
                } elsif ($args_member =~ m/<\S+>;/ or $args_member =~ m/\[\S+\];/) {
                    # just make all other array types fail
                    die "unhandled type for argument value: $args_member";
                } elsif ($args_member =~ m/^remote_uuid (\S+);/) {
530
                    if (! @args_list) {
531
                        push(@args_list, "priv->conn");
532 533
                    }

534 535 536
                    push(@args_list, "(unsigned char *) args->$1");
                } elsif ($args_member =~ m/^remote_string (\S+);/) {
                    if (! @args_list) {
537
                        push(@args_list, "priv->conn");
538
                    }
539 540 541 542 543 544

                    push(@vars_list, "char *$1");
                    push(@optionals_list, "$1");
                    push(@args_list, "$1");
                } elsif ($args_member =~ m/^remote_nonnull_string (\S+);/) {
                    if (! @args_list) {
545
                        push(@args_list, "priv->conn");
546 547 548
                    }

                    push(@args_list, "args->$1");
549
                } elsif ($args_member =~ m/^(unsigned )?int (\S+);/) {
550
                    if (! @args_list) {
551
                        push(@args_list, "priv->conn");
552 553
                    }

554 555 556
                    push(@args_list, "args->$2");
                } elsif ($args_member =~ m/^(unsigned )?hyper (\S+);/) {
                    if (! @args_list) {
557
                        push(@args_list, "priv->conn");
558 559 560 561 562 563 564 565 566 567 568 569 570 571
                    }

                    my $arg_name = $2;

                    if (hyper_to_long($call->{ProcName}, "arg", $arg_name)) {
                        my $type_name = $1; $type_name .= "long";
                        my $sign = ""; $sign = "U" if ($1);

                        push(@vars_list, "$type_name $arg_name");
                        push(@getters_list, "    HYPER_TO_${sign}LONG($arg_name, args->$arg_name);\n");
                        push(@args_list, "$arg_name");
                    } else {
                        push(@args_list, "args->$arg_name");
                    }
572
                } elsif ($args_member =~ m/^(\/)?\*/) {
573 574 575
                    # ignore comments
                } else {
                    die "unhandled type for argument value: $args_member";
576 577 578 579
                }
            }
        }

580
        # handle return values of the function
581 582 583
        my $single_ret_var = "undefined";
        my $single_ret_by_ref = 0;
        my $single_ret_check = " == undefined";
584 585 586 587
        my $single_ret_as_list = 0;
        my $single_ret_list_name = "undefined";
        my $single_ret_list_max_var = "undefined";
        my $single_ret_list_max_define = "undefined";
588 589
        my $multi_ret = 0;

590
        if ($rettype ne "void" and
591
            scalar(@{$call->{ret_members}}) > 1) {
592 593
            $multi_ret = 1;
        }
594

595
        if ($rettype ne "void") {
596
            foreach my $ret_member (@{$call->{ret_members}}) {
597
                if ($multi_ret) {
598
                    if ($ret_member =~ m/^(unsigned )?(char|short|int|hyper) (\S+)\[\S+\];/) {
599 600 601 602
                        if ($2 eq "hyper" and hyper_to_long($call->{ProcName}, "ret", $3)) {
                            die "legacy [u]long hyper arrays aren't supported";
                        }

603
                        push(@ret_list, "memcpy(ret->$3, tmp.$3, sizeof(ret->$3));");
604 605
                    } elsif ($ret_member =~ m/^(unsigned )?(char|short|int|hyper) (\S+);/) {
                        push(@ret_list, "ret->$3 = tmp.$3;");
606 607 608
                    } else {
                        die "unhandled type for multi-return-value: $ret_member";
                    }
609
                } elsif ($ret_member =~ m/^remote_nonnull_string (\S+)<(\S+)>;\s*\/\*\s*insert@(\d+)\s*\*\//) {
610
                    push(@vars_list, "int len");
611
                    splice(@args_list, int($3), 0, ("ret->$1.$1_val"));
612
                    push(@ret_list, "ret->$1.$1_len = len;");
613
                    push(@free_list_on_error, "VIR_FREE(ret->$1.$1_val);");
614 615 616 617 618 619 620
                    $single_ret_var = "len";
                    $single_ret_by_ref = 0;
                    $single_ret_check = " < 0";
                    $single_ret_as_list = 1;
                    $single_ret_list_name = $1;
                    $single_ret_list_max_var = "max$1";
                    $single_ret_list_max_define = $2;
621 622 623
                } elsif ($ret_member =~ m/^remote_nonnull_string (\S+)<\S+>;/) {
                    # error out on unannotated arrays
                    die "remote_nonnull_string array without insert@<offset> annotation: $ret_member";
624
                } elsif ($ret_member =~ m/^remote_nonnull_string (\S+);/) {
625
                    if ($call->{ProcName} eq "ConnectGetType") {
626 627 628
                        # SPECIAL: virConnectGetType returns a constant string that must
                        #          not be freed. Therefore, duplicate the string here.
                        push(@vars_list, "const char *$1");
629
                        push(@ret_list, "/* We have to VIR_STRDUP because remoteDispatchClientRequest will");
630
                        push(@ret_list, " * free this string after it's been serialised. */");
631
                        push(@ret_list, "if (VIR_STRDUP(ret->type, type) < 0)");
632 633 634 635 636 637
                        push(@ret_list, "    goto cleanup;");
                    } else {
                        push(@vars_list, "char *$1");
                        push(@ret_list, "ret->$1 = $1;");
                    }

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/^remote_string (\S+);/) {
                    push(@vars_list, "char *$1 = NULL");
                    push(@vars_list, "char **$1_p = NULL");
                    push(@ret_list, "ret->$1 = $1_p;");
                    push(@free_list, "    VIR_FREE($1);");
                    push(@free_list_on_error, "VIR_FREE($1_p);");
                    push(@prepare_ret_list,
                         "if (VIR_ALLOC($1_p) < 0) {\n" .
                         "        virReportOOMError();\n" .
                         "        goto cleanup;\n" .
                         "    }\n" .
                         "    \n" .
653 654
                         "    if (VIR_STRDUP(*$1_p, $1) < 0)\n".
                         "        goto cleanup;\n");
655

656 657 658
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
659
                } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|storage_pool|storage_vol|interface|node_device|secret|nwfilter|domain_snapshot) (\S+);/) {
660
                    my $type_name = name_to_TypeName($1);
661

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
                    if ($call->{ProcName} eq "DomainCreateWithFlags") {
                        # SPECIAL: virDomainCreateWithFlags updates the given
                        #          domain object instead of returning a new one
                        push(@ret_list, "make_nonnull_$1(&ret->$2, $2);");
                        $single_ret_var = undef;
                        $single_ret_by_ref = 1;
                    } else {
                        push(@vars_list, "vir${type_name}Ptr $2 = NULL");
                        push(@ret_list, "make_nonnull_$1(&ret->$2, $2);");
                        push(@free_list,
                             "    if ($2)\n" .
                             "        vir${type_name}Free($2);");
                        $single_ret_var = $2;
                        $single_ret_by_ref = 0;
                        $single_ret_check = " == NULL";
                    }
678
                } elsif ($ret_member =~ m/^int (\S+)<(\S+)>;\s*\/\*\s*insert@(\d+)\s*\*\//) {
679
                    push(@vars_list, "int len");
680
                    splice(@args_list, int($3), 0, ("ret->$1.$1_val"));
681
                    push(@ret_list, "ret->$1.$1_len = len;");
682
                    push(@free_list_on_error, "VIR_FREE(ret->$1.$1_val);");
683 684 685 686 687 688 689
                    $single_ret_var = "len";
                    $single_ret_by_ref = 0;
                    $single_ret_check = " < 0";
                    $single_ret_as_list = 1;
                    $single_ret_list_name = $1;
                    $single_ret_list_max_var = "max$1";
                    $single_ret_list_max_define = $2;
690 691 692
                } elsif ($ret_member =~ m/^int (\S+)<\S+>;/) {
                    # error out on unannotated arrays
                    die "int array without insert@<offset> annotation: $ret_member";
693
                } elsif ($ret_member =~ m/^int (\S+);/) {
694 695 696 697
                    push(@vars_list, "int $1");
                    push(@ret_list, "ret->$1 = $1;");
                    $single_ret_var = $1;

698
                    if ($call->{ProcName} =~ m/GetAutostart$/) {
699 700 701
                        $single_ret_by_ref = 1;
                    } else {
                        $single_ret_by_ref = 0;
702

703
                        if ($call->{ProcName} eq "CPUCompare") {
704 705 706 707
                            $single_ret_check = " == VIR_CPU_COMPARE_ERROR";
                        } else {
                            $single_ret_check = " < 0";
                        }
708
                    }
709
                } elsif ($ret_member =~ m/^(?:unsigned )?hyper (\S+)<(\S+)>;\s*\/\*\s*insert@(\d+)\s*\*\//) {
710 711 712 713
                    if (hyper_to_long($call->{ProcName}, "ret", $1)) {
                        die "legacy [u]long hyper arrays aren't supported";
                    }

714 715
                    push(@vars_list, "int len");
                    push(@ret_list, "ret->$1.$1_len = len;");
716
                    push(@free_list_on_error, "VIR_FREE(ret->$1.$1_val);");
717 718 719 720
                    $single_ret_var = "len";
                    $single_ret_by_ref = 0;
                    $single_ret_as_list = 1;
                    $single_ret_list_name = $1;
721
                    $single_ret_list_max_var = "max$1";
722 723
                    $single_ret_list_max_define = $2;

724
                    if ($call->{ProcName} eq "NodeGetCellsFreeMemory") {
725
                        $single_ret_check = " <= 0";
726
                        splice(@args_list, int($3), 0, ("(unsigned long long *)ret->$1.$1_val"));
727 728
                    } else {
                        $single_ret_check = " < 0";
729
                        splice(@args_list, int($3), 0, ("ret->$1.$1_val"));
730
                    }
731
                } elsif ($ret_member =~ m/^(?:unsigned )?hyper (\S+)<\S+>;/) {
732 733
                    # error out on unannotated arrays
                    die "hyper array without insert@<offset> annotation: $ret_member";
734
                } elsif ($ret_member =~ m/^(unsigned )?hyper (\S+);(?:\s*\/\*\s*insert@(\d+)\s*\*\/)?/) {
735
                    my $type_name = $1;
736
                    my $ret_name = $2;
737
                    my $ret_assign;
738
                    my $insert = $3;
739

740 741 742 743 744 745 746 747 748
                    if (hyper_to_long($call->{ProcName}, "ret", $ret_name)) {
                        my $sign = ""; $sign = "U" if ($1);

                        $type_name .= "long";
                        $ret_assign = "HYPER_TO_${sign}LONG(ret->$ret_name, $ret_name);";
                    } else {
                        $type_name .= "long long";
                        $ret_assign = "ret->$ret_name = $ret_name;";
                    }
749 750

                    push(@vars_list, "$type_name $ret_name");
751
                    push(@ret_list, $ret_assign);
752 753 754 755 756 757 758

                    if ($insert) {
                        splice(@args_list, int($insert), 0, "&$ret_name");
                        $single_ret_var = undef;
                    } else {
                        $single_ret_var = $ret_name;
                    }
759

760 761
                    if ($call->{ProcName} eq "DomainGetMaxMemory" or
                        $call->{ProcName} eq "NodeGetFreeMemory") {
762 763 764
                        # SPECIAL: virDomainGetMaxMemory and virNodeGetFreeMemory
                        #          return the actual value directly and 0 indicates
                        #          an error
765 766 767 768 769
                        $single_ret_by_ref = 0;
                        $single_ret_check = " == 0";
                    } else {
                        $single_ret_by_ref = 1;
                    }
770 771 772 773 774 775 776 777 778 779 780 781
                } elsif ($ret_member =~ m/^opaque (\S+)<(\S+)>;\s*\/\*\s*insert@(\d+)\s*\*\//) {
                    push(@vars_list, "char *$1 = NULL");
                    push(@vars_list, "int $1_len = 0");
                    splice(@args_list, int($3), 0, ("&$1", "&$1_len"));
                    push(@ret_list, "ret->$1.$1_val = $1;");
                    push(@ret_list, "ret->$1.$1_len = $1_len;");
                    push(@free_list_on_error, "VIR_FREE($1);");
                    $single_ret_var = undef;
                    $single_ret_by_ref = 1;
                } elsif ($ret_member =~ m/^opaque (\S+)<\S+>;/) {
                    # error out on unannotated arrays
                    die "opaque array without insert@<offset> annotation: $ret_member";
782 783
                } elsif ($ret_member =~ m/^(\/)?\*/) {
                    # ignore comments
784 785
                } else {
                    die "unhandled type for return value: $ret_member";
786 787 788 789
                }
            }
        }

790
        # select struct type for multi-return-value functions
791
        if ($multi_ret) {
792 793 794 795
            if (!(defined $call->{ret_offset})) {
                die "multi-return-value without insert@<offset> annotation: $call->{ret}";
            }

796 797
            if (! @args_list) {
                push(@args_list, "priv->conn");
798 799
            }

800
            my $struct_name = $call->{ProcName};
801 802
            $struct_name =~ s/Get//;

803 804 805 806
            splice(@args_list, $call->{ret_offset}, 0, ("&tmp"));

            if ($call->{ProcName} eq "DomainBlockStats" ||
                $call->{ProcName} eq "DomainInterfaceStats") {
807 808 809
                # SPECIAL: virDomainBlockStats and virDomainInterfaceStats
                #          have a 'Struct' suffix on the actual struct name
                #          and take the struct size as additional argument
810
                $struct_name .= "Struct";
811
                splice(@args_list, $call->{ret_offset} + 1, 0, ("sizeof(tmp)"));
812 813 814 815 816
            }

            push(@vars_list, "vir$struct_name tmp");
        }

817
        if ($call->{streamflag} ne "none") {
818
            splice(@args_list, $call->{streamoffset}, 0, ("st"));
819
            push(@free_list_on_error, "if (stream) {");
820 821 822 823
            push(@free_list_on_error, "    virStreamAbort(st);");
            push(@free_list_on_error, "    daemonFreeClientStream(client, stream);");
            push(@free_list_on_error, "} else {");
            push(@free_list_on_error, "    virStreamFree(st);");
824 825 826
            push(@free_list_on_error, "}");
        }

827
        # print functions signature
828 829 830 831 832
        print "static int $name(\n";
        print "    virNetServerPtr server ATTRIBUTE_UNUSED,\n";
        print "    virNetServerClientPtr client,\n";
        print "    virNetMessagePtr msg ATTRIBUTE_UNUSED,\n";
        print "    virNetMessageErrorPtr rerr";
833
        if ($argtype ne "void") {
834 835
            print ",\n    $argtype *args";
        }
836
        if ($rettype ne "void") {
837 838 839
            print ",\n    $rettype *ret";
        }
        print ")\n";
840 841 842 843 844

        # print function body
        print "{\n";
        print "    int rv = -1;\n";

845 846 847
        foreach my $var (@vars_list) {
            print "    $var;\n";
        }
848
        print "    struct daemonClientPrivate *priv =\n";
849
        print "        virNetServerClientGetPrivateData(client);\n";
850

851
        if ($call->{streamflag} ne "none") {
852 853
            print "    virStreamPtr st = NULL;\n";
            print "    daemonClientStreamPtr stream = NULL;\n";
854 855
        }

856
        print "\n";
857
        print "    if (!priv->conn) {\n";
858
        print "        virReportError(VIR_ERR_INTERNAL_ERROR, \"%s\", _(\"connection not open\"));\n";
859 860 861 862
        print "        goto cleanup;\n";
        print "    }\n";
        print "\n";

863 864
        if ($single_ret_as_list) {
            print "    if (args->$single_ret_list_max_var > $single_ret_list_max_define) {\n";
865 866
            print "        virReportError(VIR_ERR_INTERNAL_ERROR,\n";
            print "                       \"%s\", _(\"max$single_ret_list_name > $single_ret_list_max_define\"));\n";
867 868 869 870 871
            print "        goto cleanup;\n";
            print "    }\n";
            print "\n";
        }

872 873
        print join("\n", @getters_list);

874 875 876
        if (@getters_list) {
            print "\n";
        }
877

878 879 880 881 882 883 884 885
        foreach my $optional (@optionals_list) {
            print "    $optional = args->$optional ? *args->$optional : NULL;\n";
        }

        if (@optionals_list) {
            print "\n";
        }

886
        if ($call->{streamflag} ne "none") {
887 888 889
            print "    if (!(st = virStreamNew(priv->conn, VIR_STREAM_NONBLOCK)))\n";
            print "        goto cleanup;\n";
            print "\n";
890
            print "    if (!(stream = daemonCreateClientStream(client, st, remoteProgram, &msg->header)))\n";
891 892 893 894
            print "        goto cleanup;\n";
            print "\n";
        }

895
        if ($rettype eq "void") {
896
            print "    if (vir$call->{ProcName}(";
897 898 899 900
            print join(', ', @args_list);
            print ") < 0)\n";
            print "        goto cleanup;\n";
            print "\n";
901
        } elsif (!$multi_ret) {
902
            my $prefix = "";
903
            my $proc_name = $call->{ProcName};
904 905

            if (! @args_list) {
906
                push(@args_list, "priv->conn");
907 908
            }

909
            if ($structprefix eq "qemu" && $call->{ProcName} =~ /^Domain/) {
910
                $proc_name =~ s/^(Domain)/${1}Qemu/;
911
            }
912 913 914
            if ($structprefix eq "lxc" && $call->{ProcName} =~ /^Domain/) {
                $proc_name =~ s/^(Domain)/${1}Lxc/;
            }
915

916 917 918 919 920 921 922 923 924 925
            if ($single_ret_as_list) {
                print "    /* Allocate return buffer. */\n";
                print "    if (VIR_ALLOC_N(ret->$single_ret_list_name.${single_ret_list_name}_val," .
                      " args->$single_ret_list_max_var) < 0) {\n";
                print "        virReportOOMError();\n";
                print "        goto cleanup;\n";
                print "    }\n";
                print "\n";
            }

926 927 928
            if ($single_ret_by_ref) {
                print "    if (vir$prefix$proc_name(";
                print join(', ', @args_list);
929 930 931 932 933 934

                if (defined $single_ret_var) {
                    print ", &$single_ret_var";
                }

                print ") < 0)\n";
935 936 937 938 939 940 941 942
            } else {
                print "    if (($single_ret_var = vir$prefix$proc_name(";
                print join(', ', @args_list);
                print "))$single_ret_check)\n";
            }

            print "        goto cleanup;\n";
            print "\n";
943
        } else {
944
            print "    if (vir$call->{ProcName}(";
945 946 947 948
            print join(', ', @args_list);
            print ") < 0)\n";
            print "        goto cleanup;\n";
            print "\n";
949
        }
950

951
        if ($call->{streamflag} ne "none") {
952
            print "    if (daemonAddClientStream(client, stream, ";
953 954

            if ($call->{streamflag} eq "write") {
955
                print "false";
956
            } else {
957
                print "true";
958 959
            }

960 961 962 963 964
            print ") < 0)\n";
            print "        goto cleanup;\n";
            print "\n";
        }

965 966 967 968 969 970
        if (@prepare_ret_list) {
            print "    ";
            print join("\n    ", @prepare_ret_list);
            print "\n";
        }

971 972
        if (@ret_list) {
            print "    ";
973
            print join("\n    ", @ret_list);
974
            print "\n";
975 976 977 978 979
        }

        print "    rv = 0;\n";
        print "\n";
        print "cleanup:\n";
980 981 982 983 984 985 986 987 988 989 990 991 992
        print "    if (rv < 0)";

        if (scalar(@free_list_on_error) > 1) {
            print " {";
        }

        print "\n        ";
        print join("\n        ", @free_list_on_error);
        print "\n";

        if (scalar(@free_list_on_error) > 1) {
            print "    }\n";
        }
993 994 995

        print join("\n", @free_list);

996 997 998 999
        if (@free_list) {
            print "\n";
        }

1000
        print "    return rv;\n";
1001
        print "}\n\n\n\n";
1002
    }
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013


    # Finally we write out the huge dispatch table which lists
    # the dispatch helper method. the XDR proc for processing
    # args and return values, and the size of the args and
    # return value structs. All methods are marked as requiring
    # authentication. Methods are selectively relaxed in the
    # daemon code which registers the program.

    print "virNetServerProgramProc ${structprefix}Procs[] = {\n";
    for ($id = 0 ; $id <= $#calls ; $id++) {
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        my ($comment, $name, $argtype, $arglen, $argfilter, $retlen, $retfilter, $priority);

        if (defined $calls[$id] && !$calls[$id]->{msg}) {
            $comment = "/* Method $calls[$id]->{ProcName} => $id */";
            $name = $structprefix . "Dispatch" . $calls[$id]->{ProcName} . "Helper";
            my $argtype = $calls[$id]->{args};
            my $rettype = $calls[$id]->{ret};
            $arglen = $argtype ne "void" ? "sizeof($argtype)" : "0";
            $retlen = $rettype ne "void" ? "sizeof($rettype)" : "0";
            $argfilter = $argtype ne "void" ? "xdr_$argtype" : "xdr_void";
            $retfilter = $rettype ne "void" ? "xdr_$rettype" : "xdr_void";
        } else {
            if ($calls[$id]->{msg}) {
                $comment = "/* Async event $calls[$id]->{ProcName} => $id */";
            } else {
                $comment = "/* Unused $id */";
            }
            $name = "NULL";
            $arglen = $retlen = 0;
            $argfilter = "xdr_void";
            $retfilter = "xdr_void";
        }
1036

1037 1038
    $priority = defined $calls[$id]->{priority} ? $calls[$id]->{priority} : 0;

1039
        print "{ $comment\n   ${name},\n   $arglen,\n   (xdrproc_t)$argfilter,\n   $retlen,\n   (xdrproc_t)$retfilter,\n   true,\n   $priority\n},\n";
1040 1041 1042
    }
    print "};\n";
    print "size_t ${structprefix}NProcs = ARRAY_CARDINALITY(${structprefix}Procs);\n";
1043
}
1044

1045
# Bodies for client functions ("remote_client_bodies.h").
1046
elsif ($mode eq "client") {
1047
    my %generate = map { $_ => 1 } @autogen;
1048 1049 1050 1051 1052 1053 1054 1055
    my @keys = sort (keys %calls);

    foreach (@keys) {
        my $call = $calls{$_};

        # skip things which are REMOTE_MESSAGE
        next if $call->{msg};

1056 1057
        # skip procedures not on generate list
        next if ! exists($generate{$call->{ProcName}});
1058

1059 1060
        my $argtype = $call->{args};
        my $rettype = $call->{ret};
1061

1062
        # handle arguments to the function
1063 1064
        my @args_list = ();
        my @vars_list = ();
1065
        my @args_check_list = ();
1066
        my @setters_list = ();
1067
        my @setters_list2 = ();
1068
        my @free_list = ();
1069 1070
        my $priv_src = "conn";
        my $priv_name = "privateData";
1071
        my $call_args = "&args";
1072

1073
        if ($argtype eq "void") {
1074
            $call_args = "NULL";
1075
        } else {
1076
            push(@vars_list, "$argtype args");
1077 1078 1079 1080 1081

            my $is_first_arg = 1;
            my $has_node_device = 0;

            # node device is special
1082 1083 1084
            if ($argtype =~ m/^remote_node_/ and
                !($argtype =~ m/^remote_node_device_lookup_by_name_/) and
                !($argtype =~ m/^remote_node_device_create_xml_/)) {
1085
                $has_node_device = 1;
1086
                $priv_name = "nodeDevicePrivateData";
1087 1088 1089 1090 1091 1092 1093 1094 1095
            }

            foreach my $args_member (@{$call->{args_members}}) {
                if ($args_member =~ m/^remote_nonnull_string name;/ and $has_node_device) {
                    $priv_src = "dev->conn";
                    push(@args_list, "virNodeDevicePtr dev");
                    push(@setters_list, "args.name = dev->name;");
                } elsif ($args_member =~ m/^remote_nonnull_(domain|network|storage_pool|storage_vol|interface|secret|nwfilter|domain_snapshot) (\S+);/) {
                    my $name = $1;
1096
                    my $arg_name = $2;
1097
                    my $type_name = name_to_TypeName($name);
1098 1099 1100

                    if ($is_first_arg) {
                        if ($name eq "domain_snapshot") {
1101
                            $priv_src = "$arg_name->domain->conn";
1102
                        } else {
1103
                            $priv_src = "$arg_name->conn";
1104 1105 1106 1107 1108 1109 1110 1111 1112
                        }

                        if ($name =~ m/^storage_/) {
                            $priv_name = "storagePrivateData";
                        } elsif (!($name =~ m/^domain/)) {
                            $priv_name = "${name}PrivateData";
                        }
                    }

1113 1114 1115 1116 1117
                    push(@args_list, "vir${type_name}Ptr $arg_name");
                    push(@setters_list, "make_nonnull_$1(&args.$arg_name, $arg_name);");
                } elsif ($args_member =~ m/^remote_uuid (\S+);/) {
                    push(@args_list, "const unsigned char *$1");
                    push(@setters_list, "memcpy(args.$1, $1, VIR_UUID_BUFLEN);");
1118 1119 1120
                } elsif ($args_member =~ m/^remote_string (\S+);/) {
                    push(@args_list, "const char *$1");
                    push(@setters_list, "args.$1 = $1 ? (char **)&$1 : NULL;");
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
                } elsif ($args_member =~ m/^remote_nonnull_string (\S+)<(\S+)>;(.*)$/) {
                    my $type_name = "const char **";
                    my $arg_name = $1;
                    my $limit = $2;
                    my $annotation = $3;

                    if ($annotation ne "") {
                        if ($annotation =~ m/\s*\/\*\s*\((.*)\)\s*\*\//) {
                            $type_name = $1;
                        } else {
                            die "malformed cast annotation for argument: $args_member";
                        }
                    }

                    push(@args_list, "$type_name$arg_name");
                    push(@args_list, "unsigned int ${arg_name}len");
                    push(@setters_list, "args.$arg_name.${arg_name}_val = (char **)$arg_name;");
                    push(@setters_list, "args.$arg_name.${arg_name}_len = ${arg_name}len;");
                    push(@args_check_list, { name => "\"$arg_name\"", arg => "${arg_name}len", limit => $2 });
1140 1141 1142
                } elsif ($args_member =~ m/^remote_nonnull_string (\S+);/) {
                    push(@args_list, "const char *$1");
                    push(@setters_list, "args.$1 = (char *)$1;");
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
                } elsif ($args_member =~ m/^opaque (\S+)<(\S+)>;(.*)$/) {
                    my $type_name = "const char *";
                    my $arg_name = $1;
                    my $limit = $2;
                    my $annotation = $3;

                    if ($annotation ne "") {
                        if ($annotation =~ m/\s*\/\*\s*\((.*)\)\s*\*\//) {
                            $type_name = $1;
                        } else {
                            die "malformed cast annotation for argument: $args_member";
                        }
                    }

                    push(@args_list, "$type_name$arg_name");
1158

1159
                    if ($call->{ProcName} eq "SecretSetValue") {
1160
                        # SPECIAL: virSecretSetValue uses size_t instead of int
1161
                        push(@args_list, "size_t ${arg_name}len");
1162
                    } else {
1163
                        push(@args_list, "int ${arg_name}len");
1164 1165
                    }

1166 1167 1168 1169 1170 1171 1172 1173 1174
                    push(@setters_list, "args.$arg_name.${arg_name}_val = (char *)$arg_name;");
                    push(@setters_list, "args.$arg_name.${arg_name}_len = ${arg_name}len;");
                    push(@args_check_list, { name => "\"$arg_name\"", arg => "${arg_name}len", limit => $limit });
                } elsif ($args_member =~ m/^remote_string (\S+)<(\S+)>;/) {
                    my $arg_name = $1;
                    my $limit = $2;

                    push(@args_list, "const char *$arg_name");
                    push(@args_list, "int ${arg_name}len");
1175 1176 1177
                    push(@setters_list, "args.$arg_name.${arg_name}_val = (char *)$arg_name;");
                    push(@setters_list, "args.$arg_name.${arg_name}_len = ${arg_name}len;");
                    push(@args_check_list, { name => "\"$arg_name\"", arg => "${arg_name}len", limit => $limit });
1178 1179 1180 1181 1182 1183
                } elsif ($args_member =~ m/^((?:unsigned )?int) (\S+)<(\S+)>;/) {
                    my $type_name = $1;
                    my $arg_name = $2;
                    my $limit = $3;

                    push(@args_list, "${type_name} *$arg_name");
1184
                    push(@args_list, "int ${arg_name}len");
1185 1186 1187
                    push(@setters_list, "args.$arg_name.${arg_name}_val = $arg_name;");
                    push(@setters_list, "args.$arg_name.${arg_name}_len = ${arg_name}len;");
                    push(@args_check_list, { name => "\"$arg_name\"", arg => "${arg_name}len", limit => $limit });
1188 1189 1190 1191 1192 1193 1194
                } elsif ($args_member =~ m/^remote_typed_param (\S+)<(\S+)>;/) {
                    push(@args_list, "virTypedParameterPtr $1");
                    push(@args_list, "int n$1");
                    push(@setters_list2, "if (remoteSerializeTypedParameters($1, n$1, &args.$1.$1_val, &args.$1.$1_len) < 0) {\n" .
                                         "        xdr_free((xdrproc_t)xdr_$call->{args}, (char *)&args);\n" .
                                         "        goto done;\n" .
                                         "    }");
1195
                    push(@free_list, "    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);\n");
1196 1197
                } elsif ($args_member =~ m/^((?:unsigned )?int) (\S+);\s*\/\*\s*call-by-reference\s*\*\//) {
                    my $type_name = "$1 *";
1198 1199 1200 1201
                    my $arg_name = $2;

                    push(@args_list, "$type_name $arg_name");
                    push(@setters_list, "args.$arg_name = *$arg_name;");
1202 1203
                } elsif ($args_member =~ m/^((?:unsigned )?int) (\S+);/) {
                    my $type_name = $1;
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
                    my $arg_name = $2;

                    push(@args_list, "$type_name $arg_name");
                    push(@setters_list, "args.$arg_name = $arg_name;");
                } elsif ($args_member =~ m/^(unsigned )?hyper (\S+);/) {
                    my $type_name = $1;
                    my $arg_name = $2;

                    if (hyper_to_long($call->{ProcName}, "arg", $arg_name)) {
                        $type_name .= "long";
                    } else {
                        $type_name .= "long long";
1216 1217 1218 1219
                    }

                    push(@args_list, "$type_name $arg_name");
                    push(@setters_list, "args.$arg_name = $arg_name;");
1220
                } elsif ($args_member =~ m/^(\/)?\*/) {
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
                    # ignore comments
                } else {
                    die "unhandled type for argument value: $args_member";
                }

                if ($is_first_arg and $priv_src eq "conn") {
                    unshift(@args_list, "virConnectPtr conn");
                }

                $is_first_arg = 0;
            }
        }

1234 1235 1236 1237
        if (! @args_list) {
            push(@args_list, "virConnectPtr conn");
        }

1238 1239
        # fix priv_name for the NumOf* functions
        if ($priv_name eq "privateData" and
1240 1241 1242
            !($call->{ProcName} =~ m/(Domains|DomainSnapshot)/) and
            ($call->{ProcName} =~ m/NumOf(Defined|Domain)*(\S+)s/ or
             $call->{ProcName} =~ m/List(Defined|Domain)*(\S+)s/)) {
1243 1244 1245 1246 1247
            my $prefix = lc $2;
            $prefix =~ s/(pool|vol)$//;
            $priv_name = "${prefix}PrivateData";
        }

1248 1249
        # handle return values of the function
        my @ret_list = ();
1250
        my @ret_list2 = ();
1251 1252 1253
        my $call_ret = "&ret";
        my $single_ret_var = "int rv = -1";
        my $single_ret_type = "int";
1254 1255 1256 1257 1258
        my $single_ret_as_list = 0;
        my $single_ret_list_error_msg_type = "undefined";
        my $single_ret_list_name = "undefined";
        my $single_ret_list_max_var = "undefined";
        my $single_ret_list_max_define = "undefined";
1259
        my $single_ret_cleanup = 0;
1260 1261
        my $multi_ret = 0;

1262
        if ($rettype ne "void" and
1263 1264 1265 1266
            scalar(@{$call->{ret_members}}) > 1) {
            $multi_ret = 1;
        }

1267
        if ($rettype eq "void") {
1268
            $call_ret = "NULL";
1269
        } else {
1270
            push(@vars_list, "$rettype ret");
1271 1272

            foreach my $ret_member (@{$call->{ret_members}}) {
1273
                if ($multi_ret) {
1274
                    if ($ret_member =~ m/^(unsigned )?(char|short|int|hyper) (\S+)\[\S+\];/) {
1275 1276 1277 1278
                        if ($2 eq "hyper" and hyper_to_long($call->{ProcName}, "ret", $3)) {
                            die "legacy [u]long hyper arrays aren't supported";
                        }

1279
                        push(@ret_list, "memcpy(result->$3, ret.$3, sizeof(result->$3));");
1280 1281
                    } elsif ($ret_member =~ m/<\S+>;/ or $ret_member =~ m/\[\S+\];/) {
                        # just make all other array types fail
1282 1283
                        die "unhandled type for multi-return-value for " .
                            "procedure $call->{name}: $ret_member";
1284
                    } elsif ($ret_member =~ m/^(unsigned )?(char|short|int|hyper) (\S+);/) {
1285 1286 1287 1288 1289 1290 1291
                        if ($2 eq "hyper" and hyper_to_long($call->{ProcName}, "ret", $3)) {
                            my $sign = ""; $sign = "U" if ($1);

                            push(@ret_list, "HYPER_TO_${sign}LONG(result->$3, ret.$3);");
                        } else {
                            push(@ret_list, "result->$3 = ret.$3;");
                        }
1292
                    } else {
1293 1294
                        die "unhandled type for multi-return-value for " .
                            "procedure $call->{name}: $ret_member";
1295
                    }
1296 1297 1298 1299 1300
                } elsif ($ret_member =~ m/^remote_nonnull_string (\S+)<(\S+)>;\s*\/\*\s*insert@(\d+)\s*\*\//) {
                    splice(@args_list, int($3), 0, ("char **const $1"));
                    push(@ret_list, "rv = ret.$1.$1_len;");
                    $single_ret_var = "int rv = -1";
                    $single_ret_type = "int";
1301 1302 1303 1304
                    $single_ret_as_list = 1;
                    $single_ret_list_name = $1;
                    $single_ret_list_max_var = "max$1";
                    $single_ret_list_max_define = $2;
1305 1306 1307
                } elsif ($ret_member =~ m/^remote_nonnull_string (\S+)<\S+>;/) {
                    # error out on unannotated arrays
                    die "remote_nonnull_string array without insert@<offset> annotation: $ret_member";
1308
                } elsif ($ret_member =~ m/^remote_nonnull_string (\S+);/) {
1309 1310 1311
                    push(@ret_list, "rv = ret.$1;");
                    $single_ret_var = "char *rv = NULL";
                    $single_ret_type = "char *";
1312 1313 1314 1315 1316
                } elsif ($ret_member =~ m/^remote_string (\S+);/) {
                    push(@ret_list, "rv = ret.$1 ? *ret.$1 : NULL;");
                    push(@ret_list, "VIR_FREE(ret.$1);");
                    $single_ret_var = "char *rv = NULL";
                    $single_ret_type = "char *";
1317
                } elsif ($ret_member =~ m/^remote_nonnull_(domain|network|storage_pool|storage_vol|node_device|interface|secret|nwfilter|domain_snapshot) (\S+);/) {
1318 1319
                    my $name = $1;
                    my $arg_name = $2;
1320
                    my $type_name = name_to_TypeName($name);
1321 1322

                    if ($name eq "node_device") {
1323
                        $priv_name = "nodeDevicePrivateData";
1324 1325 1326 1327 1328 1329
                    } elsif ($name =~ m/^storage_/) {
                        $priv_name = "storagePrivateData";
                    } elsif (!($name =~ m/^domain/)) {
                        $priv_name = "${name}PrivateData";
                    }

1330 1331 1332 1333 1334 1335 1336 1337
                    if ($call->{ProcName} eq "DomainCreateWithFlags") {
                        # SPECIAL: virDomainCreateWithFlags updates the given
                        #          domain object instead of returning a new one
                        push(@ret_list, "dom->id = ret.dom.id;");
                        push(@ret_list, "xdr_free((xdrproc_t)xdr_$call->{ret}, (char *)&ret);");
                        push(@ret_list, "rv = 0;");
                        $single_ret_var = "int rv = -1";
                        $single_ret_type = "int";
1338
                    } else {
1339
                        if ($name eq "domain_snapshot") {
1340 1341 1342
                            my $dom = "$priv_src";
                            $dom =~ s/->conn//;
                            push(@ret_list, "rv = get_nonnull_$name($dom, ret.$arg_name);");
1343 1344 1345
                        } else {
                            push(@ret_list, "rv = get_nonnull_$name($priv_src, ret.$arg_name);");
                        }
1346

1347
                        push(@ret_list, "xdr_free((xdrproc_t)xdr_$rettype, (char *)&ret);");
1348 1349 1350
                        $single_ret_var = "vir${type_name}Ptr rv = NULL";
                        $single_ret_type = "vir${type_name}Ptr";
                    }
1351 1352 1353 1354 1355
                } elsif ($ret_member =~ m/^remote_typed_param (\S+)<(\S+)>;\s*\/\*\s*insert@(\d+)\s*\*\//) {
                    splice(@args_list, int($3), 0, ("virTypedParameterPtr $1"));
                    push(@ret_list2, "if (remoteDeserializeTypedParameters(ret.$1.$1_val,\n" .
                                     "                                         ret.$1.$1_len,\n" .
                                     "                                         $2,\n" .
1356
                                     "                                         &$1,\n" .
1357 1358 1359 1360 1361 1362
                                     "                                         n$1) < 0)\n" .
                                     "        goto cleanup;\n");
                    $single_ret_cleanup = 1;
                } elsif ($ret_member =~ m/^remote_typed_param (\S+)<\S+>;/) {
                    # error out on unannotated arrays
                    die "remote_typed_param array without insert@<offset> annotation: $ret_member";
1363
                } elsif ($ret_member =~ m/^int (\S+);/) {
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
                    my $arg_name = $1;

                    if ($call->{ProcName} =~ m/GetAutostart$/) {
                        push(@args_list, "int *$arg_name");
                        push(@ret_list, "if ($arg_name) *$arg_name = ret.$arg_name;");
                        push(@ret_list, "rv = 0;");
                    } else {
                        push(@ret_list, "rv = ret.$arg_name;");
                    }

1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
                    $single_ret_var = "int rv = -1";
                    $single_ret_type = "int";
                } elsif ($ret_member =~ m/^(unsigned )?hyper (\S+);\s*\/\*\s*insert@(\d+)\s*\*\//) {
                    my $type_name = $1;
                    my $sign = ""; $sign = "U" if ($1);
                    my $ret_name = $2;
                    my $insert = $3;

                    if (hyper_to_long($call->{ProcName}, "ret", $ret_name)) {
                        $type_name .= "long";
                        push(@ret_list, "if ($ret_name) HYPER_TO_${sign}LONG(*$ret_name, ret.$ret_name);");
                    } else {
                        $type_name .= "long long";
                        push(@ret_list, "if ($ret_name) *$ret_name = ret.$ret_name;");
                    }

                    splice(@args_list, int($insert), 0, ("$type_name *$ret_name"));
                    push(@ret_list, "rv = 0;");
1392 1393
                    $single_ret_var = "int rv = -1";
                    $single_ret_type = "int";
1394
                } elsif ($ret_member =~ m/^unsigned hyper (\S+);/) {
1395
                    my $ret_name = $1;
1396

1397
                    if ($call->{ProcName} =~ m/Get(Lib)?Version/) {
1398 1399
                        push(@args_list, "unsigned long *$ret_name");
                        push(@ret_list, "if ($ret_name) HYPER_TO_ULONG(*$ret_name, ret.$ret_name);");
1400 1401 1402
                        push(@ret_list, "rv = 0;");
                        $single_ret_var = "int rv = -1";
                        $single_ret_type = "int";
1403 1404
                    } elsif (hyper_to_long($call->{ProcName}, "ret", $ret_name)) {
                        push(@ret_list, "HYPER_TO_ULONG(rv, ret.$ret_name);");
1405 1406
                        $single_ret_var = "unsigned long rv = 0";
                        $single_ret_type = "unsigned long";
1407 1408 1409 1410
                    } else {
                        push(@ret_list, "rv = ret.$ret_name;");
                        $single_ret_var = "unsigned long long rv = 0";
                        $single_ret_type = "unsigned long long";
1411
                    }
1412 1413
                } elsif ($ret_member =~ m/^(\/)?\*/) {
                    # ignore comments
1414
                } else {
1415 1416
                    die "unhandled type for return value for procedure " .
                        "$call->{name}: $ret_member";
1417 1418 1419 1420
                }
            }
        }

1421 1422
        # select struct type for multi-return-value functions
        if ($multi_ret) {
1423 1424
            if (!(defined $call->{ret_offset})) {
                die "multi-return-value without insert@<offset> annotation: $call->{ret}";
1425 1426
            }

1427 1428
            my $struct_name = $call->{ProcName};
            $struct_name =~ s/Get//;
1429

1430
            splice(@args_list, $call->{ret_offset}, 0, ("vir${struct_name}Ptr result"));
1431 1432
        }

1433 1434 1435 1436
        if ($call->{streamflag} ne "none") {
            splice(@args_list, $call->{streamoffset}, 0, ("virStreamPtr st"));
        }

1437 1438
        # print function
        print "\n";
1439
        print "static $single_ret_type\n";
1440 1441 1442 1443 1444 1445 1446 1447 1448
        if ($structprefix eq "remote") {
            print "$structprefix$call->{ProcName}(";
        } else {
            my $proc = $call->{ProcName};
            my $extra = $structprefix;
            $extra =~ s/^(\w)/uc $1/e;
            $proc =~ s/^(Domain)(.*)/$1 . $extra . $2/e;
            print "remote$proc(";
        }
1449 1450 1451 1452 1453

        print join(", ", @args_list);

        print ")\n";
        print "{\n";
1454
        print "    $single_ret_var;\n";
1455 1456 1457 1458 1459 1460
        print "    struct private_data *priv = $priv_src->$priv_name;\n";

        foreach my $var (@vars_list) {
            print "    $var;\n";
        }

1461 1462 1463 1464
        if ($single_ret_as_list) {
            print "    int i;\n";
        }

1465
        if ($call->{streamflag} ne "none") {
1466
            print "    virNetClientStreamPtr netst = NULL;\n";
1467 1468
        }

1469 1470 1471
        print "\n";
        print "    remoteDriverLock(priv);\n";

1472 1473
        if ($call->{streamflag} ne "none") {
            print "\n";
1474
            print "    if (!(netst = virNetClientStreamNew(priv->remoteProgram, $call->{constname}, priv->counter)))\n";
1475
            print "        goto done;\n";
1476
            print "\n";
1477
            print "    if (virNetClientAddStream(priv->client, netst) < 0) {\n";
1478
            print "        virObjectUnref(netst);\n";
1479
            print "        goto done;\n";
1480 1481
            print "    }";
            print "\n";
1482
            print "    st->driver = &remoteStreamDrv;\n";
1483
            print "    st->privateData = netst;\n";
1484 1485
        }

1486 1487 1488 1489 1490 1491 1492 1493 1494
        if ($call->{ProcName} eq "SupportsFeature") {
            # SPECIAL: VIR_DRV_FEATURE_REMOTE feature is handled directly
            print "\n";
            print "    if (feature == VIR_DRV_FEATURE_REMOTE) {\n";
            print "        rv = 1;\n";
            print "        goto done;\n";
            print "    }\n";
        }

1495 1496 1497
        foreach my $args_check (@args_check_list) {
            print "\n";
            print "    if ($args_check->{arg} > $args_check->{limit}) {\n";
1498 1499 1500
            print "        virReportError(VIR_ERR_RPC,\n";
            print "                       _(\"%s length greater than maximum: %d > %d\"),\n";
            print "                       $args_check->{name}, (int)$args_check->{arg}, $args_check->{limit});\n";
1501 1502 1503 1504
            print "        goto done;\n";
            print "    }\n";
        }

1505 1506 1507
        if ($single_ret_as_list) {
            print "\n";
            print "    if ($single_ret_list_max_var > $single_ret_list_max_define) {\n";
1508 1509 1510
            print "        virReportError(VIR_ERR_RPC,\n";
            print "                       _(\"too many remote ${single_ret_list_error_msg_type}s: %d > %d\"),\n";
            print "                       $single_ret_list_max_var, $single_ret_list_max_define);\n";
1511 1512 1513 1514
            print "        goto done;\n";
            print "    }\n";
        }

1515
        if (@setters_list) {
1516
            print "\n";
1517 1518 1519 1520 1521 1522 1523 1524 1525
            print "    ";
        }

        print join("\n    ", @setters_list);

        if (@setters_list) {
            print "\n";
        }

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
        if (@setters_list2) {
            print "\n";
            print "    ";
        }

        print join("\n    ", @setters_list2);

        if (@setters_list2) {
            print "\n";
        }

1537
        if ($rettype ne "void") {
1538
            print "\n";
1539
            print "    memset(&ret, 0, sizeof(ret));\n";
1540 1541
        }

1542 1543 1544 1545
        my $callflags = "0";
        if ($structprefix eq "qemu") {
            $callflags = "REMOTE_CALL_QEMU";
        }
1546 1547 1548
        if ($structprefix eq "lxc") {
            $callflags = "REMOTE_CALL_LXC";
        }
1549

1550
        print "\n";
1551
        print "    if (call($priv_src, priv, $callflags, $call->{constname},\n";
1552 1553
        print "             (xdrproc_t)xdr_$argtype, (char *)$call_args,\n";
        print "             (xdrproc_t)xdr_$rettype, (char *)$call_ret) == -1) {\n";
1554 1555

        if ($call->{streamflag} ne "none") {
1556
            print "        virNetClientRemoveStream(priv->client, netst);\n";
1557
            print "        virObjectUnref(netst);\n";
1558 1559
            print "        st->driver = NULL;\n";
            print "        st->privateData = NULL;\n";
1560 1561
        }

1562
        print "        goto done;\n";
1563
        print "    }\n";
1564
        print "\n";
1565

1566 1567
        if ($single_ret_as_list) {
            print "    if (ret.$single_ret_list_name.${single_ret_list_name}_len > $single_ret_list_max_var) {\n";
1568 1569 1570
            print "        virReportError(VIR_ERR_RPC,\n";
            print "                       _(\"too many remote ${single_ret_list_error_msg_type}s: %d > %d\"),\n";
            print "                       ret.$single_ret_list_name.${single_ret_list_name}_len, $single_ret_list_max_var);\n";
1571 1572 1573 1574 1575
            print "        goto cleanup;\n";
            print "    }\n";
            print "\n";
            print "    /* This call is caller-frees (although that isn't clear from\n";
            print "     * the documentation).  However xdr_free will free up both the\n";
1576
            print "     * names and the list of pointers, so we have to VIR_STRDUP the\n";
1577 1578
            print "     * names here. */\n";
            print "    for (i = 0; i < ret.$single_ret_list_name.${single_ret_list_name}_len; ++i) {\n";
1579 1580
            print "        if (VIR_STRDUP(${single_ret_list_name}[i],\n";
            print "                       ret.$single_ret_list_name.${single_ret_list_name}_val[i]) < 0) {\n";
1581 1582 1583 1584 1585 1586 1587 1588 1589
            print "            for (--i; i >= 0; --i)\n";
            print "                VIR_FREE(${single_ret_list_name}[i]);\n";
            print "\n";
            print "            goto cleanup;\n";
            print "        }\n";
            print "    }\n";
            print "\n";
        }

1590 1591 1592 1593 1594 1595
        if (@ret_list2) {
            print "    ";
            print join("\n    ", @ret_list2);
            print "\n";
        }

1596 1597 1598 1599
        if (@ret_list) {
            print "    ";
            print join("\n    ", @ret_list);
            print "\n";
1600 1601
        }

1602
        if ($call->{ProcName} eq "DomainDestroy" ||
1603 1604
            $call->{ProcName} eq "DomainSave" ||
            $call->{ProcName} eq "DomainManagedSave") {
1605
            # SPECIAL: virDomain{Destroy|Save|ManagedSave} need to reset
1606
            # the domain id explicitly on success
1607 1608 1609
            print "    dom->id = -1;\n";
        }

1610
        if ($multi_ret or !@ret_list) {
1611 1612 1613
            print "    rv = 0;\n";
        }

1614
        if ($single_ret_as_list or $single_ret_cleanup) {
1615 1616
            print "\n";
            print "cleanup:\n";
1617
            print "    xdr_free((xdrproc_t)xdr_$call->{ret}, (char *)&ret);\n";
1618 1619
        }

1620 1621
        print "\n";
        print "done:\n";
1622 1623 1624

        print join("\n", @free_list);

1625 1626 1627 1628 1629
        print "    remoteDriverUnlock(priv);\n";
        print "    return rv;\n";
        print "}\n";
    }
}