mx-test.sh: drop permanent jobreaper, just start a new one, always
[s-mailx.git] / mk / su-make-errors.sh
blob37185570e797f5b27eaa96532b6a5f678a720b5b
1 #!/bin/sh -
2 #@ Either create src/su/gen-errors.h, or, at compile time, the OS<>SU map.
4 # Public Domain
6 IN="${SRCDIR}"su/gen-errors.h
7 XOUT=src/su/gen-errors.h
9 # We use `csop' for hashing
10 MAILX='LC_ALL=C s-nail -#:/'
12 # Acceptable "longest distance" from hash-modulo-index to key
13 MAXDISTANCE_PENALTY=5
15 # Generate a more verbose output. Not for shipout versions.
16 VERB=1
20 LC_ALL=C
21 export LC_ALL MAXDISTANCE_PENALTY VERB MAILX IN XOUT
23 : ${awk:=awk}
24 # Compile-time only
25 : ${rm:=rm}
26 : ${sort:=sort}
28 # The set of errors we support {{{
29 ERRORS="\
30 NONE='No error' \
31 2BIG='Argument list too long' \
32 ACCES='Permission denied' \
33 ADDRINUSE='Address already in use' \
34 ADDRNOTAVAIL='Cannot assign requested address' \
35 AFNOSUPPORT='Address family not supported by protocol family' \
36 AGAIN='Resource temporarily unavailable' \
37 ALREADY='Operation already in progress' \
38 BADF='Bad file descriptor' \
39 BADMSG='Bad message' \
40 BUSY='Device busy' \
41 CANCELED='Operation canceled' \
42 CHILD='No child processes' \
43 CONNABORTED='Software caused connection abort' \
44 CONNREFUSED='Connection refused' \
45 CONNRESET='Connection reset by peer' \
46 DEADLK='Resource deadlock avoided' \
47 DESTADDRREQ='Destination address required' \
48 DOM='Numerical argument out of domain' \
49 DQUOT='Disc quota exceeded' \
50 EXIST='File exists' \
51 FAULT='Bad address' \
52 FBIG='File too large' \
53 HOSTUNREACH='No route to host' \
54 IDRM='Identifier removed' \
55 ILSEQ='Illegal byte sequence' \
56 INPROGRESS='Operation now in progress' \
57 INTR='Interrupted system call' \
58 INVAL='Invalid argument' \
59 IO='Input/output error' \
60 ISCONN='Socket is already connected' \
61 ISDIR='Is a directory' \
62 LOOP='Too many levels of symbolic links' \
63 MFILE='Too many open files' \
64 MLINK='Too many links' \
65 MSGSIZE='Message too long' \
66 MULTIHOP='Multihop attempted' \
67 NAMETOOLONG='File name too long' \
68 NETDOWN='Network is down' \
69 NETRESET='Network dropped connection on reset' \
70 NETUNREACH='Network is unreachable' \
71 NFILE='Too many open files in system' \
72 NOBUFS='No buffer space available' \
73 NODATA='No data available' \
74 NODEV='Operation not supported by device' \
75 NOENT='No such entry, file or directory' \
76 NOEXEC='Exec format error' \
77 NOLCK='No locks available' \
78 NOLINK='Link has been severed' \
79 NOMEM='Cannot allocate memory' \
80 NOMSG='No message of desired type' \
81 NOPROTOOPT='Protocol not available' \
82 NOSPC='No space left on device' \
83 NOSR='Out of streams resource' \
84 NOSTR='Device not a stream' \
85 NOSYS='Function not implemented' \
86 NOTCONN='Socket is not connected' \
87 NOTDIR='Not a directory' \
88 NOTEMPTY='Directory not empty' \
89 NOTOBACCO='No tobacco, snorkeling on empty pipe' \
90 NOTSOCK='Socket operation on non-socket' \
91 NOTSUP='Operation not supported' \
92 NOTTY='Inappropriate ioctl for device' \
93 NXIO='Device not configured' \
94 OPNOTSUPP='Operation not supported' \
95 OVERFLOW='Value too large to be stored in data type' \
96 PERM='Operation not permitted' \
97 PIPE='Broken pipe' \
98 PROTO='Protocol error' \
99 PROTONOSUPPORT='Protocol not supported' \
100 PROTOTYPE='Protocol wrong type for socket' \
101 RANGE='Result too large' \
102 ROFS='Read-only filesystem' \
103 SPIPE='Invalid seek' \
104 SRCH='No such process' \
105 STALE='Stale NFS file handle' \
106 TIME='Timer expired' \
107 TIMEDOUT='Operation timed out' \
108 TXTBSY='Text file busy' \
109 WOULDBLOCK='Operation would block' \
110 XDEV='Cross-device link' \
112 export ERRORS
113 # }}}
115 error_parse() {
116 j=\'
117 ${awk} -v dodoc="${1}" -v incnone="${2}" -v input="${ERRORS}" '
118 BEGIN{
119 for(i = 0;;){
120 voff = match(input, /[0-9a-zA-Z_]+(='${j}'[^'${j}']+)?/)
121 if(voff == 0)
122 break
123 v = substr(input, voff, RLENGTH)
124 input = substr(input, voff + RLENGTH)
125 doff = index(v, "=")
126 if(doff > 0){
127 d = substr(v, doff + 2, length(v) - doff - 1)
128 v = substr(v, 1, doff - 1)
130 if(!incnone && (v == "NONE" || v == "NOTOBACCO"))
131 continue
132 print dodoc ? d : v
138 compile_time() { # {{{
139 [ -n "${TARGET}" ] || {
140 echo >&2 'Invalid usage'
141 exit 1
143 set -e
146 printf '#include <errno.h>\nsu_ERROR_START\n'
147 for n in `error_parse 0 0`; do
148 printf '#ifdef E%s\nE%s %s\n#else\n-1 %s\n#endif\n' $n $n $n $n
149 done
150 } > "${TARGET}".c
152 # The problem is that at least (some versions of) gcc mangle output.
153 # Ensure we get both arguments on one line.
154 # While here sort numerically.
155 "${CC}" -E "${TARGET}".c |
156 ${awk} '
157 BEGIN{hot=0; conti=0}
158 /^[ ]*$/{next}
159 /^[ ]*#/{next}
160 /^su_ERROR_START$/{hot=1; next}
162 if(!hot)
163 next
164 printf "%s ", $1
165 if(conti){
166 printf "\n"
167 conti = 0
168 }else if($2 != "")
169 printf $2 "\n"
170 else
171 conti = 1
174 ${sort} -n > "${TARGET}".txt
176 # EBCDIC/ASCII: we use \134 for reverse solidus \
177 j=\'
178 ${awk} -v verb="${VERB}" -v input="${ERRORS}" -v dat="${TARGET}.txt" '
179 BEGIN{
180 verb = (verb != 0) ? " " : ""
182 # Read in our OS data
184 unavail = 0
185 max = 0
186 oscnt = 0
187 while((getline dl < dat) > 0){
188 split(dl, ia)
190 ++oscnt
191 osnoa[oscnt] = ia[1]
192 osnaa[oscnt] = ia[2]
194 if(ia[1] < 0)
195 ++unavail
196 else{
197 if(ia[1] > max)
198 max = ia[1]
201 close(dat)
203 # Maximum error number defines the datatype to use.
204 # We need a value for NOTOBACCO, we warp all non-available errors to
205 # numbers too high to be regular errors, counting backwards
207 i = max + unavail + 1
208 if(i >= 65535){
209 t = "u32"
210 max = "0xFFFFFFFFu"
211 }else if(i >= 255){
212 t = "u16"
213 max = "0xFFFFu"
214 }else{
215 t = "u8"
216 max = "0xFFu"
218 print "#define su__ERR_NUMBER_TYPE " t
219 print "#define su__ERR_NUMBER_MAX " max
221 # Dump C table
223 cnt = 0
224 print "#define su__ERR_NUMBER_ENUM_C \134"
226 print verb "su_ERR_NONE = 0,\134"
227 ++cnt
229 # Since our final table is searched with binary sort,
230 # place the non-available backward counting once last
231 unavail = j = k = 0
232 for(i = 1; i <= oscnt; ++i){
233 if(osnoa[i] >= 0){
234 map[osnaa[i]] = osnoa[i]
235 print verb "su_ERR_" osnaa[i] " = " osnoa[i] ",\134"
236 ++cnt
237 }else{
238 ++unavail
239 the_unavail[unavail] = "su_ERR_" osnaa[i] " = " \
240 "(su__ERR_NUMBER_MAX - " unavail ")"
243 for(i = unavail; i >= 1; --i){
244 print verb the_unavail[i] ",\134"
245 ++cnt
248 print verb "su_ERR_NOTOBACCO = su__ERR_NUMBER_MAX,\134"
249 ++cnt
251 print verb "su__ERR_NUMBER = " cnt
253 # The C++ mapping table
255 print "#ifdef __cplusplus"
256 print "# define su__CXX_ERR_NUMBER_ENUM \134"
257 print verb "enone = su_ERR_NONE,\134"
259 unavail = 0
260 for(i = 1; i <= oscnt; ++i){
261 if(osnoa[i] >= 0)
262 print verb "e" tolower(osnaa[i]) " = su_ERR_" osnaa[i] ",\134"
263 else{
264 ++unavail
265 the_unavail[unavail] = "e" tolower(osnaa[i]) " = su_ERR_" \
266 osnaa[i]
269 for(i = unavail; i >= 1; --i){
270 print verb the_unavail[i] ",\134"
271 ++cnt
273 print verb "enotobacco = su_ERR_NOTOBACCO,\134"
274 print verb "e__number = su__ERR_NUMBER"
276 print "#endif /* __cplusplus */"
278 # And our OS errno -> name map offset table
279 # (This "OS" however includes the unavail ones)
281 voidoff = 0
282 for(mapoff = 0;; ++mapoff){
283 voff = match(input, /[0-9a-zA-Z_]+(='${j}'[^'${j}']+)?/)
284 if(voff == 0)
285 break
287 v = substr(input, voff, RLENGTH)
288 input = substr(input, voff + RLENGTH)
289 doff = index(v, "=")
290 if(doff > 0){
291 d = substr(v, doff + 2, length(v) - doff - 1)
292 v = substr(v, 1, doff - 1)
294 mapo[v] = mapoff
295 if(v == "NOTOBACCO")
296 voidoff = mapoff
299 uniq = 0
300 print "#define su__ERR_NUMBER_VOIDOFF " voidoff
301 print "#define su__ERR_NUMBER_TO_MAPOFF \134"
303 print verb "a_X(0, 0) \134"
304 ++uniq
306 unavail = 0
307 mapdups[0] = 1
308 for(i = 1; i <= oscnt; ++i){
309 if(osnoa[i] < 0){
310 the_unavail[++unavail] = i
311 continue
313 if(mapdups[osnoa[i]])
314 continue
315 mapdups[osnoa[i]] = 1
316 print verb "a_X(" osnoa[i] ", " mapo[osnaa[i]] ") \134"
317 ++uniq
320 for(i = unavail; i >= 1; --i){
321 print verb "a_X(" "su__ERR_NUMBER_MAX - " i ", " \
322 mapo[osnaa[the_unavail[i]]] ")\134"
323 ++uniq
326 print verb "a_X(su__ERR_NUMBER_MAX, su__ERR_NUMBER_VOIDOFF) \134"
327 ++uniq
328 print verb "/* " uniq " unique members */"
330 ' >> "${TARGET}"
332 ${rm} "${TARGET}".*
333 exit 0
334 } # }}}
336 if [ ${#} -ne 0 ]; then
337 if [ "${1}" = noverbose ]; then
338 shift
339 VERB=0
340 export VERB
344 if [ ${#} -eq 1 ]; then
345 [ "${1}" = compile_time ] && compile_time
346 elif [ ${#} -eq 0 ]; then
347 # Now start perl(1) without PERL5OPT set to avoid multibyte sequence errors
348 PERL5OPT= PERL5LIB= exec perl -x "${0}"
350 echo >&2 'Invalid usage'
351 exit 1
353 # PERL {{{
354 # Thanks to perl(5) and it's -x / #! perl / __END__ mechanism!
355 # Why can env(1) not be used for such easy things in #!?
356 #!perl
358 #use diagnostics -verbose;
359 use strict;
360 use warnings;
362 use FileHandle;
363 use IPC::Open2;
365 use sigtrap qw(handler cleanup normal-signals);
367 my ($S, @ENTS, $CTOOL, $CTOOL_EXE) = ($ENV{VERB} ? ' ' : '');
369 sub main_fun{
370 create_ents();
372 create_c_tool();
374 hash_em();
376 dump_map(); # Starts output file
378 reverser(); # Ends output file
380 cleanup(undef);
381 exit 0
384 sub cleanup{
385 die "$CTOOL_EXE: couldn't unlink: $^E"
386 if $CTOOL_EXE && -f $CTOOL_EXE && 1 != unlink $CTOOL_EXE;
387 die "$CTOOL: couldn't unlink: $^E"
388 if $CTOOL && -f $CTOOL && 1 != unlink $CTOOL;
389 die "Terminating due to signal $_[0]" if $_[0]
392 sub basen{
393 my $n = $_[0];
394 $n =~ s/^(.*\/)?([^\/]+)$/$2/;
398 sub create_ents{
399 my $input = $ENV{ERRORS};
400 while($input =~ /([[:alnum:]_]+)='([^']+)'(.*)/){
401 $input = $3;
402 my %vals;
403 $vals{name} = $1;
404 $vals{doc} = $2;
405 push @ENTS, \%vals
409 sub create_c_tool{
410 $CTOOL = './tmp-errors-tool-' . $$ . '.c';
411 $CTOOL_EXE = $CTOOL . '.exe';
413 die "$CTOOL: open: $^E" unless open F, '>', $CTOOL;
414 print F '#define MAX_DISTANCE_PENALTY ', $ENV{MAXDISTANCE_PENALTY}, "\n";
415 # >>>>>>>>>>>>>>>>>>>
416 print F <<'_EOT';
417 #define __CREATE_ERRORS_SH
418 #define su_SOURCE
419 #include <stdint.h>
420 #include <stdlib.h>
421 #include <stdio.h>
422 #include <string.h>
424 #define su_NELEM(A) (sizeof(A) / sizeof((A)[0]))
426 #define u32 uint32_t
427 #define s32 int32_t
428 #define u16 uint16_t
429 #define u8 uint8_t
431 struct a_corerr_map{
432 u32 cem_hash; /* Hash of name */
433 u32 cem_nameoff; /* Into a_corerr_names[] */
434 u32 cem_docoff; /* Into a_corerr_docs[] */
435 s32 cem_errno; /* OS errno value for this one */
438 _EOT
440 print F '#include "', $ENV{XOUT}, "\"\n\n";
442 print F <<'_EOT';
443 static u8 seen_wraparound;
444 static size_t longest_distance;
446 static size_t
447 next_prime(size_t no){ /* blush (brute force) */
448 jredo:
449 ++no;
450 for(size_t i = 3; i < no; i += 2)
451 if(no % i == 0)
452 goto jredo;
453 return no;
456 static size_t *
457 reversy(size_t size){
458 struct a_corerr_map const *cemp = a_corerr_map,
459 *cemaxp = cemp + su_NELEM(a_corerr_map);
460 size_t ldist = 0, *arr;
462 arr = (size_t*)malloc(sizeof *arr * size);
463 for(size_t i = 0; i < size; ++i)
464 arr[i] = su_NELEM(a_corerr_map);
466 seen_wraparound = 0;
467 longest_distance = 0;
469 while(cemp < cemaxp){
470 u32 hash = cemp->cem_hash, i = hash % size, l;
472 for(l = 0; arr[i] != su_NELEM(a_corerr_map); ++l)
473 if(++i == size){
474 seen_wraparound = 1;
475 i = 0;
477 if(l > longest_distance)
478 longest_distance = l;
479 arr[i] = (size_t)(cemp++ - a_corerr_map);
481 return arr;
485 main(int argc, char **argv){
486 size_t *arr, size = su_NELEM(a_corerr_map);
488 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
489 for(;;){
490 arr = reversy(size = next_prime(size));
491 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
492 size, longest_distance, seen_wraparound);
493 if(longest_distance <= MAX_DISTANCE_PENALTY)
494 break;
495 free(arr);
498 printf(
499 "#ifdef su_SOURCE /* Lock-out compile-time-tools */\n"
500 "# define a_CORERR_REV_ILL %zuu\n"
501 "# define a_CORERR_REV_PRIME %zuu\n"
502 "# define a_CORERR_REV_LONGEST %zuu\n"
503 "# define a_CORERR_REV_WRAPAROUND %d\n"
504 "static %s const a_corerr_revmap[a_CORERR_REV_PRIME] = {\n%s",
505 su_NELEM(a_corerr_map), size, longest_distance, seen_wraparound,
506 argv[1], (argc > 2 ? " " : ""));
507 for(size_t i = 0; i < size; ++i)
508 printf("%s%zuu", (i == 0 ? ""
509 : (i % 10 == 0 ? (argc > 2 ? ",\n " : ",\n")
510 : (argc > 2 ? ", " : ","))),
511 arr[i]);
512 printf("\n};\n#endif /* su_SOURCE */\n");
513 return 0;
515 _EOT
516 # <<<<<<<<<<<<<<<<<<<
517 close F
520 sub hash_em{
521 die "hash_em: open: $^E"
522 unless my $pid = open2 *RFD, *WFD, $ENV{MAILX};
523 foreach my $e (@ENTS){
524 print WFD "csop hash32 $e->{name}\n";
525 my $h = <RFD>;
526 chomp $h;
527 $e->{hash} = $h
529 print WFD "x\n";
530 waitpid $pid, 0;
533 sub dump_map{
534 my ($i, $alen);
536 die "$ENV{XOUT}: open: $^E" unless open F, '>', $ENV{XOUT};
537 print F '/*@ ', scalar basen($ENV{XOUT}), ', generated by ',
538 scalar basen($0), ".\n *@ See core-errors.c for more */\n\n";
540 ($i, $alen) = (0, 0);
541 print F '#ifdef su_SOURCE', "\n",
542 'static char const a_corerr_names[] = {', "\n";
543 ($i, $alen) = (0, 0);
544 foreach my $e (@ENTS){
545 $e->{nameoff} = $alen;
546 my $k = $e->{name};
547 my $l = length $k;
548 my $a = join '\',\'', split(//, $k);
549 my (@fa);
550 print F "${S}/* $i. [$alen]+$l $k */\n" if $ENV{VERB};
551 print F "${S}'$a','\\0',\n";
552 ++$i;
553 $alen += $l + 1
555 print F '};', "\n\n";
557 print F '# ifdef su_HAVE_DOCSTRINGS', "\n";
558 print F '# undef a_X', "\n", '# define a_X(X)', "\n";
559 print F 'static char const a_corerr_docs[] = {', "\n";
560 ($i, $alen) = (0, 0);
561 foreach my $e (@ENTS){
562 $e->{docoff} = $alen;
563 my $k = $e->{doc};
564 my $l = length $k;
565 my $a = join '\',\'', split(//, $k);
566 my (@fa);
567 print F "${S}/* $i. [$alen]+$l $e->{name} */ ",
568 "a_X(N_(\"$e->{doc}\"))\n" if $ENV{VERB};
569 print F "${S}'$a','\\0',\n";
570 ++$i;
571 $alen += $l + 1
573 print F '};', "\n", '# undef a_X',
574 "\n# endif /* su_HAVE_DOCSTRINGS */\n\n";
576 print F <<_EOT;
577 # undef a_X
578 # ifndef __CREATE_ERRORS_SH
579 # define a_X(X) X
580 # else
581 # define a_X(X) 0
582 # endif
583 static struct a_corerr_map const a_corerr_map[] = {
584 _EOT
585 foreach my $e (@ENTS){
586 print F "${S}{$e->{hash}u, $e->{nameoff}u, $e->{docoff}u, ",
587 "a_X(su_ERR_$e->{name})},\n"
589 print F '};', "\n", '# undef a_X', "\n",
590 '#endif /* su_SOURCE */', "\n\n";
592 die "$ENV{XOUT}: close: $^E" unless close F
595 sub reverser{
596 my $argv2 = $ENV{VERB} ? ' verb' : '';
597 system("\$CC -I. -o $CTOOL_EXE $CTOOL");
598 my $t = (@ENTS < 0xFF ? 'u8' : (@ENTS < 0xFFFF ? 'u16' : 'u32'));
599 `$CTOOL_EXE $t$argv2 >> $ENV{XOUT}`
602 {package main; main_fun()}
603 # }}}
605 # s-it-mode