-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathur_processor.sh
More file actions
executable file
·273 lines (242 loc) · 8.05 KB
/
Copy pathur_processor.sh
File metadata and controls
executable file
·273 lines (242 loc) · 8.05 KB
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
266
267
268
269
270
271
272
#!/bin/bash
#
# Copyright (C) 2016 CESNET
#
# LICENSE TERMS
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name of the Company nor the names of its contributors
# may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# ALTERNATIVELY, provided that this notice is retained in full, this
# product may be distributed under the terms of the GNU General Public
# License (GPL) version 2 or later, in which case the provisions
# of the GPL apply INSTEAD OF those given above.
#
# This software is provided ``as is'', and any express or implied
# warranties, including, but not limited to, the implied warranties of
# merchantability and fitness for a particular purpose are disclaimed.
# In no event shall the company or contributors be liable for any
# direct, indirect, incidental, special, exemplary, or consequential
# damages (including, but not limited to, procurement of substitute
# goods or services; loss of use, data, or profits; or business
# interruption) however caused and on any theory of liability, whether
# in contract, strict liability, or tort (including negligence or
# otherwise) arising in any way out of the use of this software, even
# if advised of the possibility of such damage.
while getopts "i:o:" opt; do
case $opt in
i)
inputdir="$OPTARG" >&2
;;
o)
outputdir="$OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
if [ -z "$inputdir" -o -z "$outputdir" ]; then
echo "$0 -i inputdir -o outputdir"
exit 1
fi
if [ ! -d "$inputdir" ]; then
echo "Bad inputdir parameter"
exit 2
fi
if [ ! -d "$outputdir" ]; then
echo "Bad outputdir parameter"
exit 2
fi
tempfile="`mktemp`"
sizetable='
size_table["string"] = -1;
size_table["bytes"] = -1;
size_table["char"] = 1;
size_table["uint8"] = 1;
size_table["int8"] = 1;
size_table["uint16"] = 2;
size_table["int16"] = 2;
size_table["uint32"] = 4;
size_table["int32"] = 4;
size_table["uint64"] = 8;
size_table["int64"] = 8;
size_table["float"] = 4;
size_table["double"] = 8;
size_table["ipaddr"] = 16;
size_table["macaddr"] = 6;
size_table["time"] = 8;
size_table["uint8*"] = -1;
size_table["int8*"] = -1;
size_table["uint16*"] = -2;
size_table["int16*"] = -2;
size_table["uint32*"] = -4;
size_table["int32*"] = -4;
size_table["uint64*"] = -8;
size_table["int64*"] = -8;
size_table["float*"] = -4;
size_table["double*"] = -8;
size_table["ipaddr*"] = -16;
size_table["macaddr*"] = -6;
size_table["time*"] = -8;'
find "$inputdir" \( -name '*.c' -o -name '*.h' -o -name '*.cpp' -o -name '*.hpp' \) -exec grep -l "\s*UR_FIELDS\s*" {} \; |
# remove line and block comments
xargs -I{} sed 's,\s*//.*$,,;:a; s%\(.*\)/\*.*\*/%\1%; ta; /\/\*/ !b; N; ba' {} |
# print contents of UR_FIELDS
sed -n '/UR_FIELDS([^.].*)/p; /^\s*UR_FIELDS\s*([^)]*$/,/)/p; /^\s*UR_FIELDS\s*([^)]*$/,/)/p' 2>/dev/null |
# clean output to get fields only one on line
sed 's/,/\n/g' |
sed 's/^\s*UR_FIELDS\s*(\s*//g; s/)//g; s/,/\n/g; /^\s*$/d; s/^\s*//; s/\s\s*/ /g; s/\s\s*$//' |
# sort by name
sort -k2 -t' ' | uniq |
# check for conflicting types and print type, name, size of fields
awk -F' ' 'BEGIN{
'"$sizetable"'
}
{
if (NR == 1) {
type=$1;
iden=$2;
}
if ((iden == $2) && (type != $1)) {
printf("Conflicting types (%s, %s) of UniRec field (%s)\n", type, $1, iden);
exit 1;
}
type=$1;
if (NR == 1) {
type=$1;
iden=$2;
}
if ((iden == $2) && (type != $1)) {
printf("Conflicting types (%s, %s) of UniRec field (%s)\n", type, $1, iden);
exit 1;
}
type=$1;
iden=$2;
if ($2 == "*") {
print $1"*", $3, size_table[$1];
} else if (substr($2, 1, 1) == "*") {
print $1"*", substr($2, 2, length($2) - 1), size_table[$1];
} else {
print $1, $2, size_table[$1];
}
}' | sort -k3nr -k2 > "$tempfile"
ret=$?
if [ "$ret" -ne 0 ]; then
rm "$tempfile"
exit "$ret"
fi
# generate fields.{c,h}
awk -F' ' '
BEGIN {
'"$sizetable"'
c_types["string"] = "char";
c_types["bytes"] = "char";
c_types["char"] = "char";
c_types["uint8"] = "uint8_t";
c_types["int8"] = "int8_t";
c_types["uint16"] = "uint16_t";
c_types["int16"] = "int16_t";
c_types["uint32"] = "uint32_t";
c_types["int32"] = "int32_t";
c_types["uint64"] = "uint64_t";
c_types["int64"] = "int64_t";
c_types["float"] = "float";
c_types["double"] = "double";
c_types["ipaddr"] = "ip_addr_t";
c_types["macaddr"] = "mac_addr_t";
c_types["time"] = "ur_time_t";
c_types["uint8*"] = "uint8_t*";
c_types["int8*"] = "int8_t*";
c_types["uint16*"] = "uint16_t*";
c_types["int16*"] = "int16_t*";
c_types["uint32*"] = "uint32_t*";
c_types["int32*"] = "int32_t*";
c_types["uint64*"] = "uint64_t*";
c_types["int64*"] = "int64_t*";
c_types["float*"] = "float*";
c_types["double*"] = "double*";
c_types["ipaddr*"] = "ip_addr_t*";
c_types["macaddr*"] = "mac_addr_t*";
c_types["time*"] = "ur_time_t*";
type_table["string"]="UR_TYPE_STRING";
type_table["bytes"]="UR_TYPE_BYTES";
type_table["char"]="UR_TYPE_CHAR";
type_table["uint8"]="UR_TYPE_UINT8";
type_table["int8"]="UR_TYPE_INT8";
type_table["uint16"]="UR_TYPE_UINT16";
type_table["int16"]="UR_TYPE_INT16";
type_table["uint32"]="UR_TYPE_UINT32";
type_table["int32"]="UR_TYPE_INT32";
type_table["uint64"]="UR_TYPE_UINT64";
type_table["int64"]="UR_TYPE_INT64";
type_table["float"]="UR_TYPE_FLOAT";
type_table["double"]="UR_TYPE_DOUBLE";
type_table["ipaddr"]="UR_TYPE_IP";
type_table["macaddr"]="UR_TYPE_MAC";
type_table["time"]="UR_TYPE_TIME";
type_table["uint8*"]="UR_TYPE_A_UINT8";
type_table["int8*"]="UR_TYPE_A_INT8";
type_table["uint16*"]="UR_TYPE_A_UINT16";
type_table["int16*"]="UR_TYPE_A_INT16";
type_table["uint32*"]="UR_TYPE_A_UINT32";
type_table["int32*"]="UR_TYPE_A_INT32";
type_table["uint64*"]="UR_TYPE_A_UINT64";
type_table["int64*"]="UR_TYPE_A_INT64";
type_table["float*"]="UR_TYPE_A_FLOAT";
type_table["double*"]="UR_TYPE_A_DOUBLE";
type_table["ipaddr*"]="UR_TYPE_A_IP";
type_table["macaddr*"]="UR_TYPE_A_MAC";
type_table["time*"]="UR_TYPE_A_TIME";
field_id=0;
cfile="/************* THIS IS AUTOMATICALLY GENERATED FILE, DO NOT EDIT *************/\n";
cfile=cfile"// Tables are indexed by field ID\n";
cfile=cfile"#include \"fields.h\"\n";
cnames="char *ur_field_names_static[] = {";
csizes="short ur_field_sizes_static[] = {";
cstatsizes="ur_field_type_t ur_field_types_static[] = {";
hfile="#ifndef _UR_FIELDS_H_\n"
hfile=hfile"#define _UR_FIELDS_H_\n"
hfile=hfile"\n"
hfile=hfile"/************* THIS IS AUTOMATICALLY GENERATED FILE, DO NOT EDIT *************/\n"
hfile=hfile"#include <unirec/unirec.h>\n";
}
{
if ($2 == "") {
print "Error: empty line (NR): ", $0;
} else {
cnames=cnames"\n \""$2"\",";
csizes=csizes"\n "size_table[$1]", /* "$2" */";
cstatsizes=cstatsizes"\n "type_table[$1]", /* "$2" */";
hfile=hfile"\n#define F_"$2" "field_id;
hfile=hfile"\n#define F_"$2"_T "c_types[$1];
field_id++;
}
}
END {
cnames=cnames"\n};";
csizes=csizes"\n};";
cstatsizes=cstatsizes"\n};";
hfile=hfile"\n\nextern uint16_t ur_last_id;\n";
hfile=hfile"extern ur_static_field_specs_t UR_FIELD_SPECS_STATIC;\n";
hfile=hfile"extern ur_field_specs_t ur_field_specs;\n";
hfile=hfile"\n";
hfile=hfile"#endif\n";
cfile=cfile"\n"cnames"\n"csizes"\n"cstatsizes
cfile=cfile"\nur_static_field_specs_t UR_FIELD_SPECS_STATIC = {ur_field_names_static, ur_field_sizes_static, ur_field_types_static, "NR"};\n"
cfile=cfile"ur_field_specs_t ur_field_specs = {ur_field_names_static, ur_field_sizes_static, ur_field_types_static, "NR", "NR", "NR", NULL, UR_UNINITIALIZED};"
print hfile > "'"$outputdir/fields.h"'";
print cfile > "'"$outputdir/fields.c"'";
}
' "$tempfile"
rm "$tempfile"