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