#!/usr/bin/perl -w
#
# Extract #define symbol information from C header files.
#
# Copyright 2002 Alexandre Julliard
#
# 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# list of symbols (regexps) to skip for each header
%skip_list =
(
 "winnt.h"   => [ "APIENTRY", "APIPRIVATE", "CALLBACK", "CONST", "EXTERN_C", "PASCAL",
                  "VOID", "DUMMY(STRUCT|UNION)NAME.*", "STDAPI.*", "STDMETHOD.*", "WINAPI.*",
                  "WINE_.*", "_*(cdecl|CDECL|pascal|export|fastcall|stdcall)",
                  "MEM_SYSTEM", "_GET_CONTEXT", "_QUAD_.*",
                  "CONTEXT_(ALPHA|R4000|SPARC|X86|i386|i486)" ],
 "winbase.h" => [ "(Fill|Move|Zero|Copy)Memory" ],
 "wingdi.h"  => [ "PROFILE_LINKED", "PROFILE_EMBEDDED", "GetCharWidth[AW]" ],
 "winuser.h" => [ "OemToAnsi[AW]", "OemToAnsiBuff[AW]", "AnsiToOem[AW]", "AnsiToOemBuff[AW]",
                  "Ansi(Next|Prev|Lower|Upper|LowerBuff|UpperBuff)[AW]", "GetNextWindow" ],
 "winsock2.h" => [ "WSAEVENT", "LPWSAEVENT", "WSAOVERLAPPED", "WS_.*" ]
);

%header_list =
(
 "windef.h" => "windef.pm",
 "winnt.h" => "winnt.pm",
 "winbase.h" => "winbase.pm",
 "wingdi.h" => "wingdi.pm",
 "winuser.h" => "winuser.pm",
 "winerror.h" => "winerror.pm",
 "winnls.h" => "winnls.pm",
 "winreg.h" => "winreg.pm",
 "winsock2.h" => "winsock2.pm",
 "winspool.h" => "winspool.pm",
 "winver.h" => "winver.pm",
 "wincon.h" => "wincon.pm",
 "setupapi.h" => "setupapi_h.pm",
);

$include_dir = "../../include";

@list = ($#ARGV >= 0) ? @ARGV : keys %header_list;

foreach $basename (@list)
{
    my $skip = $skip_list{$basename};
    my $result = "include/" . $header_list{$basename};
    my $package = $header_list{$basename};
    $package =~ s/\.pm$//;

    open INPUT, "$include_dir/$basename" or die "Cannot open $include_dir/$basename";
    open OUTPUT, ">sym.c" or die "Cannot create sym.c";
    print "Building $result\n";

    print OUTPUT <<EOF;
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "windef.h"
#include "winnt.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "winerror.h"
#include "winnls.h"
#include "winreg.h"
#include "winsock2.h"
#include "winspool.h"
#include "winver.h"
#include "wincon.h"
#include "setupapi.h"
EOF

    print OUTPUT <<EOF;
int main()
{
    printf( "# Automatically generated by make_symbols; DO NOT EDIT!! \\n" );
    printf( "#\\n" );
    printf( "# Perl definitions for header file $basename\\n" );
    printf( "#\\n\\n" );
    printf( "\\n" );
    printf( "package $package;\\n" );
    printf( "\\n" );
    printf( "use strict;\\n" );
    printf( "\\n" );
    printf( "use vars qw(\$VERSION \@ISA \@EXPORT \@EXPORT_OK);\\n" );
    printf( "\\n" );
    printf( "require Exporter;\\n" );
    printf( "\\n" );
    printf( "\@ISA = qw(Exporter);\\n" );
    printf( "\@EXPORT = qw(\\n" );
EOF

    my %symbols = ();
    while (<INPUT>)
    {
        # extract all #defines
        next unless (/^\s*\#\s*define\s+([A-Za-z0-9_]+)\s+(.*)$/);
        my ($name,$value) = ($1,$2);
        # skip empty value
        next if ($value eq "");
        # skip the WINELIB defines
        next if ($value =~ /WINELIB_NAME_AW/);
        # skip macros containing multiple values
        next if ($value =~ /{.*}/);
        # check against regexps to skip
        next if (grep { $name =~ /^$_$/ } @$skip);
        $symbols{$name} = $value;
    }
    foreach $sym (sort keys %symbols)
    {
        printf OUTPUT "    printf(\"    $sym\\n\");\n";
    }
    printf OUTPUT "    printf(\");\\n\");\n";
    printf OUTPUT "    printf(\"\@EXPORT_OK = qw();\\n\");\n";
    printf OUTPUT "    printf(\"\\n\");\n";

    foreach $sym (sort keys %symbols)
    {
        printf OUTPUT "    printf(\"use constant $sym => %%d;\\n\", (int)($sym));\n";
    }
    printf OUTPUT "    printf(\"\\n\");\n";
    printf OUTPUT "    printf(\"1;\\n\");\n";
    print OUTPUT "    exit(0);\n}\n";
    close OUTPUT;
    #print "cc -I../../include -o sym sym.c\n";
    if (system( "cc -I../../include -o sym sym.c" )) { die "Could not compile sym.c"; }
    #print "./sym >$result\n";
    if (system( "./sym >$result" )) { die "Could not run ./sym\n"; }
    unlink "sym","sym.c";
}

chdir "../..";
exec "tools/winapi/winapi_extract", "--no-progress", "--no-verbose";
