Make RXGEN_CPPCMD work on unix
[openafs.git] / src / rxgen / rpc_main.c
index d0e167a..ec97dc3 100644 (file)
@@ -69,7 +69,6 @@ struct commandline {
 };
 
 #define MAXCPPARGS     256     /* maximum number of arguments to cpp */
-#define MAXCMDLINE      1024   /* MAX chars on a single  cmd line */
 
 char *prefix = "";
 static char *IncludeDir[MAXCPPARGS];
@@ -85,34 +84,32 @@ char yflag = 0;                     /* if set, only emit function name arrays to xdr file */
 int debug = 0;
 static int pclose_fin = 0;
 static char *cmdname;
-#ifdef AFS_NT40_ENV
-static char *CPP = NULL;
-#else /* AFS_NT40_ENV */
 #ifdef PATH_CPP
-static char CPP[] = PATH_CPP;
+static char *CPP = PATH_CPP;
+#else
+#ifdef AFS_NT40_ENV
+static char *CPP = "cl /EP /C /nologo";
 #else
-static char CPP[] = "/lib/cpp";
+static char *CPP = "/lib/cpp";
+#endif
 #endif
-#endif /* AFS_NT40_ENV */
-static char CPPFLAGS[] = "-C";
 
-#ifdef AFS_ALPHA_ENV
 /*
  * Running "cpp" directly on DEC OSF/1 does not define anything; the "cc"
  * driver is responsible.  To compensate (and allow for other definitions
  * which should always be passed to "cpp"), place definitions which whould
- * always be passed to "rxgen" in this table.
+ * always be passed to "rxgen" in this string.
  */
-static char *XTRA_CPPFLAGS[] = {
+static char *CPPFLAGS = "-C"
+#ifdef AFS_ALPHA_ENV
 #ifdef __alpha
-    "-D__alpha",
+    " -D__alpha"
 #endif /* __alpha */
 #ifdef OSF
-    "-DOSF",
+    " -DOSF"
 #endif /* OSF */
-    NULL
-};
 #endif
+;
 
 #include "AFS_component_version_number.c"
 
@@ -139,14 +136,11 @@ int
 main(int argc, char *argv[])
 {
     struct commandline cmd;
+    char *ep;
 
-#ifdef AFS_NT40_ENV
-    /* initialize CPP with the correct pre-processor for Windows */
-    CPP = getenv("RXGEN_CPPCMD");
-    if (!CPP)
-       CPP = "cl /EP /C /nologo";
-#endif /* AFS_NT40_ENV */
-
+    ep = getenv("RXGEN_CPPCMD");
+    if (ep)
+       CPP = ep;
 #ifdef AFS_AIX32_ENV
     /*
      * The following signal action for AIX is necessary so that in case of a
@@ -282,32 +276,30 @@ open_output(char *infile, char *outfile)
 static void
 open_input(char *infile, char *define)
 {
-    char cpp_cmdline[MAXCMDLINE];
+    char *cpp_cmdline;
+    int i, l = 0;
 
-    int i;
     if (debug == 0) {
        infilename = (infile == NULL) ? "<stdin>" : infile;
-       strcpy(cpp_cmdline, CPP);
-       strcat(cpp_cmdline, " ");
-       strcat(cpp_cmdline, CPPFLAGS);
-       strcat(cpp_cmdline, " ");
-       strcat(cpp_cmdline, define);
-
-#ifdef AFS_ALPHA_ENV
-       for (i = 0;
-            i < (sizeof(XTRA_CPPFLAGS) / sizeof(XTRA_CPPFLAGS[0])) - 1;
-            i++) {
-           strcat(cpp_cmdline, " ");
-           strcat(cpp_cmdline, XTRA_CPPFLAGS[i]);
+        l = strlen(CPP) + strlen(CPPFLAGS) + strlen(define) + 3;
+       for (i = 0; i < nincludes; i++)
+           l += strlen(IncludeDir[i]) + 1;
+        l += strlen(infile) + 1;
+       cpp_cmdline = malloc(l);
+       if (!cpp_cmdline) {
+         perror("Unable to allocate space for cpp command line");
+         crash();
        }
-#endif
+
+       sprintf(cpp_cmdline, "%s %s %s", CPP, CPPFLAGS, define);
+       l = strlen(cpp_cmdline);
        for (i = 0; i < nincludes; i++) {
-           strcat(cpp_cmdline, " ");
-           strcat(cpp_cmdline, IncludeDir[i]);
+           cpp_cmdline[l++] = ' ';
+           strcpy(cpp_cmdline + l, IncludeDir[i]);
+            l += strlen(IncludeDir[i]);
        }
-
-       strcat(cpp_cmdline, " ");
-       strcat(cpp_cmdline, infile);
+       cpp_cmdline[l++] = ' ';
+       strcpy(cpp_cmdline + l, infile);
 
        fin = popen(cpp_cmdline, "r");
        if (fin == NULL)
@@ -664,6 +656,9 @@ C_output(char *infile, char *define, int extend, char *outfile, int append)
            f_print(fout, "#include <afs/param.h>\n");
            f_print(fout, "#include <roken.h>\n");
            f_print(fout, "#include <afs/opr.h>\n");
+           f_print(fout, "#ifdef AFS_PTHREAD_ENV\n");
+           f_print(fout, "# include <opr/lock.h>\n");
+           f_print(fout, "#endif\n");
            f_print(fout, "#include \"%s\"\n\n", include);
        }
        free(include);
@@ -880,6 +875,10 @@ parseargs(int argc, char *argv[], struct commandline *cmd)
                    prefix = &argv[i][j + 1];
                    goto nextarg;
                case 'I':
+                   if (nincludes >= MAXCPPARGS) {
+                       f_print(stderr, "Too many -I arguments\n");
+                       return (0);
+                   }
                    if (argv[i][j - 1] != '-')
                        return (0);
                    IncludeDir[nincludes++] = &argv[i][j - 1];