582da683922de70b2d58d10fe35c9ddaf575e639
[openafs.git] / src / comerr / compile_et.c
1 /*
2  *
3  * Copyright 1986, 1987, 1988
4  * by MIT Student Information Processing Board.
5  *
6  * For copyright info, see "mit-sipb-cr.h".
7  *
8  */
9
10 #undef MEMORYLEAK
11 #include <afs/param.h>
12 #include <stdio.h>
13
14 #ifndef AFS_NT40_ENV
15 #include <sys/file.h>
16 #include <sys/param.h>
17 #endif
18
19 #include <errno.h>
20 #include <string.h>
21 #ifdef  AFS_AIX32_ENV
22 #include <signal.h>
23 #endif
24 #include "mit-sipb-cr.h"
25 #include "compiler.h"
26
27 #ifndef lint
28 static const char copyright[] =
29     "Copyright 1987,1988 by MIT Student Information Processing Board";
30 #endif
31
32 extern char *gensym();
33 extern char *current_token;
34 extern int table_number, current;
35 char buffer[BUFSIZ];
36 char *table_name = (char *)NULL;
37 FILE *hfile, *cfile, *msfile;
38 int version = 1;
39 int use_msf = 0;
40
41 /* C library */
42 #ifndef AFS_NT40_ENV
43 extern char *malloc();
44 #endif
45
46
47 /* lex stuff */
48 extern FILE *yyin;
49 extern FILE *yyout;
50 extern int yylineno;
51
52 char * xmalloc (size) unsigned int size; {
53     char * p = malloc (size);
54     if (!p) {
55         perror (whoami);
56         exit (1);
57     }
58     return p;
59 }
60
61 static int check_arg (str_list, arg) char const *const *str_list, *arg; {
62     while (*str_list)
63         if (!strcmp(arg, *str_list++))
64             return 1;
65     return 0;
66 }
67
68 static const char *const debug_args[] = {
69     "d",
70     "debug",
71     0,
72 };
73
74 static const char *const lang_args[] = {
75     "lang",
76     "language",
77     0,
78 };
79
80 static const char *const language_names[] = {
81     "C",
82     "K&R C",
83     "C++",
84     0,
85 };
86
87 static const char * const c_src_prolog[] = {
88     "#include <afs/param.h>\n",
89     "#include <afs/error_table.h>\n",
90     "static const char * const text[] = {\n",
91     0,
92 };
93
94 static const char * const krc_src_prolog[] = {
95     "#ifdef __STDC__\n",
96     "#define NOARGS void\n",
97     "#else\n",
98     "#define NOARGS\n",
99     "#define const\n",
100     "#endif\n\n",
101     "#include <afs/param.h>\n",
102     "#include <afs/error_table.h>\n",
103     "static const char * const text[] = {\n",
104     0,
105 };
106
107 static const char warning[] =
108     "/*\n * %s:\n * This file is automatically generated; please do not edit it.\n */\n";
109
110 static const char msf_warning[] =
111     "$\n$ %s:\n$ This file is automatically generated; please do not edit it.\n$\n$set 1\n";
112
113 /* pathnames */
114 char c_file[MAXPATHLEN];        /* output file */
115 char h_file[MAXPATHLEN];        /* output */
116 char msf_file[MAXPATHLEN];
117
118 static void usage () {
119     fprintf (stderr, "%s: usage: %s ERROR_TABLE [-debug] [-language LANG] [-h INCLUDE] [-v version]\n",
120              whoami, whoami);
121     exit (1);
122 }
123
124 static void dup_err (type, one, two) char const *type, *one, *two; {
125     fprintf (stderr, "%s: multiple %s specified: `%s' and `%s'\n",
126              whoami, type, one, two);
127     usage ();
128 }
129
130 #include "AFS_component_version_number.c"
131
132 int main (argc, argv) int argc; char **argv; {
133     char *p, *ename;
134     char const * const *cpp;
135     int got_language = 0;
136     char *got_include = 0;
137
138 #ifdef  AFS_AIX32_ENV
139     /*
140      * The following signal action for AIX is necessary so that in case of a 
141      * crash (i.e. core is generated) we can include the user's data section 
142      * in the core dump. Unfortunately, by default, only a partial core is
143      * generated which, in many cases, isn't too useful.
144      */
145     struct sigaction nsa;
146     
147     sigemptyset(&nsa.sa_mask);
148     nsa.sa_handler = SIG_DFL;
149     nsa.sa_flags = SA_FULLDUMP;
150     sigaction(SIGSEGV, &nsa, NULL);
151 #endif
152     /* argument parsing */
153     debug = 0;
154     filename = 0;
155     whoami = argv[0];
156     p = strrchr (whoami, '/');
157     if (p)
158         whoami = p+1;
159     while (argv++, --argc) {
160         char *arg = *argv;
161         if (arg[0] != '-') {
162             if (filename)
163                 dup_err ("filenames", filename, arg);
164             filename = arg;
165         }
166         else {
167             arg++;
168             if (check_arg (debug_args, arg))
169                 debug++;
170             else if (check_arg (lang_args, arg)) {
171                 got_language++;
172                 arg = *++argv, argc--;
173                 if (!arg)
174                     usage ();
175                 if (language)
176                     dup_err ("languanges", language_names[(int)language], arg);
177 #define check_lang(x,v) else if (!strcasecmp(arg,x)) language = v
178                 check_lang ("c", lang_C);
179                 check_lang ("ansi_c", lang_C);
180                 check_lang ("ansi-c", lang_C);
181                 check_lang ("krc", lang_KRC);
182                 check_lang ("kr_c", lang_KRC);
183                 check_lang ("kr-c", lang_KRC);
184                 check_lang ("k&r-c", lang_KRC);
185                 check_lang ("k&r_c", lang_KRC);
186                 check_lang ("c++", lang_CPP);
187                 check_lang ("cplusplus", lang_CPP);
188                 check_lang ("c-plus-plus", lang_CPP);
189 #undef check_lang
190                 else {
191                     fprintf (stderr, "%s: unknown language name `%s'\n",
192                              whoami, arg);
193                     fprintf (stderr, "\tpick one of: C K&R-C\n");
194                     exit (1);
195                 }
196             }
197             else if (strcmp (arg, "h") == 0) {
198                 arg = *++argv, argc--;
199                 if (!arg) usage ();
200                 got_include = arg;
201             }
202             else if (strcmp (arg, "v") == 0) {
203                 arg = *++argv, argc--;
204                 version = atoi(arg);
205                 if (version != 1 && version != 2) {
206                     fprintf (stderr, "%s: unknown control argument -`%s'\n",
207                              whoami, arg);
208                     usage ();
209                     exit(1);
210                 }
211                 if (version == 2) use_msf = 1;
212             }
213             else {
214                 fprintf (stderr, "%s: unknown control argument -`%s'\n",
215                          whoami, arg);
216                 usage ();
217             }
218         }
219     }
220     if (!filename)
221         usage ();
222     if (!got_language)
223         language = lang_C;
224     else if (language == lang_CPP) {
225         fprintf (stderr, "%s: Sorry, C++ support is not yet finished.\n",
226                  whoami);
227         exit (1);
228     }
229
230
231     p = strrchr(filename, '/');
232     if (p == (char *)NULL)
233         p = filename;
234     else
235         p++;
236     ename = xmalloc (strlen(p) + 5);
237     strcpy (ename, p);
238
239     /* Now, flush .et suffix if it exists.   */
240     p = strrchr(ename, '.');
241     if (p != NULL) {
242         if (strcmp (p, ".et") == 0)
243             *p = 0;
244     }
245
246     if (use_msf) {
247         sprintf(msf_file, "%s.msf", ename);
248     }  else {
249         sprintf(c_file, "%s.c", ename);
250     }
251     if (got_include) {
252         sprintf(h_file, "%s.h", got_include);
253     } else {
254         sprintf(h_file, "%s.h", ename);
255     }
256     p = strrchr(filename, '.');
257     if (p == NULL)
258     {
259         p = xmalloc (strlen(filename) + 4);
260         sprintf(p, "%s.et", filename);
261         filename = p;
262     }
263
264     yyin = fopen(filename, "r");
265     if (!yyin) {
266         perror(filename);
267         exit(1);
268     }
269
270     /* on NT, yyout is not initialized to stdout */
271     if (!yyout) {
272         yyout = stdout;
273     }
274
275     hfile = fopen(h_file, "w");
276     if (hfile == (FILE *)NULL) {
277         perror(h_file);
278         exit(1);
279     }
280     fprintf (hfile, warning, h_file);
281     if (got_include) {
282         char  buffer[BUFSIZ];
283         char  prolog_h_file[MAXPATHLEN];
284         FILE *prolog_hfile;
285         int   count, written;
286
287         strcpy (prolog_h_file, got_include);
288         strcat (prolog_h_file, ".p.h");
289         prolog_hfile = fopen(prolog_h_file, "r");
290         if (prolog_hfile) {
291             fprintf (stderr, "Including %s at beginning of %s file.\n", prolog_h_file, h_file);
292             fprintf (hfile, "/* Including %s at beginning of %s file. */\n\n",
293                      prolog_h_file, h_file);
294             do {
295                 count = fread (buffer, sizeof(char), sizeof(buffer), prolog_hfile);
296                 if (count == EOF) {
297                     perror(prolog_h_file);
298                     exit(1);
299                 }
300                 written = fwrite (buffer, sizeof(char), count, hfile);
301                 if (count != written) {
302                     perror(prolog_h_file);
303                     exit(1);
304                 }
305             } while (count > 0);
306             fprintf (hfile, "\n/* End of prolog file %s. */\n\n", prolog_h_file);
307         }
308     }
309
310     if (use_msf) {
311         msfile = fopen(msf_file, "w");
312         if (msfile == (FILE *)NULL) {
313             perror(msf_file);
314             exit(1);
315         }
316         fprintf(msfile, msf_warning, msf_file);
317     } else {
318         cfile = fopen(c_file, "w");
319         if (cfile == (FILE *)NULL) {
320             perror(c_file);
321             exit(1);
322         }
323         fprintf (cfile, warning, c_file);
324
325         /* prologue */
326         if (language == lang_C)
327             cpp = c_src_prolog;
328         else if (language == lang_KRC)
329             cpp = krc_src_prolog;
330         else
331             abort ();
332         while (*cpp)
333             fputs (*cpp++, cfile);
334     }
335
336     /* parse it */
337     yyparse();
338     fclose(yyin);               /* bye bye input file */
339
340     if (!use_msf) {
341         fputs ("    0\n};\n\n", cfile);
342         fprintf(cfile,
343                 "static const struct error_table et = { text, %ldL, %d };\n\n",
344                 table_number, current);
345         fputs("static struct et_list etlink = { 0, &et};\n\n", cfile);
346         fprintf(cfile, "void initialize_%s_error_table (%s) {\n",
347                 table_name, (language == lang_C) ? "void" : "NOARGS");
348         fputs("    add_to_error_table(&etlink);\n", cfile);
349         fputs("}\n", cfile);
350         fclose(cfile);
351
352
353         fprintf (hfile, "extern void initialize_%s_error_table ();\n",
354                  table_name);
355     } else {
356         fprintf (hfile, "#define initialize_%s_error_table()\n",
357                  table_name);
358     }
359
360     fprintf (hfile, "#define ERROR_TABLE_BASE_%s (%ldL)\n",
361              table_name, table_number);
362     /* compatibility... */
363     fprintf (hfile, "\n/* for compatibility with older versions... */\n");
364     fprintf (hfile, "#define init_%s_err_tbl initialize_%s_error_table\n",
365              table_name, table_name);
366     fprintf (hfile, "#define %s_err_base ERROR_TABLE_BASE_%s\n", table_name,
367              table_name);
368     fclose(hfile);              /* bye bye include file */
369     if (use_msf)
370         fclose(msfile);
371     return 0;
372 }
373
374 void yyerror(const char *s) {
375     fputs(s, stderr);
376     fprintf(stderr, "\nLine number %d; last token was '%s'\n",
377             yylineno, current_token);
378 }