make_requests 14.6 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 server/protocol.def.
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 =
25 26 27 28 29 30 31 32 33
(                     # size align format
    "int"           => [  4,   4,  "%d" ],
    "short int"     => [  2,   2,  "%d" ],
    "char"          => [  1,   1,  "%c" ],
    "unsigned char" => [  1,   1,  "%02x" ],
    "unsigned short"=> [  2,   2,  "%04x" ],
    "unsigned int"  => [  4,   4,  "%08x" ],
    "data_size_t"   => [  4,   4,  "%u" ],
    "obj_handle_t"  => [  4,   4,  "%04x" ],
34
    "atom_t"        => [  4,   4,  "%04x" ],
35 36 37
    "user_handle_t" => [  4,   4,  "%08x" ],
    "process_id_t"  => [  4,   4,  "%04x" ],
    "thread_id_t"   => [  4,   4,  "%04x" ],
38
    "client_ptr_t"  => [  8,   8,  "&dump_uint64" ],
39
    "mod_handle_t"  => [  8,   8,  "&dump_uint64" ],
40
    "lparam_t"      => [  8,   8,  "&dump_uint64" ],
41
    "apc_param_t"   => [  8,   8,  "&dump_uint64" ],
42 43
    "file_pos_t"    => [  8,   8,  "&dump_uint64" ],
    "mem_size_t"    => [  8,   8,  "&dump_uint64" ],
44
    "affinity_t"    => [  8,   8,  "&dump_uint64" ],
45 46 47
    "timeout_t"     => [  8,   8,  "&dump_timeout" ],
    "rectangle_t"   => [  16,  4,  "&dump_rectangle" ],
    "char_info_t"   => [  4,   2,  "&dump_char_info" ],
48 49
    "apc_call_t"    => [  40,  8,  "&dump_apc_call" ],
    "apc_result_t"  => [  40,  8,  "&dump_apc_result" ],
50
    "async_data_t"  => [  40,  8,  "&dump_async_data" ],
51
    "irp_params_t"  => [  16,  8,  "&dump_irp_params" ],
52 53
    "luid_t"        => [  8,   4,  "&dump_luid" ],
    "ioctl_code_t"  => [  4,   4,  "&dump_ioctl_code" ],
54
    "cpu_type_t"    => [  4,   4,  "&dump_cpu_type" ],
55
    "hw_input_t"    => [  32,  8,  "&dump_hw_input" ],
Alexandre Julliard's avatar
Alexandre Julliard committed
56 57 58 59
);

my @requests = ();
my %replies = ();
60
my @asserts = ();
Alexandre Julliard's avatar
Alexandre Julliard committed
61

62
my @trace_lines = ();
Alexandre Julliard's avatar
Alexandre Julliard committed
63

64
my $max_req_size = 64;
65

66
my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
Alexandre Julliard's avatar
Alexandre Julliard committed
67

68 69 70 71 72 73 74 75 76 77 78 79
sub add_padding($$)
{
    my ($offset, $padding) = @_;
    if ($offset % $padding)
    {
        my $count = $padding - ($offset % $padding);
        print SERVER_PROT "    char __pad_$offset\[$count\];\n";
        $offset += $count;
    }
    return $offset;
}

80 81 82 83 84 85
### Generate a dumping function

sub DO_DUMP_FUNC($$@)
{
    my $name = shift;
    my $req = shift;
86
    my $prefix = " ";
87 88 89 90 91
    push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
    while ($#_ >= 0)
    {
	my $type = shift;
	my $var = shift;
92
        next if $var =~ /^__pad/;
93 94
	if (defined($formats{$type}))
	{
95 96
            my $fmt = ${$formats{$type}}[2];
            if ($fmt =~ /^&(.*)/)
97 98
            {
                my $func = $1;
99
                push @trace_lines, "    $func( \"$prefix$var=\", &req->$var );\n";
100
            }
101
            elsif ($fmt =~ /^(%.*)\s+\((.*)\)/)
102 103
            {
                my ($format, $cast) = ($1, $2);
104
                push @trace_lines, "    fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
105
            }
106 107
            else
            {
108
                push @trace_lines, "    fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
109 110 111 112
            }
	}
	else  # must be some varargs format
	{
113
            push @trace_lines, "    " . sprintf($type, "$prefix$var=") . ";\n";
114
        }
115
        $prefix = ", ";
116 117 118 119
    }
    push @trace_lines, "}\n\n";
}

120
### Parse the request definitions
Alexandre Julliard's avatar
Alexandre Julliard committed
121

Francois Gouget's avatar
Francois Gouget committed
122
sub PARSE_REQUESTS()
Alexandre Julliard's avatar
Alexandre Julliard committed
123
{
124 125
    # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
    my $state = 0;
126
    my $offset = 0;
127
    my $name = "";
128 129
    my @in_struct = ();
    my @out_struct = ();
130 131 132 133

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

    while (<PROTOCOL>)
Alexandre Julliard's avatar
Alexandre Julliard committed
134
    {
135 136
        my ($type, $var);
        # strip comments
Alexandre Julliard's avatar
Alexandre Julliard committed
137
	s!/\*.*\*/!!g;
138 139 140 141 142 143 144 145 146 147 148 149 150 151
        # 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*\)/)
152
        {
153 154 155 156 157
            $name = $1;
            die "Misplaced \@REQ" unless $state == 1;
            # start a new request
            @in_struct = ();
            @out_struct = ();
158
            $offset = 12;
159 160 161
            print SERVER_PROT "struct ${name}_request\n{\n";
            print SERVER_PROT "    struct request_header __header;\n";
            $state++;
162 163
            next;
        }
164 165

        if (/^\@REPLY/)
166
        {
167
            die "Misplaced \@REPLY" unless $state == 2;
168 169 170
            $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
            die "request $name too large ($offset)" if ($offset > $max_req_size);
            push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
171 172 173
            print SERVER_PROT "};\n";
            print SERVER_PROT "struct ${name}_reply\n{\n";
            print SERVER_PROT "    struct reply_header __header;\n";
174 175
            die "request $name too large ($offset)" if ($offset > $max_req_size);
            $offset = 8;
176 177
            $state++;
            next;
178
        }
179 180

        if (/^\@END/)
181
        {
182 183
            die "Misplaced \@END" unless ($state == 2 || $state == 3);

184
            $offset = add_padding( $offset, 8 ); # all requests should be 8-byte aligned
185
            print SERVER_PROT "};\n";
186 187
            if ($state == 2)  # build dummy reply struct
            {
188
                die "request $name too large ($offset)" if ($offset > $max_req_size);
189
                push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
190 191 192 193
                print SERVER_PROT "struct ${name}_reply\n{\n";
                print SERVER_PROT "    struct reply_header __header;\n";
                print SERVER_PROT "};\n";
            }
194 195 196
            else
            {
                die "reply $name too large ($offset)" if ($offset > $max_req_size);
197
                push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
198
            }
199 200
            # got a complete request
            push @requests, $name;
201
            DO_DUMP_FUNC( $name, "request", @in_struct);
202 203 204
            if ($#out_struct >= 0)
            {
                $replies{$name} = 1;
205
                DO_DUMP_FUNC( $name, "reply", @out_struct);
206 207 208
            }
            $state = 1;
            next;
209
        }
210 211

        if ($state != 1)
212
        {
213 214 215 216 217 218 219
            # skip empty lines (but keep them in output file)
            if (/^$/)
            {
                print SERVER_PROT "\n";
                next;
            }

220 221 222
            if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
            {
                $var = $1;
223
                $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
224 225 226
                s!(VARARG\(.*\)\s*;)!/* $1 */!;
            }
            elsif (/^\s*VARARG\((\w+),(\w+)\)/)
227 228
            {
                $var = $1;
229
                $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
230 231
                s!(VARARG\(.*\)\s*;)!/* $1 */!;
            }
232
            elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
233
            {
234
                $type = $1;
235
                $var = $3;
236
                die "Unrecognized type $type" unless defined($formats{$type});
237 238 239
                my @fmt = @{$formats{$type}};
                if ($offset & ($fmt[1] - 1))
                {
240
                    my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
241
                    print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
242 243 244 245 246 247 248 249 250 251
                    print SERVER_PROT "    char __pad_$offset\[$count\];\n";
                    $offset += $count;
                }
                if ($state == 2)
                {
                    push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_request, $var) == $offset );\n";
                }
                else
                {
                    push @asserts, "C_ASSERT( FIELD_OFFSET(struct ${name}_reply, $var) == $offset );\n";
252 253
                }
                $offset += $fmt[0];
254 255 256 257 258 259 260
            }
            else
            {
                die "Unrecognized syntax $_";
            }
            if ($state == 2) { push @in_struct, $type, $var; }
            if ($state == 3) { push @out_struct, $type, $var; }
261
        }
262 263 264

        # Pass it through into the output file
        print SERVER_PROT $_ . "\n";
Alexandre Julliard's avatar
Alexandre Julliard committed
265
    }
266
    close PROTOCOL;
Alexandre Julliard's avatar
Alexandre Julliard committed
267 268
}

269 270
### Retrieve the server protocol version from the existing server_protocol.h file

Francois Gouget's avatar
Francois Gouget committed
271
sub GET_PROTOCOL_VERSION()
272 273 274 275 276 277 278 279 280 281 282
{
    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;
}

283 284 285 286 287 288 289
### Retrieve the list of status and errors used in the server

sub GET_ERROR_NAMES()
{
    my %errors = ();
    foreach my $f (glob "server/*.c")
    {
290
        next if $f eq "server/trace.c";
291 292 293
        open FILE, $f or die "Can't open $f";
        while (<FILE>)
        {
294
            if (/STATUS_(\w+)/)
295 296
            {
                $errors{$1} = "STATUS_$1" unless $1 eq "SUCCESS";
297 298 299 300 301 302 303 304 305 306 307
            }
            elsif (/set_win32_error\s*\(\s*(\w+)\s*\)/)
            {
                $errors{$1} = "0xc0010000 | $1";
            }
        }
        close FILE;
    }
    return %errors;
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
# update a file if changed
sub update_file($)
{
    my $file = shift;
    my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
    if (!$ret)
    {
        unlink "$file.new";
    }
    else
    {
        rename "$file.new", "$file";
        print "$file updated\n";
    }
    return $ret;
}
324

325 326
# replace some lines in a file between two markers
sub replace_in_file($$$@)
327
{
328 329 330 331 332 333 334
    my $file = shift;
    my $start = shift;
    my $end = shift;

    open NEW_FILE, ">$file.new" or die "cannot create $file.new";

    if (defined($start))
335
    {
336 337 338 339 340 341
        open OLD_FILE, "$file" or die "cannot open $file";
        while (<OLD_FILE>)
        {
            print NEW_FILE $_;
            last if /$start/;
        }
342
    }
343 344 345 346

    print NEW_FILE "\n", @_, "\n";

    if (defined($end))
347
    {
348 349 350 351 352 353
        my $skip=1;
        while (<OLD_FILE>)
        {
            $skip = 0 if /$end/;
            print NEW_FILE $_ unless $skip;
        }
354
    }
355 356 357 358

    close OLD_FILE if defined($start);
    close NEW_FILE;
    return update_file($file);
Alexandre Julliard's avatar
Alexandre Julliard committed
359
}
Francois Gouget's avatar
Francois Gouget committed
360 361 362 363 364 365

### Main

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

366 367
my %errors = GET_ERROR_NAMES();

Francois Gouget's avatar
Francois Gouget committed
368 369
### Create server_protocol.h and print header

370
open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
Francois Gouget's avatar
Francois Gouget committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
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;
403
update_file( "include/wine/server_protocol.h" );
Francois Gouget's avatar
Francois Gouget committed
404 405 406 407 408 409 410 411 412 413 414 415 416

### 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)
{
417
    push @trace_lines, "    ", $replies{$req} ? "(dump_func)dump_${req}_reply,\n" : "NULL,\n";
Francois Gouget's avatar
Francois Gouget committed
418 419 420 421 422 423 424 425
}
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";
}
426 427 428 429 430 431 432 433 434 435 436 437
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
438 439
push @trace_lines, "};\n";

440 441 442 443
replace_in_file( "server/trace.c",
                 "### make_requests begin ###",
                 "### make_requests end ###",
                 @trace_lines );
444 445 446 447 448 449 450 451 452 453 454 455 456

### 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";
}
457 458 459 460 461 462 463 464 465
push @request_lines, "};\n\n";

foreach my $type (sort keys %formats)
{
    my $size = ${$formats{$type}}[0];
    push @request_lines, "C_ASSERT( sizeof($type) == $size );\n";
}
push @request_lines, @asserts;
push @request_lines, "\n#endif  /* WANT_REQUEST_HANDLERS */\n";
466

467 468 469 470
replace_in_file( "server/request.h",
                 "### make_requests begin ###",
                 "### make_requests end ###",
                 @request_lines );