5 char *gensym(const char *x);
6 static char *ds(const char *string);
7 static void add_ec(const char *name, const char *description);
8 static void add_ec_val(const char *name, const char *val,
9 const char *description);
10 static void put_ecs(void);
11 static int char_to_num(char c);
12 static void set_table_num(char *string);
14 static long gensym_n = 0;
16 char *current_token = NULL;
18 extern int yyerror(char *s);
19 extern char *table_name;
25 %token ERROR_TABLE ERROR_CODE_ENTRY END
26 %token <dynstr> STRING QUOTED_STRING
27 %type <dynstr> ec_name description table_id
33 error_table : ERROR_TABLE table_id error_codes END
34 { table_name = ds($2);
35 current_token = table_name;
45 error_codes : error_codes ec_entry
49 ec_entry : ERROR_CODE_ENTRY ec_name ',' description
53 | ERROR_CODE_ENTRY ec_name '=' STRING ',' description
54 { add_ec_val($2, $4, $6);
66 description : QUOTED_STRING
74 * Copyright 1986, 1987 by the MIT Student Information Processing Board
76 * For copyright info, see mit-sipb-copyright.h.
79 #include "mit-sipb-copyright.h"
84 #include <sys/types.h>
86 #include "error_table.h"
88 static const char rcsid[] = "$Id$";
90 #include "et_lex.lex.c"
92 extern FILE *hfile, *cfile;
94 char *gensym(const char *x)
101 gettimeofday(&tv, &tzp);
102 gensym_n = (tv.tv_sec%10000)*100 + tv.tv_usec/10000;
104 symbol = malloc(32 * sizeof(char));
106 sprintf(symbol, "et%ld", gensym_n);
110 static char *ds(const char *string)
114 rv = malloc(strlen(string)+1);
121 char **error_codes = NULL;
123 static void add_ec(const char *name, const char *description)
125 fprintf(cfile, "\t\"%s\",\n", description);
126 if (error_codes == NULL) {
127 error_codes = malloc(sizeof(char *));
130 error_codes = realloc(error_codes, (current + 2) * sizeof(char *));
131 error_codes[current++] = ds(name);
132 error_codes[current] = NULL;
135 static void add_ec_val(const char *name, const char *val,
136 const char *description)
138 const int ncurrent = atoi(val);
140 if (ncurrent < current) {
141 printf("Error code %s (%d) out of order", name,
146 while (ncurrent > current)
147 fputs("\tNULL,\n", cfile), current++;
149 fprintf(cfile, "\t\"%s\",\n", description);
150 if (error_codes == NULL) {
151 error_codes = malloc(sizeof(char *));
154 error_codes = realloc(error_codes, (current + 2) * sizeof(char *));
155 error_codes[current++] = ds(name);
156 error_codes[current] = NULL;
159 static void put_ecs(void)
162 for (i = 0; i < current; i++) {
163 if (error_codes[i] != NULL)
164 fprintf(hfile, "#define %-40s (%ldL)\n",
165 error_codes[i], table_number + i);
170 * char_to_num -- maps letters and numbers into a small numbering space
177 static const char char_set[] =
178 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
180 static int char_to_num(char c)
185 where = strchr (char_set, c);
187 diff = where - char_set + 1;
188 assert (diff < (1 << ERRCODE_RANGE));
191 else if (isprint (c))
193 "Illegal character `%c' in error table name\n",
197 "Illegal character %03o in error table name\n",
202 static void set_table_num(char *string)
204 if (char_to_num (string[0]) > char_to_num ('z')) {
205 fprintf (stderr, "%s%s%s%s",
206 "First character of error table name must be ",
208 string, "'' rejected\n");
211 if (strlen(string) > 4) {
212 fprintf(stderr, "Table name %s too long, truncated ",
215 fprintf(stderr, "to %s\n", string);
217 while (*string != '\0') {
218 table_number = (table_number << BITS_PER_CHAR)
219 + char_to_num(*string);
222 table_number = table_number << ERRCODE_RANGE;