enum okeys: new flags, additions and removals
[s-mailx.git] / mk-okey-map.pl
blob4f00c7199bc371bb687f8354a3428f9777b8a704
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 # Generate a more verbose output. Not for shipout versions.
11 my $VERB = 1;
13 my $OUT = 'okeys.h';
15 ## -- >8 -- 8< -- ##
17 use diagnostics -verbose;
18 use strict;
19 use warnings;
21 use sigtrap qw(handler cleanup normal-signals);
23 my ($S, @ENTS, $CTOOL, $CTOOL_EXE) = ($VERB ? ' ' : '');
25 sub main_fun{
26 if(@ARGV) {$VERB = 0; $S = ''}
28 parse_nail_h();
30 create_c_tool();
32 hash_em();
33 dump_map();
35 reverser();
37 cleanup(undef);
38 exit 0
41 sub cleanup{
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]
49 sub parse_nail_h{
50 die "nail.h: open: $^E" unless open F, '<', 'nail.h';
51 my ($init) = (0);
52 while(<F>){
53 # Only want the enum okeys content
54 if(/^enum okeys/) {$init = 1; next}
55 if(/^};/) {if($init) {$init = 2; last}; next}
56 $init || next;
58 # Ignore empty and comment lines
59 /^$/ && next;
60 /^\s*\/\*/ && next;
62 # An entry may have a comment with special directives
63 /^\s*(\w+),?\s*(?:\/\*\s*(?:{(.*)})\s*\*\/\s*)?$/;
64 next unless $1;
65 my ($k, $x) = ($1, $2);
66 my %vals;
67 $vals{enum} = $k;
68 $vals{bool} = ($k =~ /^ok_b/ ? 1 : 0);
69 $k = $1 if $k =~ /^ok_[bv]_(.+)$/;
70 $k =~ s/_/-/g;
71 $vals{name} = $k;
72 if($x){
73 while($x && $x =~ /^([^,]+?)(?:,(.*))?$/){
74 $x = $2;
75 $1 =~ /([^=]+)=(.+)/;
76 die "Unsupported special directive: $1"
77 if($1 ne 'name' &&
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');
82 $vals{$1} = $2
85 push @ENTS, \%vals
87 if($init != 2) {die 'nail.h does not have the expected content'}
88 close F
91 sub create_c_tool{
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";
98 # >>>>>>>>>>>>>>>>>>>
99 print F <<'_EOT';
100 #define __CREATE_OKEY_MAP_PL
101 #include <stdint.h>
102 #include <stdlib.h>
103 #include <stdio.h>
104 #include <string.h>
106 #ifndef NELEM
107 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
108 #endif
110 #define ui32_t uint32_t
111 #define ui16_t uint16_t
112 #define ui8_t uint8_t
114 enum a_amv_var_flags{
115 a_AMV_VF_NONE = 0,
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{
131 ui32_t avm_hash;
132 ui16_t avm_keyoff;
133 ui16_t avm_flags; /* enum a_amv_var_flags */
136 #ifdef HASH_MODE
137 /* NOTE: copied over verbatim from auxlily.c */
138 static ui32_t
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
142 * algorithm!! */
143 ui32_t h = 0;
145 while(*name != '\0'){
146 h *= 33;
147 h += *name++;
149 return h;
152 #else
153 /* Include what has been written in HASH_MODE */
154 # define n_CTA(A,S)
155 # include "okeys.h"
157 static ui8_t seen_wraparound;
158 static size_t longest_distance;
160 static size_t
161 next_prime(size_t no){ /* blush (brute force) */
162 jredo:
163 ++no;
164 for(size_t i = 3; i < no; i += 2)
165 if(no % i == 0)
166 goto jredo;
167 return no;
170 static size_t *
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);
180 seen_wraparound = 0;
181 longest_distance = 0;
183 while(vmp < vmaxp){
184 ui32_t hash = vmp->avm_hash, i = hash % size, l;
186 for(l = 0; arr[i] != NELEM(a_amv_var_map); ++l)
187 if(++i == size){
188 seen_wraparound = 1;
189 i = 0;
191 if(l > longest_distance)
192 longest_distance = l;
193 arr[i] = (size_t)(vmp++ - a_amv_var_map);
195 return arr;
197 #endif /* !HASH_MODE */
200 main(int argc, char **argv){
201 #ifdef HASH_MODE
202 size_t h = torek_hash(argv[1]);
204 printf("%lu\n", (unsigned long)h);
206 #else
207 size_t *arr, size = NELEM(a_amv_var_map);
209 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
210 for(;;){
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)
215 break;
216 free(arr);
219 printf(
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 ? ", " : ","))),
231 arr[i]);
232 printf("\n};\n");
233 #endif
234 return 0;
236 _EOT
237 # <<<<<<<<<<<<<<<<<<<
238 close F
241 sub hash_em{
242 system("c99 -DHASH_MODE -I. -o $CTOOL_EXE $CTOOL");
244 foreach my $e (@ENTS){
245 my $h = `$CTOOL_EXE $e->{name}`;
246 chomp $h;
247 $e->{hash} = $h
251 sub dump_map{
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;
261 my $k = $e->{name};
262 my $l = length $k;
263 my $a = join '\',\'', split(//, $k);
264 my ($f, $s) = ('', ', ');
265 if($e->{bool}) {$f .= $s . 'a_AMV_VF_BOOL'; $s = '|'}
266 if($e->{virt}){
267 $virts{$k} = $e;
268 $f .= $s . 'a_AMV_VF_VIRT'; $s = '|';
269 # Virtuals are implicitly rdonly and nodel
270 $e->{rdonly} = $e->{nodel} = 1
272 if($e->{i3val}){
273 $i3vals{$k} = $e;
274 $f .= $s . 'a_AMV_VF_I3VAL'; $s = '|'
276 if($e->{defval}){
277 $defvals{$k} = $e;
278 $f .= $s . 'a_AMV_VF_DEFVAL'; $s = '|';
279 $e->{notempty} = 1
281 if($e->{import}){
282 $f .= $s . 'a_AMV_VF_IMPORT'; $s = '|';
283 $e->{env} = 1
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";
292 ++$i;
293 $alen += $l + 1
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"}
314 else {print F "\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 :(
321 print F <<_EOT;
322 #ifndef __CREATE_OKEY_MAP_PL
323 # ifdef HAVE_PUTENV
324 # define a_X(X) X
325 # else
326 # define a_X(X)
327 # endif
329 /* Unfortunately init of varsized buffer won't work */
330 _EOT
331 my @skeys = sort keys %virts;
333 foreach(@skeys){
334 my $e = $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";
348 print F "\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";
351 foreach(@skeys){
352 my $e = $virts{$_};
353 my $n = $1 if $e->{enum} =~ /ok_._(.*)/;
354 print F "${S}{$e->{enum}, (void const*)&a_amv_$e->{vstruct}},\n";
356 print F "};\n";
359 @skeys = sort keys %i3vals;
361 print F "\n";
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";
364 foreach(@skeys){
365 my $e = $i3vals{$_};
366 print F "${S}{", $e->{enum}, ', {0,}, ',
367 (!$e->{bool} ? $e->{i3val} : "NULL"), "},\n"
369 print F "};\n";
372 @skeys = sort keys %defvals;
374 print F "\n";
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";
377 foreach(@skeys){
378 my $e = $defvals{$_};
379 print F "${S}{", $e->{enum}, ', {0,}, ',
380 (!$e->{bool} ? $e->{defval} : "NULL"), "},\n"
382 print F "};\n";
384 print F "#endif /* __CREATE_OKEY_MAP_PL */\n\n";
386 die "$OUT: close: $^E" unless close F
389 sub reverser{
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()}
398 # s-it-mode