stacinhtml

Static C in HTML - simple static site generator
git clone git://git.kocotian.pl/stacinhtml.git
Log | Files | Refs | README

commit 93138d39224bf92731215e8e4c9be0caf6ae6501
parent 3522cd389575bad4227ff2d21c4b6e8bc70008c5
Author: kocotian <kocotian@kocotian.pl>
Date:   Sat, 20 Mar 2021 16:17:35 +0100

README additions, libsl, more in generate.c

Diffstat:
MREADME | 7++++++-
Aarg.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mgenerate.c | 32++++++++++++++++++++++++++++++++
Alicenses/LIBSL | 24++++++++++++++++++++++++
Autil.c | 35+++++++++++++++++++++++++++++++++++
Autil.h | 8++++++++
6 files changed, 155 insertions(+), 1 deletion(-)

diff --git a/README b/README @@ -22,4 +22,9 @@ Examples: @template blog Template is HTML code, but can write nested code surrounded by `%` signs (like -in node.c by Tsoding). +in node.c by Tsoding). For example, this will print "Hello!" 255 times: + + <!DOCTYPE html> + %for (int i = 0; i < 255; ++i) {% + Hello! + %}% diff --git a/arg.h b/arg.h @@ -0,0 +1,50 @@ +/* See licenses/LIBSL file for copyright and license details. */ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/generate.c b/generate.c @@ -1,7 +1,39 @@ #include <stdio.h> +#include "arg.h" +#include "util.h" + +static void usage(void); + +char *argv0; + +static void +usage(void) +{ + die("usage: %s [-v] INPUT [TEMPLATE]", argv0); +} + int main(int argc, char *argv[]) { + /* Variables */ + char *input = NULL, *template = NULL; + + /* Options parsing */ + ARGBEGIN { + case 'v': + die("generate-"VERSION); break; + default: + usage(); break; + } ARGEND + + /* Argument parsing */ + if (argc == 1) + input = argv[0]; + else if (argc == 2) + template = argv[1]; + else + usage(); + return 0; } diff --git a/licenses/LIBSL b/licenses/LIBSL @@ -0,0 +1,24 @@ +MIT/X Consortium License + +© 2013-2019 Anselm R Garbe <anselm@garbe.ca> +© 2015-2019 Hiltjo Posthuma <hiltjo@codemadness.org> +© 2016 Markus Teich <markus.teich@stusta.mhn.de> +© 2015 Eric Pruitt <eric.pruitt@gmail.com> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/util.c b/util.c @@ -0,0 +1,35 @@ +/* See licenses/LIBSL file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +void * +ecalloc(size_t nmemb, size_t size) +{ + void *p; + + if (!(p = calloc(nmemb, size))) + die("calloc:"); + return p; +} + +void +die(const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + exit(1); +} diff --git a/util.h b/util.h @@ -0,0 +1,8 @@ +/* See licenses/LIBSL file for copyright and license details. */ + +#define MAX(A, B) ((A) > (B) ? (A) : (B)) +#define MIN(A, B) ((A) < (B) ? (A) : (B)) +#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B)) + +void die(const char *fmt, ...); +void *ecalloc(size_t nmemb, size_t size);