remote_generator.pl 27.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/perl -w
#
# 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:
#
# remote_generator.pl -c -t remote ../src/remote/remote_protocol.x
# remote_generator.pl -t qemu ../src/remote/qemu_protocol.x
#
# By Richard Jones <rjones@redhat.com>
16
# Extended by Matthias Bolte <matthias.bolte@googlemail.com>
17 18 19 20 21 22

use strict;

use Getopt::Std;

# Command line options.
23 24
our ($opt_p, $opt_t, $opt_a, $opt_r, $opt_d, $opt_c, $opt_b);
getopts ('ptardcb');
25 26 27 28 29 30 31 32 33 34

my $structprefix = $ARGV[0];
my $procprefix = uc $structprefix;
shift;

# Convert name_of_call to NameOfCall.
sub name_to_ProcName {
    my $name = shift;
    my @elems = split /_/, $name;
    @elems = map ucfirst, @elems;
35
    @elems = map { $_ =~ s/Nwfilter/NWFilter/; $_ =~ s/Xml/XML/;
36 37
                   $_ =~ s/Uri/URI/; $_ =~ s/Uuid/UUID/; $_ =~ s/Id/ID/;
                   $_ =~ s/Mac/MAC/; $_ } @elems;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    join "", @elems
}

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

# only generate a close method if -c was passed
if ($opt_c) {
    # REMOTE_PROC_CLOSE has no args or ret.
    $calls{close} = {
        name => "close",
        ProcName => "Close",
        UC_NAME => "CLOSE",
        args => "void",
        ret => "void",
    };
}

57
my $collect_args_members = 0;
58
my $collect_ret_members = 0;
59 60
my $last_name;

61
while (<>) {
62 63 64 65 66 67
    if ($collect_args_members) {
        if (/^};/) {
            $collect_args_members = 0;
        } elsif ($_ =~ m/^\s*(.*\S)\s*$/) {
            push(@{$calls{$name}->{args_members}}, $1);
        }
68 69 70 71 72 73
    } elsif ($collect_ret_members) {
        if (/^};/) {
            $collect_ret_members = 0;
        } elsif ($_ =~ m/^\s*(.*\S)\s*$/) {
            push(@{$calls{$name}->{ret_members}}, $1);
        }
74
    } elsif (/^struct ${structprefix}_(.*)_args/) {
75 76 77 78 79 80 81 82 83 84 85
        $name = $1;
        $ProcName = name_to_ProcName ($name);

        die "duplicate definition of ${structprefix}_${name}_args"
            if exists $calls{$name};

        $calls{$name} = {
            name => $name,
            ProcName => $ProcName,
            UC_NAME => uc $name,
            args => "${structprefix}_${name}_args",
86 87
            args_members => [],
            ret => "void"
88 89
        };

90
        $collect_args_members = 1;
91
        $collect_ret_members = 0;
92
        $last_name = $name;
93 94 95 96 97 98 99 100 101 102 103 104
    } elsif (/^struct ${structprefix}_(.*)_ret/) {
        $name = $1;
        $ProcName = name_to_ProcName ($name);

        if (exists $calls{$name}) {
            $calls{$name}->{ret} = "${structprefix}_${name}_ret";
        } else {
            $calls{$name} = {
                name => $name,
                ProcName => $ProcName,
                UC_NAME => uc $name,
                args => "void",
105 106
                ret => "${structprefix}_${name}_ret",
                ret_members => []
107 108
            }
        }
109 110

        $collect_args_members = 0;
111 112
        $collect_ret_members = 1;
        $last_name = $name;
113 114 115 116 117 118 119 120 121
    } elsif (/^struct ${structprefix}_(.*)_msg/) {
        $name = $1;
        $ProcName = name_to_ProcName ($name);

        $calls{$name} = {
            name => $name,
            ProcName => $ProcName,
            UC_NAME => uc $name,
            msg => "${structprefix}_${name}_msg"
122 123 124
        };

        $collect_args_members = 0;
125
        $collect_ret_members = 0;
126 127 128 129 130 131
    } elsif (/^\s*${procprefix}_PROC_(.*?)\s+=\s+(\d+),?$/) {
        $name = lc $1;
        $id = $2;
        $ProcName = name_to_ProcName ($name);

        $calls[$id] = $calls{$name};
132 133

        $collect_args_members = 0;
134
        $collect_ret_members = 0;
135 136
    } else {
        $collect_args_members = 0;
137
        $collect_ret_members = 0;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    }
}

#----------------------------------------------------------------------
# Output

print <<__EOF__;
/* Automatically generated by remote_generator.pl.
 * Do not edit this file.  Any changes you make will be lost.
 */

__EOF__

# Debugging.
if ($opt_d) {
    my @keys = sort (keys %calls);
    foreach (@keys) {
        print "$_:\n";
        print "        name $calls{$_}->{name} ($calls{$_}->{ProcName})\n";
        print "        $calls{$_}->{args} -> $calls{$_}->{ret}\n";
    }
}

# Prototypes for dispatch functions ("remote_dispatch_prototypes.h").
elsif ($opt_p) {
    my @keys = sort (keys %calls);
    foreach (@keys) {
        # Skip things which are REMOTE_MESSAGE
        next if $calls{$_}->{msg};

        print "static int ${structprefix}Dispatch$calls{$_}->{ProcName}(\n";
        print "    struct qemud_server *server,\n";
        print "    struct qemud_client *client,\n";
        print "    virConnectPtr conn,\n";
        print "    remote_message_header *hdr,\n";
        print "    remote_error *rerr,\n";
        print "    $calls{$_}->{args} *args,\n";
        print "    $calls{$_}->{ret} *ret);\n";
    }
}

# Union of all arg types
# ("remote_dispatch_args.h").
elsif ($opt_a) {
    for ($id = 0 ; $id <= $#calls ; $id++) {
        if (defined $calls[$id] &&
            !$calls[$id]->{msg} &&
            $calls[$id]->{args} ne "void") {
            print "    $calls[$id]->{args} val_$calls[$id]->{args};\n";
        }
    }
}

# Union of all arg types
# ("remote_dispatch_ret.h").
elsif ($opt_r) {
    for ($id = 0 ; $id <= $#calls ; $id++) {
        if (defined $calls[$id] &&
            !$calls[$id]->{msg} &&
            $calls[$id]->{ret} ne "void") {
            print "    $calls[$id]->{ret} val_$calls[$id]->{ret};\n";
        }
    }
}

# Inside the switch statement, prepare the 'fn', 'args_filter', etc
# ("remote_dispatch_table.h").
elsif ($opt_t) {
    for ($id = 0 ; $id <= $#calls ; $id++) {
        if (defined $calls[$id] && !$calls[$id]->{msg}) {
            print "{   /* $calls[$id]->{ProcName} => $id */\n";
            print "    .fn = (dispatch_fn) ${structprefix}Dispatch$calls[$id]->{ProcName},\n";
            if ($calls[$id]->{args} ne "void") {
                print "    .args_filter = (xdrproc_t) xdr_$calls[$id]->{args},\n";
            } else {
                print "    .args_filter = (xdrproc_t) xdr_void,\n";
            }
            if ($calls[$id]->{ret} ne "void") {
                print "    .ret_filter = (xdrproc_t) xdr_$calls[$id]->{ret},\n";
            } else {
                print "    .ret_filter = (xdrproc_t) xdr_void,\n";
            }
            print "},\n";
        } else {
            if ($calls[$id]->{msg}) {
                print "{   /* Async event $calls[$id]->{ProcName} => $id */\n";
            } else {
                print "{   /* (unused) => $id */\n";
            }
            print "    .fn = NULL,\n";
            print "    .args_filter = (xdrproc_t) xdr_void,\n";
            print "    .ret_filter = (xdrproc_t) xdr_void,\n";
            print "},\n";
        }
    }
}
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

# Bodies for dispatch functions ("remote_dispatch_bodies.c").
elsif ($opt_b) {
    # list of functions that currently are not generatable
    my @ungeneratable;

    if ($structprefix eq "remote") {
        @ungeneratable = ("Close",
                          "DomainEventsDeregisterAny",
                          "DomainEventsRegisterAny",
                          "DomainMigratePerform",
                          "DomainMigratePrepareTunnel",
                          "DomainOpenConsole",
                          "DomainPinVcpu",
                          "DomainSetSchedulerParameters",
                          "DomainSetMemoryParameters",
                          "DomainSetBlkioParameters",
                          "Open",
                          "StorageVolUpload",
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 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
                          "StorageVolDownload",

                          "AuthList",
                          "AuthSaslInit",
                          "AuthSaslStart",
                          "AuthSaslStep",
                          "AuthPolkit",

                          "CpuBaseline",
                          "CpuCompare",
                          "DomainBlockPeek",
                          "DomainBlockStats",
                          "DomainCreateWithFlags",
                          "DomainEventsDeregister",
                          "DomainEventsRegister",
                          "DomainGetBlkioParameters",
                          "DomainGetBlockInfo",
                          "DomainGetInfo",
                          "DomainGetJobInfo",
                          "DomainGetMemoryParameters",
                          "DomainGetSchedulerParameters",
                          "DomainGetSchedulerType",
                          "DomainGetSecurityLabel",
                          "DomainGetVcpus",
                          "DomainInterfaceStats",
                          "DomainMemoryPeek",
                          "DomainMemoryStats",
                          "DomainMigratePrepare",
                          "DomainMigratePrepare2",
                          "DomainSnapshotListNames",
                          "FindStoragePoolSources",
                          "GetMaxVcpus",
                          "GetType",
                          "ListDefinedDomains",
                          "ListDefinedInterfaces",
                          "ListDefinedNetworks",
                          "ListDefinedStoragePools",
                          "ListDomains",
                          "ListInterfaces",
                          "ListNetworks",
                          "ListNWFilters",
                          "ListSecrets",
                          "ListStoragePools",
                          "NodeDeviceGetParent",
                          "NodeDeviceListCaps",
                          "NodeGetCellsFreeMemory",
                          "NodeGetInfo",
                          "NodeGetSecurityModel",
                          "NodeListDevices",
                          "NodeNumOfDevices",
                          "SecretGetValue",
                          "StoragePoolGetInfo",
                          "StoragePoolListVolumes",
306
                          "StorageVolGetInfo");
307 308 309 310 311 312 313 314 315 316 317
    } elsif ($structprefix eq "qemu") {
        @ungeneratable = ("MonitorCommand");
    }

    my %ug = map { $_ => 1 } @ungeneratable;
    my @keys = sort (keys %calls);

    foreach (@keys) {
        # skip things which are REMOTE_MESSAGE
        next if $calls{$_}->{msg};

318 319 320
        if (exists($ug{$calls{$_}->{ProcName}})) {
            print "\n/* ${structprefix}Dispatch$calls{$_}->{ProcName} has " .
                  "to be implemented manually */\n";
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
            next;
        }

        print "\n";
        print "static int\n";
        print "${structprefix}Dispatch$calls{$_}->{ProcName}(\n";
        print "    struct qemud_server *server ATTRIBUTE_UNUSED,\n";
        print "    struct qemud_client *client ATTRIBUTE_UNUSED,\n";
        print "    virConnectPtr conn,\n";
        print "    remote_message_header *hdr ATTRIBUTE_UNUSED,\n";
        print "    remote_error *rerr,\n";
        print "    $calls{$_}->{args} *args";

        if ($calls{$_}->{args} eq "void") {
            print " ATTRIBUTE_UNUSED"
        }

        print ",\n";
        print "    $calls{$_}->{ret} *ret";

        if ($calls{$_}->{ret} eq "void") {
            print " ATTRIBUTE_UNUSED"
        }

        print ")\n";
        print "{\n";
        print "    int rv = -1;\n";

        my $has_node_device = 0;
        my @vars_list = ();
        my @getters_list = ();
        my @args_list = ();
353
        my @ret_list = ();
354 355 356 357
        my @free_list = ();

        if ($calls{$_}->{args} ne "void") {
            # node device is special, as it's identified by name
358 359 360
            if ($calls{$_}->{args} =~ m/^remote_node_device_/ and
                !($calls{$_}->{args} =~ m/^remote_node_device_lookup_by_name_/) and
                !($calls{$_}->{args} =~ m/^remote_node_device_create_xml_/)) {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
                $has_node_device = 1;
                push(@vars_list, "virNodeDevicePtr dev = NULL");
                push(@getters_list,
                     "    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))\n" .
                     "        goto cleanup;\n");
                push(@args_list, "dev");
                push(@free_list,
                     "    if (dev)\n" .
                     "        virNodeDeviceFree(dev);");
            }

            foreach my $args_member (@{$calls{$_}->{args_members}}) {
                if ($args_member =~ m/^remote_nonnull_string name;/ and $has_node_device) {
                    # ignore the name arg for node devices
                    next
376 377
                } elsif ($args_member =~ m/^remote_nonnull_domain (\S+);/) {
                    push(@vars_list, "virDomainPtr $1 = NULL");
378
                    push(@getters_list,
379
                         "    if (!($1 = get_nonnull_domain(conn, args->$1)))\n" .
380
                         "        goto cleanup;\n");
381
                    push(@args_list, "$1");
382
                    push(@free_list,
383 384 385 386
                         "    if ($1)\n" .
                         "        virDomainFree($1);");
                } elsif ($args_member =~ m/^remote_nonnull_network (\S+);/) {
                    push(@vars_list, "virNetworkPtr $1 = NULL");
387
                    push(@getters_list,
388
                         "    if (!($1 = get_nonnull_network(conn, args->$1)))\n" .
389
                         "        goto cleanup;\n");
390
                    push(@args_list, "$1");
391
                    push(@free_list,
392 393 394 395
                         "    if ($1)\n" .
                         "        virNetworkFree($1);");
                } elsif ($args_member =~ m/^remote_nonnull_storage_pool (\S+);/) {
                    push(@vars_list, "virStoragePoolPtr $1 = NULL");
396
                    push(@getters_list,
397
                         "    if (!($1 = get_nonnull_storage_pool(conn, args->$1)))\n" .
398
                         "        goto cleanup;\n");
399
                    push(@args_list, "$1");
400
                    push(@free_list,
401 402 403 404
                         "    if ($1)\n" .
                         "        virStoragePoolFree($1);");
                } elsif ($args_member =~ m/^remote_nonnull_storage_vol (\S+);/) {
                    push(@vars_list, "virStorageVolPtr $1 = NULL");
405
                    push(@getters_list,
406
                         "    if (!($1 = get_nonnull_storage_vol(conn, args->$1)))\n" .
407
                         "        goto cleanup;\n");
408
                    push(@args_list, "$1");
409
                    push(@free_list,
410 411 412 413
                         "    if ($1)\n" .
                         "        virStorageVolFree($1);");
                } elsif ($args_member =~ m/^remote_nonnull_interface (\S+);/) {
                    push(@vars_list, "virInterfacePtr $1 = NULL");
414
                    push(@getters_list,
415
                         "    if (!($1 = get_nonnull_interface(conn, args->$1)))\n" .
416
                         "        goto cleanup;\n");
417
                    push(@args_list, "$1");
418
                    push(@free_list,
419 420 421 422
                         "    if ($1)\n" .
                         "        virInterfaceFree($1);");
                } elsif ($args_member =~ m/^remote_nonnull_secret (\S+);/) {
                    push(@vars_list, "virSecretPtr $1 = NULL");
423
                    push(@getters_list,
424
                         "    if (!($1 = get_nonnull_secret(conn, args->$1)))\n" .
425
                         "        goto cleanup;\n");
426
                    push(@args_list, "$1");
427
                    push(@free_list,
428 429 430 431
                         "    if ($1)\n" .
                         "        virSecretFree($1);");
                } elsif ($args_member =~ m/^remote_nonnull_nwfilter (\S+);/) {
                    push(@vars_list, "virNWFilterPtr $1 = NULL");
432
                    push(@getters_list,
433
                         "    if (!($1 = get_nonnull_nwfilter(conn, args->$1)))\n" .
434
                         "        goto cleanup;\n");
435
                    push(@args_list, "$1");
436
                    push(@free_list,
437 438
                         "    if ($1)\n" .
                         "        virNWFilterFree($1);");
439 440 441 442
                } elsif ($args_member =~ m/^remote_nonnull_domain_snapshot /) {
                    push(@vars_list, "virDomainPtr dom = NULL");
                    push(@vars_list, "virDomainSnapshotPtr snapshot = NULL");
                    push(@getters_list,
443
                         "    if (!(dom = get_nonnull_domain(conn, args->snap.dom)))\n" .
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
                         "        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);");
                } elsif ($args_member =~ m/(\S+)<\S+>;/) {
                    if (! @args_list) {
                        push(@args_list, "conn");
                    }

                    if ($calls{$_}->{ProcName} eq "SecretSetValue") {
                        push(@args_list, "(const unsigned char *)args->$1.$1_val");
                    } else {
                        push(@args_list, "args->$1.$1_val");
                    }

                    push(@args_list, "args->$1.$1_len");
466
                } elsif ($args_member =~ m/(\S+) (\S+);/) {
467 468 469 470
                    if (! @args_list) {
                        push(@args_list, "conn");
                    }

471 472 473 474 475
                    if ($1 eq "remote_uuid") {
                        push(@args_list, "(unsigned char *) args->$2");
                    } else {
                        push(@args_list, "args->$2");
                    }
476 477 478 479
                }
            }
        }

480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
        my $single_ret_var = "undefined";
        my $single_ret_by_ref = 0;
        my $single_ret_check = " == undefined";

        if ($calls{$_}->{ret} ne "void") {
            foreach my $ret_member (@{$calls{$_}->{ret_members}}) {
                if ($ret_member =~ m/(\S+)<\S+>;/) {
                    push(@ret_list, "ret->$1.$1_val");
                    push(@ret_list, "ret->$1.$1_len");
                } elsif ($ret_member =~ m/remote_nonnull_string (\S+);/) {
                    push(@vars_list, "char *$1");
                    push(@ret_list, "ret->$1 = $1;");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
                } elsif ($ret_member =~ m/remote_nonnull_domain (\S+);/) {
                    push(@vars_list, "virDomainPtr $1 = NULL");
                    push(@ret_list, "make_nonnull_domain(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virDomainFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_network (\S+);/) {
                    push(@vars_list, "virNetworkPtr $1 = NULL");
                    push(@ret_list, "make_nonnull_network(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virNetworkFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_storage_pool (\S+);/) {
                    push(@vars_list, "virStoragePoolPtr $1 = NULL");
                    push(@ret_list, "make_nonnull_storage_pool(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virStoragePoolFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_storage_vol (\S+);/) {
                    push(@vars_list, "virStorageVolPtr $1 = NULL");
                    push(@ret_list, "make_nonnull_storage_vol(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virStorageVolFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_interface (\S+);/) {
                    push(@vars_list, "virInterfacePtr $1 = NULL");
                    push(@ret_list, "make_nonnull_interface(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virInterfaceFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_node_device (\S+);/) {
                    push(@vars_list, "virNodeDevicePtr $1 = NULL");
                    push(@ret_list, "make_nonnull_node_device(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virNodeDeviceFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_secret (\S+);/) {
                    push(@vars_list, "virSecretPtr $1 = NULL");
                    push(@ret_list, "make_nonnull_secret(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virSecretFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_nwfilter (\S+);/) {
                    push(@vars_list, "virNWFilterPtr $1 = NULL");
                    push(@ret_list, "make_nonnull_nwfilter(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virNWFilterFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
                } elsif ($ret_member =~ m/remote_nonnull_domain_snapshot (\S+);/) {
                    push(@vars_list, "virDomainSnapshotPtr $1 = NULL");
                    push(@ret_list, "make_nonnull_domain_snapshot(&ret->$1, $1);");
                    push(@free_list,
                         "    if ($1)\n" .
                         "        virDomainSnapshotFree($1);");
                    $single_ret_var = $1;
                    $single_ret_by_ref = 0;
                    $single_ret_check = " == NULL";
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
                } elsif ($ret_member =~ m/int (\S+);/) {
                    push(@vars_list, "int $1");
                    push(@ret_list, "ret->$1 = $1;");
                    $single_ret_var = $1;

                    if ($calls{$_}->{ProcName} eq "DomainGetAutostart" or
                        $calls{$_}->{ProcName} eq "NetworkGetAutostart" or
                        $calls{$_}->{ProcName} eq "StoragePoolGetAutostart") {
                        $single_ret_by_ref = 1;
                    } else {
                        $single_ret_by_ref = 0;
                        $single_ret_check = " < 0";
                    }
                } elsif ($ret_member =~ m/hyper (\S+);/) {
                    push(@vars_list, "unsigned long $1");
                    push(@ret_list, "ret->$1 = $1;");
                    $single_ret_var = $1;

594 595
                    if ($calls{$_}->{ProcName} eq "DomainGetMaxMemory" or
                        $calls{$_}->{ProcName} eq "NodeGetFreeMemory") {
596 597 598 599 600 601 602 603 604
                        $single_ret_by_ref = 0;
                        $single_ret_check = " == 0";
                    } else {
                        $single_ret_by_ref = 1;
                    }
                }
            }
        }

605 606 607 608 609 610 611 612 613 614 615 616 617
        foreach my $var (@vars_list) {
            print "    $var;\n";
        }

        print "\n";
        print "    if (!conn) {\n";
        print "        virNetError(VIR_ERR_INTERNAL_ERROR, \"%s\", _(\"connection not open\"));\n";
        print "        goto cleanup;\n";
        print "    }\n";
        print "\n";

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

618 619 620
        if (@getters_list) {
            print "\n";
        }
621 622 623 624 625 626 627

        if ($calls{$_}->{ret} eq "void") {
            print "    if (vir$calls{$_}->{ProcName}(";
            print join(', ', @args_list);
            print ") < 0)\n";
            print "        goto cleanup;\n";
            print "\n";
628 629 630 631 632 633
        } elsif (scalar(@{$calls{$_}->{ret_members}}) == 1) {
            my $prefix = "";
            my $proc_name = $calls{$_}->{ProcName};

            if (! @args_list) {
                push(@args_list, "conn");
634 635 636 637

                if ($calls{$_}->{ProcName} ne "NodeGetFreeMemory") {
                    $prefix = "Connect"
                }
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
            }

            if ($calls{$_}->{ProcName} eq "GetSysinfo" or
                $calls{$_}->{ProcName} eq "DomainXMLFromNative" or
                $calls{$_}->{ProcName} eq "DomainXMLToNative") {
                $prefix = "Connect"
            } elsif ($calls{$_}->{ProcName} eq "SupportsFeature") {
                $prefix = "Drv"
            } elsif ($calls{$_}->{ProcName} eq "DomainDumpXML") {
                $proc_name = "DomainGetXMLDesc"
            } elsif ($calls{$_}->{ProcName} eq "NetworkDumpXML") {
                $proc_name = "NetworkGetXMLDesc"
            } elsif ($calls{$_}->{ProcName} eq "StoragePoolDumpXML") {
                $proc_name = "StoragePoolGetXMLDesc"
            } elsif ($calls{$_}->{ProcName} eq "StorageVolDumpXML") {
                $proc_name = "StorageVolGetXMLDesc"
            } elsif ($calls{$_}->{ProcName} eq "NodeDeviceDumpXML") {
                $proc_name = "NodeDeviceGetXMLDesc"
            } elsif ($calls{$_}->{ProcName} eq "DomainSnapshotDumpXML") {
                $proc_name = "DomainSnapshotGetXMLDesc"
            } elsif ($calls{$_}->{ProcName} eq "DomainGetOsType") {
                $proc_name = "DomainGetOSType"
            }

            if ($single_ret_by_ref) {
                print "    if (vir$prefix$proc_name(";
                print join(', ', @args_list);
                print ", &$single_ret_var) < 0)\n";
            } 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";

            if (@ret_list) {
                print "    ";
            }

            print join("    \n", @ret_list);
            print "\n";
681 682 683 684 685 686 687 688 689 690
        }

        print "    rv = 0;\n";
        print "\n";
        print "cleanup:\n";
        print "    if (rv < 0)\n";
        print "        remoteDispatchError(rerr);\n";

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

691 692 693 694
        if (@free_list) {
            print "\n";
        }

695 696 697 698
        print "    return rv;\n";
        print "}\n";
    }
}