Added support for FreeBSD 3.0 to DEBUG_checkmap_bad().
[wine/wine-kai.git] / tools / make_requests
blob0b0f69f77f6bf3f666c66b38e2e0f908e5d5e242
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 "long" => "%ld",
13 "char" => "%c",
14 "char[0]" => "\\\"%.*s\\\"",
15 "unsigned int" => "%08x",
16 "void*" => "%p",
17 "time_t" => "%ld"
20 my @requests = ();
21 my %replies = ();
23 open(SERVER,"include/server.h") or die "Can't open include/server.h";
24 open(TRACE,">server/trace.c") or die "Can't create server/trace.c";
25 open(REQUESTS,">include/server/request.h") or die "Can't create include/server/request.h";
27 ### Generate the header
29 print TRACE <<EOF;
30 /* File generated automatically by $0; DO NOT EDIT!! */
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <sys/uio.h>
35 #include "server.h"
36 #include "thread.h"
37 EOF
39 ### Parse server.h to find request/reply structure definitions
41 while (<SERVER>)
43 if (/^struct +(\w+)_request/) { &DO_REQUEST($1); }
44 if (/^struct +(\w+)_reply/) { &DO_REPLY($1); }
47 ### Output the dumping function tables
49 print TRACE<<EOF;
51 struct dumper
53 int (*dump_req)( void *data, int len );
54 void (*dump_reply)( void *data );
57 static const struct dumper dumpers[REQ_NB_REQUESTS] =
59 EOF
61 foreach $req (@requests)
63 $request = $req . "_request";
64 $reply = $replies{$req} ? "dump_${req}_reply" : "0";
65 print TRACE " { (int(*)(void *,int))dump_$request,\n";
66 print TRACE " (void(*)())$reply },\n";
69 print TRACE <<EOF;
72 static const char * const req_names[REQ_NB_REQUESTS] =
74 EOF
75 foreach $req (@requests)
77 print TRACE " \"$req\",\n";
80 ### Output the tracing functions
82 print TRACE <<EOF;
85 void trace_request( enum request req, void *data, int len, int fd )
87 int size;
88 current->last_req = req;
89 fprintf( stderr, "%08x: %s(", (unsigned int)current, req_names[req] );
90 size = dumpers[req].dump_req( data, len );
91 if ((len -= size) > 0)
93 unsigned char *ptr = (unsigned char *)data + size;
94 while (len--) fprintf( stderr, ", %02x", *ptr++ );
96 if (fd != -1) fprintf( stderr, " ) fd=%d\\n", fd );
97 else fprintf( stderr, " )\\n" );
100 void trace_timeout(void)
102 fprintf( stderr, "%08x: *timeout*\\n", (unsigned int)current );
105 void trace_kill( int exit_code )
107 fprintf( stderr,"%08x: *killed* exit_code=%d\\n",
108 (unsigned int)current, exit_code );
111 void trace_reply( struct thread *thread, int type, int pass_fd,
112 struct iovec *vec, int veclen )
114 if (!thread) return;
115 fprintf( stderr, "%08x: %s() = %d",
116 (unsigned int)thread, req_names[thread->last_req], type );
117 if (veclen)
119 fprintf( stderr, " {" );
120 if (dumpers[thread->last_req].dump_reply)
122 dumpers[thread->last_req].dump_reply( vec->iov_base );
123 vec++;
124 veclen--;
126 for (; veclen; veclen--, vec++)
128 unsigned char *ptr = vec->iov_base;
129 int len = vec->iov_len;
130 while (len--) fprintf( stderr, ", %02x", *ptr++ );
132 fprintf( stderr, " }" );
134 if (pass_fd != -1) fprintf( stderr, " fd=%d\\n", pass_fd );
135 else fprintf( stderr, "\\n" );
139 ### Output the requests list
141 print REQUESTS <<EOF;
142 /* File generated automatically by $0; DO NOT EDIT!! */
144 #ifndef __WINE_SERVER_REQUEST_H
145 #define __WINE_SERVER_REQUEST_H
147 enum request
151 foreach $req (@requests)
153 print REQUESTS " REQ_\U$req,\n";
156 print REQUESTS <<EOF;
157 REQ_NB_REQUESTS
160 #ifdef WANT_REQUEST_HANDLERS
164 foreach $req (@requests) { print REQUESTS "DECL_HANDLER($req);\n"; }
166 print REQUESTS <<EOF;
168 static const struct handler {
169 void (*handler)();
170 unsigned int min_size;
171 } req_handlers[REQ_NB_REQUESTS] = {
174 foreach $req (@requests)
176 print REQUESTS " { (void(*)())req_$req, sizeof(struct ${req}_request) },\n";
179 print REQUESTS <<EOF;
181 #endif /* WANT_REQUEST_HANDLERS */
183 #endif /* __WINE_SERVER_REQUEST_H */
186 ### Handle a request structure definition
188 sub DO_REQUEST
190 my $name = shift;
191 my @struct = ();
192 while (<SERVER>)
194 last if /^};$/;
195 next if /^{$/;
196 s!/\*.*\*/!!g;
197 next if /^\s*$/;
198 / *(\w+\**( +\w+\**)*) +(\w+)(\[0\])?;/ or die "Unrecognized syntax $_";
199 my $type = $1 . ($4 || "");
200 my $var = $3;
201 die "Unrecognized type $type" unless defined($formats{$type});
202 push @struct, $type, $var;
204 push @requests, $name;
205 &DO_DUMP_FUNC( $name . "_request",@struct);
208 ### Handle a reply structure definition
210 sub DO_REPLY
212 my $name = shift;
213 my @struct = ();
214 while (<SERVER>)
216 last if /^};$/;
217 next if /^{$/;
218 s!/\*.*\*/!!g;
219 next if /^\s*$/;
220 / *(\w+\**( +\w+\**)*) +(\w+)(\[0\])?;/ or die "Unrecognized syntax $_";
221 my $type = $1 . ($4 || "");
222 my $var = $3;
223 die "Unrecognized type $type" unless defined($formats{$type});
224 push @struct, $type, $var;
226 $replies{$name} = 1;
227 &DO_DUMP_FUNC( $name . "_reply" ,@struct);
230 ### Generate a dumping function
232 sub DO_DUMP_FUNC
234 my $vararg = 0;
235 my $name = shift;
236 print TRACE "\nstatic int dump_$name( struct $name *req, int len )\n{\n";
237 while ($#_ >= 0)
239 my $type = shift;
240 my $var = shift;
241 print TRACE " fprintf( stderr, \" $var=$formats{$type}";
242 print TRACE "," if ($#_ > 0);
243 print TRACE "\", ";
244 if ($type =~ s/\[0\]$//g) # vararg type?
246 $vararg = 1;
247 print TRACE "len - (int)sizeof(*req), ($type *)(req+1) );\n";
249 else
251 print TRACE "req->$var );\n";
254 print TRACE " return ", $vararg ? "len" : "(int)sizeof(*req)", ";\n}\n";