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 (wich 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 happes).
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, 0, (char) UCHAR_MAX);
102 unMarkString(NTL_IRCCH, "\007\040\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_ALPHA, NTL_IRCNK);
114 markString(NTL_IRCNK, "-_`");
116 moveMacro(NTL_ALNUM, NTL_IRCUI);
117 markRange(NTL_IRCUI, '\xe0', '\xf6');
118 markRange(NTL_IRCUI, '\xf8', '\xfe');
119 markRange(NTL_IRCUI, '\xc0', '\xd6');
120 markRange(NTL_IRCUI, '\xd8', '\xde');
121 markString(NTL_IRCUI, ".-_^'`~");
122 markString(NTL_EOL, "\n\r");
123 markString(NTL_CHPFX, "#&+");
124 markString(NTL_KTIME, " ,-0123456789");
126 /* And finally let's take care of the toLower/toUpper stuff */
128 setLowHi('a', 'z', 'A');
129 setLowHi('\xe0', '\xf6', '\xc0');
130 setLowHi('\xf8', '\xfe', '\xd8');
131 setLowHi('{', '~', '[');
136 * This is the main program to be executed for -DMAKETABLES
139 static void dumphw(int *p, int beg);
140 static void dumphb(char *p, int beg);
146 /* Make the tables */
149 /* Dump them as ANSI C source to be included below */
150 printf("/*\n * Automatically Generated Tables - DO NOT EDIT\n */\n");
151 printf("#include <limits.h>\n");
153 /* NTL_tolower_tab */
154 printf("const char ToLowerTab_8859_1[] = {\n");
155 printf("#if (CHAR_MIN<0)\n");
156 i = (int)((char)SCHAR_MIN);
157 dumphb(NTL_tolower_tab, i);
159 printf("#endif /* (CHAR_MIN<0) */\n");
161 dumphb(NTL_tolower_tab, i);
162 printf("#if (!(CHAR_MIN<0))\n");
164 i = (int)((char)SCHAR_MIN);
165 dumphb(NTL_tolower_tab, i);
166 printf("#endif /* (!(CHAR_MIN<0)) */\n");
169 /* NTL_toupper_tab */
170 printf("const char ToUpperTab_8859_1[] = {\n");
171 printf("#if (CHAR_MIN<0)\n");
172 i = (int)((char)SCHAR_MIN);
173 dumphb(NTL_toupper_tab, i);
175 printf("#endif /* (CHAR_MIN<0) */\n");
177 dumphb(NTL_toupper_tab, i);
178 printf("#if (!(CHAR_MIN<0))\n");
180 i = (int)((char)SCHAR_MIN);
181 dumphb(NTL_toupper_tab, i);
182 printf("#endif /* (!(CHAR_MIN<0)) */\n");
185 /* NTL_char_attrib */
186 printf("const unsigned int IRCD_CharAttrTab[] = {\n");
187 printf("#if (CHAR_MIN<0)\n");
188 i = (int)((char)SCHAR_MIN);
189 dumphw(NTL_char_attrib, i);
191 printf("#endif /* (CHAR_MIN<0) */\n");
193 dumphw(NTL_char_attrib, i);
194 printf("#if (!(CHAR_MIN<0))\n");
196 i = (int)((char)SCHAR_MIN);
197 dumphw(NTL_char_attrib, i);
198 printf("#endif /* (!(CHAR_MIN<0)) */\n");
205 /* A few utility functions for makeTables() */
207 static void zeroTables(void)
210 for (i = CHAR_MIN; i <= CHAR_MAX; i++)
212 NTL_tolower_tab[i - CHAR_MIN] = (char)i; /* Unchanged */
213 NTL_toupper_tab[i - CHAR_MIN] = (char)i; /* Unchanged */
214 NTL_char_attrib[i - CHAR_MIN] = 0x0000; /* Nothing */
218 static void markString(int macro, const char *s)
221 NTL_char_attrib[*(s++) - CHAR_MIN] |= macro;
224 static void unMarkString(int macro, const char *s)
227 NTL_char_attrib[*(s++) - CHAR_MIN] &= ~macro;
230 static void markRange(int macro, char from, char to)
233 for (i = CHAR_MIN; i <= CHAR_MAX; i++)
234 if (((unsigned char)i >= (unsigned char)from)
235 && ((unsigned char)i <= (unsigned char)to))
236 NTL_char_attrib[(char)i - CHAR_MIN] |= macro;
239 static void moveMacro(int from, int to)
242 for (i = CHAR_MIN; i <= CHAR_MAX; i++)
243 if (NTL_char_attrib[i - CHAR_MIN] & from)
244 NTL_char_attrib[i - CHAR_MIN] |= to;
247 static void setLowHi(const char firstlow, const char lastlow,
251 for (i = CHAR_MIN; i <= CHAR_MAX; i++)
252 if (((unsigned char)i >= (unsigned char)firstlow)
253 && ((unsigned char)i <= (unsigned char)lastlow))
255 j = ((int)((char)(i + (int)(firsthi - firstlow))));
256 NTL_tolower_tab[((char)j) - CHAR_MIN] = (char)i;
257 NTL_toupper_tab[((char)i) - CHAR_MIN] = (char)j;
261 /* These are used in main() to actually dump the tables, each function
262 dumps half table as hex/char constants... */
266 static void dumphb(char *tbl, int beg)
269 char *p = &tbl[beg - CHAR_MIN];
271 for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
279 if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
283 printf(" x%02x", ((int)((unsigned char)c)));
287 if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
291 printf("x%02x", ((int)((unsigned char)c)));
294 for (j = i; j <= k; j++)
297 if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
301 printf(" '\\x%02x'", ((int)((unsigned char)c)));
309 static void dumphw(int *tbl, int beg)
312 int *p = &tbl[beg - CHAR_MIN];
314 for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
322 if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
326 printf(" x%02x", ((int)((unsigned char)c)));
330 if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
334 printf("x%02x", ((int)((unsigned char)c)));
337 for (j = i; j <= k; j++)
339 printf(" 0x%04x", p[j] & 0xffffffff);