You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2015/01/29 16:26:45 UTC

karaf git commit: [KARAF-3455] Fix support for multi-line commands parsing

Repository: karaf
Updated Branches:
  refs/heads/master cbbed8ac4 -> 0589621a5


[KARAF-3455] Fix support for multi-line commands parsing

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/0589621a
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/0589621a
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/0589621a

Branch: refs/heads/master
Commit: 0589621a5a3408a2f75e300f84a06ffa741507cb
Parents: cbbed8a
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Thu Jan 29 16:26:37 2015 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Thu Jan 29 16:26:37 2015 +0100

----------------------------------------------------------------------
 .../impl/console/parsing/CommandLineParser.java   |  3 +++
 .../karaf/shell/support/parsing/GogoParser.java   | 18 +++++++++++-------
 .../shell/impl/console/parsing/ParsingTest.java   | 15 +++++++++++++++
 3 files changed, 29 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/0589621a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java
index b5f28fb..5dbb6ff 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/parsing/CommandLineParser.java
@@ -100,6 +100,9 @@ public class CommandLineParser {
                     parsed.append(ch);
                     parsed.append(" ");
                     length++;
+                } else if (ch == '\n') {
+                    parsed.append(ch);
+                    length++;
                 } else {
                     throw new IllegalArgumentException("Unrecognized character: '" + ch + "'");
                 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/0589621a/shell/core/src/main/java/org/apache/karaf/shell/support/parsing/GogoParser.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/support/parsing/GogoParser.java b/shell/core/src/main/java/org/apache/karaf/shell/support/parsing/GogoParser.java
index 73a1584..731a61e 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/support/parsing/GogoParser.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/support/parsing/GogoParser.java
@@ -47,9 +47,9 @@ public class GogoParser {
 
 	public void ws() {
         // derek: BUGFIX: loop if comment  at beginning of input
-        //while (!eof() && Character.isWhitespace(peek())) {
-        while (!eof() && (!escaped && Character.isWhitespace(peek()) || current == 0)) {
-            if (current != 0 || !escaped && Character.isWhitespace(peek())) {
+        //while (!eof() && isWhitespace(peek())) {
+        while (!eof() && (!escaped && isWhitespace(peek()) || current == 0)) {
+            if (current != 0 || !escaped && isWhitespace(peek())) {
                 current++;
             }
             if (peek() == '/' && current < text.length() - 2
@@ -62,6 +62,10 @@ public class GogoParser {
         }
     }
 
+    private boolean isWhitespace(char ch) {
+        return ch != '\n' && Character.isWhitespace(ch);
+    }
+
     private void comment() {
         while (!eof() && peek() != '\n' && peek() != '\r') {
             next();
@@ -139,7 +143,7 @@ public class GogoParser {
         ws();
         if (!eof()) {
             program.add(pipeline());
-            while (peek() == ';') {
+            while (peek() == ';' || peek() == '\n') {
                 current++;
                 List<List<String>> pipeline = pipeline();
                 program.add(pipeline);
@@ -183,7 +187,7 @@ public class GogoParser {
         statement.add(value());
         while (!eof()) {
             ws();
-            if (peek() == '|' || peek() == ';') {
+            if (peek() == '|' || peek() == ';' || peek() == '\n') {
                 break;
             }
 
@@ -205,7 +209,7 @@ public class GogoParser {
             try {
                 while (!eof()) {
                     c = peek();
-                    if (!escaped && (c == ';' || c == '|' || Character.isWhitespace(c))) {
+                    if (!escaped && (c == ';' || c == '|' || c == '\n' || isWhitespace(c))) {
                         break;
                     }
                     next();
@@ -261,7 +265,7 @@ public class GogoParser {
             while (!eof()) {
                 c = peek();
                 if (!escaped) {
-                    if (Character.isWhitespace(c) || c == ';' || c == '|' || c == '=') {
+                    if (isWhitespace(c) || c == ';' || c == '|' || c == '=') {
                         break;
                     }
                     else if (c == '{') {

http://git-wip-us.apache.org/repos/asf/karaf/blob/0589621a/shell/core/src/test/java/org/apache/karaf/shell/impl/console/parsing/ParsingTest.java
----------------------------------------------------------------------
diff --git a/shell/core/src/test/java/org/apache/karaf/shell/impl/console/parsing/ParsingTest.java b/shell/core/src/test/java/org/apache/karaf/shell/impl/console/parsing/ParsingTest.java
index 7e8a727..ff605ee 100644
--- a/shell/core/src/test/java/org/apache/karaf/shell/impl/console/parsing/ParsingTest.java
+++ b/shell/core/src/test/java/org/apache/karaf/shell/impl/console/parsing/ParsingTest.java
@@ -72,7 +72,22 @@ public class ParsingTest {
 
         String parsed = CommandLineParser.parse(session, " foo bar (a + b); another   command with spaces ");
         assertEquals("foo bar (a + b) ; another \"command with spaces\"", parsed);
+    }
+
+    @Test
+    public void testCommandLineParserMultiLine() {
+
+        SessionFactoryImpl sessionFactory = new SessionFactoryImpl(new ThreadIOImpl());
+        ManagerImpl manager = new ManagerImpl(sessionFactory, sessionFactory);
+        sessionFactory.getRegistry().register(new ActionCommand(manager, FooCommand.class));
+        sessionFactory.getRegistry().register(new ActionCommand(manager, AnotherCommand.class));
+        sessionFactory.getRegistry().register(new CustomParser());
+        Session session = new HeadlessSessionImpl(sessionFactory, sessionFactory.getCommandProcessor(),
+                new ByteArrayInputStream(new byte[0]), new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream())
+        );
 
+        String parsed = CommandLineParser.parse(session, "echo a\necho b");
+        assertEquals("echo a\necho b", parsed);
     }
 
     @Command(scope = "scope", name = "foo")