From 349abe5942908dd46b2b15070fe29166585820e8 Mon Sep 17 00:00:00 2001
From: Jakub Jelen <jjelen@redhat.com>
Date: Mon, 16 Sep 2019 16:36:22 +0200
Subject: [PATCH] config_parser: Implement more useful variant of get_token()

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
---
 src/config_parser.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/config_parser.c b/src/config_parser.c
index ae2aa2c8..354ed35c 100644
--- a/src/config_parser.c
+++ b/src/config_parser.c
@@ -31,6 +31,11 @@
 #include "libssh/config_parser.h"
 #include "libssh/priv.h"
 
+/* Returns the original string after skipping the leading whitespace
+ * and optional quotes.
+ * This is useful in case we need to get the rest of the line (for example
+ * external command).
+ */
 char *ssh_config_get_cmd(char **str)
 {
     register char *c;
@@ -65,15 +70,34 @@ out:
     return r;
 }
 
+/* Returns the next token delimited by whitespace or equal sign (=)
+ * respecting the quotes creating separate token (including whitespaces).
+ */
 char *ssh_config_get_token(char **str)
 {
     register char *c;
     char *r;
 
-    c = ssh_config_get_cmd(str);
+    /* Ignore leading spaces */
+    for (c = *str; *c; c++) {
+        if (! isblank(*c)) {
+            break;
+        }
+    }
 
+    /* If we start with quote, return the whole quoted block */
+    if (*c == '\"') {
+        for (r = ++c; *c; c++) {
+            if (*c == '\"') {
+                *c = '\0';
+                goto out;
+            }
+        }
+    }
+
+    /* Otherwise terminate on space, equal or newline */
     for (r = c; *c; c++) {
-        if (isblank(*c) || *c == '=') {
+        if (isblank(*c) || *c == '=' || *c == '\n') {
             *c = '\0';
             goto out;
         }