]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/table_gen.c
2 * IRC - Internet Relay Chat, include/common.c
3 * Copyright (C) 1998 Andrea Cocito
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * The following part of code is NOT included in the actual server's
23 * or library source, it's just used to build the above tables
25 * This should rebuild the actual tables and automatically place them
26 * into this source file, note that this part of code is for developers
27 * only, it's supposed to work on both signed and unsigned chars but I
28 * actually tested it only on signed-char architectures, the code and
29 * macros actually used by the server instead DO work and have been tested
30 * on platforms where0 char is both signed or unsigned, this is true as long
31 * as the <limits.h> macros are set properly and without any need to rebuild
32 * the tables (which as said an admin should NEVER do, tables need to be rebuilt
33 * only when one wants to really change the results or when one has to
34 * compile on architectures where a char is NOT eight bits [?!], yes
35 * it all is supposed to work in that case too... but I can't test it
36 * because I've not found a machine in the world where this happens).
38 * NEVER -f[un]signed-char on gcc since that does NOT fix the named macros
39 * and you end up in a non-ANSI environment where CHAR_MIN and CHAR_MAX
40 * are _not_ the real limits of a default 'char' type. This is true for
41 * both admins and coders.
46 #include "ircd_chattr.h"
52 static void zeroTables(void);
53 static void markString(int macro
, const char *s
);
54 static void unMarkString(int macro
, const char *s
);
55 static void markRange(int macro
, char from
, char to
);
56 static void moveMacro(int from
, int to
);
57 static void setLowHi(const char firstlow
, const char lastlow
,
60 char NTL_tolower_tab
[1 + CHAR_MAX
- CHAR_MIN
]; /* 256 bytes */
61 char NTL_toupper_tab
[1 + CHAR_MAX
- CHAR_MIN
]; /* 256 bytes */
62 int NTL_char_attrib
[1 + CHAR_MAX
- CHAR_MIN
]; /* 256 ints = 0.5 to 2 kilobytes */
66 * Where we make the tables, edit ONLY this to change the tables.
69 static void makeTables(void)
72 /* Start from a known status */
75 /* Make the very elementary sets */
76 markRange(NTL_LOWER
, 'a', 'z');
77 markString(NTL_LOWER
, "{|}~");
79 markRange(NTL_UPPER
, 'A', 'Z');
80 markString(NTL_UPPER
, "[\\]^");
82 markRange(NTL_DIGIT
, '0', '9');
84 markRange(NTL_CNTRL
, '\000', '\037');
86 markString(NTL_PUNCT
, "!\"#$%&'()*+,-./:;<=>?@_`");
88 markString(NTL_SPACE
, "\011\012\013\014\015\040");
90 /* Make the derived sets,
91 * WARNING: The order of these calls is important, some depend on
92 * the results of the previous ones ! */
94 moveMacro(NTL_LOWER
| NTL_UPPER
, NTL_ALPHA
);
95 moveMacro(NTL_ALPHA
| NTL_DIGIT
, NTL_ALNUM
);
96 moveMacro(NTL_ALNUM
| NTL_PUNCT
, NTL_GRAPH
);
98 moveMacro(NTL_GRAPH
, NTL_PRINT
);
99 markString(NTL_PRINT
, " ");
101 markRange(NTL_IRCCH
, '\041', (char) UCHAR_MAX
);
102 unMarkString(NTL_IRCCH
, "\054\240");
104 markRange(NTL_IRCCL
, '\300', '\326');
105 markRange(NTL_IRCCL
, '\330', '\336');
107 moveMacro(NTL_ALNUM
, NTL_IRCHN
);
108 markString(NTL_IRCHN
, "-_."); /* Some DNS might allow '_' per RFC 1033 ! */
110 moveMacro(NTL_DIGIT
, NTL_IRCIP
);
111 markString(NTL_IRCIP
, ".");
113 moveMacro(NTL_DIGIT
, NTL_IRCIP6
);
114 markString(NTL_IRCIP6
, ":.ABCDEFabcdef");
116 moveMacro(NTL_DIGIT
| NTL_ALPHA
, NTL_IRCNK
);
117 markString(NTL_IRCNK
, "-_`");
119 moveMacro(NTL_ALNUM
, NTL_IRCUI
);
120 markRange(NTL_IRCUI
, '\xe0', '\xf6');
121 markRange(NTL_IRCUI
, '\xf8', '\xfe');
122 markRange(NTL_IRCUI
, '\xc0', '\xd6');
123 markRange(NTL_IRCUI
, '\xd8', '\xde');
124 markString(NTL_IRCUI
, ".-_^'`~");
125 markString(NTL_EOL
, "\n\r");
126 markString(NTL_CHPFX
, "#&");
127 markString(NTL_KTIME
, " ,-0123456789");
129 /* And finally let's take care of the toLower/toUpper stuff */
131 setLowHi('a', 'z', 'A');
132 setLowHi('\xe0', '\xf6', '\xc0');
133 setLowHi('\xf8', '\xfe', '\xd8');
134 setLowHi('{', '~', '[');
139 * This is the main program to be executed for -DMAKETABLES
142 static void dumphw(int *p
, int beg
);
143 static void dumphb(char *p
, int beg
);
149 /* Make the tables */
152 /* Dump them as ANSI C source to be included below */
153 printf("/*\n * Automatically Generated Tables - DO NOT EDIT\n */\n");
154 printf("#include <limits.h>\n");
156 /* NTL_tolower_tab */
157 printf("const char ToLowerTab_8859_1[] = {\n");
158 printf("#if (CHAR_MIN<0)\n");
159 i
= (int)((char)SCHAR_MIN
);
160 dumphb(NTL_tolower_tab
, i
);
162 printf("#endif /* (CHAR_MIN<0) */\n");
164 dumphb(NTL_tolower_tab
, i
);
165 printf("#if (!(CHAR_MIN<0))\n");
167 i
= (int)((char)SCHAR_MIN
);
168 dumphb(NTL_tolower_tab
, i
);
169 printf("#endif /* (!(CHAR_MIN<0)) */\n");
172 /* NTL_toupper_tab */
173 printf("const char ToUpperTab_8859_1[] = {\n");
174 printf("#if (CHAR_MIN<0)\n");
175 i
= (int)((char)SCHAR_MIN
);
176 dumphb(NTL_toupper_tab
, i
);
178 printf("#endif /* (CHAR_MIN<0) */\n");
180 dumphb(NTL_toupper_tab
, i
);
181 printf("#if (!(CHAR_MIN<0))\n");
183 i
= (int)((char)SCHAR_MIN
);
184 dumphb(NTL_toupper_tab
, i
);
185 printf("#endif /* (!(CHAR_MIN<0)) */\n");
188 /* NTL_char_attrib */
189 printf("const unsigned int IRCD_CharAttrTab[] = {\n");
190 printf("#if (CHAR_MIN<0)\n");
191 i
= (int)((char)SCHAR_MIN
);
192 dumphw(NTL_char_attrib
, i
);
194 printf("#endif /* (CHAR_MIN<0) */\n");
196 dumphw(NTL_char_attrib
, i
);
197 printf("#if (!(CHAR_MIN<0))\n");
199 i
= (int)((char)SCHAR_MIN
);
200 dumphw(NTL_char_attrib
, i
);
201 printf("#endif /* (!(CHAR_MIN<0)) */\n");
208 /* A few utility functions for makeTables() */
210 static void zeroTables(void)
213 for (i
= CHAR_MIN
; i
<= CHAR_MAX
; i
++)
215 NTL_tolower_tab
[i
- CHAR_MIN
] = (char)i
; /* Unchanged */
216 NTL_toupper_tab
[i
- CHAR_MIN
] = (char)i
; /* Unchanged */
217 NTL_char_attrib
[i
- CHAR_MIN
] = 0x0000; /* Nothing */
221 static void markString(int macro
, const char *s
)
224 NTL_char_attrib
[*(s
++) - CHAR_MIN
] |= macro
;
227 static void unMarkString(int macro
, const char *s
)
230 NTL_char_attrib
[*(s
++) - CHAR_MIN
] &= ~macro
;
233 static void markRange(int macro
, char from
, char to
)
236 for (i
= CHAR_MIN
; i
<= CHAR_MAX
; i
++)
237 if (((unsigned char)i
>= (unsigned char)from
)
238 && ((unsigned char)i
<= (unsigned char)to
))
239 NTL_char_attrib
[(char)i
- CHAR_MIN
] |= macro
;
242 static void moveMacro(int from
, int to
)
245 for (i
= CHAR_MIN
; i
<= CHAR_MAX
; i
++)
246 if (NTL_char_attrib
[i
- CHAR_MIN
] & from
)
247 NTL_char_attrib
[i
- CHAR_MIN
] |= to
;
250 static void setLowHi(const char firstlow
, const char lastlow
,
254 for (i
= CHAR_MIN
; i
<= CHAR_MAX
; i
++)
255 if (((unsigned char)i
>= (unsigned char)firstlow
)
256 && ((unsigned char)i
<= (unsigned char)lastlow
))
258 j
= ((int)((char)(i
+ (int)(firsthi
- firstlow
))));
259 NTL_tolower_tab
[((char)j
) - CHAR_MIN
] = (char)i
;
260 NTL_toupper_tab
[((char)i
) - CHAR_MIN
] = (char)j
;
264 /* These are used in main() to actually dump the tables, each function
265 dumps half table as hex/char constants... */
269 static void dumphb(char *tbl
, int beg
)
272 char *p
= &tbl
[beg
- CHAR_MIN
];
274 for (i
= 0; i
<= SCHAR_MAX
; i
+= ROWSIZE
)
280 c
= (unsigned char)(beg
+ i
);
282 if ((c
> 0) && (c
< SCHAR_MAX
) && (isprint(c
)) && (c
!= '\\')
286 printf(" x%02x", ((int)c
));
288 c
= (unsigned char)(beg
+ k
);
290 if ((c
> 0) && (c
< SCHAR_MAX
) && (isprint(c
)) && (c
!= '\\')
294 printf("x%02x", ((int)c
));
297 for (j
= i
; j
<= k
; j
++)
300 if ((c
> 0) && (c
< SCHAR_MAX
) && (isprint(c
)) && (c
!= '\\')
304 printf(" '\\x%02x'", ((int)c
));
312 static void dumphw(int *tbl
, int beg
)
315 int *p
= &tbl
[beg
- CHAR_MIN
];
317 for (i
= 0; i
<= SCHAR_MAX
; i
+= ROWSIZE
)
323 c
= (unsigned char)(beg
+ i
);
325 if ((c
> 0) && (c
< SCHAR_MAX
) && (isprint(c
)) && (c
!= '\\')
329 printf(" x%02x", ((int)c
));
331 c
= (unsigned char)(beg
+ k
);
333 if ((c
> 0) && (c
< SCHAR_MAX
) && (isprint(c
)) && (c
!= '\\')
337 printf("x%02x", ((int)c
));
340 for (j
= i
; j
<= k
; j
++)
342 printf(" 0x%04x", p
[j
] & 0xffffffff);