FYI: Reply from HP-UX
[git/dscho.git] / flex-2.5.33 / filter.c
blob001872132867d2e89cb51e4159ba076ae1e05b77
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 */
7 /* are met: */
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 */
22 /* PURPOSE. */
24 #include "flexdef.h"
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.)"
28 " m4exit(2)')\n";
31 /** global chain. */
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,
42 ...)
44 struct filter *f;
45 int max_args;
46 const char *s;
47 va_list ap;
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;
53 f->extra = NULL;
54 f->next = NULL;
55 f->argc = 0;
57 if (chain != NULL) {
58 /* append f to end of chain */
59 while (chain->next)
60 chain = chain->next;
61 chain->next = f;
65 /* allocate argv, and populate it with the argument list. */
66 max_args = 8;
67 f->argv =
68 (const char **) flex_alloc (sizeof (char *) *
69 (max_args + 1));
70 f->argv[f->argc++] = cmd;
72 va_start (ap, cmd);
73 while ((s = va_arg (ap, const char *)) != NULL) {
74 if (f->argc >= max_args) {
75 max_args += 8;
76 f->argv =
77 (const char **) flex_realloc (f->argv,
78 sizeof (char
79 *) *
80 (max_args +
81 1));
83 f->argv[f->argc++] = s;
85 f->argv[f->argc] = NULL;
87 va_end (ap);
88 return f;
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 *),
101 void *extra)
103 struct filter *f;
105 /* allocate and initialize new filter */
106 f = (struct filter *) flex_alloc (sizeof (struct filter));
107 memset (f, 0, sizeof (*f));
108 f->next = NULL;
109 f->argc = 0;
110 f->argv = NULL;
112 f->filter_func = filter_func;
113 f->extra = extra;
115 if (chain != NULL) {
116 /* append f to end of chain */
117 while (chain->next)
118 chain = chain->next;
119 chain->next = f;
122 return f;
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)
131 int pid, pipes[2];
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.
137 if (chain)
138 filter_apply_chain (chain->next);
139 else
140 return true;
142 /* Now we are the right-most unprocessed link in the chain.
145 fflush (stdout);
146 fflush (stderr);
148 if (pipe (pipes) == -1)
149 flexerror (_("pipe failed"));
151 if ((pid = fork ()) == -1)
152 flexerror (_("fork failed"));
154 if (pid == 0) {
155 /* child */
156 close (pipes[1]);
157 if (dup2 (pipes[0], 0) == -1)
158 flexfatal (_("dup2(pipes[0],0)"));
159 close (pipes[0]);
161 /* run as a filter, either internally or by exec */
162 if (chain->filter_func) {
163 int r;
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"));
173 exit (0);
175 else {
176 execvp (chain->argv[0],
177 (char **const) (chain->argv));
178 flexfatal (_("exec failed"));
181 exit (1);
184 /* Parent */
185 close (pipes[0]);
186 if (dup2 (pipes[1], 1) == -1)
187 flexfatal (_("dup2(pipes[1],1)"));
188 close (pipes[1]);
189 if ( !fdopen (1, "w"))
190 flexfatal (_("fdopen(1) failed"));
192 return true;
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)
202 int len = 1;
204 if (!chain)
205 return 0;
207 while (chain->next && len < max_len) {
208 chain = chain->next;
209 ++len;
212 chain->next = NULL;
213 return 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;
228 char *buf;
229 int to_cfd = -1;
230 FILE *to_c = NULL, *to_h = NULL;
231 bool write_header;
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");
244 if (write_header) {
245 if (freopen ((char *) chain->extra, "w", stdout) == NULL)
246 flexfatal (_("freopen(headerfilename) failed"));
248 filter_apply_chain (chain->next);
249 to_h = stdout;
252 /* Now to_c is a pipe to the C branch, and to_h is a pipe to the H branch.
255 if (write_header) {
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",
261 to_h);
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);
265 fprintf (to_h,
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)) {
280 fputs (buf, to_c);
281 if (write_header)
282 fputs (buf, to_h);
285 if (write_header) {
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);
295 fflush (to_h);
296 if (ferror (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);
305 fflush (to_c);
306 if (ferror (to_c))
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) ;
316 exit (0);
317 return 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)
328 char *buf;
329 const int readsz = 512;
330 int lineno = 1;
331 bool in_gen = true; /* in generated code */
332 bool last_was_blank = false;
334 if (!chain)
335 return 0;
337 buf = (char *) flex_alloc (readsz);
339 while (fgets (buf, readsz, stdin)) {
341 regmatch_t m[10];
343 /* Check for #line directive. */
344 if (buf[0] == '#'
345 && regexec (&regex_linedir, buf, 3, m, 0) == 0) {
347 int num;
348 char *fname;
350 /* extract the line number and filename */
351 num = regmatch_strtol (&m[1], buf, NULL, 0);
352 fname = regmatch_dup (&m[2], buf);
354 if (strcmp
355 (fname, outfilename ? outfilename : "<stdout>")
356 == 0
357 || strcmp (fname,
358 headerfilename ? headerfilename :
359 "<stdout>") == 0) {
360 /* Adjust the line directives. */
361 in_gen = true;
362 sprintf (buf, "#line %d \"%s\"\n",
363 lineno + 1, fname);
364 free (fname);
367 else {
368 /* it's a #line directive for code we didn't write */
369 in_gen = false;
372 last_was_blank = false;
375 /* squeeze blank lines from generated code */
376 else if (in_gen
377 && regexec (&regex_blank_line, buf, 0, NULL,
378 0) == 0) {
379 if (last_was_blank)
380 continue;
381 else
382 last_was_blank = true;
385 else {
386 /* it's a line of normal, non-empty code. */
387 last_was_blank = false;
390 fputs (buf, stdout);
391 lineno++;
393 fflush (stdout);
394 if (ferror (stdout))
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>");
402 return 0;
405 /* vim:set expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */