1 /* filter - postprocessing of flex output through filters */
3 /* This file is part of flex. */
5 /* Redistribution and use in source and binary forms, with or without */
6 /* modification, are permitted provided that the following conditions */
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. */
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. */
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 */
25 static 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.)"
32 struct filter
*output_chain
= NULL
;
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
41 struct filter
*filter_create_ext (struct filter
*chain
, const char *cmd
,
49 /* allocate and initialize new filter */
50 f
= (struct filter
*) flex_alloc (sizeof (struct filter
));
51 memset (f
, 0, sizeof (*f
));
52 f
->filter_func
= NULL
;
58 /* append f to end of chain */
65 /* allocate argv, and populate it with the argument list. */
68 (const char **) flex_alloc (sizeof (char *) *
70 f
->argv
[f
->argc
++] = cmd
;
73 while ((s
= va_arg (ap
, const char *)) != NULL
) {
74 if (f
->argc
>= max_args
) {
77 (const char **) flex_realloc (f
->argv
,
83 f
->argv
[f
->argc
++] = s
;
85 f
->argv
[f
->argc
] = NULL
;
91 /* Allocate and initialize an internal filter.
92 * @param chain the current chain or NULL for new chain
93 * @param filter_func The function that will perform the filtering.
94 * filter_func should return 0 if successful, and -1
95 * if an error occurs -- or it can simply exit().
96 * @param extra optional user-defined data to pass to the filter.
97 * @return newest filter in chain
99 struct filter
*filter_create_int (struct filter
*chain
,
100 int (*filter_func
) (struct filter
*),
105 /* allocate and initialize new filter */
106 f
= (struct filter
*) flex_alloc (sizeof (struct filter
));
107 memset (f
, 0, sizeof (*f
));
112 f
->filter_func
= filter_func
;
116 /* append f to end of chain */
125 /** Fork and exec entire filter chain.
126 * @param chain The head of the chain.
127 * @return true on success.
129 bool filter_apply_chain (struct filter
* chain
)
133 /* Tricky recursion, since we want to begin the chain
134 * at the END. Why? Because we need all the forked processes
135 * to be children of the main flex process.
138 filter_apply_chain (chain
->next
);
142 /* Now we are the right-most unprocessed link in the chain.
148 if (pipe (pipes
) == -1)
149 flexerror (_("pipe failed"));
151 if ((pid
= fork ()) == -1)
152 flexerror (_("fork failed"));
157 if (dup2 (pipes
[0], 0) == -1)
158 flexfatal (_("dup2(pipes[0],0)"));
161 /* run as a filter, either internally or by exec */
162 if (chain
->filter_func
) {
165 /* setup streams again */
166 if ( ! fdopen (0, "r"))
167 flexfatal (_("fdopen(0) failed"));
168 if (!fdopen (1, "w"))
169 flexfatal (_("fdopen(1) failed"));
171 if ((r
= chain
->filter_func (chain
)) == -1)
172 flexfatal (_("filter_func failed"));
176 execvp (chain
->argv
[0],
177 (char **const) (chain
->argv
));
178 flexfatal (_("exec failed"));
186 if (dup2 (pipes
[1], 1) == -1)
187 flexfatal (_("dup2(pipes[1],1)"));
189 if ( !fdopen (1, "w"))
190 flexfatal (_("fdopen(1) failed"));
195 /** Truncate the chain to max_len number of filters.
196 * @param chain the current chain.
197 * @param max_len the maximum length of the chain.
198 * @return the resulting length of the chain.
200 int filter_truncate (struct filter
*chain
, int max_len
)
207 while (chain
->next
&& len
< max_len
) {
216 /** Splits the chain in order to write to a header file.
217 * Similar in spirit to the 'tee' program.
218 * The header file name is in extra.
219 * @return 0 (zero) on success, and -1 on failure.
221 int filter_tee_header (struct filter
*chain
)
223 /* This function reads from stdin and writes to both the C file and the
224 * header file at the same time.
227 const int readsz
= 512;
230 FILE *to_c
= NULL
, *to_h
= NULL
;
233 write_header
= (chain
->extra
!= NULL
);
235 /* Store a copy of the stdout pipe, which is already piped to C file
236 * through the running chain. Then create a new pipe to the H file as
237 * stdout, and fork the rest of the chain again.
240 if ((to_cfd
= dup (1)) == -1)
241 flexfatal (_("dup(1) failed"));
242 to_c
= fdopen (to_cfd
, "w");
245 if (freopen ((char *) chain
->extra
, "w", stdout
) == NULL
)
246 flexfatal (_("freopen(headerfilename) failed"));
248 filter_apply_chain (chain
->next
);
252 /* Now to_c is a pipe to the C branch, and to_h is a pipe to the H branch.
256 fputs (check_4_gnu_m4
, to_h
);
257 fputs ("m4_changecom`'m4_dnl\n", to_h
);
258 fputs ("m4_changequote`'m4_dnl\n", to_h
);
259 fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_h
);
260 fputs ("m4_define( [[M4_YY_IN_HEADER]],[[]])m4_dnl\n",
262 fprintf (to_h
, "#ifndef %sHEADER_H\n", prefix
);
263 fprintf (to_h
, "#define %sHEADER_H 1\n", prefix
);
264 fprintf (to_h
, "#define %sIN_HEADER 1\n\n", prefix
);
266 "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
267 headerfilename
? headerfilename
: "<stdout>");
271 fputs (check_4_gnu_m4
, to_c
);
272 fputs ("m4_changecom`'m4_dnl\n", to_c
);
273 fputs ("m4_changequote`'m4_dnl\n", to_c
);
274 fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_c
);
275 fprintf (to_c
, "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
276 outfilename
? outfilename
: "<stdout>");
278 buf
= (char *) flex_alloc (readsz
);
279 while (fgets (buf
, readsz
, stdin
)) {
286 fprintf (to_h
, "\n");
288 /* write a fake line number. It will get fixed by the linedir filter. */
289 fprintf (to_h
, "#line 4000 \"M4_YY_OUTFILE_NAME\"\n");
291 fprintf (to_h
, "#undef %sIN_HEADER\n", prefix
);
292 fprintf (to_h
, "#endif /* %sHEADER_H */\n", prefix
);
293 fputs ("m4_undefine( [[M4_YY_IN_HEADER]])m4_dnl\n", to_h
);
297 lerrsf (_("error writing output file %s"),
298 (char *) chain
->extra
);
300 else if (fclose (to_h
))
301 lerrsf (_("error closing output file %s"),
302 (char *) chain
->extra
);
307 lerrsf (_("error writing output file %s"),
308 outfilename
? outfilename
: "<stdout>");
310 else if (fclose (to_c
))
311 lerrsf (_("error closing output file %s"),
312 outfilename
? outfilename
: "<stdout>");
314 while (wait (0) > 0) ;
320 /** Adjust the line numbers in the #line directives of the generated scanner.
321 * After the m4 expansion, the line numbers are incorrect since the m4 macros
322 * can add or remove lines. This only adjusts line numbers for generated code,
323 * not user code. This also happens to be a good place to squeeze multiple
324 * blank lines into a single blank line.
326 int filter_fix_linedirs (struct filter
*chain
)
329 const int readsz
= 512;
331 bool in_gen
= true; /* in generated code */
332 bool last_was_blank
= false;
337 buf
= (char *) flex_alloc (readsz
);
339 while (fgets (buf
, readsz
, stdin
)) {
343 /* Check for #line directive. */
345 && regexec (®ex_linedir
, buf
, 3, m
, 0) == 0) {
350 /* extract the line number and filename */
351 num
= regmatch_strtol (&m
[1], buf
, NULL
, 0);
352 fname
= regmatch_dup (&m
[2], buf
);
355 (fname
, outfilename
? outfilename
: "<stdout>")
358 headerfilename
? headerfilename
:
360 /* Adjust the line directives. */
362 sprintf (buf
, "#line %d \"%s\"\n",
368 /* it's a #line directive for code we didn't write */
372 last_was_blank
= false;
375 /* squeeze blank lines from generated code */
377 && regexec (®ex_blank_line
, buf
, 0, NULL
,
382 last_was_blank
= true;
386 /* it's a line of normal, non-empty code. */
387 last_was_blank
= false;
395 lerrsf (_("error writing output file %s"),
396 outfilename
? outfilename
: "<stdout>");
398 else if (fclose (stdout
))
399 lerrsf (_("error closing output file %s"),
400 outfilename
? outfilename
: "<stdout>");
405 /* vim:set expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */