4 #@ Parse 'enum okeys' from nail.h and create okeys.h. And see accmacvar.c.
7 # Acceptable "longest distance" from hash-modulo-index to key
8 my $MAXDISTANCE_PENALTY = 5;
14 use diagnostics
-verbose
;
18 use sigtrap
qw(handler cleanup normal-signals);
20 my (@ENTS, $CTOOL, $CTOOL_EXE);
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]
45 die "nail.h: open: $^E" unless open F
, '<', 'nail.h';
48 # Only want the enum okeys content
49 if (/^enum okeys/) {$init = 1; next}
50 if (/^};/) {if ($init) {$init = 2; last}; next}
53 # Ignore empty and comment lines
57 # An entry may have a comment with special directives
58 /^\s*(\w+),?\s*(?:\/\
*\s
*(?
:{(.*)})\s
*\
*\
/\s*)?$/;
60 my ($k, $x) = ($1, $2);
63 $vals{boolean
} = ($k =~ /^ok_b/ ?
1 : 0);
64 $k = $1 if $k =~ /^ok_[bv]_(.+)$/;
68 while ($x && $x =~ /^([^,]+?)(?:,(.*))?$/) {
71 die "Unsupported special directive: $1"
72 if ($1 ne 'name' && $1 ne 'rdonly' &&
73 $1 ne 'special' && $1 ne 'virtual');
79 if ($init != 2) {die 'nail.h does not have the expected content'}
84 $CTOOL = './tmp-okey-tool-' . $$ . '.c';
85 $CTOOL_EXE = $CTOOL . '.exe';
87 die "$CTOOL: open: $^E" unless open F
, '>', $CTOOL;
88 # xxx optimize: could read lines and write lines in HASH_MODE..
89 print F
'#define MAX_DISTANCE_PENALTY ', $MAXDISTANCE_PENALTY, "\n";
92 #define __CREATE_OKEY_MAP_PL
99 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
102 #define ui32_t uint32_t
103 #define ui16_t uint16_t
104 #define ui8_t uint8_t
108 VM_BOOLEAN
= 1<<0, /* ok_b_* */
109 VM_RDONLY
= 1<<1, /* May not be set by user */
110 VM_SPECIAL
= 1<<2, /* Wants _var_check_specials() evaluation */
111 VM_VIRTUAL
= 1<<3 /* "Stateless": no var* -- implies VM_RDONLY */
114 /* Binary compatible with struct var! (xxx make it superclass?) */
122 struct var_vx vv_var
;
128 ui16_t vm_flags
; /* var_map_flags bits */
132 /* NOTE: copied over verbatim from auxlily.c */
134 torek_hash
(char const
*name
)
136 /* Chris Torek
's hash.
137 * NOTE: need to change *at least* create-okey-map.pl when changing the
141 while (*name != '\
0') {
149 /* Include what has been written in HASH_MODE */
152 static ui8_t seen_wraparound;
153 static size_t longest_distance;
156 next_prime(size_t no) /* blush (brute force) */
160 for (size_t i = 3; i < no; i += 2)
169 struct var_map const *vmp = _var_map, *vmaxp = vmp + NELEM(_var_map);
170 size_t ldist = 0, *arr;
172 arr = malloc(sizeof *arr * size);
173 for (size_t i = 0; i < size; ++i)
174 arr[i] = NELEM(_var_map);
177 longest_distance = 0;
179 while (vmp < vmaxp) {
180 ui32_t hash = vmp->vm_hash, i = hash % size, l;
182 for (l = 0; arr[i] != NELEM(_var_map); ++l)
187 if (l > longest_distance)
188 longest_distance = l;
189 arr[i] = (size_t)(vmp++ - _var_map);
193 #endif /* !HASH_MODE */
196 main(int argc, char **argv)
199 size_t h = torek_hash(argv[1]);
201 printf("%lu\n", (unsigned long)h);
204 size_t *arr, size = NELEM(_var_map);
206 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
208 arr = reversy(size = next_prime(size));
209 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
210 size, longest_distance, seen_wraparound);
211 if (longest_distance <= MAX_DISTANCE_PENALTY)
217 "#define _VAR_REV_ILL %zuu\n"
218 "#define _VAR_REV_PRIME %zuu\n"
219 "#define _VAR_REV_LONGEST %zuu\n"
220 "#define _VAR_REV_WRAPAROUND %d\n"
221 "static %s const _var_revmap[_VAR_REV_PRIME] = {\n ",
222 NELEM(_var_map), size, longest_distance, seen_wraparound, argv[1]);
223 for (size_t i = 0; i < size; ++i)
224 printf("%s%zuu", (i == 0 ? "" : (i % 10 == 0 ? ",\n " : ", ")), arr[i]);
230 # <<<<<<<<<<<<<<<<<<<
235 system("c99 -DHASH_MODE -I. -o $CTOOL_EXE $CTOOL");
237 foreach my $e (@ENTS) {
238 my $h = `$CTOOL_EXE $e->{name}`;
244 sub dump_keydat_varmap {
245 die "$OUT: open: $^E" unless open F, '>', $OUT;
246 print F "/*@ $OUT, generated by $0 on ", scalar gmtime(), ".\n",
247 " *@ See accmacvar.c for more */\n\n";
249 print F 'static char const _var_keydat
[] = {', "\n";
250 my ($i, $alen) = (0, 0);
252 foreach my $e (@ENTS) {
253 $e->{keyoff} = $alen;
256 my $a = join '\',\'', split(//, $k);
257 my ($f, $s) = ('', ', ');
258 if ($e->{boolean}) {$f .= $s . 'VM_BOOLEAN
'; $s = ' | '}
259 if ($e->{rdonly}) {$f .= $s . 'VM_RDONLY
'; $s = ' | '}
260 if ($e->{special}) {$f .= $s . 'VM_SPECIAL
'; $s = ' | '}
262 die("*$k*: virtual MUST be rdonly, too!") unless $e->{rdonly};
263 $f .= $s . 'VM_VIRTUAL
'; $s = ' | ';
266 print F " /* $i. [$alen]+$l $k$f */\n", " '$a','\\0',\n";
270 print F '};', "\n\n";
272 print F 'static struct var_map const _var_map
[] = {', "\n";
273 foreach my $e (@ENTS) {
275 $f .= ' | VM_BOOLEAN
' if $e->{boolean};
276 $f .= ' | VM_RDONLY
' if $e->{rdonly};
277 $f .= ' | VM_SPECIAL
' if $e->{special};
278 $f .= ' | VM_VIRTUAL
' if $e->{virtual};
279 my $n = $1 if $e->{enum} =~ /ok_._(.*)/;
280 print F " {$e->{hash}u, $e->{keyoff}u, $f}, /* $n */\n"
282 print F '};', "\n\n";
284 # We have at least version stuff in here
285 # The problem is that struct var uses a variable sized character buffer
286 # which cannot be initialized in a conforming way :(
287 print F "#ifndef __CREATE_OKEY_MAP_PL\n";
288 print F " /* Unfortunately init of varsized buffer won't work
*/\n";
289 foreach my $k (keys %virts) {
291 $e->{vname} = $1 if $e->{enum} =~ /ok_._(.*)/;
292 $e->{vstruct} = "var_virt_
$e->{vname
}";
293 print F "static struct
{\n";
294 print F " struct var
*v_link
;\n";
295 print F " char const
*v_value
;\n";
296 print F " char const v_name
[", length($e->{name}), " +1];\n";
297 print F "} const _
$e->{vstruct
} = ",
298 "{NULL
, $e->{virtual
}, \"$e->{name
}\"};\n\n";
301 print F 'static struct var_virtual const _var_virtuals[] = {', "\n";
302 foreach my $k (keys %virts) {
304 my $n = $1 if $e->{enum} =~ /ok_._(.*)/;
305 print F " {$e->{enum
}, (void const
*)&_
$e->{vstruct
}},\n";
307 print F "};\n#endif /* __CREATE_OKEY_MAP_PL */\n\n";
309 die "$OUT: close: $^E" unless close F
313 system("c99 -I. -o $CTOOL_EXE $CTOOL");
314 my $t = (@ENTS < 0xFF ?
'ui8_t' : (@ENTS < 0xFFFF ?
'ui16_t' : 'ui32_t'));
315 `$CTOOL_EXE $t >> $OUT`
318 {package main
; main_fun
()}