wbemprox: Always convert from BSTR.
[wine] / tools / make_requests
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     "hw_input_t"    => [  32,  8,  "&dump_hw_input" ],
55 );
56
57 my @requests = ();
58 my %replies = ();
59 my @asserts = ();
60
61 my @trace_lines = ();
62
63 my $max_req_size = 64;
64
65 my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
66
67 sub add_padding($$)
68 {
69     my ($offset, $padding) = @_;
70     if ($offset % $padding)
71     {
72         my $count = $padding - ($offset % $padding);
73         print SERVER_PROT "    char __pad_$offset\[$count\];\n";
74         $offset += $count;
75     }
76     return $offset;
77 }
78
79 ### Generate a dumping function
80
81 sub DO_DUMP_FUNC($$@)
82 {
83     my $name = shift;
84     my $req = shift;
85     my $prefix = " ";
86     push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
87     while ($#_ >= 0)
88     {
89         my $type = shift;
90         my $var = shift;
91         next if $var =~ /^__pad/;
92         if (defined($formats{$type}))
93         {
94             my $fmt = ${$formats{$type}}[2];
95             if ($fmt =~ /^&(.*)/)
96             {
97                 my $func = $1;
98                 push @trace_lines, "    $func( \"$prefix$var=\", &req->$var );\n";
99             }
100             elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
101             {
102                 my ($format, $cast) = ($1, $2);
103                 push @trace_lines, "    fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
104             }
105             else
106             {
107                 push @trace_lines, "    fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
108             }
109         }
110         else  # must be some varargs format
111         {
112             push @trace_lines, "    " . sprintf($type, "$prefix$var=") . ";\n";
113         }
114         $prefix = ", ";
115     }
116     push @trace_lines, "}\n\n";
117 }
118
119 ### Parse the request definitions
120
121 sub PARSE_REQUESTS()
122 {
123     # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
124     my $state = 0;
125     my $offset = 0;
126     my $name = "";
127     my @in_struct = ();
128     my @out_struct = ();
129
130     open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
131
132     while (<PROTOCOL>)
133     {
134         my ($type, $var);
135         # strip comments
136         s!/\*.*\*/!!g;
137         # strip white space at end of line
138         s/\s+$//;
139
140         if (/^\@HEADER/)
141         {
142             die "Misplaced \@HEADER" unless $state == 0;
143             $state++;
144             next;
145         }
146
147         # ignore everything while in state 0
148         next if $state == 0;
149
150         if (/^\@REQ\(\s*(\w+)\s*\)/)
151         {
152             $name = $1;
153             die "Misplaced \@REQ" unless $state == 1;
154             # start a new request
155             @in_struct = ();
156             @out_struct = ();
157             $offset = 12;
158             print SERVER_PROT "struct ${name}_request\n{\n";
159             print SERVER_PROT "    struct request_header __header;\n";
160             $state++;
161             next;
162         }
163
164         if (/^\@REPLY/)
165         {
166             die "Misplaced \@REPLY" unless $state == 2;
167             $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
168             die "request $name too large ($offset)" if ($offset > $max_req_size);
169             push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
170             print SERVER_PROT "};\n";
171             print SERVER_PROT "struct ${name}_reply\n{\n";
172             print SERVER_PROT "    struct reply_header __header;\n";
173             die "request $name too large ($offset)" if ($offset > $max_req_size);
174             $offset = 8;
175             $state++;
176             next;
177         }
178
179         if (/^\@END/)
180         {
181             die "Misplaced \@END" unless ($state == 2 || $state == 3);
182
183             $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
184             print SERVER_PROT "};\n";
185             if ($state == 2)  # build dummy reply struct
186             {
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 "struct ${name}_reply\n{\n";
190                 print SERVER_PROT "    struct reply_header __header;\n";
191                 print SERVER_PROT "};\n";
192             }
193             else
194             {
195                 die "reply $name too large ($offset)" if ($offset > $max_req_size);
196                 push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
197             }
198             # got a complete request
199             push @requests, $name;
200             DO_DUMP_FUNC( $name, "request", @in_struct);
201             if ($#out_struct >= 0)
202             {
203                 $replies{$name} = 1;
204                 DO_DUMP_FUNC( $name, "reply", @out_struct);
205             }
206             $state = 1;
207             next;
208         }
209
210         if ($state != 1)
211         {
212             # skip empty lines (but keep them in output file)
213             if (/^$/)
214             {
215                 print SERVER_PROT "\n";
216                 next;
217             }
218
219             if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
220             {
221                 $var = $1;
222                 $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
223                 s!(VARARG\(.*\)\s*;)!/* $1 */!;
224             }
225             elsif (/^\s*VARARG\((\w+),(\w+)\)/)
226             {
227                 $var = $1;
228                 $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
229                 s!(VARARG\(.*\)\s*;)!/* $1 */!;
230             }
231             elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
232             {
233                 $type = $1;
234                 $var = $3;
235                 die "Unrecognized type $type" unless defined($formats{$type});
236                 my @fmt = @{$formats{$type}};
237                 if ($offset & ($fmt[1] - 1))
238                 {
239                     my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
240                     print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
241                     print SERVER_PROT "    char __pad_$offset\[$count\];\n";
242                     $offset += $count;
243                 }
244                 if ($state == 2)
245                 {
246                     push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
247                 }
248                 else
249                 {
250                     push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
251                 }
252                 $offset += $fmt[0];
253             }
254             else
255             {
256                 die "Unrecognized syntax $_";
257             }
258             if ($state == 2) { push @in_struct, $type, $var; }
259             if ($state == 3) { push @out_struct, $type, $var; }
260         }
261
262         # Pass it through into the output file
263         print SERVER_PROT $_ . "\n";
264     }
265     close PROTOCOL;
266 }
267
268 ### Retrieve the server protocol version from the existing server_protocol.h file
269
270 sub GET_PROTOCOL_VERSION()
271 {
272     my $protocol = 0;
273     open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
274     while (<SERVER_PROT>)
275     {
276         if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
277     }
278     close SERVER_PROT;
279     return $protocol;
280 }
281
282 ### Retrieve the list of status and errors used in the server
283
284 sub GET_ERROR_NAMES()
285 {
286     my %errors = ();
287     foreach my $f (glob "server/*.c")
288     {
289         next if $f eq "server/trace.c";
290         open FILE, $f or die "Can't open $f";
291         while (<FILE>)
292         {
293             if (/STATUS_(\w+)/)
294             {
295                 $errors{$1} = "STATUS_$1" unless $1 eq "SUCCESS";
296             }
297             elsif (/set_win32_error\s*\(\s*(\w+)\s*\)/)
298             {
299                 $errors{$1} = "0xc0010000 | $1";
300             }
301         }
302         close FILE;
303     }
304     return %errors;
305 }
306
307 # update a file if changed
308 sub update_file($)
309 {
310     my $file = shift;
311     my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
312     if (!$ret)
313     {
314         unlink "$file.new";
315     }
316     else
317     {
318         rename "$file.new", "$file";
319         print "$file updated\n";
320     }
321     return $ret;
322 }
323
324 # replace some lines in a file between two markers
325 sub replace_in_file($$$@)
326 {
327     my $file = shift;
328     my $start = shift;
329     my $end = shift;
330
331     open NEW_FILE, ">$file.new" or die "cannot create $file.new";
332
333     if (defined($start))
334     {
335         open OLD_FILE, "$file" or die "cannot open $file";
336         while (<OLD_FILE>)
337         {
338             print NEW_FILE $_;
339             last if /$start/;
340         }
341     }
342
343     print NEW_FILE "\n", @_, "\n";
344
345     if (defined($end))
346     {
347         my $skip=1;
348         while (<OLD_FILE>)
349         {
350             $skip = 0 if /$end/;
351             print NEW_FILE $_ unless $skip;
352         }
353     }
354
355     close OLD_FILE if defined($start);
356     close NEW_FILE;
357     return update_file($file);
358 }
359
360 ### Main
361
362 # Get the server protocol version
363 my $protocol = GET_PROTOCOL_VERSION();
364
365 my %errors = GET_ERROR_NAMES();
366
367 ### Create server_protocol.h and print header
368
369 open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
370 print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
371 print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
372 print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
373 print SERVER_PROT " */\n\n";
374 print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
375 print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
376
377 ### Parse requests to find request/reply structure definitions
378
379 PARSE_REQUESTS();
380
381 ### Build the request list and structures
382
383 print SERVER_PROT "\n\nenum request\n{\n";
384 foreach my $req (@requests) { print SERVER_PROT "    REQ_$req,\n"; }
385 print SERVER_PROT "    REQ_NB_REQUESTS\n};\n\n";
386
387 print SERVER_PROT "union generic_request\n{\n";
388 print SERVER_PROT "    struct request_max_size max_size;\n";
389 print SERVER_PROT "    struct request_header request_header;\n";
390 foreach my $req (@requests) { print SERVER_PROT "    struct ${req}_request ${req}_request;\n"; }
391 print SERVER_PROT "};\n";
392
393 print SERVER_PROT "union generic_reply\n{\n";
394 print SERVER_PROT "    struct request_max_size max_size;\n";
395 print SERVER_PROT "    struct reply_header reply_header;\n";
396 foreach my $req (@requests) { print SERVER_PROT "    struct ${req}_reply ${req}_reply;\n"; }
397 print SERVER_PROT "};\n\n";
398
399 printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol + 1;
400 print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
401 close SERVER_PROT;
402 update_file( "include/wine/server_protocol.h" );
403
404 ### Output the dumping function tables
405
406 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
407 foreach my $req (@requests)
408 {
409     push @trace_lines, "    (dump_func)dump_${req}_request,\n";
410 }
411 push @trace_lines, "};\n\n";
412
413 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
414 foreach my $req (@requests)
415 {
416     push @trace_lines, "    ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
417 }
418 push @trace_lines, "};\n\n";
419
420 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
421 foreach my $req (@requests)
422 {
423     push @trace_lines, "    \"$req\",\n";
424 }
425 push @trace_lines, "};\n\n";
426
427 push @trace_lines, "static const struct\n{\n";
428 push @trace_lines, "    const char  *name;\n";
429 push @trace_lines, "    unsigned int value;\n";
430 push @trace_lines, "} status_names[] =\n{\n";
431
432 foreach my $err (sort keys %errors)
433 {
434     push @trace_lines, sprintf("    { %-30s %s },\n", "\"$err\",", $errors{$err});
435 }
436 push @trace_lines, "    { NULL, 0 }\n";
437 push @trace_lines, "};\n";
438
439 replace_in_file( "server/trace.c",
440                  "### make_requests begin ###",
441                  "### make_requests end ###",
442                  @trace_lines );
443
444 ### Output the request handlers list
445
446 my @request_lines = ();
447
448 foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
449 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
450 push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
451 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
452 foreach my $req (@requests)
453 {
454     push @request_lines, "    (req_handler)req_$req,\n";
455 }
456 push @request_lines, "};\n\n";
457
458 foreach my $type (sort keys %formats)
459 {
460     my $size = ${$formats{$type}}[0];
461     push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
462 }
463 push @request_lines, @asserts;
464 push @request_lines, "\n#endif  /* WANT_REQUEST_HANDLERS */\n";
465
466 replace_in_file( "server/request.h",
467                  "### make_requests begin ###",
468                  "### make_requests end ###",
469                  @request_lines );