1 #! /usr/bin/perl -w
2 #
3 # Build the server/trace.c and server/request.h files
4 # from the contents of server/protocol.def.
5 #
6 # Copyright (C) 1998 Alexandre Julliard
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #
22 use strict;
23
24 my %formats =
25 ( # size align format
26 "int" => [ 4, 4, "%d" ],
27 "short int" => [ 2, 2, "%d" ],
28 "char" => [ 1, 1, "%c" ],
29 "unsigned char" => [ 1, 1, "%02x" ],
30 "unsigned short"=> [ 2, 2, "%04x" ],
31 "unsigned int" => [ 4, 4, "%08x" ],
32 "data_size_t" => [ 4, 4, "%u" ],
33 "obj_handle_t" => [ 4, 4, "%04x" ],
34 "atom_t" => [ 4, 4, "%04x" ],
35 "user_handle_t" => [ 4, 4, "%08x" ],
36 "process_id_t" => [ 4, 4, "%04x" ],
37 "thread_id_t" => [ 4, 4, "%04x" ],
38 "client_ptr_t" => [ 8, 8, "&dump_uint64" ],
39 "mod_handle_t" => [ 8, 8, "&dump_uint64" ],
40 "lparam_t" => [ 8, 8, "&dump_uint64" ],
41 "apc_param_t" => [ 8, 8, "&dump_uint64" ],
42 "file_pos_t" => [ 8, 8, "&dump_uint64" ],
43 "mem_size_t" => [ 8, 8, "&dump_uint64" ],
44 "affinity_t" => [ 8, 8, "&dump_uint64" ],
45 "timeout_t" => [ 8, 8, "&dump_timeout" ],
46 "rectangle_t" => [ 16, 4, "&dump_rectangle" ],
47 "char_info_t" => [ 4, 2, "&dump_char_info" ],
48 "apc_call_t" => [ 40, 8, "&dump_apc_call" ],
49 "apc_result_t" => [ 40, 8, "&dump_apc_result" ],
50 "async_data_t" => [ 40, 8, "&dump_async_data" ],
51 "luid_t" => [ 8, 4, "&dump_luid" ],
52 "ioctl_code_t" => [ 4, 4, "&dump_ioctl_code" ],
53 "cpu_type_t" => [ 4, 4, "&dump_cpu_type" ],
54 );
55
56 my @requests = ();
57 my %replies = ();
58 my @asserts = ();
59
60 my @trace_lines = ();
61
62 my $max_req_size = 64;
63
64 my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
65
66 ### Generate a dumping function
67
68 sub DO_DUMP_FUNC($$@)
69 {
70 my $name = shift;
71 my $req = shift;
72 my $prefix = " ";
73 push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
74 while ($#_ >= 0)
75 {
76 my $type = shift;
77 my $var = shift;
78 next if $var =~ /^__pad/;
79 if (defined($formats{$type}))
80 {
81 my $fmt = ${$formats{$type}}[2];
82 if ($fmt =~ /^&(.*)/)
83 {
84 my $func = $1;
85 push @trace_lines, " $func( \"$prefix$var=\", &req->$var );\n";
86 }
87 elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
88 {
89 my ($format, $cast) = ($1, $2);
90 push @trace_lines, " fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
91 }
92 else
93 {
94 push @trace_lines, " fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
95 }
96 }
97 else # must be some varargs format
98 {
99 push @trace_lines, " " . sprintf($type, "$prefix$var=") . ";\n";
100 }
101 $prefix = ", ";
102 }
103 push @trace_lines, "}\n\n";
104 }
105
106 ### Parse the request definitions
107
108 sub PARSE_REQUESTS()
109 {
110 # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
111 my $state = 0;
112 my $offset = 0;
113 my $name = "";
114 my @in_struct = ();
115 my @out_struct = ();
116
117 open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
118
119 while (<PROTOCOL>)
120 {
121 my ($type, $var);
122 # strip comments
123 s!/\*.*\*/!!g;
124 # strip white space at end of line
125 s/\s+$//;
126
127 if (/^\@HEADER/)
128 {
129 die "Misplaced \@HEADER" unless $state == 0;
130 $state++;
131 next;
132 }
133
134 # ignore everything while in state 0
135 next if $state == 0;
136
137 if (/^\@REQ\(\s*(\w+)\s*\)/)
138 {
139 $name = $1;
140 die "Misplaced \@REQ" unless $state == 1;
141 # start a new request
142 @in_struct = ();
143 @out_struct = ();
144 $offset = 12;
145 print SERVER_PROT "struct ${name}_request\n{\n";
146 print SERVER_PROT " struct request_header __header;\n";
147 $state++;
148 next;
149 }
150
151 if (/^\@REPLY/)
152 {
153 die "Misplaced \@REPLY" unless $state == 2;
154 print SERVER_PROT "};\n";
155 print SERVER_PROT "struct ${name}_reply\n{\n";
156 print SERVER_PROT " struct reply_header __header;\n";
157 die "request $name too large ($offset)" if ($offset > $max_req_size);
158 $offset = 8;
159 $state++;
160 next;
161 }
162
163 if (/^\@END/)
164 {
165 die "Misplaced \@END" unless ($state == 2 || $state == 3);
166
167 if ($offset & 7) # all requests should be 8-byte aligned
168 {
169 my $count = 8 - ($offset & 7);
170 print SERVER_PROT " char __pad_$offset\[$count\];\n";
171 $offset += $count;
172 }
173 print SERVER_PROT "};\n";
174 if ($state == 2) # build dummy reply struct
175 {
176 die "request $name too large ($offset)" if ($offset > $max_req_size);
177 push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
178 print SERVER_PROT "struct ${name}_reply\n{\n";
179 print SERVER_PROT " struct reply_header __header;\n";
180 print SERVER_PROT "};\n";
181 }
182 else
183 {
184 die "reply $name too large ($offset)" if ($offset > $max_req_size);
185 push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
186 }
187 # got a complete request
188 push @requests, $name;
189 DO_DUMP_FUNC( $name, "request", @in_struct);
190 if ($#out_struct >= 0)
191 {
192 $replies{$name} = 1;
193 DO_DUMP_FUNC( $name, "reply", @out_struct);
194 }
195 $state = 1;
196 next;
197 }
198
199 if ($state != 1)
200 {
201 # skip empty lines (but keep them in output file)
202 if (/^$/)
203 {
204 print SERVER_PROT "\n";
205 next;
206 }
207
208 if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
209 {
210 $var = $1;
211 $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
212 s!(VARARG\(.*\)\s*;)!/* $1 */!;
213 }
214 elsif (/^\s*VARARG\((\w+),(\w+)\)/)
215 {
216 $var = $1;
217 $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
218 s!(VARARG\(.*\)\s*;)!/* $1 */!;
219 }
220 elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
221 {
222 $type = $1;
223 $var = $3;
224 die "Unrecognized type $type" unless defined($formats{$type});
225 my @fmt = @{$formats{$type}};
226 if ($offset & ($fmt[1] - 1))
227 {
228 my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
229 print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
230 print SERVER_PROT " char __pad_$offset\[$count\];\n";
231 $offset += $count;
232 }
233 if ($state == 2)
234 {
235 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
236 }
237 else
238 {
239 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
240 }
241 $offset += $fmt[0];
242 }
243 else
244 {
245 die "Unrecognized syntax $_";
246 }
247 if ($state == 2) { push @in_struct, $type, $var; }
248 if ($state == 3) { push @out_struct, $type, $var; }
249 }
250
251 # Pass it through into the output file
252 print SERVER_PROT $_ . "\n";
253 }
254 close PROTOCOL;
255 }
256
257 ### Retrieve the server protocol version from the existing server_protocol.h file
258
259 sub GET_PROTOCOL_VERSION()
260 {
261 my $protocol = 0;
262 open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
263 while (<SERVER_PROT>)
264 {
265 if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
266 }
267 close SERVER_PROT;
268 return $protocol;
269 }
270
271 ### Retrieve the list of status and errors used in the server
272
273 sub GET_ERROR_NAMES()
274 {
275 my %errors = ();
276 foreach my $f (glob "server/*.c")
277 {
278 next if $f eq "server/trace.c";
279 open FILE, $f or die "Can't open $f";
280 while (<FILE>)
281 {
282 if (/STATUS_(\w+)/)
283 {
284 $errors{$1} = "STATUS_$1" unless $1 eq "SUCCESS";
285 }
286 elsif (/set_win32_error\s*\(\s*(\w+)\s*\)/)
287 {
288 $errors{$1} = "0xc0010000 | $1";
289 }
290 }
291 close FILE;
292 }
293 return %errors;
294 }
295
296 # update a file if changed
297 sub update_file($)
298 {
299 my $file = shift;
300 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
301 if (!$ret)
302 {
303 unlink "$file.new";
304 }
305 else
306 {
307 rename "$file.new", "$file";
308 print "$file updated\n";
309 }
310 return $ret;
311 }
312
313 # replace some lines in a file between two markers
314 sub replace_in_file($$$@)
315 {
316 my $file = shift;
317 my $start = shift;
318 my $end = shift;
319
320 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
321
322 if (defined($start))
323 {
324 open OLD_FILE, "$file" or die "cannot open $file";
325 while (<OLD_FILE>)
326 {
327 print NEW_FILE $_;
328 last if /$start/;
329 }
330 }
331
332 print NEW_FILE "\n", @_, "\n";
333
334 if (defined($end))
335 {
336 my $skip=1;
337 while (<OLD_FILE>)
338 {
339 $skip = 0 if /$end/;
340 print NEW_FILE $_ unless $skip;
341 }
342 }
343
344 close OLD_FILE if defined($start);
345 close NEW_FILE;
346 return update_file($file);
347 }
348
349 ### Main
350
351 # Get the server protocol version
352 my $protocol = GET_PROTOCOL_VERSION();
353
354 my %errors = GET_ERROR_NAMES();
355
356 ### Create server_protocol.h and print header
357
358 open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
359 print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
360 print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
361 print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
362 print SERVER_PROT " */\n\n";
363 print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
364 print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
365
366 ### Parse requests to find request/reply structure definitions
367
368 PARSE_REQUESTS();
369
370 ### Build the request list and structures
371
372 print SERVER_PROT "\n\nenum request\n{\n";
373 foreach my $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
374 print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
375
376 print SERVER_PROT "union generic_request\n{\n";
377 print SERVER_PROT " struct request_max_size max_size;\n";
378 print SERVER_PROT " struct request_header request_header;\n";
379 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
380 print SERVER_PROT "};\n";
381
382 print SERVER_PROT "union generic_reply\n{\n";
383 print SERVER_PROT " struct request_max_size max_size;\n";
384 print SERVER_PROT " struct reply_header reply_header;\n";
385 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
386 print SERVER_PROT "};\n\n";
387
388 printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol + 1;
389 print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
390 close SERVER_PROT;
391 update_file( "include/wine/server_protocol.h" );
392
393 ### Output the dumping function tables
394
395 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
396 foreach my $req (@requests)
397 {
398 push @trace_lines, " (dump_func)dump_${req}_request,\n";
399 }
400 push @trace_lines, "};\n\n";
401
402 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
403 foreach my $req (@requests)
404 {
405 push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
406 }
407 push @trace_lines, "};\n\n";
408
409 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
410 foreach my $req (@requests)
411 {
412 push @trace_lines, " \"$req\",\n";
413 }
414 push @trace_lines, "};\n\n";
415
416 push @trace_lines, "static const struct\n{\n";
417 push @trace_lines, " const char *name;\n";
418 push @trace_lines, " unsigned int value;\n";
419 push @trace_lines, "} status_names[] =\n{\n";
420
421 foreach my $err (sort keys %errors)
422 {
423 push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
424 }
425 push @trace_lines, " { NULL, 0 }\n";
426 push @trace_lines, "};\n";
427
428 replace_in_file( "server/trace.c",
429 "### make_requests begin ###",
430 "### make_requests end ###",
431 @trace_lines );
432
433 ### Output the request handlers list
434
435 my @request_lines = ();
436
437 foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
438 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
439 push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
440 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
441 foreach my $req (@requests)
442 {
443 push @request_lines, " (req_handler)req_$req,\n";
444 }
445 push @request_lines, "};\n\n";
446
447 foreach my $type (sort keys %formats)
448 {
449 my $size = ${$formats{$type}}[0];
450 push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
451 }
452 push @request_lines, @asserts;
453 push @request_lines, "\n#endif /* WANT_REQUEST_HANDLERS */\n";
454
455 replace_in_file( "server/request.h",
456 "### make_requests begin ###",
457 "### make_requests end ###",
458 @request_lines );
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.