summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Millaway <john43@users.sourceforge.net>2006-03-21 18:23:06 +0000
committerJohn Millaway <john43@users.sourceforge.net>2006-03-21 18:23:06 +0000
commit6f9c1218cbcd2ea3eb441ccc312780108db937b8 (patch)
tree0ba0af979f9b5667469667b681ca955e6da3ec18
parent81351ccb95e44db2c7d161c172b8f6e280ae457a (diff)
Moved set_input_file to different file.
-rw-r--r--filter.c81
-rw-r--r--flexdef.h2
-rw-r--r--main.c2
-rw-r--r--scan.l22
4 files changed, 65 insertions, 42 deletions
diff --git a/filter.c b/filter.c
index 416b431..409f044 100644
--- a/filter.c
+++ b/filter.c
@@ -126,7 +126,7 @@ struct filter *filter_create_int (struct filter *chain,
* @param chain The head of the chain.
* @return true on success.
*/
-bool filter_apply_chain (struct filter * chain)
+bool filter_apply_chain (struct filter * chain, const bool parent_to_child)
{
int pid, pipes[2];
@@ -135,7 +135,7 @@ bool filter_apply_chain (struct filter * chain)
* to be children of the main flex process.
*/
if (chain)
- filter_apply_chain (chain->next);
+ filter_apply_chain (chain->next, parent_to_child);
else
return true;
@@ -154,17 +154,29 @@ bool filter_apply_chain (struct filter * chain)
if (pid == 0) {
/* child */
- /* We need stdin (the FILE* stdin) to connect to this new pipe.
- * There is no portable way to set stdin to a new file descriptor,
- * as stdin is not an lvalue on some systems (BSD).
- * So we dup the new pipe onto the stdin descriptor and use a no-op fseek
- * to sync the stream. This is a Hail Mary situation. It seems to work.
+ /* For the parent_to_child direction, we need stdin (the FILE* stdin)
+ * to connect to this new pipe. There is no portable way to set stdin
+ * to a new file descriptor, as stdin is not an lvalue on some systems
+ * (BSD). So we dup the new pipe onto the stdin descriptor and use a
+ * no-op fseek to sync the stream. This is a Hail Mary situation. It
+ * seems to work. Note that the same concept applies, but opposite,
+ * for child_to_parent filters.
*/
- close (pipes[1]);
- if (dup2 (pipes[0], fileno (stdin)) == -1)
- flexfatal (_("dup2(pipes[0],0)"));
- close (pipes[0]);
- fseek (stdin, 0, SEEK_CUR);
+
+ if (parent_to_child){
+ close (pipes[1]);
+ if (dup2 (pipes[0], fileno (stdin)) == -1)
+ flexfatal (_("dup2(pipes[0],0)"));
+ close (pipes[0]);
+ fseek (stdin, 0, SEEK_CUR);
+ }
+ else{
+ close (pipes[0]);
+ if (dup2 (pipes[1], fileno (stdout)) == -1)
+ flexfatal (_("dup2(pipes[1],1)"));
+ close (pipes[1]);
+ fseek (stdout, 0, SEEK_CUR);
+ }
/* run as a filter, either internally or by exec */
if (chain->filter_func) {
@@ -184,11 +196,20 @@ bool filter_apply_chain (struct filter * chain)
}
/* Parent */
- close (pipes[0]);
- if (dup2 (pipes[1], fileno (stdout)) == -1)
- flexfatal (_("dup2(pipes[1],1)"));
- close (pipes[1]);
- fseek (stdout, 0, SEEK_CUR);
+ if (parent_to_child){
+ close (pipes[0]);
+ if (dup2 (pipes[1], fileno (stdout)) == -1)
+ flexfatal (_("dup2(pipes[1],1)"));
+ close (pipes[1]);
+ fseek (stdout, 0, SEEK_CUR);
+ }
+ else{
+ close (pipes[1]);
+ if (dup2 (pipes[0], fileno (stdin)) == -1)
+ flexfatal (_("dup2(pipes[0],0)"));
+ close (pipes[0]);
+ fseek (stdin, 0, SEEK_CUR);
+ }
return true;
}
@@ -246,7 +267,7 @@ int filter_tee_header (struct filter *chain)
if (freopen ((char *) chain->extra, "w", stdout) == NULL)
flexfatal (_("freopen(headerfilename) failed"));
- filter_apply_chain (chain->next);
+ filter_apply_chain (chain->next, true);
to_h = stdout;
}
@@ -403,4 +424,28 @@ int filter_fix_linedirs (struct filter *chain)
return 0;
}
+/* set_input_file - open the given file (if NULL, stdin) for scanning */
+
+void set_input_file( file )
+char *file;
+{
+ /* Preprocess the input to quote m4 escapes.*/
+ if ( file && strcmp( file, "-" ) )
+ {
+ infilename = copy_string( file );
+ yyin = fopen( infilename, "r" );
+
+ if ( yyin == NULL )
+ lerrsf( _( "can't open %s" ), file );
+ }
+
+ else
+ {
+ yyin = stdin;
+ infilename = copy_string( "<stdin>" );
+ }
+
+ linenum = 1;
+}
+
/* vim:set expandtab cindent tabstop=4 softtabstop=4 shiftwidth=4 textwidth=0: */
diff --git a/flexdef.h b/flexdef.h
index 8e99bb8..322b554 100644
--- a/flexdef.h
+++ b/flexdef.h
@@ -1160,7 +1160,7 @@ extern struct filter *filter_create_ext PROTO((struct filter * chain, const char
struct filter *filter_create_int PROTO((struct filter *chain,
int (*filter_func) (struct filter *),
void *extra));
-extern bool filter_apply_chain PROTO((struct filter * chain));
+extern bool filter_apply_chain PROTO((struct filter * chain, const bool parent_to_child));
extern int filter_truncate (struct filter * chain, int max_len);
extern int filter_tee_header PROTO((struct filter *chain));
extern int filter_fix_linedirs PROTO((struct filter *chain));
diff --git a/main.c b/main.c
index f28c5da..8a5d494 100644
--- a/main.c
+++ b/main.c
@@ -367,7 +367,7 @@ void check_options ()
/* For debugging, only run the requested number of filters. */
if (preproc_level > 0) {
filter_truncate(output_chain, preproc_level);
- filter_apply_chain(output_chain);
+ filter_apply_chain(output_chain, true);
}
yyout = stdout;
diff --git a/scan.l b/scan.l
index fe9a0fc..831f291 100644
--- a/scan.l
+++ b/scan.l
@@ -835,28 +835,6 @@ int yywrap()
}
-/* set_input_file - open the given file (if NULL, stdin) for scanning */
-
-void set_input_file( file )
-char *file;
- {
- if ( file && strcmp( file, "-" ) )
- {
- infilename = copy_string( file );
- yyin = fopen( infilename, "r" );
-
- if ( yyin == NULL )
- lerrsf( _( "can't open %s" ), file );
- }
-
- else
- {
- yyin = stdin;
- infilename = copy_string( "<stdin>" );
- }
-
- linenum = 1;
- }
/* Wrapper routines for accessing the scanner's malloc routines. */