]> git.saurik.com Git - bison.git/blobdiff - src/files.c
* tests/Makefile.am ($(srcdir)/$(TESTSUITE)): No longer depend upon package.m4.
[bison.git] / src / files.c
index 033d2816237fc08ff9513436825f98699dfeb273..f9cb6f3312dac431f8b49baf19fdb8224f4bb902 100644 (file)
@@ -39,6 +39,8 @@ struct obstack graph_obstack;
 char *spec_outfile = NULL;     /* for -o. */
 char *spec_file_prefix = NULL; /* for -b. */
 char *spec_name_prefix = NULL; /* for -p. */
 char *spec_outfile = NULL;     /* for -o. */
 char *spec_file_prefix = NULL; /* for -b. */
 char *spec_name_prefix = NULL; /* for -p. */
+char *spec_graph_file = NULL;   /* for -g. */
+char *spec_defines_file = NULL; /* for --defines. */
 
 char *infile = NULL;
 char *attrsfile = NULL;
 
 char *infile = NULL;
 char *attrsfile = NULL;
@@ -84,6 +86,37 @@ stringappend (const char *string1, const char *string2)
   return res;
 }
 
   return res;
 }
 
+
+/*---------------------------------------------------------------.
+|  Computes the macro name used to avoid double inclusion in the |
+|  header of the parser and store it in header_macro_name.       |
+`---------------------------------------------------------------*/
+
+static char *
+compute_header_macro (void)
+{
+  int ite;
+  char *macro_name;
+
+  macro_name = XMALLOC (char,
+                       strlen (base_name) +
+                       strlen (header_extension) + 1);
+
+  stpcpy (macro_name, base_name);
+  strcat (macro_name, header_extension);
+
+  for (ite = 0; macro_name[ite]; ite++)
+    if (macro_name[ite] == '.')
+      macro_name[ite] = '_';
+    else
+      {
+       if (islower (macro_name[ite]))
+         macro_name[ite] -= ('a' - 'A');
+      }
+  return macro_name;
+}
+
+
 /*-----------------------------------------------------------------.
 | Try to open file NAME with mode MODE, and print an error message |
 | if fails.                                                        |
 /*-----------------------------------------------------------------.
 | Try to open file NAME with mode MODE, and print an error message |
 | if fails.                                                        |
@@ -133,6 +166,25 @@ obstack_save (struct obstack *obs, const char *filename)
   xfclose (out);
 }
 
   xfclose (out);
 }
 
+/*---------------------------------------------------------------------.
+| Output double inclusion protection macros and saves defines_obstack  |
+`---------------------------------------------------------------------*/
+
+static void
+defines_obstack_save (const char *filename)
+{
+  FILE *out = xfopen (filename, "w");
+  size_t size = obstack_object_size (&defines_obstack);
+  char *macro_name = compute_header_macro ();
+
+  fprintf (out, "#ifndef %s\n", macro_name);
+  fprintf (out, "# define %s\n\n", macro_name);
+  fwrite (obstack_finish (&defines_obstack), 1, size, out);
+  fprintf (out, "\n#endif /* not %s */\n", macro_name);
+
+  free (macro_name);
+  xfclose (out);
+}
 
 /*------------------------------------------------------------------.
 | Return the path to the skeleton which locaction might be given in |
 
 /*------------------------------------------------------------------.
 | Return the path to the skeleton which locaction might be given in |
@@ -210,20 +262,10 @@ get_extension_index (const char *filename)
 static void
 compute_exts_from_gf (const char *ext)
 {
 static void
 compute_exts_from_gf (const char *ext)
 {
-  /* Checks if SRC_EXTENSION is NULL. In the other case, %source_extension
-     was specified in the grammar file.  */
-  if (src_extension == NULL)
-    {
-      src_extension = tr (ext, 'y', 'c');
-      src_extension = tr (src_extension, 'Y', 'C');
-    }
-  /* Checks if HEADER_EXTENSION is NULL. In the other case,
-     %header_extension was specified in the grammar file.  */
-  if (header_extension == NULL)
-    {
-      header_extension = tr (ext, 'y', 'h');
-      header_extension = tr (header_extension, 'Y', 'H');
-    }
+  src_extension = tr (ext, 'y', 'c');
+  src_extension = tr (src_extension, 'Y', 'C');
+  header_extension = tr (ext, 'y', 'h');
+  header_extension = tr (header_extension, 'Y', 'H');
 }
 
 /* Computes extensions from the given c source file extension. */
 }
 
 /* Computes extensions from the given c source file extension. */
@@ -378,12 +420,14 @@ output_files (void)
 
   compute_base_names ();
 
 
   compute_base_names ();
 
-  /* Set default extensions */
-  if (!src_extension)
-    src_extension = ".c";
-  if (!header_extension)
-    header_extension = ".h";
-
+  /* It the defines filename if not given, we create it.  */
+  if (!spec_defines_file)
+    spec_defines_file = stringappend (base_name, header_extension);
+  
+  /* It the graph filename if not given, we create it.  */
+  if (!spec_graph_file)
+    spec_graph_file = stringappend (short_base_name, ".vcg");
+  
   attrsfile = stringappend (short_base_name, EXT_STYPE_H);
 #ifndef MSDOS
   stringappend (attrsfile, header_extension);
   attrsfile = stringappend (short_base_name, EXT_STYPE_H);
 #ifndef MSDOS
   stringappend (attrsfile, header_extension);
@@ -397,8 +441,7 @@ output_files (void)
 
   /* Output the header file if wanted. */
   if (defines_flag)
 
   /* Output the header file if wanted. */
   if (defines_flag)
-    obstack_save (&defines_obstack,
-                 stringappend (base_name, header_extension));
+    defines_obstack_save (spec_defines_file);
 
   /* If we output only the table, dump the actions in ACTFILE. */
   if (no_parser_flag)
 
   /* If we output only the table, dump the actions in ACTFILE. */
   if (no_parser_flag)
@@ -425,5 +468,5 @@ output_files (void)
                  stringappend (short_base_name, EXT_OUTPUT));
 
   if (graph_flag)
                  stringappend (short_base_name, EXT_OUTPUT));
 
   if (graph_flag)
-    obstack_save (&graph_obstack, stringappend (short_base_name, ".vcg"));
+    obstack_save (&graph_obstack, spec_graph_file);
 }
 }