]> git.saurik.com Git - bison.git/blobdiff - src/output.c
* src/output.c (output_skeleton): tempdir is const.
[bison.git] / src / output.c
index 702f0cb18bf8f95608f8e0e79013272fb1f8be0f..140f220b59ba7932015bd1e5dacaf8e41858a392 100644 (file)
@@ -89,6 +89,7 @@
    negative short int.  Used to flag ??  */
 
 #include "system.h"
+#include "bitsetv.h"
 #include "quotearg.h"
 #include "error.h"
 #include "getargs.h"
@@ -226,7 +227,7 @@ output_gram (void)
 static void
 output_stos (void)
 {
-  int i;
+  size_t i;
   short *values = (short *) alloca (sizeof (short) * nstates);
   for (i = 0; i < nstates; ++i)
     values[i] = states[i]->accessing_symbol;
@@ -275,8 +276,9 @@ output_rule_data (void)
       obstack_sgrow (&format_obstack, ", ");
       j += strsize;
     }
-  /* add a NULL entry to list of tokens */
-  obstack_sgrow (&format_obstack, "NULL");
+  /* Add a NULL entry to list of tokens (well, 0, as NULL might not be
+     defined).  */
+  obstack_sgrow (&format_obstack, "0");
 
   /* Finish table and store. */
   obstack_1grow (&format_obstack, 0);
@@ -355,7 +357,7 @@ action_row (state_t *state)
        for (j = 0; j < ntokens; j++)
          /* and record this rule as the rule to use if that
             token follows.  */
-         if (BITISSET (LA (state->lookaheadsp + i), j))
+         if (bitset_test (LA[state->lookaheadsp + i], j))
            actrow[j] = -LAruleno[state->lookaheadsp + i];
     }
 
@@ -487,7 +489,7 @@ save_row (int state)
 static void
 token_actions (void)
 {
-  int i;
+  size_t i;
   short *yydefact = XCALLOC (short, nstates);
 
   actrow = XCALLOC (short, ntokens);
@@ -640,9 +642,9 @@ save_column (int symbol, int default_state)
 static int
 default_goto (int symbol)
 {
-  int i;
-  int m = goto_map[symbol];
-  int n = goto_map[symbol + 1];
+  size_t i;
+  size_t m = goto_map[symbol];
+  size_t n = goto_map[symbol + 1];
   int default_state = -1;
   int max = 0;
 
@@ -740,7 +742,7 @@ matching_state (int vector)
   int w;
   int prev;
 
-  if (i >= nstates)
+  if (i >= (int) nstates)
     return -1;
 
   t = tally[i];
@@ -912,7 +914,7 @@ output_check (void)
 static void
 output_actions (void)
 {
-  int i;
+  size_t i;
   nvectors = nstates + nvars;
 
   froms = XCALLOC (short *, nvectors);
@@ -921,7 +923,7 @@ output_actions (void)
   width = XCALLOC (short, nvectors);
 
   token_actions ();
-  XFREE (LA);
+  bitsetv_free (LA);
   XFREE (LAruleno);
 
   goto_actions ();
@@ -955,60 +957,72 @@ output_actions (void)
 static void
 output_skeleton (void)
 {
-  /* Find the right skeleton file.  */
-  if (!skeleton)
-    {
-      if (semantic_parser)
-       skeleton = "bison.hairy";
-      else
-       skeleton = "bison.simple";
-    }
-
-  /* Parse the skeleton file and output the needed parsers.  */
-  muscle_insert ("skeleton", skeleton);
-
   /* Store the definition of all the muscles. */
-  {
-    FILE *muscles_m4 = xfopen ("/tmp/muscles.m4", "w");
-    /* There are no comments, especially not `#': we do want M4 expansion
-       after `#': think of CPP macros!  */
-    fprintf (muscles_m4, "m4_changecom()\n");
-    fprintf (muscles_m4, "m4_init()\n");
-
-    fprintf (muscles_m4, "m4_define([b4_actions], \n[[");
-    actions_output (muscles_m4);
-    fprintf (muscles_m4, "]])\n\n");
-
-    fprintf (muscles_m4, "m4_define([b4_guards], \n[[");
-    guards_output (muscles_m4);
-    fprintf (muscles_m4, "]])\n\n");
-
-    fprintf (muscles_m4, "m4_define([b4_tokendef], \n[[");
-    token_definitions_output (muscles_m4);
-    fprintf (muscles_m4, "]])\n\n");
+  const char *tempdir = getenv ("TMPDIR");
+  char *tempfile = NULL;
+  FILE *out = NULL;
+  int fd;
+
+  if (tempdir == NULL)
+    tempdir = DEFAULT_TMPDIR;
+  tempfile = xmalloc (strlen (tempdir) + 11);
+  sprintf (tempfile, "%s/bsnXXXXXX", tempdir);
+  fd = mkstemp (tempfile);
+  if (fd == -1)
+    error (EXIT_FAILURE, errno, "%s", tempfile);
+
+  out = fdopen (fd, "w");
+  if (out == NULL)
+    error (EXIT_FAILURE, errno, "%s", tempfile);
+
+  /* There are no comments, especially not `#': we do want M4 expansion
+     after `#': think of CPP macros!  */
+  fputs ("m4_changecom()\n", out);
+  fputs ("m4_init()\n", out);
+
+  fputs ("m4_define([b4_actions], \n[[", out);
+  actions_output (out);
+  fputs ("]])\n\n", out);
+
+  fputs ("m4_define([b4_guards], \n[[", out);
+  guards_output (out);
+  fputs ("]])\n\n", out);
+
+  fputs ("m4_define([b4_tokendef], \n[[", out);
+  token_definitions_output (out);
+  fputs ("]])\n\n", out);
 
-    muscles_m4_output (muscles_m4);
+  muscles_m4_output (out);
 
-    fprintf (muscles_m4, "m4_wrap([m4_divert_pop(0)])\n");
-    fprintf (muscles_m4, "m4_divert_push(0)dnl\n");
-    xfclose (muscles_m4);
-  }
+  fputs ("m4_wrap([m4_divert_pop(0)])\n", out);
+  fputs ("m4_divert_push(0)dnl\n", out);
+  xfclose (out);
 
   /* Invoke m4 on the definition of the muscles, and the skeleton. */
   {
     const char *bison_pkgdatadir = getenv ("BISON_PKGDATADIR");
+    const char *m4 = getenv ("M4");
+    if (!m4)
+      m4 = M4;
     if (!bison_pkgdatadir)
       bison_pkgdatadir = PKGDATADIR;
-    skel_in = readpipe ("m4",
-                       "-I",
-                       bison_pkgdatadir,
+    if (trace_flag)
+      fprintf (stderr,
+              "running: %s -I %s m4sugar/m4sugar.m4 %s %s\n",
+              m4, bison_pkgdatadir, tempfile, skeleton);
+    skel_in = readpipe (m4,
+                       "-I", bison_pkgdatadir,
                        "m4sugar/m4sugar.m4",
-                       "/tmp/muscles.m4",
+                       tempfile,
                        skeleton,
                        NULL);
     if (!skel_in)
       error (EXIT_FAILURE, errno, "cannot run m4");
     skel_lex ();
+
+    /* If `debugging', keep this file alive. */
+    if (!trace_flag)
+      unlink (tempfile);
   }
 }
 
@@ -1039,66 +1053,22 @@ prepare (void)
 
   MUSCLE_INSERT_INT ("locations_flag", locations_flag);
   MUSCLE_INSERT_INT ("defines_flag", defines_flag);
-}
-
-/*-------------------------.
-| Output the header file.  |
-`-------------------------*/
 
-static void
-header_output (void)
-{
-  FILE *out = xfopen (spec_defines_file, "w");
-  char *macro_name = compute_header_macro ();
-
-  fprintf (out, "#ifndef %s\n", macro_name);
-  fprintf (out, "# define %s\n\n", macro_name);
-
-  token_definitions_output (out);
-  fprintf (out, "\
-#ifndef YYSTYPE\n\
-typedef %s
-yystype;\n\
-# define YYSTYPE yystype\n\
-#endif\n",
-                   muscle_find ("stype"));
-
-  if (!pure_parser)
-    fprintf (out, "\nextern YYSTYPE %slval;\n",
-            spec_name_prefix ? spec_name_prefix : "yy");
-
-  if (locations_flag)
-    {
-      fputs ("\n\n", out);
-      fprintf (out, "\
-#ifndef YYLTYPE\n\
-typedef struct yyltype\n\
-{\n\
-  int first_line;\n\
-  int first_column;\n\
-  int last_line;\n\
-  int last_column;\n\
-} yyltype;\n\
-# define YYLTYPE yyltype\n\
-#endif\n");
-      if (!pure_parser)
-       fprintf (out, "\nextern YYLTYPE %slloc;\n",
-                spec_name_prefix ? spec_name_prefix : "yy");
-    }
+  /* Copy definitions in directive.  */
+  obstack_1grow (&attrs_obstack, 0);
+  muscle_insert ("prologue", obstack_finish (&attrs_obstack));
 
-  if (semantic_parser)
+  /* Find the right skeleton file.  */
+  if (!skeleton)
     {
-      int i;
-
-      for (i = ntokens; i < nsyms; i++)
-       /* don't make these for dummy nonterminals made by gensym.  */
-       if (*symbols[i]->tag != '@')
-         fprintf (out, "# define NT%s\t%d\n", symbols[i]->tag, i);
+      if (semantic_parser)
+       skeleton = "bison.hairy";
+      else
+       skeleton = "bison.simple";
     }
 
-  fprintf (out, "\n#endif /* not %s */\n", macro_name);
-  free (macro_name);
-  xfclose (out);
+  /* Parse the skeleton file and output the needed parsers.  */
+  muscle_insert ("skeleton", skeleton);
 }
 
 
@@ -1114,26 +1084,16 @@ output (void)
   output_token_translations ();
   output_gram ();
 
-  XFREE (ritem);
   if (semantic_parser)
     output_stos ();
   output_rule_data ();
   output_actions ();
 
   prepare ();
-  /* Copy definitions in directive.  */
-  obstack_1grow (&attrs_obstack, 0);
-  muscle_insert ("prologue", obstack_finish (&attrs_obstack));
 
   /* Process the selected skeleton file.  */
   output_skeleton ();
 
-  /* Output the header if needed. */
-
-  if (0)
-    header_output ();
-
-  free (rules + 1);
   obstack_free (&muscle_obstack, NULL);
   obstack_free (&format_obstack, NULL);
   obstack_free (&action_obstack, NULL);