1/*	$NetBSD: filter.c,v 1.1.1.1 2009/10/26 00:26:00 christos Exp $	*/
2
3/* filter - postprocessing of flex output through filters */
4
5/*  This file is part of flex. */
6
7/*  Redistribution and use in source and binary forms, with or without */
8/*  modification, are permitted provided that the following conditions */
9/*  are met: */
10
11/*  1. Redistributions of source code must retain the above copyright */
12/*     notice, this list of conditions and the following disclaimer. */
13/*  2. Redistributions in binary form must reproduce the above copyright */
14/*     notice, this list of conditions and the following disclaimer in the */
15/*     documentation and/or other materials provided with the distribution. */
16
17/*  Neither the name of the University nor the names of its contributors */
18/*  may be used to endorse or promote products derived from this software */
19/*  without specific prior written permission. */
20
21/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
22/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
23/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
24/*  PURPOSE. */
25
26#include "flexdef.h"
27static const char * check_4_gnu_m4 =
28    "m4_dnl ifdef(`__gnu__', ,"
29    "`errprint(Flex requires GNU M4. Set the PATH or set the M4 environment variable to its path name.)"
30    " m4exit(2)')\n";
31
32
33/** global chain. */
34struct filter *output_chain = NULL;
35
36/* Allocate and initialize an external filter.
37 * @param chain the current chain or NULL for new chain
38 * @param cmd the command to execute.
39 * @param ... a NULL terminated list of (const char*) arguments to command,
40 *            not including argv[0].
41 * @return newest filter in chain
42 */
43struct filter *filter_create_ext (struct filter *chain, const char *cmd,
44				  ...)
45{
46	struct filter *f;
47	int     max_args;
48	const char *s;
49	va_list ap;
50
51	/* allocate and initialize new filter */
52	f = (struct filter *) flex_alloc (sizeof (struct filter));
53	memset (f, 0, sizeof (*f));
54	f->filter_func = NULL;
55	f->extra = NULL;
56	f->next = NULL;
57	f->argc = 0;
58
59	if (chain != NULL) {
60		/* append f to end of chain */
61		while (chain->next)
62			chain = chain->next;
63		chain->next = f;
64	}
65
66
67	/* allocate argv, and populate it with the argument list. */
68	max_args = 8;
69	f->argv =
70		(const char **) flex_alloc (sizeof (char *) *
71					    (max_args + 1));
72	f->argv[f->argc++] = cmd;
73
74	va_start (ap, cmd);
75	while ((s = va_arg (ap, const char *)) != NULL) {
76		if (f->argc >= max_args) {
77			max_args += 8;
78			f->argv =
79				(const char **) flex_realloc (f->argv,
80							      sizeof (char
81								      *) *
82							      (max_args +
83							       1));
84		}
85		f->argv[f->argc++] = s;
86	}
87	f->argv[f->argc] = NULL;
88
89	va_end (ap);
90	return f;
91}
92
93/* Allocate and initialize an internal filter.
94 * @param chain the current chain or NULL for new chain
95 * @param filter_func The function that will perform the filtering.
96 *        filter_func should return 0 if successful, and -1
97 *        if an error occurs -- or it can simply exit().
98 * @param extra optional user-defined data to pass to the filter.
99 * @return newest filter in chain
100 */
101struct filter *filter_create_int (struct filter *chain,
102				  int (*filter_func) (struct filter *),
103				  void *extra)
104{
105	struct filter *f;
106
107	/* allocate and initialize new filter */
108	f = (struct filter *) flex_alloc (sizeof (struct filter));
109	memset (f, 0, sizeof (*f));
110	f->next = NULL;
111	f->argc = 0;
112	f->argv = NULL;
113
114	f->filter_func = filter_func;
115	f->extra = extra;
116
117	if (chain != NULL) {
118		/* append f to end of chain */
119		while (chain->next)
120			chain = chain->next;
121		chain->next = f;
122	}
123
124	return f;
125}
126
127/** Fork and exec entire filter chain.
128 *  @param chain The head of the chain.
129 *  @return true on success.
130 */
131bool filter_apply_chain (struct filter * chain)
132{
133	int     pid, pipes[2];
134
135	/* Tricky recursion, since we want to begin the chain
136	 * at the END. Why? Because we need all the forked processes
137	 * to be children of the main flex process.
138	 */
139	if (chain)
140		filter_apply_chain (chain->next);
141	else
142		return true;
143
144	/* Now we are the right-most unprocessed link in the chain.
145	 */
146
147	fflush (stdout);
148	fflush (stderr);
149
150	if (pipe (pipes) == -1)
151		flexerror (_("pipe failed"));
152
153	if ((pid = fork ()) == -1)
154		flexerror (_("fork failed"));
155
156	if (pid == 0) {
157		/* child */
158
159        /* We need stdin (the FILE* stdin) to connect to this new pipe.
160         * There is no portable way to set stdin to a new file descriptor,
161         * as stdin is not an lvalue on some systems (BSD).
162         * So we dup the new pipe onto the stdin descriptor and use a no-op fseek
163         * to sync the stream. This is a Hail Mary situation. It seems to work.
164         */
165		close (pipes[1]);
166		if (dup2 (pipes[0], fileno (stdin)) == -1)
167			flexfatal (_("dup2(pipes[0],0)"));
168		close (pipes[0]);
169        fseek (stdin, 0, SEEK_CUR);
170
171		/* run as a filter, either internally or by exec */
172		if (chain->filter_func) {
173			int     r;
174
175			if ((r = chain->filter_func (chain)) == -1)
176				flexfatal (_("filter_func failed"));
177			exit (0);
178		}
179		else {
180			execvp (chain->argv[0],
181				(char **const) (chain->argv));
182			flexfatal (_("exec failed"));
183		}
184
185		exit (1);
186	}
187
188	/* Parent */
189	close (pipes[0]);
190	if (dup2 (pipes[1], fileno (stdout)) == -1)
191		flexfatal (_("dup2(pipes[1],1)"));
192	close (pipes[1]);
193    fseek (stdout, 0, SEEK_CUR);
194
195	return true;
196}
197
198/** Truncate the chain to max_len number of filters.
199 * @param chain the current chain.
200 * @param max_len the maximum length of the chain.
201 * @return the resulting length of the chain.
202 */
203int filter_truncate (struct filter *chain, int max_len)
204{
205	int     len = 1;
206
207	if (!chain)
208		return 0;
209
210	while (chain->next && len < max_len) {
211		chain = chain->next;
212		++len;
213	}
214
215	chain->next = NULL;
216	return len;
217}
218
219/** Splits the chain in order to write to a header file.
220 *  Similar in spirit to the 'tee' program.
221 *  The header file name is in extra.
222 *  @return 0 (zero) on success, and -1 on failure.
223 */
224int filter_tee_header (struct filter *chain)
225{
226	/* This function reads from stdin and writes to both the C file and the
227	 * header file at the same time.
228	 */
229
230	const int readsz = 512;
231	char   *buf;
232	int     to_cfd = -1;
233	FILE   *to_c = NULL, *to_h = NULL;
234	bool    write_header;
235
236	write_header = (chain->extra != NULL);
237
238	/* Store a copy of the stdout pipe, which is already piped to C file
239	 * through the running chain. Then create a new pipe to the H file as
240	 * stdout, and fork the rest of the chain again.
241	 */
242
243	if ((to_cfd = dup (1)) == -1)
244		flexfatal (_("dup(1) failed"));
245	to_c = fdopen (to_cfd, "w");
246
247	if (write_header) {
248		if (freopen ((char *) chain->extra, "w", stdout) == NULL)
249			flexfatal (_("freopen(headerfilename) failed"));
250
251		filter_apply_chain (chain->next);
252		to_h = stdout;
253	}
254
255	/* Now to_c is a pipe to the C branch, and to_h is a pipe to the H branch.
256	 */
257
258	if (write_header) {
259        fputs (check_4_gnu_m4, to_h);
260		fputs ("m4_changecom`'m4_dnl\n", to_h);
261		fputs ("m4_changequote`'m4_dnl\n", to_h);
262		fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_h);
263	    fputs ("m4_define([[M4_YY_NOOP]])[[]]m4_dnl\n", to_h);
264		fputs ("m4_define( [[M4_YY_IN_HEADER]],[[]])m4_dnl\n",
265		       to_h);
266		fprintf (to_h, "#ifndef %sHEADER_H\n", prefix);
267		fprintf (to_h, "#define %sHEADER_H 1\n", prefix);
268		fprintf (to_h, "#define %sIN_HEADER 1\n\n", prefix);
269		fprintf (to_h,
270			 "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
271			 headerfilename ? headerfilename : "<stdout>");
272
273	}
274
275    fputs (check_4_gnu_m4, to_c);
276	fputs ("m4_changecom`'m4_dnl\n", to_c);
277	fputs ("m4_changequote`'m4_dnl\n", to_c);
278	fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_c);
279	fputs ("m4_define([[M4_YY_NOOP]])[[]]m4_dnl\n", to_c);
280	fprintf (to_c, "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
281		 outfilename ? outfilename : "<stdout>");
282
283	buf = (char *) flex_alloc (readsz);
284	while (fgets (buf, readsz, stdin)) {
285		fputs (buf, to_c);
286		if (write_header)
287			fputs (buf, to_h);
288	}
289
290	if (write_header) {
291		fprintf (to_h, "\n");
292
293		/* write a fake line number. It will get fixed by the linedir filter. */
294		fprintf (to_h, "#line 4000 \"M4_YY_OUTFILE_NAME\"\n");
295
296		fprintf (to_h, "#undef %sIN_HEADER\n", prefix);
297		fprintf (to_h, "#endif /* %sHEADER_H */\n", prefix);
298		fputs ("m4_undefine( [[M4_YY_IN_HEADER]])m4_dnl\n", to_h);
299
300		fflush (to_h);
301	    if (ferror (to_h))
302		    lerrsf (_("error writing output file %s"),
303                (char *) chain->extra);
304
305    	else if (fclose (to_h))
306	    	lerrsf (_("error closing output file %s"),
307                (char *) chain->extra);
308	}
309
310	fflush (to_c);
311	if (ferror (to_c))
312		lerrsf (_("error writing output file %s"),
313			outfilename ? outfilename : "<stdout>");
314
315	else if (fclose (to_c))
316		lerrsf (_("error closing output file %s"),
317			outfilename ? outfilename : "<stdout>");
318
319	while (wait (0) > 0) ;
320
321	exit (0);
322	return 0;
323}
324
325/** Adjust the line numbers in the #line directives of the generated scanner.
326 * After the m4 expansion, the line numbers are incorrect since the m4 macros
327 * can add or remove lines.  This only adjusts line numbers for generated code,
328 * not user code. This also happens to be a good place to squeeze multiple
329 * blank lines into a single blank line.
330 */
331int filter_fix_linedirs (struct filter *chain)
332{
333	char   *buf;
334	const int readsz = 512;
335	int     lineno = 1;
336	bool    in_gen = true;	/* in generated code */
337	bool    last_was_blank = false;
338
339	if (!chain)
340		return 0;
341
342	buf = (char *) flex_alloc (readsz);
343
344	while (fgets (buf, readsz, stdin)) {
345
346		regmatch_t m[10];
347
348		/* Check for #line directive. */
349		if (buf[0] == '#'
350		    && regexec (&regex_linedir, buf, 3, m, 0) == 0) {
351
352			int     num;
353			char   *fname;
354
355			/* extract the line number and filename */
356			num = regmatch_strtol (&m[1], buf, NULL, 0);
357			fname = regmatch_dup (&m[2], buf);
358
359			if (strcmp (fname,
360				outfilename ? outfilename : "<stdout>")
361					== 0
362			 || strcmp (fname,
363			 	headerfilename ? headerfilename : "<stdout>")
364					== 0) {
365
366				char    *s1, *s2;
367				char	filename[MAXLINE];
368
369				s1 = fname;
370				s2 = filename;
371
372				while ((s2 - filename) < (MAXLINE - 1) && *s1) {
373					/* Escape the backslash */
374					if (*s1 == '\\')
375						*s2++ = '\\';
376					/* Escape the double quote */
377					if (*s1 == '\"')
378						*s2++ = '\\';
379					/* Copy the character as usual */
380					*s2++ = *s1++;
381				}
382
383				*s2 = '\0';
384
385				/* Adjust the line directives. */
386				in_gen = true;
387				snprintf (buf, readsz, "#line %d \"%s\"\n",
388					  lineno + 1, filename);
389			}
390			else {
391				/* it's a #line directive for code we didn't write */
392				in_gen = false;
393			}
394
395			free (fname);
396			last_was_blank = false;
397		}
398
399		/* squeeze blank lines from generated code */
400		else if (in_gen
401			 && regexec (&regex_blank_line, buf, 0, NULL,
402				     0) == 0) {
403			if (last_was_blank)
404				continue;
405			else
406				last_was_blank = true;
407		}
408
409		else {
410			/* it's a line of normal, non-empty code. */
411			last_was_blank = false;
412		}
413
414		fputs (buf, stdout);
415		lineno++;
416	}
417	fflush (stdout);
418	if (ferror (stdout))
419		lerrsf (_("error writing output file %s"),
420			outfilename ? outfilename : "<stdout>");
421
422	else if (fclose (stdout))
423		lerrsf (_("error closing output file %s"),
424			outfilename ? outfilename : "<stdout>");
425
426	return 0;
427}
428
429/* vim:set expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */
430