forked from apache/incubator-heron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.sh
executable file
·265 lines (231 loc) · 9.08 KB
/
errors.sh
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env perl
# *************************************************************
#
# usage: <this-script> [-list listfile] [-e] [-t] filename [filename]*
#
# -e generate enums and error messages
# -t generate #defines and error messages
#
# (both -e and -t can be used)
#
# -list: generate a list of error codes instead of other outputs
#
# *************************************************************
#
# INPUT: any number of sets of error codes for software
# layers called "name", with (unique) masks as follows:
#
# name = mask {
# ERRNAME Error string
# ERRNAME Error string
# ...
# ERRNAME Error string
# }
#
# (mask can be in octal (0777777), hex (0xabcdeff) or decimal
# notation)
#
# If you want the error_info[] structure to be part of a class,
# put the class name after the mask and groupname and before the open "{"
# for the group of error messages; in that case the <name>_einfo_gen.h
# file will look like:
# heron::error::error_info_t <class>::error_info[] = ...
# If you don't do that, the name of the error_info structure will
# have the <name> prepended, e.g.:
# heron::error::error_info_t <name>_error_info[] = ...
#
# *************************************************************
#
# OUTPUT:
# for each software layer ("name"), this script creates:
# <name>-errmsg-gen.h (-m)
# <name>-einfo-gen.h (-p)
# <name>-einfo-bakw-gen.h (-p)
# <name>-error-enum-gen.h (-e)
# <name>-error-def-gen.h (-d)
#
# name-error-gen.h contains a static char * array, each element is
# the error message associated with an error code
# name-einfo-gen.h contains a definition of a error_info_t array
# for use with the error package.
# name-error-gen.h contains an enumeration for the
# error codes , and an enum containing e_ERRMIN & e_ERRMAX
# name-error-def-gen.h contains the #defined constants for the error
# codes, and for minimum and maximum error codes
#
# *************************************************************
use strict;
use Getopt::Long;
sub Usage
{
my $progname = $0;
$progname =~ s/.*[\\\/]//;
print STDERR <<EOF;
Usage: $progname [-t] [-e] filename...
Generate C++ code representing error information from file.
You must specify one of -t or -e
--t generate #defines and error messages
--e generate enums and error messages
--help|h print this message and exit
--list generate list of error codes instead of other outputs
EOF
}
my %options = (m => 0, p => 0, e => 0, d => 0, help => 0, 'list' => '');
my @options = ("m!", "p!", "e!", "d!", "help|h", "list=s");
my $ok = GetOptions(\%options, @options);
$ok = 0 if $#ARGV == -1;
my $m = $options{m};
my $p = $options{p};
my $e = $options{e};
my $d = $options{d};
$ok = 0 if (!$m && !$p && !$e && !$d);
if (!$ok || $options{help}) {
Usage();
die(!$ok);
}
my ( $list, $listfile );
$list = 0;
$list = 1, $listfile = $options{'list'} if ( $options{'list'} ne '' );
my $timeStamp = localtime;
sub MakeStdHeader
{
my ($fileName, $baseName) = @_;
my $headerExclusionName = uc($baseName);
$headerExclusionName =~ tr/A-Z0-9/_/c;
my $header = <<EOF;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef $headerExclusionName
#define $headerExclusionName
/*
* DO NOT EDIT ---
* generated by $0 from $fileName
* generated on $timeStamp
*/
EOF
return $header;
}
sub GenErrorFiles
{
my ($fileName, $baseName, $base, $groupName, $className, $m, $p, $e, $d, @lines) = @_;
$className .= '::' if $className;
$base = oct($base) if $base =~ /^0/;
my $arrayPrefix = $className || "${baseName}_";
my $num = 0;
my $uBaseName = uc($baseName);
my $errorMsgName = "${baseName}-errmsg-gen.h";
my $errorInfoName = "${baseName}-einfo-gen.h";
my $errorEnumName = "${baseName}-error-enum-gen.h";
my $errorDefName = "${baseName}-error-def-gen.h";
my $errorInfoBakwName = "${baseName}-einfo-bakw-gen.h";
if ( ! $list ) {
open MSG_OUT, ">$errorMsgName" or die "Couldn't open $errorMsgName" if ($m);
open INFO_OUT, ">$errorInfoName" or die "Couldn't open $errorInfoName" if ($p);
open ENUM_OUT, ">$errorEnumName" or die "Couldn't open $errorEnumName" if ($e);
open DEF_OUT, ">$errorDefName" or die "Couldn't open $errorDefName" if ($d);
open INFOBAKW_OUT, ">$errorInfoBakwName" or die "Couldn't open $errorInfoBakwName" if ($p);
print MSG_OUT MakeStdHeader($fileName, $errorMsgName) if ($m);
print INFO_OUT MakeStdHeader($fileName, $errorInfoName) if ($p);
print ENUM_OUT MakeStdHeader($fileName, $errorEnumName) if ($e);
print DEF_OUT MakeStdHeader($fileName, $errorDefName) if ($d);
print INFOBAKW_OUT MakeStdHeader($fileName, $errorInfoBakwName) if ($p);
print MSG_OUT "static char* ${baseName}_errmsg[] = {\n" if ($m);
print INFO_OUT "heron::error::error_info_t ${arrayPrefix}error_info[] = {\n" if ($p);
print ENUM_OUT "enum {\n" if ($e);
print INFOBAKW_OUT "heron::error::error_info_t ${baseName}_error_info_bakw[] = {\n" if ($p);
}
foreach my $line (@lines) {
my ($tag, $msg);
if ($line =~ /\s*(\S*)\s*(.*)/) {
($tag, $msg) = ($1, $2);
chomp $msg;
} else {
die "bad line $line";
}
my $dTag = "${baseName}_$tag";
my $eTag = "${uBaseName}_$tag";
if ( $list ) {
print LIST sprintf("%s_%s = %s\n", $baseName, $tag, $num + 1);
} else {
print MSG_OUT sprintf("/* %-25s */ \"%s\",\n", $m ? $eTag : $dTag, $msg) if ($m);
print INFO_OUT sprintf(" { %-25s, \"%s\" },\n", $p ? $eTag : $dTag, $msg) if ($p);
print ENUM_OUT sprintf(" %-25s = 0x%x,\n", $eTag, $base + $num) if ($e);
print DEF_OUT sprintf("#define %-25s 0x%x\n", $dTag, $base + $num) if ($d);
print INFOBAKW_OUT " { $eTag, \"$eTag\" },\n" if ($p);
}
$num++;
}
$num--;
return if ( $list );
if ($m) {
print MSG_OUT <<EOF;
"dummy error code"
};
const ${baseName}_msg_size = $num;
#endif
EOF
}
print INFO_OUT "};\n\n" if ($p);
print INFO_OUT sprintf("const sp_uint32 %-25s = %d;", "${uBaseName}_ERRCNT", $num+1) if ($p);
print INFO_OUT "\n\n#endif\n" if ($p);
# print ENUM_OUT sprintf(" %-25s = 0x%x,\n", "${uBaseName}_OK", 0) if ($e);
print ENUM_OUT sprintf(" %-25s = 0x%x,\n", "${uBaseName}_ERRMIN", $base) if ($e);
print ENUM_OUT sprintf(" %-25s = 0x%x\n", "${uBaseName}_ERRMAX", $base + $num) if ($e);
print ENUM_OUT "};\n\n#endif\n" if ($e);
# print DEF_OUT sprintf("#define %-25s 0x%x\n", "${baseName}_OK", 0) if ($d);
print DEF_OUT sprintf("#define %-25s 0x%x\n", "${baseName}_ERRMIN", $base) if ($d);
print DEF_OUT sprintf("#define %-25s 0x%x\n", "${baseName}_ERRMAX", $base + $num) if ($d);
print DEF_OUT "\n#endif\n" if ($d);
print INFOBAKW_OUT "};\n\n#endif\n" if ($p);
close MSG_OUT, or die "Couldn't close $errorMsgName" if ($m);
close INFO_OUT, or die "Couldn't close $errorInfoName" if ($p);
close ENUM_OUT, or die "Couldn't close $errorEnumName" if ($e);
close DEF_OUT, or die "Couldn't close $errorDefName" if ($d);
close INFOBAKW_OUT, or die "Couldn't close $errorInfoBakwName" if ($p);
}
sub ProcessFile
{
my ($fileName, $m, $p, $e, $d) = @_;
my ($baseName, $base, $groupName, $className, @lines);
my $line;
open INFILE, "<$fileName" or die "Couldn't open $fileName";
while (defined($line = <INFILE>)) {
next if $line =~ /^\s*\#/ || $line =~ /^\s*$/;
if ($line =~ /^\s*(\S+)\s*=\s*([0-9A-Fa-fxX]+)\s*(".*")\s*(\S*)\s*{/) {
($baseName, $base, $groupName, $className) = ($1, $2, $3, $4);
} elsif ($line =~ /^\s*}/) {
GenErrorFiles($fileName, $baseName, $base, $groupName, $className, $m, $p, $e, $d, @lines);
undef $baseName;
undef $base;
undef $groupName;
undef $className;
undef @lines;
} else {
push @lines, $line;
}
}
die "missing }" if defined $baseName;
}
if ( $list ) {
open LIST, ">$listfile" or die "Couldn't open $listfile";
}
foreach my $file (@ARGV) {
ProcessFile($file, $m, $p, $e, $d);
}
close LIST if ( $list );