dmime: Rewrite segment IDirectMusicSegment_RemoveTrack.
[wine.git] / tools / make_requests
blob5303ff058df448b57c6ca581b8204d5b7effd8d7
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 "timeout_t" => [ 8, 8, "&dump_timeout" ],
46 "abstime_t" => [ 8, 8, "&dump_abstime" ],
47 "rectangle_t" => [ 16, 4, "&dump_rectangle" ],
48 "apc_result_t" => [ 40, 8, "&dump_apc_result" ],
49 "async_data_t" => [ 40, 8, "&dump_async_data" ],
50 "irp_params_t" => [ 32, 8, "&dump_irp_params" ],
51 "struct luid" => [ 8, 4, "&dump_luid" ],
52 "generic_map_t" => [ 16, 4, "&dump_generic_map" ],
53 "ioctl_code_t" => [ 4, 4, "&dump_ioctl_code" ],
54 "hw_input_t" => [ 40, 8, "&dump_hw_input" ],
55 # varargs-only structures
56 "apc_call_t" => [ 64, 8 ],
57 "context_t" => [ 1720, 8 ],
58 "cursor_pos_t" => [ 24, 8 ],
59 "debug_event_t" => [ 160, 8 ],
60 "message_data_t" => [ 56, 8 ],
61 "pe_image_info_t" => [ 80, 8 ],
62 "property_data_t" => [ 16, 8 ],
63 "select_op_t" => [ 264, 8 ],
64 "startup_info_t" => [ 96, 4 ],
65 "user_apc_t" => [ 40, 8 ],
66 "struct filesystem_event" => [ 12, 4 ],
67 "struct handle_info" => [ 20, 4 ],
68 "struct luid_attr" => [ 12, 4 ],
69 "struct object_attributes" => [ 16, 4 ],
70 "struct object_type_info" => [ 44, 4 ],
71 "struct process_info" => [ 40, 8 ],
72 "struct rawinput_device" => [ 12, 4 ],
73 "struct thread_info" => [ 40, 8 ],
76 my @requests = ();
77 my %replies = ();
78 my @asserts = ();
80 my @trace_lines = ();
82 my $max_req_size = 64;
84 my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
86 sub add_padding($$)
88 my ($offset, $padding) = @_;
89 if ($offset % $padding)
91 my $count = $padding - ($offset % $padding);
92 print SERVER_PROT " char __pad_$offset\[$count\];\n";
93 $offset += $count;
95 return $offset;
98 ### Generate a dumping function
100 sub DO_DUMP_FUNC($$@)
102 my $name = shift;
103 my $req = shift;
104 my $prefix = " ";
105 push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
106 while ($#_ >= 0)
108 my $type = shift;
109 my $var = shift;
110 next if $var =~ /^__pad/;
111 if (defined($formats{$type}))
113 my $fmt = ${$formats{$type}}[2];
114 if ($fmt =~ /^&(.*)/)
116 my $func = $1;
117 push @trace_lines, " $func( \"$prefix$var=\", &req->$var );\n";
119 elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
121 my ($format, $cast) = ($1, $2);
122 push @trace_lines, " fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
124 else
126 push @trace_lines, " fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
129 else # must be some varargs format
131 push @trace_lines, " " . sprintf($type, "$prefix$var=") . ";\n";
133 $prefix = ", ";
135 push @trace_lines, "}\n\n";
138 ### Parse the request definitions
140 sub PARSE_REQUESTS()
142 # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
143 my $state = 0;
144 my $offset = 0;
145 my $name = "";
146 my @in_struct = ();
147 my @out_struct = ();
149 open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
151 while (<PROTOCOL>)
153 my ($type, $var);
154 # strip comments
155 s!/\*.*\*/!!g;
156 # strip white space at end of line
157 s/\s+$//;
159 if (/^\@HEADER/)
161 die "Misplaced \@HEADER" unless $state == 0;
162 $state++;
163 next;
166 # ignore everything while in state 0
167 next if $state == 0;
169 if (/^\@REQ\(\s*(\w+)\s*\)/)
171 $name = $1;
172 die "Misplaced \@REQ" unless $state == 1;
173 # start a new request
174 @in_struct = ();
175 @out_struct = ();
176 $offset = 12;
177 print SERVER_PROT "struct ${name}_request\n{\n";
178 print SERVER_PROT " struct request_header __header;\n";
179 $state++;
180 next;
183 if (/^\@REPLY/)
185 die "Misplaced \@REPLY" unless $state == 2;
186 $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
187 die "request $name too large ($offset)" if ($offset > $max_req_size);
188 push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
189 print SERVER_PROT "};\n";
190 print SERVER_PROT "struct ${name}_reply\n{\n";
191 print SERVER_PROT " struct reply_header __header;\n";
192 $offset = 8;
193 $state++;
194 next;
197 if (/^\@END/)
199 die "Misplaced \@END" unless ($state == 2 || $state == 3);
201 $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
202 print SERVER_PROT "};\n";
203 if ($state == 2) # build dummy reply struct
205 die "request $name too large ($offset)" if ($offset > $max_req_size);
206 push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
207 print SERVER_PROT "struct ${name}_reply\n{\n";
208 print SERVER_PROT " struct reply_header __header;\n";
209 print SERVER_PROT "};\n";
211 else
213 die "reply $name too large ($offset)" if ($offset > $max_req_size);
214 push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
216 # got a complete request
217 push @requests, $name;
218 DO_DUMP_FUNC( $name, "request", @in_struct);
219 if ($#out_struct >= 0)
221 $replies{$name} = 1;
222 DO_DUMP_FUNC( $name, "reply", @out_struct);
224 $state = 1;
225 next;
228 if ($state != 1)
230 # skip empty lines (but keep them in output file)
231 if (/^$/)
233 print SERVER_PROT "\n";
234 next;
237 if (/^\s*VARARG\((\w+),(\w+),(\d+)\)/)
239 $var = $1;
240 $type = "dump_varargs_$2( \"%s\", min(cur_size,$3) )";
241 s!(VARARG\(.*\)\s*;)!/* $1 */!;
243 elsif (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
245 $var = $1;
246 $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
247 s!(VARARG\(.*\)\s*;)!/* $1 */!;
249 elsif (/^\s*VARARG\((\w+),(\w+)\)/)
251 $var = $1;
252 $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
253 s!(VARARG\(.*\)\s*;)!/* $1 */!;
255 elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
257 $type = $1;
258 $var = $3;
259 die "Unrecognized type $type" unless defined($formats{$type});
260 my @fmt = @{$formats{$type}};
261 if ($offset & ($fmt[1] - 1))
263 my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
264 print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
265 print SERVER_PROT " char __pad_$offset\[$count\];\n";
266 $offset += $count;
268 if ($state == 2)
270 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
272 else
274 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
276 $offset += $fmt[0];
278 else
280 die "Unrecognized syntax $_";
282 if ($state == 2) { push @in_struct, $type, $var; }
283 if ($state == 3) { push @out_struct, $type, $var; }
286 # Pass it through into the output file
287 print SERVER_PROT $_ . "\n";
289 close PROTOCOL;
292 ### Retrieve the server protocol version from the existing server_protocol.h file
294 sub GET_PROTOCOL_VERSION()
296 my $protocol = 0;
297 open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
298 while (<SERVER_PROT>)
300 if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
302 close SERVER_PROT;
303 return $protocol;
306 ### Retrieve the list of status and errors used in the server
308 sub GET_ERROR_NAMES()
310 my %errors = ();
311 foreach my $f (glob "server/*.c")
313 next if $f eq "server/trace.c";
314 open FILE, $f or die "Can't open $f";
315 while (<FILE>)
317 while (/\bSTATUS_(\w+)/g)
319 $errors{$1} = "STATUS_$1" unless ($1 eq "SUCCESS" || $1 eq "WAIT_0");
321 while (/\bset_win32_error\s*\(\s*(\w+)\s*\)/g)
323 $errors{$1} = "0xc0010000 | $1";
325 while (/\breturn\s+(WSA\w+)/g)
327 $errors{$1} = "0xc0010000 | $1";
330 close FILE;
332 return %errors;
335 # update a file if changed
336 sub update_file($)
338 my $file = shift;
339 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
340 if (!$ret)
342 unlink "$file.new";
344 else
346 rename "$file.new", "$file";
347 print "$file updated\n";
349 return $ret;
352 # replace some lines in a file between two markers
353 sub replace_in_file($$$@)
355 my $file = shift;
356 my $start = shift;
357 my $end = shift;
359 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
361 if (defined($start))
363 open OLD_FILE, "$file" or die "cannot open $file";
364 while (<OLD_FILE>)
366 print NEW_FILE $_;
367 last if /$start/;
371 print NEW_FILE "\n", @_, "\n";
373 if (defined($end))
375 my $skip=1;
376 while (<OLD_FILE>)
378 $skip = 0 if /$end/;
379 print NEW_FILE $_ unless $skip;
383 close OLD_FILE if defined($start);
384 close NEW_FILE;
385 return update_file($file);
388 ### Main
390 # Get the server protocol version
391 my $protocol = GET_PROTOCOL_VERSION();
393 my %errors = GET_ERROR_NAMES();
395 ### Create server_protocol.h and print header
397 open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
398 print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
399 print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
400 print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
401 print SERVER_PROT " */\n\n";
402 print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
403 print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
405 ### Parse requests to find request/reply structure definitions
407 PARSE_REQUESTS();
409 ### Build the request list and structures
411 print SERVER_PROT "\n\nenum request\n{\n";
412 foreach my $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
413 print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
415 print SERVER_PROT "union generic_request\n{\n";
416 print SERVER_PROT " struct request_max_size max_size;\n";
417 print SERVER_PROT " struct request_header request_header;\n";
418 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
419 print SERVER_PROT "};\n";
421 print SERVER_PROT "union generic_reply\n{\n";
422 print SERVER_PROT " struct request_max_size max_size;\n";
423 print SERVER_PROT " struct reply_header reply_header;\n";
424 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
425 print SERVER_PROT "};\n\n";
427 print SERVER_PROT "/* ### protocol_version begin ### */\n\n";
428 printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol;
429 print SERVER_PROT "/* ### protocol_version end ### */\n\n";
430 print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
431 close SERVER_PROT;
433 if (update_file( "include/wine/server_protocol.h" ))
435 my @version_lines = ();
437 push @version_lines, sprintf( "#define SERVER_PROTOCOL_VERSION %d\n", $protocol + 1 );
439 replace_in_file( "include/wine/server_protocol.h",
440 "### protocol_version begin ###",
441 "### protocol_version end ###",
442 @version_lines );
445 ### Output the dumping function tables
447 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
448 foreach my $req (@requests)
450 push @trace_lines, " (dump_func)dump_${req}_request,\n";
452 push @trace_lines, "};\n\n";
454 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
455 foreach my $req (@requests)
457 push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
459 push @trace_lines, "};\n\n";
461 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
462 foreach my $req (@requests)
464 push @trace_lines, " \"$req\",\n";
466 push @trace_lines, "};\n\n";
468 push @trace_lines, "static const struct\n{\n";
469 push @trace_lines, " const char *name;\n";
470 push @trace_lines, " unsigned int value;\n";
471 push @trace_lines, "} status_names[] =\n{\n";
473 foreach my $err (sort keys %errors)
475 push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
477 push @trace_lines, " { NULL, 0 }\n";
478 push @trace_lines, "};\n";
480 replace_in_file( "server/trace.c",
481 "### make_requests begin ###",
482 "### make_requests end ###",
483 @trace_lines );
485 ### Output the request handlers list
487 my @request_lines = ();
489 foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
490 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
491 push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
492 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
493 foreach my $req (@requests)
495 push @request_lines, " (req_handler)req_$req,\n";
497 push @request_lines, "};\n\n";
499 foreach my $type (sort keys %formats)
501 my ($size, $align) = @{$formats{$type}};
502 die "$type: invalid size $size for alignment $align" if $size % $align;
503 push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
505 push @request_lines, @asserts;
506 push @request_lines, "\n#endif /* WANT_REQUEST_HANDLERS */\n";
508 replace_in_file( "server/request.h",
509 "### make_requests begin ###",
510 "### make_requests end ###",
511 @request_lines );