3 * Copyright 1986, 1987, 1988
4 * by MIT Student Information Processing Board.
6 * For copyright info, see "mit-sipb-copyright.h".
11 #include <sys/types.h>
14 #include <sys/param.h>
15 #include "mit-sipb-copyright.h"
23 static const char copyright[] =
24 "Copyright 1987,1988 by MIT Student Information Processing Board";
26 static const char rcsid_compile_et_c[] =
30 extern char *gensym();
31 extern char *current_token;
32 extern int table_number, current;
34 char *table_name = (char *)NULL;
38 extern char *malloc();
45 char * xmalloc (size) unsigned int size; {
46 char * p = malloc (size);
54 static int check_arg (str_list, arg) char const *const *str_list, *arg; {
56 if (!strcmp(arg, *str_list++))
61 static const char *const debug_args[] = {
67 static const char *const lang_args[] = {
73 static const char *const language_names[] = {
80 static const char * const c_src_prolog[] = {
81 "static const char * const text[] = {\n",
85 static const char * const krc_src_prolog[] = {
87 "#define NOARGS void\n",
92 "static const char * const text[] = {\n",
96 static const char *const struct_def[] = {
97 "struct error_table {\n",
98 " char const * const * msgs;\n",
102 "struct et_list {\n",
103 " struct et_list *next;\n",
104 " const struct error_table * table;\n",
106 "extern struct et_list *_et_list;\n",
110 static const char warning[] =
111 "/*\n * %s:\n * This file is automatically generated; please do not edit it.\n */\n";
114 char c_file[MAXPATHLEN]; /* output file */
115 char h_file[MAXPATHLEN]; /* output */
117 static void usage () {
118 fprintf (stderr, "%s: usage: %s ERROR_TABLE\n",
123 static void dup_err (type, one, two) char const *type, *one, *two; {
124 fprintf (stderr, "%s: multiple %s specified: `%s' and `%s'\n",
125 whoami, type, one, two);
129 int main (argc, argv) int argc; char **argv; {
132 char const * const *cpp;
133 int got_language = 0;
135 /* argument parsing */
139 p = strrchr (whoami, '/');
142 while (argv++, --argc) {
146 dup_err ("filenames", filename, arg);
151 if (check_arg (debug_args, arg))
153 else if (check_arg (lang_args, arg)) {
155 arg = *++argv, argc--;
159 dup_err ("languanges", language_names[(int)language], arg);
160 #define check_lang(x,y,v) else if (!strcmp(arg,x)||!strcmp(arg,y)) language = v
161 check_lang ("c", "C", lang_C);
162 check_lang ("ansi_c", "ANSI_C", lang_C);
163 check_lang ("ansi-c", "ANSI-C", lang_C);
164 check_lang ("krc", "KRC", lang_KRC);
165 check_lang ("kr_c", "KR_C", lang_KRC);
166 check_lang ("kr-c", "KR-C", lang_KRC);
167 check_lang ("k&r-c", "K&R-C", lang_KRC);
168 check_lang ("k&r_c", "K&R_C", lang_KRC);
169 check_lang ("c++", "C++", lang_CPP);
170 check_lang ("cplusplus", "CPLUSPLUS", lang_CPP);
171 check_lang ("c-plus-plus", "C-PLUS-PLUS", lang_CPP);
174 fprintf (stderr, "%s: unknown language name `%s'\n",
176 fprintf (stderr, "\tpick one of: C K&R-C\n");
181 fprintf (stderr, "%s: unknown control argument -`%s'\n",
191 else if (language == lang_CPP) {
192 fprintf (stderr, "%s: Sorry, C++ support is not yet finished.\n",
197 p = xmalloc (strlen (filename) + 5);
198 strcpy (p, filename);
200 p = strrchr(filename, '/');
201 if (p == (char *)NULL)
206 len = strlen (ename);
208 if (strcmp (p, ".et"))
211 /* now p points to where "et" suffix should start */
212 /* generate new filenames */
214 strcpy (c_file, ename);
216 strcpy (h_file, ename);
219 yyin = fopen(filename, "r");
225 hfile = fopen(h_file, "w");
226 if (hfile == (FILE *)NULL) {
230 fprintf (hfile, warning, h_file);
232 cfile = fopen(c_file, "w");
233 if (cfile == (FILE *)NULL) {
237 fprintf (cfile, warning, c_file);
240 if (language == lang_C)
242 else if (language == lang_KRC)
243 cpp = krc_src_prolog;
247 fputs (*cpp++, cfile);
251 fclose(yyin); /* bye bye input file */
253 fputs (" 0\n};\n\n", cfile);
254 for (cpp = struct_def; *cpp; cpp++)
257 "static const struct error_table et = { text, %ldL, %d };\n\n",
258 table_number, current);
259 fputs("static struct et_list link = { 0, 0 };\n\n",
261 fprintf(cfile, "void initialize_%s_error_table (%s) {\n",
262 table_name, (language == lang_C) ? "void" : "NOARGS");
263 fputs(" if (!link.table) {\n", cfile);
264 fputs(" link.next = _et_list;\n", cfile);
265 fputs(" link.table = &et;\n", cfile);
266 fputs(" _et_list = &link;\n", cfile);
267 fputs(" }\n", cfile);
271 fprintf (hfile, "extern void initialize_%s_error_table ();\n",
273 fprintf (hfile, "#define ERROR_TABLE_BASE_%s (%ldL)\n",
274 table_name, table_number);
275 /* compatibility... */
276 fprintf (hfile, "\n/* for compatibility with older versions... */\n");
277 fprintf (hfile, "#define init_%s_err_tbl initialize_%s_error_table\n",
278 table_name, table_name);
279 fprintf (hfile, "#define %s_err_base ERROR_TABLE_BASE_%s\n", table_name,
281 fclose(hfile); /* bye bye include file */
286 int yyerror(s) char *s; {
288 fprintf(stderr, "\nLine number %d; last token was '%s'\n",
289 yylineno, current_token);