4 #@ Parse 'enum okeys' from nail.h and create gen-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.
13 my $MAILX = 'LC_ALL=C s-nail -#:/';
14 my $OUT = 'gen-okeys.h';
18 use diagnostics
-verbose
;
25 use sigtrap
qw(handler cleanup normal-signals);
27 my ($S, @ENTS, $CTOOL, $CTOOL_EXE) = ($VERB ?
' ' : '');
30 if(@ARGV) {$VERB = 0; $S = ''}
47 die "$CTOOL_EXE: couldn't unlink: $^E"
48 if $CTOOL_EXE && -f
$CTOOL_EXE && 1 != unlink $CTOOL_EXE;
49 die "$CTOOL: couldn't unlink: $^E"
50 if $CTOOL && -f
$CTOOL && 1 != unlink $CTOOL;
51 die "Terminating due to signal $_[0]" if $_[0]
55 die "nail.h: open: $^E" unless open F
, '<', 'nail.h';
58 # Only want the enum okeys content
59 if(/^enum okeys/) {$init = 1; next}
60 if(/^};/) {if($init) {$init = 2; last}; next}
63 # Ignore empty and comment lines
67 # An entry may have a comment with special directives
68 /^\s*(\w+),?\s*(?:\/\
*\s
*(?
:{(.*)})\s
*\
*\
/\s*)?$/;
70 my ($k, $x) = ($1, $2);
73 $vals{bool
} = ($k =~ /^ok_b/ ?
1 : 0);
74 $k = $1 if $k =~ /^ok_[bv]_(.+)$/;
78 # {\}: overlong entry, placed on follow line
79 if($x =~ /\s*\\\s*$/){
81 die 'nail.h: missing continuation line' unless $_;
82 /^\s*\/\
*\s
*{(.*)}\s
*\
*\
/\s*$/;
84 die 'nail.h: invalid continuation line' unless $x
87 while($x && $x =~ /^([^,]+?)(?:,(.*))?$/){
90 die "Unsupported special directive: $1"
92 $1 ne 'virt' && $1 ne 'chain' && $1 ne 'vip' &&
93 $1 ne 'rdonly' && $1 ne 'nodel' &&
94 $1 ne 'i3val' && $1 ne 'defval' &&
95 $1 ne 'import' && $1 ne 'env' && $1 ne 'nolopts' &&
96 $1 ne 'notempty' && $1 ne 'nocntrls' &&
97 $1 ne 'num' && $1 ne 'posnum' && $1 ne 'lower');
103 if($init != 2) {die 'nail.h does not have the expected content'}
108 $CTOOL = './tmp-okey-tool-' . $$ . '.c';
109 $CTOOL_EXE = $CTOOL . '.exe';
111 die "$CTOOL: open: $^E" unless open F
, '>', $CTOOL;
112 print F
'#define MAX_DISTANCE_PENALTY ', $MAXDISTANCE_PENALTY, "\n";
113 # >>>>>>>>>>>>>>>>>>>
115 #define __CREATE_OKEY_MAP_PL
121 #define n_NELEM(A) (sizeof(A) / sizeof(A[0]))
123 #define ui32_t uint32_t
124 #define ui16_t uint16_t
125 #define ui8_t uint8_t
127 enum a_amv_var_flags
{
130 /* The basic set of flags, also present in struct a_amv_var_map.avm_flags */
131 a_AMV_VF_BOOL
= 1u<<0, /* ok_b_* */
132 a_AMV_VF_VIRT
= 1u<<1, /* "Stateless" automatic variable */
133 a_AMV_VF_CHAIN
= 1u<<2, /* Is a variable chain (-USER{,@HOST} variants) */
134 a_AMV_VF_VIP
= 1u<<3, /* Wants _var_check_vips() evaluation */
135 a_AMV_VF_RDONLY
= 1u<<4, /* May not be set by user */
136 a_AMV_VF_NODEL
= 1u<<5, /* May not be deleted */
137 a_AMV_VF_I3VAL
= 1u<<6, /* Has an initial value */
138 a_AMV_VF_DEFVAL
= 1u<<7, /* Has a default value */
139 a_AMV_VF_IMPORT
= 1u<<8, /* Import ONLY from environ (pre n_PSO_STARTED) */
140 a_AMV_VF_ENV
= 1u<<9, /* Update environment on change */
141 a_AMV_VF_NOLOPTS
= 1u<<10, /* May not be tracked by `localopts' */
142 a_AMV_VF_NOTEMPTY
= 1u<<11, /* May not be assigned an empty value */
143 a_AMV_VF_NOCNTRLS
= 1u<<12, /* Value may not contain control characters */
144 a_AMV_VF_NUM
= 1u<<13, /* Value must be a 32-bit number */
145 a_AMV_VF_POSNUM
= 1u<<14, /* Value must be positive 32-bit number */
146 a_AMV_VF_LOWER
= 1u<<15, /* Values will be stored in a lowercase version */
147 a_AMV_VF__MASK
= (1u<<(15+1)) - 1,
150 struct a_amv_var_map
{
153 ui16_t avm_flags
; /* enum a_amv_var_flags */
157 #include "gen-okeys.h"
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
);
201 main
(int argc
, char
**argv
){
202 size_t
*arr
, size
= n_NELEM
(a_amv_var_map
);
204 fprintf
(stderr
, "Starting reversy, okeys=%zu\n", size
);
206 arr
= reversy
(size
= next_prime
(size
));
207 fprintf
(stderr
, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
208 size
, longest_distance
, seen_wraparound
);
209 if(longest_distance
<= MAX_DISTANCE_PENALTY
)
215 "#define a_AMV_VAR_REV_ILL %zuu\n"
216 "#define a_AMV_VAR_REV_PRIME %zuu\n"
217 "#define a_AMV_VAR_REV_LONGEST %zuu\n"
218 "#define a_AMV_VAR_REV_WRAPAROUND %d\n"
219 "static %s const a_amv_var_revmap[a_AMV_VAR_REV_PRIME] = {\n%s",
220 n_NELEM
(a_amv_var_map
), size
, longest_distance
, seen_wraparound
,
221 argv
[1], (argc
> 2 ?
" " : ""));
222 for(size_t i
= 0; i
< size
; ++i
)
223 printf("%s%zuu", (i
== 0 ?
""
224 : (i
% 10 == 0 ?
(argc
> 2 ?
",\n " : ",\n")
225 : (argc
> 2 ?
", " : ","))),
231 # <<<<<<<<<<<<<<<<<<<
236 die "hash_em: open: $^E"
237 unless my $pid = open2
*RFD
, *WFD
, $MAILX;
238 foreach my $e (@ENTS){
239 print WFD
"vexpr hash $e->{name}\n";
249 die "$OUT: open: $^E" unless open F
, '>', $OUT;
250 print F
"/*@ $OUT, generated by $0.\n",
251 " *@ See accmacvar.c for more */\n\n";
253 print F
'static char const a_amv_var_names[] = {', "\n";
254 my ($i, $alen) = (0, 0);
255 my (%virts, %defvals, %i3vals);
256 foreach my $e (@ENTS){
257 $e->{keyoff
} = $alen;
260 my $a = join '\',\'', split(//, $k);
262 if($e->{bool
}) {push @fa, 'a_AMV_VF_BOOL'}
264 # Virtuals are implicitly rdonly and nodel
265 $e->{rdonly
} = $e->{nodel
} = 1;
267 push @fa, 'a_AMV_VF_VIRT'
269 if($e->{chain
}) {push @fa, 'a_AMV_VF_CHAIN'}
270 if($e->{vip
}) {push @fa, 'a_AMV_VF_VIP'}
271 if($e->{rdonly
}) {push @fa, 'a_AMV_VF_RDONLY'}
272 if($e->{nodel
}) {push @fa, 'a_AMV_VF_NODEL'}
273 if(defined $e->{i3val
}){
275 push @fa, 'a_AMV_VF_I3VAL'
279 push @fa, 'a_AMV_VF_DEFVAL'
283 push @fa, 'a_AMV_VF_IMPORT'
285 if($e->{env
}) {push @fa, 'a_AMV_VF_ENV'}
286 if($e->{nolopts
}) {push @fa, 'a_AMV_VF_NOLOPTS'}
287 if($e->{notempty
}) {push @fa, 'a_AMV_VF_NOTEMPTY'}
288 if($e->{nocntrls
}) {push @fa, 'a_AMV_VF_NOCNTRLS'}
289 if($e->{num
}) {push @fa, 'a_AMV_VF_NUM'}
290 if($e->{posnum
}) {push @fa, 'a_AMV_VF_POSNUM'}
291 if($e->{lower
}) {push @fa, 'a_AMV_VF_LOWER'}
293 my $f = join('|', @fa);
294 $f = ', ' . $f if length $f;
295 print F
"${S}/* $i. [$alen]+$l $k$f */\n" if $VERB;
296 print F
"${S}'$a','\\0',\n";
300 print F
'};', "\n\n";
302 print F
'n_CTA(a_AMV_VF_NONE == 0, "Value not 0 as expected");', "\n";
303 print F
'static struct a_amv_var_map const a_amv_var_map[] = {', "\n";
304 foreach my $e (@ENTS){
305 my $f = $VERB ?
'a_AMV_VF_NONE' : '0';
306 my $fa = join '|', @
{$e->{flags
}};
307 $f .= '|' . $fa if length $fa;
308 print F
"${S}{$e->{hash}u, $e->{keyoff}u, $f},";
309 if($VERB) {print F
"${S}/* $e->{name} */\n"}
312 print F
'};', "\n\n";
314 # We have at least version stuff in here
315 # The problem is that struct var uses a variable sized character buffer
316 # which cannot be initialized in a conforming way :(
318 #ifndef __CREATE_OKEY_MAP_PL
325 /* Unfortunately init of varsized buffer won't work: define "subclass"es */
327 my @skeys = sort keys %virts;
331 $e->{vname
} = $1 if $e->{enum
} =~ /ok_._(.*)/;
332 $e->{vstruct
} = "var_virt_$e->{vname}";
333 print F
"static char const a_amv_$e->{vstruct}_val[] = {$e->{virt}};\n";
334 print F
"static struct{\n";
335 print F
"${S}struct a_amv_var *av_link;\n";
336 print F
"${S}char const *av_value;\n";
337 print F
"${S}a_X(char *av_env;)\n";
338 print F
"${S}ui32_t av_flags;\n";
339 print F
"${S}char const av_name[", length($e->{name
}), " +1];\n";
340 my $f = $VERB ?
'a_AMV_VF_NONE' : '0';
341 my $fa = join '|', @
{$e->{flags
}};
342 $f .= '|' . $fa if length $fa;
343 print F
"} const a_amv_$e->{vstruct} = ",
344 "{NULL, a_amv_$e->{vstruct}_val, a_X(0 COMMA) $f, ",
345 "\"$e->{name}\"};\n\n"
347 print F
"# undef a_X\n";
350 print F
'#define a_AMV_VAR_VIRTS_CNT ', scalar @skeys, "\n";
351 print F
'static struct a_amv_var_virt const a_amv_var_virts[] = {', "\n";
354 my $n = $1 if $e->{enum
} =~ /ok_._(.*)/;
355 print F
"${S}{$e->{enum}, {0,}, (void const*)&a_amv_$e->{vstruct}},\n";
360 @skeys = sort keys %i3vals;
363 print F
'#define a_AMV_VAR_I3VALS_CNT ', scalar @skeys, "\n";
364 print F
'static struct a_amv_var_defval const a_amv_var_i3vals[] = {', "\n";
367 print F
"${S}{", $e->{enum
}, ', {0,}, ',
368 (!$e->{bool
} ?
$e->{i3val
} : "NULL"), "},\n"
373 @skeys = sort keys %defvals;
376 print F
'#define a_AMV_VAR_DEFVALS_CNT ', scalar @skeys, "\n";
377 print F
'static struct a_amv_var_defval const a_amv_var_defvals[] = {', "\n";
379 my $e = $defvals{$_};
380 print F
"${S}{", $e->{enum
}, ', {0,}, ',
381 (!$e->{bool
} ?
$e->{defval
} : "NULL"), "},\n"
385 print F
"#endif /* __CREATE_OKEY_MAP_PL */\n";
387 # Special var backing [#@*?]|[1-9][0-9]*|0
390 foreach my $e (@ENTS){
391 if($e->{name
} eq '--special-param'){
392 print F
"#define a_AMV_VAR__SPECIAL_PARAM_MAP_IDX ${i}u\n"
394 # The rest are only speedups
395 elsif($e->{name
} eq '?'){
396 print F
"#define a_AMV_VAR__QM_MAP_IDX ${i}u\n"
397 }elsif($e->{name
} eq '!'){
398 print F
"#define a_AMV_VAR__EM_MAP_IDX ${i}u\n"
404 die "$OUT: close: $^E" unless close F
408 my $argv2 = $VERB ?
' verb' : '';
409 system("\$CC -I. -o $CTOOL_EXE $CTOOL");
410 my $t = (@ENTS < 0xFF ?
'ui8_t' : (@ENTS < 0xFFFF ?
'ui16_t' : 'ui32_t'));
411 `$CTOOL_EXE $t$argv2 >> $OUT`
414 {package main
; main_fun
()}