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;
10 # Generate a more verbose output. Not for shipout versions.
17 use diagnostics
-verbose
;
21 use sigtrap
qw(handler cleanup normal-signals);
23 my ($S, @ENTS, $CTOOL, $CTOOL_EXE) = ($VERB ?
' ' : '');
26 if(@ARGV) {$VERB = 0; $S = ''}
42 die "$CTOOL_EXE: couldn't unlink: $^E"
43 if $CTOOL_EXE && -f
$CTOOL_EXE && 1 != unlink $CTOOL_EXE;
44 die "$CTOOL: couldn't unlink: $^E"
45 if $CTOOL && -f
$CTOOL && 1 != unlink $CTOOL;
46 die "Terminating due to signal $_[0]" if $_[0]
50 die "nail.h: open: $^E" unless open F
, '<', 'nail.h';
53 # Only want the enum okeys content
54 if(/^enum okeys/) {$init = 1; next}
55 if(/^};/) {if($init) {$init = 2; last}; next}
58 # Ignore empty and comment lines
62 # An entry may have a comment with special directives
63 /^\s*(\w+),?\s*(?:\/\
*\s
*(?
:{(.*)})\s
*\
*\
/\s*)?$/;
65 my ($k, $x) = ($1, $2);
68 $vals{bool
} = ($k =~ /^ok_b/ ?
1 : 0);
69 $k = $1 if $k =~ /^ok_[bv]_(.+)$/;
73 while($x && $x =~ /^([^,]+?)(?:,(.*))?$/){
76 die "Unsupported special directive: $1"
78 $1 ne 'rdonly' && $1 ne 'nodel' && $1 ne 'notempty' &&
79 $1 ne 'vip' && $1 ne 'virt' &&
80 $1 ne 'env' && $1 ne 'import' &&
81 $1 ne 'i3val' && $1 ne 'defval');
87 if($init != 2) {die 'nail.h does not have the expected content'}
92 $CTOOL = './tmp-okey-tool-' . $$ . '.c';
93 $CTOOL_EXE = $CTOOL . '.exe';
95 die "$CTOOL: open: $^E" unless open F
, '>', $CTOOL;
96 # xxx optimize: could read lines and write lines in HASH_MODE..
97 print F
'#define MAX_DISTANCE_PENALTY ', $MAXDISTANCE_PENALTY, "\n";
100 #define __CREATE_OKEY_MAP_PL
107 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
110 #define ui32_t uint32_t
111 #define ui16_t uint16_t
112 #define ui8_t uint8_t
114 enum a_amv_var_flags
{
116 a_AMV_VF_BOOL
= 1<<0, /* ok_b_* */
117 a_AMV_VF_VIRT
= 1<<1, /* "Stateless" automatic variable */
118 a_AMV_VF_RDONLY
= 1<<2, /* May not be set by user */
119 a_AMV_VF_NODEL
= 1<<3, /* May not be deleted */
120 a_AMV_VF_NOTEMPTY
= 1<<4, /* May not be assigned an empty value */
121 a_AMV_VF_VIP
= 1<<5, /* Wants _var_check_vips() evaluation */
122 a_AMV_VF_IMPORT
= 1<<6, /* Import ONLY from environ (before PS_STARTED) */
123 a_AMV_VF_ENV
= 1<<7, /* Update environment on change */
124 a_AMV_VF_I3VAL
= 1<<8, /* Has an initial value */
125 a_AMV_VF_DEFVAL
= 1<<9, /* Has a default value */
126 a_AMV_VF_LINKED
= 1<<10, /* `environ' linked */
127 a_AMV_VF__MASK
= (1<<(10+1)) - 1
130 struct a_amv_var_map
{
133 ui16_t avm_flags
; /* enum a_amv_var_flags */
137 /* NOTE: copied over verbatim from auxlily.c */
139 torek_hash
(char const
*name
){
140 /* Chris Torek
's hash.
141 * NOTE: need to change *at least* mk-okey-map.pl when changing the
145 while(*name != '\
0'){
153 /* Include what has been written in HASH_MODE */
157 static ui8_t seen_wraparound;
158 static size_t longest_distance;
161 next_prime(size_t no){ /* blush (brute force) */
164 for(size_t i = 3; i < no; i += 2)
171 reversy(size_t size){
172 struct a_amv_var_map const *vmp = a_amv_var_map,
173 *vmaxp = vmp + NELEM(a_amv_var_map);
174 size_t ldist = 0, *arr;
176 arr = malloc(sizeof *arr * size);
177 for(size_t i = 0; i < size; ++i)
178 arr[i] = NELEM(a_amv_var_map);
181 longest_distance = 0;
184 ui32_t hash = vmp->avm_hash, i = hash % size, l;
186 for(l = 0; arr[i] != NELEM(a_amv_var_map); ++l)
191 if(l > longest_distance)
192 longest_distance = l;
193 arr[i] = (size_t)(vmp++ - a_amv_var_map);
197 #endif /* !HASH_MODE */
200 main(int argc, char **argv){
202 size_t h = torek_hash(argv[1]);
204 printf("%lu\n", (unsigned long)h);
207 size_t *arr, size = NELEM(a_amv_var_map);
209 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
211 arr = reversy(size = next_prime(size));
212 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
213 size, longest_distance, seen_wraparound);
214 if(longest_distance <= MAX_DISTANCE_PENALTY)
220 "#define a_AMV_VAR_REV_ILL %zuu\n"
221 "#define a_AMV_VAR_REV_PRIME %zuu\n"
222 "#define a_AMV_VAR_REV_LONGEST %zuu\n"
223 "#define a_AMV_VAR_REV_WRAPAROUND %d\n"
224 "static %s const a_amv_var_revmap[a_AMV_VAR_REV_PRIME] = {\n%s",
225 NELEM(a_amv_var_map), size, longest_distance, seen_wraparound,
226 argv[1], (argc > 2 ? " " : ""));
227 for(size_t i = 0; i < size; ++i)
228 printf("%s%zuu", (i == 0 ? ""
229 : (i % 10 == 0 ? (argc > 2 ? ",\n " : ",\n")
230 : (argc > 2 ? ", " : ","))),
237 # <<<<<<<<<<<<<<<<<<<
242 system("c99 -DHASH_MODE -I. -o $CTOOL_EXE $CTOOL");
244 foreach my $e (@ENTS){
245 my $h = `$CTOOL_EXE $e->{name}`;
252 die "$OUT: open: $^E" unless open F, '>', $OUT;
253 print F "/*@ $OUT, generated by $0 on ", scalar gmtime(), ".\n",
254 " *@ See accmacvar.c for more */\n\n";
256 print F 'static char const a_amv_var_names
[] = {', "\n";
257 my ($i, $alen) = (0, 0);
258 my (%virts, %defvals, %i3vals);
259 foreach my $e (@ENTS){
260 $e->{keyoff} = $alen;
263 my $a = join '\',\'', split(//, $k);
264 my ($f, $s) = ('', ', ');
265 if($e->{bool}) {$f .= $s . 'a_AMV_VF_BOOL
'; $s = '|'}
268 $f .= $s . 'a_AMV_VF_VIRT
'; $s = '|';
269 # Virtuals are implicitly rdonly and nodel
270 $e->{rdonly} = $e->{nodel} = 1
274 $f .= $s . 'a_AMV_VF_I3VAL
'; $s = '|'
278 $f .= $s . 'a_AMV_VF_DEFVAL
'; $s = '|';
282 $f .= $s . 'a_AMV_VF_IMPORT
'; $s = '|';
285 if($e->{rdonly}) {$f .= $s . 'a_AMV_VF_RDONLY
'; $s = '|'}
286 if($e->{nodel}) {$f .= $s . 'a_AMV_VF_NODEL
'; $s = '|'}
287 if($e->{notempty}) {$f .= $s . 'a_AMV_VF_NOTEMPTY
'; $s = '|'}
288 if($e->{vip}) {$f .= $s . 'a_AMV_VF_VIP
'; $s = '|'}
289 if($e->{env}) {$f .= $s . 'a_AMV_VF_ENV
'; $s = '|'}
290 print F "${S}/* $i. [$alen]+$l $k$f */\n" if $VERB;
291 print F "${S}'$a','\\0',\n";
295 print F '};', "\n\n";
297 print F 'n_CTA
(a_AMV_VF_NONE
== 0, "Value not 0 as expected");', "\n";
298 print F 'static struct a_amv_var_map const a_amv_var_map
[] = {', "\n";
299 foreach my $e (@ENTS){
300 my $f = $VERB ? 'a_AMV_VF_NONE
' : '0';
301 $f .= '|a_AMV_VF_BOOL
' if $e->{bool};
302 $f .= '|a_AMV_VF_VIRT
' if $e->{virt};
303 $f .= '|a_AMV_VF_RDONLY
' if $e->{rdonly};
304 $f .= '|a_AMV_VF_NODEL
' if $e->{nodel};
305 $f .= '|a_AMV_VF_NOTEMPTY
' if $e->{notempty};
306 $f .= '|a_AMV_VF_VIP
' if $e->{vip};
307 $f .= '|a_AMV_VF_IMPORT
' if $e->{import};
308 $f .= '|a_AMV_VF_ENV
' if $e->{env};
309 $f .= '|a_AMV_VF_I3VAL
' if $e->{i3val};
310 $f .= '|a_AMV_VF_DEFVAL
' if $e->{defval};
311 my $n = $1 if $e->{enum} =~ /ok_._(.*)/;
312 print F "${S}{$e->{hash}u, $e->{keyoff}u, $f},";
313 if($VERB) {print F "${S}/* $n */\n"}
316 print F '};', "\n\n";
318 # We have at least version stuff in here
319 # The problem is that struct var uses a variable sized character buffer
320 # which cannot be initialized in a conforming way :(
322 #ifndef __CREATE_OKEY_MAP_PL
329 /* Unfortunately init of varsized buffer won't work
*/
331 my @skeys = sort keys %virts;
335 $e->{vname
} = $1 if $e->{enum
} =~ /ok_._(.*)/;
336 $e->{vstruct
} = "var_virt_$e->{vname}";
337 print F
"static struct{\n";
338 print F
"${S}struct a_amv_var *av_link;\n";
339 print F
"${S}char const *av_value;\n";
340 print F
"${S}a_X(char *av_env;)\n";
341 print F
"${S}ui16_t av_flags;\n";
342 print F
"${S}char const av_name[", length($e->{name
}), " +1];\n";
343 print F
"} const a_amv_$e->{vstruct} = ",
344 "{NULL, $e->{virt}, a_X(0 COMMA) 0, ", "\"$e->{name}\"};\n\n"
346 print F
"# undef a_X\n";
349 print F
'#define a_AMV_VAR_VIRTS_CNT ', scalar @skeys, "\n";
350 print F
'static struct a_amv_var_virt const a_amv_var_virts[] = {', "\n";
353 my $n = $1 if $e->{enum
} =~ /ok_._(.*)/;
354 print F
"${S}{$e->{enum}, (void const*)&a_amv_$e->{vstruct}},\n";
359 @skeys = sort keys %i3vals;
362 print F
'#define a_AMV_VAR_I3VALS_CNT ', scalar @skeys, "\n";
363 print F
'static struct a_amv_var_defval const a_amv_var_i3vals[] = {', "\n";
366 print F
"${S}{", $e->{enum
}, ', {0,}, ',
367 (!$e->{bool
} ?
$e->{i3val
} : "NULL"), "},\n"
372 @skeys = sort keys %defvals;
375 print F
'#define a_AMV_VAR_DEFVALS_CNT ', scalar @skeys, "\n";
376 print F
'static struct a_amv_var_defval const a_amv_var_defvals[] = {', "\n";
378 my $e = $defvals{$_};
379 print F
"${S}{", $e->{enum
}, ', {0,}, ',
380 (!$e->{bool
} ?
$e->{defval
} : "NULL"), "},\n"
384 print F
"#endif /* __CREATE_OKEY_MAP_PL */\n\n";
386 die "$OUT: close: $^E" unless close F
390 my $argv2 = $VERB ?
' verb' : '';
391 system("c99 -I. -o $CTOOL_EXE $CTOOL");
392 my $t = (@ENTS < 0xFF ?
'ui8_t' : (@ENTS < 0xFFFF ?
'ui16_t' : 'ui32_t'));
393 `$CTOOL_EXE $t$argv2 >> $OUT`
396 {package main
; main_fun
()}