make_requests 11.1 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
#! /usr/bin/perl -w
Alexandre Julliard's avatar
Alexandre Julliard committed
2
#
3
# Build the server/trace.c and server/request.h files
4
# from the contents of include/wine/server.h.
Alexandre Julliard's avatar
Alexandre Julliard committed
5 6 7
#
# Copyright (C) 1998 Alexandre Julliard
#
8 9 10 11 12 13 14 15 16 17 18 19
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
20
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21
#
Francois Gouget's avatar
Francois Gouget committed
22
use strict;
Alexandre Julliard's avatar
Alexandre Julliard committed
23

Francois Gouget's avatar
Francois Gouget committed
24
my %formats =
Alexandre Julliard's avatar
Alexandre Julliard committed
25
(
26
    "int"           => "%d",
27
    "short int"     => "%d",
28
    "char"          => "%c",
29
    "unsigned char" => "%02x",
30
    "unsigned short"=> "%04x",
31
    "unsigned int"  => "%08x",
32
    "unsigned long" => "%lx",
33
    "void*"         => "%p",
34 35
    "time_t"        => "%ld (long)",
    "size_t"        => "%lu (unsigned long)",
36
    "data_size_t"   => "%u",
37
    "obj_handle_t"  => "%p",
38
    "atom_t"        => "%04x",
39
    "user_handle_t" => "%p",
40 41
    "process_id_t"  => "%04x",
    "thread_id_t"   => "%04x",
42
    "abs_time_t"    => "&dump_abs_time",
43
    "rectangle_t"   => "&dump_rectangle",
44
    "char_info_t"   => "&dump_char_info",
Alexandre Julliard's avatar
Alexandre Julliard committed
45 46 47 48 49
);

my @requests = ();
my %replies = ();

50
my @trace_lines = ();
Alexandre Julliard's avatar
Alexandre Julliard committed
51

52

Alexandre Julliard's avatar
Alexandre Julliard committed
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
### Generate a dumping function

sub DO_DUMP_FUNC($$@)
{
    my $name = shift;
    my $req = shift;
    push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
    while ($#_ >= 0)
    {
	my $type = shift;
	my $var = shift;
	if (defined($formats{$type}))
	{
            if ($formats{$type} =~ /^&(.*)/)
            {
                my $func = $1;
                push @trace_lines, "    fprintf( stderr, \" $var=\" );\n";
                push @trace_lines, "    $func( &req->$var );\n";
                push @trace_lines, "    fprintf( stderr, \",\" );\n" if ($#_ > 0);
            }
74 75 76 77 78 79 80
            elsif ($formats{$type} =~ /^(%.*)\s+\((.*)\)/)
            {
                my ($format, $cast) = ($1, $2);
                push @trace_lines, "    fprintf( stderr, \" $var=$format";
                push @trace_lines, "," if ($#_ > 0);
                push @trace_lines, "\", ($cast)req->$var );\n";
            }
81 82 83 84
            else
            {
                push @trace_lines, "    fprintf( stderr, \" $var=$formats{$type}";
                push @trace_lines, "," if ($#_ > 0);
85
                push @trace_lines, "\", req->$var );\n";
86 87 88 89 90 91 92 93 94 95 96 97 98
            }
	}
	else  # must be some varargs format
	{
            my $func = $type;
            push @trace_lines, "    fprintf( stderr, \" $var=\" );\n";
            push @trace_lines, "    $func;\n";
            push @trace_lines, "    fputc( ',', stderr );\n" if ($#_ > 0);
        }
    }
    push @trace_lines, "}\n\n";
}

99
### Parse the request definitions
Alexandre Julliard's avatar
Alexandre Julliard committed
100

Francois Gouget's avatar
Francois Gouget committed
101
sub PARSE_REQUESTS()
Alexandre Julliard's avatar
Alexandre Julliard committed
102
{
103 104 105
    # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
    my $state = 0;
    my $name = "";
106 107
    my @in_struct = ();
    my @out_struct = ();
108 109 110 111

    open(PROTOCOL,"server/protocol.def") or die "Can't open server/protocol.def";

    while (<PROTOCOL>)
Alexandre Julliard's avatar
Alexandre Julliard committed
112
    {
113 114
        my ($type, $var);
        # strip comments
Alexandre Julliard's avatar
Alexandre Julliard committed
115
	s!/\*.*\*/!!g;
116 117 118 119 120 121 122 123 124 125 126 127 128 129
        # strip white space at end of line
        s/\s+$//;

        if (/^\@HEADER/)
        {
            die "Misplaced \@HEADER" unless $state == 0;
            $state++;
            next;
        }

        # ignore everything while in state 0
        next if $state == 0;

        if (/^\@REQ\(\s*(\w+)\s*\)/)
130
        {
131 132 133 134 135 136 137 138
            $name = $1;
            die "Misplaced \@REQ" unless $state == 1;
            # start a new request
            @in_struct = ();
            @out_struct = ();
            print SERVER_PROT "struct ${name}_request\n{\n";
            print SERVER_PROT "    struct request_header __header;\n";
            $state++;
139 140
            next;
        }
141 142

        if (/^\@REPLY/)
143
        {
144
            die "Misplaced \@REPLY" unless $state == 2;
145 146 147
            print SERVER_PROT "};\n";
            print SERVER_PROT "struct ${name}_reply\n{\n";
            print SERVER_PROT "    struct reply_header __header;\n";
148 149
            $state++;
            next;
150
        }
151 152

        if (/^\@END/)
153
        {
154 155 156
            die "Misplaced \@END" unless ($state == 2 || $state == 3);
            print SERVER_PROT "};\n";

157 158 159 160 161 162 163
            if ($state == 2)  # build dummy reply struct
            {
                print SERVER_PROT "struct ${name}_reply\n{\n";
                print SERVER_PROT "    struct reply_header __header;\n";
                print SERVER_PROT "};\n";
            }

164 165
            # got a complete request
            push @requests, $name;
166
            DO_DUMP_FUNC( $name, "request", @in_struct);
167 168 169
            if ($#out_struct >= 0)
            {
                $replies{$name} = 1;
170
                DO_DUMP_FUNC( $name, "reply", @out_struct);
171 172 173
            }
            $state = 1;
            next;
174
        }
175 176

        if ($state != 1)
177
        {
178 179 180 181 182 183 184
            # skip empty lines (but keep them in output file)
            if (/^$/)
            {
                print SERVER_PROT "\n";
                next;
            }

185 186 187 188 189 190 191
            if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
            {
                $var = $1;
                $type = "dump_varargs_" . $2 . "( min(cur_size,req->" . $3 . ") )";
                s!(VARARG\(.*\)\s*;)!/* $1 */!;
            }
            elsif (/^\s*VARARG\((\w+),(\w+)\)/)
192 193
            {
                $var = $1;
194
                $type = "dump_varargs_" . $2 . "( cur_size )";
195 196
                s!(VARARG\(.*\)\s*;)!/* $1 */!;
            }
197
            elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
198
            {
199
                $type = $1;
200
                $var = $3;
201
                die "Unrecognized type $type" unless defined($formats{$type});
202 203 204 205 206 207 208
            }
            else
            {
                die "Unrecognized syntax $_";
            }
            if ($state == 2) { push @in_struct, $type, $var; }
            if ($state == 3) { push @out_struct, $type, $var; }
209
        }
210 211 212

        # Pass it through into the output file
        print SERVER_PROT $_ . "\n";
Alexandre Julliard's avatar
Alexandre Julliard committed
213
    }
214
    close PROTOCOL;
Alexandre Julliard's avatar
Alexandre Julliard committed
215 216
}

217 218
### Retrieve the server protocol version from the existing server_protocol.h file

Francois Gouget's avatar
Francois Gouget committed
219
sub GET_PROTOCOL_VERSION()
220 221 222 223 224 225 226 227 228 229 230
{
    my $protocol = 0;
    open SERVER_PROT, "include/wine/server_protocol.h" or return 0;
    while (<SERVER_PROT>)
    {
        if (/^\#define SERVER_PROTOCOL_VERSION (\d+)/) { $protocol = $1; last; }
    }
    close SERVER_PROT;
    return $protocol;
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
### Retrieve the list of status and errors used in the server

sub GET_ERROR_NAMES()
{
    my %errors = ();
    foreach my $f (glob "server/*.c")
    {
        open FILE, $f or die "Can't open $f";
        while (<FILE>)
        {
            if (/set_error\s*\(\s*STATUS_(\w+)\s*\)/)
            {
                $errors{$1} = "STATUS_$1";
            }
            elsif (/set_win32_error\s*\(\s*(\w+)\s*\)/)
            {
                $errors{$1} = "0xc0010000 | $1";
            }
        }
        close FILE;
    }
    return %errors;
}

255 256
### Replace the contents of a file between ### make_requests ### marks

Francois Gouget's avatar
Francois Gouget committed
257
sub REPLACE_IN_FILE($@)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
{
    my $name = shift;
    my @data = @_;
    my @lines = ();
    open(FILE,$name) or die "Can't open $name";
    while (<FILE>)
    {
	push @lines, $_;
	last if /\#\#\# make_requests begin \#\#\#/;
    }
    push @lines, "\n", @data;
    while (<FILE>)
    {
	if (/\#\#\# make_requests end \#\#\#/) { push @lines, "\n", $_; last; }
    }
    push @lines, <FILE>;
    open(FILE,">$name") or die "Can't modify $name";
    print FILE @lines;
    close(FILE);
Alexandre Julliard's avatar
Alexandre Julliard committed
277
}
Francois Gouget's avatar
Francois Gouget committed
278 279 280 281 282 283

### Main

# Get the server protocol version
my $protocol = GET_PROTOCOL_VERSION();

284 285
my %errors = GET_ERROR_NAMES();

Francois Gouget's avatar
Francois Gouget committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
### Create server_protocol.h and print header

open SERVER_PROT, ">include/wine/server_protocol.h" or die "Cannot create include/wine/server_protocol.h";
print SERVER_PROT "/*\n * Wine server protocol definitions\n *\n";
print SERVER_PROT " * This file is automatically generated; DO NO EDIT!\n";
print SERVER_PROT " * Edit server/protocol.def instead and re-run tools/make_requests\n";
print SERVER_PROT " */\n\n";
print SERVER_PROT "#ifndef __WINE_WINE_SERVER_PROTOCOL_H\n";
print SERVER_PROT "#define __WINE_WINE_SERVER_PROTOCOL_H\n";

### Parse requests to find request/reply structure definitions

PARSE_REQUESTS();

### Build the request list and structures

print SERVER_PROT "\n\nenum request\n{\n";
foreach my $req (@requests) { print SERVER_PROT "    REQ_$req,\n"; }
print SERVER_PROT "    REQ_NB_REQUESTS\n};\n\n";

print SERVER_PROT "union generic_request\n{\n";
print SERVER_PROT "    struct request_max_size max_size;\n";
print SERVER_PROT "    struct request_header request_header;\n";
foreach my $req (@requests) { print SERVER_PROT "    struct ${req}_request ${req}_request;\n"; }
print SERVER_PROT "};\n";

print SERVER_PROT "union generic_reply\n{\n";
print SERVER_PROT "    struct request_max_size max_size;\n";
print SERVER_PROT "    struct reply_header reply_header;\n";
foreach my $req (@requests) { print SERVER_PROT "    struct ${req}_reply ${req}_reply;\n"; }
print SERVER_PROT "};\n\n";

printf SERVER_PROT "#define SERVER_PROTOCOL_VERSION %d\n\n", $protocol + 1;
print SERVER_PROT "#endif /* __WINE_WINE_SERVER_PROTOCOL_H */\n";
close SERVER_PROT;

### Output the dumping function tables

push @trace_lines, "static const dump_func req_dumpers[REQ_NB_REQUESTS] = {\n";
foreach my $req (@requests)
{
    push @trace_lines, "    (dump_func)dump_${req}_request,\n";
}
push @trace_lines, "};\n\n";

push @trace_lines, "static const dump_func reply_dumpers[REQ_NB_REQUESTS] = {\n";
foreach my $req (@requests)
{
    push @trace_lines, "    (dump_func)", $replies{$req} ? "dump_${req}_reply,\n" : "0,\n";
}
push @trace_lines, "};\n\n";

push @trace_lines, "static const char * const req_names[REQ_NB_REQUESTS] = {\n";
foreach my $req (@requests)
{
    push @trace_lines, "    \"$req\",\n";
}
343 344 345 346 347 348 349 350 351 352 353 354
push @trace_lines, "};\n\n";

push @trace_lines, "static const struct\n{\n";
push @trace_lines, "    const char  *name;\n";
push @trace_lines, "    unsigned int value;\n";
push @trace_lines, "} status_names[] =\n{\n";

foreach my $err (sort keys %errors)
{
    push @trace_lines, sprintf("    { %-30s %s },\n", "\"$err\",", $errors{$err});
}
push @trace_lines, "    { NULL, 0 }\n";
Francois Gouget's avatar
Francois Gouget committed
355 356 357
push @trace_lines, "};\n";

REPLACE_IN_FILE( "server/trace.c", @trace_lines );
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

### Output the request handlers list

my @request_lines = ();

foreach my $req (@requests) { push @request_lines, "DECL_HANDLER($req);\n"; }
push @request_lines, "\n#ifdef WANT_REQUEST_HANDLERS\n\n";
push @request_lines, "typedef void (*req_handler)( const void *req, void *reply );\n";
push @request_lines, "static const req_handler req_handlers[REQ_NB_REQUESTS] =\n{\n";
foreach my $req (@requests)
{
    push @request_lines, "    (req_handler)req_$req,\n";
}
push @request_lines, "};\n#endif  /* WANT_REQUEST_HANDLERS */\n";

REPLACE_IN_FILE( "server/request.h", @request_lines );