This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch NLPCRAFT-108
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-108 by this push:
     new cc33935  WIP.
cc33935 is described below

commit cc339356956f001fedd39304e73e74239ac9e83e
Author: Aaron Radzinski <[email protected]>
AuthorDate: Thu Oct 1 15:31:27 2020 -0700

    WIP.
---
 .../nlpcraft/model/tools/cmdline/NCCli.scala       | 90 +++++++++++++++++++++-
 .../org/apache/nlpcraft/server/NCServer.scala      |  8 +-
 2 files changed, 93 insertions(+), 5 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index 26bf082..b0c2757 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -62,6 +62,10 @@ object NCCli extends App {
     private final lazy val PROMPT = if (SCRIPT_NAME.endsWith("cmd")) ">" else 
"$"
 
     private final val T___ = "    "
+    private val OPEN_BRK = Seq('[', '{', '(', '<')
+    private val CLOSE_BRK = Seq(']', '}', ')', '>')
+    // Pair for each open or close bracket.
+    private val BRK_PAIR = OPEN_BRK.zip(CLOSE_BRK).toMap ++ 
CLOSE_BRK.zip(OPEN_BRK).toMap
 
     private var exitStatus = 0
 
@@ -666,7 +670,14 @@ object NCCli extends App {
             else {
                 val line = rawLine.trim()
 
-                logln(s"\nEntered: $line")
+                try {
+                    val args = splitBySpace(line)
+
+                    logln(args.mkString(", "))
+                }
+                catch {
+                    case e: SplitError ⇒ logln(s"Error around index 
${e.index}")
+                }
             }
         }
     }
@@ -792,6 +803,83 @@ object NCCli extends App {
             get.releaseConnection()
     }
 
+    case class SplitError(index: Int) extends Exception
+
+    /**
+     * Splits given string by spaces taking into an account double and single 
quotes,
+     * and checking for uneven <>, {}, [], () pairs.
+     *
+     * @param line
+     * @return
+     */
+    @throws[SplitError]
+    private def splitBySpace(line: String): Seq[String] = {
+        val lines = mutable.Buffer.empty[String]
+        val buf = new StringBuilder
+        var stack = List.empty[Char]
+        var escape = false
+        var index = 0
+
+        def head: Char = stack.headOption.getOrElse(Char.MinValue)
+
+        for (ch ← line) {
+            if (ch.isWhitespace && !stack.contains('"') && 
!stack.contains('\'') && !escape) {
+                if (buf.nonEmpty) {
+                    lines += buf.toString()
+                    buf.clear()
+                }
+            }
+            else if (ch == '\\') {
+                if (escape)
+                    buf += ch
+                else
+                    // SKip '\'.
+                    escape = true
+            }
+            else if (ch == '"' || ch == '\'') {
+                if (!escape) {
+                    if (!stack.contains(ch))
+                        stack ::= ch // Push.
+                    else if (head == ch)
+                        stack = stack.tail // Pop.
+                    else
+                        throw SplitError(index)
+                }
+
+                buf += ch
+            }
+            else if (OPEN_BRK.contains(ch)) {
+                stack ::= ch // Push.
+
+                buf += ch
+            }
+            else if (CLOSE_BRK.contains(ch)) {
+                if (head != BRK_PAIR(ch))
+                    throw SplitError(index)
+
+                stack = stack.tail // Pop.
+
+                buf += ch
+            }
+            else
+                buf += ch
+
+            // Drop escape flag.
+            if (escape && ch != '\\')
+                escape = false
+
+            index += 1
+        }
+
+        if (stack.nonEmpty)
+            throw SplitError(index)
+
+        if (buf.nonEmpty)
+            lines += buf.toString()
+
+        lines.map(_.trim)
+    }
+
     /**
      *
      * @param cmd
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
index e9ef08c..c37a6dc 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/NCServer.scala
@@ -235,7 +235,7 @@ object NCServer extends App with NCIgniteInstance with 
LazyLogging with NCOpenCe
         /**
          *
          */
-        def storeInUserHome() = {
+        def save() = {
             final object Config extends NCConfigurable {
                 final private val pre = "nlpcraft.server"
 
@@ -278,7 +278,7 @@ object NCServer extends App with NCIgniteInstance with 
LazyLogging with NCOpenCe
 
                     logger.trace(s"Overriding failed server beacon: 
${path.getAbsolutePath}")
 
-                    storeInUserHome()
+                    save()
 
                 case Right(rawObj) ⇒
                     val beacon = rawObj.asInstanceOf[NCCliServerBeacon]
@@ -288,12 +288,12 @@ object NCServer extends App with NCIgniteInstance with 
LazyLogging with NCOpenCe
                     else {
                         logger.trace(s"Overriding server beacon for a phantom 
process [pid=${beacon.pid}]")
 
-                        storeInUserHome()
+                        save()
                     }
             }
         else
             // No existing beacon file detected.
-            storeInUserHome()
+            save()
     }
 
     NCIgniteRunner.runWith(

Reply via email to