-
Notifications
You must be signed in to change notification settings - Fork 0
/
GENSEARCHDATA
executable file
·292 lines (284 loc) · 7.62 KB
/
GENSEARCHDATA
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/sh
#######
#
# GENSEARCHDATA: Script to take STRINGS.in file and create _autodata.c
# and _autodefs.h files for initializing the search strings structure
# used to search for licenses.
#
# Copyright (C) 2006,2009,2013 Hewlett-Packard Development Company, L.P.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 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.
#
###########
PROG=$(basename $0)
## [ $# -ne 1 ] && echo "Usage: $PROG input-file" && exit 1
## [ ! -f $1 ] && echo "$PROG: $1 is not a file" && exit 1
#
# CDB? TMPDIR=/tmp/,cryptParse$$
INFILE=STRINGS.in
NEW_C=_autodata.c
NEW_H=_autodefs.h
STR_HIST=strings.HISTOGRAM
# CDB? trap "rm -rf $TMPDIR" 0
trap 'echo INTERRUPT!; exit 1' 1 2 3 15
[ ! -f $INFILE ] && echo "$PROG: $INFILE: No such file or directory" && exit 2
#make encode > /dev/null || exit 2
grep -Hn \"\" $INFILE && exit 2
#####
# Start construction of the new header file
#####
cat > $NEW_H <<@EOF@
/*
* $(cat ./GenCodeCopyright)
*****
* License footprints, auto-generated $(date)
*****
* Prefixes on these strings are supposed to indicate the following:
* "_LT" == License Text footprints
* "_KW" == License Keywords
* "_CR" == License Copyrights
* "_MISC" == Miscelleany in licenses
*/
#ifndef _AUTODEFS_H
#define _AUTODEFS_H
@EOF@
#####
# Start construction of the new source file
#####
cat > $NEW_C <<@EOF@
/*
* $(cat ./GenCodeCopyright)
*/
#include "nomos.h"
#include "$NEW_H"
/*
* This source file was auto-generated $(date);
*****
* Changes to this file will NOT be kept; the file will be over-written
* the next time the license-specifications (./$INFILE) change!
*/
/* BEGIN ENCODED LICENSE-FOOTPRINTS SECTION */
licSpec_t licSpec[NFOOTPRINTS] = {
@EOF@
cat > _STRFILTER <<- @EOF@
sed -e 's/ =FEW= /.{0,30}/g' -e 's/ =SOME= /.{0,60}/g' -e 's/ =ANY= /.*/g' \
-e 's/=YEAR=/(19|20)[0-9][0-9][ ,-]+/g'
@EOF@
chmod 755 _STRFILTER
## awk --lint '
awk -v SRC=$NEW_C -v HDR=$NEW_H '
#####
# All strings to be encoded MUST be include double-quotes; this way,
# we can check for syntax-errors and bail out when an error is found.
#####
function syntaxCheck(s) {
if (match(s, "\".*\"") == 0 && match(s, "=NULL=$") == 0) {
printf("ERROR (line %d): string not in quotes\n", nextLine);
exit 1;
}
# if (stateDebug) {
# printf("RSTART = %d, RLENGTH = %d\n", RSTART, RLENGTH);
# }
}
#####
# Initialize.
#####
BEGIN {
lineNo = defineNo = inEntry = gotKey = gotStr = stateDebug = 0;
## stateDebug++;
defined = "";
}
#####
# Ignore comments (lines starting with the # character)
#####
/^#/ {
lineNo++;
nextLine = lineNo+1;
next;
}
#####
# %DEF% - insert a generic #define foo bar line
#####
/^[ ]*%DEF%/ {
if (NF != 3) {
printf("ERROR (line %d), need 2 strings for %DEF%\n", nextLine);
exit 1;
}
printf("#define %s %s\n", $2, $3) >> HDR;
close(HDR);
}
#####
# %ALIAS% - new symbolic name for the last #define created
#####
/^[ ]*%ALIAS%/ {
if (defined == "") {
printf("%ALIAS% error - no previous #define string\n");
exit 1;
}
else if (NF == 1) {
printf("ERROR (line %d), syntax error\n", nextLine);
exit 1;
}
else if ($2 != "") {
printf("#define %s %s\n", $2, defined) >> HDR;
close(HDR);
}
}
#####
# %ENTRY% - create new #define, increment counter (for NEXT #define)
#####
/^[ ]*%ENTRY%/ {
printf("#define %s %d\n", $2, defineNo) >> HDR;
close(HDR);
defined=$2;
inEntry++;
if (stateDebug) {
# printf("... created ifdef #%d (%s)\n", defineNo, defined);
printf("... created ifdef #%d, ", defineNo);
}
}
#####
# %KEY% - specify first of two strings for auto-generated search strings.
# ... this is the lookup string used to find paragraphs of text.
# ... this value must be declared/found PRIOR to the %STR% entry
#####
/^[ ]*%KEY%/ {
syntaxCheck($0);
keyString = sprintf("%s", substr($0, RSTART, RLENGTH));
gotKey++;
}
#####
# %STR% - specify second of two strings for auto-generated search strings
# ... this is the actual text searched for in paragraphs
# ... this value must be declared/found AFTER to the %KEY% entry
#####
/^[ ]*%STR%/ {
syntaxCheck($0);
encodedString = sprintf("%s", substr($0, RSTART, RLENGTH));
gotStr++;
}
#####
# Hmmm, malformed keywords cause troubles, so look for them.
####
/(^[ ]*%ENTRY[ ]|^[ ]*ENTRY%[ ])/ {
lineNo++;
printf("Malformed %ENTRY% keyword/entry, line %d\n", lineNo);
exit(1);
}
/(^[ ]*%KEY[ ]|^[ ]*KEY%[ ])/ {
lineNo++;
printf("Malformed %KEY% keyword/entry, line %d\n", lineNo);
exit(1);
}
/(^[ ]*%STR[ ]|^[ ]*STR%[ ])/ {
lineNo++;
printf("Malformed %STR% keyword/entry, line %d\n", lineNo);
exit(1);
}
/(^[ ]*%DEF[ ]|^[ ]*DEF%[ ])/ {
lineNo++;
printf("Malformed %DEF% keyword/entry, line %d\n", lineNo);
exit(1);
}
/(^[ ]*%ALIAS[ ]|^[ ]*ALIAS%[ ])/ {
lineNo++;
printf("Malformed %ALIAS% keyword/entry, line %d\n", lineNo);
exit(1);
}
{
lineNo++;
if (stateDebug && inEntry) {
printf("... in-ENTRY [%s]", defined);
if (gotKey) {
printf(", with Key %s", keyString);
}
if (gotStr) {
printf(" AND String!");
}
printf("\n");
}
#####
# %KEY% value MUST be declared before %STR% value
#####
if (inEntry && gotStr && !gotKey) {
printf("Error (line %d): %STR% with no %KEY% value\n", \
lineNo);
exit 1;
}
#####
# If we have everything, add the encoded strings to the source file.
# Note we already added what we need to the header file (above).
#####
if (inEntry && gotKey && gotStr) {
# process search-key (seed)
KSlen = length(keyString);
cmd = sprintf("echo %s | ./_STRFILTER > _KTARGET", keyString);
system(cmd);
cmd = sprintf("./encode _KTARGET > _KEY");
system(cmd);
getline < "_KEY"
K=$0
close("_KEY");
# process search-string (the paragraph)
CSlen = length(encodedString);
cmd = sprintf("echo %s | ./_STRFILTER > _STARGET", encodedString);
system(cmd);
cmd = sprintf("./encode _STARGET > _STR");
system(cmd);
getline < "_STR"
S=$0
close("_STR");
# add the entry to the source file
printf("\n#if\t0\n Seed[%s=#%03d]: (%d) %s\n", defined, \
defineNo, KSlen-2, keyString) >> SRC;
printf("Phrase[%s=#%03d]: (%d) %s\n#endif\n", defined, \
defineNo, CSlen-2, encodedString) >> SRC;
printf("{/*Seed*/%s,\n /*Text*/%s},\n", K, S) >> SRC;
# reset everything in preparation for the next line
close(SRC);
inEntry = gotKey = gotStr = 0;
defineNo++;
# if (stateDebug) {
# printf("Next entry == #%d\n", defineNo);
# }
}
nextLine = lineNo+1; # for syntax errors reported above
}
#####
# All done, clean up whether we were successful or not.
#####
END {
cmd = sprintf("rm -f _KEY _STR _KTARGET _STARGET");
system(cmd);
}' < $INFILE || exit 1
#####
# Above, we exit non-zero if awk bailed, and thus we intentionally leave
# the auto-generated source file with a syntax error so we don't build.
# The build _should_ fail until the error is repaired.
#####
grep "%KEY%" $INFILE | grep -v '^#' | sed -e 's/%KEY% / /g' | sort |
uniq -c | sort -nr > $STR_HIST
# [ -f ${NEW_H}_TAIL ] && cat ${NEW_H}_TAIL >> $NEW_H # no remove!
#####
# Complete the source-file so the build won't fail :)
#####
cat >> ${NEW_C} <<@EOF@
};
/* END ENCODED LICENSE-FOOTPRINTS SECTION */
licText_t licText[NFOOTPRINTS];
@EOF@
cat >> $NEW_H <<@EOF@
#endif /* _AUTODEFS_H */
@EOF@
exit 0