naccyde / rpms / iproute

Forked from rpms/iproute 5 months ago
Clone
Blob Blame History Raw
From a8e630258cafd1e93e85fd8ee7227583a28fb7cd Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Thu, 18 Feb 2016 15:31:08 +0100
Subject: [PATCH] batch: support quoted strings

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1272593
Upstream Status: iproute2.git commit 39e3d3836c138

commit 39e3d3836c1384506d0a76a496133c5361940770
Author: Christophe Gouault <christophe.gouault@6wind.com>
Date:   Fri Oct 2 11:59:37 2015 +0200

    batch: support quoted strings

    Support quoting strings with " or ' in an iproute2 batch file.

    Enables to configure empty crypto keys (for ESP-null) or keys with
    spaces:

        xfrm state add src 1.1.1.1 dst 2.2.2.2 proto ah spi 0x1 \
            mode tunnel auth hmac(sha1) "r4ezR/@kd6'749f2 6zf$"

        xfrm state add src 5.5.5.5 dst 2.2.2.2 proto esp spi 0x2 \
            mode tunnel enc cipher_null ""

    Signed-off-by: Christophe Gouault <christophe.gouault@6wind.com>
---
 lib/utils.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/lib/utils.c b/lib/utils.c
index 8c5cd46..1cd4fdd 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -907,12 +907,31 @@ int makeargs(char *line, char *argv[], int maxargs)
 	char *cp;
 	int argc = 0;
 
-	for (cp = strtok(line, ws); cp; cp = strtok(NULL, ws)) {
+	for (cp = line + strspn(line, ws); *cp; cp += strspn(cp, ws)) {
 		if (argc >= (maxargs - 1)) {
 			fprintf(stderr, "Too many arguments to command\n");
 			exit(1);
 		}
+
+		/* word begins with quote */
+		if (*cp == '\'' || *cp == '"') {
+			char quote = *cp++;
+
+			argv[argc++] = cp;
+			/* find ending quote */
+			cp = strchr(cp, quote);
+			if (cp == NULL) {
+				fprintf(stderr, "Unterminated quoted string\n");
+				exit(1);
+			}
+			*cp++ = 0;
+			continue;
+		}
+
 		argv[argc++] = cp;
+		/* find end of word */
+		cp += strcspn(cp, ws);
+		*cp++ = 0;
 	}
 	argv[argc] = NULL;
 
-- 
1.8.3.1