Make compile_et output usable out-of-tree
[openafs.git] / src / comerr / compile_et.c
index c73433f..87e000e 100644 (file)
 #include "internal.h"
 #include "compiler.h"
 
-#ifndef lint
-static const char copyright[] =
-    "Copyright 1987,1988 by MIT Student Information Processing Board";
-#endif
-
 extern char *current_token;
 extern int table_number, current;
 char buffer[BUFSIZ];
 char *table_name = NULL;
-FILE *hfile, *cfile, *msfile;
+FILE *hfile = NULL, *cfile = NULL, *msfile = NULL;
 int version = 1;
 int use_msf = 0;
+int emit_source = 0;
+int emit_header = 0;
 
 /* lex stuff */
 extern FILE *yyin;
@@ -72,6 +69,18 @@ static const char *const lang_args[] = {
     0,
 };
 
+static const char *const prefix_args[] = {
+    "p",
+    "prefix",
+    0,
+};
+
+static const char *const emit_args[] = {
+    "e",
+    "emit",
+    0,
+};
+
 static const char *const language_names[] = {
     "C",
     "K&R C",
@@ -80,8 +89,6 @@ static const char *const language_names[] = {
 };
 
 static const char *const c_src_prolog[] = {
-    "#include <afsconfig.h>\n",
-    "#include <afs/param.h>\n",
     "#include <afs/error_table.h>\n",
     "static const char * const text[] = {\n",
     0,
@@ -116,7 +123,7 @@ static void
 usage(void)
 {
     fprintf(stderr,
-           "%s: usage: %s ERROR_TABLE [-debug] [-language LANG] [-h INCLUDE] [-p prefix] [-v version]\n",
+           "%s: usage: %s ERROR_TABLE [-debug] [-language LANG] [-h INCLUDE] [-p PREFIX] [-v VERSION] [-emit source|header]\n",
            whoami, whoami);
     exit(1);
 }
@@ -139,6 +146,7 @@ main(int argc, char **argv)
     int got_language = 0;
     char *got_include = 0;
     char *got_prefix = ".";
+    int got_emit = 0;
     char lcname[6];
 
 #ifdef AFS_AIX32_ENV
@@ -162,8 +170,8 @@ main(int argc, char **argv)
     p = strrchr(whoami, '/');
     if (p)
        whoami = p + 1;
-    while (argv++, --argc) {
-       char *arg = *argv;
+    while (--argc) {
+       char *arg = *++argv;
        if (arg[0] != '-') {
            if (filename)
                dup_err("filenames", filename, arg);
@@ -174,7 +182,8 @@ main(int argc, char **argv)
                debug++;
            else if (check_arg(lang_args, arg)) {
                got_language++;
-               arg = *++argv, argc--;
+               arg = *++argv;
+               argc--;
                if (!arg)
                    usage();
                if (language)
@@ -201,17 +210,20 @@ main(int argc, char **argv)
                    exit(1);
                }
            } else if (strcmp(arg, "h") == 0) {
-               arg = *++argv, argc--;
+               arg = *++argv;
+               argc--;
                if (!arg)
                    usage();
                got_include = arg;
-           } else if (strcmp(arg, "p") == 0) {
-               arg = *++argv, argc--;
+           } else if (check_arg(prefix_args, arg)) {
+               arg = *++argv;
+               argc--;
                if (!arg)
                    usage();
                got_prefix = arg;
            } else if (strcmp(arg, "v") == 0) {
-               arg = *++argv, argc--;
+               arg = *++argv;
+               argc--;
                if (arg == NULL) {
                    fprintf(stderr, "%s: -v option requires an argument\n",
                            whoami);
@@ -227,6 +239,24 @@ main(int argc, char **argv)
                }
                if (version == 2)
                    use_msf = 1;
+           } else if (check_arg(emit_args, arg)) {
+               arg = *++argv;
+               argc--;
+               got_emit = 1;
+               if (!strcasecmp(arg, "source")) {
+                   emit_source = 1;
+               } else if (!strcasecmp(arg, "c")) {
+                   emit_source = 1;
+               } else if (!strcasecmp(arg, "header")) {
+                   emit_header = 1;
+               } else if (!strcasecmp(arg, "h")) {
+                   emit_header = 1;
+               } else {
+                   fprintf(stderr, "%s: unknown emit argument - `%s'\n",
+                           whoami, arg);
+                   usage();
+                   exit(1);
+               }
            } else {
                fprintf(stderr, "%s: unknown control argument -`%s'\n",
                        whoami, arg);
@@ -244,6 +274,9 @@ main(int argc, char **argv)
        exit(1);
     }
 
+    if (!got_emit) {
+       emit_source = emit_header = 1; /* generate both by default */
+    }
 
     p = strrchr(filename, '/');
     if (p == NULL)
@@ -260,15 +293,19 @@ main(int argc, char **argv)
            *p = 0;
     }
 
-    if (use_msf) {
-       sprintf(msf_file, "%s.msf", ename);
-    } else {
-       sprintf(c_file, "%s.c", ename);
+    if (emit_source) {
+       if (use_msf) {
+           sprintf(msf_file, "%s.msf", ename);
+       } else {
+           sprintf(c_file, "%s.c", ename);
+       }
     }
-    if (got_include) {
-       sprintf(h_file, "%s.h", got_include);
-    } else {
-       sprintf(h_file, "%s.h", ename);
+    if (emit_header) {
+       if (got_include) {
+           sprintf(h_file, "%s.h", got_include);
+       } else {
+           sprintf(h_file, "%s.h", ename);
+       }
     }
     p = strrchr(filename, '.');
     if (p == NULL) {
@@ -290,13 +327,15 @@ main(int argc, char **argv)
        yyout = stdout;
     }
 
-    hfile = fopen(h_file, "w");
-    if (hfile == NULL) {
-       perror(h_file);
-       exit(1);
+    if (emit_header) {
+       hfile = fopen(h_file, "w");
+       if (hfile == NULL) {
+           perror(h_file);
+           exit(1);
+       }
+       fprintf(hfile, warning, h_file);
     }
-    fprintf(hfile, warning, h_file);
-    if (got_include) {
+    if (emit_header && got_include) {
        char buffer[BUFSIZ];
        char prolog_h_file[MAXPATHLEN];
        FILE *prolog_hfile;
@@ -330,14 +369,14 @@ main(int argc, char **argv)
        }
     }
 
-    if (use_msf) {
+    if (emit_source && use_msf) {
        msfile = fopen(msf_file, "w");
        if (msfile == NULL) {
            perror(msf_file);
            exit(1);
        }
        fprintf(msfile, msf_warning, msf_file);
-    } else {
+    } else if (emit_source) {
        cfile = fopen(c_file, "w");
        if (cfile == NULL) {
            perror(c_file);
@@ -361,46 +400,52 @@ main(int argc, char **argv)
     fclose(yyin);              /* bye bye input file */
 
     if (!use_msf) {
-       fputs("    0\n};\n\n", cfile);
-       fprintf(cfile,
-               "static const struct error_table et = { text, %ldL, %d };\n\n",
-               (long int)table_number, current);
-       fputs("static struct et_list etlink = { 0, &et};\n\n", cfile);
-       fprintf(cfile, "void initialize_%s_error_table(void) {\n",
-               table_name);
-       fputs("    afs_add_to_error_table(&etlink);\n", cfile);
-       fputs("}\n", cfile);
-       fclose(cfile);
-
-
-       fprintf(hfile, "extern void initialize_%s_error_table(void);\n",
-               table_name);
+       if (cfile) {
+           fputs("    0\n};\n\n", cfile);
+           fprintf(cfile,
+                   "static const struct error_table et = { text, %ldL, %d };\n\n",
+                   (long int)table_number, current);
+           fputs("static struct et_list etlink = { 0, &et};\n\n", cfile);
+           fprintf(cfile, "void initialize_%s_error_table(void) {\n",
+                   table_name);
+           fputs("    afs_add_to_error_table(&etlink);\n", cfile);
+           fputs("}\n", cfile);
+           fclose(cfile);
+       }
+       if (hfile) {
+           fprintf(hfile, "extern void initialize_%s_error_table(void);\n",
+                   table_name);
+       }
     } else {
-       fprintf(hfile, "#define initialize_%s_error_table(void)\n",
-               table_name);
+       if (hfile) {
+           fprintf(hfile, "#define initialize_%s_error_table(void)\n",
+                   table_name);
+       }
     }
 
-    fprintf(hfile, "#define ERROR_TABLE_BASE_%s (%ldL)\n", table_name,
-           (long int)table_number);
-    /* compatibility... */
-    fprintf(hfile, "\n/* for compatibility with older versions... */\n");
-    fprintf(hfile, "#define init_%s_err_tbl initialize_%s_error_table\n",
-           table_name, table_name);
-    fprintf(hfile, "#define %s_err_base ERROR_TABLE_BASE_%s\n", table_name,
-           table_name);
-    fprintf(hfile, "\n/* for compatibility with other users... */\n");
-    lcstring(lcname, table_name, sizeof(lcname));
-    fprintf(hfile, "#define ERROR_TABLE_BASE_%s (%ldL)\n", lcname,
-           (long int)table_number);
-    fprintf(hfile, "#define init_%s_err_tbl initialize_%s_error_table\n",
-           lcname, table_name);
-    fprintf(hfile,
-           "#define initialize_%s_error_table initialize_%s_error_table\n",
-           lcname, table_name);
-    fprintf(hfile, "#define %s_err_base ERROR_TABLE_BASE_%s\n", lcname,
-           lcname);
-    fclose(hfile);             /* bye bye include file */
-    if (use_msf)
+    if (hfile) {
+       fprintf(hfile, "#define ERROR_TABLE_BASE_%s (%ldL)\n", table_name,
+               (long int)table_number);
+       /* compatibility... */
+       fprintf(hfile, "\n/* for compatibility with older versions... */\n");
+       fprintf(hfile, "#define init_%s_err_tbl initialize_%s_error_table\n",
+               table_name, table_name);
+       fprintf(hfile, "#define %s_err_base ERROR_TABLE_BASE_%s\n",
+               table_name, table_name);
+       fprintf(hfile, "\n/* for compatibility with other users... */\n");
+       lcstring(lcname, table_name, sizeof(lcname));
+       fprintf(hfile, "#define ERROR_TABLE_BASE_%s (%ldL)\n", lcname,
+               (long int)table_number);
+       fprintf(hfile, "#define init_%s_err_tbl initialize_%s_error_table\n",
+               lcname, table_name);
+       fprintf(hfile,
+               "#define initialize_%s_error_table initialize_%s_error_table\n",
+               lcname, table_name);
+       fprintf(hfile, "#define %s_err_base ERROR_TABLE_BASE_%s\n", lcname,
+               lcname);
+       fclose(hfile);          /* bye bye include file */
+    }
+    if (msfile)
        fclose(msfile);
     return 0;
 }