]> andersk Git - moira.git/blame - util/et/error_table.y
sync'ing files for RCS->CVS migration
[moira.git] / util / et / error_table.y
CommitLineData
d1b2a10e 1%{
2#include <stdio.h>
3char *str_concat(), *ds(), *quote(), *malloc(), *realloc();
4char *current_token = (char *)NULL;
5extern char *table_name;
6%}
7%union {
8 char *dynstr;
9}
10
11%token ERROR_TABLE ERROR_CODE_ENTRY END
12%token <dynstr> STRING QUOTED_STRING
13%type <dynstr> ec_name description table_id
14%{
15%}
16%start error_table
17%%
18
19error_table : ERROR_TABLE table_id error_codes END
20 { table_name = ds($2);
21 current_token = table_name;
22 put_ecs(); }
23 ;
24
25table_id : STRING
26 { current_token = $1;
27 set_table_num($1);
28 $$ = $1; }
29 ;
30
31error_codes : error_codes ec_entry
32 | ec_entry
33 ;
34
35ec_entry : ERROR_CODE_ENTRY ec_name ',' description
36 { add_ec($2, $4);
37 free($2);
38 free($4); }
39 | ERROR_CODE_ENTRY ec_name '=' STRING ',' description
40 { add_ec_val($2, $4, $6);
41 free($2);
42 free($4);
43 free($6);
44 }
45 ;
46
47ec_name : STRING
48 { $$ = ds($1);
49 current_token = $$; }
50 ;
51
52description : QUOTED_STRING
53 { $$ = ds($1);
54 current_token = $$; }
55 ;
56
57%%
58/*
59 *
60 * Copyright 1986, 1987 by the MIT Student Information Processing Board
61 *
62 * For copyright info, see mit-sipb-copyright.h.
63 */
64
65#include <string.h>
66#include <assert.h>
67#include <ctype.h>
68#include <sys/types.h>
69#include <sys/time.h>
70#include "internal.h"
71#include "error_table.h"
72#include "mit-sipb-copyright.h"
73
74#ifndef lint
75static char const rcsid_error_table_y[] =
76 "$Header$";
77#endif
78
79char *malloc(), *realloc();
80extern FILE *hfile, *cfile;
81
82static long gensym_n = 0;
83char *
84gensym(x)
85 char const *x;
86{
87 char *symbol;
88 if (!gensym_n) {
89 struct timeval tv;
90 struct timezone tzp;
91 gettimeofday(&tv, &tzp);
92 gensym_n = (tv.tv_sec%10000)*100 + tv.tv_usec/10000;
93 }
94 symbol = malloc(32 * sizeof(char));
95 gensym_n++;
96 sprintf(symbol, "et%ld", gensym_n);
97 return(symbol);
98}
99
100char *
101ds(string)
102 char const *string;
103{
104 char *rv;
105 rv = malloc(strlen(string)+1);
106 strcpy(rv, string);
107 return(rv);
108}
109
110char *
111quote(string)
112 char const *string;
113{
114 char *rv;
115 rv = malloc(strlen(string)+3);
116 strcpy(rv, "\"");
117 strcat(rv, string);
118 strcat(rv, "\"");
119 return(rv);
120}
121
122long table_number;
123int current = 0;
124char **error_codes = (char **)NULL;
125
126add_ec(name, description)
127 char const *name, *description;
128{
129 fprintf(cfile, "\t\"%s\",\n", description);
130 if (error_codes == (char **)NULL) {
131 error_codes = (char **)malloc(sizeof(char *));
132 *error_codes = (char *)NULL;
133 }
134 error_codes = (char **)realloc((char *)error_codes,
135 (current + 2)*sizeof(char *));
136 error_codes[current++] = ds(name);
137 error_codes[current] = (char *)NULL;
138}
139
140add_ec_val(name, val, description)
141 char const *name, *val, *description;
142{
143 const int ncurrent = atoi(val);
144 if (ncurrent < current) {
145 printf("Error code %s (%d) out of order", name,
146 current);
147 return;
148 }
149
150 while (ncurrent > current)
151 fputs("\t(char *)NULL,\n", cfile), current++;
152
153 fprintf(cfile, "\t\"%s\",\n", description);
154 if (error_codes == (char **)NULL) {
155 error_codes = (char **)malloc(sizeof(char *));
156 *error_codes = (char *)NULL;
157 }
158 error_codes = (char **)realloc((char *)error_codes,
159 (current + 2)*sizeof(char *));
160 error_codes[current++] = ds(name);
161 error_codes[current] = (char *)NULL;
162}
163
164put_ecs()
165{
166 int i;
167 for (i = 0; i < current; i++) {
168 if (error_codes[i] != (char *)NULL)
169 fprintf(hfile, "#define %-40s (%ldL)\n",
170 error_codes[i], table_number + i);
171 }
172}
173
174/*
175 * char_to_num -- maps letters and numbers into a small numbering space
176 * uppercase -> 1-26
177 * lowercase -> 27-52
178 * digits -> 53-62
179 * underscore-> 63
180 */
181
182static const char char_set[] =
183 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
184
185int char_to_num(c)
186 char c;
187{
188 const char *where;
189 int diff;
190
191 where = strchr (char_set, c);
192 if (where) {
193 diff = where - char_set + 1;
194 assert (diff < (1 << ERRCODE_RANGE));
195 return diff;
196 }
197 else if (isprint (c))
198 fprintf (stderr,
199 "Illegal character `%c' in error table name\n",
200 c);
201 else
202 fprintf (stderr,
203 "Illegal character %03o in error table name\n",
204 c);
205 exit (1);
206}
207
208set_table_num(string)
209 char *string;
210{
211 if (char_to_num (string[0]) > char_to_num ('z')) {
212 fprintf (stderr, "%s%s%s%s",
213 "First character of error table name must be ",
214 "a letter; name ``",
215 string, "'' rejected\n");
216 exit (1);
217 }
218 if (strlen(string) > 4) {
219 fprintf(stderr, "Table name %s too long, truncated ",
220 string);
221 string[4] = '\0';
222 fprintf(stderr, "to %s\n", string);
223 }
224 while (*string != '\0') {
225 table_number = (table_number << BITS_PER_CHAR)
226 + char_to_num(*string);
227 string++;
228 }
229 table_number = table_number << ERRCODE_RANGE;
230}
231
232#include "et_lex.lex.c"
This page took 0.195993 seconds and 5 git commands to generate.