Deleted Added
sdiff udiff text old ( 228072 ) new ( 250125 )
full compact
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

--- 34 unchanged lines hidden (view full) ---

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 memset (f, 0, sizeof (*f));
52 f->filter_func = NULL;
53 f->extra = NULL;
54 f->next = NULL;
55 f->argc = 0;
56
57 if (chain != NULL) {
58 /* append f to end of chain */
59 while (chain->next)
60 chain = chain->next;
61 chain->next = f;
62 }
63
64
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;
71
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,

--- 21 unchanged lines hidden (view full) ---

99struct filter *filter_create_int (struct filter *chain,
100 int (*filter_func) (struct filter *),
101 void *extra)
102{
103 struct filter *f;
104
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;
111
112 f->filter_func = filter_func;
113 f->extra = extra;
114

--- 9 unchanged lines hidden (view full) ---

124
125/** Fork and exec entire filter chain.
126 * @param chain The head of the chain.
127 * @return true on success.
128 */
129bool filter_apply_chain (struct filter * chain)
130{
131 int pid, pipes[2];
132
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.
136 */
137 if (chain)
138 filter_apply_chain (chain->next);
139 else
140 return true;
141
142 /* Now we are the right-most unprocessed link in the chain.
143 */
144
145 fflush (stdout);
146 fflush (stderr);
147
148 if (pipe (pipes) == -1)
149 flexerror (_("pipe failed"));
150
151 if ((pid = fork ()) == -1)
152 flexerror (_("fork failed"));
153
154 if (pid == 0) {
155 /* child */
156
157 /* We need stdin (the FILE* stdin) to connect to this new pipe.
158 * There is no portable way to set stdin to a new file descriptor,
159 * as stdin is not an lvalue on some systems (BSD).
160 * So we dup the new pipe onto the stdin descriptor and use a no-op fseek
161 * to sync the stream. This is a Hail Mary situation. It seems to work.
162 */
163 close (pipes[1]);
164 if (dup2 (pipes[0], fileno (stdin)) == -1)
165 flexfatal (_("dup2(pipes[0],0)"));
166 close (pipes[0]);
167 fseek (stdin, 0, SEEK_CUR);
168
169 /* run as a filter, either internally or by exec */
170 if (chain->filter_func) {
171 int r;
172
173 if ((r = chain->filter_func (chain)) == -1)
174 flexfatal (_("filter_func failed"));
175 exit (0);
176 }
177 else {
178 execvp (chain->argv[0],
179 (char **const) (chain->argv));
180 flexfatal (_("exec failed"));
181 }
182
183 exit (1);
184 }
185
186 /* Parent */
187 close (pipes[0]);
188 if (dup2 (pipes[1], fileno (stdout)) == -1)

--- 85 unchanged lines hidden (view full) ---

274 fputs ("m4_changecom`'m4_dnl\n", to_c);
275 fputs ("m4_changequote`'m4_dnl\n", to_c);
276 fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_c);
277 fputs ("m4_define([[M4_YY_NOOP]])[[]]m4_dnl\n", to_c);
278 fprintf (to_c, "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n",
279 outfilename ? outfilename : "<stdout>");
280
281 buf = (char *) flex_alloc (readsz);
282 while (fgets (buf, readsz, stdin)) {
283 fputs (buf, to_c);
284 if (write_header)
285 fputs (buf, to_h);
286 }
287
288 if (write_header) {
289 fprintf (to_h, "\n");
290
291 /* write a fake line number. It will get fixed by the linedir filter. */
292 fprintf (to_h, "#line 4000 \"M4_YY_OUTFILE_NAME\"\n");
293
294 fprintf (to_h, "#undef %sIN_HEADER\n", prefix);
295 fprintf (to_h, "#endif /* %sHEADER_H */\n", prefix);
296 fputs ("m4_undefine( [[M4_YY_IN_HEADER]])m4_dnl\n", to_h);
297
298 fflush (to_h);
299 if (ferror (to_h))
300 lerrsf (_("error writing output file %s"),
301 (char *) chain->extra);
302
303 else if (fclose (to_h))
304 lerrsf (_("error closing output file %s"),
305 (char *) chain->extra);
306 }
307
308 fflush (to_c);
309 if (ferror (to_c))
310 lerrsf (_("error writing output file %s"),
311 outfilename ? outfilename : "<stdout>");
312
313 else if (fclose (to_c))

--- 19 unchanged lines hidden (view full) ---

333 int lineno = 1;
334 bool in_gen = true; /* in generated code */
335 bool last_was_blank = false;
336
337 if (!chain)
338 return 0;
339
340 buf = (char *) flex_alloc (readsz);
341
342 while (fgets (buf, readsz, stdin)) {
343
344 regmatch_t m[10];
345
346 /* Check for #line directive. */
347 if (buf[0] == '#'
348 && regexec (&regex_linedir, buf, 3, m, 0) == 0) {
349
350 int num;
351 char *fname;
352
353 /* extract the line number and filename */
354 num = regmatch_strtol (&m[1], buf, NULL, 0);
355 fname = regmatch_dup (&m[2], buf);
356

--- 71 unchanged lines hidden ---