Lindenii Project Forge
Login

hare-htmpl

HTML templating engine for Hare
Commit info
ID
719d75e731437208d78add5f377dc5634e644a72
Author
Runxi Yu <me@runxiyu.org>
Author date
Thu, 13 Mar 2025 07:37:27 +0800
Committer
Runxi Yu <me@runxiyu.org>
Committer date
Thu, 13 Mar 2025 07:37:27 +0800
Actions
Reorganize directories
/chtmpl
/parse.c
/parse.h
*.o
*.a
*.so
*.so.*
CC = cc
YACC = yacc
CFLAGS = -Wall -Werror -O2 -fPIC
YFLAGS = -d
LDFLAGS =

VERSION = 1
SONAME = libchtmplr.so.$(VERSION)

TARGET = chtmpl
STATIC_LIB = libchtmplr.a
SHARED_LIB = libchtmplr.so.$(VERSION)
SHARED_LINK = libchtmplr.so

SRCS = chtmpl.c parse.y
OBJS = chtmpl.o parse.o

LIB_SRCS = chtmplr.c
LIB_OBJS = chtmplr.o

all: $(TARGET) $(STATIC_LIB) $(SHARED_LIB)

parse.c parse.h: parse.y
	$(YACC) $(YFLAGS) -o parse.c $<

chtmpl.o: chtmpl.c parse.h
	$(CC) $(CFLAGS) -c -o $@ $<

parse.o: parse.c
	$(CC) $(CFLAGS) -c -o $@ $<

chtmplr.o: chtmplr.c chtmplr.h
	$(CC) $(CFLAGS) -c -o $@ $<

$(STATIC_LIB): $(LIB_OBJS)
	ar rcs $@ $<

$(SHARED_LIB): $(LIB_OBJS)
	$(CC) -shared -Wl,-soname,$(SONAME) -o $@ $< $(LDFLAGS)
	ln -sf $(SHARED_LIB) $(SHARED_LINK)

$(TARGET): $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $(OBJS)

clean:
	rm -f $(TARGET) $(OBJS) parse.c parse.h $(LIB_OBJS) $(STATIC_LIB) $(SHARED_LIB) $(SHARED_LINK) *~

PREFIX ?= /usr/local
LIBDIR ?= $(PREFIX)/lib
INCLUDEDIR ?= $(PREFIX)/include

install: install-bin install-lib

install-bin: $(TARGET)
	install -d $(DESTDIR)$(PREFIX)/bin
	install -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin/

install-lib: $(STATIC_LIB) $(SHARED_LIB)
	install -d $(DESTDIR)$(INCLUDEDIR)
	install -m 644 chtmplr.h $(DESTDIR)$(INCLUDEDIR)/

	install -d $(DESTDIR)$(LIBDIR)
	install -m 644 $(STATIC_LIB) $(DESTDIR)$(LIBDIR)/

	install -m 755 $(SHARED_LIB) $(DESTDIR)$(LIBDIR)/
	ln -sf $(SHARED_LIB) $(DESTDIR)$(LIBDIR)/$(SHARED_LINK)
	ln -sf $(SHARED_LIB) $(DESTDIR)$(LIBDIR)/$(SONAME)
	
	-if [ $$(id -u) -eq 0 ]; then ldconfig; fi

.PHONY: all clean install install-bin install-lib
.\" Copyright (c) 2022 Omar Polo <op@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd January 6, 2022
.Dt TEMPLATE 1
.Os
.Sh NAME
.Nm chtmpl
.Nd templating system compiler
.Sh SYNOPSIS
.Nm
.Op Fl G
.Op Fl o Ar out
.Op Ar
.Sh DESCRIPTION
.Nm
is an utility that converts files written in the
.Xr chtmpl 7
format format to a set of routine writtens in the C programming
language.
.Nm
converts the files given as arguments or from standard input, and
writes to standard output.
.Pp
The options are as follows:
.Bl -tag -width Ds
.It Fl G
Do not emit debug info in the generated source.
It's disabled by default, unless
.Nm
is reading from standard input.
.It Fl o Ar out
Write output to file.
.Ar out
will be created or truncated if exists and will be removed if
.Nm
encounters any error.
.El
.Sh EXIT STATUS
.Ex -std
.Sh SEE ALSO
.Xr chtmpl 7
.Sh AUTHORS
.An -nosplit
The
.Nm
utility was written by
.An Omar Polo Aq Mt op@openbsd.org .
.Sh CAVEATS
The compiler is very naive, so there are quite a few shortcomings:
.Bl -bullet -compact
.It
No attempt is made to validate the C code provided inline, nor the
validity of the arguments to many constructs.
.It
The generated code assumes that a variable called
.Va tp
of type
.Vt struct template *
is in scope inside each block.
.It
Each block may have additional variables used for the template
generation implicitly defined: to avoid clashes, don't name variables
or arguments with the
.Sq tp_
prefix.
.It
Blanks are, in most cases, trimmed.
Normally this is not a problem, but a workaround is needed in case
they need to be preserved, for e.g.:
.Bd -literal -offset indent
Name: {{ " " }} {{ render name_field(tp) }}
.Ed
.El
.\" Copyright (c) 2022 Omar Polo <op@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd January 6, 2022
.Dt TEMPLATE 7
.Os
.Sh NAME
.Nm chtmpl
.Nd templating language
.Sh DESCRIPTION
.Nm
is a language used to define programs that output data in some way.
These programs are called
.Dq templates .
A
.Nm
file is assumed to be compiled using the
.Xr chtmpl 1
utility into C code, to be further compiled as part of a bigger
application.
The language itself is format-agnostic and can thus be used to produce
various type of outputs.
.Pp
There are two special sequences:
.Bl -tag -width 9m
.It Cm {{ Ar ... Cm }}
used for
.Nm
special syntax.
.It Cm {! Ar ... Cm !}
used to include literal C code.
This is the only special syntax permitted as top-level, except for block
definition and includes.
.El
.Pp
The basic unit of a
.Nm
file is the block.
Each block is turned into a C function that output its content via some
provided functions.
Here's an example of a block:
.Bd -literal -offset indent
{{ define tp_base(struct template *tp, const char *title) }}
<!doctype html>
<html>
	<head>
		<title>{{ title }}</title>
	</head>
	<body>
		{{ render tp->tp_body(tp) }}
	</body>
</html>
{{ end }}
.Ed
.Ss SPECIAL SYNTAX
This section is a reference for all the special syntaxes supported.
.Bl -tag -width Ds
.It Cm {{ Ic include Ar file Cm }}
Include additional template files.
.It Cm {{ Ic define Ar name Ns ( Ar arguments ... ) Cm }} Ar body Cm {{ Ic end Cm }}
Defines the block
.Ar name
with the given
.Ar arguments .
.Ar body
will be outputted as-is via the provided functions
.Pq i.e.\& is still escaped eventually
and can contain all the special syntaxes documented here except
.Ic include
and
.Ic define .
.It Cm {{ Ic render Ar expression() Cm }}
Executes
.Ar expression()
and terminate the template if it returns -1.
It's used to render (call) another template.
.It Cm {{ Ic printf Ar fmt , Ar arguments ... Cm }}
Outputs the string that would be produced by calling
.Xr printf 3
with the given
.Ar fmt
format string and the given
.Ar arguments .
.It Cm {{ Ic if Ar expr Cm }} Ar ... Cm {{ Ic elseif Ar expr Cm }} Ar ... Cm {{ Ic else Cm }} Ar ... Cm {{ Ic end Cm }}
Conditional evaluation.
.Ic elseif
can be provided zero or more times,
.Ic else
only zero or one time and always for last.
.It Cm {{ Ic for Ar ... ; Ar ... ; Ar ... Cm }} Ar ... Cm {{ Ic end Cm }}
Looping construct similar to the C for loop.
.It Cm {{ Ic tailq-foreach Ar var head fieldname Cm }} Ar .. Cm {{ Ic end Cm }}
Looping construct similar to the queue.h macro TAILQ_FOREACH.
.It Cm {{ Ic while Ar ... Cm }} Ar ... Cm {{ Ic end Cm }}
Looping construct similar to the C while loop.
.It Cm {{ Ar expression Cm \&| Ic unsafe Cm }}
Output
.Ar expression
as-is.
.It Cm {{ Ar expression Cm \&| Ic urlescape Cm }}
Output
.Ar expression
escaped in a way that can be made part of an URL.
.It Cm {{ Ar expression Cm }}
Output
.Ar expression
with the default escaping.
.El
.Sh SEE ALSO
.Xr chtmpl 1
.Sh AUTHORS
.An -nosplit
The
.Nm
reference was written by
.An Omar Polo Aq Mt op@openbsd.org .
/*
 * Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int	 parse(FILE *, const char *);

int	 nodebug;

void usage(char *progname)
{
	fprintf(stderr, "usage: %s [-G] [-o out] [file ...]\n", progname);
	exit(1);
}

int
main(int argc, char **argv)
{
	FILE		*fp = stdout;
	const char	*out = NULL;
	int		 ch, i;

	while ((ch = getopt(argc, argv, "Go:")) != -1) {
		switch (ch) {
		case 'G':
			nodebug = 1;
			break;
		case 'o':
			out = optarg;
			break;
		default:
			if (argc == 0)
				usage("chtmpl");
			else
				usage(argv[0]);
		}
	}
	argc -= optind;
	argv += optind;

	if (out && (fp = fopen(out, "w")) == NULL)
		err(1, "can't open %s", out);

	if (argc == 0) {
		if (parse(fp, "/dev/stdin") == -1)
			goto err;
	} else {
		for (i = 0; i < argc; ++i)
			if (parse(fp, argv[i]) == -1)
				goto err;
	}

	if (ferror(fp))
		goto err;

	if (fclose(fp) == -1) {
		fp = NULL;
		goto err;
	}

	return (0);

err:
	if (fp)
		fclose(fp);
	if (out && unlink(out) == -1)
		err(1, "unlink %s", out);
	return (1);
}
/*
 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "chtmplr.h"

int
tp_write(struct template *tp, const char *str, size_t len)
{
	size_t	 avail;

	while (len > 0) {
		avail = tp->tp_cap - tp->tp_len;
		if (avail == 0) {
			if (template_flush(tp) == -1)
				return (-1);
			avail = tp->tp_cap;
		}

		if (len < avail)
			avail = len;

		memcpy(tp->tp_buf + tp->tp_len, str, avail);
		tp->tp_len += avail;
		str += avail;
		len -= avail;
	}

	return (0);
}

int
tp_writes(struct template *tp, const char *str)
{
	return (tp_write(tp, str, strlen(str)));
}

int
tp_writef(struct template *tp, const char *fmt, ...)
{
	va_list	 ap;
	char	*str;
	int	 r;

	va_start(ap, fmt);
	r = vasprintf(&str, fmt, ap);
	va_end(ap);
	if (r == -1)
		return (-1);
	r = tp_write(tp, str, r);
	free(str);
	return (r);
}

int
tp_urlescape(struct template *tp, const char *str)
{
	int	 r;
	char	 tmp[4];

	if (str == NULL)
		return (0);

	for (; *str; ++str) {
		if (iscntrl((unsigned char)*str) ||
		    isspace((unsigned char)*str) ||
		    *str == '\'' || *str == '"' || *str == '\\') {
			r = snprintf(tmp, sizeof(tmp), "%%%2X", *str);
			if (r < 0  || (size_t)r >= sizeof(tmp))
				return (0);
			if (tp_write(tp, tmp, r) == -1)
				return (-1);
		} else {
			if (tp_write(tp, str, 1) == -1)
				return (-1);
		}
	}

	return (0);
}

static inline int
htmlescape(struct template *tp, char c)
{
	switch (c) {
	case '<':
		return tp_write(tp, "&lt;", 4);
	case '>':
		return tp_write(tp, "&gt;", 4);
	case '&':
		return tp_write(tp, "&amp;", 5);
	case '"':
		return tp_write(tp, "&quot;", 6);
	case '\'':
		return tp_write(tp, "&apos;", 6);
	default:
		return tp_write(tp, &c, 1);
	}
}

int
tp_htmlescape(struct template *tp, const char *str)
{
	if (str == NULL)
		return (0);

	for (; *str; ++str) {
		if (htmlescape(tp, *str) == -1)
			return (-1);
	}

	return (0);
}

int
tp_write_htmlescape(struct template *tp, const char *str, size_t len)
{
	size_t i;

	for (i = 0; i < len; ++i) {
		if (htmlescape(tp, str[i]) == -1)
			return (-1);
	}

	return (0);
}

struct template *
template(void *arg, tmpl_write writefn, char *buf, size_t siz)
{
	struct template *tp;

	if ((tp = calloc(1, sizeof(*tp))) == NULL)
		return (NULL);

	tp->tp_arg = arg;
	tp->tp_write = writefn;
	tp->tp_buf = buf;
	tp->tp_cap = siz;

	return (tp);
}

int
template_flush(struct template *tp)
{
	if (tp->tp_len == 0)
		return (0);

	if (tp->tp_write(tp->tp_arg, tp->tp_buf, tp->tp_len) == -1)
		return (-1);
	tp->tp_len = 0;
	return (0);
}

void
template_free(struct template *tp)
{
	free(tp->tp_tmp);
	free(tp);
}
/*
 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef TMPL_H
#define TMPL_H

struct template;

typedef int (*tmpl_write)(void *, const void *, size_t);

struct template {
	void		*tp_arg;
	char		*tp_tmp;
	tmpl_write	 tp_write;
	char		*tp_buf;
	size_t		 tp_len;
	size_t		 tp_cap;
};

int	 tp_write(struct template *, const char *, size_t);
int	 tp_writes(struct template *, const char *);
int	 tp_writef(struct template *, const char *, ...)
	    __attribute__((__format__ (printf, 2, 3)));
int	 tp_urlescape(struct template *, const char *);
int	 tp_htmlescape(struct template *, const char *);
int	 tp_write_htmlescape(struct template *, const char *, size_t);

struct template	*template(void *, tmpl_write, char *, size_t);
int		 template_flush(struct template *);
void		 template_free(struct template *);

#endif
*.o
/parse.c
/parse.h
/htmplgen
CC = cc
YACC = yacc
CFLAGS = -Wall -Werror -O2
YFLAGS = -d
LDFLAGS =

TARGET = htmplgen

PREFIX ?= /usr/local

SRCS = htmplgen.c parse.y
OBJS = htmplgen.o parse.o

all: $(TARGET)

parse.c parse.h: parse.y
	$(YACC) $(YFLAGS) -o parse.c $<

htmplgen.o: htmplgen.c parse.h
	$(CC) $(CFLAGS) -c -o $@ $<

parse.o: parse.c
	$(CC) $(CFLAGS) -c -o $@ $<

$(TARGET): $(OBJS)
	$(CC) $(LDFLAGS) -o $@ $(OBJS)

clean:
	rm -f $(TARGET) $(OBJS) parse.c parse.h *~

install: $(TARGET)
	install -d $(DESTDIR)$(PREFIX)/bin
	install -m 755 $(TARGET) $(DESTDIR)$(PREFIX)/bin/

.PHONY: all clean install
/*
 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
 * Copyright (c) 2007-2016 Reyk Floeter <reyk@openbsd.org>
 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
 * Copyright (c) 2001 Markus Friedl.  All rights reserved.
 * Copyright (c) 2001 Daniel Hartmeier.  All rights reserved.
 * Copyright (c) 2001 Theo de Raadt.  All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

%{

#include <sys/queue.h>

#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif

TAILQ_HEAD(files, file)		 files = TAILQ_HEAD_INITIALIZER(files);
static struct file {
	TAILQ_ENTRY(file)	 entry;
	FILE			*stream;
	char			*name;
	size_t			 ungetpos;
	size_t			 ungetsize;
	unsigned char		*ungetbuf;
	int			 eof_reached;
	int			 lineno;
	int			 errors;
} *file, *topfile;
int		 parse(FILE *, const char *);
struct file	*pushfile(const char *, int);
int		 popfile(void);
int		 yyparse(void);
int		 yylex(void);
int		 yyerror(const char *, ...)
    __attribute__((__format__ (printf, 1, 2)))
    __attribute__((__nonnull__ (1)));
int		 kw_cmp(const void *, const void *);
int		 lookup(char *);
int		 igetc(void);
int		 lgetc(int);
void		 lungetc(int);
int		 findeol(void);

void		 dbg(void);
void		 printq(const char *);

extern int	 nodebug;

static FILE	*fp;

static int	 block;
static int	 in_define;
static int	 errors;
static int	 lastline = -1;

typedef struct {
	union {
		char		*string;
	} v;
	int lineno;
} YYSTYPE;

%}

%token	DEFINE ELSE END ERROR FINALLY FOR IF INCLUDE PRINTF
%token	RENDER TQFOREACH UNSAFE URLESCAPE WHILE
%token	<v.string>	STRING
%type	<v.string>	string nstring
%type	<v.string>	stringy

%%

grammar		: /* empty */
		| grammar include
		| grammar verbatim
		| grammar block
		| grammar error		{ file->errors++; }
		;

include		: INCLUDE STRING {
			struct file	*nfile;

			if ((nfile = pushfile($2, 0)) == NULL) {
				yyerror("failed to include file %s", $2);
				free($2);
				YYERROR;
			}
			free($2);

			file = nfile;
			lungetc('\n');
		}
		;

verbatim	: '!' verbatim1 '!' {
			if (in_define) {
				/* TODO: check template status and exit in case */
			}
		}
		;

verbatim1	: /* empty */
		| verbatim1 STRING {
			if (*$2 != '\0') {
				dbg();
				fprintf(fp, "%s\n", $2);
			}
			free($2);
		}
		;

verbatims	: /* empty */
		| verbatims verbatim
		;

raw		: nstring {
			dbg();
			fprintf(fp, "if ((tp_ret = tp_write(tp, ");
			printq($1);
			fprintf(fp, ", %zu)) == -1) goto err;\n",
			    strlen($1));

			free($1);
		}
		;

block		: define body end {
			fputs("err:\n", fp);
			fputs("return tp_ret;\n", fp);
			fputs("}\n", fp);
			in_define = 0;
		}
		| define body finally end {
			fputs("return tp_ret;\n", fp);
			fputs("}\n", fp);
			in_define = 0;
		}
		;

define		: '{' DEFINE string '}' {
			in_define = 1;

			dbg();
			fprintf(fp, "int\n%s\n{\n", $3);
			fputs("int tp_ret = 0;\n", fp);

			free($3);
		}
		;

body		: /* empty */
		| body verbatim
		| body raw
		| body special
		;

special		: '{' RENDER string '}' {
			dbg();
			fprintf(fp, "if ((tp_ret = %s) == -1) goto err;\n",
			    $3);
			free($3);
		}
		| printf
		| if body endif			{ fputs("}\n", fp); }
		| loop
		| '{' string '|' UNSAFE '}' {
			dbg();
			fprintf(fp,
			    "if ((tp_ret = tp_writes(tp, %s)) == -1)\n",
			    $2);
			fputs("goto err;\n", fp);
			free($2);
		}
		| '{' string '|' URLESCAPE '}' {
			dbg();
			fprintf(fp,
			    "if ((tp_ret = tp_urlescape(tp, %s)) == -1)\n",
			    $2);
			fputs("goto err;\n", fp);
			free($2);
		}
		| '{' string '}' {
			dbg();
			fprintf(fp,
			    "if ((tp_ret = tp_htmlescape(tp, %s)) == -1)\n",
			    $2);
			fputs("goto err;\n", fp);
			free($2);
		}
		;

printf		: '{' PRINTF {
			dbg();
			fprintf(fp, "if (asprintf(&tp->tp_tmp, ");
		} printfargs '}' {
			fputs(") == -1)\n", fp);
			fputs("goto err;\n", fp);
			fputs("if ((tp_ret = tp_htmlescape(tp, tp->tp_tmp)) "
			    "== -1)\n", fp);
			fputs("goto err;\n", fp);
			fputs("free(tp->tp_tmp);\n", fp);
			fputs("tp->tp_tmp = NULL;\n", fp);
		}
		;

printfargs	: /* empty */
		| printfargs STRING {
			fprintf(fp, " %s", $2);
			free($2);
		}
		;

if		: '{' IF stringy '}' {
			dbg();
			fprintf(fp, "if (%s) {\n", $3);
			free($3);
		}
		;

endif		: end
		| else body end
		| elsif body endif
		;

elsif		: '{' ELSE IF stringy '}' {
			dbg();
			fprintf(fp, "} else if (%s) {\n", $4);
			free($4);
		}
		;

else		: '{' ELSE '}' {
			dbg();
			fputs("} else {\n", fp);
		}
		;

loop		: '{' FOR stringy '}' {
			fprintf(fp, "for (%s) {\n", $3);
			free($3);
		} body end {
			fputs("}\n", fp);
		}
		| '{' TQFOREACH STRING STRING STRING '}' {
			fprintf(fp, "TAILQ_FOREACH(%s, %s, %s) {\n",
			    $3, $4, $5);
			free($3);
			free($4);
			free($5);
		} body end {
			fputs("}\n", fp);
		}
		| '{' WHILE stringy '}' {
			fprintf(fp, "while (%s) {\n", $3);
			free($3);
		} body end {
			fputs("}\n", fp);
		}
		;

end		: '{' END '}'
		;

finally		: '{' FINALLY '}' {
			dbg();
			fputs("err:\n", fp);
		} verbatims
		;

nstring	:	STRING nstring {
			if (asprintf(&$$, "%s%s", $1, $2) == -1)
				err(1, "asprintf");
			free($1);
			free($2);
		}
		| STRING
		;

string		: STRING string {
			if (asprintf(&$$, "%s %s", $1, $2) == -1)
				err(1, "asprintf");
			free($1);
			free($2);
		}
		| STRING
		;

stringy		: STRING
		| STRING stringy {
			if (asprintf(&$$, "%s %s", $1, $2) == -1)
				err(1, "asprintf");
			free($1);
			free($2);
		}
		| '|' stringy {
			if (asprintf(&$$, "|%s", $2) == -1)
				err(1, "asprintf");
			free($2);
		}
		;

%%

struct keywords {
	const char	*k_name;
	int		 k_val;
};

int
yyerror(const char *fmt, ...)
{
	va_list	 ap;
	char	*msg;

	file->errors++;
	va_start(ap, fmt);
	if (vasprintf(&msg, fmt, ap) == -1)
		err(1, "yyerror vasprintf");
	va_end(ap);
	fprintf(stderr, "%s:%d: %s\n", file->name, yylval.lineno, msg);
	free(msg);
	return (0);
}

int
kw_cmp(const void *k, const void *e)
{
	return (strcmp(k, ((const struct keywords *)e)->k_name));
}

int
lookup(char *s)
{
	/* this has to be sorted always */
	static const struct keywords keywords[] = {
		{ "define",		DEFINE },
		{ "else",		ELSE },
		{ "end",		END },
		{ "finally",		FINALLY },
		{ "for",		FOR },
		{ "if",			IF },
		{ "include",		INCLUDE },
		{ "printf",		PRINTF },
		{ "render",		RENDER },
		{ "tailq-foreach",	TQFOREACH },
		{ "unsafe",		UNSAFE },
		{ "urlescape",		URLESCAPE },
		{ "while",		WHILE },
	};
	const struct keywords	*p;

	p = bsearch(s, keywords, nitems(keywords), sizeof(keywords[0]),
	    kw_cmp);

	if (p)
		return (p->k_val);
	else
		return (STRING);
}

#define START_EXPAND	1
#define DONE_EXPAND	2

static int	expanding;

int
igetc(void)
{
	int	c;

	while (1) {
		if (file->ungetpos > 0)
			c = file->ungetbuf[--file->ungetpos];
		else
			c = getc(file->stream);

		if (c == START_EXPAND)
			expanding = 1;
		else if (c == DONE_EXPAND)
			expanding = 0;
		else
			break;
	}
	return (c);
}

int
lgetc(int quotec)
{
	int		c;

	if (quotec) {
		if ((c = igetc()) == EOF) {
			yyerror("reached end of filewhile parsing "
			    "quoted string");
			if (file == topfile || popfile() == EOF)
				return (EOF);
			return (quotec);
		}
		return (c);
	}

	c = igetc();
	if (c == '\t' || c == ' ') {
		/* Compress blanks to a sigle space. */
		do {
			c = getc(file->stream);
		} while (c == '\t'  || c == ' ');
		ungetc(c, file->stream);
		c = ' ';
	}

	if (c == EOF) {
		/*
		 * Fake EOL when hit EOF for the first time. This gets line
		 * count right if last line in included file is syntactically
		 * invalid and has no newline.
		 */
		if (file->eof_reached == 0) {
			file->eof_reached = 1;
			return ('\n');
		}
		while (c == EOF) {
			if (file == topfile || popfile() == EOF)
				return (EOF);
			c = igetc();
		}
	}
	return (c);
}

void
lungetc(int c)
{
	if (c == EOF)
		return;

	if (file->ungetpos >= file->ungetsize) {
		void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
		if (p == NULL)
			err(1, "reallocarray");
		file->ungetbuf = p;
		file->ungetsize *= 2;
	}
	file->ungetbuf[file->ungetpos++] = c;
}

int
findeol(void)
{
	int	c;

	/* skip to either EOF or the first real EOL */
	while (1) {
		c = lgetc(0);
		if (c == '\n') {
			file->lineno++;
			break;
		}
		if (c == EOF)
			break;
	}
	return (ERROR);
}

int
yylex(void)
{
	char		 buf[8096];
	char		*p = buf;
	int		 c;
	int		 token;
	int		 starting = 0;
	int		 ending = 0;
	int		 quote = 0;

	if (!in_define && block == 0) {
		while ((c = lgetc(0)) != '{' && c != EOF) {
			if (c == '\n')
				file->lineno++;
		}

		if (c == EOF)
			return (0);

newblock:
		c = lgetc(0);
		if (c == '{' || c == '!') {
			if (c == '{')
				block = '}';
			else
				block = c;
			return (c);
		}
		if (c == '\n')
			file->lineno++;
	}

	while ((c = lgetc(0)) == ' ' || c == '\t' || c == '\n') {
		if (c == '\n')
			file->lineno++;
	}

	if (c == EOF) {
		yyerror("unterminated block");
		return (0);
	}

	yylval.lineno = file->lineno;

	if (block != 0 && c == block) {
		if ((c = lgetc(0)) == '}') {
			if (block == '!') {
				block = 0;
				return ('!');
			}
			block = 0;
			return ('}');
		}
		lungetc(c);
		c = block;
	}

	if (in_define && block == 0) {
		if (c == '{')
			goto newblock;

		do {
			if (starting) {
				if (c == '!' || c == '{') {
					lungetc(c);
					lungetc('{');
					break;
				}
				starting = 0;
				lungetc(c);
				c = '{';
			} else if (c == '{') {
				starting = 1;
				continue;
			} else if (c == '\n')
				break;

			*p++ = c;
			if ((size_t)(p - buf) >= sizeof(buf)) {
				yyerror("string too long");
				return (findeol());
			}
		} while ((c = lgetc(0)) != EOF);
		*p = '\0';
		if (c == EOF) {
			yyerror("unterminated block");
			return (0);
		}
		if (c == '\n')
			file->lineno++;
		if ((yylval.v.string = strdup(buf)) == NULL)
			err(1, "strdup");
		return (STRING);
	}

	if (block == '!') {
		do {
			if (ending) {
				if (c == '}') {
					lungetc(c);
					lungetc(block);
					break;
				}
				ending = 0;
				lungetc(c);
				c = block;
			} else if (c == '!') {
				ending = 1;
				continue;
			} else if (c == '\n')
				break;

			*p++ = c;
			if ((size_t)(p - buf) >= sizeof(buf)) {
				yyerror("line too long");
				return (findeol());
			}
		} while ((c = lgetc(0)) != EOF);
		*p = '\0';

		if (c == EOF) {
			yyerror("unterminated block");
			return (0);
		}
		if (c == '\n')
			file->lineno++;

		if ((yylval.v.string = strdup(buf)) == NULL)
			err(1, "strdup");
		return (STRING);
	}

	if (c == '|')
		return (c);

	do {
		if (!quote && isspace((unsigned char)c))
			break;

		if (c == '"')
			quote = !quote;

		if (!quote && c == '|') {
			lungetc(c);
			break;
		}

		if (ending) {
			if (c == '}') {
				lungetc(c);
				lungetc('}');
				break;
			}
			ending = 0;
			lungetc(c);
			c = block;
		} else if (!quote && c == '}') {
			ending = 1;
			continue;
		}

		*p++ = c;
		if ((size_t)(p - buf) >= sizeof(buf)) {
			yyerror("string too long");
			return (findeol());
		}
	} while ((c = lgetc(0)) != EOF);
	*p = '\0';

	if (c == EOF) {
		yyerror(quote ? "unterminated quote" : "unterminated block");
		return (0);
	}
	if (c ==  '\n')
		file->lineno++;
	if ((token = lookup(buf)) == STRING)
		if ((yylval.v.string = strdup(buf)) == NULL)
			err(1, "strdup");
	return (token);
}

struct file *
pushfile(const char *name, int secret)
{
	struct file	*nfile;

	if ((nfile = calloc(1, sizeof(*nfile))) == NULL)
		err(1, "calloc");
	if ((nfile->name = strdup(name)) == NULL)
		err(1, "strdup");
	if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
		warn("can't open %s", nfile->name);
		free(nfile->name);
		free(nfile);
		return (NULL);
	}
	nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
	nfile->ungetsize = 16;
	nfile->ungetbuf = malloc(nfile->ungetsize);
	if (nfile->ungetbuf == NULL)
		err(1, "malloc");
	TAILQ_INSERT_TAIL(&files, nfile, entry);
	return (nfile);
}

int
popfile(void)
{
	struct file	*prev;

	if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
		prev->errors += file->errors;

	TAILQ_REMOVE(&files, file, entry);
	fclose(file->stream);
	free(file->name);
	free(file->ungetbuf);
	free(file);
	file = prev;
	return (file ? 0 : EOF);
}

int
parse(FILE *outfile, const char *filename)
{
	fp = outfile;

	if ((file = pushfile(filename, 0)) == 0)
		return (-1);
	topfile = file;

	yyparse();
	errors = file->errors;
	popfile();

	return (errors ? -1 : 0);
}

void
dbg(void)
{
	if (nodebug)
		return;

	if (yylval.lineno == lastline + 1) {
		lastline = yylval.lineno;
		return;
	}
	lastline = yylval.lineno;

	fprintf(fp, "#line %d ", yylval.lineno);
	printq(file->name);
	putc('\n', fp);
}

void
printq(const char *str)
{
	putc('"', fp);
	for (; *str; ++str) {
		if (*str == '"')
			putc('\\', fp);
		putc(*str, fp);
	}
	putc('"', fp);
}