From ef3b05ff11cbf84c5cd1eb6f962ebb98112f7862 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Mon, 4 Nov 2019 09:34:37 +0100 Subject: [PATCH] Escaped @ ("\@") is treated as "@" (doesn't read file) closes #103 --- jo.c | 72 ++++++++++++++++++++++++++----------------------- tests/jo.22.exp | 1 + tests/jo.22.sh | 4 +++ 3 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 tests/jo.22.exp create mode 100644 tests/jo.22.sh diff --git a/jo.c b/jo.c index 118b4cb..0aa43ce 100644 --- a/jo.c +++ b/jo.c @@ -13,7 +13,7 @@ #include "base64.h" /* - * Copyright (C) 2016 Jan-Piet Mens + * Copyright (C) 2016-2019 Jan-Piet Mens * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -255,43 +255,47 @@ JsonNode *vnode(char *str, int flags) } } - if (*str == '@' || *str == '%' || *str == ':') { - char *filename = str + 1, *content; - bool binmode = (*str == '%'); - bool jsonmode = (*str == ':'); - size_t len = 0; - JsonNode *j = NULL; - - if ((content = slurp_file(filename, &len, false)) == NULL) { - errx(1, "Error reading file %s", filename); - } - - if (binmode) { - char *encoded; - - if ((encoded = base64_encode(content, len)) == NULL) { - errx(1, "Cannot base64-encode file %s", filename); + if (*str == '\\') { + ++str; + } else { + if (*str == '@' || *str == '%' || *str == ':') { + char *filename = str + 1, *content; + bool binmode = (*str == '%'); + bool jsonmode = (*str == ':'); + size_t len = 0; + JsonNode *j = NULL; + + if ((content = slurp_file(filename, &len, false)) == NULL) { + errx(1, "Error reading file %s", filename); } - - j = json_mkstring(encoded); - free(encoded); - } else if (jsonmode) { - j = json_decode(content); + + if (binmode) { + char *encoded; + + if ((encoded = base64_encode(content, len)) == NULL) { + errx(1, "Cannot base64-encode file %s", filename); + } + + j = json_mkstring(encoded); + free(encoded); + } else if (jsonmode) { + j = json_decode(content); + if (j == NULL) { + errx(1, "Cannot decode JSON in file %s", filename); + } + } + + // If it got this far without valid JSON, just consider it a string if (j == NULL) { - errx(1, "Cannot decode JSON in file %s", filename); + char *bp = content + strlen(content) - 1; + + if (*bp == '\n') *bp-- = 0; + if (*bp == '\r') *bp = 0; + j = json_mkstring(content); } + free(content); + return (j); } - - // If it got this far without valid JSON, just consider it a string - if (j == NULL) { - char *bp = content + strlen(content) - 1; - - if (*bp == '\n') *bp-- = 0; - if (*bp == '\r') *bp = 0; - j = json_mkstring(content); - } - free(content); - return (j); } if (*str == '{' || *str == '[') { diff --git a/tests/jo.22.exp b/tests/jo.22.exp new file mode 100644 index 0000000..3deedf8 --- /dev/null +++ b/tests/jo.22.exp @@ -0,0 +1 @@ +{"key":"@timestamp"} diff --git a/tests/jo.22.sh b/tests/jo.22.sh new file mode 100644 index 0000000..d37617f --- /dev/null +++ b/tests/jo.22.sh @@ -0,0 +1,4 @@ +# avoid reading from file if escaped \@ + +${JO:-jo} key="\@timestamp" +