Bump S-nail v14.9.24 ("Black ships ate the sky"), 2022-03-26
[s-mailx.git] / mk / su-make-cs-ctype.sh
blob2a20c4c4f0a86fa1583e7cac6c9328524873234b
1 #!/bin/sh -
2 #@ Create src/su/gen-cs-ctype.h.
3 # Public Domain
5 OUT=src/su/gen-cs-ctype.h
7 # Generate a more verbose output. Not for shipout versions.
8 VERB=1
12 LC_ALL=C
13 export LC_ALL OUT
15 create() {
16 # Note this may be ISO C89, so we cannot
17 cat <<__EOT__
18 #include <ctype.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #define su_S(T,I) ((T)(I))
23 #define u8 uint8_t
24 #define S8_MAX INT8_MAX
25 static void
26 a_csc_init(FILE *fp){
27 u8 i, any;
28 char const *xpre, *pre;
30 fprintf(fp, "CTAV(su_CS_CTYPE_NONE == 0);\n"
31 "#undef a_X\n"
32 "#define a_X(X) su_CONCAT(su_CS_CTYPE_,X)\n"
33 "u16 const su__cs_ctype[S8_MAX + 1] = {\n");
34 xpre = $VERB ? " | " : "|";
35 i = 0;
36 do{
37 any = 0, pre = "";
38 if($VERB)
39 fprintf(fp, " /* 0x%02X=%c */ ",
40 (unsigned)i, (isprint(i) ? (char)i : '?'));
41 if(isalnum(i))
42 fprintf(fp, "%sa_X(ALNUM)", pre), any = 1, pre = xpre;
43 if(isalpha(i))
44 fprintf(fp, "%sa_X(ALPHA)", pre), any = 1, pre = xpre;
45 if(isblank(i))
46 fprintf(fp, "%sa_X(BLANK)", pre), any = 1, pre = xpre;
47 if(iscntrl(i))
48 fprintf(fp, "%sa_X(CNTRL)", pre), any = 1, pre = xpre;
49 if(isdigit(i))
50 fprintf(fp, "%sa_X(DIGIT)", pre), any = 1, pre = xpre;
51 if(isgraph(i))
52 fprintf(fp, "%sa_X(GRAPH)", pre), any = 1, pre = xpre;
53 if(islower(i))
54 fprintf(fp, "%sa_X(LOWER)", pre), any = 1, pre = xpre;
55 if(isprint(i))
56 fprintf(fp, "%sa_X(PRINT)", pre), any = 1, pre = xpre;
57 if(ispunct(i))
58 fprintf(fp, "%sa_X(PUNCT)", pre), any = 1, pre = xpre;
59 if(isspace(i))
60 fprintf(fp, "%sa_X(SPACE)", pre), any = 1, pre = xpre;
61 if(isupper(i))
62 fprintf(fp, "%sa_X(UPPER)", pre), any = 1, pre = xpre;
63 if(isblank(i) || i == '\n')
64 fprintf(fp, "%sa_X(WHITE)", pre), any = 1, pre = xpre;
65 if(isxdigit(i))
66 fprintf(fp, "%sa_X(XDIGIT)", pre), any = 1, pre = xpre;
67 if(!any)
68 fprintf(fp, "a_X(NONE)");
69 fprintf(fp, ",\n");
70 }while(i++ != S8_MAX);
71 fprintf(fp, "};\n#undef a_X\n");
72 if($VERB)
73 fprintf(fp, "\n");
75 fprintf(fp, "u8 const su__cs_tolower[S8_MAX + 1] = {\n");
76 i = any = 0;
77 do{
78 if(any + 7 >= 78){
79 fprintf(fp, "\n");
80 any = 0;
82 if(any == 0 && $VERB)
83 fprintf(fp, " "), any = 3;
84 fprintf(fp, "'\\\x%02X',", (u8)tolower((char)i));
85 any += 7;
86 }while(i++ != S8_MAX);
87 fprintf(fp, "\n};\n");
88 if($VERB)
89 fprintf(fp, "\n");
91 fprintf(fp, "u8 const su__cs_toupper[S8_MAX + 1] = {\n");
92 i = any = 0;
93 do{
94 if(any + 7 >= 78){
95 fprintf(fp, "\n");
96 any = 0;
98 if(any == 0 && $VERB)
99 fprintf(fp, " "), any = 3;
100 fprintf(fp, "'\\\x%02X',", (u8)toupper((char)i));
101 any += 7;
102 }while(i++ != S8_MAX);
103 fprintf(fp, "\n};\n");
104 if($VERB)
105 fprintf(fp, "\n");
107 int main(void){
108 FILE *ofp = fopen("${OUT}", "a");
109 if(ofp == NULL){
110 fprintf(stderr, "ERROR: cannot open output\n");
111 return 1;
113 fprintf(ofp, "/*@ ${OUT}, generated by su-make-cs-ctype.sh.\n"
114 " *@ See cs-ctype.c for more */\n\n");
115 a_csc_init(ofp);
116 fclose(ofp);
117 return 0;
119 __EOT__
122 if [ ${#} -ne 0 ]; then
123 if [ "${1}" = noverbose ]; then
124 shift
125 VERB=0
126 export VERB
130 if [ ${#} -eq 0 ]; then
131 create > "${TMPDIR}"/su-mk-cs-ctype.c || exit 2
132 ${CC} -o "${TMPDIR}"/su-mk-cs-ctype "${TMPDIR}"/su-mk-cs-ctype.c || exit 3
133 rm -f src/su/gen-cs-ctype.h
134 "${TMPDIR}"/su-mk-cs-ctype || exit 4
135 rm -f "${TMPDIR}"/su-mk-cs-ctype "${TMPDIR}"/su-mk-cs-ctype.c || exit 5
136 exit 0
138 echo >&2 'Invalid usage'
139 exit 1
141 # s-it-mode