You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2016/03/21 17:53:39 UTC

svn commit: r1736000 - in /felix/trunk/gogo/runtime: pom.xml src/main/java/org/apache/felix/gogo/runtime/Pipe.java src/test/java/org/apache/felix/gogo/runtime/TestParser4.java

Author: gnodet
Date: Mon Mar 21 16:53:39 2016
New Revision: 1736000

URL: http://svn.apache.org/viewvc?rev=1736000&view=rev
Log:
Fix input redirection

Modified:
    felix/trunk/gogo/runtime/pom.xml
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java
    felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser4.java

Modified: felix/trunk/gogo/runtime/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/pom.xml?rev=1736000&r1=1735999&r2=1736000&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/pom.xml (original)
+++ felix/trunk/gogo/runtime/pom.xml Mon Mar 21 16:53:39 2016
@@ -84,6 +84,13 @@
                     </instructions>
                 </configuration>
             </plugin>
+            <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 </project>

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java?rev=1736000&r1=1735999&r2=1736000&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java Mon Mar 21 16:53:39 2016
@@ -78,7 +78,7 @@ public class Pipe extends Thread
     private static final int READ = 1;
     private static final int WRITE = 2;
 
-    private void setStream(Channel ch, int fd, int readWrite) throws IOException {
+    private void setStream(Channel ch, int fd, int readWrite, boolean begOfPipe, boolean endOfPipe) throws IOException {
         if ((readWrite & READ) != 0 && !(ch instanceof ReadableByteChannel)) {
             throw new IllegalArgumentException("Channel is not readable");
         }
@@ -106,7 +106,13 @@ public class Pipe extends Thread
                     mrbc = (MultiReadableByteChannel) streams[fd];
                 } else {
                     mrbc = new MultiReadableByteChannel();
-                    mrbc.addChannel((ReadableByteChannel) streams[fd], toclose[fd]);
+                    if (streams[fd] != null && begOfPipe) {
+                        if (toclose[fd]) {
+                            streams[fd].close();
+                        }
+                    } else {
+                        mrbc.addChannel((ReadableByteChannel) streams[fd], toclose[fd]);
+                    }
                     streams[fd] = mrbc;
                     toclose[fd] = true;
                 }
@@ -117,7 +123,13 @@ public class Pipe extends Thread
                     mrbc = (MultiWritableByteChannel) streams[fd];
                 } else {
                     mrbc = new MultiWritableByteChannel();
-                    mrbc.addChannel((WritableByteChannel) streams[fd], toclose[fd]);
+                    if (streams[fd] != null && endOfPipe) {
+                        if (toclose[fd]) {
+                            streams[fd].close();
+                        }
+                    } else {
+                        mrbc.addChannel((WritableByteChannel) streams[fd], toclose[fd]);
+                    }
                     streams[fd] = mrbc;
                     toclose[fd] = true;
                 }
@@ -196,6 +208,11 @@ public class Pipe extends Thread
         WritableByteChannel errChannel = (WritableByteChannel) streams[2];
 
         Channel[] prevStreams = tStreams.get();
+
+        // TODO: not sure this is the correct way
+        boolean begOfPipe = !toclose[0];
+        boolean endOfPipe = !toclose[1];
+
         try
         {
             if (executable instanceof Statement) {
@@ -227,10 +244,10 @@ public class Pipe extends Thread
                         }
                         Channel ch = Files.newByteChannel(outPath, options);
                         if (fd >= 0) {
-                            setStream(ch, fd, WRITE);
+                            setStream(ch, fd, WRITE, begOfPipe, endOfPipe);
                         } else {
-                            setStream(ch, 1, WRITE);
-                            setStream(ch, 2, WRITE);
+                            setStream(ch, 1, WRITE, begOfPipe, endOfPipe);
+                            setStream(ch, 2, WRITE, begOfPipe, endOfPipe);
                         }
                     }
                     else if ((m = Pattern.compile("([0-9])?>&([0-9])").matcher(t)).matches()) {
@@ -262,7 +279,7 @@ public class Pipe extends Thread
                             options.add(StandardOpenOption.CREATE);
                         }
                         Channel ch = Files.newByteChannel(inPath, options);
-                        setStream(ch, fd, READ + (output ? WRITE : 0));
+                        setStream(ch, fd, READ + (output ? WRITE : 0), begOfPipe, endOfPipe);
                     }
                 }
             } else {
@@ -271,9 +288,6 @@ public class Pipe extends Thread
 
             tStreams.set(streams);
 
-            // TODO: not sure this is the correct way
-            boolean endOfPipe = !toclose[1];
-
             in = Channels.newInputStream((ReadableByteChannel) streams[0]);
             out = new PrintStream(Channels.newOutputStream((WritableByteChannel) streams[1]), true);
             err = new PrintStream(Channels.newOutputStream((WritableByteChannel) streams[2]), true);

Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser4.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser4.java?rev=1736000&r1=1735999&r2=1736000&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser4.java (original)
+++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestParser4.java Mon Mar 21 16:53:39 2016
@@ -21,6 +21,7 @@ package org.apache.felix.gogo.runtime;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.StringWriter;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -41,8 +42,8 @@ public class TestParser4 extends Abstrac
         c.addCommand("echoerr", this);
         c.addCommand("tac", this);
 
-        assertEquals("hello", c.execute("echo | tac"));
-        assertEquals("hello", c.execute("echoerr |& tac"));
+        assertEquals("hello\n", c.execute("echo hello| tac"));
+        assertEquals("hello\n", c.execute("echoerr hello|& tac"));
     }
 
     public void testRedir() throws Exception {
@@ -55,31 +56,70 @@ public class TestParser4 extends Abstrac
         c.currentDir(path);
 
         Files.deleteIfExists(path.resolve("foo"));
-        assertEquals("hello", c.execute("echo >foo | tac"));
+        assertEquals("hello\n", c.execute("echo hello>foo | tac"));
         assertEquals("hello\n", new String(Files.readAllBytes(path.resolve("foo"))));
     }
 
-    public void echo()
+    public void testRedirInput() throws Exception {
+        Context c = new Context();
+        c.addCommand("echo", this);
+        c.addCommand("tac", this);
+        c.addCommand("cat", this);
+
+        Path path = Paths.get("target/tmp");
+        Files.createDirectories(path);
+        c.currentDir(path);
+
+        Files.deleteIfExists(path.resolve("fooa"));
+        Files.deleteIfExists(path.resolve("foob"));
+        c.execute("echo a>fooa");
+        c.execute("echo b>foob");
+        assertEquals("a\nb\n", c.execute("cat <fooa <foob | tac"));
+    }
+
+    public void testMultiInput() throws Exception {
+        Context c = new Context();
+        c.addCommand("echo", this);
+        c.addCommand("tac", this);
+        c.addCommand("cat", this);
+
+        Path path = Paths.get("target/tmp");
+        Files.createDirectories(path);
+        c.currentDir(path);
+
+        Files.deleteIfExists(path.resolve("fooa"));
+        Files.deleteIfExists(path.resolve("foob"));
+        c.execute("echo a>fooa");
+        c.execute("echo b>foob");
+        assertEquals("foo\na\nb\n", c.execute("echo foo | cat <fooa | cat<foob | tac"));
+    }
+
+    public void echo(String msg)
     {
-        System.out.println("hello");
+        System.out.println(msg);
     }
 
-    public void echoerr()
+    public void echoerr(String msg)
     {
-        System.err.println("hello");
+        System.err.println(msg);
+    }
+
+    public void cat() throws IOException {
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                System.out.println(line);
+            }
+        }
     }
 
     public String tac() throws IOException {
         StringWriter sw = new StringWriter();
-        BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
-        boolean first = true;
-        String s;
-        while ((s = rdr.readLine()) != null) {
-            if (!first) {
-                sw.write(' ');
-            }
-            first = false;
-            sw.write(s);
+        Reader rdr = new InputStreamReader(System.in);
+        char[] buf = new char[1024];
+        int len;
+        while ((len = rdr.read(buf)) >= 0) {
+            sw.write(buf, 0, len);
         }
         return sw.toString();
     }