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
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 "mod_handle_t" => [ 4, 4, "%p" ],
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 "timeout_t" => [ 8, 8, "&dump_timeout" ],
45 "rectangle_t" => [ 16, 4, "&dump_rectangle" ],
46 "char_info_t" => [ 4, 2, "&dump_char_info" ],
47 "apc_call_t" => [ 40, 8, "&dump_apc_call" ],
48 "apc_result_t" => [ 40, 8, "&dump_apc_result" ],
49 "async_data_t" => [ 32, 8, "&dump_async_data" ],
50 "luid_t" => [ 8, 4, "&dump_luid" ],
51 "ioctl_code_t" => [ 4, 4, "&dump_ioctl_code" ],
59 my $max_req_size = 64;
61 my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
63 ### Generate a dumping function
69 push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
74 if (defined($formats{$type}))
76 my $fmt = ${$formats{$type}}[2];
80 push @trace_lines, " fprintf( stderr, \" $var=\" );\n";
81 push @trace_lines, " $func( &req->$var );\n";
82 push @trace_lines, " fprintf( stderr, \",\" );\n" if ($#_ > 0);
84 elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
86 my ($format, $cast) = ($1, $2);
87 push @trace_lines, " fprintf( stderr, \" $var=$format";
88 push @trace_lines, "," if ($#_ > 0);
89 push @trace_lines, "\", ($cast)req->$var );\n";
93 push @trace_lines, " fprintf( stderr, \" $var=$fmt";
94 push @trace_lines, "," if ($#_ > 0);
95 push @trace_lines, "\", req->$var );\n";
98 else # must be some varargs format
101 push @trace_lines, " fprintf( stderr, \" $var=\" );\n";
102 push @trace_lines, " $func;\n";
103 push @trace_lines, " fputc( ',', stderr );\n" if ($#_ > 0);
106 push @trace_lines, "}\n\n";
109 ### Parse the request definitions
113 # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
120 open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
127 # strip white space at end of line
132 die "Misplaced \@HEADER" unless $state == 0;
137 # ignore everything while in state 0
140 if (/^\@REQ\(\s*(\w+)\s*\)/)
143 die "Misplaced \@REQ" unless $state == 1;
144 # start a new request
148 print SERVER_PROT "struct ${name}_request\n{\n";
149 print SERVER_PROT " struct request_header __header;\n";
156 die "Misplaced \@REPLY" unless $state == 2;
157 print SERVER_PROT "};\n";
158 print SERVER_PROT "struct ${name}_reply\n{\n";
159 print SERVER_PROT " struct reply_header __header;\n";
160 die "request $name too large ($offset)" if ($offset > $max_req_size);
168 die "Misplaced \@END" unless ($state == 2 || $state == 3);
169 print SERVER_PROT "};\n";
171 if ($state == 2) # build dummy reply struct
173 die "request $name too large ($offset)" if ($offset > $max_req_size);
174 print SERVER_PROT "struct ${name}_reply\n{\n";
175 print SERVER_PROT " struct reply_header __header;\n";
176 print SERVER_PROT "};\n";
180 die "reply $name too large ($offset)" if ($offset > $max_req_size);
182 # got a complete request
183 push @requests, $name;
184 DO_DUMP_FUNC( $name, "request", @in_struct);
185 if ($#out_struct >= 0)
188 DO_DUMP_FUNC( $name, "reply", @out_struct);
196 # skip empty lines (but keep them in output file)
199 print SERVER_PROT "\n";
203 if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
206 $type = "dump_varargs_" . $2 . "( min(cur_size,req->" . $3 . ") )";
207 s!(VARARG\(.*\)\s*;)!/* $1 */!;
209 elsif (/^\s*VARARG\((\w+),(\w+)\)/)
212 $type = "dump_varargs_" . $2 . "( cur_size )";
213 s!(VARARG\(.*\)\s*;)!/* $1 */!;
215 elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
219 die "Unrecognized type $type" unless defined($formats{$type});
220 my @fmt = @{$formats{$type}};
221 if ($offset & ($fmt[1] - 1))
223 print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
225 $offset = ($offset + $fmt[1] - 1) & ~($fmt[1] - 1);
230 die "Unrecognized syntax $_";
232 if ($state == 2) { push @in_struct, $type, $var; }
233 if ($state == 3) { push @out_struct, $type, $var; }
236 # Pass it through into the output file
237 print SERVER_PROT $_ . "\n";
242 ### Retrieve the server protocol version from the existing server_protocol.h file
244 sub GET_PROTOCOL_VERSION()
247 open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
248 while (<SERVER_PROT>)
250 if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
256 ### Retrieve the list of status and errors used in the server
258 sub GET_ERROR_NAMES()
261 foreach my $f (glob "server/*.c")
263 next if $f eq "server/trace.c";
264 open FILE, $f or die "Can't open $f";
269 $errors{$1} = "STATUS_$1" unless $1 eq "SUCCESS";
271 elsif (/set_win32_error\s*\(\s*(\w+)\s*\)/)
273 $errors{$1} = "0xc0010000 | $1";
281 # update a file if changed
285 my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
292 rename "$file.new", "$file";
293 print "$file updated\n";
298 # replace some lines in a file between two markers
299 sub replace_in_file($$$@)
305 open NEW_FILE, ">$file.new" or die "cannot create $file.new";
309 open OLD_FILE, "$file" or die "cannot open $file";
317 print NEW_FILE "\n", @_, "\n";
325 print NEW_FILE $_ unless $skip;
329 close OLD_FILE if defined($start);
331 return update_file($file);
336 # Get the server protocol version
337 my $protocol = GET_PROTOCOL_VERSION();
339 my %errors = GET_ERROR_NAMES();
341 ### Create server_protocol.h and print header
343 open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
344 print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
345 print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
346 print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
347 print SERVER_PROT " */\n\n";
348 print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
349 print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
351 ### Parse requests to find request/reply structure definitions
355 ### Build the request list and structures
357 print SERVER_PROT "\n\nenum request\n{\n";
358 foreach my $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
359 print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
361 print SERVER_PROT "union generic_request\n{\n";
362 print SERVER_PROT " struct request_max_size max_size;\n";
363 print SERVER_PROT " struct request_header request_header;\n";
364 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
365 print SERVER_PROT "};\n";
367 print SERVER_PROT "union generic_reply\n{\n";
368 print SERVER_PROT " struct request_max_size max_size;\n";
369 print SERVER_PROT " struct reply_header reply_header;\n";
370 foreach my $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
371 print SERVER_PROT "};\n\n";
373 printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol + 1;
374 print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
376 update_file( "include/wine/server_protocol.h" );
378 ### Output the dumping function tables
380 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
381 foreach my $req (@requests)
383 push @trace_lines, " (dump_func)dump_${req}_request,\n";
385 push @trace_lines, "};\n\n";
387 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
388 foreach my $req (@requests)
390 push @trace_lines, " ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
392 push @trace_lines, "};\n\n";
394 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
395 foreach my $req (@requests)
397 push @trace_lines, " \"$req\",\n";
399 push @trace_lines, "};\n\n";
401 push @trace_lines, "static const struct\n{\n";
402 push @trace_lines, " const char *name;\n";
403 push @trace_lines, " unsigned int value;\n";
404 push @trace_lines, "} status_names[] =\n{\n";
406 foreach my $err (sort keys %errors)
408 push @trace_lines, sprintf(" { %-30s %s },\n", "\"$err\",", $errors{$err});
410 push @trace_lines, " { NULL, 0 }\n";
411 push @trace_lines, "};\n";
413 replace_in_file( "server/trace.c",
414 "### make_requests begin ###",
415 "### make_requests end ###",
418 ### Output the request handlers list
420 my @request_lines = ();
422 foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
423 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
424 push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
425 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
426 foreach my $req (@requests)
428 push @request_lines, " (req_handler)req_$req,\n";
430 push @request_lines, "};\n#endif /* WANT_REQUEST_HANDLERS */\n";
432 replace_in_file( "server/request.h",
433 "### make_requests begin ###",
434 "### make_requests end ###",