Added AvgCharWidth member to font metrics.
[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/server.h.
5 #
6 # Copyright (C) 1998 Alexandre Julliard
7 #
8
9 %formats =
10 (
11     "int"           => "%d",
12     "char"          => "%c",
13     "unsigned char" => "%02x",
14     "unsigned int"  => "%08x",
15     "void*"         => "%p",
16     "time_t"        => "%ld",
17     "handle_t"      => "%d",
18 );
19
20 my @requests = ();
21 my %replies = ();
22
23 open(SERVER,"include/server.h") or die "Can't open include/server.h";
24
25 ### Parse server.h to find request/reply structure definitions
26
27 my @trace_lines = ();
28 my $protocol = 0;  # server protocol version
29
30 while (<SERVER>)
31 {
32     if (/^struct +(\w+)_request/) { &DO_REQUEST($1); }
33     if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1 + 1; }
34 }
35
36 ### Output the dumping function tables
37
38 push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
39 foreach $req (@requests)
40 {
41     push @trace_lines, "    (dump_func)dump_${req}_request,\n";
42 }
43 push @trace_lines, "};\n\n";
44
45 push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
46 foreach $req (@requests)
47 {
48     push @trace_lines, "    (dump_func)", $replies{$req} ? "dump_${req}_reply,\n" : "0,\n";
49 }
50 push @trace_lines, "};\n\n";
51
52 push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
53 foreach $req (@requests)
54 {
55     push @trace_lines, "    \"$req\",\n";
56 }
57 push @trace_lines, "};\n";
58
59 REPLACE_IN_FILE( "server/trace.c", @trace_lines );
60
61 ### Replace the request list in server.h by the new values
62
63 my @server_lines = ();
64
65 push @server_lines, "enum request\n{\n";
66 foreach $req (@requests) { push @server_lines, "    REQ_$req,\n"; }
67 push @server_lines, "    REQ_NB_REQUESTS\n};\n\n";
68 push @server_lines, "union generic_request\n{\n";
69 push @server_lines, "    struct request_max_size max_size;\n";
70 push @server_lines, "    struct request_header header;\n";
71 foreach $req (@requests) { push @server_lines, "    struct ${req}_request $req;\n"; }
72 push @server_lines, "};\n\n";
73 push @server_lines, "#define SERVER_PROTOCOL_VERSION $protocol\n";
74
75 REPLACE_IN_FILE( "include/server.h", @server_lines );
76
77 ### Output the request handlers list
78
79 my @request_lines = ();
80
81 foreach $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
82 push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
83 push @request_lines, "typedef void (*req_handler)( void *req );\n";
84 push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
85 foreach $req (@requests)
86 {
87     push @request_lines, "    (req_handler)req_$req,\n";
88 }
89 push @request_lines, "};\n#endif  /* WANT_REQUEST_HANDLERS */\n";
90
91 REPLACE_IN_FILE( "server/request.h", @request_lines );
92
93 ### Handle a request structure definition
94
95 sub DO_REQUEST
96 {
97     my $name = shift;
98     my @in_struct = ();
99     my @out_struct = ();
100     my $got_header = 0;
101     while (<SERVER>)
102     {
103         my ($dir, $type, $var);
104         last if /^};$/;
105         next if /^{$/;
106         s!/\*.*\*/!!g;
107         next if /^\s*$/;
108         if (/REQUEST_HEADER/)
109         {
110             die "Duplicated header" if $got_header;
111             die "Header must be first" if ($#in_struct != -1 || $#out_struct != -1);
112             $got_header++;
113             next;
114         }
115         if (/^\s*(IN|OUT)\s*VARARG\((\w+),(\w+)\)/)
116         {
117             $dir = $1;
118             $var = $2;
119             $type = "&dump_varargs_" . $3;
120         }
121         elsif (/^\s*(IN|OUT)\s*(\w+\**(\s+\w+\**)*)\s+(\w+)(\[[1]\])?;/)
122         {
123             $dir = $1;
124             $type = $2 . ($5 || "");
125             $var = $4;
126             die "Unrecognized type $type" unless (defined($formats{$type}) || $5);
127         }
128         else
129         {
130             die "Unrecognized syntax $_";
131         }
132         if ($dir =~ /IN/) { push @in_struct, $type, $var; }
133         if ($dir =~ /OUT/) { push @out_struct, $type, $var; }
134     }
135     die "Missing header" unless $got_header;
136     push @requests, $name;
137     &DO_DUMP_FUNC( $name, "request", @in_struct);
138     if ($#out_struct >= 0)
139     {
140         $replies{$name} = 1;
141         &DO_DUMP_FUNC( $name, "reply", @out_struct);
142     }
143 }
144
145 ### Generate a dumping function
146
147 sub DO_DUMP_FUNC
148 {
149     my $name = shift;
150     my $req = shift;
151     push @trace_lines, "static void dump_${name}_$req( const struct ${name}_request *req )\n{\n";
152     while ($#_ >= 0)
153     {
154         my $type = shift;
155         my $var = shift;
156         if (defined($formats{$type}))
157         {
158             if ($formats{$type} =~ /^&(.*)/)
159             {
160                 my $func = $1;
161                 push @trace_lines, "    fprintf( stderr, \" $var=\" );\n";
162                 if ($type =~ /[1]/) { push @trace_lines, "    $func( req, req->$var );\n"; }
163                 else { push @trace_lines, "    $func( req, &req->$var );\n"; }
164                 push @trace_lines, "    fprintf( stderr, \",\" );\n" if ($#_ > 0);
165             }
166             else
167             {
168                 push @trace_lines, "    fprintf( stderr, \" $var=$formats{$type}";
169                 push @trace_lines, "," if ($#_ > 0);
170                 push @trace_lines, "\", ";
171                 push @trace_lines, "req->$var );\n";
172             }
173         }
174         else  # must be some varargs format
175         {
176             if ($type =~ /^&(.*)/)
177             {
178                 my $func = $1;
179                 push @trace_lines, "    fprintf( stderr, \" $var=\" );\n";
180                 push @trace_lines, "    cur_pos += $func( req );\n";
181                 push @trace_lines, "    fputc( ',', stderr );\n" if ($#_ > 0);
182             }
183             else
184             {
185                 push @trace_lines, "    fprintf( stderr, \" $var=\" );\n";
186                 push @trace_lines, "    dump_varargs_${name}_${req}( req );\n";
187             }
188         }
189     }
190     push @trace_lines, "}\n\n";
191 }
192
193 ### Replace the contents of a file between ### make_requests ### marks
194
195 sub REPLACE_IN_FILE
196 {
197     my $name = shift;
198     my @data = @_;
199     my @lines = ();
200     open(FILE,$name) or die "Can't open $name";
201     while (<FILE>)
202     {
203         push @lines, $_;
204         last if /\#\#\# make_requests begin \#\#\#/;
205     }
206     push @lines, "\n", @data;
207     while (<FILE>)
208     {
209         if (/\#\#\# make_requests end \#\#\#/) { push @lines, "\n", $_; last; }
210     }
211     push @lines, <FILE>;
212     open(FILE,">$name") or die "Can't modify $name";
213     print FILE @lines;
214     close(FILE);
215 }