saait

Simple static page generator
git clone https://git.sinitax.com/codemadness/saait
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit 63d47467edf428f92bdb01f1f0e7d60239782942
parent f43f198c66c1fcc3515fda2640ee6ad846418865
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat, 15 Feb 2020 00:12:34 +0100

rm arg.h and config.h: hand-roll argument parsing

Diffstat:
Darg.h | 42------------------------------------------
Dconfig.h | 18------------------
Msaait.c | 51++++++++++++++++++++++++++++++++-------------------
3 files changed, 32 insertions(+), 79 deletions(-)

diff --git a/arg.h b/arg.h @@ -1,42 +0,0 @@ -/* - * 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;\ - }\ - int i_;\ - for (i_ = 1, brk_ = 0, argv_ = argv;\ - argv[0][i_] && !brk_;\ - i_++) {\ - if (argv_ != argv)\ - break;\ - argc_ = argv[0][i_];\ - switch (argc_) - -#define ARGEND }\ - } - -#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ - ((x), abort(), (char *)0) :\ - (brk_ = 1, (argv[0][i_+1] != '\0')?\ - (&argv[0][i_+1]) :\ - (argc--, argv++, argv[0]))) - -#endif diff --git a/config.h b/config.h @@ -1,18 +0,0 @@ -static const char *configfile = "config.cfg"; -static const char *outputdir = "output"; -static const char *templatedir = "templates"; - -enum { BlockHeader = 0, BlockItem, BlockFooter, BlockLast }; - -struct block { - char *name; /* filename */ - char *data; /* content (set at runtime) */ -}; - -struct template { - char *name; - /* blocks: header, item, footer */ - struct block blocks[BlockLast]; - /* output FILE * (set at runtime) */ - FILE *fp; -}; diff --git a/saait.c b/saait.c @@ -19,15 +19,29 @@ #define READ_BUF_SIZ 16384 #define LEN(s) (sizeof(s)/sizeof(*s)) +enum { BlockHeader = 0, BlockItem, BlockFooter, BlockLast }; + struct variable { char *key, *value; struct variable *next; }; -#include "arg.h" -char *argv0; +struct block { + char *name; /* filename */ + char *data; /* content (set at runtime) */ +}; + +struct template { + char *name; + /* blocks: header, item, footer */ + struct block blocks[BlockLast]; + /* output FILE * (set at runtime) */ + FILE *fp; +}; -#include "config.h" +static const char *configfile = "config.cfg"; +static const char *outputdir = "output"; +static const char *templatedir = "templates"; static struct variable *global; /* global config variables */ @@ -375,7 +389,7 @@ writepage(FILE *fp, const char *name, const char *forname, } void -usage(void) +usage(const char *argv0) { fprintf(stderr, "%s [-c configfile] [-o outputdir] [-t templatesdir] " "pages...\n", argv0); @@ -393,26 +407,25 @@ main(int argc, char *argv[]) char file[PATH_MAX + 1], htmlfile[PATH_MAX + 1], path[PATH_MAX + 1]; char outputfile[PATH_MAX + 1], *p, *filename; size_t i, j, k, templateslen; - int r, doindex; + int argi, r; if (pledge("stdio cpath rpath wpath", NULL) == -1) { fprintf(stderr, "pledge: %s\n", strerror(errno)); return 1; } - ARGBEGIN { - case 'c': - configfile = EARGF(usage()); - break; - case 'o': - outputdir = EARGF(usage()); - break; - case 't': - templatedir = EARGF(usage()); - break; - default: - usage(); - } ARGEND + for (argi = 1; argi < argc; argi++) { + if (argv[argi][0] != '-') + break; + if (argi + 1 >= argc) + usage(argv[0]); + switch (argv[argi][1]) { + case 'c': configfile = argv[argi + 1]; break; + case 'o': outputdir = argv[argi + 1]; break; + case 't': templatedir = argv[argi + 1]; break; + default: usage(argv[0]); break; + } + } /* global config */ global = readconfig(configfile); @@ -497,7 +510,7 @@ main(int argc, char *argv[]) } /* pages */ - for (i = 0; i < (size_t)argc; i++) { + for (i = argi; i < (size_t)argc; i++) { c = readconfig(argv[i]); setvar(&c, newvar("index", "1"), 0);