Added server-side file mapping object support.
[wine/multimedia.git] / tools / make_requests
blob5c80ecf2277c377b5a524dc0b952bbc37ac97199
1 #! /usr/bin/perl -w
3 # Build the server/trace.c and include/server/request.h files
4 # from the contents of include/server.h.
6 # Copyright (C) 1998 Alexandre Julliard
9 %formats =
11 "int" => "%d",
12 "char" => "%c",
13 "char[0]" => "\\\"%.*s\\\"",
14 "unsigned int" => "%08x",
15 "void*" => "%p",
16 "time_t" => "%ld"
19 my @requests = ();
20 my %replies = ();
22 open(SERVER,"include/server.h") or die "Can't open include/server.h";
23 open(TRACE,">server/trace.c") or die "Can't create server/trace.c";
24 open(REQUESTS,">include/server/request.h") or die "Can't create include/server/request.h";
26 ### Generate the header
28 print TRACE <<EOF;
29 /* File generated automatically by $0; DO NOT EDIT!! */
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include "server.h"
35 #include "server/thread.h"
36 EOF
38 ### Parse server.h to find request/reply structure definitions
40 while (<SERVER>)
42 if (/^struct +(\w+)_request/) { &DO_REQUEST($1); }
43 if (/^struct +(\w+)_reply/) { &DO_REPLY($1); }
46 ### Output the dumping function tables
48 print TRACE<<EOF;
50 struct dumper
52 int (*dump_req)( void *data, int len );
53 void (*dump_reply)( void *data );
56 static const struct dumper dumpers[REQ_NB_REQUESTS] =
58 EOF
60 foreach $req (@requests)
62 $request = $req . "_request";
63 $reply = $replies{$req} ? "dump_${req}_reply" : "0";
64 print TRACE " { (int(*)(void *,int))dump_$request,\n";
65 print TRACE " (void(*)())$reply },\n";
68 print TRACE <<EOF;
71 static const char * const req_names[REQ_NB_REQUESTS] =
73 EOF
74 foreach $req (@requests)
76 print TRACE " \"$req\",\n";
79 ### Output the tracing functions
81 print TRACE <<EOF;
84 void trace_request( enum request req, void *data, int len, int fd )
86 int size;
87 current->last_req = req;
88 printf( "%08x: %s(", (unsigned int)current, req_names[req] );
89 size = dumpers[req].dump_req( data, len );
90 if ((len -= size) > 0)
92 unsigned char *ptr = (unsigned char *)data + size;
93 while (len--) printf( ", %02x", *ptr++ );
95 if (fd != -1) printf( " ) fd=%d\\n", fd );
96 else printf( " )\\n" );
99 void trace_timeout(void)
101 printf( "%08x: *timeout*\\n", (unsigned int)current );
104 void trace_kill( int exit_code )
106 printf( "%08x: *killed* exit_code=%d\\n",
107 (unsigned int)current, exit_code );
110 void trace_reply( struct thread *thread, int type, int pass_fd,
111 struct iovec *vec, int veclen )
113 if (!thread) return;
114 printf( "%08x: %s() = %d",
115 (unsigned int)thread, req_names[thread->last_req], type );
116 if (veclen)
118 printf( " {" );
119 if (dumpers[thread->last_req].dump_reply)
121 dumpers[thread->last_req].dump_reply( vec->iov_base );
122 vec++;
123 veclen--;
125 for (; veclen; veclen--, vec++)
127 unsigned char *ptr = vec->iov_base;
128 int len = vec->iov_len;
129 while (len--) printf( ", %02x", *ptr++ );
131 printf( " }" );
133 if (pass_fd != -1) printf( " fd=%d\\n", pass_fd );
134 else printf( "\\n" );
138 ### Output the requests list
140 print REQUESTS <<EOF;
141 /* File generated automatically by $0; DO NOT EDIT!! */
143 #ifndef __WINE_SERVER_REQUEST_H
144 #define __WINE_SERVER_REQUEST_H
146 enum request
150 foreach $req (@requests)
152 print REQUESTS " REQ_\U$req,\n";
155 print REQUESTS <<EOF;
156 REQ_NB_REQUESTS
159 #ifdef WANT_REQUEST_HANDLERS
161 #define DECL_HANDLER(name) \\
162 static void req_##name( struct name##_request *req, void *data, int len, int fd )
166 foreach $req (@requests) { print REQUESTS "DECL_HANDLER($req);\n"; }
168 print REQUESTS <<EOF;
170 static const struct handler {
171 void (*handler)();
172 unsigned int min_size;
173 } req_handlers[REQ_NB_REQUESTS] = {
176 foreach $req (@requests)
178 print REQUESTS " { (void(*)())req_$req, sizeof(struct ${req}_request) },\n";
181 print REQUESTS <<EOF;
183 #endif /* WANT_REQUEST_HANDLERS */
185 #endif /* __WINE_SERVER_REQUEST_H */
188 ### Handle a request structure definition
190 sub DO_REQUEST
192 my $name = shift;
193 my @struct = ();
194 while (<SERVER>)
196 last if /^};$/;
197 next if /^{$/;
198 s!/\*.*\*/!!g;
199 next if /^\s*$/;
200 / *(\w+\**( +\w+\**)*) +(\w+)(\[0\])?;/ or die "Unrecognized syntax $_";
201 my $type = $1 . ($4 || "");
202 my $var = $3;
203 die "Unrecognized type $type" unless defined($formats{$type});
204 push @struct, $type, $var;
206 push @requests, $name;
207 &DO_DUMP_FUNC( $name . "_request",@struct);
210 ### Handle a reply structure definition
212 sub DO_REPLY
214 my $name = shift;
215 my @struct = ();
216 while (<SERVER>)
218 last if /^};$/;
219 next if /^{$/;
220 s!/\*.*\*/!!g;
221 next if /^\s*$/;
222 / *(\w+\**( +\w+\**)*) +(\w+);/ or die "Unrecognized syntax $_";
223 my $type = $1;
224 my $var = $3;
225 die "Unrecognized type $type" unless defined($formats{$type});
226 push @struct, $type, $var;
228 $replies{$name} = 1;
229 &DO_DUMP_FUNC( $name . "_reply" ,@struct);
232 ### Generate a dumping function
234 sub DO_DUMP_FUNC
236 my $vararg = 0;
237 my $name = shift;
238 print TRACE "\nstatic int dump_$name( struct $name *req, int len )\n{\n";
239 while ($#_ >= 0)
241 my $type = shift;
242 my $var = shift;
243 print TRACE " printf( \" $var=$formats{$type}";
244 print TRACE "," if ($#_ > 0);
245 print TRACE "\", ";
246 if ($type =~ s/\[0\]$//g) # vararg type?
248 $vararg = 1;
249 print TRACE "len - (int)sizeof(*req), ($type *)(req+1) );\n";
251 else
253 print TRACE "req->$var );\n";
256 print TRACE " return ", $vararg ? "len" : "(int)sizeof(*req)", ";\n}\n";