1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#!/usr/bin/perl -w
#
# codes - extract message codes from DeuTex source
# AYM 2005-08-28
#
# This file is copyright Andr Majorel 2005.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
use strict;
use File::Basename;
use IO::Handle;
sub split_args ($);
sub err (@);
# DeuTex functions that take a message code as argument
my %funcs =
(
'Bug' => { class => 'b', code => 0 },
'Detail' => { class => 'i', code => 0 },
'Info' => { class => 'i', code => 0 },
'LimitedEpilog' => { class => 'w', code => 1 },
'LimitedWarn' => { class => 'w', code => 1 },
'Phase' => { class => 'i', code => 0 },
'ProgError' => { class => 'e', code => 0 },
'Warning' => { class => 'w', code => 0 },
'nf_err' => { class => 'e', code => 0 },
);
# Used to spot duplicates
my %codes;
my $status = 0;
foreach my $pathname (@ARGV)
{
my $path = dirname $pathname;
my $inc = '';
$inc = "-I$path" if length ($path) > 0;
my $pathnameq = $pathname;
$pathnameq =~ s/'/'\''/g;
# Hide errno because the Glibc headers #define it to something hairy.
my $cmd = "sed 's/errno\\([^.]\\)/errno_\\1/g' '$pathnameq'"
. " | gcc -DDeuTex $inc -E - | indent -l9999";
my @canonsrc = `$cmd`;
if (@canonsrc == 0)
{
err "$pathname: an error occurred while running indent(1)";
$status = 1;
next;
}
foreach my $lbuf (@canonsrc)
{
# Function call ?
if ($lbuf =~ /^\s*([_a-zA-Z][_a-zA-Z0-9]*)\s*\((.*)\);\s*$/)
{
next unless exists $funcs{$1};
my $name = $1;
my $args = $2;
my $hash = $funcs{$1};
# Extract the arguments to the function
my @args = split_args $args;
# Extract the message code from the arguments. Any arguments
# before the code are removed (cf. LimitedWarn()).
my $code = splice @args, 0, $hash->{code} + 1;
if ($code !~ s/^"([A-Z][A-Z][0-9][0-9])"$/$1/)
{
err "invalid code \"$code\"";
$status = 1;
next;
}
# Report duplicates
if (exists $codes{$code})
{
err "$pathname: code $code already used in $codes{$code}";
}
else
{
$codes{$code} = $pathname;
}
printf "%s %s %-13s %s\n",
$code, $hash->{class}, $pathname, join ("\t", @args);
}
}
}
exit $status;
#
# split_args - split the arguments of a C function
#
sub split_args ($)
{
my ($string) = @_;
my @args = ('');
my $quote = '';
my $backslash = 0;
my $paren = 0;
for (my $i = 0; $i < length $string; $i++)
{
my $c = substr $string, $i, 1;
if ($quote eq '')
{
if ($c eq "'")
{
$quote = "'";
}
elsif ($c eq '"')
{
$quote = '"';
}
elsif ($c eq '(')
{
$paren++;
}
elsif ($c eq ')')
{
$paren--;
if ($paren < 0)
{
err "extra \")\" in \"$string\"";
exit 1;
}
}
elsif ($c eq ',' && $paren == 0)
{
push @args, '';
$c = '';
}
elsif ($c =~ /^\s$/ && $paren == 0 && length ($args[@args - 1]) == 0)
{
$c = '';
}
}
elsif ($quote eq "'")
{
if ($c eq '\\' && ! $backslash)
{
$backslash = 2;
}
elsif ($c eq "'" && ! $backslash)
{
$quote = '';
}
}
elsif ($quote eq '"')
{
if ($c eq '\\' && ! $backslash)
{
$backslash = 2;
}
elsif ($c eq '"' && ! $backslash)
{
$quote = '';
}
}
else
{
err "internal error, invalid quote";
}
$args[@args - 1] .= $c;
$backslash-- if $backslash > 0;
}
return @args;
}
#
# err - print an error message
#
sub err (@)
{
STDOUT->flush;
print STDERR "codes: ", @_, "\n";
return 1;
}
|