From c3d3cc8b9a2551b58573ba419452456f69fb04e9 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 12 Mar 2025 08:34:14 +0800 Subject: [PATCH] Initial commit --- .gitignore | 4 ++++ LICENSE | 17 +++++++++++++++++ Makefile | 35 +++++++++++++++++++++++++++++++++++ README.md | 30 ++++++++++++++++++++++++++++++ parse.y | 756 +++++++++++++++++++++++++++++++++++++++++++++++++++++ template.1 | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ template.7 | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++ template.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tmpl.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tmpl.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..2d4e44cdce8dfe6d05142302bb90fa8845e445e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/chtmpl +/parse.c +/parse.h +*.o diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..7738476ed148626e7a0e97c2821c6a31c0deb961 --- /dev/null +++ b/LICENSE @@ -0,0 +1,17 @@ +Copyright (c) 2025 Runxi Yu +Copyright (c) 2017-2024 Stefan Sperling + +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. + +See the individual source code files for additional licence statements +by other copyright holders. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..15ec6a975040051690f332011af760ba9465f33b --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +CC = cc +YACC = yacc +CFLAGS = -Wall -Werror -O2 +YFLAGS = -d +LDFLAGS = + +TARGET = chtmpl + +PREFIX ?= /usr/local + +SRCS = template.c parse.y +OBJS = template.o parse.o + +all: $(TARGET) + +parse.c parse.h: parse.y + $(YACC) $(YFLAGS) -o parse.c $< + +template.o: template.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 diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b02ea64d0d31da36593f884c67cb024466bfe255 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# chtmpl – A simple HTML templating engine + +This is a portable fork of the HTML templating engine used in +[Got](https://got.gameoftrees.org/?action=summary&path=got.git) +and described in +[Omar Polo's blog post](https://www.omarpolo.com/post/template.html). + +## License + +This inherits the ISC license from the original code. See the LICENSE file. + +## Contributing + +Create a branch that begins with `contrib/` and push to the +[main repo](https://forge.runxiyu.org/lib/:/repos/chtmpl/) +via SSH directly. + +``` +git clone ssh://forge.runxiyu.org/lib/:/repos/chtmpl/ +cd chtmpl +git checkout -b contrib/whatever +# edit and commit stuff +git push -u origin HEAD +``` + +## Support + +[`#chat`](https://webirc.runxiyu.org/kiwiirc/#chat) +on +[irc.runxiyu.org](https://irc.runxiyu.org/). diff --git a/parse.y b/parse.y new file mode 100644 index 0000000000000000000000000000000000000000..59cf6888097e2e08fde77614c4ea7f9517e3f4ac --- /dev/null +++ b/parse.y @@ -0,0 +1,756 @@ +/* + * Copyright (c) 2022 Omar Polo + * Copyright (c) 2007-2016 Reyk Floeter + * Copyright (c) 2004, 2005 Esben Norby + * Copyright (c) 2004 Ryan McBride + * Copyright (c) 2002, 2003, 2004 Henning Brauer + * 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 + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 STRING +%type string nstring +%type 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); +} diff --git a/template.1 b/template.1 new file mode 100644 index 0000000000000000000000000000000000000000..7fedd72e9e4ad82be4b969ab099a0cb4ebdde049 --- /dev/null +++ b/template.1 @@ -0,0 +1,85 @@ +.\" Copyright (c) 2022 Omar Polo +.\" +.\" 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 template +.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 template 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 template 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 diff --git a/template.7 b/template.7 new file mode 100644 index 0000000000000000000000000000000000000000..cfbb3973bf63eea0c29ae560c37fcdb4322207c0 --- /dev/null +++ b/template.7 @@ -0,0 +1,127 @@ +.\" Copyright (c) 2022 Omar Polo +.\" +.\" 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 template +.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 template 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) }} + + + + {{ title }} + + + {{ render tp->tp_body(tp) }} + + +{{ 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 template 1 +.Sh AUTHORS +.An -nosplit +The +.Nm +reference was written by +.An Omar Polo Aq Mt op@openbsd.org . diff --git a/template.c b/template.c new file mode 100644 index 0000000000000000000000000000000000000000..e4a2827d95f22383eb69df9cc89b318bb1f4ded9 --- /dev/null +++ b/template.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2025 Runxi Yu + * Copyright (c) 2022 Omar Polo + * + * 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 +#include +#include +#include + +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); +} diff --git a/tmpl.c b/tmpl.c new file mode 100644 index 0000000000000000000000000000000000000000..a462a35fdd53ed1e7997e205fd621e462ad511bc --- /dev/null +++ b/tmpl.c @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2022 Omar Polo + * + * 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 +#include +#include +#include +#include + +#include "tmpl.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, "<", 4); + case '>': + return tp_write(tp, ">", 4); + case '&': + return tp_write(tp, "&", 5); + case '"': + return tp_write(tp, """, 6); + case '\'': + return tp_write(tp, "'", 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); +} diff --git a/tmpl.h b/tmpl.h new file mode 100644 index 0000000000000000000000000000000000000000..3246735b8276b8bcf62702651f4c61e069bf7158 --- /dev/null +++ b/tmpl.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 Omar Polo + * + * 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 -- 2.48.1