comctl32/listview: Add support for LVFI_SUBSTRING.
[wine] / tools / make_requests
index c87cdc0..e7a1ffb 100755 (executable)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl -w
 #
 # Build the server/trace.c and server/request.h files
-# from the contents of include/wine/server.h.
+# from the contents of server/protocol.def.
 #
 # Copyright (C) 1998 Alexandre Julliard
 #
 use strict;
 
 my %formats =
-(
-    "int"           => "%d",
-    "short int"     => "%d",
-    "char"          => "%c",
-    "unsigned char" => "%02x",
-    "unsigned short"=> "%04x",
-    "unsigned int"  => "%08x",
-    "unsigned long" => "%lx",
-    "void*"         => "%p",
-    "size_t"        => "%lu (unsigned long)",
-    "data_size_t"   => "%u",
-    "obj_handle_t"  => "%04x",
-    "atom_t"        => "%04x",
-    "user_handle_t" => "%p",
-    "process_id_t"  => "%04x",
-    "thread_id_t"   => "%04x",
-    "timeout_t"     => "&dump_timeout",
-    "rectangle_t"   => "&dump_rectangle",
-    "char_info_t"   => "&dump_char_info",
-    "apc_call_t"    => "&dump_apc_call",
-    "apc_result_t"  => "&dump_apc_result",
-    "async_data_t"  => "&dump_async_data",
-    "luid_t"        => "&dump_luid",
-    "ioctl_code_t"  => "&dump_ioctl_code",
-    "file_pos_t"    => "&dump_file_pos",
+(                     # 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" ],
+    "atom_t"        => [  4,   4,  "%04x" ],
+    "user_handle_t" => [  4,   4,  "%08x" ],
+    "process_id_t"  => [  4,   4,  "%04x" ],
+    "thread_id_t"   => [  4,   4,  "%04x" ],
+    "client_ptr_t"  => [  8,   8,  "&dump_uint64" ],
+    "mod_handle_t"  => [  8,   8,  "&dump_uint64" ],
+    "lparam_t"      => [  8,   8,  "&dump_uint64" ],
+    "apc_param_t"   => [  8,   8,  "&dump_uint64" ],
+    "file_pos_t"    => [  8,   8,  "&dump_uint64" ],
+    "mem_size_t"    => [  8,   8,  "&dump_uint64" ],
+    "affinity_t"    => [  8,   8,  "&dump_uint64" ],
+    "timeout_t"     => [  8,   8,  "&dump_timeout" ],
+    "rectangle_t"   => [  16,  4,  "&dump_rectangle" ],
+    "char_info_t"   => [  4,   2,  "&dump_char_info" ],
+    "apc_call_t"    => [  40,  8,  "&dump_apc_call" ],
+    "apc_result_t"  => [  40,  8,  "&dump_apc_result" ],
+    "async_data_t"  => [  40,  8,  "&dump_async_data" ],
+    "luid_t"        => [  8,   4,  "&dump_luid" ],
+    "ioctl_code_t"  => [  4,   4,  "&dump_ioctl_code" ],
+    "cpu_type_t"    => [  4,   4,  "&dump_cpu_type" ],
 );
 
 my @requests = ();
 my %replies = ();
+my @asserts = ();
 
 my @trace_lines = ();
 
+my $max_req_size = 64;
 
+my $warnings = scalar(@ARGV) && $ARGV[0] eq "-w";
 
 ### Generate a dumping function
 
@@ -62,41 +69,36 @@ sub DO_DUMP_FUNC($$@)
 {
     my $name = shift;
     my $req = shift;
+    my $prefix = " ";
     push @trace_lines, "static void dump_${name}_$req( const struct ${name}_$req *req )\n{\n";
     while ($#_ >= 0)
     {
        my $type = shift;
        my $var = shift;
+        next if $var =~ /^__pad/;
        if (defined($formats{$type}))
        {
-            if ($formats{$type} =~ /^&(.*)/)
+            my $fmt = ${$formats{$type}}[2];
+            if ($fmt =~ /^&(.*)/)
             {
                 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);
+                push @trace_lines, "    $func( \"$prefix$var=\", &req->$var );\n";
             }
-            elsif ($formats{$type} =~ /^(%.*)\s+\((.*)\)/)
+            elsif ($fmt =~ /^(%.*)\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";
+                push @trace_lines, "    fprintf( stderr, \"$prefix$var=$format\", ($cast)req->$var );\n";
             }
             else
             {
-                push @trace_lines, "    fprintf( stderr, \" $var=$formats{$type}";
-                push @trace_lines, "," if ($#_ > 0);
-                push @trace_lines, "\", req->$var );\n";
+                push @trace_lines, "    fprintf( stderr, \"$prefix$var=$fmt\", req->$var );\n";
             }
        }
        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, "    " . sprintf($type, "$prefix$var=") . ";\n";
         }
+        $prefix = ", ";
     }
     push @trace_lines, "}\n\n";
 }
@@ -107,6 +109,7 @@ sub PARSE_REQUESTS()
 {
     # states: 0 = header 1 = declarations 2 = inside @REQ 3 = inside @REPLY
     my $state = 0;
+    my $offset = 0;
     my $name = "";
     my @in_struct = ();
     my @out_struct = ();
@@ -138,6 +141,7 @@ sub PARSE_REQUESTS()
             # start a new request
             @in_struct = ();
             @out_struct = ();
+            $offset = 12;
             print SERVER_PROT "struct ${name}_request\n{\n";
             print SERVER_PROT "    struct request_header __header;\n";
             $state++;
@@ -150,6 +154,8 @@ sub PARSE_REQUESTS()
             print SERVER_PROT "};\n";
             print SERVER_PROT "struct ${name}_reply\n{\n";
             print SERVER_PROT "    struct reply_header __header;\n";
+            die "request $name too large ($offset)" if ($offset > $max_req_size);
+            $offset = 8;
             $state++;
             next;
         }
@@ -157,15 +163,27 @@ sub PARSE_REQUESTS()
         if (/^\@END/)
         {
             die "Misplaced \@END" unless ($state == 2 || $state == 3);
-            print SERVER_PROT "};\n";
 
+            if ($offset & 7)  # all requests should be 8-byte aligned
+            {
+                my $count = 8 - ($offset & 7);
+                print SERVER_PROT "    char __pad_$offset\[$count\];\n";
+                $offset += $count;
+            }
+            print SERVER_PROT "};\n";
             if ($state == 2)  # build dummy reply struct
             {
+                die "request $name too large ($offset)" if ($offset > $max_req_size);
+                push @asserts, "C_ASSERT( sizeof(struct ${name}_request) == $offset );\n";
                 print SERVER_PROT "struct ${name}_reply\n{\n";
                 print SERVER_PROT "    struct reply_header __header;\n";
                 print SERVER_PROT "};\n";
             }
-
+            else
+            {
+                die "reply $name too large ($offset)" if ($offset > $max_req_size);
+                push @asserts, "C_ASSERT( sizeof(struct ${name}_reply) == $offset );\n";
+            }
             # got a complete request
             push @requests, $name;
             DO_DUMP_FUNC( $name, "request", @in_struct);
@@ -190,13 +208,13 @@ sub PARSE_REQUESTS()
             if (/^\s*VARARG\((\w+),(\w+),(\w+)\)/)
             {
                 $var = $1;
-                $type = "dump_varargs_" . $2 . "( min(cur_size,req->" . $3 . ") )";
+                $type = "dump_varargs_" . $2 . "( \"%s\", min(cur_size,req->" . $3 . ") )";
                 s!(VARARG\(.*\)\s*;)!/* $1 */!;
             }
             elsif (/^\s*VARARG\((\w+),(\w+)\)/)
             {
                 $var = $1;
-                $type = "dump_varargs_" . $2 . "( cur_size )";
+                $type = "dump_varargs_" . $2 . "( \"%s\", cur_size )";
                 s!(VARARG\(.*\)\s*;)!/* $1 */!;
             }
             elsif (/^\s*(\w+\**(\s+\w+\**)*)\s+(\w+);/)
@@ -204,6 +222,23 @@ sub PARSE_REQUESTS()
                 $type = $1;
                 $var = $3;
                 die "Unrecognized type $type" unless defined($formats{$type});
+                my @fmt = @{$formats{$type}};
+                if ($offset & ($fmt[1] - 1))
+                {
+                    my $count = $fmt[1] - ($offset & ($fmt[1] - 1));
+                    print "protocol.def:$.: warning: $name $offset $type $var needs padding\n" if $warnings;
+                    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";
+                }
+                $offset += $fmt[0];
             }
             else
             {
@@ -258,28 +293,57 @@ sub GET_ERROR_NAMES()
     return %errors;
 }
 
-### Replace the contents of a file between ### make_requests ### marks
+# 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;
+}
 
-sub REPLACE_IN_FILE($@)
+# replace some lines in a file between two markers
+sub replace_in_file($$$@)
 {
-    my $name = shift;
-    my @data = @_;
-    my @lines = ();
-    open(FILE,$name) or die "Can't open $name";
-    while (<FILE>)
+    my $file = shift;
+    my $start = shift;
+    my $end = shift;
+
+    open NEW_FILE, ">$file.new" or die "cannot create $file.new";
+
+    if (defined($start))
     {
-       push @lines, $_;
-       last if /\#\#\# make_requests begin \#\#\#/;
+        open OLD_FILE, "$file" or die "cannot open $file";
+        while (<OLD_FILE>)
+        {
+            print NEW_FILE $_;
+            last if /$start/;
+        }
     }
-    push @lines, "\n", @data;
-    while (<FILE>)
+
+    print NEW_FILE "\n", @_, "\n";
+
+    if (defined($end))
     {
-       if (/\#\#\# make_requests end \#\#\#/) { push @lines, "\n", $_; last; }
+        my $skip=1;
+        while (<OLD_FILE>)
+        {
+            $skip = 0 if /$end/;
+            print NEW_FILE $_ unless $skip;
+        }
     }
-    push @lines, <FILE>;
-    open(FILE,">$name") or die "Can't modify $name";
-    print FILE @lines;
-    close(FILE);
+
+    close OLD_FILE if defined($start);
+    close NEW_FILE;
+    return update_file($file);
 }
 
 ### Main
@@ -291,7 +355,7 @@ my %errors = GET_ERROR_NAMES();
 
 ### Create server_protocol.h and print header
 
-open SERVER_PROT, ">include/wine/server_protocol.h" or die "Cannot create include/wine/server_protocol.h";
+open SERVER_PROT, ">include/wine/server_protocol.h.new" or die "Cannot create include/wine/server_protocol.h.new";
 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";
@@ -324,6 +388,7 @@ 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;
+update_file( "include/wine/server_protocol.h" );
 
 ### Output the dumping function tables
 
@@ -360,7 +425,10 @@ foreach my $err (sort keys %errors)
 push @trace_lines, "    { NULL, 0 }\n";
 push @trace_lines, "};\n";
 
-REPLACE_IN_FILE( "server/trace.c", @trace_lines );
+replace_in_file( "server/trace.c",
+                 "### make_requests begin ###",
+                 "### make_requests end ###",
+                 @trace_lines );
 
 ### Output the request handlers list
 
@@ -374,6 +442,17 @@ foreach my $req (@requests)
 {
     push @request_lines, "    (req_handler)req_$req,\n";
 }
-push @request_lines, "};\n#endif  /* WANT_REQUEST_HANDLERS */\n";
+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";
 
-REPLACE_IN_FILE( "server/request.h", @request_lines );
+replace_in_file( "server/request.h",
+                 "### make_requests begin ###",
+                 "### make_requests end ###",
+                 @request_lines );