Imported Upstream version 20120526
[platform/upstream/byacc.git] / main.c
diff --git a/main.c b/main.c
index 1ee55da..bcd7d50 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.35 2010/12/27 01:21:59 tom Exp $ */
+/* $Id: main.c,v 1.39 2012/05/26 00:50:20 tom Exp $ */
 
 #include <signal.h>
 #include <unistd.h>            /* for _exit() */
@@ -34,9 +34,11 @@ static MY_TMPFILES *my_tmpfiles;
 
 char dflag;
 char gflag;
+char iflag;
 char lflag;
 static char oflag;
 char rflag;
+char sflag;
 char tflag;
 char vflag;
 
@@ -53,7 +55,9 @@ static char *file_prefix = default_file_prefix;
 
 char *code_file_name;
 char *input_file_name = empty_string;
-static char *defines_file_name;
+char *defines_file_name;
+char *externs_file_name;
+
 static char *graph_file_name;
 static char *output_file_name;
 static char *verbose_file_name;
@@ -62,6 +66,7 @@ FILE *action_file;    /*  a temp file, used to save actions associated    */
                        /*  with rules until the parser is written          */
 FILE *code_file;       /*  y.code.c (used when the -r option is specified) */
 FILE *defines_file;    /*  y.tab.h                                         */
+FILE *externs_file;    /*  y.tab.i                                         */
 FILE *input_file;      /*  the input file                                  */
 FILE *output_file;     /*  y.tab.c                                         */
 FILE *text_file;       /*  a temp file, used to save text until all        */
@@ -132,6 +137,9 @@ done(int k)
     if (dflag)
        DO_FREE(defines_file_name);
 
+    if (iflag)
+       DO_FREE(externs_file_name);
+
     if (oflag)
        DO_FREE(output_file_name);
 
@@ -187,12 +195,14 @@ usage(void)
        ,"Options:"
        ,"  -b file_prefix        set filename prefix (default \"y.\")"
        ,"  -d                    write definitions (y.tab.h)"
+       ,"  -i                    write interface (y.tab.i)"
        ,"  -g                    write a graphical description"
        ,"  -l                    suppress #line directives"
        ,"  -o output_file        (default \"y.tab.c\")"
        ,"  -p symbol_prefix      set symbol prefix (default \"yy\")"
        ,"  -P                    create a reentrant parser, e.g., \"%pure-parser\""
        ,"  -r                    produce separate code and table files (y.code.c)"
+       ,"  -s                    suppress #define's for quoted names in %token lines"
        ,"  -t                    add debugging support"
        ,"  -v                    write description (y.output)"
        ,"  -V                    show version information and exit"
@@ -220,6 +230,10 @@ setflag(int ch)
        gflag = 1;
        break;
 
+    case 'i':
+       iflag = 1;
+       break;
+
     case 'l':
        lflag = 1;
        break;
@@ -232,6 +246,10 @@ setflag(int ch)
        rflag = 1;
        break;
 
+    case 's':
+       sflag = 1;
+       break;
+
     case 't':
        tflag = 1;
        break;
@@ -349,7 +367,7 @@ allocate(size_t n)
 }
 
 #define CREATE_FILE_NAME(dest, suffix) \
-       dest = MALLOC(len + strlen(suffix) + 1); \
+       dest = TMALLOC(char, len + strlen(suffix) + 1); \
        NO_SPACE(dest); \
        strcpy(dest, file_prefix); \
        strcpy(dest + len, suffix)
@@ -359,23 +377,28 @@ create_file_names(void)
 {
     size_t len;
     const char *defines_suffix;
+    const char *externs_suffix;
     char *prefix;
 
     prefix = NULL;
     defines_suffix = DEFINES_SUFFIX;
+    externs_suffix = EXTERNS_SUFFIX;
 
     /* compute the file_prefix from the user provided output_file_name */
     if (output_file_name != 0)
     {
        if (!(prefix = strstr(output_file_name, ".tab.c"))
            && (prefix = strstr(output_file_name, ".c")))
+       {
            defines_suffix = ".h";
+           externs_suffix = ".i";
+       }
     }
 
     if (prefix != NULL)
     {
        len = (size_t) (prefix - output_file_name);
-       file_prefix = (char *)MALLOC(len + 1);
+       file_prefix = TMALLOC(char, len + 1);
        NO_SPACE(file_prefix);
        strncpy(file_prefix, output_file_name, len)[len] = 0;
     }
@@ -401,6 +424,11 @@ create_file_names(void)
        CREATE_FILE_NAME(defines_file_name, defines_suffix);
     }
 
+    if (iflag)
+    {
+       CREATE_FILE_NAME(externs_file_name, externs_suffix);
+    }
+
     if (vflag)
     {
        CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
@@ -592,6 +620,13 @@ open_files(void)
        union_file = open_tmpfile("union_file");
     }
 
+    if (iflag)
+    {
+       externs_file = fopen(externs_file_name, "w");
+       if (externs_file == 0)
+           open_error(externs_file_name);
+    }
+
     output_file = fopen(output_file_name, "w");
     if (output_file == 0)
        open_error(output_file_name);