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
13 "unsigned char" => "%02x",
14 "unsigned short"=> "%04x",
15 "unsigned int" => "%08x",
26 # Get the server protocol version
27 my $protocol = &GET_PROTOCOL_VERSION;
29 ### Create server_protocol.h and print header
31 open SERVER_PROT, ">include/wine/server_protocol.h" or die "Cannot create include/wine/server_protocol.h";
32 print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
33 print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
34 print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
35 print SERVER_PROT " */\n\n";
36 print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
37 print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
39 ### Parse requests to find request/reply structure definitions
43 ### Build the request list
45 print SERVER_PROT "\n\nenum request\n{\n";
46 foreach $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
47 print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
48 print SERVER_PROT "union generic_request\n{\n";
49 print SERVER_PROT " struct request_max_size max_size;\n";
50 print SERVER_PROT " struct request_header header;\n";
51 foreach $req (@requests) { print SERVER_PROT " struct ${req}_request $req;\n"; }
52 print SERVER_PROT "};\n\n";
53 printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol + 1;
54 print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
57 ### Output the dumping function tables
59 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
60 foreach $req (@requests)
62 push @trace_lines, " (dump_func)dump_${req}_request,\n";
64 push @trace_lines, "};\n\n";
66 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
67 foreach $req (@requests)
69 push @trace_lines, " (dump_func)", $replies{$req} ? "dump_${req}_reply,\n" : "0,\n";
71 push @trace_lines, "};\n\n";
73 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
74 foreach $req (@requests)
76 push @trace_lines, " \"$req\",\n";
78 push @trace_lines, "};\n";
80 REPLACE_IN_FILE( "server/trace.c", @trace_lines );
82 ### Output the request handlers list
84 my @request_lines = ();
86 foreach $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
87 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
88 push @request_lines, "typedef void (*req_handler)( void *req );\n";
89 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
90 foreach $req (@requests)
92 push @request_lines, " (req_handler)req_$req,\n";
94 push @request_lines, "};\n#endif /* WANT_REQUEST_HANDLERS */\n";
96 REPLACE_IN_FILE( "server/request.h", @request_lines );
98 ### Parse the request definitions
102 # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
108 open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
115 # strip white space at end of line
120 die "Misplaced \@HEADER" unless $state == 0;
125 # ignore everything while in state 0
128 if (/^\@REQ\(\s*(\w+)\s*\)/)
131 die "Misplaced \@REQ" unless $state == 1;
132 # start a new request
135 print SERVER_PROT "struct ${name}_request\n{\n";
136 print SERVER_PROT " struct request_header __header;\n";
143 die "Misplaced \@REPLY" unless $state == 2;
150 die "Misplaced \@END" unless ($state == 2 || $state == 3);
151 print SERVER_PROT "};\n";
153 # got a complete request
154 push @requests, $name;
155 &DO_DUMP_FUNC( $name, "request", @in_struct);
156 if ($#out_struct >= 0)
159 &DO_DUMP_FUNC( $name, "reply", @out_struct);
167 # skip empty lines (but keep them in output file)
170 print SERVER_PROT "\n";
174 if (/^\s*VARARG\((\w+),(\w+)\)/)
177 $type = "&dump_varargs_" . $2;
178 s!(VARARG\(.*\)\s*;)!/* $1 */!;
180 elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+)(\[[1]\])?;/)
182 $type = $1 . ($4 || "");
184 die "Unrecognized type $type" unless (defined($formats{$type}) || $4);
188 die "Unrecognized syntax $_";
190 if ($state == 2) { push @in_struct, $type, $var; }
191 if ($state == 3) { push @out_struct, $type, $var; }
194 # Pass it through into the output file
195 print SERVER_PROT $_ . "\n";
200 ### Generate a dumping function
206 push @trace_lines, "static void dump_${name}_$req( const struct ${name}_request *req )\n{\n";
211 if (defined($formats{$type}))
213 if ($formats{$type} =~ /^&(.*)/)
216 push @trace_lines, " fprintf( stderr, \" $var=\" );\n";
217 if ($type =~ /[1]/) { push @trace_lines, " $func( req, req->$var );\n"; }
218 else { push @trace_lines, " $func( req, &req->$var );\n"; }
219 push @trace_lines, " fprintf( stderr, \",\" );\n" if ($#_ > 0);
223 push @trace_lines, " fprintf( stderr, \" $var=$formats{$type}";
224 push @trace_lines, "," if ($#_ > 0);
225 push @trace_lines, "\", ";
226 push @trace_lines, "req->$var );\n";
229 else # must be some varargs format
231 if ($type =~ /^&(.*)/)
234 push @trace_lines, " fprintf( stderr, \" $var=\" );\n";
235 push @trace_lines, " cur_pos += $func( req );\n";
236 push @trace_lines, " fputc( ',', stderr );\n" if ($#_ > 0);
240 push @trace_lines, " fprintf( stderr, \" $var=\" );\n";
241 push @trace_lines, " dump_varargs_${name}_${req}( req );\n";
245 push @trace_lines, "}\n\n";
248 ### Retrieve the server protocol version from the existing server_protocol.h file
250 sub GET_PROTOCOL_VERSION
253 open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
254 while (<SERVER_PROT>)
256 if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
262 ### Replace the contents of a file between ### make_requests ### marks
269 open(FILE,$name) or die "Can't open $name";
273 last if /\#\#\# make_requests begin \#\#\#/;
275 push @lines, "\n", @data;
278 if (/\#\#\# make_requests end \#\#\#/) { push @lines, "\n", $_; last; }
281 open(FILE,">$name") or die "Can't modify $name";