nail.1: last fixes
[s-mailx.git] / make-okey-map.pl
blob22b579ea24821b0f6c9d84b26eeea9f984651fd2
1 #!/usr/bin/env perl
2 require 5.008_001;
3 use utf8;
4 #@ Parse 'enum okeys' from nail.h and create gen-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 $MAILX = 'LC_ALL=C s-nail -#:/';
14 my $OUT = 'gen-okeys.h';
16 ## -- >8 -- 8< -- ##
18 use diagnostics -verbose;
19 use strict;
20 use warnings;
22 use FileHandle;
23 use IPC::Open2;
25 use sigtrap qw(handler cleanup normal-signals);
27 my ($S, @ENTS, $CTOOL, $CTOOL_EXE) = ($VERB ? ' ' : '');
29 sub main_fun{
30 if(@ARGV) {$VERB = 0; $S = ''}
32 parse_nail_h();
34 create_c_tool();
36 hash_em();
38 dump_map();
40 reverser();
42 cleanup(undef);
43 exit 0
46 sub cleanup{
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]
54 sub parse_nail_h{
55 die "nail.h: open: $^E" unless open F, '<', 'nail.h';
56 my ($init) = (0);
57 while(<F>){
58 # Only want the enum okeys content
59 if(/^enum okeys/) {$init = 1; next}
60 if(/^};/) {if($init) {$init = 2; last}; next}
61 $init || next;
63 # Ignore empty and comment lines
64 /^$/ && next;
65 /^\s*\/\*/ && next;
67 # An entry may have a comment with special directives
68 /^\s*(\w+),?\s*(?:\/\*\s*(?:{(.*)})\s*\*\/\s*)?$/;
69 next unless $1;
70 my ($k, $x) = ($1, $2);
71 my %vals;
72 $vals{enum} = $k;
73 $vals{bool} = ($k =~ /^ok_b/ ? 1 : 0);
74 $k = $1 if $k =~ /^ok_[bv]_(.+)$/;
75 $k =~ s/_/-/g;
76 $vals{name} = $k;
77 if($x){
78 # {\}: overlong entry, placed on follow line
79 if($x =~ /\s*\\\s*$/){
80 $_ = <F>;
81 die 'nail.h: missing continuation line' unless $_;
82 /^\s*\/\*\s*{(.*)}\s*\*\/\s*$/;
83 $x = $1;
84 die 'nail.h: invalid continuation line' unless $x
87 while($x && $x =~ /^([^,]+?)(?:,(.*))?$/){
88 $x = $2;
89 $1 =~ /([^=]+)=(.+)/;
90 die "Unsupported special directive: $1"
91 if($1 ne 'name' &&
92 $1 ne 'virt' && $1 ne 'nolopts' &&
93 $1 ne 'rdonly' && $1 ne 'nodel' && $1 ne 'notempty' &&
94 $1 ne 'nocntrls' &&
95 $1 ne 'num' && $1 ne 'posnum' && $1 ne 'lower' &&
96 $1 ne 'vip' && $1 ne 'import' && $1 ne 'env' &&
97 $1 ne 'i3val' && $1 ne 'defval');
98 $vals{$1} = $2
101 push @ENTS, \%vals
103 if($init != 2) {die 'nail.h does not have the expected content'}
104 close F
107 sub create_c_tool{
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 # >>>>>>>>>>>>>>>>>>>
114 print F <<'_EOT';
115 #define __CREATE_OKEY_MAP_PL
116 #include <stdint.h>
117 #include <stdlib.h>
118 #include <stdio.h>
119 #include <string.h>
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{
128 a_AMV_VF_NONE = 0,
129 a_AMV_VF_BOOL = 1u<<0, /* ok_b_* */
130 a_AMV_VF_VIRT = 1u<<1, /* "Stateless" automatic variable */
131 a_AMV_VF_NOLOPTS = 1u<<2, /* May not be tracked by `localopts' */
132 a_AMV_VF_RDONLY = 1u<<3, /* May not be set by user */
133 a_AMV_VF_NODEL = 1u<<4, /* May not be deleted */
134 a_AMV_VF_NOTEMPTY = 1u<<5, /* May not be assigned an empty value */
135 a_AMV_VF_NOCNTRLS = 1u<<6, /* Value may not contain control characters */
136 a_AMV_VF_NUM = 1u<<7, /* Value must be a 32-bit number */
137 a_AMV_VF_POSNUM = 1u<<8, /* Value must be positive 32-bit number */
138 a_AMV_VF_LOWER = 1u<<9, /* Values will be stored in a lowercase version */
139 a_AMV_VF_VIP = 1u<<10, /* Wants _var_check_vips() evaluation */
140 a_AMV_VF_IMPORT = 1u<<11, /* Import ONLY from environ (pre n_PSO_STARTED) */
141 a_AMV_VF_ENV = 1u<<12, /* Update environment on change */
142 a_AMV_VF_I3VAL = 1u<<13, /* Has an initial value */
143 a_AMV_VF_DEFVAL = 1u<<14, /* Has a default value */
144 a_AMV_VF_LINKED = 1u<<15, /* `environ' linked */
145 a_AMV_VF__MASK = (1u<<(15+1)) - 1
148 struct a_amv_var_map{
149 ui32_t avm_hash;
150 ui16_t avm_keyoff;
151 ui16_t avm_flags; /* enum a_amv_var_flags */
154 #define n_CTA(A,S)
155 #include "gen-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 + n_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] = n_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] != n_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;
199 main(int argc, char **argv){
200 size_t *arr, size = n_NELEM(a_amv_var_map);
202 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
203 for(;;){
204 arr = reversy(size = next_prime(size));
205 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
206 size, longest_distance, seen_wraparound);
207 if(longest_distance <= MAX_DISTANCE_PENALTY)
208 break;
209 free(arr);
212 printf(
213 "#define a_AMV_VAR_REV_ILL %zuu\n"
214 "#define a_AMV_VAR_REV_PRIME %zuu\n"
215 "#define a_AMV_VAR_REV_LONGEST %zuu\n"
216 "#define a_AMV_VAR_REV_WRAPAROUND %d\n"
217 "static %s const a_amv_var_revmap[a_AMV_VAR_REV_PRIME] = {\n%s",
218 n_NELEM(a_amv_var_map), size, longest_distance, seen_wraparound,
219 argv[1], (argc > 2 ? " " : ""));
220 for(size_t i = 0; i < size; ++i)
221 printf("%s%zuu", (i == 0 ? ""
222 : (i % 10 == 0 ? (argc > 2 ? ",\n " : ",\n")
223 : (argc > 2 ? ", " : ","))),
224 arr[i]);
225 printf("\n};\n");
226 return 0;
228 _EOT
229 # <<<<<<<<<<<<<<<<<<<
230 close F
233 sub hash_em{
234 die "hash_em: open: $^E"
235 unless my $pid = open2 *RFD, *WFD, $MAILX;
236 foreach my $e (@ENTS){
237 print WFD "vexpr hash $e->{name}\n";
238 my $h = <RFD>;
239 chomp $h;
240 $e->{hash} = $h
242 print WFD "x\n";
243 waitpid $pid, 0;
246 sub dump_map{
247 die "$OUT: open: $^E" unless open F, '>', $OUT;
248 print F "/*@ $OUT, generated by $0.\n",
249 " *@ See accmacvar.c for more */\n\n";
251 print F 'static char const a_amv_var_names[] = {', "\n";
252 my ($i, $alen) = (0, 0);
253 my (%virts, %defvals, %i3vals);
254 foreach my $e (@ENTS){
255 $e->{keyoff} = $alen;
256 my $k = $e->{name};
257 my $l = length $k;
258 my $a = join '\',\'', split(//, $k);
259 my (@fa);
260 if($e->{bool}) {push @fa, 'a_AMV_VF_BOOL'}
261 if($e->{virt}){
262 # Virtuals are implicitly rdonly and nodel
263 $e->{rdonly} = $e->{nodel} = 1;
264 $virts{$k} = $e;
265 push @fa, 'a_AMV_VF_VIRT'
267 if(defined $e->{i3val}){
268 $i3vals{$k} = $e;
269 push @fa, 'a_AMV_VF_I3VAL'
271 if($e->{defval}){
272 $defvals{$k} = $e;
273 push @fa, 'a_AMV_VF_DEFVAL'
275 if($e->{import}){
276 $e->{env} = 1;
277 push @fa, 'a_AMV_VF_IMPORT'
279 if($e->{nolopts}) {push @fa, 'a_AMV_VF_NOLOPTS'}
280 if($e->{rdonly}) {push @fa, 'a_AMV_VF_RDONLY'}
281 if($e->{nodel}) {push @fa, 'a_AMV_VF_NODEL'}
282 if($e->{notempty}) {push @fa, 'a_AMV_VF_NOTEMPTY'}
283 if($e->{nocntrls}) {push @fa, 'a_AMV_VF_NOCNTRLS'}
284 if($e->{num}) {push @fa, 'a_AMV_VF_NUM'}
285 if($e->{posnum}) {push @fa, 'a_AMV_VF_POSNUM'}
286 if($e->{lower}) {push @fa, 'a_AMV_VF_LOWER'}
287 if($e->{vip}) {push @fa, 'a_AMV_VF_VIP'}
288 if($e->{env}) {push @fa, 'a_AMV_VF_ENV'}
289 $e->{flags} = \@fa;
290 my $f = join('|', @fa);
291 $f = ', ' . $f if length $f;
292 print F "${S}/* $i. [$alen]+$l $k$f */\n" if $VERB;
293 print F "${S}'$a','\\0',\n";
294 ++$i;
295 $alen += $l + 1
297 print F '};', "\n\n";
299 print F 'n_CTA(a_AMV_VF_NONE == 0, "Value not 0 as expected");', "\n";
300 print F 'static struct a_amv_var_map const a_amv_var_map[] = {', "\n";
301 foreach my $e (@ENTS){
302 my $f = $VERB ? 'a_AMV_VF_NONE' : '0';
303 my $fa = join '|', @{$e->{flags}};
304 $f .= '|' . $fa if length $fa;
305 print F "${S}{$e->{hash}u, $e->{keyoff}u, $f},";
306 if($VERB) {print F "${S}/* $e->{name} */\n"}
307 else {print F "\n"}
309 print F '};', "\n\n";
311 # We have at least version stuff in here
312 # The problem is that struct var uses a variable sized character buffer
313 # which cannot be initialized in a conforming way :(
314 print F <<_EOT;
315 #ifndef __CREATE_OKEY_MAP_PL
316 # ifdef HAVE_PUTENV
317 # define a_X(X) X
318 # else
319 # define a_X(X)
320 # endif
322 /* Unfortunately init of varsized buffer won't work: define "subclass"es */
323 _EOT
324 my @skeys = sort keys %virts;
326 foreach(@skeys){
327 my $e = $virts{$_};
328 $e->{vname} = $1 if $e->{enum} =~ /ok_._(.*)/;
329 $e->{vstruct} = "var_virt_$e->{vname}";
330 print F "static char const a_amv_$e->{vstruct}_val[] = {$e->{virt}};\n";
331 print F "static struct{\n";
332 print F "${S}struct a_amv_var *av_link;\n";
333 print F "${S}char const *av_value;\n";
334 print F "${S}a_X(char *av_env;)\n";
335 print F "${S}ui16_t av_flags;\n";
336 print F "${S}char const av_name[", length($e->{name}), " +1];\n";
337 my $f = $VERB ? 'a_AMV_VF_NONE' : '0';
338 my $fa = join '|', @{$e->{flags}};
339 $f .= '|' . $fa if length $fa;
340 print F "} const a_amv_$e->{vstruct} = ",
341 "{NULL, a_amv_$e->{vstruct}_val, a_X(0 COMMA) $f, ",
342 "\"$e->{name}\"};\n\n"
344 print F "# undef a_X\n";
346 print F "\n";
347 print F '#define a_AMV_VAR_VIRTS_CNT ', scalar @skeys, "\n";
348 print F 'static struct a_amv_var_virt const a_amv_var_virts[] = {', "\n";
349 foreach(@skeys){
350 my $e = $virts{$_};
351 my $n = $1 if $e->{enum} =~ /ok_._(.*)/;
352 print F "${S}{$e->{enum}, {0,}, (void const*)&a_amv_$e->{vstruct}},\n";
354 print F "};\n";
357 @skeys = sort keys %i3vals;
359 print F "\n";
360 print F '#define a_AMV_VAR_I3VALS_CNT ', scalar @skeys, "\n";
361 print F 'static struct a_amv_var_defval const a_amv_var_i3vals[] = {', "\n";
362 foreach(@skeys){
363 my $e = $i3vals{$_};
364 print F "${S}{", $e->{enum}, ', {0,}, ',
365 (!$e->{bool} ? $e->{i3val} : "NULL"), "},\n"
367 print F "};\n";
370 @skeys = sort keys %defvals;
372 print F "\n";
373 print F '#define a_AMV_VAR_DEFVALS_CNT ', scalar @skeys, "\n";
374 print F 'static struct a_amv_var_defval const a_amv_var_defvals[] = {', "\n";
375 foreach(@skeys){
376 my $e = $defvals{$_};
377 print F "${S}{", $e->{enum}, ', {0,}, ',
378 (!$e->{bool} ? $e->{defval} : "NULL"), "},\n"
380 print F "};\n";
382 print F "#endif /* __CREATE_OKEY_MAP_PL */\n";
384 # Special var backing [#@*?]|[1-9][0-9]*|0
385 $i = 0;
386 print F "\n";
387 foreach my $e (@ENTS){
388 if($e->{name} eq '--special-param'){
389 print F "#define a_AMV_VAR__SPECIAL_PARAM_MAP_IDX ${i}u\n"
391 # The rest are only speedups
392 elsif($e->{name} eq '?'){
393 print F "#define a_AMV_VAR__QM_MAP_IDX ${i}u\n"
394 }elsif($e->{name} eq '!'){
395 print F "#define a_AMV_VAR__EM_MAP_IDX ${i}u\n"
397 ++$i
400 print F "\n";
401 die "$OUT: close: $^E" unless close F
404 sub reverser{
405 my $argv2 = $VERB ? ' verb' : '';
406 system("c99 -I. -o $CTOOL_EXE $CTOOL");
407 my $t = (@ENTS < 0xFF ? 'ui8_t' : (@ENTS < 0xFFFF ? 'ui16_t' : 'ui32_t'));
408 `$CTOOL_EXE $t$argv2 >> $OUT`
411 {package main; main_fun()}
413 # s-it-mode