table_gen.cc
1 /*
2  * IRC - Internet Relay Chat, include/common.c
3  * Copyright (C) 1998 Andrea Cocito
4 *
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)
8  * any later version.
9 *
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.
14 *
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.
18 * /
19 
20 /*
21  * TABLE GENERATOR
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
24 *
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).
37 *
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.
42 *
43 * /
44 #include "config.h"
45 
46 #include "chattr.h"
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 
51 static void zeroTables(void);
52 static void markString(attr_t macro, char const* s);
53 static void unMarkString(attr_t macro, char const* s);
54 static void markRange(attr_t macro, char from, char to);
55 static void moveMacro(attr_t from, attr_t to);
56 static void setLowHi(char const firstlow, char const lastlow, char const firsthi);
57 
58 char NTL_tolower_tab[1 + CHAR_MAX - CHAR_MIN]; /* 256 bytes* /
59 char NTL_toupper_tab[1 + CHAR_MAX - CHAR_MIN]; /* 256 bytes* /
60 attr_t NTL_char_attrib[1 + CHAR_MAX - CHAR_MIN]; /* 256 attr_t = 512 bytes* /
61 
62 /*
63  * makeTables()
64  * Where we make the tables, edit ONLY this to change the tables.
65 * /
66 
67 static void makeTables(void)
68 {
69  zeroTables();
70 
71  markString(pgn_blank, "\011\013\014\040");
72 
73  markString(pgn_eol, "\r\n");
74 
75  markRange(pgn_file, 'a', 'h');
76 
77  markRange(pgn_rank, '1', '8');
78 
79  markString(pgn_piece, "RNBQK");
80 
81  markString(pgn_check, "+#");
82 
83  markString(pgn_punctuation_junk, ",;");
84 
85  markRange(pgn_digit, '0', '9');
86 
87  markString(pgn_alpha, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
88 
90  markString(pgn_tagname_continuation, "_");
91 
92  markString(pgn_tag_separator_junk, ":=");
93 
94  markRange(pgn_printable_string, -96, -1);
95  markRange(pgn_printable_string, 35, 91);
96  markRange(pgn_printable_string, 93, 126);
97  markString(pgn_printable_string, " !");
98 
99  moveMacro(pgn_eol, pgn_quote_or_eol);
100  markString(pgn_quote_or_eol, "\"");
101 
102  markString(pgn_comment_start, "{;");
103 
104  markRange(pgn_printable_comment, -96, -1);
105  markRange(pgn_printable_comment, 32, 124);
107  markString(pgn_printable_comment, "~");
108 
109  markRange(pgn_printable, -96, -1);
110  markRange(pgn_printable, 32, 126);
111 
112  /* And finally let's take care of the toLower/toUpper stuff* /
113 
114  setLowHi('a', 'z', 'A');
115  setLowHi('\xe0', '\xf6', '\xc0');
116  setLowHi('\xf8', '\xfe', '\xd8');
117 }
118 
119 /*
120  * main()
121  * This is the main program to be executed for -DMAKETABLES
122 * /
123 
124 static void dumphw(attr_t* p, int beg);
125 static void dumphb(char* p, int beg);
126 
127 int main(void)
128 {
129  int i;
130 
131  /* Make the tables* /
132  makeTables();
133 
134  /* Dump them as ANSI C source to be included below* /
135  printf("/*\n * Automatically Generated Tables - DO NOT EDIT\n* /\n");
136  printf("#include <limits.h>\n#include \"chattr.h\"\n");
137 
138  /* NTL_tolower_tab* /
139  printf("char const ToLowerTab_8859_1[] = {\n");
140  printf("#if (CHAR_MIN<0)\n");
141  i = (int)((char)SCHAR_MIN);
142  dumphb(NTL_tolower_tab, i);
143  printf(" ,\n");
144  printf("#endif /* (CHAR_MIN<0)* /\n");
145  i = 0;
146  dumphb(NTL_tolower_tab, i);
147  printf("#if (!(CHAR_MIN<0))\n");
148  printf(" ,\n");
149  i = (int)((char)SCHAR_MIN);
150  dumphb(NTL_tolower_tab, i);
151  printf("#endif /* (!(CHAR_MIN<0))* /\n");
152  printf(" };\n\n");
153 
154  /* NTL_toupper_tab* /
155  printf("char const ToUpperTab_8859_1[] = {\n");
156  printf("#if (CHAR_MIN<0)\n");
157  i = (int)((char)SCHAR_MIN);
158  dumphb(NTL_toupper_tab, i);
159  printf(" ,\n");
160  printf("#endif /* (CHAR_MIN<0)* /\n");
161  i = 0;
162  dumphb(NTL_toupper_tab, i);
163  printf("#if (!(CHAR_MIN<0))\n");
164  printf(" ,\n");
165  i = (int)((char)SCHAR_MIN);
166  dumphb(NTL_toupper_tab, i);
167  printf("#endif /* (!(CHAR_MIN<0))* /\n");
168  printf(" };\n\n");
169 
170  /* NTL_char_attrib* /
171  printf("attr_t const PGN_CharAttrTab[] = {\n");
172  printf("#if (CHAR_MIN<0)\n");
173  i = (int)((char)SCHAR_MIN);
174  dumphw(NTL_char_attrib, i);
175  printf(" ,\n");
176  printf("#endif /* (CHAR_MIN<0)* /\n");
177  i = 0;
178  dumphw(NTL_char_attrib, i);
179  printf("#if (!(CHAR_MIN<0))\n");
180  printf(" ,\n");
181  i = (int)((char)SCHAR_MIN);
182  dumphw(NTL_char_attrib, i);
183  printf("#endif /* (!(CHAR_MIN<0))* /\n");
184  printf(" };\n\n");
185 }
186 
187 /* A few utility functions for makeTables()* /
188 
189 static void zeroTables(void)
190 {
191  for (int i = CHAR_MIN; i <= CHAR_MAX; ++i)
192  {
193  NTL_tolower_tab[i - CHAR_MIN] = (char)i; /* Unchanged* /
194  NTL_toupper_tab[i - CHAR_MIN] = (char)i; /* Unchanged* /
195  NTL_char_attrib[i - CHAR_MIN] = 0x0000; /* Nothing* /
196  }
197 }
198 
199 static void markString(attr_t macro, char const* s)
200 {
201  while (*s)
202  NTL_char_attrib[*(s++) - CHAR_MIN] |= macro;
203 }
204 
205 static void unMarkString(attr_t macro, char const* s)
206 {
207  while (*s)
208  NTL_char_attrib[*(s++) - CHAR_MIN]& = ~macro;
209 }
210 
211 static void markRange(attr_t macro, char from, char to)
212 {
213  for (int i = CHAR_MIN; i <= CHAR_MAX; ++i)
214  if (((unsigned char)i >= (unsigned char)from)
215  && ((unsigned char)i <= (unsigned char)to))
216  NTL_char_attrib[(char)i - CHAR_MIN] |= macro;
217 }
218 
219 static void moveMacro(attr_t from, attr_t to)
220 {
221  for (int i = CHAR_MIN; i <= CHAR_MAX; ++i)
222  if (NTL_char_attrib[i - CHAR_MIN] & from)
223  NTL_char_attrib[i - CHAR_MIN] |= to;
224 }
225 
226 static void setLowHi(char const firstlow, char const lastlow, char const firsthi)
227 {
228  for (int i = CHAR_MIN; i <= CHAR_MAX; ++i)
229  if (((unsigned char)i >= (unsigned char)firstlow)
230  && ((unsigned char)i <= (unsigned char)lastlow))
231  {
232  int j = ((int)((char)(i + (int)(firsthi - firstlow))));
233  NTL_tolower_tab[((char)j) - CHAR_MIN] = (char)i;
234  NTL_toupper_tab[((char)i) - CHAR_MIN] = (char)j;
235  }
236 }
237 
238 /* These are used in main() to actually dump the tables, each function
239  dumps half table as hex/char constants...* /
240 
241 #define ROWSIZE 8
242 
243 static void dumphb(char* tbl, int beg)
244 {
245  int i, j, k;
246  char* p =& tbl[beg - CHAR_MIN];
247  unsigned char c;
248  for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
249  {
250  k = i + ROWSIZE - 1;
251  if (k > SCHAR_MAX)
252  k = SCHAR_MAX;
253 
254  c = (unsigned char)(beg + i);
255  printf("/*");
256  if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
257  && (c != '\''))
258  printf(" '%c'", c);
259  else
260  printf(" x%02x", ((int)c));
261 
262  c = (unsigned char)(beg + k);
263  printf("-");
264  if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
265  && (c != '\''))
266  printf("'%c'", c);
267  else
268  printf("x%02x", ((int)c));
269  printf("* /");
270 
271  for (j = i; j <= k; j++)
272  {
273  c = p[j];
274  if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
275  && (c != '\''))
276  printf(" '%c'", c);
277  else
278  printf(" '\\x%02x'", ((int)c));
279  if (j < SCHAR_MAX)
280  printf(",");
281  }
282  printf("\n");
283  }
284 }
285 
286 static void dumphw(attr_t* tbl, int beg)
287 {
288  int i, j, k;
289  attr_t* p =& tbl[beg - CHAR_MIN];
290  unsigned char c;
291  for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
292  {
293  k = i + ROWSIZE - 1;
294  if (k > SCHAR_MAX)
295  k = SCHAR_MAX;
296 
297  c = (unsigned char)(beg + i);
298  printf("/*");
299  if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
300  && (c != '\''))
301  printf(" '%c'", c);
302  else
303  printf(" x%02x", ((int)c));
304 
305  c = (unsigned char)(beg + k);
306  printf("-");
307  if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
308  && (c != '\''))
309  printf("'%c'", c);
310  else
311  printf("x%02x", ((int)c));
312  printf("* /");
313 
314  for (j = i; j <= k; j++)
315  {
316  printf(" 0x%04x", p[j] & 0xffffffff);
317  if (j < SCHAR_MAX)
318  printf(",");
319  }
320  printf("\n");
321  }
322 }
323 
attr_t const pgn_digit
0123456789
Definition: chattr.h:46
attr_t const pgn_file
abcdefgh
Definition: chattr.h:41
attr_t const pgn_eol
&#39;\r&#39; |& #39;\n&#39;
Definition: chattr.h:39
attr_t const pgn_tag_separator_junk
:=
Definition: chattr.h:51
attr_t const pgn_piece
RNBQK.
Definition: chattr.h:43
attr_t const pgn_tagname_continuation
(pgn_alnum |& #39;_&#39;)
Definition: chattr.h:50
Character attribute definitions and arrays.
attr_t const pgn_alnum
(pgn_alpha | pgn_digit)
Definition: chattr.h:48
attr_t const pgn_comment_start
{;
Definition: chattr.h:54
attr_t const pgn_alpha
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Definition: chattr.h:47
attr_t const pgn_white_space
(pgn_blank | pgn_eol)
Definition: chattr.h:40
attr_t const pgn_quote_or_eol
(ntl_eol |& #39;"&#39;)
Definition: chattr.h:53
attr_t const pgn_punctuation_junk
,;
Definition: chattr.h:45
attr_t const pgn_printable_string
ascii range(-96, -1) | ascii range(35, 91) |& #39;& #39; | ascii range(93, 126) |& #39;!&#39;
Definition: chattr.h:52
attr_t const pgn_check
+#
Definition: chattr.h:44
attr_t const pgn_printable
ascii range(-96, -1) | ascii range(32, 126)
Definition: chattr.h:56
attr_t const pgn_blank
&#39;& #39; |& #39;\t&#39; |& #39;\v&#39; |& #39;\f&#39;. Note that& #39;\f&#39; is normally not legal in a PGN.
Definition: chattr.h:38
attr_t const pgn_rank
12345678
Definition: chattr.h:42
attr_t const pgn_printable_comment
ascii range(-96, -1) | ascii range(32, 124) |& #39;~&#39; | pgn_blank | pgn_eol
Definition: chattr.h:55

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.