felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1736058 - in /felix/trunk/gogo/runtime/src: main/java/org/apache/felix/gogo/runtime/ test/java/org/apache/felix/gogo/runtime/
Date Mon, 21 Mar 2016 17:00:05 GMT
Author: gnodet
Date: Mon Mar 21 17:00:05 2016
New Revision: 1736058

URL: http://svn.apache.org/viewvc?rev=1736058&view=rev
Log:
heredoc << support

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

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Parser.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Parser.java?rev=1736058&r1=1736057&r2=1736058&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Parser.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Parser.java Mon Mar
21 17:00:05 2016
@@ -308,6 +308,7 @@ public class Parser
 
     private static final Pattern redirNoArg = Pattern.compile("[0-9]?>&[0-9-]|[0-9-]?<&[0-9-]");
     private static final Pattern redirArg = Pattern.compile("[0-9&]?>|[0-9]?>>|[0-9]?<|[0-9]?<>|<<<");
+    private static final Pattern redirHereDoc = Pattern.compile("<<-?");
 
     public Statement statement()
     {
@@ -366,6 +367,11 @@ public class Parser
                 redirs.add(t);
                 needRedirArg = true;
             }
+            else if (redirHereDoc.matcher(t).matches())
+            {
+                redirs.add(t);
+                redirs.add(tz.readHereDoc(t.charAt(t.length() - 1) == '-'));
+            }
             else
             {
                 tokens.add(t);

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=1736058&r1=1736057&r2=1736058&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 17:00:05 2016
@@ -338,6 +338,37 @@ public class Pipe implements Callable<Re
                         setStream(ch, fd, READ + (output ? WRITE : 0));
                     }
                 }
+                else if ((m = Pattern.compile("<<-?").matcher(t)).matches())
+                {
+                    Token hereDoc = tokens.get(++i);
+                    boolean stripLeadingTabs = t.charAt(t.length() - 1) == '-';
+                    InputStream doc = new InputStream()
+                    {
+                        final byte[] bytes = hereDoc.toString().getBytes();
+                        int index = 0;
+                        boolean nl = true;
+                        @Override
+                        public int read() throws IOException
+                        {
+                            if (nl && stripLeadingTabs)
+                            {
+                                while (index < bytes.length && bytes[index] ==
'\t')
+                                {
+                                    index++;
+                                }
+                            }
+                            if (index < bytes.length)
+                            {
+                                int ch = bytes[index++];
+                                nl = ch == '\n';
+                                return ch;
+                            }
+                            return -1;
+                        }
+                    };
+                    Channel ch = Channels.newChannel(doc);
+                    setStream(ch, 0, READ);
+                }
                 else if (Token.eq("<<<", t))
                 {
                     Token word = tokens.get(++i);

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java?rev=1736058&r1=1736057&r2=1736058&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Tokenizer.java Mon
Mar 21 17:00:05 2016
@@ -23,7 +23,7 @@ import java.util.regex.Pattern;
 public class Tokenizer extends BaseTokenizer
 {
 
-    private final Pattern redir = Pattern.compile("[0-9&]?>|[0-9]?>>|[0-9]?>&|[0-9]?<|[0-9]?<>|<<<");
+    private final Pattern redir = Pattern.compile("[0-9&]?>|[0-9]?>>|[0-9]?>&|[0-9]?<|[0-9]?<>|<<<|<<\\-?");
 
     protected boolean inArray;
     protected int word = 0;
@@ -119,6 +119,17 @@ public class Tokenizer extends BaseToken
                     }
                     word = 0;
                     return token(start);
+                case '-':
+                    t = text.subSequence(start, index);
+                    if (redir.matcher(t).matches())
+                    {
+                        getch();
+                        return token(start);
+                    }
+                    else {
+                        getch();
+                        break;
+                    }
                 case '&':
                     // beginning of token
                     if (start == index - 1) {
@@ -211,4 +222,58 @@ public class Tokenizer extends BaseToken
         this.pushed = token;
     }
 
+    public Token readHereDoc(boolean ignoreLeadingTabs)
+    {
+        final short sLine = line;
+        final short sCol = column;
+        int start;
+        int nlIndex;
+        boolean nl;
+        // Find word
+        skipSpace();
+        start = index - 1;
+        while (ch != '\n' && ch != EOT) {
+            getch();
+        }
+        if (ch == EOT) {
+            throw new EOFError(sLine, sCol, "expected here-doc start", "heredoc", "foo");
+        }
+        Token token = text.subSequence(start, index - 1);
+        getch();
+        start = index - 1;
+        nlIndex = start;
+        nl = true;
+        // Get heredoc
+        while (true)
+        {
+            if (nl)
+            {
+                if (ignoreLeadingTabs && ch == '\t')
+                {
+                    nlIndex++;
+                }
+                else
+                {
+                    nl = false;
+                }
+            }
+            if (ch == '\n' || ch == EOT)
+            {
+                Token s = text.subSequence(nlIndex, index - 1);
+                if (Token.eq(s, token))
+                {
+                    Token hd = text.subSequence(start, s.start());
+                    getch();
+                    return hd;
+                }
+                nlIndex = index;
+                nl = true;
+            }
+            if (ch == EOT)
+            {
+                throw new EOFError(sLine, sCol, "unexpected eof found in here-doc", "heredoc",
token.toString());
+            }
+            getch();
+        }
+    }
 }

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=1736058&r1=1736057&r2=1736058&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 17:00:05 2016
@@ -62,6 +62,9 @@ public class TestParser4 extends Abstrac
         Files.deleteIfExists(path.resolve("foo"));
         assertEquals("hello\n", c.execute("echo hello>foo | tac"));
         assertEquals("hello\n", new String(Files.readAllBytes(path.resolve("foo"))));
+
+        Files.deleteIfExists(path.resolve("foo2"));
+        assertEquals("hello\n", c.execute("echo hello>\\\nfoo2 | tac"));
     }
 
     @Test
@@ -152,6 +155,18 @@ public class TestParser4 extends Abstrac
         assertEquals("ar1 ar2\n", c.execute("cat <<<$c | tac"));
     }
 
+    @Test
+    public void testHereDoc() throws Exception
+    {
+        Context c = new Context();
+        c.addCommand("echo", this);
+        c.addCommand("tac", this);
+        c.addCommand("cat", this);
+
+        assertEquals("bar\nbaz\n", c.execute("cat <<foo\nbar\nbaz\nfoo\n| tac"));
+        assertEquals("bar\nbaz\n", c.execute("cat <<-foo\n\tbar\n\tbaz\n\tfoo\n| tac"));
+    }
+
     public void echo(String msg)
     {
         System.out.println(msg);



Mime
View raw message