mustach-json-c.c 5.78 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 27 28 29 30
/*
 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 <stdio.h>
#include <string.h>

#include <json-c/json.h>

#include "mustach.h"

#define MAX_DEPTH 256

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#if defined(NO_EXTENSION_FOR_MUSTACH)
# if !defined(NO_COLON_EXTENSION_FOR_MUSTACH)
#  define NO_COLON_EXTENSION_FOR_MUSTACH
# endif
# if !defined(NO_EQUAL_VALUE_EXTENSION_FOR_MUSTACH)
#  define NO_EQUAL_VALUE_EXTENSION_FOR_MUSTACH
# endif
# if !defined(NO_JSON_POINTER_EXTENSION_FOR_MUSTACH)
#  define NO_JSON_POINTER_EXTENSION_FOR_MUSTACH
# endif
#endif

#if defined(NO_COLON_EXTENSION_FOR_MUSTACH)
# if !defined(NO_JSON_POINTER_EXTENSION_FOR_MUSTACH)
#  define NO_JSON_POINTER_EXTENSION_FOR_MUSTACH
# endif
#endif

josé bollo committed
49 50 51 52 53 54 55 56 57 58
struct expl {
	struct json_object *root;
	int depth;
	struct {
		struct json_object *cont;
		struct json_object *obj;
		int index, count;
	} stack[MAX_DEPTH];
};

59 60
#if !defined(NO_EQUAL_VALUE_EXTENSION_FOR_MUSTACH)
static char *keyval(char *head, int isptr)
josé bollo committed
61 62 63 64 65
{
	char *w, c;

	c = *(w = head);
	while (c && c != '=') {
66 67 68 69 70 71 72
#if !defined(NO_JSON_POINTER_EXTENSION_FOR_MUSTACH)
		if (isptr) {
			if (isptr && c == '~' && head[1] == '=')
				c = *++head;
		} else
#endif
		if (!isptr && c == '\\' && head[1] == '=')
josé bollo committed
73 74 75 76 77 78 79
			c = *++head;
		*w++ = c;
		c = *++head;
	}
	*w = 0;
	return c == '=' ? ++head : NULL;
}
80
#endif
josé bollo committed
81

82
static char *key(char **head, int isptr)
josé bollo committed
83 84 85
{
	char *r, *i, *w, c;

86
	c = *(i = *head);
josé bollo committed
87
	if (!c)
josé bollo committed
88 89
		r = NULL;
	else {
90
		r = w = i;
91 92 93 94 95
#if !defined(NO_JSON_POINTER_EXTENSION_FOR_MUSTACH)
		if (isptr)
			while (c && c != '/') {
				if (c == '~')
					switch (i[1]) {
96
					case '1': c = '/'; /*@fallthrough@*/
97 98 99 100 101 102 103
					case '0': i++;
					}
				*w++ = c;
				c = *++i;
			}
		else
#endif
josé bollo committed
104 105
		while (c && c != '.') {
			if (c == '\\' && (i[1] == '.' || i[1] == '\\'))
josé bollo committed
106 107 108 109 110
				c = *++i;
			*w++ = c;
			c = *++i;
		}
		*w = 0;
josé bollo committed
111
		*head = i + !!c;
josé bollo committed
112 113 114 115
	}
	return r;
}

josé bollo committed
116 117
static struct json_object *find(struct expl *e, const char *name)
{
118
	int i, isptr;
josé bollo committed
119
	struct json_object *o;
josé bollo committed
120
	char *n, *c, *v;
josé bollo committed
121 122

	n = strdupa(name);
123 124 125 126 127 128
	isptr = 0;
#if !defined(NO_JSON_POINTER_EXTENSION_FOR_MUSTACH)
	isptr = n[0] == '/';
	n += isptr;
#endif

129
	v = NULL;
130 131
#if !defined(NO_EQUAL_VALUE_EXTENSION_FOR_MUSTACH)
	v = keyval(n, isptr);
132
#endif
133 134 135 136
	if (n[0] == '.' && !n[1]) {
		/* case of . alone */
		o = e->stack[e->depth].obj;
	} else {
137
		c = key(&n, isptr);
138 139 140 141 142 143 144
		if (c == NULL)
			return NULL;
		o = NULL;
		i = e->depth;
		while (i >= 0 && !json_object_object_get_ex(e->stack[i].obj, c, &o))
			i--;
		if (i < 0)
josé bollo committed
145
			return NULL;
146
		c = key(&n, isptr);
147 148 149
		while(c) {
			if (!json_object_object_get_ex(o, c, &o))
				return NULL;
150
			c = key(&n, isptr);
151
		}
josé bollo committed
152
	}
153 154 155 156 157
	if (v) {
		i = v[0] == '!';
		if (i == !strcmp(&v[i], json_object_get_string(o)))
			o = NULL;
	}
josé bollo committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171
	return o;
}

static int start(void *closure)
{
	struct expl *e = closure;
	e->depth = 0;
	e->stack[0].cont = NULL;
	e->stack[0].obj = e->root;
	e->stack[0].index = 0;
	e->stack[0].count = 1;
	return 0;
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static void print(FILE *file, const char *string, int escape)
{
	if (!escape)
		fprintf(file, "%s", string);
	else if (*string)
		do {
			switch(*string) {
			case '<': fputs("&lt;", file); break;
			case '>': fputs("&gt;", file); break;
			case '&': fputs("&amp;", file); break;
			default: putc(*string, file); break;
			}
		} while(*++string);
}

josé bollo committed
187 188 189 190 191
static int put(void *closure, const char *name, int escape, FILE *file)
{
	struct expl *e = closure;
	struct json_object *o = find(e, name);
	if (o)
192
		print(file, json_object_get_string(o), escape);
josé bollo committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	return 0;
}

static int enter(void *closure, const char *name)
{
	struct expl *e = closure;
	struct json_object *o = find(e, name);
	if (++e->depth >= MAX_DEPTH)
		return MUSTACH_ERROR_TOO_DEPTH;
	if (json_object_is_type(o, json_type_array)) {
		e->stack[e->depth].count = json_object_array_length(o);
		if (e->stack[e->depth].count == 0) {
			e->depth--;
			return 0;
		}
		e->stack[e->depth].cont = o;
		e->stack[e->depth].obj = json_object_array_get_idx(o, 0);
		e->stack[e->depth].index = 0;
	} else if (json_object_is_type(o, json_type_object) || json_object_get_boolean(o)) {
		e->stack[e->depth].count = 1;
		e->stack[e->depth].cont = NULL;
		e->stack[e->depth].obj = o;
		e->stack[e->depth].index = 0;
	} else {
		e->depth--;
		return 0;
	}
	return 1;
}

static int next(void *closure)
{
	struct expl *e = closure;
	if (e->depth <= 0)
		return MUSTACH_ERROR_CLOSING;
	e->stack[e->depth].index++;
	if (e->stack[e->depth].index >= e->stack[e->depth].count)
		return 0;
	e->stack[e->depth].obj = json_object_array_get_idx(e->stack[e->depth].cont, e->stack[e->depth].index);
	return 1;
}

static int leave(void *closure)
{
	struct expl *e = closure;
	if (e->depth <= 0)
		return MUSTACH_ERROR_CLOSING;
	e->depth--;
	return 0;
}

static struct mustach_itf itf = {
	.start = start,
	.put = put,
	.enter = enter,
	.next = next,
249
	.leave = leave
josé bollo committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
};

int fmustach_json_c(const char *template, struct json_object *root, FILE *file)
{
	struct expl e;
	e.root = root;
	return fmustach(template, &itf, &e, file);
}

int fdmustach_json_c(const char *template, struct json_object *root, int fd)
{
	struct expl e;
	e.root = root;
	return fdmustach(template, &itf, &e, fd);
}

int mustach_json_c(const char *template, struct json_object *root, char **result, size_t *size)
{
	struct expl e;
	e.root = root;
	return mustach(template, &itf, &e, result, size);
}