3 /* filter - postprocessing of flex output through filters */
5 /* This file is part of flex. */
7 /* Redistribution and use in source and binary forms, with or without */
8 /* modification, are permitted provided that the following conditions */
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. */
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. */
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 */
27 static 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.)"
34 struct filter
*output_chain
= NULL
;
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
43 struct filter
*filter_create_ext (struct filter
*chain
, const char *cmd
,
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
;
60 /* append f to end of chain */
67 /* allocate argv, and populate it with the argument list. */
70 (const char **) flex_alloc (sizeof (char *) *
72 f
->argv
[f
->argc
++] = cmd
;
75 while ((s
= va_arg (ap
, const char *)) != NULL
) {
76 if (f
->argc
>= max_args
) {
79 (const char **) flex_realloc (f
->argv
,
85 f
->argv
[f
->argc
++] = s
;
87 f
->argv
[f
->argc
] = NULL
;
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
101 struct filter
*filter_create_int (struct filter
*chain
,
102 int (*filter_func
) (struct filter
*),
107 /* allocate and initialize new filter */
108 f
= (struct filter
*) flex_alloc (sizeof (struct filter
));
109 memset (f
, 0, sizeof (*f
));
114 f
->filter_func
= filter_func
;
118 /* append f to end of chain */
127 /** Fork and exec entire filter chain.
128 * @param chain The head of the chain.
129 * @return true on success.
131 bool filter_apply_chain (struct filter
* chain
)
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.
140 filter_apply_chain (chain
->next
);
144 /* Now we are the right-most unprocessed link in the chain.
150 if (pipe (pipes
) == -1)
151 flexerror (_("pipe failed"));
153 if ((pid
= fork ()) == -1)
154 flexerror (_("fork failed"));
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.
166 if (dup2 (pipes
[0], fileno (stdin
)) == -1)
167 flexfatal (_("dup2(pipes[0],0)"));
169 fseek (stdin
, 0, SEEK_CUR
);
171 /* run as a filter, either internally or by exec */
172 if (chain
->filter_func
) {
175 if ((r
= chain
->filter_func (chain
)) == -1)
176 flexfatal (_("filter_func failed"));
180 execvp (chain
->argv
[0],
181 (char **const) (chain
->argv
));
182 flexfatal (_("exec failed"));
190 if (dup2 (pipes
[1], fileno (stdout
)) == -1)
191 flexfatal (_("dup2(pipes[1],1)"));
193 fseek (stdout
, 0, SEEK_CUR
);
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.
203 int filter_truncate (struct filter
*chain
, int max_len
)
210 while (chain
->next
&& len
< max_len
) {
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.
224 int filter_tee_header (struct filter
*chain
)
226 /* This function reads from stdin and writes to both the C file and the
227 * header file at the same time.
230 const int readsz
= 512;
233 FILE *to_c
= NULL
, *to_h
= NULL
;
236 write_header
= (chain
->extra
!= NULL
);
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.
243 if ((to_cfd
= dup (1)) == -1)
244 flexfatal (_("dup(1) failed"));
245 to_c
= fdopen (to_cfd
, "w");
248 if (freopen ((char *) chain
->extra
, "w", stdout
) == NULL
)
249 flexfatal (_("freopen(headerfilename) failed"));
251 filter_apply_chain (chain
->next
);
255 /* Now to_c is a pipe to the C branch, and to_h is a pipe to the H branch.
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",
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
);
270 "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
271 headerfilename
? headerfilename
: "<stdout>");
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>");
283 buf
= (char *) flex_alloc (readsz
);
284 while (fgets (buf
, readsz
, stdin
)) {
291 fprintf (to_h
, "\n");
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");
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
);
302 lerrsf (_("error writing output file %s"),
303 (char *) chain
->extra
);
305 else if (fclose (to_h
))
306 lerrsf (_("error closing output file %s"),
307 (char *) chain
->extra
);
312 lerrsf (_("error writing output file %s"),
313 outfilename
? outfilename
: "<stdout>");
315 else if (fclose (to_c
))
316 lerrsf (_("error closing output file %s"),
317 outfilename
? outfilename
: "<stdout>");
319 while (wait (0) > 0) ;
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.
331 int filter_fix_linedirs (struct filter
*chain
)
334 const int readsz
= 512;
336 bool in_gen
= true; /* in generated code */
337 bool last_was_blank
= false;
342 buf
= (char *) flex_alloc (readsz
);
344 while (fgets (buf
, readsz
, stdin
)) {
348 /* Check for #line directive. */
350 && regexec (®ex_linedir
, buf
, 3, m
, 0) == 0) {
355 /* extract the line number and filename */
356 num
= regmatch_strtol (&m
[1], buf
, NULL
, 0);
357 fname
= regmatch_dup (&m
[2], buf
);
360 outfilename
? outfilename
: "<stdout>")
363 headerfilename
? headerfilename
: "<stdout>")
367 char filename
[MAXLINE
];
372 while ((s2
- filename
) < (MAXLINE
- 1) && *s1
) {
373 /* Escape the backslash */
376 /* Escape the double quote */
379 /* Copy the character as usual */
385 /* Adjust the line directives. */
387 snprintf (buf
, readsz
, "#line %d \"%s\"\n",
388 lineno
+ 1, filename
);
391 /* it's a #line directive for code we didn't write */
396 last_was_blank
= false;
399 /* squeeze blank lines from generated code */
401 && regexec (®ex_blank_line
, buf
, 0, NULL
,
406 last_was_blank
= true;
410 /* it's a line of normal, non-empty code. */
411 last_was_blank
= false;
419 lerrsf (_("error writing output file %s"),
420 outfilename
? outfilename
: "<stdout>");
422 else if (fclose (stdout
))
423 lerrsf (_("error closing output file %s"),
424 outfilename
? outfilename
: "<stdout>");
429 /* vim:set expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */