docparse: Escape backslash, tab and double quote in JSON
PATCH v2 1/1 docparse: Escape backslash, tab and double quote in JSON
From: Cyril Hrubis <[email protected]>
Avoid quoting new line as it's usually expected formatting, e.g. in .options field.
---
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(-)
docparse/data_storage.h +34 -1
@@ -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 pswitch (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++) {
Signature · 1 line
2.31.1
PATCH v2 1/1 docparse: Escape backslash, tab and double quote in JSON
Hi!
Quoted 2 lines
Avoid quoting new line as it's usually expected formatting, e.g. in .options field.
Hmm, looking at the cases that have \n in option strings it looks like a messy hack rather than anything else...
I guess that a proper fix would be an ability to group the options, i.e. make the .options an array of arrays so that the structures would look like:
.options = (struct tst_option_group[]) {
{.title = "Server", .options = (struct tst_option) {
{"l", &foo, "Foo option help"},
{}
},
...
{}
}But that is something that is completely out of scope for this patch...
Signature · 2 lines
Cyril Hrubis [email protected]