xref: /aosp_15_r20/external/e2fsprogs/lib/et/et_c.awk (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard WorkerBEGIN {
2*6a54128fSAndroid Build Coastguard Workerif ( length(outfn) == 0) {
3*6a54128fSAndroid Build Coastguard Worker	outfn = outfile
4*6a54128fSAndroid Build Coastguard Worker}
5*6a54128fSAndroid Build Coastguard Workerchar_shift=64
6*6a54128fSAndroid Build Coastguard Worker## "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
7*6a54128fSAndroid Build Coastguard Workerc2n["A"]=1
8*6a54128fSAndroid Build Coastguard Workerc2n["B"]=2
9*6a54128fSAndroid Build Coastguard Workerc2n["C"]=3
10*6a54128fSAndroid Build Coastguard Workerc2n["D"]=4
11*6a54128fSAndroid Build Coastguard Workerc2n["E"]=5
12*6a54128fSAndroid Build Coastguard Workerc2n["F"]=6
13*6a54128fSAndroid Build Coastguard Workerc2n["G"]=7
14*6a54128fSAndroid Build Coastguard Workerc2n["H"]=8
15*6a54128fSAndroid Build Coastguard Workerc2n["I"]=9
16*6a54128fSAndroid Build Coastguard Workerc2n["J"]=10
17*6a54128fSAndroid Build Coastguard Workerc2n["K"]=11
18*6a54128fSAndroid Build Coastguard Workerc2n["L"]=12
19*6a54128fSAndroid Build Coastguard Workerc2n["M"]=13
20*6a54128fSAndroid Build Coastguard Workerc2n["N"]=14
21*6a54128fSAndroid Build Coastguard Workerc2n["O"]=15
22*6a54128fSAndroid Build Coastguard Workerc2n["P"]=16
23*6a54128fSAndroid Build Coastguard Workerc2n["Q"]=17
24*6a54128fSAndroid Build Coastguard Workerc2n["R"]=18
25*6a54128fSAndroid Build Coastguard Workerc2n["S"]=19
26*6a54128fSAndroid Build Coastguard Workerc2n["T"]=20
27*6a54128fSAndroid Build Coastguard Workerc2n["U"]=21
28*6a54128fSAndroid Build Coastguard Workerc2n["V"]=22
29*6a54128fSAndroid Build Coastguard Workerc2n["W"]=23
30*6a54128fSAndroid Build Coastguard Workerc2n["X"]=24
31*6a54128fSAndroid Build Coastguard Workerc2n["Y"]=25
32*6a54128fSAndroid Build Coastguard Workerc2n["Z"]=26
33*6a54128fSAndroid Build Coastguard Workerc2n["a"]=27
34*6a54128fSAndroid Build Coastguard Workerc2n["b"]=28
35*6a54128fSAndroid Build Coastguard Workerc2n["c"]=29
36*6a54128fSAndroid Build Coastguard Workerc2n["d"]=30
37*6a54128fSAndroid Build Coastguard Workerc2n["e"]=31
38*6a54128fSAndroid Build Coastguard Workerc2n["f"]=32
39*6a54128fSAndroid Build Coastguard Workerc2n["g"]=33
40*6a54128fSAndroid Build Coastguard Workerc2n["h"]=34
41*6a54128fSAndroid Build Coastguard Workerc2n["i"]=35
42*6a54128fSAndroid Build Coastguard Workerc2n["j"]=36
43*6a54128fSAndroid Build Coastguard Workerc2n["k"]=37
44*6a54128fSAndroid Build Coastguard Workerc2n["l"]=38
45*6a54128fSAndroid Build Coastguard Workerc2n["m"]=39
46*6a54128fSAndroid Build Coastguard Workerc2n["n"]=40
47*6a54128fSAndroid Build Coastguard Workerc2n["o"]=41
48*6a54128fSAndroid Build Coastguard Workerc2n["p"]=42
49*6a54128fSAndroid Build Coastguard Workerc2n["q"]=43
50*6a54128fSAndroid Build Coastguard Workerc2n["r"]=44
51*6a54128fSAndroid Build Coastguard Workerc2n["s"]=45
52*6a54128fSAndroid Build Coastguard Workerc2n["t"]=46
53*6a54128fSAndroid Build Coastguard Workerc2n["u"]=47
54*6a54128fSAndroid Build Coastguard Workerc2n["v"]=48
55*6a54128fSAndroid Build Coastguard Workerc2n["w"]=49
56*6a54128fSAndroid Build Coastguard Workerc2n["x"]=50
57*6a54128fSAndroid Build Coastguard Workerc2n["y"]=51
58*6a54128fSAndroid Build Coastguard Workerc2n["z"]=52
59*6a54128fSAndroid Build Coastguard Workerc2n["0"]=53
60*6a54128fSAndroid Build Coastguard Workerc2n["1"]=54
61*6a54128fSAndroid Build Coastguard Workerc2n["2"]=55
62*6a54128fSAndroid Build Coastguard Workerc2n["3"]=56
63*6a54128fSAndroid Build Coastguard Workerc2n["4"]=57
64*6a54128fSAndroid Build Coastguard Workerc2n["5"]=58
65*6a54128fSAndroid Build Coastguard Workerc2n["6"]=59
66*6a54128fSAndroid Build Coastguard Workerc2n["7"]=60
67*6a54128fSAndroid Build Coastguard Workerc2n["8"]=61
68*6a54128fSAndroid Build Coastguard Workerc2n["9"]=62
69*6a54128fSAndroid Build Coastguard Workerc2n["_"]=63
70*6a54128fSAndroid Build Coastguard Worker}
71*6a54128fSAndroid Build Coastguard Worker/^#/ { next }
72*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(error_table|et)[ \t]+[a-zA-Z][a-zA-Z0-9_]+/ {
73*6a54128fSAndroid Build Coastguard Worker	table_number = 0
74*6a54128fSAndroid Build Coastguard Worker	mod_base = 1000000
75*6a54128fSAndroid Build Coastguard Worker	if (NF > 2) {
76*6a54128fSAndroid Build Coastguard Worker	    table_name = $3
77*6a54128fSAndroid Build Coastguard Worker	    base_name = $2
78*6a54128fSAndroid Build Coastguard Worker	} else {
79*6a54128fSAndroid Build Coastguard Worker	    table_name = $2
80*6a54128fSAndroid Build Coastguard Worker	    base_name = table_name
81*6a54128fSAndroid Build Coastguard Worker	}
82*6a54128fSAndroid Build Coastguard Worker	for(i=1; i<=length(base_name); i++) {
83*6a54128fSAndroid Build Coastguard Worker	    table_number=(table_number*char_shift)+c2n[substr(base_name,i,1)]
84*6a54128fSAndroid Build Coastguard Worker	}
85*6a54128fSAndroid Build Coastguard Worker
86*6a54128fSAndroid Build Coastguard Worker	# We start playing *_high, *low games here because the some
87*6a54128fSAndroid Build Coastguard Worker	# awk programs do not have the necessary precision (sigh)
88*6a54128fSAndroid Build Coastguard Worker	tab_base_low = table_number % mod_base
89*6a54128fSAndroid Build Coastguard Worker	if (tab_base_low < 0) {
90*6a54128fSAndroid Build Coastguard Worker		# Work around stupid bug in the ARM libm
91*6a54128fSAndroid Build Coastguard Worker		tab_base_low = tab_base_low + mod_base
92*6a54128fSAndroid Build Coastguard Worker	}
93*6a54128fSAndroid Build Coastguard Worker	tab_base_high = int(table_number / mod_base)
94*6a54128fSAndroid Build Coastguard Worker	tab_base_sign = 1;
95*6a54128fSAndroid Build Coastguard Worker
96*6a54128fSAndroid Build Coastguard Worker	# figure out: table_number_base=table_number*256
97*6a54128fSAndroid Build Coastguard Worker	tab_base_low = tab_base_low * 256
98*6a54128fSAndroid Build Coastguard Worker	tab_base_high = (tab_base_high * 256) + \
99*6a54128fSAndroid Build Coastguard Worker			int(tab_base_low / mod_base)
100*6a54128fSAndroid Build Coastguard Worker	tab_base_low = tab_base_low % mod_base
101*6a54128fSAndroid Build Coastguard Worker	if (tab_base_low < 0) {
102*6a54128fSAndroid Build Coastguard Worker		# Work around stupid bug in the ARM libm
103*6a54128fSAndroid Build Coastguard Worker		tab_base_low = tab_base_low + mod_base
104*6a54128fSAndroid Build Coastguard Worker	}
105*6a54128fSAndroid Build Coastguard Worker
106*6a54128fSAndroid Build Coastguard Worker	if (table_number > 128*256*256) {
107*6a54128fSAndroid Build Coastguard Worker		# figure out:  table_number_base -= 256*256*256*256
108*6a54128fSAndroid Build Coastguard Worker		# sub_high, sub_low is 256*256*256*256
109*6a54128fSAndroid Build Coastguard Worker		sub_low = 256*256*256 % mod_base
110*6a54128fSAndroid Build Coastguard Worker		sub_high = int(256*256*256 / mod_base)
111*6a54128fSAndroid Build Coastguard Worker
112*6a54128fSAndroid Build Coastguard Worker		sub_low = sub_low * 256
113*6a54128fSAndroid Build Coastguard Worker		sub_high = (sub_high * 256) + int(sub_low / mod_base)
114*6a54128fSAndroid Build Coastguard Worker		sub_low = sub_low % mod_base
115*6a54128fSAndroid Build Coastguard Worker
116*6a54128fSAndroid Build Coastguard Worker		tab_base_low = sub_low - tab_base_low;
117*6a54128fSAndroid Build Coastguard Worker		tab_base_high = sub_high - tab_base_high;
118*6a54128fSAndroid Build Coastguard Worker		tab_base_sign = -1;
119*6a54128fSAndroid Build Coastguard Worker		if (tab_base_low < 0) {
120*6a54128fSAndroid Build Coastguard Worker			tab_base_low = tab_base_low + mod_base
121*6a54128fSAndroid Build Coastguard Worker			tab_base_high--
122*6a54128fSAndroid Build Coastguard Worker		}
123*6a54128fSAndroid Build Coastguard Worker	}
124*6a54128fSAndroid Build Coastguard Worker	print "/*" > outfile
125*6a54128fSAndroid Build Coastguard Worker	print " * " outfn ":" > outfile
126*6a54128fSAndroid Build Coastguard Worker	print " * This file is automatically generated; please do not edit it." > outfile
127*6a54128fSAndroid Build Coastguard Worker	print " */" > outfile
128*6a54128fSAndroid Build Coastguard Worker
129*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
130*6a54128fSAndroid Build Coastguard Worker	print "#include <stdlib.h>" > outfile
131*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
132*6a54128fSAndroid Build Coastguard Worker	print "#define N_(a) a" > outfile
133*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
134*6a54128fSAndroid Build Coastguard Worker	print "static const char * const text[] = {" > outfile
135*6a54128fSAndroid Build Coastguard Worker	table_item_count = 0
136*6a54128fSAndroid Build Coastguard Worker}
137*6a54128fSAndroid Build Coastguard Worker
138*6a54128fSAndroid Build Coastguard Worker(continuation == 1) && ($0 ~ /\\[ \t]*$/) {
139*6a54128fSAndroid Build Coastguard Worker	text=substr($0,1,length($0)-1);
140*6a54128fSAndroid Build Coastguard Worker#	printf "\t\t\"%s\"\n", text > outfile
141*6a54128fSAndroid Build Coastguard Worker	cont_buf=cont_buf text;
142*6a54128fSAndroid Build Coastguard Worker}
143*6a54128fSAndroid Build Coastguard Worker
144*6a54128fSAndroid Build Coastguard Worker(continuation == 1) && ($0 ~ /"[ \t]*$/) {
145*6a54128fSAndroid Build Coastguard Worker#	printf "\t\t\"%s,\n", $0 > outfile
146*6a54128fSAndroid Build Coastguard Worker	printf "\tN_(%s),\n", cont_buf $0 > outfile
147*6a54128fSAndroid Build Coastguard Worker	continuation = 0;
148*6a54128fSAndroid Build Coastguard Worker}
149*6a54128fSAndroid Build Coastguard Worker
150*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[^ \t]/ {
151*6a54128fSAndroid Build Coastguard Worker	# Be tolerant to missing whitespace after `,' ...
152*6a54128fSAndroid Build Coastguard Worker	sub(/,/, ", ")
153*6a54128fSAndroid Build Coastguard Worker}
154*6a54128fSAndroid Build Coastguard Worker
155*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*$/ {
156*6a54128fSAndroid Build Coastguard Worker	table_item_count++
157*6a54128fSAndroid Build Coastguard Worker	skipone=1
158*6a54128fSAndroid Build Coastguard Worker	next
159*6a54128fSAndroid Build Coastguard Worker}
160*6a54128fSAndroid Build Coastguard Worker
161*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*".*"[ \t]*$/ {
162*6a54128fSAndroid Build Coastguard Worker	text=""
163*6a54128fSAndroid Build Coastguard Worker	for (i=3; i<=NF; i++) {
164*6a54128fSAndroid Build Coastguard Worker	    text = text FS $i
165*6a54128fSAndroid Build Coastguard Worker	}
166*6a54128fSAndroid Build Coastguard Worker	text=substr(text,2,length(text)-1);
167*6a54128fSAndroid Build Coastguard Worker	printf "\tN_(%s),\n", text > outfile
168*6a54128fSAndroid Build Coastguard Worker	table_item_count++
169*6a54128fSAndroid Build Coastguard Worker}
170*6a54128fSAndroid Build Coastguard Worker
171*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*".*\\[ \t]*$/ {
172*6a54128fSAndroid Build Coastguard Worker	text=""
173*6a54128fSAndroid Build Coastguard Worker	for (i=3; i<=NF; i++) {
174*6a54128fSAndroid Build Coastguard Worker	    text = text FS $i
175*6a54128fSAndroid Build Coastguard Worker	}
176*6a54128fSAndroid Build Coastguard Worker	text=substr(text,2,length(text)-2);
177*6a54128fSAndroid Build Coastguard Worker#	printf "\t%s\"\n", text > outfile
178*6a54128fSAndroid Build Coastguard Worker	cont_buf=text
179*6a54128fSAndroid Build Coastguard Worker	table_item_count++
180*6a54128fSAndroid Build Coastguard Worker	continuation++;
181*6a54128fSAndroid Build Coastguard Worker}
182*6a54128fSAndroid Build Coastguard Worker
183*6a54128fSAndroid Build Coastguard Worker/^[ \t]*".*\\[ \t]*$/ {
184*6a54128fSAndroid Build Coastguard Worker	if (skipone) {
185*6a54128fSAndroid Build Coastguard Worker	    text=substr($0,1,length($0)-1);
186*6a54128fSAndroid Build Coastguard Worker#	    printf "\t%s\"\n", text > outfile
187*6a54128fSAndroid Build Coastguard Worker	    cont_buf=text
188*6a54128fSAndroid Build Coastguard Worker	    continuation++;
189*6a54128fSAndroid Build Coastguard Worker	}
190*6a54128fSAndroid Build Coastguard Worker	skipone=0
191*6a54128fSAndroid Build Coastguard Worker}
192*6a54128fSAndroid Build Coastguard Worker
193*6a54128fSAndroid Build Coastguard Worker{
194*6a54128fSAndroid Build Coastguard Worker	if (skipone) {
195*6a54128fSAndroid Build Coastguard Worker	    printf "\tN_(%s),\n", $0 > outfile
196*6a54128fSAndroid Build Coastguard Worker	}
197*6a54128fSAndroid Build Coastguard Worker	skipone=0
198*6a54128fSAndroid Build Coastguard Worker}
199*6a54128fSAndroid Build Coastguard Worker
200*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(prefix)$/ {
201*6a54128fSAndroid Build Coastguard Worker	prefix_str = ""
202*6a54128fSAndroid Build Coastguard Worker}
203*6a54128fSAndroid Build Coastguard Worker
204*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(prefix)[ \t]+[A-Z_0-9]+/ {
205*6a54128fSAndroid Build Coastguard Worker	prefix_str = $2 "_"
206*6a54128fSAndroid Build Coastguard Worker}
207*6a54128fSAndroid Build Coastguard Worker
208*6a54128fSAndroid Build Coastguard Worker/^[ \t]*(index)[ \t]+[A-Z_0-9]+/ {
209*6a54128fSAndroid Build Coastguard Worker	new_idx = $2
210*6a54128fSAndroid Build Coastguard Worker	for (i = table_item_count ; i < new_idx; i++) {
211*6a54128fSAndroid Build Coastguard Worker		printf "\tN_(\"Reserved %s error (%d)\"),\n", \
212*6a54128fSAndroid Build Coastguard Worker			table_name, table_item_count++ > outfile
213*6a54128fSAndroid Build Coastguard Worker	}
214*6a54128fSAndroid Build Coastguard Worker}
215*6a54128fSAndroid Build Coastguard Worker
216*6a54128fSAndroid Build Coastguard WorkerEND {
217*6a54128fSAndroid Build Coastguard Worker	print "    0" > outfile
218*6a54128fSAndroid Build Coastguard Worker	print "};" > outfile
219*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
220*6a54128fSAndroid Build Coastguard Worker	print "struct error_table {" > outfile
221*6a54128fSAndroid Build Coastguard Worker	print "    char const * const * msgs;" > outfile
222*6a54128fSAndroid Build Coastguard Worker	print "    long base;" > outfile
223*6a54128fSAndroid Build Coastguard Worker	print "    int n_msgs;" > outfile
224*6a54128fSAndroid Build Coastguard Worker	print "};" > outfile
225*6a54128fSAndroid Build Coastguard Worker	print "struct et_list {" > outfile
226*6a54128fSAndroid Build Coastguard Worker	print "    struct et_list *next;" > outfile
227*6a54128fSAndroid Build Coastguard Worker	print "    const struct error_table * table;" > outfile
228*6a54128fSAndroid Build Coastguard Worker	print "};" > outfile
229*6a54128fSAndroid Build Coastguard Worker	print "extern struct et_list *_et_list;" > outfile
230*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
231*6a54128fSAndroid Build Coastguard Worker	if (tab_base_high == 0) {
232*6a54128fSAndroid Build Coastguard Worker		print "const struct error_table et_" table_name "_error_table = { text, " \
233*6a54128fSAndroid Build Coastguard Worker		sprintf("%dL, %d };", tab_base_sign*tab_base_low, \
234*6a54128fSAndroid Build Coastguard Worker		table_item_count) > outfile
235*6a54128fSAndroid Build Coastguard Worker	} else {
236*6a54128fSAndroid Build Coastguard Worker		print "const struct error_table et_" table_name "_error_table = { text, " \
237*6a54128fSAndroid Build Coastguard Worker		sprintf("%d%06dL, %d };", tab_base_sign*tab_base_high, \
238*6a54128fSAndroid Build Coastguard Worker		tab_base_low, table_item_count) > outfile
239*6a54128fSAndroid Build Coastguard Worker	}
240*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
241*6a54128fSAndroid Build Coastguard Worker	print "static struct et_list link = { 0, 0 };" > outfile
242*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
243*6a54128fSAndroid Build Coastguard Worker	print "void initialize_" table_name "_error_table_r(struct et_list **list);" > outfile
244*6a54128fSAndroid Build Coastguard Worker	print "void initialize_" table_name "_error_table(void);" > outfile
245*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
246*6a54128fSAndroid Build Coastguard Worker	print "void initialize_" table_name "_error_table(void) {" > outfile
247*6a54128fSAndroid Build Coastguard Worker	print "    initialize_" table_name "_error_table_r(&_et_list);" > outfile
248*6a54128fSAndroid Build Coastguard Worker	print "}" > outfile
249*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
250*6a54128fSAndroid Build Coastguard Worker	print "/* For Heimdal compatibility */" > outfile
251*6a54128fSAndroid Build Coastguard Worker	print "void initialize_" table_name "_error_table_r(struct et_list **list)" > outfile
252*6a54128fSAndroid Build Coastguard Worker	print "{" > outfile
253*6a54128fSAndroid Build Coastguard Worker	print "    struct et_list *et, **end;" > outfile
254*6a54128fSAndroid Build Coastguard Worker	print "" > outfile
255*6a54128fSAndroid Build Coastguard Worker	print "    for (end = list, et = *list; et; end = &et->next, et = et->next)" > outfile
256*6a54128fSAndroid Build Coastguard Worker	print "        if (et->table->msgs == text)" > outfile
257*6a54128fSAndroid Build Coastguard Worker	print "            return;" > outfile
258*6a54128fSAndroid Build Coastguard Worker	print "    et = malloc(sizeof(struct et_list));" > outfile
259*6a54128fSAndroid Build Coastguard Worker	print "    if (et == 0) {" > outfile
260*6a54128fSAndroid Build Coastguard Worker	print "        if (!link.table)" > outfile
261*6a54128fSAndroid Build Coastguard Worker	print "            et = &link;" > outfile
262*6a54128fSAndroid Build Coastguard Worker	print "        else" > outfile
263*6a54128fSAndroid Build Coastguard Worker	print "            return;" > outfile
264*6a54128fSAndroid Build Coastguard Worker	print "    }" > outfile
265*6a54128fSAndroid Build Coastguard Worker	print "    et->table = &et_" table_name "_error_table;" > outfile
266*6a54128fSAndroid Build Coastguard Worker	print "    et->next = 0;" > outfile
267*6a54128fSAndroid Build Coastguard Worker	print "    *end = et;" > outfile
268*6a54128fSAndroid Build Coastguard Worker	print "}" > outfile
269*6a54128fSAndroid Build Coastguard Worker}
270