Blame src/filter.c

Packit f00812
/* filter - postprocessing of flex output through filters */
Packit f00812
Packit f00812
/*  This file is part of flex. */
Packit f00812
Packit f00812
/*  Redistribution and use in source and binary forms, with or without */
Packit f00812
/*  modification, are permitted provided that the following conditions */
Packit f00812
/*  are met: */
Packit f00812
Packit f00812
/*  1. Redistributions of source code must retain the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer. */
Packit f00812
/*  2. Redistributions in binary form must reproduce the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer in the */
Packit f00812
/*     documentation and/or other materials provided with the distribution. */
Packit f00812
Packit f00812
/*  Neither the name of the University nor the names of its contributors */
Packit f00812
/*  may be used to endorse or promote products derived from this software */
Packit f00812
/*  without specific prior written permission. */
Packit f00812
Packit f00812
/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
Packit f00812
/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
Packit f00812
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
Packit f00812
/*  PURPOSE. */
Packit f00812
Packit f00812
#include "flexdef.h"
Packit f00812
static const char * check_4_gnu_m4 =
Packit f00812
    "m4_dnl ifdef(`__gnu__', ,"
Packit f00812
    "`errprint(Flex requires GNU M4. Set the PATH or set the M4 environment variable to its path name.)"
Packit f00812
    " m4exit(2)')\n";
Packit f00812
Packit f00812
Packit f00812
/** global chain. */
Packit f00812
struct filter *output_chain = NULL;
Packit f00812
Packit f00812
/* Allocate and initialize an external filter.
Packit f00812
 * @param chain the current chain or NULL for new chain
Packit f00812
 * @param cmd the command to execute.
Packit f00812
 * @param ... a NULL terminated list of (const char*) arguments to command,
Packit f00812
 *            not including argv[0].
Packit f00812
 * @return newest filter in chain
Packit f00812
 */
Packit f00812
struct filter *filter_create_ext (struct filter *chain, const char *cmd,
Packit f00812
				  ...)
Packit f00812
{
Packit f00812
	struct filter *f;
Packit f00812
	int     max_args;
Packit f00812
	const char *s;
Packit f00812
	va_list ap;
Packit f00812
Packit f00812
	/* allocate and initialize new filter */
Packit f00812
	f = malloc(sizeof(struct filter));
Packit f00812
	if (!f)
Packit f00812
		flexerror(_("malloc failed (f) in filter_create_ext"));
Packit f00812
	memset (f, 0, sizeof (*f));
Packit f00812
	f->filter_func = NULL;
Packit f00812
	f->extra = NULL;
Packit f00812
	f->next = NULL;
Packit f00812
	f->argc = 0;
Packit f00812
Packit f00812
	if (chain != NULL) {
Packit f00812
		/* append f to end of chain */
Packit f00812
		while (chain->next)
Packit f00812
			chain = chain->next;
Packit f00812
		chain->next = f;
Packit f00812
	}
Packit f00812
Packit f00812
Packit f00812
	/* allocate argv, and populate it with the argument list. */
Packit f00812
	max_args = 8;
Packit f00812
	f->argv = malloc(sizeof(char *) * (size_t) (max_args + 1));
Packit f00812
	if (!f->argv)
Packit f00812
		flexerror(_("malloc failed (f->argv) in filter_create_ext"));
Packit f00812
	f->argv[f->argc++] = cmd;
Packit f00812
Packit f00812
	va_start (ap, cmd);
Packit f00812
	while ((s = va_arg (ap, const char *)) != NULL) {
Packit f00812
		if (f->argc >= max_args) {
Packit f00812
			max_args += 8;
Packit f00812
			f->argv = realloc(f->argv, sizeof(char*) * (size_t) (max_args + 1));
Packit f00812
		}
Packit f00812
		f->argv[f->argc++] = s;
Packit f00812
	}
Packit f00812
	f->argv[f->argc] = NULL;
Packit f00812
Packit f00812
	va_end (ap);
Packit f00812
	return f;
Packit f00812
}
Packit f00812
Packit f00812
/* Allocate and initialize an internal filter.
Packit f00812
 * @param chain the current chain or NULL for new chain
Packit f00812
 * @param filter_func The function that will perform the filtering.
Packit f00812
 *        filter_func should return 0 if successful, and -1
Packit f00812
 *        if an error occurs -- or it can simply exit().
Packit f00812
 * @param extra optional user-defined data to pass to the filter.
Packit f00812
 * @return newest filter in chain
Packit f00812
 */
Packit f00812
struct filter *filter_create_int (struct filter *chain,
Packit f00812
				  int (*filter_func) (struct filter *),
Packit f00812
				  void *extra)
Packit f00812
{
Packit f00812
	struct filter *f;
Packit f00812
Packit f00812
	/* allocate and initialize new filter */
Packit f00812
	f = malloc(sizeof(struct filter));
Packit f00812
	if (!f)
Packit f00812
		flexerror(_("malloc failed in filter_create_int"));
Packit f00812
	memset (f, 0, sizeof (*f));
Packit f00812
	f->next = NULL;
Packit f00812
	f->argc = 0;
Packit f00812
	f->argv = NULL;
Packit f00812
Packit f00812
	f->filter_func = filter_func;
Packit f00812
	f->extra = extra;
Packit f00812
Packit f00812
	if (chain != NULL) {
Packit f00812
		/* append f to end of chain */
Packit f00812
		while (chain->next)
Packit f00812
			chain = chain->next;
Packit f00812
		chain->next = f;
Packit f00812
	}
Packit f00812
Packit f00812
	return f;
Packit f00812
}
Packit f00812
Packit f00812
/** Fork and exec entire filter chain.
Packit f00812
 *  @param chain The head of the chain.
Packit f00812
 *  @return true on success.
Packit f00812
 */
Packit f00812
bool filter_apply_chain (struct filter * chain)
Packit f00812
{
Packit f00812
	int     pid, pipes[2];
Packit f00812
Packit f00812
Packit f00812
	/* Tricky recursion, since we want to begin the chain
Packit f00812
	 * at the END. Why? Because we need all the forked processes
Packit f00812
	 * to be children of the main flex process.
Packit f00812
	 */
Packit f00812
	if (chain)
Packit f00812
		filter_apply_chain (chain->next);
Packit f00812
	else
Packit f00812
		return true;
Packit f00812
Packit f00812
	/* Now we are the right-most unprocessed link in the chain.
Packit f00812
	 */
Packit f00812
Packit f00812
	fflush (stdout);
Packit f00812
	fflush (stderr);
Packit f00812
Packit f00812
Packit f00812
	if (pipe (pipes) == -1)
Packit f00812
		flexerror (_("pipe failed"));
Packit f00812
Packit f00812
	if ((pid = fork ()) == -1)
Packit f00812
		flexerror (_("fork failed"));
Packit f00812
Packit f00812
	if (pid == 0) {
Packit f00812
		/* child */
Packit f00812
Packit f00812
        /* We need stdin (the FILE* stdin) to connect to this new pipe.
Packit f00812
         * There is no portable way to set stdin to a new file descriptor,
Packit f00812
         * as stdin is not an lvalue on some systems (BSD).
Packit f00812
         * So we dup the new pipe onto the stdin descriptor and use a no-op fseek
Packit f00812
         * to sync the stream. This is a Hail Mary situation. It seems to work.
Packit f00812
         */
Packit f00812
		close (pipes[1]);
Packit f00812
clearerr(stdin);
Packit f00812
		if (dup2 (pipes[0], fileno (stdin)) == -1)
Packit f00812
			flexfatal (_("dup2(pipes[0],0)"));
Packit f00812
		close (pipes[0]);
Packit f00812
        fseek (stdin, 0, SEEK_CUR);
Packit f00812
        ungetc(' ', stdin); /* still an evil hack, but one that works better */
Packit f00812
        (void)fgetc(stdin); /* on NetBSD than the fseek attempt does */
Packit f00812
Packit f00812
		/* run as a filter, either internally or by exec */
Packit f00812
		if (chain->filter_func) {
Packit f00812
			int     r;
Packit f00812
Packit f00812
			if ((r = chain->filter_func (chain)) == -1)
Packit f00812
				flexfatal (_("filter_func failed"));
Packit f00812
			exit (0);
Packit f00812
		}
Packit f00812
		else {
Packit f00812
			execvp (chain->argv[0],
Packit f00812
				(char **const) (chain->argv));
Packit f00812
            lerr_fatal ( _("exec of %s failed"),
Packit f00812
                    chain->argv[0]);
Packit f00812
		}
Packit f00812
Packit f00812
		exit (1);
Packit f00812
	}
Packit f00812
Packit f00812
	/* Parent */
Packit f00812
	close (pipes[0]);
Packit f00812
	if (dup2 (pipes[1], fileno (stdout)) == -1)
Packit f00812
		flexfatal (_("dup2(pipes[1],1)"));
Packit f00812
	close (pipes[1]);
Packit f00812
    fseek (stdout, 0, SEEK_CUR);
Packit f00812
Packit f00812
	return true;
Packit f00812
}
Packit f00812
Packit f00812
/** Truncate the chain to max_len number of filters.
Packit f00812
 * @param chain the current chain.
Packit f00812
 * @param max_len the maximum length of the chain.
Packit f00812
 * @return the resulting length of the chain.
Packit f00812
 */
Packit f00812
int filter_truncate (struct filter *chain, int max_len)
Packit f00812
{
Packit f00812
	int     len = 1;
Packit f00812
Packit f00812
	if (!chain)
Packit f00812
		return 0;
Packit f00812
Packit f00812
	while (chain->next && len < max_len) {
Packit f00812
		chain = chain->next;
Packit f00812
		++len;
Packit f00812
	}
Packit f00812
Packit f00812
	chain->next = NULL;
Packit f00812
	return len;
Packit f00812
}
Packit f00812
Packit f00812
/** Splits the chain in order to write to a header file.
Packit f00812
 *  Similar in spirit to the 'tee' program.
Packit f00812
 *  The header file name is in extra.
Packit f00812
 *  @return 0 (zero) on success, and -1 on failure.
Packit f00812
 */
Packit f00812
int filter_tee_header (struct filter *chain)
Packit f00812
{
Packit f00812
	/* This function reads from stdin and writes to both the C file and the
Packit f00812
	 * header file at the same time.
Packit f00812
	 */
Packit f00812
Packit f00812
	const int readsz = 512;
Packit f00812
	char   *buf;
Packit f00812
	int     to_cfd = -1;
Packit f00812
	FILE   *to_c = NULL, *to_h = NULL;
Packit f00812
	bool    write_header;
Packit f00812
Packit f00812
	write_header = (chain->extra != NULL);
Packit f00812
Packit f00812
	/* Store a copy of the stdout pipe, which is already piped to C file
Packit f00812
	 * through the running chain. Then create a new pipe to the H file as
Packit f00812
	 * stdout, and fork the rest of the chain again.
Packit f00812
	 */
Packit f00812
Packit f00812
	if ((to_cfd = dup (1)) == -1)
Packit f00812
		flexfatal (_("dup(1) failed"));
Packit f00812
	to_c = fdopen (to_cfd, "w");
Packit f00812
Packit f00812
	if (write_header) {
Packit f00812
		if (freopen ((char *) chain->extra, "w", stdout) == NULL)
Packit f00812
			flexfatal (_("freopen(headerfilename) failed"));
Packit f00812
Packit f00812
		filter_apply_chain (chain->next);
Packit f00812
		to_h = stdout;
Packit f00812
	}
Packit f00812
Packit f00812
	/* Now to_c is a pipe to the C branch, and to_h is a pipe to the H branch.
Packit f00812
	 */
Packit f00812
Packit f00812
	if (write_header) {
Packit f00812
        fputs (check_4_gnu_m4, to_h);
Packit f00812
		fputs ("m4_changecom`'m4_dnl\n", to_h);
Packit f00812
		fputs ("m4_changequote`'m4_dnl\n", to_h);
Packit f00812
		fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_h);
Packit f00812
	    fputs ("m4_define([[M4_YY_NOOP]])[[]]m4_dnl\n", to_h);
Packit f00812
		fputs ("m4_define( [[M4_YY_IN_HEADER]],[[]])m4_dnl\n",
Packit f00812
		       to_h);
Packit f00812
		fprintf (to_h, "#ifndef %sHEADER_H\n", prefix);
Packit f00812
		fprintf (to_h, "#define %sHEADER_H 1\n", prefix);
Packit f00812
		fprintf (to_h, "#define %sIN_HEADER 1\n\n", prefix);
Packit f00812
		fprintf (to_h,
Packit f00812
			 "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
Packit f00812
			 headerfilename ? headerfilename : "<stdout>");
Packit f00812
Packit f00812
	}
Packit f00812
Packit f00812
    fputs (check_4_gnu_m4, to_c);
Packit f00812
	fputs ("m4_changecom`'m4_dnl\n", to_c);
Packit f00812
	fputs ("m4_changequote`'m4_dnl\n", to_c);
Packit f00812
	fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_c);
Packit f00812
	fputs ("m4_define([[M4_YY_NOOP]])[[]]m4_dnl\n", to_c);
Packit f00812
	fprintf (to_c, "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
Packit f00812
		 outfilename ? outfilename : "<stdout>");
Packit f00812
Packit f00812
	buf = malloc((size_t) readsz);
Packit f00812
	if (!buf)
Packit f00812
		flexerror(_("malloc failed in filter_tee_header"));
Packit f00812
	while (fgets (buf, readsz, stdin)) {
Packit f00812
		fputs (buf, to_c);
Packit f00812
		if (write_header)
Packit f00812
			fputs (buf, to_h);
Packit f00812
	}
Packit f00812
Packit f00812
	if (write_header) {
Packit f00812
		fprintf (to_h, "\n");
Packit f00812
Packit f00812
		/* write a fake line number. It will get fixed by the linedir filter. */
Packit f00812
		fprintf (to_h, "#line 4000 \"M4_YY_OUTFILE_NAME\"\n");
Packit f00812
Packit f00812
		fprintf (to_h, "#undef %sIN_HEADER\n", prefix);
Packit f00812
		fprintf (to_h, "#endif /* %sHEADER_H */\n", prefix);
Packit f00812
		fputs ("m4_undefine( [[M4_YY_IN_HEADER]])m4_dnl\n", to_h);
Packit f00812
Packit f00812
		fflush (to_h);
Packit f00812
		if (ferror (to_h))
Packit f00812
			lerr (_("error writing output file %s"),
Packit f00812
				(char *) chain->extra);
Packit f00812
Packit f00812
		else if (fclose (to_h))
Packit f00812
			lerr (_("error closing output file %s"),
Packit f00812
				(char *) chain->extra);
Packit f00812
	}
Packit f00812
Packit f00812
	fflush (to_c);
Packit f00812
	if (ferror (to_c))
Packit f00812
		lerr (_("error writing output file %s"),
Packit f00812
			outfilename ? outfilename : "<stdout>");
Packit f00812
Packit f00812
	else if (fclose (to_c))
Packit f00812
		lerr (_("error closing output file %s"),
Packit f00812
			outfilename ? outfilename : "<stdout>");
Packit f00812
Packit f00812
	while (wait (0) > 0) ;
Packit f00812
Packit f00812
	exit (0);
Packit f00812
	return 0;
Packit f00812
}
Packit f00812
Packit f00812
/** Adjust the line numbers in the #line directives of the generated scanner.
Packit f00812
 * After the m4 expansion, the line numbers are incorrect since the m4 macros
Packit f00812
 * can add or remove lines.  This only adjusts line numbers for generated code,
Packit f00812
 * not user code. This also happens to be a good place to squeeze multiple
Packit f00812
 * blank lines into a single blank line.
Packit f00812
 */
Packit f00812
int filter_fix_linedirs (struct filter *chain)
Packit f00812
{
Packit f00812
	char   *buf;
Packit f00812
	const size_t readsz = 512;
Packit f00812
	int     lineno = 1;
Packit f00812
	bool    in_gen = true;	/* in generated code */
Packit f00812
	bool    last_was_blank = false;
Packit f00812
Packit f00812
	if (!chain)
Packit f00812
		return 0;
Packit f00812
Packit f00812
	buf = malloc(readsz);
Packit f00812
	if (!buf)
Packit f00812
		flexerror(_("malloc failed in filter_fix_linedirs"));
Packit f00812
Packit f00812
	while (fgets (buf, (int) readsz, stdin)) {
Packit f00812
Packit f00812
		regmatch_t m[10];
Packit f00812
Packit f00812
		/* Check for #line directive. */
Packit f00812
		if (buf[0] == '#'
Packit f00812
			&& regexec (&regex_linedir, buf, 3, m, 0) == 0) {
Packit f00812
Packit f00812
			char   *fname;
Packit f00812
Packit f00812
			/* extract the line number and filename */
Packit f00812
			fname = regmatch_dup (&m[2], buf);
Packit f00812
Packit f00812
			if (strcmp (fname,
Packit f00812
				outfilename ? outfilename : "<stdout>")
Packit f00812
					== 0
Packit f00812
			 || strcmp (fname,
Packit f00812
			 	headerfilename ? headerfilename : "<stdout>")
Packit f00812
					== 0) {
Packit f00812
Packit f00812
				char    *s1, *s2;
Packit f00812
				char	filename[MAXLINE];
Packit f00812
Packit f00812
				s1 = fname;
Packit f00812
				s2 = filename;
Packit f00812
Packit f00812
				while ((s2 - filename) < (MAXLINE - 1) && *s1) {
Packit f00812
					/* Escape the backslash */
Packit f00812
					if (*s1 == '\\')
Packit f00812
						*s2++ = '\\';
Packit f00812
					/* Escape the double quote */
Packit f00812
					if (*s1 == '\"')
Packit f00812
						*s2++ = '\\';
Packit f00812
					/* Copy the character as usual */
Packit f00812
					*s2++ = *s1++;
Packit f00812
				}
Packit f00812
Packit f00812
				*s2 = '\0';
Packit f00812
Packit f00812
				/* Adjust the line directives. */
Packit f00812
				in_gen = true;
Packit f00812
				snprintf (buf, readsz, "#line %d \"%s\"\n",
Packit f00812
					  lineno + 1, filename);
Packit f00812
			}
Packit f00812
			else {
Packit f00812
				/* it's a #line directive for code we didn't write */
Packit f00812
				in_gen = false;
Packit f00812
			}
Packit f00812
Packit f00812
			free (fname);
Packit f00812
			last_was_blank = false;
Packit f00812
		}
Packit f00812
Packit f00812
		/* squeeze blank lines from generated code */
Packit f00812
		else if (in_gen
Packit f00812
			 && regexec (&regex_blank_line, buf, 0, NULL,
Packit f00812
				     0) == 0) {
Packit f00812
			if (last_was_blank)
Packit f00812
				continue;
Packit f00812
			else
Packit f00812
				last_was_blank = true;
Packit f00812
		}
Packit f00812
Packit f00812
		else {
Packit f00812
			/* it's a line of normal, non-empty code. */
Packit f00812
			last_was_blank = false;
Packit f00812
		}
Packit f00812
Packit f00812
		fputs (buf, stdout);
Packit f00812
		lineno++;
Packit f00812
	}
Packit f00812
	fflush (stdout);
Packit f00812
	if (ferror (stdout))
Packit f00812
		lerr (_("error writing output file %s"),
Packit f00812
			outfilename ? outfilename : "<stdout>");
Packit f00812
Packit f00812
	else if (fclose (stdout))
Packit f00812
		lerr (_("error closing output file %s"),
Packit f00812
			outfilename ? outfilename : "<stdout>");
Packit f00812
Packit f00812
	return 0;
Packit f00812
}
Packit f00812
Packit f00812
/* vim:set expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */