shell32: Add the SHCreateDirectoryEx() macro.
[wine/multimedia.git] / tools / make_requests
blobb53af11787fe961923b12ff9d01b59ab90da6857
1 #! /usr/bin/perl -w
3 # Build the server/trace.c and server/request.h files
4 # from the contents of include/wine/server.h.
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 "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" ],
56 my @requests = ();
57 my %replies = ();
58 my @asserts = ();
60 my @trace_lines = ();
62 my $max_req_size = 64;
64 my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
66 ### Generate a dumping function
68 sub DO_DUMP_FUNC($$@)
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)
76 my $type = shift;
77 my $var = shift;
78 next if $var =~ /^__pad/;
79 if (defined($formats{$type}))
81 my $fmt = ${$formats{$type}}[2];
82 if ($fmt =~ /^&(.*)/)
84 my $func = $1;
85 push @trace_lines, " $func( \"$prefix$var=\", &req->$var );\n";
87 elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
89 my ($format, $cast) = ($1, $2);
90 push @trace_lines, " fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
92 else
94 push @trace_lines, " fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
97 else # must be some varargs format
99 push @trace_lines, " " . sprintf($type, "$prefix$var=") . ";\n";
101 $prefix = ", ";
103 push @trace_lines, "}\n\n";
106 ### Parse the request definitions
108 sub PARSE_REQUESTS()
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 = ();
117 open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
119 while (<PROTOCOL>)
121 my ($type, $var);
122 # strip comments
123 s!/\*.*\*/!!g;
124 # strip white space at end of line
125 s/\s+$//;
127 if (/^\@HEADER/)
129 die "Misplaced \@HEADER" unless $state == 0;
130 $state++;
131 next;
134 # ignore everything while in state 0
135 next if $state == 0;
137 if (/^\@REQ\(\s*(\w+)\s*\)/)
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;
151 if (/^\@REPLY/)
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;
163 if (/^\@END/)
165 die "Misplaced \@END" unless ($state == 2 || $state == 3);
167 if ($offset & 7) # all requests should be 8-byte aligned
169 my $count = 8 - ($offset & 7);
170 print SERVER_PROT " char __pad_$offset\[$count\];\n";
171 $offset += $count;
173 print SERVER_PROT "};\n";
174 if ($state == 2) # build dummy reply struct
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";
182 else
184 die "reply $name too large ($offset)" if ($offset > $max_req_size);
185 push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
187 # got a complete request
188 push @requests, $name;
189 DO_DUMP_FUNC( $name, "request", @in_struct);
190 if ($#out_struct >= 0)
192 $replies{$name} = 1;
193 DO_DUMP_FUNC( $name, "reply", @out_struct);
195 $state = 1;
196 next;
199 if ($state != 1)
201 # skip empty lines (but keep them in output file)
202 if (/^$/)
204 print SERVER_PROT "\n";
205 next;
208 if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
210 $var = $1;
211 $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
212 s!(VARARG\(.*\)\s*;)!/* $1 */!;
214 elsif (/^\s*VARARG\((\w+),(\w+)\)/)
216 $var = $1;
217 $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
218 s!(VARARG\(.*\)\s*;)!/* $1 */!;
220 elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
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))
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;
233 if ($state == 2)
235 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
237 else
239 push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
241 $offset += $fmt[0];
243 else
245 die "Unrecognized syntax $_";
247 if ($state == 2) { push @in_struct, $type, $var; }
248 if ($state == 3) { push @out_struct, $type, $var; }
251 # Pass it through into the output file
252 print SERVER_PROT $_ . "\n";
254 close PROTOCOL;
257 ### Retrieve the server protocol version from the existing server_protocol.h file
259 sub GET_PROTOCOL_VERSION()
261 my $protocol = 0;
262 open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
263 while (<SERVER_PROT>)
265 if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
267 close SERVER_PROT;
268 return $protocol;
271 ### Retrieve the list of status and errors used in the server
273 sub GET_ERROR_NAMES()
275 my %errors = ();
276 foreach my $f (glob "server/*.c")
278 next if $f eq "server/trace.c";
279 open FILE, $f or die "Can't open $f";
280 while (<FILE>)
282 if (/STATUS_(\w+)/)
284 $errors{$1} = "STATUS_$1" unless $1 eq "SUCCESS";
286 elsif (/set_win32_error\s*\(\s*(\w+)\s*\)/)
288 $errors{$1} = "0xc0010000 | $1";
291 close FILE;
293 return %errors;
296 # update a file if changed
297 sub update_file($)
299 my $file = shift;
300 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
301 if (!$ret)
303 unlink "$file.new";
305 else
307 rename "$file.new", "$file";
308 print "$file updated\n";
310 return $ret;
313 # replace some lines in a file between two markers
314 sub replace_in_file($$$@)
316 my $file = shift;
317 my $start = shift;
318 my $end = shift;
320 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
322 if (defined($start))
324 open OLD_FILE, "$file" or die "cannot open $file";
325 while (<OLD_FILE>)
327 print NEW_FILE $_;
328 last if /$start/;
332 print NEW_FILE "\n", @_, "\n";
334 if (defined($end))
336 my $skip=1;
337 while (<OLD_FILE>)
339 $skip = 0 if /$end/;
340 print NEW_FILE $_ unless $skip;
344 close OLD_FILE if defined($start);
345 close NEW_FILE;
346 return update_file($file);
349 ### Main
351 # Get the server protocol version
352 my $protocol = GET_PROTOCOL_VERSION();
354 my %errors = GET_ERROR_NAMES();
356 ### Create server_protocol.h and print header
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";
366 ### Parse requests to find request/reply structure definitions
368 PARSE_REQUESTS();
370 ### Build the request list and structures
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";
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";
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";
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" );
393 ### Output the dumping function tables
395 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
396 foreach my $req (@requests)
398 push @trace_lines, " (dump_func)dump_${req}_request,\n";
400 push @trace_lines, "};\n\n";
402 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
403 foreach my $req (@requests)
405 push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
407 push @trace_lines, "};\n\n";
409 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
410 foreach my $req (@requests)
412 push @trace_lines, " \"$req\",\n";
414 push @trace_lines, "};\n\n";
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";
421 foreach my $err (sort keys %errors)
423 push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
425 push @trace_lines, " { NULL, 0 }\n";
426 push @trace_lines, "};\n";
428 replace_in_file( "server/trace.c",
429 "### make_requests begin ###",
430 "### make_requests end ###",
431 @trace_lines );
433 ### Output the request handlers list
435 my @request_lines = ();
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)
443 push @request_lines, " (req_handler)req_$req,\n";
445 push @request_lines, "};\n\n";
447 foreach my $type (sort keys %formats)
449 my $size = ${$formats{$type}}[0];
450 push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
452 push @request_lines, @asserts;
453 push @request_lines, "\n#endif /* WANT_REQUEST_HANDLERS */\n";
455 replace_in_file( "server/request.h",
456 "### make_requests begin ###",
457 "### make_requests end ###",
458 @request_lines );