summaryrefslogtreecommitdiff
path: root/src/templating/mustach-wrap.c
blob: 2cd00db12a6a11033582b3781bb25d0e9ffc4a41 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 Author: José Bollo <jobol@nonadev.net>

 https://gitlab.com/jobol/mustach

 SPDX-License-Identifier: ISC
*/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <malloc.h>
#endif

#include "mustach.h"
#include "mustach-wrap.h"

/*
* It was stated that allowing to include files
* through template is not safe when the mustache
* template is open to any value because it could
* create leaks (example: {{>/etc/passwd}}).
*/
#if MUSTACH_SAFE
# undef MUSTACH_LOAD_TEMPLATE
#elif !defined(MUSTACH_LOAD_TEMPLATE)
# define MUSTACH_LOAD_TEMPLATE 1
#endif

#if !defined(INCLUDE_PARTIAL_EXTENSION)
# define INCLUDE_PARTIAL_EXTENSION ".mustache"
#endif

/* global hook for partials */
int (*mustach_wrap_get_partial)(const char *name, struct mustach_sbuf *sbuf) = NULL;

/* internal structure for wrapping */
struct wrap {
	/* original interface */
	const struct mustach_wrap_itf *itf;

	/* original closure */
	void *closure;

	/* flags */
	int flags;

	/* emiter callback */
	mustach_emit_cb_t *emitcb;

	/* write callback */
	mustach_write_cb_t *writecb;
};

/* length given by masking with 3 */
enum comp {
	C_no = 0,
	C_eq = 1,
	C_lt = 5,
	C_le = 6,
	C_gt = 9,
	C_ge = 10
};

enum sel {
	S_none = 0,
	S_ok = 1,
	S_objiter = 2,
	S_ok_or_objiter = S_ok | S_objiter
};

static enum comp getcomp(char *head, int sflags)
{
	return (head[0] == '=' && (sflags & Mustach_With_Equal)) ? C_eq
		: (head[0] == '<' && (sflags & Mustach_With_Compare)) ? (head[1] == '=' ? C_le : C_lt)
		: (head[0] == '>' && (sflags & Mustach_With_Compare)) ? (head[1] == '=' ? C_ge : C_gt)
		: C_no;
}

static char *keyval(char *head, int sflags, enum comp *comp)
{
	char *w, car, escaped;
	enum comp k;

	k = C_no;
	w = head;
	car = *head;
	escaped = (sflags & Mustach_With_EscFirstCmp) && (getcomp(head, sflags) != C_no);
	while (car && (escaped || (k = getcomp(head, sflags)) == C_no)) {
		if (escaped)
			escaped = 0;
		else
			escaped = ((sflags & Mustach_With_JsonPointer) ? car == '~' : car == '\\')
			    && (getcomp(head + 1, sflags) != C_no);
		if (!escaped)
			*w++ = car;
		head++;
		car = *head;
	}
	*w = 0;
	*comp = k;
	return k == C_no ? NULL : &head[k & 3];
}

static char *getkey(char **head, int sflags)
{
	char *result, *iter, *write, car;

	car = *(iter = *head);
	if (!car)
		result = NULL;
	else {
		result = write = iter;
		if (sflags & Mustach_With_JsonPointer)
		{
			while (car && car != '/') {
				if (car == '~')
					switch (iter[1]) {
					case '1': car = '/'; /*@fallthrough@*/
					case '0': iter++;
					}
				*write++ = car;
				car = *++iter;
			}
			*write = 0;
			while (car == '/')
				car = *++iter;
		}
		else
		{
			while (car && car != '.') {
				if (car == '\\' && (iter[1] == '.' || iter[1] == '\\'))
					car = *++iter;
				*write++ = car;
				car = *++iter;
			}
			*write = 0;
			while (car == '.')
				car = *++iter;
		}
		*head = iter;
	}
	return result;
}

static enum sel sel(struct wrap *w, const char *name)
{
	enum sel result;
	int i, j, sflags, scmp;
	char *key, *value;
	enum comp k;

	/* make a local writeable copy */
	size_t lenname = 1 + strlen(name);
	char buffer[lenname];
	char *copy = buffer;
	memcpy(copy, name, lenname);

	/* check if matches json pointer selection */
	sflags = w->flags;
	if (sflags & Mustach_With_JsonPointer) {
		if (copy[0] == '/')
			copy++;
		else
			sflags ^= Mustach_With_JsonPointer;
	}

	/* extract the value, translate the key and get the comparator */
	if (sflags & (Mustach_With_Equal | Mustach_With_Compare))
		value = keyval(copy, sflags, &k);
	else {
		k = C_no;
		value = NULL;
	}

	/* case of . alone if Mustach_With_SingleDot? */
	if (copy[0] == '.' && copy[1] == 0 /*&& (sflags & Mustach_With_SingleDot)*/)
		/* yes, select current */
		result = w->itf->sel(w->closure, NULL) ? S_ok : S_none;
	else
	{
		/* not the single dot, extract the first key */
		key = getkey(&copy, sflags);
		if (key == NULL)
			return 0;

		/* select the root item */
		if (w->itf->sel(w->closure, key))
			result = S_ok;
		else if (key[0] == '*'
		      && !key[1]
		      && !value
		      && !*copy
		      && (w->flags & Mustach_With_ObjectIter)
		      && w->itf->sel(w->closure, NULL))
			result = S_ok_or_objiter;
		else
			result = S_none;
		if (result == S_ok) {
			/* iterate the selection of sub items */
			key = getkey(&copy, sflags);
			while(result == S_ok && key) {
				if (w->itf->subsel(w->closure, key))
					/* nothing */;
				else if (key[0] == '*'
				      && !key[1]
				      && !value
				      && !*copy
				      && (w->flags & Mustach_With_ObjectIter))
					result = S_objiter;
				else
					result = S_none;
				key = getkey(&copy, sflags);
			}
		}
	}
	/* should it be compared? */
	if (result == S_ok && value) {
		if (!w->itf->compare)
			result = S_none;
		else {
			i = value[0] == '!';
			scmp = w->itf->compare(w->closure, &value[i]);
			switch (k) {
			case C_eq: j = scmp == 0; break;
			case C_lt: j = scmp < 0; break;
			case C_le: j = scmp <= 0; break;
			case C_gt: j = scmp > 0; break;
			case C_ge: j = scmp >= 0; break;
			default: j = i; break;
			}
			if (i == j)
				result = S_none;
		}
	}
	return result;
}

static int start_callback(void *closure)
{
	struct wrap *w = closure;
	return w->itf->start ? w->itf->start(w->closure) : MUSTACH_OK;
}

static void stop_callback(void *closure, int status)
{
	struct wrap *w = closure;
	if (w->itf->stop)
		w->itf->stop(w->closure, status);
}

static int emit(struct wrap *w, const char *buffer, size_t size, FILE *file)
{
	int r;

	if (w->writecb)
		r = w->writecb(file, buffer, size);
	else
		r = fwrite(buffer, 1, size, file) == size ? MUSTACH_OK : MUSTACH_ERROR_SYSTEM;
	return r;
}

static int emit_callback(void *closure, const char *buffer, size_t size, int escape, FILE *file)
{
	struct wrap *w = closure;
	int r;
	size_t s, i;
	char car;

	if (w->emitcb)
		r = w->emitcb(file, buffer, size, escape);
	else if (!escape)
		r = emit(w, buffer, size, file);
	else {
		i = 0;
		r = MUSTACH_OK;
		while(i < size && r == MUSTACH_OK) {
			s = i;
			while (i < size && (car = buffer[i]) != '<' && car != '>' && car != '&' && car != '"')
				i++;
			if (i != s)
				r = emit(w, &buffer[s], i - s, file);
			if (i < size && r == MUSTACH_OK) {
				switch(car) {
				case '<': r = emit(w, "&lt;", 4, file); break;
				case '>': r = emit(w, "&gt;", 4, file); break;
				case '&': r = emit(w, "&amp;", 5, file); break;
				case '"': r = emit(w, "&quot;", 6, file); break;
				}
				i++;
			}
		}
	}
	return r;
}

static int enter_callback(void *closure, const char *name)
{
	struct wrap *w = closure;
	enum sel s = sel(w, name);
	return s == S_none ? 0 : w->itf->enter(w->closure, s & S_objiter);
}

static int next_callback(void *closure)
{
	struct wrap *w = closure;
	return w->itf->next(w->closure);
}

static int leave_callback(void *closure)
{
	struct wrap *w = closure;
	return w->itf->leave(w->closure);
}

static int getoptional(struct wrap *w, const char *name, struct mustach_sbuf *sbuf)
{
	enum sel s = sel(w, name);
	if (!(s & S_ok))
		return 0;
	return w->itf->get(w->closure, sbuf, s & S_objiter);
}

static int get_callback(void *closure, const char *name, struct mustach_sbuf *sbuf)
{
	struct wrap *w = closure;
	if (getoptional(w, name, sbuf) <= 0) {
		if (w->flags & Mustach_With_ErrorUndefined)
			return MUSTACH_ERROR_UNDEFINED_TAG;
		sbuf->value = "";
	}
	return MUSTACH_OK;
}

#if MUSTACH_LOAD_TEMPLATE
static int get_partial_from_file(const char *name, struct mustach_sbuf *sbuf)
{
	static char extension[] = INCLUDE_PARTIAL_EXTENSION;
	size_t s;
	long pos;
	FILE *file;
	char *path, *buffer;

	/* allocate path */
	s = strlen(name);
	path = malloc(s + sizeof extension);
	if (path == NULL)
		return MUSTACH_ERROR_SYSTEM;

	/* try without extension first */
	memcpy(path, name, s + 1);
	file = fopen(path, "r");
	if (file == NULL) {
		memcpy(&path[s], extension, sizeof extension);
		file = fopen(path, "r");
	}
	free(path);

	/* if file opened */
	if (file == NULL)
		return MUSTACH_ERROR_PARTIAL_NOT_FOUND;

	/* compute file size */
	if (fseek(file, 0, SEEK_END) >= 0
	 && (pos = ftell(file)) >= 0
	 && fseek(file, 0, SEEK_SET) >= 0) {
		/* allocate value */
		s = (size_t)pos;
		buffer = malloc(s + 1);
		if (buffer != NULL) {
			/* read value */
			if (1 == fread(buffer, s, 1, file)) {
				/* force zero at end */
				sbuf->value = buffer;
				buffer[s] = 0;
				sbuf->freecb = free;
				fclose(file);
				return MUSTACH_OK;
			}
			free(buffer);
		}
	}
	fclose(file);
	return MUSTACH_ERROR_SYSTEM;
}
#endif

static int partial_callback(void *closure, const char *name, struct mustach_sbuf *sbuf)
{
	struct wrap *w = closure;
	int rc;
	if (mustach_wrap_get_partial != NULL) {
		rc = mustach_wrap_get_partial(name, sbuf);
		if (rc != MUSTACH_ERROR_PARTIAL_NOT_FOUND) {
			if (rc != MUSTACH_OK)
				sbuf->value = "";
			return rc;
		}
	}
#if MUSTACH_LOAD_TEMPLATE
	if (w->flags & Mustach_With_PartialDataFirst) {
		if (getoptional(w, name, sbuf) > 0)
			rc = MUSTACH_OK;
		else
			rc = get_partial_from_file(name, sbuf);
	}
	else {
		rc = get_partial_from_file(name, sbuf);
		if (rc != MUSTACH_OK &&  getoptional(w, name, sbuf) > 0)
			rc = MUSTACH_OK;
	}
#else
	rc = getoptional(w, name, sbuf) > 0 ?  MUSTACH_OK : MUSTACH_ERROR_PARTIAL_NOT_FOUND;
#endif
	if (rc != MUSTACH_OK)
		sbuf->value = "";
	return MUSTACH_OK;
}

const struct mustach_itf mustach_wrap_itf = {
	.start = start_callback,
	.put = NULL,
	.enter = enter_callback,
	.next = next_callback,
	.leave = leave_callback,
	.partial = partial_callback,
	.get = get_callback,
	.emit = emit_callback,
	.stop = stop_callback
};

static void wrap_init(struct wrap *wrap, const struct mustach_wrap_itf *itf, void *closure, int flags, mustach_emit_cb_t *emitcb, mustach_write_cb_t *writecb)
{
	if (flags & Mustach_With_Compare)
		flags |= Mustach_With_Equal;
	wrap->closure = closure;
	wrap->itf = itf;
	wrap->flags = flags;
	wrap->emitcb = emitcb;
	wrap->writecb = writecb;
}

int mustach_wrap_file(const char *template, size_t length, const struct mustach_wrap_itf *itf, void *closure, int flags, FILE *file)
{
	struct wrap w;
	wrap_init(&w, itf, closure, flags, NULL, NULL);
	return mustach_file(template, length, &mustach_wrap_itf, &w, flags, file);
}

int mustach_wrap_fd(const char *template, size_t length, const struct mustach_wrap_itf *itf, void *closure, int flags, int fd)
{
	struct wrap w;
	wrap_init(&w, itf, closure, flags, NULL, NULL);
	return mustach_fd(template, length, &mustach_wrap_itf, &w, flags, fd);
}

int mustach_wrap_mem(const char *template, size_t length, const struct mustach_wrap_itf *itf, void *closure, int flags, char **result, size_t *size)
{
	struct wrap w;
	wrap_init(&w, itf, closure, flags, NULL, NULL);
	return mustach_mem(template, length, &mustach_wrap_itf, &w, flags, result, size);
}

int mustach_wrap_write(const char *template, size_t length, const struct mustach_wrap_itf *itf, void *closure, int flags, mustach_write_cb_t *writecb, void *writeclosure)
{
	struct wrap w;
	wrap_init(&w, itf, closure, flags, NULL, writecb);
	return mustach_file(template, length, &mustach_wrap_itf, &w, flags, writeclosure);
}

int mustach_wrap_emit(const char *template, size_t length, const struct mustach_wrap_itf *itf, void *closure, int flags, mustach_emit_cb_t *emitcb, void *emitclosure)
{
	struct wrap w;
	wrap_init(&w, itf, closure, flags, emitcb, NULL);
	return mustach_file(template, length, &mustach_wrap_itf, &w, flags, emitclosure);
}