include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / tools / make_requests
blob36254faec40a4802616c7fec940b9020592d5152
1 #! /usr/bin/perl -w
3 # Build the server/trace.c and server/request.h files
4 # from the contents of server/protocol.def.
6 # Copyright (C) 1998 Alexandre Julliard
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.
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.
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
22 use strict;
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 "object_id_t" => [ 8, 8, "&dump_uint64" ],
46 "timeout_t" => [ 8, 8, "&dump_timeout" ],
47 "abstime_t" => [ 8, 8, "&dump_abstime" ],
48 "rectangle_t" => [ 16, 4, "&dump_rectangle" ],
49 "apc_result_t" => [ 40, 8, "&dump_apc_result" ],
50 "async_data_t" => [ 40, 8, "&dump_async_data" ],
51 "irp_params_t" => [ 32, 8, "&dump_irp_params" ],
52 "struct luid" => [ 8, 4, "&dump_luid" ],
53 "generic_map_t" => [ 16, 4, "&dump_generic_map" ],
54 "ioctl_code_t" => [ 4, 4, "&dump_ioctl_code" ],
55 "hw_input_t" => [ 40, 8, "&dump_hw_input" ],
56 "obj_locator_t" => [ 16, 8, "&dump_obj_locator" ],
57 # varargs-only structures
58 "apc_call_t" => [ 64, 8 ],
59 "context_t" => [ 1728, 8 ],
60 "cursor_pos_t" => [ 24, 8 ],
61 "debug_event_t" => [ 160, 8 ],
62 "message_data_t" => [ 48, 8 ],
63 "pe_image_info_t" => [ 88, 8 ],
64 "property_data_t" => [ 16, 8 ],
65 "select_op_t" => [ 264, 8 ],
66 "startup_info_t" => [ 96, 4 ],
67 "user_apc_t" => [ 40, 8 ],
68 "struct filesystem_event" => [ 12, 4 ],
69 "struct handle_info" => [ 20, 4 ],
70 "struct luid_attr" => [ 12, 4 ],
71 "struct object_attributes" => [ 16, 4 ],
72 "struct object_type_info" => [ 44, 4 ],
73 "struct process_info" => [ 40, 8 ],
74 "struct rawinput_device" => [ 12, 4 ],
75 "struct thread_info" => [ 40, 8 ],
78 my @requests = ();
79 my %replies = ();
80 my @asserts = ();
82 my @trace_lines = ();
84 my $max_req_size = 64;
86 my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
88 sub add_padding($$)
90 my ($offset, $padding) = @_;
91 if ($offset % $padding)
93 my $count = $padding - ($offset % $padding);
94 print SERVER_PROT " char __pad_$offset\[$count\];\n";
95 $offset += $count;
97 return $offset;
100 ### Generate a dumping function
102 sub DO_DUMP_FUNC($$@)
104 my $name = shift;
105 my $req = shift;
106 my $prefix = " ";
107 push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
108 while ($#_ >= 0)
110 my $type = shift;
111 my $var = shift;
112 next if $var =~ /^__pad/;
113 if (defined($formats{$type}))
115 my $fmt = ${$formats{$type}}[2];
116 if ($fmt =~ /^&(.*)/)
118 my $func = $1;
119 push @trace_lines, " $func( \"$prefix$var=\", &req->$var );\n";
121 elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
123 my ($format, $cast) = ($1, $2);
124 push @trace_lines, " fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
126 else
128 push @trace_lines, " fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
131 else # must be some varargs format
133 push @trace_lines, " " . sprintf($type, "$prefix$var=") . ";\n";
135 $prefix = ", ";
137 push @trace_lines, "}\n\n";
140 ### Parse the request definitions
142 sub PARSE_REQUESTS()
144 # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
145 my $state = 0;
146 my $offset = 0;
147 my $name = "";
148 my @in_struct = ();
149 my @out_struct = ();
151 open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
153 while (<PROTOCOL>)
155 my ($type, $var);
156 # strip comments
157 s!/\*.*\*/!!g;
158 # strip white space at end of line
159 s/\s+$//;
161 if (/^\@HEADER/)
163 die "Misplaced \@HEADER" unless $state == 0;
164 $state++;
165 next;
168 # ignore everything while in state 0
169 next if $state == 0;
171 if (/^\@REQ\(\s*(\w+)\s*\)/)
173 $name = $1;
174 die "Misplaced \@REQ" unless $state == 1;
175 # start a new request
176 @in_struct = ();
177 @out_struct = ();
178 $offset = 12;
179 print SERVER_PROT "struct ${name}_request\n{\n";
180 print SERVER_PROT " struct request_header __header;\n";
181 $state++;
182 next;
185 if (/^\@REPLY/)
187 die "Misplaced \@REPLY" unless $state == 2;
188 $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
189 die "request $name too large ($offset)" if ($offset > $max_req_size);
190 push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
191 print SERVER_PROT "};\n";
192 print SERVER_PROT "struct ${name}_reply\n{\n";
193 print SERVER_PROT " struct reply_header __header;\n";
194 $offset = 8;
195 $state++;
196 next;
199 if (/^\@END/)
201 die "Misplaced \@END" unless ($state == 2 || $state == 3);
203 $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
204 print SERVER_PROT "};\n";
205 if ($state == 2) # build dummy reply struct
207 die "request $name too large ($offset)" if ($offset > $max_req_size);
208 push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
209 print SERVER_PROT "struct ${name}_reply\n{\n";
210 print SERVER_PROT " struct reply_header __header;\n";
211 print SERVER_PROT "};\n";
213 else
215 die "reply $name too large ($offset)" if ($offset > $max_req_size);
216 push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
218 # got a complete request
219 push @requests, $name;
220 DO_DUMP_FUNC( $name, "request", @in_struct);
221 if ($#out_struct >= 0)
223 $replies{$name} = 1;
224 DO_DUMP_FUNC( $name, "reply", @out_struct);
226 $state = 1;
227 next;
230 if ($state != 1)
232 # skip empty lines (but keep them in output file)
233 if (/^$/)
235 print SERVER_PROT "\n";
236 next;
239 if (/^\s*VARARG\((\w+),(\w+),(\d+)\)/)
241 $var = $1;
242 $type = "dump_varargs_$2( \"%s\", min(cur_size,$3) )";
243 s!(VARARG\(.*\)\s*;)!/* $1 */!;
245 elsif (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
247 $var = $1;
248 $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
249 s!(VARARG\(.*\)\s*;)!/* $1 */!;
251 elsif (/^\s*VARARG\((\w+),(\w+)\)/)
253 $var = $1;
254 $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
255 s!(VARARG\(.*\)\s*;)!/* $1 */!;
257 elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
259 $type = $1;
260 $var = $3;
261 die "Unrecognized type $type" unless defined($formats{$type});
262 my @fmt = @{$formats{$type}};
263 if ($offset & ($fmt[1] - 1))
265 my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
266 print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
267 print SERVER_PROT " char __pad_$offset\[$count\];\n";
268 $offset += $count;
270 if ($state == 2)
272 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
274 else
276 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
278 $offset += $fmt[0];
280 else
282 die "Unrecognized syntax $_";
284 if ($state == 2) { push @in_struct, $type, $var; }
285 if ($state == 3) { push @out_struct, $type, $var; }
288 # Pass it through into the output file
289 print SERVER_PROT $_ . "\n";
291 close PROTOCOL;
294 ### Retrieve the server protocol version from the existing server_protocol.h file
296 sub GET_PROTOCOL_VERSION()
298 my $protocol = 0;
299 open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
300 while (<SERVER_PROT>)
302 if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
304 close SERVER_PROT;
305 return $protocol;
308 ### Retrieve the list of status and errors used in the server
310 sub GET_ERROR_NAMES()
312 my %errors = ();
313 foreach my $f (glob "server/*.c")
315 next if $f eq "server/trace.c";
316 open FILE, $f or die "Can't open $f";
317 while (<FILE>)
319 while (/\bSTATUS_(\w+)/g)
321 $errors{$1} = "STATUS_$1" unless ($1 eq "SUCCESS" || $1 eq "WAIT_0");
323 while (/\bset_win32_error\s*\(\s*(\w+)\s*\)/g)
325 $errors{$1} = "0xc0010000 | $1";
327 while (/\breturn\s+(WSA\w+)/g)
329 $errors{$1} = "0xc0010000 | $1";
332 close FILE;
334 return %errors;
337 # update a file if changed
338 sub update_file($)
340 my $file = shift;
341 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
342 if (!$ret)
344 unlink "$file.new";
346 else
348 rename "$file.new", "$file";
349 print "$file updated\n";
351 return $ret;
354 # replace some lines in a file between two markers
355 sub replace_in_file($$$@)
357 my $file = shift;
358 my $start = shift;
359 my $end = shift;
361 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
363 if (defined($start))
365 open OLD_FILE, "$file" or die "cannot open $file";
366 while (<OLD_FILE>)
368 print NEW_FILE $_;
369 last if /$start/;
373 print NEW_FILE "\n", @_, "\n";
375 if (defined($end))
377 my $skip=1;
378 while (<OLD_FILE>)
380 $skip = 0 if /$end/;
381 print NEW_FILE $_ unless $skip;
385 close OLD_FILE if defined($start);
386 close NEW_FILE;
387 return update_file($file);
390 ### Main
392 # Get the server protocol version
393 my $protocol = GET_PROTOCOL_VERSION();
395 my %errors = GET_ERROR_NAMES();
397 ### Create server_protocol.h and print header
399 open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
400 print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
401 print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
402 print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
403 print SERVER_PROT " */\n\n";
404 print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
405 print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
407 ### Parse requests to find request/reply structure definitions
409 PARSE_REQUESTS();
411 ### Build the request list and structures
413 print SERVER_PROT "\n\nenum request\n{\n";
414 foreach my $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
415 print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
417 print SERVER_PROT "union generic_request\n{\n";
418 print SERVER_PROT " struct request_max_size max_size;\n";
419 print SERVER_PROT " struct request_header request_header;\n";
420 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
421 print SERVER_PROT "};\n";
423 print SERVER_PROT "union generic_reply\n{\n";
424 print SERVER_PROT " struct request_max_size max_size;\n";
425 print SERVER_PROT " struct reply_header reply_header;\n";
426 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
427 print SERVER_PROT "};\n\n";
429 print SERVER_PROT "/* ### protocol_version begin ### */\n\n";
430 printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol;
431 print SERVER_PROT "/* ### protocol_version end ### */\n\n";
432 print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
433 close SERVER_PROT;
435 if (update_file( "include/wine/server_protocol.h" ))
437 my @version_lines = ();
439 push @version_lines, sprintf( "#define SERVER_PROTOCOL_VERSION %d\n", $protocol + 1 );
441 replace_in_file( "include/wine/server_protocol.h",
442 "### protocol_version begin ###",
443 "### protocol_version end ###",
444 @version_lines );
447 ### Output the dumping function tables
449 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
450 foreach my $req (@requests)
452 push @trace_lines, " (dump_func)dump_${req}_request,\n";
454 push @trace_lines, "};\n\n";
456 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
457 foreach my $req (@requests)
459 push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
461 push @trace_lines, "};\n\n";
463 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
464 foreach my $req (@requests)
466 push @trace_lines, " \"$req\",\n";
468 push @trace_lines, "};\n\n";
470 push @trace_lines, "static const struct\n{\n";
471 push @trace_lines, " const char *name;\n";
472 push @trace_lines, " unsigned int value;\n";
473 push @trace_lines, "} status_names[] =\n{\n";
475 foreach my $err (sort keys %errors)
477 push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
479 push @trace_lines, " { NULL, 0 }\n";
480 push @trace_lines, "};\n";
482 replace_in_file( "server/trace.c",
483 "### make_requests begin ###",
484 "### make_requests end ###",
485 @trace_lines );
487 ### Output the request handlers list
489 my @request_lines = ();
491 foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
492 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
493 push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
494 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
495 foreach my $req (@requests)
497 push @request_lines, " (req_handler)req_$req,\n";
499 push @request_lines, "};\n\n";
501 foreach my $type (sort keys %formats)
503 my ($size, $align) = @{$formats{$type}};
504 die "$type: invalid size $size for alignment $align" if $size % $align;
505 push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
507 push @request_lines, @asserts;
508 push @request_lines, "\n#endif /* WANT_REQUEST_HANDLERS */\n";
510 replace_in_file( "server/request.h",
511 "### make_requests begin ###",
512 "### make_requests end ###",
513 @request_lines );