Fix false resource release <-> double free (Bob Tennent)..
[s-mailx.git] / create-okey-map.pl
blob8aab4b5e3c19ab422e94a3766f3b65b09bafa4b3
1 #!/usr/bin/env perl
2 require 5.008_001;
3 use utf8;
4 #@ Parse 'enum okeys' from nail.h and create okeys.h. And see accmacvar.c.
5 # Public Domain
7 # Acceptable "longest distance" from hash-modulo-index to key
8 my $MAXDISTANCE_PENALTY = 5;
10 my $OUT = 'okeys.h';
12 ## -- >8 -- 8< -- ##
14 use diagnostics -verbose;
15 use strict;
16 use warnings;
18 use sigtrap qw(handler cleanup normal-signals);
20 my (@ENTS, $CTOOL, $CTOOL_EXE);
22 sub main_fun {
23 parse_nail_h();
25 create_c_tool();
27 hash_em();
28 dump_keydat_varmap();
30 reverser();
32 cleanup(undef);
33 exit 0
36 sub cleanup {
37 die "$CTOOL_EXE: couldn't unlink: $^E"
38 if $CTOOL_EXE && -f $CTOOL_EXE && 1 != unlink $CTOOL_EXE;
39 die "$CTOOL: couldn't unlink: $^E"
40 if $CTOOL && -f $CTOOL && 1 != unlink $CTOOL;
41 die "Terminating due to signal $_[0]" if $_[0]
44 sub parse_nail_h {
45 die "nail.h: open: $^E" unless open F, '<', 'nail.h';
46 my ($init) = (0);
47 while (<F>) {
48 # Only want the enum okeys content
49 if (/^enum okeys/) {$init = 1; next}
50 if (/^};/) {if ($init) {$init = 2; last}; next}
51 $init || next;
53 # Ignore empty and comment lines
54 /^$/ && next;
55 /^\s*\/\*/ && next;
57 # An entry may have a comment with special directives
58 /^\s*(\w+),?\s*(?:\/\*\s*(?:{(.*)})\s*\*\/\s*)?$/;
59 my ($k, $x) = ($1, $2);
60 my %vals;
61 $vals{enum} = $k;
62 $vals{binary} = ($k =~ /^ok_b/ ? 1 : 0);
63 $k = $1 if $k =~ /^ok_[bv]_(.+)$/;
64 $k =~ s/_/-/g;
65 $vals{name} = $k;
66 if ($x) {
67 while ($x && $x =~ /^([^,]+?)(?:,(.*))?$/) {
68 $x = $2;
69 $1 =~ /([^=]+)=(.+)/;
70 die "Unsupported special directive: $1"
71 if ($1 ne 'name' && $1 ne 'rdonly' &&
72 $1 ne 'special' && $1 ne 'virtual');
73 $vals{$1} = $2
76 push @ENTS, \%vals
78 if ($init != 2) {die 'nail.h does not have the expected content'}
79 close F
82 sub create_c_tool {
83 $CTOOL = './tmp-okey-tool-' . $$ . '.c';
84 $CTOOL_EXE = $CTOOL . '.exe';
86 die "$CTOOL: open: $^E" unless open F, '>', $CTOOL;
87 # xxx optimize: could read lines and write lines in HASH_MODE..
88 print F '#define MAX_DISTANCE_PENALTY ', $MAXDISTANCE_PENALTY, "\n";
89 # >>>>>>>>>>>>>>>>>>>
90 print F <<'__EOT';
91 #define __CREATE_OKEY_MAP_PL
92 #include <stdint.h>
93 #include <stdlib.h>
94 #include <stdio.h>
95 #include <string.h>
97 #ifndef NELEM
98 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
99 #endif
101 #define ui32_t uint32_t
102 #define ui16_t uint16_t
103 #define ui8_t uint8_t
105 enum var_map_flags {
106 VM_NONE = 0,
107 VM_BINARY = 1<<0, /* ok_b_* */
108 VM_RDONLY = 1<<1, /* May not be set by user */
109 VM_SPECIAL = 1<<2, /* Wants _var_check_specials() evaluation */
110 VM_VIRTUAL = 1<<3 /* "Stateless": no var* -- implies VM_RDONLY */
113 /* Binary compatible with struct var! (xxx make it superclass?) */
114 struct var_vx {
115 struct var *v_link;
116 char *v_value;
119 struct var_virtual {
120 ui32_t vv_okey;
121 struct var_vx vv_var;
124 struct var_map {
125 ui32_t vm_hash;
126 ui16_t vm_keyoff;
127 ui16_t vm_flags; /* var_map_flags bits */
130 #ifdef HASH_MODE
131 /* NOTE: copied over verbatim from auxlily.c */
132 static ui32_t
133 torek_hash(char const *name)
135 /* Chris Torek's hash.
136 * NOTE: need to change *at least* create-okey-map.pl when changing the
137 * algorithm!! */
138 ui32_t h = 0;
140 while (*name != '\0') {
141 h *= 33;
142 h += *name++;
144 return h;
147 #else
148 /* Include what has been written in HASH_MODE */
149 # include "okeys.h"
151 static ui8_t seen_wraparound;
152 static size_t longest_distance;
154 static size_t
155 next_prime(size_t no) /* blush (brute force) */
157 jredo:
158 ++no;
159 for (size_t i = 3; i < no; i += 2)
160 if (no % i == 0)
161 goto jredo;
162 return no;
165 static size_t *
166 reversy(size_t size)
168 struct var_map const *vmp = _var_map, *vmaxp = vmp + NELEM(_var_map);
169 size_t ldist = 0, *arr;
171 arr = malloc(sizeof *arr * size);
172 for (size_t i = 0; i < size; ++i)
173 arr[i] = NELEM(_var_map);
175 seen_wraparound = 0;
176 longest_distance = 0;
178 while (vmp < vmaxp) {
179 ui32_t hash = vmp->vm_hash, i = hash % size, l;
181 for (l = 0; arr[i] != NELEM(_var_map); ++l)
182 if (++i == size) {
183 seen_wraparound = 1;
184 i = 0;
186 if (l > longest_distance)
187 longest_distance = l;
188 arr[i] = (size_t)(vmp++ - _var_map);
190 return arr;
192 #endif /* !HASH_MODE */
195 main(int argc, char **argv)
197 #ifdef HASH_MODE
198 size_t h = torek_hash(argv[1]);
200 printf("%lu\n", (unsigned long)h);
202 #else
203 size_t *arr, size = NELEM(_var_map);
205 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
206 for (;;) {
207 arr = reversy(size = next_prime(size));
208 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
209 size, longest_distance, seen_wraparound);
210 if (longest_distance <= MAX_DISTANCE_PENALTY)
211 break;
212 free(arr);
215 printf(
216 "#define _VAR_REV_ILL %zuu\n"
217 "#define _VAR_REV_PRIME %zuu\n"
218 "#define _VAR_REV_LONGEST %zuu\n"
219 "#define _VAR_REV_WRAPAROUND %d\n"
220 "static %s const _var_revmap[_VAR_REV_PRIME] = {\n ",
221 NELEM(_var_map), size, longest_distance, seen_wraparound, argv[1]);
222 for (size_t i = 0; i < size; ++i)
223 printf("%s%zuu", (i == 0 ? "" : (i % 10 == 0 ? ",\n " : ", ")), arr[i]);
224 printf("\n};\n");
225 #endif
226 return 0;
228 __EOT
229 # <<<<<<<<<<<<<<<<<<<
230 close F
233 sub hash_em {
234 system("c99 -DHASH_MODE -I. -o $CTOOL_EXE $CTOOL");
236 foreach my $e (@ENTS) {
237 my $h = `$CTOOL_EXE $e->{name}`;
238 chomp $h;
239 $e->{hash} = $h
243 sub dump_keydat_varmap {
244 die "$OUT: open: $^E" unless open F, '>', $OUT;
245 print F "/*@ $OUT, generated by $0 on ", scalar gmtime(), ".\n",
246 " *@ See accmacvar.c for more */\n\n";
248 print F 'static char const _var_keydat[] = {', "\n";
249 my ($i, $alen) = (0, 0);
250 my %virts;
251 foreach my $e (@ENTS) {
252 $e->{keyoff} = $alen;
253 my $k = $e->{name};
254 my $l = length $k;
255 my $a = join '\',\'', split(//, $k);
256 my ($f, $s) = ('', ', ');
257 if ($e->{binary}) {$f .= $s . 'VM_BINARY'; $s = ' | '}
258 if ($e->{rdonly}) {$f .= $s . 'VM_RDONLY'; $s = ' | '}
259 if ($e->{special}) {$f .= $s . 'VM_SPECIAL'; $s = ' | '}
260 if ($e->{virtual}) {
261 die("*$k*: virtual MUST be rdonly, too!") unless $e->{rdonly};
262 $f .= $s . 'VM_VIRTUAL'; $s = ' | ';
263 $virts{$k} = $e;
265 print F " /* $i. [$alen]+$l $k$f */\n", " '$a','\\0',\n";
266 ++$i;
267 $alen += $l + 1
269 print F '};', "\n\n";
271 print F 'static struct var_map const _var_map[] = {', "\n";
272 foreach my $e (@ENTS) {
273 my $f = 'VM_NONE';
274 $f .= ' | VM_BINARY' if $e->{binary};
275 $f .= ' | VM_RDONLY' if $e->{rdonly};
276 $f .= ' | VM_SPECIAL' if $e->{special};
277 $f .= ' | VM_VIRTUAL' if $e->{virtual};
278 my $n = $1 if $e->{enum} =~ /ok_._(.*)/;
279 print F " {$e->{hash}u, $e->{keyoff}u, $f}, /* $n */\n"
281 print F '};', "\n\n";
283 # We have at least version stuff in here
284 # The problem is that struct var uses a variable sized character buffer
285 # which cannot be initialized in a conforming way :(
286 print F "#ifndef __CREATE_OKEY_MAP_PL\n";
287 print F " /* Unfortunately init of varsized buffer won't work */\n";
288 foreach my $k (keys %virts) {
289 my $e = $virts{$k};
290 $e->{vname} = $1 if $e->{enum} =~ /ok_._(.*)/;
291 $e->{vstruct} = "var_virt_$e->{vname}";
292 print F "static struct {\n";
293 print F " struct var *v_link;\n";
294 print F " char const *v_value;\n";
295 print F " char const v_name[", length($e->{name}), " +1];\n";
296 print F "} const _$e->{vstruct} = ",
297 "{NULL, $e->{virtual}, \"$e->{name}\"};\n\n";
300 print F 'static struct var_virtual const _var_virtuals[] = {', "\n";
301 foreach my $k (keys %virts) {
302 my $e = $virts{$k};
303 my $n = $1 if $e->{enum} =~ /ok_._(.*)/;
304 print F " {$e->{enum}, (void const*)&_$e->{vstruct}},\n";
306 print F "};\n#endif /* __CREATE_OKEY_MAP_PL */\n\n";
308 die "$OUT: close: $^E" unless close F
311 sub reverser {
312 system("c99 -I. -o $CTOOL_EXE $CTOOL");
313 my $t = (@ENTS < 0xFF ? 'ui8_t' : (@ENTS < 0xFFFF ? 'ui16_t' : 'ui32_t'));
314 `$CTOOL_EXE $t >> $OUT`
317 {package main; main_fun()}
319 # s-it-mode