All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Vorel <pvorel@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 1/1] docparse: Escape backslash, tab and double quote in JSON
Date: Mon,  3 May 2021 15:30:25 +0200	[thread overview]
Message-ID: <20210503133025.2557-1-pvorel@suse.cz> (raw)

From: Cyril Hrubis <chrubis@suse.cz>

Avoid quoting new line as it's usually expected formatting,
e.g. in .options field.

Tested-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
[ pvorel: keep \n ]
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Hi,

I kept \n, it's usually expected formatting in .options:

* \n kept:
p: p_opt -p port Rhost port (mandatory)

r: r_opt -r x Rhost IP (mandatory)

-g, -r IP parameter can contain more IP, separated by CHR2STR ( IP_ADDR_DELIM )

* \n replaced:
p: p_opt -p port Rhost port (mandatory)

r: r_opt -r x Rhost IP (mandatory)\n\n-g, -r IP parameter can contain more IP, separated by CHR2STR ( IP_ADDR_DELIM )

Kind regards,
Petr

 docparse/data_storage.h | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/docparse/data_storage.h b/docparse/data_storage.h
index ef420c08f..5afeba42c 100644
--- a/docparse/data_storage.h
+++ b/docparse/data_storage.h
@@ -256,6 +256,39 @@ static inline void data_fprintf(FILE *f, unsigned int padd, const char *fmt, ...
 	va_end(va);
 }
 
+
+static inline void data_fprintf_esc(FILE *f, unsigned int padd, const char *str)
+{
+	while (padd-- > 0)
+		fputc(' ', f);
+
+	fputc('"', f);
+
+	while (*str) {
+		switch (*str) {
+		case '\\':
+			/* keep \n, it's usually expected formatting in .options */
+			if (*(str+1) == 'n')
+				putc(*str, f);
+			else
+				fputs("\\\\", f);
+			break;
+		case '"':
+			fputs("\\\"", f);
+			break;
+		case '\t':
+			fputs("\\t", f);
+			break;
+		default:
+			putc(*str, f);
+			break;
+		}
+		str++;
+	}
+
+	fputc('"', f);
+}
+
 static inline void data_to_json_(struct data_node *self, FILE *f, unsigned int padd, int do_padd)
 {
 	unsigned int i;
@@ -263,7 +296,7 @@ static inline void data_to_json_(struct data_node *self, FILE *f, unsigned int p
 	switch (self->type) {
 	case DATA_STRING:
 		padd = do_padd ? padd : 0;
-		data_fprintf(f, padd, "\"%s\"", self->string.val);
+		data_fprintf_esc(f, padd, self->string.val);
 	break;
 	case DATA_HASH:
 		for (i = 0; i < self->hash.elems_used; i++) {
-- 
2.31.1


             reply	other threads:[~2021-05-03 13:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-03 13:30 Petr Vorel [this message]
2021-05-04 11:42 ` [LTP] [PATCH v2 1/1] docparse: Escape backslash, tab and double quote in JSON Cyril Hrubis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210503133025.2557-1-pvorel@suse.cz \
    --to=pvorel@suse.cz \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.