2 #@ Create gen-errors.h. And see auxlily.c.
5 # Acceptable "longest distance" from hash-modulo-index to key
8 # Generate a more verbose output. Not for shipout versions.
11 MAILX
='LC_ALL=C s-nail -#:/'
12 OUT
="${SRCDIR}"gen-errors.h
17 export LC_ALL MAXDISTANCE_PENALTY VERB MAILX OUT
19 : ${awk:=`command -v awk`}
21 # The set of errors we support
24 2BIG='Argument list too long' \
25 ACCES='Permission denied' \
26 ADDRINUSE='Address already in use' \
27 ADDRNOTAVAIL='Cannot assign requested address' \
28 AFNOSUPPORT='Address family not supported by protocol family' \
29 AGAIN='Resource temporarily unavailable' \
30 ALREADY='Operation already in progress' \
31 BADF='Bad file descriptor' \
32 BADMSG='Bad message' \
34 CANCELED='Operation canceled' \
35 CHILD='No child processes' \
36 CONNABORTED='Software caused connection abort' \
37 CONNREFUSED='Connection refused' \
38 CONNRESET='Connection reset by peer' \
39 DEADLK='Resource deadlock avoided' \
40 DESTADDRREQ='Destination address required' \
41 DOM='Numerical argument out of domain' \
42 DQUOT='Disc quota exceeded' \
45 FBIG='File too large' \
46 HOSTUNREACH='No route to host' \
47 IDRM='Identifier removed' \
48 ILSEQ='Illegal byte sequence' \
49 INPROGRESS='Operation now in progress' \
50 INTR='Interrupted system call' \
51 INVAL='Invalid argument' \
52 IO='Input/output error' \
53 ISCONN='Socket is already connected' \
54 ISDIR='Is a directory' \
55 MFILE='Too many open files' \
56 MLINK='Too many links' \
57 MSGSIZE='Message too long' \
58 MULTIHOP='Multihop attempted' \
59 NAMETOOLONG='File name too long' \
60 NETDOWN='Network is down' \
61 NETRESET='Network dropped connection on reset' \
62 NETUNREACH='Network is unreachable' \
63 NFILE='Too many open files in system' \
64 NOBUFS='No buffer space available' \
65 NODATA='No data available' \
66 NODEV='Operation not supported by device' \
67 NOENT='No such file or directory' \
68 NOEXEC='Exec format error' \
69 NOLCK='No locks available' \
70 NOLINK='Link has been severed' \
71 NOMEM='Cannot allocate memory' \
72 NOMSG='No message of desired type' \
73 NOPROTOOPT='Protocol not available' \
74 NOSPC='No space left on device' \
75 NOSR='Out of streams resource' \
76 NOSTR='Device not a stream' \
77 NOSYS='Function not implemented' \
78 NOTCONN='Socket is not connected' \
79 NOTDIR='Not a directory' \
80 NOTEMPTY='Directory not empty' \
81 NOTOBACCO='Snorkeling on empty pipe' \
82 NOTSOCK='Socket operation on non-socket' \
83 NOTSUP='Operation not supported' \
84 NOTTY='Inappropriate ioctl for device' \
85 NXIO='Device not configured' \
86 OPNOTSUPP='Operation not supported' \
87 OVERFLOW='Value too large to be stored in data type' \
88 PERM='Operation not permitted' \
90 PROTO='Protocol error' \
91 PROTONOSUPPORT='Protocol not supported' \
92 PROTOTYPE='Protocol wrong type for socket' \
93 RANGE='Result too large' \
94 ROFS='Read-only filesystem' \
95 SPIPE='Invalid seek' \
96 SRCH='No such process' \
97 STALE='Stale NFS file handle' \
98 TIME='Timer expired' \
99 TIMEDOUT='Operation timed out' \
100 TXTBSY='Text file busy' \
101 WOULDBLOCK='Operation would block' \
102 XDEV='Cross-device link' \
108 ${awk} -v dodoc="${1}" -v incnone="${2}" -v input="${ERRORS}" '
111 voff = match(input, /[[:alnum:]_]+(='${j}'[^'${j}']+)?/)
114 v = substr(input, voff, RLENGTH)
115 input = substr(input, voff + RLENGTH)
118 d = substr(v, doff + 2, length(v) - doff - 1)
119 v = substr(v, 1, doff - 1)
121 if(!incnone && v == "NONE
")
130 [ -n "${TARGET}" ] || {
131 echo >&2 'Invalid usage'
134 # Note this may be ISO C89, so we cannot
141 #if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L
144 # include <inttypes.h>
147 typedef uint32_t ui32_t;
148 typedef int32_t si32_t;
149 #elif ULONG_MAX == 0xFFFFFFFFu
150 typedef unsigned long int ui32_t;
151 typedef signed long int si32_t;
153 typedef unsigned int ui32_t;
154 typedef signed int si32_t;
156 struct a_in {struct a_in *next; char const *name; si32_t no; ui32_t uno;};
157 static int a_sortin(void const *a, void const *b){
158 return (*(struct a_in const* const *)a)->uno -
159 (*(struct a_in const* const *)b)->uno;
162 char line[1024], *lp;
163 struct a_in *head, *tail, *np, **nap;
165 si32_t xavail = 0, total = 1, imin = 0, imax = 0, voidoff = 0,
167 FILE *ifp = fopen("${OUT}", "r
"), *ofp = fopen("${TARGET}", "a
");
168 if(ifp == NULL || ofp == NULL){
169 fprintf(stderr, "ERROR
: cannot open in- or output
\n");
173 /* Create a list of all errors */
174 head = tail = malloc(sizeof *head);
175 head->next = NULL; head->name = "n_ERR_NONE
"; head->no = 0;
177 for n in `error_parse 0 0`; do
185 if(imin > i) imin = i; if(imax < i) imax = i;
186 np = malloc(sizeof *np);
187 np->next = NULL; np->name = "n_ERR_
${n}"; np->no = i;
188 tail->next = np; tail = np;
192 /* The unsigned type used for storage */
194 fputs("#define n__ERR_NUMBER_TYPE ", ofp);
195 if((ui32_t
)imax
<= 0xFFu
&& (ui32_t
)-imin <= 0xFFu
){
196 fputs
("ui8_t\n", ofp
);
198 }else if(((ui32_t
)imax
<= 0xFFFFu
&& (ui32_t
)-imin <= 0xFFFFu
)){
199 fputs
("ui16_t\n", ofp
);
202 fputs
("ui32_t\n", ofp
);
203 maxsub
= 0xFFFFFFFFu
;
206 /* Now that we know the storage
type, create the unsigned numbers
*/
207 for(umax
= 0, np
= head; np
!= NULL
; np
= np-
>next
){
209 np-
>uno
= maxsub
+ np-
>no
+ 1;
215 if(umax
<= (ui32_t
)imax
){
216 fprintf
(stderr
, "ERROR: errno ranges overlap\n");
222 nap
= malloc
(sizeof
(*nap
) * (unsigned
)total
);
223 for(i
= 0, np
= head; np
!= NULL
; ++i
, np
= np-
>next
)
226 fprintf
(stderr
, "ERROR: implementation error i != total\n");
229 qsort
(nap
, (ui32_t
)i
, sizeof
*nap
, &a_sortin
);
231 /* The enumeration of numbers
*/
233 fputs
("enum n_err_number{\\n", ofp
);
234 for(i
= 0; i
< total
; ++i
)
235 fprintf
(ofp
, " %s = %lu,\\n",
236 nap
[i
]->name
, (unsigned long
)nap
[i
]->uno
);
237 fprintf
(ofp
, " n__ERR_NUMBER = %ld\\n", (long
)total
);
240 /* The binary search mapping table from OS error value to our internal
241 * a_aux_err_map
[] error description table
*/
243 /* A real hack to have a fast NUMBER
-> MAP mapping without compiling
244 * and running a second C program during config
*/
245 while((lp = fgets
(line
, sizeof line
, ifp
)) != NULL
&&
246 strstr
(lp, "a_aux_err_map[]") == NULL
)
248 if(feof
(ifp
) || ferror
(ifp
)){
249 fprintf
(stderr
, "ERROR: I/O error when reading \"ifp\"\n");
252 telloff
= (int
)ftell
(ifp
);
254 fprintf
(ofp
, "#define n__ERR_NUMBER_TO_MAPOFF \\\\\\n");
255 for(xavail
= 0, i
= 0; i
< total
; ++i
){
256 if(i
== 0 || nap
[i
]->no
!= nap
[i
- 1]->no
){
257 if(fseek
(ifp
, telloff
, SEEK_SET
) == -1){
258 fprintf
(stderr
, "ERROR: I/O error when searching \"ifp\", I.\n");
262 while((lp = fgets
(line
, sizeof line
, ifp
)) != NULL
&&
263 strstr
(lp, nap
[i
]->name
) == NULL
)
265 if(feof
(ifp
) || ferror
(ifp
)){
266 fprintf
(stderr
, "ERROR: I/O error when reading \"ifp\", II.\n");
269 fprintf
(ofp
, "\ta_X(%lu, %lu) %s%s%s\\\\\\n",
270 (unsigned long
)nap
[i
]->uno
, (long
)(ui32_t
)j
,
271 ((${VERB}) ?
"/* " : ""), ((${VERB}) ? nap
[i
]->name
: ""),
272 ((${VERB}) ?
" */ " : ""));
273 if(!strcmp
("n_ERR_NOTOBACCO", nap
[i
]->name
))
278 fprintf
(ofp
, "\\t/* %ld unique members */\\n", (long
)xavail
);
279 fprintf
(ofp
, "#define n__ERR_NUMBER_VOIDOFF %ld\\n", (long
)voidoff
);
282 while((np
= head) != NULL
){
293 if [ ${#} -ne 0 ]; then
294 if [ "${1}" = noverbose
]; then
301 if [ ${#} -eq 1 ]; then
302 [ "${1}" = config
] && config
303 elif [ ${#} -eq 0 ]; then
304 # Now start perl(1) without PERL5OPT set to avoid multibyte sequence errors
305 PERL5OPT
= PERL5LIB
= exec perl
-x "${0}"
307 echo >&2 'Invalid usage'
310 # Thanks to perl(5) and it's -x / #! perl / __END__ mechanism!
311 # Why can env(1) not be used for such easy things in #!?
314 use diagnostics
-verbose;
321 use sigtrap qw
(handler cleanup normal-signals
);
323 my
($S, @ENTS
, $CTOOL, $CTOOL_EXE) = ($ENV{VERB
} ?
' ' : '');
341 die
"$CTOOL_EXE: couldn't unlink: $^E"
342 if $CTOOL_EXE && -f $CTOOL_EXE && 1 != unlink
$CTOOL_EXE;
343 die
"$CTOOL: couldn't unlink: $^E"
344 if $CTOOL && -f $CTOOL && 1 != unlink
$CTOOL;
345 die
"Terminating due to signal $_[0]" if $_[0]
349 my
$input = $ENV{ERRORS
};
350 while($input =~
/([[:alnum
:]_
]+)='([^']+)'(.*)/){
360 $CTOOL = '.
/tmp-errors-tool-
' . $$ . '.c
';
361 $CTOOL_EXE = $CTOOL . '.exe
';
363 die "$CTOOL: open: $^E" unless open F, '>', $CTOOL;
364 print F '#define MAX_DISTANCE_PENALTY ', $ENV{MAXDISTANCE_PENALTY}, "\n";
365 # >>>>>>>>>>>>>>>>>>>
367 #define __CREATE_ERRORS_SH
373 #define n_NELEM(A) (sizeof(A) / sizeof(A[0]))
375 #define ui32_t uint32_t
376 #define si32_t int32_t
377 #define ui16_t uint16_t
378 #define ui8_t uint8_t
380 struct a_aux_err_map{
381 ui32_t aem_hash; /* Hash of name */
382 ui32_t aem_nameoff; /* Into a_aux_err_names[] */
383 ui32_t aem_docoff; /* Into a_aux_err docs[] */
384 si32_t aem_errno; /* The OS errno value for this one */
387 #include "gen-errors.h"
389 static ui8_t seen_wraparound;
390 static size_t longest_distance;
393 next_prime(size_t no){ /* blush (brute force) */
396 for(size_t i = 3; i < no; i += 2)
403 reversy(size_t size){
404 struct a_aux_err_map const *aemp = a_aux_err_map,
405 *aemaxp = aemp + n_NELEM(a_aux_err_map);
406 size_t ldist = 0, *arr;
408 arr = malloc(sizeof *arr * size);
409 for(size_t i = 0; i < size; ++i)
410 arr[i] = n_NELEM(a_aux_err_map);
413 longest_distance = 0;
415 while(aemp < aemaxp){
416 ui32_t hash = aemp->aem_hash, i = hash % size, l;
418 for(l = 0; arr[i] != n_NELEM(a_aux_err_map); ++l)
423 if(l > longest_distance)
424 longest_distance = l;
425 arr[i] = (size_t)(aemp++ - a_aux_err_map);
431 main(int argc, char **argv){
432 size_t *arr, size = n_NELEM(a_aux_err_map);
434 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
436 arr = reversy(size = next_prime(size));
437 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
438 size, longest_distance, seen_wraparound);
439 if(longest_distance <= MAX_DISTANCE_PENALTY)
445 "#define a_AUX_ERR_REV_ILL %zuu\n"
446 "#define a_AUX_ERR_REV_PRIME %zuu\n"
447 "#define a_AUX_ERR_REV_LONGEST %zuu\n"
448 "#define a_AUX_ERR_REV_WRAPAROUND %d\n"
449 "static %s const a_aux_err_revmap[a_AUX_ERR_REV_PRIME] = {\n%s",
450 n_NELEM(a_aux_err_map), size, longest_distance, seen_wraparound,
451 argv[1], (argc > 2 ? " " : ""));
452 for(size_t i = 0; i < size; ++i)
453 printf("%s%zuu", (i == 0 ? ""
454 : (i % 10 == 0 ? (argc > 2 ? ",\n " : ",\n")
455 : (argc > 2 ? ", " : ","))),
461 # <<<<<<<<<<<<<<<<<<<
466 die
"hash_em: open: $^E"
467 unless my
$pid = open2
*RFD
, *WFD
, $ENV{MAILX
};
468 foreach my
$e (@ENTS
){
469 print WFD
"vexpr hash $e->{name}\n";
479 die
"$ENV{OUT}: open: $^E" unless open F
, '>', $ENV{OUT
};
480 print F
"/*@ $ENV{OUT}, generated by $0.\n",
481 " *@ See auxlily.c for more */\n\n";
483 print F
'static char const a_aux_err_names[] = {', "\n";
484 my
($i, $alen) = (0, 0);
485 foreach my
$e (@ENTS
){
486 $e->{nameoff
} = $alen;
489 my
$a = join '\',\'', split(//, $k);
491 print F
"${S}/* $i. [$alen]+$l $k */\n" if $ENV{VERB
};
492 print F
"${S}'$a','\\0',\n";
496 print F
'};', "\n\n";
498 print F
'#undef a_X', "\n", '#define a_X(X)', "\n";
499 print F
'static char const a_aux_err_docs[] = {', "\n";
500 ($i, $alen) = (0, 0);
501 foreach my
$e (@ENTS
){
502 $e->{docoff
} = $alen;
505 my
$a = join '\',\'', split(//, $k);
507 print F
"${S}/* $i. [$alen]+$l $e->{name} */ ",
508 "a_X(N_(\"$e->{doc}\"))\n" if $ENV{VERB
};
509 print F
"${S}'$a','\\0',\n";
513 print F
'};', "\n", '#undef a_X', "\n\n";
517 #ifndef __CREATE_ERRORS_SH
522 static struct a_aux_err_map const a_aux_err_map[] = {
524 foreach my
$e (@ENTS
){
525 print F
"${S}{$e->{hash}u, $e->{nameoff}u, $e->{docoff}u, ",
526 "a_X(n_ERR_$e->{name})},\n"
528 print F
'};', "\n", '#undef a_X', "\n\n";
530 die
"$ENV{OUT}: close: $^E" unless close F
534 my
$argv2 = $ENV{VERB
} ?
' verb' : '';
535 system
("c99 -I. -o $CTOOL_EXE $CTOOL");
536 my
$t = (@ENTS
< 0xFF ?
'ui8_t' : (@ENTS
< 0xFFFF ?
'ui16_t' : 'ui32_t'));
537 `$CTOOL_EXE $t$argv2 >> $ENV{OUT}`
540 {package main
; main_fun
()}