File: remote_generate_stubs.pl

package info (click to toggle)
libvirt 0.4.6-10%2Blenny2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 29,084 kB
  • ctags: 8,508
  • sloc: ansic: 75,483; xml: 11,901; sh: 10,518; python: 4,309; makefile: 863; perl: 356; awk: 48; sed: 16
file content (224 lines) | stat: -rwxr-xr-x 5,770 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
#!/usr/bin/perl -w
#
# This script parses remote_protocol.x and produces lots of boilerplate
# code for both ends of the remote connection.
#
# By Richard Jones <rjones@redhat.com>
#
# $Id: remote_generate_stubs.pl,v 1.3 2008/05/22 15:20:25 berrange Exp $

use strict;

use Getopt::Std;

# Command line options.
our ($opt_c, $opt_d, $opt_i, $opt_s, $opt_v, $opt_w);
getopts ('cdisvw');

# Convert name_of_call to NameOfCall.
sub name_to_ProcName {
    my $name = shift;
    my @elems = split /_/, $name;
    @elems = map ucfirst, @elems;
    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, %calls);
while (<>) {
    if (/^struct remote_(.*)_args/) {
	$name = $1;
	$ProcName = name_to_ProcName ($name);

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

	$calls{$name} = {
	    name => $name,
	    ProcName => $ProcName,
	    UC_NAME => uc $name,
	    args => "remote_${name}_args",
	    ret => "void",
	};

    } elsif (/^struct remote_(.*)_ret/) {
	$name = $1;
	$ProcName = name_to_ProcName ($name);

	if (exists $calls{$name}) {
	    $calls{$name}->{ret} = "remote_${name}_ret";
	} else {
	    $calls{$name} = {
		name => $name,
		ProcName => $ProcName,
		UC_NAME => uc $name,
		args => "void",
		ret => "remote_${name}_ret"
	    }
	}
    }
}

# REMOTE_PROC_CLOSE has no args or ret.
$calls{close} = {
    name => "close",
    ProcName => "Close",
    UC_NAME => "CLOSE",
    args => "void",
    ret => "void",
};

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

print <<__EOF__;
/* Automatically generated by remote_generate_stubs.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_i) {
    my @keys = sort (keys %calls);
    foreach (@keys) {
	print "static int remoteDispatch$calls{$_}->{ProcName} (struct qemud_server *server, struct qemud_client *client, remote_message_header *req, $calls{$_}->{args} *args, $calls{$_}->{ret} *ret);\n";
    }
}

# Local variables used inside remoteDispatchClientRequest
# ("remote_dispatch_localvars.h").
elsif ($opt_v) {
    my @values = values %calls;
    foreach (@values) {
	if ($_->{args} ne "void") {
	    print "$_->{args} lv_$_->{args};\n";
	}
	if ($_->{ret} ne "void") {
	    print "$_->{ret} lv_$_->{ret};\n";
	}
    }
}

# Inside the switch statement, prepare the 'fn', 'args_filter', etc
# ("remote_dispatch_proc_switch.h").
elsif ($opt_w) {
    my @keys = sort (keys %calls);
    foreach (@keys) {
	print "case REMOTE_PROC_$calls{$_}->{UC_NAME}:\n";
	print "        fn = (dispatch_fn) remoteDispatch$calls{$_}->{ProcName};\n";
	if ($calls{$_}->{args} ne "void") {
	    print "        args_filter = (xdrproc_t) xdr_$calls{$_}->{args};\n";
	    print "        args = (char *) &lv_$calls{$_}->{args};\n";
	    print "        memset (&lv_$calls{$_}->{args}, 0, sizeof lv_$calls{$_}->{args});\n"
	}
	if ($calls{$_}->{ret} ne "void") {
	    print "        ret_filter = (xdrproc_t) xdr_$calls{$_}->{ret};\n";
	    print "        ret = (char *) &lv_$calls{$_}->{ret};\n";
	    print "        memset (&lv_$calls{$_}->{ret}, 0, sizeof lv_$calls{$_}->{ret});\n"
	}
	print "        break;\n";
    }
}

# Generate client stubs - just used to generate the first
# version of the stubs in remote_internal.c.  They need
# hand-hacking afterwards.
elsif ($opt_c) {
    my @keys = sort (keys %calls);

    foreach (@keys) {
	my $args = $calls{$_}->{args};
	my $argsvoid = $args eq "void";
	my $ret = $calls{$_}->{ret};
	my $retvoid = $ret eq "void";

	print "static @@\n";
	print "remote$calls{$_}->{ProcName} (@@)\n";
	print "{\n";
	if (!$argsvoid) {
	    print "    $args args;\n";
	}
	if (!$retvoid) {
	    print "    $ret ret;\n";
	}
	print "    GET_PRIVATE (conn, @@);\n";
	print "\n";
	if (!$argsvoid) {
	    print "    @@\n";
	    print "\n";
	}
	if (!$retvoid) {
	    print "    memset (&ret, 0, sizeof ret);\n";
	}
	print "    if (call (conn, priv, 0, REMOTE_PROC_$calls{$_}->{UC_NAME},\n";
	print "              (xdrproc_t) xdr_$args, (char *) ";
	if ($argsvoid) {
	    print "NULL";
	} else {
	    print "&args";
	}
	print ",\n";
	print "              (xdrproc_t) xdr_$ret, (char *) ";
	if ($retvoid) {
	    print "NULL";
	} else {
	    print "&ret";
	}
	print ") == -1)\n";
	print "        return -1;\n";
	print "\n    @@\n";
	print "}\n\n";
    }
}

# Generate server stubs - just used to generate the first
# version of the stubs in remote.c.  They need hand-hacking
# afterwards.
elsif ($opt_s) {
    my @keys = sort (keys %calls);

    foreach (@keys) {
	my $args = $calls{$_}->{args};
	my $argsvoid = $args eq "void";
	my $ret = $calls{$_}->{ret};
	my $retvoid = $ret eq "void";

	print "static int\n";
	print "remoteDispatch$calls{$_}->{ProcName} (struct qemud_server *server,\n";
	print "            struct qemud_client *client,\n";
	print "            remote_message_header *req,\n";
	print "            remote_get_max_vcpus_args *args,\n";
	print "            remote_get_max_vcpus_ret *ret)\n";
	print "{\n";
	if (!$argsvoid || !$retvoid) {
	    print "    @@\n";
	}
	print "    CHECK_CONN;\n";
	print "\n";
	if (!$argsvoid) {
	    print "    @@\n";
	    print "\n";
	}
	print "    @@ = vir$calls{$_}->{ProcName} (@@);\n";
	print "    if (@@) return -1;\n";
	print "\n";
	if (!$retvoid) {
	    print "    @@\n";
	    print "\n";
	}
	print "    return 0;\n";
	print "}\n\n";
    }
}