Make wineinstall work in the new-autoconf-version world.
[wine/testsucceed.git] / tools / make_requests
blob34cd54d0b53285eec73bc4e03bf053223922c1c4
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
9 %formats =
11 "int" => "%d",
12 "short int" => "%d",
13 "char" => "%c",
14 "unsigned char" => "%02x",
15 "unsigned short"=> "%04x",
16 "unsigned int" => "%08x",
17 "void*" => "%p",
18 "time_t" => "%ld",
19 "size_t" => "%d",
20 "handle_t" => "%d",
21 "atom_t" => "%04x",
22 "user_handle_t" => "%08x",
23 "rectangle_t" => "&dump_rectangle",
24 "char_info_t" => "&dump_char_info",
27 my @requests = ();
28 my %replies = ();
30 my @trace_lines = ();
32 # Get the server protocol version
33 my $protocol = &GET_PROTOCOL_VERSION;
35 ### Create server_protocol.h and print header
37 open SERVER_PROT, ">include/wine/server_protocol.h" or die "Cannot create include/wine/server_protocol.h";
38 print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
39 print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
40 print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
41 print SERVER_PROT " */\n\n";
42 print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
43 print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";
45 ### Parse requests to find request/reply structure definitions
47 &PARSE_REQUESTS;
49 ### Build the request list and structures
51 print SERVER_PROT "\n\nenum request\n{\n";
52 foreach $req (@requests) { print SERVER_PROT " REQ_$req,\n"; }
53 print SERVER_PROT " REQ_NB_REQUESTS\n};\n\n";
55 print SERVER_PROT "union generic_request\n{\n";
56 print SERVER_PROT " struct request_max_size max_size;\n";
57 print SERVER_PROT " struct request_header request_header;\n";
58 foreach $req (@requests) { print SERVER_PROT " struct ${req}_request ${req}_request;\n"; }
59 print SERVER_PROT "};\n";
61 print SERVER_PROT "union generic_reply\n{\n";
62 print SERVER_PROT " struct request_max_size max_size;\n";
63 print SERVER_PROT " struct reply_header reply_header;\n";
64 foreach $req (@requests) { print SERVER_PROT " struct ${req}_reply ${req}_reply;\n"; }
65 print SERVER_PROT "};\n\n";
67 printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol + 1;
68 print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
69 close SERVER_PROT;
71 ### Output the dumping function tables
73 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
74 foreach $req (@requests)
76 push @trace_lines, " (dump_func)dump_${req}_request,\n";
78 push @trace_lines, "};\n\n";
80 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
81 foreach $req (@requests)
83 push @trace_lines, " (dump_func)", $replies{$req} ? "dump_${req}_reply,\n" : "0,\n";
85 push @trace_lines, "};\n\n";
87 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
88 foreach $req (@requests)
90 push @trace_lines, " \"$req\",\n";
92 push @trace_lines, "};\n";
94 REPLACE_IN_FILE( "server/trace.c", @trace_lines );
96 ### Output the request handlers list
98 my @request_lines = ();
100 foreach $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
101 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
102 push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
103 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
104 foreach $req (@requests)
106 push @request_lines, " (req_handler)req_$req,\n";
108 push @request_lines, "};\n#endif /* WANT_REQUEST_HANDLERS */\n";
110 REPLACE_IN_FILE( "server/request.h", @request_lines );
112 ### Parse the request definitions
114 sub PARSE_REQUESTS
116 # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
117 my $state = 0;
118 my $name = "";
119 my @in_struct = ();
120 my @out_struct = ();
122 open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
124 while (<PROTOCOL>)
126 my ($type, $var);
127 # strip comments
128 s!/\*.*\*/!!g;
129 # strip white space at end of line
130 s/\s+$//;
132 if (/^\@HEADER/)
134 die "Misplaced \@HEADER" unless $state == 0;
135 $state++;
136 next;
139 # ignore everything while in state 0
140 next if $state == 0;
142 if (/^\@REQ\(\s*(\w+)\s*\)/)
144 $name = $1;
145 die "Misplaced \@REQ" unless $state == 1;
146 # start a new request
147 @in_struct = ();
148 @out_struct = ();
149 print SERVER_PROT "struct ${name}_request\n{\n";
150 print SERVER_PROT " struct request_header __header;\n";
151 $state++;
152 next;
155 if (/^\@REPLY/)
157 die "Misplaced \@REPLY" unless $state == 2;
158 print SERVER_PROT "};\n";
159 print SERVER_PROT "struct ${name}_reply\n{\n";
160 print SERVER_PROT " struct reply_header __header;\n";
161 $state++;
162 next;
165 if (/^\@END/)
167 die "Misplaced \@END" unless ($state == 2 || $state == 3);
168 print SERVER_PROT "};\n";
170 if ($state == 2) # build dummy reply struct
172 print SERVER_PROT "struct ${name}_reply\n{\n";
173 print SERVER_PROT " struct reply_header __header;\n";
174 print SERVER_PROT "};\n";
177 # got a complete request
178 push @requests, $name;
179 &DO_DUMP_FUNC( $name, "request", @in_struct);
180 if ($#out_struct >= 0)
182 $replies{$name} = 1;
183 &DO_DUMP_FUNC( $name, "reply", @out_struct);
185 $state = 1;
186 next;
189 if ($state != 1)
191 # skip empty lines (but keep them in output file)
192 if (/^$/)
194 print SERVER_PROT "\n";
195 next;
198 if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
200 $var = $1;
201 $type = "dump_varargs_" . $2 . "( min(cur_size,req->" . $3 . ") )";
202 s!(VARARG\(.*\)\s*;)!/* $1 */!;
204 elsif (/^\s*VARARG\((\w+),(\w+)\)/)
206 $var = $1;
207 $type = "dump_varargs_" . $2 . "( cur_size )";
208 s!(VARARG\(.*\)\s*;)!/* $1 */!;
210 elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
212 $type = $1;
213 $var = $3;
214 die "Unrecognized type $type" unless defined($formats{$type});
216 else
218 die "Unrecognized syntax $_";
220 if ($state == 2) { push @in_struct, $type, $var; }
221 if ($state == 3) { push @out_struct, $type, $var; }
224 # Pass it through into the output file
225 print SERVER_PROT $_ . "\n";
227 close PROTOCOL;
230 ### Generate a dumping function
232 sub DO_DUMP_FUNC
234 my $name = shift;
235 my $req = shift;
236 push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
237 while ($#_ >= 0)
239 my $type = shift;
240 my $var = shift;
241 if (defined($formats{$type}))
243 if ($formats{$type} =~ /^&(.*)/)
245 my $func = $1;
246 push @trace_lines, " fprintf( stderr, \" $var=\" );\n";
247 push @trace_lines, " $func( &req->$var );\n";
248 push @trace_lines, " fprintf( stderr, \",\" );\n" if ($#_ > 0);
250 else
252 push @trace_lines, " fprintf( stderr, \" $var=$formats{$type}";
253 push @trace_lines, "," if ($#_ > 0);
254 push @trace_lines, "\", ";
255 push @trace_lines, "req->$var );\n";
258 else # must be some varargs format
260 my $func = $type;
261 push @trace_lines, " fprintf( stderr, \" $var=\" );\n";
262 push @trace_lines, " $func;\n";
263 push @trace_lines, " fputc( ',', stderr );\n" if ($#_ > 0);
266 push @trace_lines, "}\n\n";
269 ### Retrieve the server protocol version from the existing server_protocol.h file
271 sub GET_PROTOCOL_VERSION
273 my $protocol = 0;
274 open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
275 while (<SERVER_PROT>)
277 if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
279 close SERVER_PROT;
280 return $protocol;
283 ### Replace the contents of a file between ### make_requests ### marks
285 sub REPLACE_IN_FILE
287 my $name = shift;
288 my @data = @_;
289 my @lines = ();
290 open(FILE,$name) or die "Can't open $name";
291 while (<FILE>)
293 push @lines, $_;
294 last if /\#\#\# make_requests begin \#\#\#/;
296 push @lines, "\n", @data;
297 while (<FILE>)
299 if (/\#\#\# make_requests end \#\#\#/) { push @lines, "\n", $_; last; }
301 push @lines, <FILE>;
302 open(FILE,">$name") or die "Can't modify $name";
303 print FILE @lines;
304 close(FILE);