mustach-tool.c 1.52 KB
Newer Older
josé bollo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 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.
*/

#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
27
#include <string.h>
josé bollo committed
28 29 30 31 32

#include <json-c/json.h>

#include "mustach-json-c.h"

33 34 35 36 37 38
static void help(char *prog)
{
	printf("usage: %s json-file mustach-templates...\n", basename(prog));
	exit(0);
}

josé bollo committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static char *readfile(const char *filename)
{
	struct stat s;
	int f;
	char *result;

	f = open(filename, O_RDONLY);
	fstat(f, &s);
	result = malloc(s.st_size + 1);
	read(f, result, s.st_size);
	close(f);
	result[s.st_size] = 0;
	return result;
}

int main(int ac, char **av)
{
	struct json_object *o;
	char *t;
58
	char *prog = *av;
josé bollo committed
59 60

	if (*++av) {
61 62
		if (!strcmp(*av, "-h") || !strcmp(*av, "--help"))
			help(prog);
josé bollo committed
63 64 65 66 67 68 69 70 71 72 73 74
		o = json_object_from_file(*av++);
		while(o && *av) {
			t = readfile(*av++);
			fmustach_json_c(t, o, stdout);
			free(t);
		}
		json_object_put(o);
	}
	return 0;
}