Redesign of the server communication protocol to allow arbitrary sized
[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
9 %formats =
10 (
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",
25 );
26
27 my @requests = ();
28 my %replies = ();
29
30 my @trace_lines = ();
31
32 # Get the server protocol version
33 my $protocol = &GET_PROTOCOL_VERSION;
34
35 ### Create server_protocol.h and print header
36
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";
44
45 ### Parse requests to find request/reply structure definitions
46
47 &PARSE_REQUESTS;
48
49 ### Build the request list and structures
50
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";
54
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";
60
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";
66
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;
70
71 ### Output the dumping function tables
72
73 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
74 foreach $req (@requests)
75 {
76     push @trace_lines, "    (dump_func)dump_${req}_request,\n";
77 }
78 push @trace_lines, "};\n\n";
79
80 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
81 foreach $req (@requests)
82 {
83     push @trace_lines, "    (dump_func)", $replies{$req} ? "dump_${req}_reply,\n" : "0,\n";
84 }
85 push @trace_lines, "};\n\n";
86
87 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
88 foreach $req (@requests)
89 {
90     push @trace_lines, "    \"$req\",\n";
91 }
92 push @trace_lines, "};\n";
93
94 REPLACE_IN_FILE( "server/trace.c", @trace_lines );
95
96 ### Output the request handlers list
97
98 my @request_lines = ();
99
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)
105 {
106     push @request_lines, "    (req_handler)req_$req,\n";
107 }
108 push @request_lines, "};\n#endif  /* WANT_REQUEST_HANDLERS */\n";
109
110 REPLACE_IN_FILE( "server/request.h", @request_lines );
111
112 ### Parse the request definitions
113
114 sub PARSE_REQUESTS
115 {
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 = ();
121
122     open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";
123
124     while (<PROTOCOL>)
125     {
126         my ($type, $var);
127         # strip comments
128         s!/\*.*\*/!!g;
129         # strip white space at end of line
130         s/\s+$//;
131
132         if (/^\@HEADER/)
133         {
134             die "Misplaced \@HEADER" unless $state == 0;
135             $state++;
136             next;
137         }
138
139         # ignore everything while in state 0
140         next if $state == 0;
141
142         if (/^\@REQ\(\s*(\w+)\s*\)/)
143         {
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;
153         }
154
155         if (/^\@REPLY/)
156         {
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;
163         }
164
165         if (/^\@END/)
166         {
167             die "Misplaced \@END" unless ($state == 2 || $state == 3);
168             print SERVER_PROT "};\n";
169
170             if ($state == 2)  # build dummy reply struct
171             {
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
177             # got a complete request
178             push @requests, $name;
179             &DO_DUMP_FUNC( $name, "request", @in_struct);
180             if ($#out_struct >= 0)
181             {
182                 $replies{$name} = 1;
183                 &DO_DUMP_FUNC( $name, "reply", @out_struct);
184             }
185             $state = 1;
186             next;
187         }
188
189         if ($state != 1)
190         {
191             # skip empty lines (but keep them in output file)
192             if (/^$/)
193             {
194                 print SERVER_PROT "\n";
195                 next;
196             }
197
198             if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
199             {
200                 $var = $1;
201                 $type = "dump_varargs_" . $2 . "( min(cur_size,req->" . $3 . ") )";
202                 s!(VARARG\(.*\)\s*;)!/* $1 */!;
203             }
204             elsif (/^\s*VARARG\((\w+),(\w+)\)/)
205             {
206                 $var = $1;
207                 $type = "dump_varargs_" . $2 . "( cur_size )";
208                 s!(VARARG\(.*\)\s*;)!/* $1 */!;
209             }
210             elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
211             {
212                 $type = $1;
213                 $var = $3;
214                 die "Unrecognized type $type" unless defined($formats{$type});
215             }
216             else
217             {
218                 die "Unrecognized syntax $_";
219             }
220             if ($state == 2) { push @in_struct, $type, $var; }
221             if ($state == 3) { push @out_struct, $type, $var; }
222         }
223
224         # Pass it through into the output file
225         print SERVER_PROT $_ . "\n";
226     }
227     close PROTOCOL;
228 }
229
230 ### Generate a dumping function
231
232 sub DO_DUMP_FUNC
233 {
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)
238     {
239         my $type = shift;
240         my $var = shift;
241         if (defined($formats{$type}))
242         {
243             if ($formats{$type} =~ /^&(.*)/)
244             {
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);
249             }
250             else
251             {
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";
256             }
257         }
258         else  # must be some varargs format
259         {
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);
264         }
265     }
266     push @trace_lines, "}\n\n";
267 }
268
269 ### Retrieve the server protocol version from the existing server_protocol.h file
270
271 sub GET_PROTOCOL_VERSION
272 {
273     my $protocol = 0;
274     open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
275     while (<SERVER_PROT>)
276     {
277         if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
278     }
279     close SERVER_PROT;
280     return $protocol;
281 }
282
283 ### Replace the contents of a file between ### make_requests ### marks
284
285 sub REPLACE_IN_FILE
286 {
287     my $name = shift;
288     my @data = @_;
289     my @lines = ();
290     open(FILE,$name) or die "Can't open $name";
291     while (<FILE>)
292     {
293         push @lines, $_;
294         last if /\#\#\# make_requests begin \#\#\#/;
295     }
296     push @lines, "\n", @data;
297     while (<FILE>)
298     {
299         if (/\#\#\# make_requests end \#\#\#/) { push @lines, "\n", $_; last; }
300     }
301     push @lines, <FILE>;
302     open(FILE,">$name") or die "Can't modify $name";
303     print FILE @lines;
304     close(FILE);
305 }