- /* Store the definition of all the muscles. */
- 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_tokens], \n[", out);
- token_definitions_output (out);
- fputs ("])\n\n", out);
-
- muscles_m4_output (out);
-
- 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. */
+ FILE *in;
+ int filter_fd[2];
+ char const *argv[9];
+ pid_t pid;
+
+ /* Compute the names of the package data dir and skeleton files. */
+ 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 = compute_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);
+
+ /* Test whether m4sugar.m4 is readable, to check for proper
+ installation. A faulty installation can cause deadlock, so a
+ cheap sanity check is worthwhile. */
+ 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);
+
+ /* Some future version of GNU M4 (most likely 1.6) may treat the -dV in a
+ position-dependent manner. Keep it as the first argument so that all
+ files are traced.
+
+ See the thread starting at
+ <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
+ for details. */
+ {
+ int i = 0;
+ argv[i++] = m4;
+ argv[i++] = "-I";
+ argv[i++] = pkgdatadir;
+ if (trace_flag & trace_m4)
+ argv[i++] = "-dV";
+ argv[i++] = full_m4sugar;
+ argv[i++] = "-";
+ argv[i++] = full_m4bison;
+ argv[i++] = full_skeleton;
+ argv[i++] = NULL;
+ }
+ /* When POSIXLY_CORRECT is set, some future versions of GNU M4 (most likely
+ 2.0) may drop some of the GNU extensions that Bison's skeletons depend
+ upon. So that the next release of Bison is forward compatible with those
+ future versions of GNU M4, we unset POSIXLY_CORRECT here.
+
+ FIXME: A user might set POSIXLY_CORRECT to affect processes run from
+ macros like m4_syscmd in a custom skeleton. For now, Bison makes no
+ promises about the behavior of custom skeletons, so this scenario is not a
+ concern. However, we eventually want to eliminate this shortcoming. The
+ next release of GNU M4 (1.4.12 or 1.6) will accept the -g command-line
+ option as a no-op, and later releases will accept it to indicate that
+ POSIXLY_CORRECT should be ignored. Once the GNU M4 versions that accept
+ -g are pervasive, Bison should use -g instead of unsetting
+ POSIXLY_CORRECT.
+
+ See the thread starting at
+ <http://lists.gnu.org/archive/html/bug-bison/2008-07/msg00000.html>
+ for details. */
+ unsetenv ("POSIXLY_CORRECT");
+ init_subpipe ();
+ pid = create_subpipe (argv, filter_fd);
+ free (full_m4sugar);
+ free (full_m4bison);
+ free (full_skeleton);
+
+ if (trace_flag & trace_muscles)
+ muscles_output (stderr);