filter.c (228072) | filter.c (250125) |
---|---|
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)); | 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 if (!f) 52 flexerror (_("flex_alloc failed (f) in filter_create_ext")); |
|
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)); | 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")); |
|
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)); | 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, --- 21 unchanged lines hidden (view full) --- 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")); |
|
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]; | 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 --- 9 unchanged lines hidden (view full) --- 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 int r; 139 const int readsz = 512; 140 char *buf; |
|
132 | 141 |
142 |
|
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 | 143 /* Tricky recursion, since we want to begin the chain 144 * at the END. Why? Because we need all the forked processes 145 * to be children of the main flex process. 146 */ 147 if (chain) 148 filter_apply_chain (chain->next); 149 else 150 return true; 151 152 /* Now we are the right-most unprocessed link in the chain. 153 */ 154 155 fflush (stdout); 156 fflush (stderr); 157 |
158 |
|
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]); | 159 if (pipe (pipes) == -1) 160 flexerror (_("pipe failed")); 161 162 if ((pid = fork ()) == -1) 163 flexerror (_("fork failed")); 164 165 if (pid == 0) { 166 /* child */ 167 168 /* We need stdin (the FILE* stdin) to connect to this new pipe. 169 * There is no portable way to set stdin to a new file descriptor, 170 * as stdin is not an lvalue on some systems (BSD). 171 * So we dup the new pipe onto the stdin descriptor and use a no-op fseek 172 * to sync the stream. This is a Hail Mary situation. It seems to work. 173 */ 174 close (pipes[1]); |
175clearerr(stdin); |
|
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)); | 176 if (dup2 (pipes[0], fileno (stdin)) == -1) 177 flexfatal (_("dup2(pipes[0],0)")); 178 close (pipes[0]); 179 fseek (stdin, 0, SEEK_CUR); 180 181 /* run as a filter, either internally or by exec */ 182 if (chain->filter_func) { 183 int r; 184 185 if ((r = chain->filter_func (chain)) == -1) 186 flexfatal (_("filter_func failed")); 187 exit (0); 188 } 189 else { 190 execvp (chain->argv[0], 191 (char **const) (chain->argv)); |
180 flexfatal (_("exec failed")); | 192 lerrsf_fatal ( _("exec of %s failed"), 193 chain->argv[0]); |
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); | 194 } 195 196 exit (1); 197 } 198 199 /* Parent */ 200 close (pipes[0]); 201 if (dup2 (pipes[1], fileno (stdout)) == -1) --- 85 unchanged lines hidden (view full) --- 287 fputs ("m4_changecom`'m4_dnl\n", to_c); 288 fputs ("m4_changequote`'m4_dnl\n", to_c); 289 fputs ("m4_changequote([[,]])[[]]m4_dnl\n", to_c); 290 fputs ("m4_define([[M4_YY_NOOP]])[[]]m4_dnl\n", to_c); 291 fprintf (to_c, "m4_define( [[M4_YY_OUTFILE_NAME]],[[%s]])m4_dnl\n", 292 outfilename ? outfilename : "<stdout>"); 293 294 buf = (char *) flex_alloc (readsz); |
295 if (!buf) 296 flexerror (_("flex_alloc failed in filter_tee_header")); |
|
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); | 297 while (fgets (buf, readsz, stdin)) { 298 fputs (buf, to_c); 299 if (write_header) 300 fputs (buf, to_h); 301 } 302 303 if (write_header) { 304 fprintf (to_h, "\n"); 305 306 /* write a fake line number. It will get fixed by the linedir filter. */ 307 fprintf (to_h, "#line 4000 \"M4_YY_OUTFILE_NAME\"\n"); 308 309 fprintf (to_h, "#undef %sIN_HEADER\n", prefix); 310 fprintf (to_h, "#endif /* %sHEADER_H */\n", prefix); 311 fputs ("m4_undefine( [[M4_YY_IN_HEADER]])m4_dnl\n", to_h); 312 313 fflush (to_h); |
299 if (ferror (to_h)) 300 lerrsf (_("error writing output file %s"), 301 (char *) chain->extra); | 314 if (ferror (to_h)) 315 lerrsf (_("error writing output file %s"), 316 (char *) chain->extra); |
302 | 317 |
303 else if (fclose (to_h)) 304 lerrsf (_("error closing output file %s"), 305 (char *) chain->extra); | 318 else if (fclose (to_h)) 319 lerrsf (_("error closing output file %s"), 320 (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); | 321 } 322 323 fflush (to_c); 324 if (ferror (to_c)) 325 lerrsf (_("error writing output file %s"), 326 outfilename ? outfilename : "<stdout>"); 327 328 else if (fclose (to_c)) --- 19 unchanged lines hidden (view full) --- 348 int lineno = 1; 349 bool in_gen = true; /* in generated code */ 350 bool last_was_blank = false; 351 352 if (!chain) 353 return 0; 354 355 buf = (char *) flex_alloc (readsz); |
356 if (!buf) 357 flexerror (_("flex_alloc failed in filter_fix_linedirs")); |
|
341 342 while (fgets (buf, readsz, stdin)) { 343 344 regmatch_t m[10]; 345 346 /* Check for #line directive. */ 347 if (buf[0] == '#' | 358 359 while (fgets (buf, readsz, stdin)) { 360 361 regmatch_t m[10]; 362 363 /* Check for #line directive. */ 364 if (buf[0] == '#' |
348 && regexec (®ex_linedir, buf, 3, m, 0) == 0) { | 365 && regexec (®ex_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 --- | 366 367 int num; 368 char *fname; 369 370 /* extract the line number and filename */ 371 num = regmatch_strtol (&m[1], buf, NULL, 0); 372 fname = regmatch_dup (&m[2], buf); 373 --- 71 unchanged lines hidden --- |