]> granicus.if.org Git - flex/commitdiff
Moved set_input_file to different file.
authorJohn Millaway <john43@users.sourceforge.net>
Tue, 21 Mar 2006 18:23:06 +0000 (18:23 +0000)
committerJohn Millaway <john43@users.sourceforge.net>
Tue, 21 Mar 2006 18:23:06 +0000 (18:23 +0000)
filter.c
flexdef.h
main.c
scan.l

index 416b431ea9c4e6a2384557a3c2bfbc2ccd9f7bd4..409f044c0e167499f13ff2346ae2e8b25d5c911e 100644 (file)
--- 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: */
index 8e99bb8a6dc9e5ef2ee29de3b6d9d4daf584366d..322b5545bc50a354a568e0ed4b42c923dc832d4e 100644 (file)
--- 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 f28c5da90116583c573bb0e9a4905dc5a80c0295..8a5d494a3bd78c04f6fde24bee7503d915069013 100644 (file)
--- 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 fe9a0fc5018d7ceb32434e7c081bc4b49c45cb8e..831f291ff48c422f57e55f3047b2f270fa726ad0 100644 (file)
--- 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. */