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