aboutsummaryrefslogtreecommitdiff
path: root/src/templating/mustach-tool.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/templating/mustach-tool.c')
-rw-r--r--src/templating/mustach-tool.c176
1 files changed, 139 insertions, 37 deletions
diff --git a/src/templating/mustach-tool.c b/src/templating/mustach-tool.c
index 364e34a84..0c8f44070 100644
--- a/src/templating/mustach-tool.c
+++ b/src/templating/mustach-tool.c
@@ -1,20 +1,9 @@
/*
Author: José Bollo <jobol@nonadev.net>
- Author: José Bollo <jose.bollo@iot.bzh>
https://gitlab.com/jobol/mustach
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
+ SPDX-License-Identifier: ISC
*/
#define _GNU_SOURCE
@@ -27,7 +16,7 @@
#include <string.h>
#include <libgen.h>
-#include "mustach-json-c.h"
+#include "mustach-wrap.h"
static const size_t BLOCKSIZE = 8192;
@@ -43,16 +32,39 @@ static const char *errors[] = {
"bad unescape tag",
"invalid interface",
"item not found",
- "partial not found"
+ "partial not found",
+ "undefined tag"
};
+static const char *errmsg = 0;
+static int flags = 0;
+static FILE *output = 0;
+
static void help(char *prog)
{
- printf("usage: %s json-file mustach-templates...\n", basename(prog));
+ char *name = basename(prog);
+#define STR_INDIR(x) #x
+#define STR(x) STR_INDIR(x)
+ printf("%s version %s\n", name, STR(VERSION));
+#undef STR
+#undef STR_INDIR
+ printf(
+ "\n"
+ "USAGE:\n"
+ " %s [FLAGS] <json-file> <mustach-templates...>\n"
+ "\n"
+ "FLAGS:\n"
+ " -h, --help Prints help information\n"
+ " -s, --strict Error when a tag is undefined\n"
+ "\n"
+ "ARGS: (if a file is -, read standard input)\n"
+ " <json-file> JSON file with input data\n"
+ " <mustach-templates...> Template files to instantiate\n",
+ name);
exit(0);
}
-static char *readfile(const char *filename)
+static char *readfile(const char *filename, size_t *length)
{
int f;
struct stat s;
@@ -106,50 +118,140 @@ static char *readfile(const char *filename)
} while(rc > 0);
close(f);
+ if (length != NULL)
+ *length = pos;
result[pos] = 0;
return result;
}
+static int load_json(const char *filename);
+static int process(const char *content, size_t length);
+static void close_json();
+
int main(int ac, char **av)
{
- struct json_object *o;
- char *t;
+ char *t, *f;
char *prog = *av;
int s;
+ size_t length;
(void)ac; /* unused */
+ flags = Mustach_With_AllExtensions;
+ output = stdout;
- if (*++av) {
+ for( ++av ; av[0] && av[0][0] == '-' && av[0][1] != 0 ; av++) {
if (!strcmp(*av, "-h") || !strcmp(*av, "--help"))
help(prog);
- if (av[0][0] == '-' && !av[0][1])
- o = json_object_from_fd(0);
- else
- o = json_object_from_file(av[0]);
-#if JSON_C_VERSION_NUM >= 0x000D00
- if (json_util_get_last_err() != NULL) {
- fprintf(stderr, "Bad json: %s (file %s)\n", json_util_get_last_err(), av[0]);
- exit(1);
- }
- else
-#endif
- if (o == NULL) {
- fprintf(stderr, "Aborted: null json (file %s)\n", av[0]);
+ if (!strcmp(*av, "-s") || !strcmp(*av, "--strict"))
+ flags |= Mustach_With_ErrorUndefined;
+ }
+ if (*av) {
+ f = (av[0][0] == '-' && !av[0][1]) ? "/dev/stdin" : av[0];
+ s = load_json(f);
+ if (s < 0) {
+ fprintf(stderr, "Can't load json file %s\n", av[0]);
+ if(errmsg)
+ fprintf(stderr, " reason: %s\n", errmsg);
exit(1);
}
while(*++av) {
- t = readfile(*av);
- s = fmustach_json_c(t, o, stdout);
- if (s != 0) {
+ t = readfile(*av, &length);
+ s = process(t, length);
+ free(t);
+ if (s != MUSTACH_OK) {
s = -s;
if (s < 1 || s >= (int)(sizeof errors / sizeof * errors))
s = 0;
fprintf(stderr, "Template error %s (file %s)\n", errors[s], *av);
}
- free(t);
}
- json_object_put(o);
+ close_json();
+ }
+ return 0;
+}
+
+#define MUSTACH_TOOL_JSON_C 1
+#define MUSTACH_TOOL_JANSSON 2
+#define MUSTACH_TOOL_CJSON 3
+
+#define TOOL MUSTACH_TOOL_JANSSON
+
+#if TOOL == MUSTACH_TOOL_JSON_C
+
+#include "mustach-json-c.h"
+
+static struct json_object *o;
+static int load_json(const char *filename)
+{
+ o = json_object_from_file(filename);
+#if JSON_C_VERSION_NUM >= 0x000D00
+ errmsg = json_util_get_last_err();
+ if (errmsg != NULL)
+ return -1;
+#endif
+ if (o == NULL) {
+ errmsg = "null json";
+ return -1;
+ }
+ return 0;
+}
+static int process(const char *content, size_t length)
+{
+ return mustach_json_c_file(content, length, o, flags, output);
+}
+static void close_json()
+{
+ json_object_put(o);
+}
+
+#elif TOOL == MUSTACH_TOOL_JANSSON
+
+#include "mustach-jansson.h"
+
+static json_t *o;
+static json_error_t e;
+static int load_json(const char *filename)
+{
+ o = json_load_file(filename, JSON_DECODE_ANY, &e);
+ if (o == NULL) {
+ errmsg = e.text;
+ return -1;
}
return 0;
}
+static int process(const char *content, size_t length)
+{
+ return mustach_jansson_file(content, length, o, flags, output);
+}
+static void close_json()
+{
+ json_decref(o);
+}
+
+#elif TOOL == MUSTACH_TOOL_CJSON
+#include "mustach-cjson.h"
+
+static cJSON *o;
+static int load_json(const char *filename)
+{
+ char *t;
+ size_t length;
+
+ t = readfile(filename, &length);
+ o = t ? cJSON_ParseWithLength(t, length) : NULL;
+ free(t);
+ return -!o;
+}
+static int process(const char *content, size_t length)
+{
+ return mustach_cJSON_file(content, length, o, flags, output);
+}
+static void close_json()
+{
+ cJSON_Delete(o);
+}
+
+#else
+#error "no defined json library"
+#endif