- obstack_fgrow1 (&table_obstack, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
- output_short_table (&table_obstack, NULL, "yytable", table,
- table[0], 1, high + 1);
- XFREE (table);
-}
-
-
-static void
-output_check (void)
-{
- output_short_table (&table_obstack, NULL, "yycheck", check,
- check[0], 1, high + 1);
- XFREE (check);
-}
-
-/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
- and yycheck. */
-
-static void
-output_actions (void)
-{
- nvectors = nstates + nvars;
-
- froms = XCALLOC (short *, nvectors);
- tos = XCALLOC (short *, nvectors);
- tally = XCALLOC (short, nvectors);
- width = XCALLOC (short, nvectors);
-
- token_actions ();
- free_shifts ();
- free_reductions ();
- XFREE (lookaheads);
- XFREE (LA);
- XFREE (LAruleno);
- XFREE (accessing_symbol);
-
- goto_actions ();
- XFREE (goto_map + ntokens);
- XFREE (from_state);
- XFREE (to_state);
-
- sort_actions ();
- pack_table ();
- obstack_1grow (&table_obstack, '\n');
- output_base ();
- output_table ();
- obstack_1grow (&table_obstack, '\n');
- output_check ();
-}
-
-/*------------------------------------------.
-| Copy the parser code into TABLE_OBSTACK. |
-`------------------------------------------*/
-
-static void
-output_parser (void)
-{
- int c;
- FILE *fskel;
- size_t line;
- int actions_dumped = 0;
-
- if (pure_parser)
- obstack_sgrow (&table_obstack, "#define YYPURE 1\n\n");
-
- /* Loop over lines in the standard parser file. */
- if (!skeleton)
- {
- if (semantic_parser)
- skeleton = skeleton_find ("BISON_HAIRY", BISON_HAIRY);
- else
- skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
- }
- fskel = xfopen (skeleton, "r");
-
- /* Set LINE to 2, not 1: `#line LINENUM' -- Here LINENUM is a
- decimal integer constant. This specifies that the line number of
- the *following* line of input, in its original source file, was
- LINENUM. */
- line = 2;
-
- while (1)
- {
- enum line_type_e
- {
- regular_line,
- sync_line, /* #line. */
- actions_line /* %% actions. */
- };
- enum line_type_e line_type = regular_line;
-
- c = getc (fskel);
-
- /* Is this line special? */
- if (c == '#')
- {
- /* See if it's a `#line' line. */
- if ((c = getc (fskel)) == 'l')
- if ((c = getc (fskel)) == 'i')
- if ((c = getc (fskel)) == 'n')
- if ((c = getc (fskel)) == 'e')
- line_type = sync_line;
- else
- obstack_sgrow (&table_obstack, "#lin");
- else
- obstack_sgrow (&table_obstack, "#li");
- else
- obstack_sgrow (&table_obstack, "#l");
- else
- obstack_sgrow (&table_obstack, "#");
- }
- else if (c == '%')
- {
- /* See if it's a `%% actions' line. */
- if ((c = getc (fskel)) == '%')
- if ((c = getc (fskel)) == ' ')
- if ((c = getc (fskel)) == 'a')
- if ((c = getc (fskel)) == 'c')
- if ((c = getc (fskel)) == 't')
- if ((c = getc (fskel)) == 'i')
- if ((c = getc (fskel)) == 'o')
- if ((c = getc (fskel)) == 'n')
- if ((c = getc (fskel)) == 's')
- line_type = actions_line;
- else
- obstack_sgrow (&table_obstack, "%% action");
- else
- obstack_sgrow (&table_obstack, "%% actio");
- else
- obstack_sgrow (&table_obstack, "%% acti");
- else
- obstack_sgrow (&table_obstack, "%% act");
- else
- obstack_sgrow (&table_obstack, "%% ac");
- else
- obstack_sgrow (&table_obstack, "%% a");
- else
- obstack_sgrow (&table_obstack, "%% ");
- else
- obstack_sgrow (&table_obstack, "%%");
- else
- obstack_sgrow (&table_obstack, "%");
- }
-
- switch (line_type)
- {
- case sync_line:
- if (!no_lines_flag)
- obstack_fgrow2 (&table_obstack, "#line %d %s\n",
- line, quotearg_style (c_quoting_style, skeleton));
-
- /* Skip the end of line. */
- for (; c != '\n' && c != EOF; c = getc (fskel))
- /* nothing */;
- break;
-
- case actions_line:
- {
- size_t size = obstack_object_size (&action_obstack);
-
- actions_dumped++;
- assert (actions_dumped == 1);
- obstack_grow (&table_obstack,
- obstack_finish (&action_obstack),
- size);
- }
-
- /* Skip the end of line. */
- for (; c != '\n' && c != EOF; c = getc (fskel))
- /* nothing */;
- break;
-
- case regular_line:
- for (; c != '\n' && c != EOF; c = getc (fskel))
- obstack_1grow (&table_obstack, c);
- }
-
- if (c == EOF)
- break;
- obstack_1grow (&table_obstack, c);
- line++;
- }
- assert (actions_dumped == 1);
- xfclose (fskel);
-}
-
-static void
-output_program (void)
-{
- int c;
-
- if (!no_lines_flag)
- obstack_fgrow2 (&table_obstack, "#line %d %s\n",
- lineno, quotearg_style (c_quoting_style, infile));
-
- while ((c = getc (finput)) != EOF)
- obstack_1grow (&table_obstack, c);
+ FILE *in;
+ FILE *out;
+ int filter_fd[2];
+ char const *argv[7];
+ pid_t pid;
+
+ /* Compute the names of the package data dir and skeleton file.
+ Test whether m4sugar.m4 is readable, to check for proper
+ installation. A faulty installation can cause deadlock, so a
+ cheap sanity check is worthwhile. */
+ char const m4sugar[] = "m4sugar/m4sugar.m4";
+ char const m4bison[] = "bison.m4";
+ char *full_m4sugar;
+ char *full_m4bison;
+ char *full_skeleton;
+ char const *p;
+ char const *m4 = (p = getenv ("M4")) ? p : M4;
+ char const *pkgdatadir = (p = getenv ("BISON_PKGDATADIR")) ? p : PKGDATADIR;
+ size_t skeleton_size = strlen (skeleton) + 1;
+ size_t pkgdatadirlen = strlen (pkgdatadir);
+ while (pkgdatadirlen && pkgdatadir[pkgdatadirlen - 1] == '/')
+ pkgdatadirlen--;
+ full_skeleton = xmalloc (pkgdatadirlen + 1
+ + (skeleton_size < sizeof m4sugar
+ ? sizeof m4sugar : skeleton_size));
+ strncpy (full_skeleton, pkgdatadir, pkgdatadirlen);
+ full_skeleton[pkgdatadirlen] = '/';
+ strcpy (full_skeleton + pkgdatadirlen + 1, m4sugar);
+ full_m4sugar = xstrdup (full_skeleton);
+ strcpy (full_skeleton + pkgdatadirlen + 1, m4bison);
+ full_m4bison = xstrdup (full_skeleton);
+ if (strchr (skeleton, '/'))
+ strcpy (full_skeleton, skeleton);
+ else
+ strcpy (full_skeleton + pkgdatadirlen + 1, skeleton);
+ xfclose (xfopen (full_m4sugar, "r"));
+
+ /* Create an m4 subprocess connected to us via two pipes. */
+
+ if (trace_flag & trace_tools)
+ fprintf (stderr, "running: %s %s - %s %s\n",
+ m4, full_m4sugar, full_m4bison, full_skeleton);
+
+ argv[0] = m4;
+ argv[1] = full_m4sugar;
+ argv[2] = "-";
+ argv[3] = full_m4bison;
+ argv[4] = full_skeleton;
+ argv[5] = trace_flag & trace_m4 ? "-dV" : NULL;
+ argv[6] = NULL;
+
+ init_subpipe ();
+ pid = create_subpipe (argv, filter_fd);
+ free (full_m4bison);
+ free (full_m4sugar);
+ free (full_skeleton);
+
+ out = fdopen (filter_fd[0], "w");
+ if (! out)
+ error (EXIT_FAILURE, get_errno (),
+ "fdopen");
+
+ /* Output the definitions of all the muscles. */
+ fputs ("m4_init()\n", out);
+
+ user_actions_output (out);
+ merger_output (out);
+ token_definitions_output (out);
+ symbol_code_props_output (out, "destructors", &symbol_destructor_get);
+ symbol_code_props_output (out, "printers", &symbol_printer_get);
+
+ muscles_m4_output (out);
+ xfclose (out);
+
+ /* Read and process m4's output. */
+ timevar_push (TV_M4);
+ end_of_output_subpipe (pid, filter_fd);
+ in = fdopen (filter_fd[1], "r");
+ if (! in)
+ error (EXIT_FAILURE, get_errno (),
+ "fdopen");
+ scan_skel (in);
+ xfclose (in);
+ reap_subpipe (pid, m4);
+ timevar_pop (TV_M4);