commit 2c02675e51b8f7413665e81e1e2f3bf77ee9acb7 from: Omar Polo date: Wed Dec 14 18:05:43 2022 UTC import template ok tracey@ commit - eec68231a79adb345b1a6a3194e5ddb353570356 commit + 2c02675e51b8f7413665e81e1e2f3bf77ee9acb7 blob - bd9d446ba6e36728e565674927327fb69fc7b3c9 blob + 819941027860474c6e36a936822d7c2f9c640bbc --- Makefile +++ Makefile @@ -7,7 +7,7 @@ SUBDIR += regress .endif .if make(clean) || make(obj) || make(release) -SUBDIR += gotweb gotwebd gotd gotsh gotctl +SUBDIR += gotweb gotwebd gotd gotsh gotctl template .endif .if make(tags) || make(cleandir) @@ -33,13 +33,19 @@ dist: clean diff -u got-dist.txt got-dist.txt.new rm got-dist.txt.new +tmpl: + ${MAKE} -C template + +tmpl-regress: + ${MAKE} -C regress/template + web: ${MAKE} -C gotweb web-install: ${MAKE} -C gotweb install -webd: +webd: tmpl ${MAKE} -C gotwebd webd-install: blob - /dev/null blob + e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 (mode 644) blob - /dev/null blob + c2d0fad50754e9ec3740571ad5e77d7dc475ab26 (mode 644) --- /dev/null +++ regress/template/01-noise-only.tmpl @@ -0,0 +1,2 @@ +only +noise blob - /dev/null blob + c35618a4287faab518c419c4bbbe06ecbca7f157 (mode 644) --- /dev/null +++ regress/template/02-only-verbatim.tmpl @@ -0,0 +1,17 @@ +{! +#include + +#include "tmpl.h" +!} + +noise {! /* woops */ !} +here + +{! +int +main(void) +{ + puts("hello, world!"); + return (0); +} +!} blob - /dev/null blob + 270c611ee72c567bc1b2abec4cbc345bab9f15ba (mode 644) --- /dev/null +++ regress/template/02.expected @@ -0,0 +1 @@ +hello, world! blob - /dev/null blob + eee67b419ef25bca620f07fbef7169020bd6a490 (mode 644) --- /dev/null +++ regress/template/03-block.tmpl @@ -0,0 +1,24 @@ +{! +#include + +#include "tmpl.h" + +int base(struct template *, const char *); +!} + +{{ define base(struct template *tp, const char *title) }} +{! char *foo = NULL; !} + + + + {{ title }} + + {! /* TODO: frobnicate this line! */ !} +

{{ title }}

+ {{ " | " }} + {{ "other stuff" }} + + +{{ finally }} +{! free(foo); !} +{{ end }} blob - /dev/null blob + 0f766820b9e5de1343ca8034bea3b2b0a4418221 (mode 644) --- /dev/null +++ regress/template/03.expected @@ -0,0 +1,2 @@ + *hello*

*hello*

| other stuff +<hello>

<hello>

| other stuff blob - /dev/null blob + 5494a40b208b5f39f24c8a161095e089309d47fb (mode 644) --- /dev/null +++ regress/template/04-flow.tmpl @@ -0,0 +1,33 @@ +{! +#include +#include + +#include "tmpl.h" + +int base(struct template *, const char *); +!} + +{{ define base(struct template *tp, const char *title) }} +{! char *foo = NULL; !} + + + + {{ title }} + + +

{{ title }}

+ {{ if strchr(title, '*') != NULL }} +

"{{ title }}" has a '*' in it

+ {{ if 1 }} +

tautology!

+ {{ end }} + {{ else if strchr(title, '=') != NULL }} +

"{{ title }}" has a '=' in it!

+ {{ else }} +

"{{ title }}" doesn't have a '*' in it

+ {{ end }} + + +{{ finally }} +{! free(foo); !} +{{ end }} blob - /dev/null blob + 32240e27d9a6dbeeb395f7e760e2a043e29b939c (mode 644) --- /dev/null +++ regress/template/04.expected @@ -0,0 +1,2 @@ + *hello*

*hello*

" *hello* " has a '*' in it

tautology!

+<hello>

<hello>

"<hello>" doesn't have a '*' in it

blob - /dev/null blob + 5cb351ad4205991f16bb17f7caffc03c7594f15e (mode 644) --- /dev/null +++ regress/template/05-loop.tmpl @@ -0,0 +1,43 @@ +{! +#include +#include +#include "lists.h" +#include "tmpl.h" + +int list(struct template *, struct tailhead *); +int base(struct template *, struct tailhead *); + +!} + +{{ define base(struct template *tp, struct tailhead *head) }} + + + + {{ render list(tp, head) }} + + +{{ end }} + +{{ define list(struct template *tp, struct tailhead *head) }} +{! + struct entry *np; + int i; +!} + {{ if !TAILQ_EMPTY(head) }} +

items:

+
    + {{ tailq-foreach np head entries }} +
  • {{ np->text }}
  • + {{ end }} +
+ {{ else }} +

no items

+ {{ end }} + +

+ {{ for i = 0; i < 3; ++i }} + hello{{ " " }} + {{ end }} + world! +

+{{ end }} blob - /dev/null blob + d4c20d67eeee5e6e0217d8abd4c19f2d440ba6d9 (mode 644) --- /dev/null +++ regress/template/05.expected @@ -0,0 +1,2 @@ +

items:

  • 1
  • 2

hello hello hello world!

+

no items

hello hello hello world!

blob - /dev/null blob + 8a1d436209ad2a5cd22583fbb5c7c5b8cf06fdd7 (mode 644) --- /dev/null +++ regress/template/06-escape.tmpl @@ -0,0 +1,19 @@ +{! +#include + +#include "tmpl.h" + +int base(struct template *, const char *); +!} + +{{ define base(struct template *tp, const char *title) }} + + + + {{ title | urlescape }} + + +

{{ title | unsafe }}

+ + +{{ end }} blob - /dev/null blob + 6a9d734b454093206236753143743d95d5e473af (mode 644) --- /dev/null +++ regress/template/06.expected @@ -0,0 +1,2 @@ +%20*hello*%20

*hello*

+<hello>

blob - /dev/null blob + 0ad8f7070ecbf501efdda6004283ba83ce93ff15 (mode 644) --- /dev/null +++ regress/template/07-printf.tmpl @@ -0,0 +1,13 @@ +{! +#include +#include + +#include "tmpl.h" + +int base(struct template *, const char *); + +!} + +{{ define base(struct template *tp, const char *title) }} +{{ printf "%.2s:\t%d\n", title, 42 }} +{{ end }} blob - /dev/null blob + 681daf3915b708a380ddc75467b47beceff9b367 (mode 644) --- /dev/null +++ regress/template/07.expected @@ -0,0 +1,4 @@ + *: 42 + +<h: 42 + blob - /dev/null blob + f781b7ac44f61c8065695ac89ec64680ac84268c (mode 644) --- /dev/null +++ regress/template/Makefile @@ -0,0 +1,55 @@ +REGRESS_TARGETS = 00-empty \ + 01-noise-only \ + 02-only-verbatim \ + 03-block \ + 04-flow \ + 05-loop \ + 06-escape \ + 07-printf + +REGRESS_CLEANUP = clean-comp +NO_OBJ = Yes + +CFLAGS += -I../../template + +.PATH:../../template + +clean-comp: + rm -f t got 0*.[cdo] runbase.[do] runlist.[do] tmpl.* + +.SUFFIXES: .tmpl .c .o + +.tmpl.c: + ../../template/obj/template -o $@ $< + +00-empty: + ../../template/obj/template 00-empty.tmpl >/dev/null + +01-noise-only: + ../../template/obj/template 01-noise-only.tmpl >/dev/null + +02-only-verbatim: 02-only-verbatim.o tmpl.o + ${CC} 02-only-verbatim.o tmpl.o -o t && ./t > got + diff -u ${.CURDIR}/02.expected got + +03-block: 03-block.o runbase.o tmpl.o + ${CC} 03-block.o runbase.o tmpl.o -o t && ./t > got + diff -u ${.CURDIR}/03.expected got + +04-flow: 04-flow.o runbase.o tmpl.o + ${CC} 04-flow.o runbase.o tmpl.o -o t && ./t > got + diff -u ${.CURDIR}/04.expected got + +05-loop: 05-loop.o runlist.o tmpl.o + ${CC} 05-loop.o runlist.o tmpl.o -o t && ./t > got + diff -u ${.CURDIR}/05.expected got + +06-escape: 06-escape.o runbase.o tmpl.o + ${CC} 06-escape.o runbase.o tmpl.o -o t && ./t > got + diff -u ${.CURDIR}/06.expected got + +07-printf: 07-printf.o runbase.o tmpl.o + ${CC} 07-printf.o runbase.o tmpl.o -o t && ./t > got + diff -u ${.CURDIR}/07.expected got + +.include blob - /dev/null blob + 7229706ecd6bfb0bb2f38be92950b7082f7a6c41 (mode 644) --- /dev/null +++ regress/template/lists.h @@ -0,0 +1,7 @@ +#include + +TAILQ_HEAD(tailhead, entry); +struct entry { + char *text; + TAILQ_ENTRY(entry) entries; +}; blob - /dev/null blob + 0500bcadda792968ed1a7c8294ba9761ecc4ff71 (mode 644) --- /dev/null +++ regress/template/runbase.c @@ -0,0 +1,67 @@ +/* + * 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 "tmpl.h" + +int base(struct template *, const char *title); +int my_putc(struct template *, int); +int my_puts(struct template *, const char *); + +int +my_putc(struct template *tp, int c) +{ + FILE *fp = tp->tp_arg; + + if (putc(c, fp) < 0) + return (-1); + + return (0); +} + +int +my_puts(struct template *tp, const char *s) +{ + FILE *fp = tp->tp_arg; + + if (fputs(s, fp) < 0) + return (-1); + + return (0); +} + +int +main(int argc, char **argv) +{ + struct template *tp; + + if ((tp = template(stdout, my_puts, my_putc)) == NULL) + err(1, "template"); + + if (base(tp, " *hello* ") == -1) + return (1); + puts(""); + + if (base(tp, "") == -1) + return (1); + puts(""); + + free(tp); + return (0); +} blob - /dev/null blob + 0ec0a95185c3eccde7793744bde73bc5d87efb82 (mode 644) --- /dev/null +++ regress/template/runlist.c @@ -0,0 +1,87 @@ +/* + * 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 "tmpl.h" +#include "lists.h" + +int base(struct template *, struct tailhead *); +int my_putc(struct template *, int); +int my_puts(struct template *, const char *); + +int +my_putc(struct template *tp, int c) +{ + FILE *fp = tp->tp_arg; + + if (putc(c, fp) < 0) + return (-1); + + return (0); +} + +int +my_puts(struct template *tp, const char *s) +{ + FILE *fp = tp->tp_arg; + + if (fputs(s, fp) < 0) + return (-1); + + return (0); +} + +int +main(int argc, char **argv) +{ + struct template *tp; + struct tailhead head; + struct entry *np; + int i; + + if ((tp = template(stdout, my_puts, my_putc)) == NULL) + err(1, "template"); + + TAILQ_INIT(&head); + for (i = 0; i < 2; ++i) { + if ((np = calloc(1, sizeof(*np))) == NULL) + err(1, "calloc"); + if (asprintf(&np->text, "%d", i+1) == -1) + err(1, "asprintf"); + TAILQ_INSERT_TAIL(&head, np, entries); + } + + if (base(tp, &head) == -1) + return (1); + puts(""); + + while ((np = TAILQ_FIRST(&head))) { + TAILQ_REMOVE(&head, np, entries); + free(np->text); + free(np); + } + + if (base(tp, &head) == -1) + return (1); + puts(""); + + free(tp); + + return (0); +} blob - /dev/null blob + 7e95545a3d5b04f4123fd275efd2ad5186d17885 (mode 644) --- /dev/null +++ template/Makefile @@ -0,0 +1,6 @@ +PROG= template +SRCS= template.c tmpl.c parse.y + +MAN= template.1 template.7 + +.include blob - /dev/null blob + 34f42f83d0770ec2ab269538aac80935c414dfec (mode 644) --- /dev/null +++ template/parse.y @@ -0,0 +1,737 @@ +/* + * 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 +%token STRING +%type string +%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 : STRING { + dbg(); + fprintf(fp, "if ((tp_ret = tp->tp_puts(tp, "); + printq($1); + fputs(")) == -1) goto err;\n", fp); + + 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->tp_puts(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->tp_escape(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->tp_escape(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); + } + ; + +end : '{' END '}' + ; + +finally : '{' FINALLY '}' { + dbg(); + fputs("err:\n", fp); + } verbatims + ; + +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 }, + }; + 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 rigchtif last line 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; + } + + *p++ = c; + if ((size_t)(p - buf) >= sizeof(buf)) { + yyerror("string too long"); + return (findeol()); + } + } while ((c = lgetc(0)) != EOF && c != '\n'); + *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; + } + + *p++ = c; + if ((size_t)(p - buf) >= sizeof(buf)) { + yyerror("line too long"); + return (findeol()); + } + } while ((c = lgetc(0)) != EOF && c != '\n'); + *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); +} blob - /dev/null blob + 4c3a752a31718ca8ad29543f9f2f61b00c13cc4e (mode 644) --- /dev/null +++ template/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 November 25, 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 +.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 blob - /dev/null blob + 31b32697b9affdd4231edc3b70f8abaf0bb8300c (mode 644) --- /dev/null +++ template/template.7 @@ -0,0 +1,125 @@ +.\" 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 November 25, 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 -indent 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 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 {{ 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 +.Ar Omar Polo Aq Mt op@openbsd.org . blob - /dev/null blob + f52199db7b8acc13c6f1a44dc1cf4222b189b3e7 (mode 644) --- /dev/null +++ template/template.c @@ -0,0 +1,92 @@ +/* + * 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; + +static void __dead +usage(void) +{ + fprintf(stderr, "usage: %s [file...]\n", + getprogname()); + 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: + usage(); + } + } + argc -= optind; + argv += optind; + + if (out && (fp = fopen(out, "w")) == NULL) + err(1, "can't open %s", out); + + if (out && unveil(out, "wc") == -1) + err(1, "unveil %s", out); + if (unveil("/", "r") == -1) + err(1, "unveil /"); + if (pledge(out ? "stdio rpath cpath" : "stdio rpath", NULL) == -1) + err(1, "pledge"); + + if (argc == 0) { + nodebug = 1; + 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); +} blob - /dev/null blob + 36bb4882a5fec3f37fd3b75d26c1514f42bb77f5 (mode 644) --- /dev/null +++ template/tmpl.c @@ -0,0 +1,108 @@ +/* + * 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 "tmpl.h" + +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->tp_puts(tp, tmp) == -1) + return (-1); + } else { + if (tp->tp_putc(tp, *str) == -1) + return (-1); + } + } + + return (0); +} + +int +tp_htmlescape(struct template *tp, const char *str) +{ + int r; + + if (str == NULL) + return (0); + + for (; *str; ++str) { + switch (*str) { + case '<': + r = tp->tp_puts(tp, "<"); + break; + case '>': + r = tp->tp_puts(tp, ">"); + break; + case '&': + r = tp->tp_puts(tp, "&"); + break; + case '"': + r = tp->tp_puts(tp, """); + break; + case '\'': + r = tp->tp_puts(tp, "'"); + break; + default: + r = tp->tp_putc(tp, *str); + break; + } + + if (r == -1) + return (-1); + } + + return (0); +} + +struct template * +template(void *arg, tmpl_puts putsfn, tmpl_putc putcfn) +{ + struct template *tp; + + if ((tp = calloc(1, sizeof(*tp))) == NULL) + return (NULL); + + tp->tp_arg = arg; + tp->tp_escape = tp_htmlescape; + tp->tp_puts = putsfn; + tp->tp_putc = putcfn; + + return (tp); +} + +void +template_free(struct template *tp) +{ + free(tp->tp_tmp); + free(tp); +} blob - /dev/null blob + 2367b76c1353b204d520091d5212fd0a438e15cf (mode 644) --- /dev/null +++ template/tmpl.h @@ -0,0 +1,39 @@ +/* + * 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_puts)(struct template *, const char *); +typedef int (*tmpl_putc)(struct template *, int); + +struct template { + void *tp_arg; + char *tp_tmp; + tmpl_puts tp_escape; + tmpl_puts tp_puts; + tmpl_putc tp_putc; +}; + +int tp_urlescape(struct template *, const char *); +int tp_htmlescape(struct template *, const char *); + +struct template *template(void *, tmpl_puts, tmpl_putc); +void template_free(struct template *); + +#endif