4f53fc3eb5afe7995d12f78694eaef980b353a6f
[krb5.git] / src / util / et / et_c.awk
1 BEGIN { 
2 char_shift=64
3 ## "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
4 c2n["A"]=1
5 c2n["B"]=2
6 c2n["C"]=3
7 c2n["D"]=4
8 c2n["E"]=5
9 c2n["F"]=6
10 c2n["G"]=7
11 c2n["H"]=8
12 c2n["I"]=9
13 c2n["J"]=10
14 c2n["K"]=11
15 c2n["L"]=12
16 c2n["M"]=13
17 c2n["N"]=14
18 c2n["O"]=15
19 c2n["P"]=16
20 c2n["Q"]=17
21 c2n["R"]=18
22 c2n["S"]=19
23 c2n["T"]=20
24 c2n["U"]=21
25 c2n["V"]=22
26 c2n["W"]=23
27 c2n["X"]=24
28 c2n["Y"]=25
29 c2n["Z"]=26
30 c2n["a"]=27
31 c2n["b"]=28
32 c2n["c"]=29
33 c2n["d"]=30
34 c2n["e"]=31
35 c2n["f"]=32
36 c2n["g"]=33
37 c2n["h"]=34
38 c2n["i"]=35
39 c2n["j"]=36
40 c2n["k"]=37
41 c2n["l"]=38
42 c2n["m"]=39
43 c2n["n"]=40
44 c2n["o"]=41
45 c2n["p"]=42
46 c2n["q"]=43
47 c2n["r"]=44
48 c2n["s"]=45
49 c2n["t"]=46
50 c2n["u"]=47
51 c2n["v"]=48
52 c2n["w"]=49
53 c2n["x"]=50
54 c2n["y"]=51
55 c2n["z"]=52
56 c2n["0"]=53
57 c2n["1"]=54
58 c2n["2"]=55
59 c2n["3"]=56
60 c2n["4"]=57
61 c2n["5"]=58
62 c2n["6"]=59
63 c2n["7"]=60
64 c2n["8"]=61
65 c2n["9"]=62
66 c2n["_"]=63
67 }
68 /^#/ { next }
69 /^[ \t]*(error_table|et)[ \t]+[a-zA-Z][a-zA-Z0-9_]+/ {
70         table_number = 0
71         table_name = $2
72         mod_base = 1000000
73         for(i=1; i<=length(table_name); i++) {
74             table_number=(table_number*char_shift)+c2n[substr(table_name,i,1)]
75         }
76
77         # We start playing *_high, *low games here because the some
78         # awk programs do not have the necessary precision (sigh)
79         tab_base_low = table_number % mod_base
80         tab_base_high = int(table_number / mod_base)
81         tab_base_sign = 1;
82
83         # figure out: table_number_base=table_number*256
84         tab_base_low = tab_base_low * 256
85         tab_base_high = (tab_base_high * 256) + \
86                         int(tab_base_low / mod_base)
87         tab_base_low = tab_base_low % mod_base
88
89         if (table_number > 128*256*256) {
90                 # figure out:  table_number_base -= 256*256*256*256
91                 # sub_high, sub_low is 256*256*256*256
92                 sub_low = 256*256*256 % mod_base
93                 sub_high = int(256*256*256 / mod_base)
94
95                 sub_low = sub_low * 256
96                 sub_high = (sub_high * 256) + int(sub_low / mod_base)
97                 sub_low = sub_low % mod_base
98
99                 tab_base_low = sub_low - tab_base_low;
100                 tab_base_high = sub_high - tab_base_high;
101                 tab_base_sign = -1;
102                 if (tab_base_low < 0) {
103                         tab_base_low = tab_base_low + mod_base
104                         tab_base_high--
105                 }
106         }
107         print "/*" > outfile
108         print " * " outfile ":" > outfile
109         print " * This file is automatically generated; please do not edit it." > outfile
110         print " */" > outfile
111
112         print "#ifdef __STDC__" > outfile
113         print "#define NOARGS void" > outfile
114         print "#else" > outfile
115         print "#define NOARGS" > outfile
116         print "#define const" > outfile
117         print "#endif" > outfile
118         print "" > outfile
119         print "static const char * const text[] = {" > outfile
120         table_item_count = 0
121 }
122
123 /^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*$/ {
124         skipone=1
125         next
126 }
127
128 /^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*".*"[ \t]*$/ {
129         text=""
130         for (i=3; i<=NF; i++) { 
131             text = text FS $i
132         }
133         text=substr(text,2,length(text)-1);
134         printf "\t%s,\n", text > outfile
135         table_item_count++
136 }
137
138
139         if (skipone) {
140             printf "\t%s,\n", $0 > outfile
141             table_item_count++
142         }
143         skipone=0
144 }
145 END {
146
147
148         print "    0" > outfile
149         print "};" > outfile
150         print "" > outfile
151         print "struct error_table {" > outfile
152         print "    char const * const * msgs;" > outfile
153         print "    long base;" > outfile
154         print "    int n_msgs;" > outfile
155         print "};" > outfile
156         print "struct et_list {" > outfile
157         print "    struct et_list *next;" > outfile
158         print "    const struct error_table * table;" > outfile
159         print "};" > outfile
160         print "extern struct et_list *_et_list;" > outfile
161         print "" > outfile
162         if (tab_base_high == 0) {
163             print "static const struct error_table et = { text, " \
164                 sprintf("%dL, %d };", tab_base_sign*tab_base_low, \
165                 table_item_count) > outfile
166         } else {
167             print "static const struct error_table et = { text, " \
168                 sprintf("%d%06dL, %d };", tab_base_sign*tab_base_high, \
169                 tab_base_low, table_item_count) > outfile
170         }
171         print "" > outfile
172         print "static struct et_list link = { 0, 0 };" > outfile
173         print "" > outfile
174         print "void initialize_" table_name "_error_table (NOARGS) {" > outfile
175         print "    if (!link.table) {" > outfile
176         print "        link.next = _et_list;" > outfile
177         print "        link.table = &et;" > outfile
178         print "        _et_list = &link;" > outfile
179         print "    }" > outfile
180         print "}" > outfile
181         
182
183 }