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