#!/usr/bin/perl -w
#
# kmsg kernel messages check and print tool.
#
# To check the source code for missing messages the script is called
# with check, the name compiler and the compile parameters
#	kmsg-doc check $(CC) $(c_flags) $<
# To create man pages for the messages the script is called with
#	kmsg-doc print $(CC) $(c_flags) $<
#
# Copyright IBM Corp. 2008
# Author(s):  Martin Schwidefsky <schwidefsky@de.ibm.com>
#	      Michael Holzheu <holzheu@linux.vnet.ibm.com>
#

use Cwd;
use bigint;

my $errors = 0;
my $warnings = 0;
my $srctree = "";
my $objtree = "";
my $kmsg_count = 0;

sub remove_quotes($)
{
    my ($string) = @_;
    my $inside = 0;
    my $slash = 0;
    my $result = "";

    foreach my $str (split(/([\\"])/, $string)) {
        if ($inside && ($str ne "\"" || $slash)) {
            $result .= $str;
        }
        # Check for backslash before quote
        if ($str eq "\"") {
            if (!$slash) {
                $inside = !$inside;
            }
	     $slash = 0;
        } elsif ($str eq "\\") {
            $slash = !$slash;
        } elsif ($str ne "") {
            $slash = 0;
        }
    }
    return $result;
}

sub string_to_bytes($)
{
    my ($string) = @_;
    my %is_escape = ('"', 0x22, '\'', 0x27, 'n', 0x0a, 'r', 0x0d, 'b', 0x08,
		     't', 0x09, 'f', 0x0c, 'a', 0x07, 'v', 0x0b, '?', 0x3f);
    my (@ar, $slash, $len);

    # scan string, interpret backslash escapes and write bytes to @ar
    $len = 0;
    foreach my $ch (split(//, $string)) {
	if ($ch eq '\\') {
	    $slash = !$slash;
	    if (!$slash) {
		$ar[$len] = ord('\\');
		$len++;
	    }
	} elsif ($slash && defined $is_escape{$ch}) {
	    # C99 backslash escapes: \\ \" \' \n \r \b \t \f \a \v \?
	    $ar[$len] = $is_escape{$ch};
	    $len++;
	    $slash = 0;
	} elsif ($slash) {
	    # FIXME: C99 backslash escapes \nnn \xhh
	    die("Unknown backslash escape in message $string.");
	} else {
	    # normal character
	    $ar[$len] = ord($ch);
	    $len++;
	}
    }
    return @ar;
}

sub calc_jhash($)
{
    my ($string) = @_;
    my @ar;
    my ($a, $b, $c, $i, $length, $len);

    @ar = string_to_bytes($string);
    $length = @ar;
    # add dummy elements to @ar to avoid if then else hell
    push @ar, (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    $a = 0x9e3779b9;
    $b = 0x9e3779b9;
    $c = 0;
    $i = 0;
    for ($len = $length + 12; $len >= 12; $len -= 12) {
	if ($len < 24) {
	    # add length for last round
	    $c += $length;
	}
	$a += $ar[$i] + ($ar[$i+1]<<8) + ($ar[$i+2]<<16) + ($ar[$i+3]<<24);
	$b += $ar[$i+4] + ($ar[                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                