]> granicus.if.org Git - flex/commitdiff
Documented filter chain. Removed fdopen. Added no-op fseek.
authorJohn Millaway <john43@users.sourceforge.net>
Mon, 20 Mar 2006 01:50:44 +0000 (01:50 +0000)
committerJohn Millaway <john43@users.sourceforge.net>
Mon, 20 Mar 2006 01:50:44 +0000 (01:50 +0000)
filter.c

index 001872132867d2e89cb51e4159ba076ae1e05b77..416b431ea9c4e6a2384557a3c2bfbc2ccd9f7bd4 100644 (file)
--- a/filter.c
+++ b/filter.c
@@ -153,21 +153,23 @@ 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.
+         */
                close (pipes[1]);
-               if (dup2 (pipes[0], 0) == -1)
+               if (dup2 (pipes[0], fileno (stdin)) == -1)
                        flexfatal (_("dup2(pipes[0],0)"));
                close (pipes[0]);
+        fseek (stdin, 0, SEEK_CUR);
 
                /* run as a filter, either internally or by exec */
                if (chain->filter_func) {
                        int     r;
 
-                       /* setup streams again */
-                       if ( ! fdopen (0, "r"))
-                               flexfatal (_("fdopen(0) failed"));
-                       if (!fdopen (1, "w"))
-                               flexfatal (_("fdopen(1) failed"));
-
                        if ((r = chain->filter_func (chain)) == -1)
                                flexfatal (_("filter_func failed"));
                        exit (0);
@@ -183,11 +185,10 @@ bool filter_apply_chain (struct filter * chain)
 
        /* Parent */
        close (pipes[0]);
-       if (dup2 (pipes[1], 1) == -1)
+       if (dup2 (pipes[1], fileno (stdout)) == -1)
                flexfatal (_("dup2(pipes[1],1)"));
        close (pipes[1]);
-       if ( !fdopen (1, "w"))
-               flexfatal (_("fdopen(1) failed"));
+    fseek (stdout, 0, SEEK_CUR);
 
        return true;
 }