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 'nocntrls' && $1 ne 'num' && $1 ne 'posnum' &&
80 $1 ne 'vip' && $1 ne 'virt' &&
81 $1 ne 'env' && $1 ne 'import' &&
82 $1 ne 'i3val' && $1 ne 'defval');
88 if($init != 2) {die 'nail.h does not have the expected content'}
93 $CTOOL = './tmp-okey-tool-' . $$ . '.c';
94 $CTOOL_EXE = $CTOOL . '.exe';
96 die "$CTOOL: open: $^E" unless open F
, '>', $CTOOL;
97 # xxx optimize: could read lines and write lines in HASH_MODE..
98 print F
'#define MAX_DISTANCE_PENALTY ', $MAXDISTANCE_PENALTY, "\n";
101 #define __CREATE_OKEY_MAP_PL
107 #define n_NELEM(A) (sizeof(A) / sizeof(A[0]))
109 #define ui32_t uint32_t
110 #define ui16_t uint16_t
111 #define ui8_t uint8_t
113 enum a_amv_var_flags
{
115 a_AMV_VF_BOOL
= 1<<0, /* ok_b_* */
116 a_AMV_VF_VIRT
= 1<<1, /* "Stateless" automatic variable */
117 a_AMV_VF_RDONLY
= 1<<2, /* May not be set by user */
118 a_AMV_VF_NODEL
= 1<<3, /* May not be deleted */
119 a_AMV_VF_NOTEMPTY
= 1<<4, /* May not be assigned an empty value */
120 a_AMV_VF_NOCNTRLS
= 1<<5, /* Value may not contain control characters */
121 a_AMV_VF_NUM
= 1<<6, /* Value must be a 32-bit number */
122 a_AMV_VF_POSNUM
= 1<<7, /* Value must be positive 32-bit number */
123 a_AMV_VF_VIP
= 1<<8, /* Wants _var_check_vips() evaluation */
124 a_AMV_VF_IMPORT
= 1<<9, /* Import ONLY from environ (before PS_STARTED) */
125 a_AMV_VF_ENV
= 1<<10, /* Update environment on change */
126 a_AMV_VF_I3VAL
= 1<<11, /* Has an initial value */
127 a_AMV_VF_DEFVAL
= 1<<12, /* Has a default value */
128 a_AMV_VF_LINKED
= 1<<13, /* `environ' linked */
129 a_AMV_VF__MASK
= (1<<(13+1)) - 1
132 struct a_amv_var_map
{
135 ui16_t avm_flags
; /* enum a_amv_var_flags */
139 /* NOTE: copied over verbatim from auxlily.c */
141 torek_hash
(char const
*name
){
142 /* Chris Torek
's hash.
143 * NOTE: need to change *at least* mk-okey-map.pl when changing the
147 while(*name != '\
0'){
155 /* Include what has been written in HASH_MODE */
159 static ui8_t seen_wraparound;
160 static size_t longest_distance;
163 next_prime(size_t no){ /* blush (brute force) */
166 for(size_t i = 3; i < no; i += 2)
173 reversy(size_t size){
174 struct a_amv_var_map const *vmp = a_amv_var_map,
175 *vmaxp = vmp + n_NELEM(a_amv_var_map);
176 size_t ldist = 0, *arr;
178 arr = malloc(sizeof *arr * size);
179 for(size_t i = 0; i < size; ++i)
180 arr[i] = n_NELEM(a_amv_var_map);
183 longest_distance = 0;
186 ui32_t hash = vmp->avm_hash, i = hash % size, l;
188 for(l = 0; arr[i] != n_NELEM(a_amv_var_map); ++l)
193 if(l > longest_distance)
194 longest_distance = l;
195 arr[i] = (size_t)(vmp++ - a_amv_var_map);
199 #endif /* !HASH_MODE */
202 main(int argc, char **argv){
204 size_t h = torek_hash(argv[1]);
206 printf("%lu\n", (unsigned long)h);
209 size_t *arr, size = n_NELEM(a_amv_var_map);
211 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
213 arr = reversy(size = next_prime(size));
214 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
215 size, longest_distance, seen_wraparound);
216 if(longest_distance <= MAX_DISTANCE_PENALTY)
222 "#define a_AMV_VAR_REV_ILL %zuu\n"
223 "#define a_AMV_VAR_REV_PRIME %zuu\n"
224 "#define a_AMV_VAR_REV_LONGEST %zuu\n"
225 "#define a_AMV_VAR_REV_WRAPAROUND %d\n"
226 "static %s const a_amv_var_revmap[a_AMV_VAR_REV_PRIME] = {\n%s",
227 n_NELEM(a_amv_var_map), size, longest_distance, seen_wraparound,
228 argv[1], (argc > 2 ? " " : ""));
229 for(size_t i = 0; i < size; ++i)
230 printf("%s%zuu", (i == 0 ? ""
231 : (i % 10 == 0 ? (argc > 2 ? ",\n " : ",\n")
232 : (argc > 2 ? ", " : ","))),
239 # <<<<<<<<<<<<<<<<<<<
244 system("c99 -DHASH_MODE -I. -o $CTOOL_EXE $CTOOL");
246 foreach my $e (@ENTS){
247 my $h = `$CTOOL_EXE $e->{name}`;
254 die "$OUT: open: $^E" unless open F, '>', $OUT;
255 print F "/*@ $OUT, generated by $0 on ", scalar gmtime(), ".\n",
256 " *@ See accmacvar.c for more */\n\n";
258 print F 'static char const a_amv_var_names
[] = {', "\n";
259 my ($i, $alen) = (0, 0);
260 my (%virts, %defvals, %i3vals);
261 foreach my $e (@ENTS){
262 $e->{keyoff} = $alen;
265 my $a = join '\',\'', split(//, $k);
267 if($e->{bool}) {push @fa, 'a_AMV_VF_BOOL
'}
269 # Virtuals are implicitly rdonly and nodel
270 $e->{rdonly} = $e->{nodel} = 1;
272 push @fa, 'a_AMV_VF_VIRT
'
276 push @fa, 'a_AMV_VF_I3VAL
'
281 push @fa, 'a_AMV_VF_DEFVAL
'
285 push @fa, 'a_AMV_VF_IMPORT
'
287 if($e->{rdonly}) {push @fa, 'a_AMV_VF_RDONLY
'}
288 if($e->{nodel}) {push @fa, 'a_AMV_VF_NODEL
'}
289 if($e->{notempty}) {push @fa, 'a_AMV_VF_NOTEMPTY
'}
290 if($e->{nocntrls}) {push @fa, 'a_AMV_VF_NOCNTRLS
'}
291 if($e->{num}) {push @fa, 'a_AMV_VF_NUM
'}
292 if($e->{posnum}) {push @fa, 'a_AMV_VF_POSNUM
'}
293 if($e->{vip}) {push @fa, 'a_AMV_VF_VIP
'}
294 if($e->{env}) {push @fa, 'a_AMV_VF_ENV
'}
296 my $f = join('|', @fa);
297 $f = ', ' . $f if length $f;
298 print F "${S}/* $i. [$alen]+$l $k$f */\n" if $VERB;
299 print F "${S}'$a','\\0',\n";
303 print F '};', "\n\n";
305 print F 'n_CTA
(a_AMV_VF_NONE
== 0, "Value not 0 as expected");', "\n";
306 print F 'static struct a_amv_var_map const a_amv_var_map
[] = {', "\n";
307 foreach my $e (@ENTS){
308 my $f = $VERB ? 'a_AMV_VF_NONE
' : '0';
309 my $fa = join '|', @{$e->{flags}};
310 $f .= '|' . $fa if length $fa;
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
: define
"subclass"es
*/
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 char const a_amv_$e->{vstruct}_val[] = {$e->{virt}};\n";
338 print F
"static struct{\n";
339 print F
"${S}struct a_amv_var *av_link;\n";
340 print F
"${S}char const *av_value;\n";
341 print F
"${S}a_X(char *av_env;)\n";
342 print F
"${S}ui16_t av_flags;\n";
343 print F
"${S}char const av_name[", length($e->{name
}), " +1];\n";
344 my $f = $VERB ?
'a_AMV_VF_NONE' : '0';
345 my $fa = join '|', @
{$e->{flags
}};
346 $f .= '|' . $fa if length $fa;
347 print F
"} const a_amv_$e->{vstruct} = ",
348 "{NULL, a_amv_$e->{vstruct}_val, a_X(0 COMMA) $f, ",
349 "\"$e->{name}\"};\n\n"
351 print F
"# undef a_X\n";
354 print F
'#define a_AMV_VAR_VIRTS_CNT ', scalar @skeys, "\n";
355 print F
'static struct a_amv_var_virt const a_amv_var_virts[] = {', "\n";
358 my $n = $1 if $e->{enum
} =~ /ok_._(.*)/;
359 print F
"${S}{$e->{enum}, {0,}, (void const*)&a_amv_$e->{vstruct}},\n";
364 @skeys = sort keys %i3vals;
367 print F
'#define a_AMV_VAR_I3VALS_CNT ', scalar @skeys, "\n";
368 print F
'static struct a_amv_var_defval const a_amv_var_i3vals[] = {', "\n";
371 print F
"${S}{", $e->{enum
}, ', {0,}, ',
372 (!$e->{bool
} ?
$e->{i3val
} : "NULL"), "},\n"
377 @skeys = sort keys %defvals;
380 print F
'#define a_AMV_VAR_DEFVALS_CNT ', scalar @skeys, "\n";
381 print F
'static struct a_amv_var_defval const a_amv_var_defvals[] = {', "\n";
383 my $e = $defvals{$_};
384 print F
"${S}{", $e->{enum
}, ', {0,}, ',
385 (!$e->{bool
} ?
$e->{defval
} : "NULL"), "},\n"
389 print F
"#endif /* __CREATE_OKEY_MAP_PL */\n\n";
391 die "$OUT: close: $^E" unless close F
395 my $argv2 = $VERB ?
' verb' : '';
396 system("c99 -I. -o $CTOOL_EXE $CTOOL");
397 my $t = (@ENTS < 0xFF ?
'ui8_t' : (@ENTS < 0xFFFF ?
'ui16_t' : 'ui32_t'));
398 `$CTOOL_EXE $t$argv2 >> $OUT`
401 {package main
; main_fun
()}