"GOT", but the "O" is a cute, smiling sun Index | Thread

From:
Omar Polo <op@omarpolo.com>
Subject:
gotwebd.conf: add varset and include
To:
gameoftrees@openbsd.org
Date:
Sun, 25 Sep 2022 18:38:12 +0200

Download raw body.

gotwebd' parse.y is derived from the "usual" parse.y, yet it doesn't
have `include's nor the macro syntax, yet it has -D.  Don't know if it
was a deliberate decision, but I personally like having the include
and variables available.  (i've also took the liberty to rename
newfile/closefile to pushfile/popfile to be closer to the other
parse.y files.)

the manpage wording is stolen from smtpd.conf, vm.conf and httd.conf.

ok?

diff /home/op/w/got-main
commit - e02bf8ada9fce6518a7ad40401425a26bdaadbb4
path + /home/op/w/got-main
blob - db41a6d395d3330ea9e08eca17fb286d7186eba5
file + gotwebd/gotwebd.8
--- gotwebd/gotwebd.8
+++ gotwebd/gotwebd.8
@@ -22,7 +22,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl dnv
-.Op Fl D Ar macro=value
+.Op Fl D Ar macro Ns = Ns Ar value
 .Op Fl f Ar file
 .Sh DESCRIPTION
 .Nm
@@ -38,8 +38,14 @@ Do not daemonize and log to stderr.
 .Bl -tag -width tenletters
 .It Fl d
 Do not daemonize and log to stderr.
-.It Fl D Ar macro=value
-Override the value of a macro used in the configuration file.
+.It Fl D Ar macro Ns = Ns Ar value
+Define
+.Ar macro
+to be set to
+.Ar value .
+Overrides the definition of
+.Ar macro
+in the configuration file.
 .It Fl f Ar file
 Set the path to the configuration file.
 If not specified, the file
blob - b0f31dc628d54ccc683a1c5b00750a7362ebc124
file + gotwebd/gotwebd.conf.5
--- gotwebd/gotwebd.conf.5
+++ gotwebd/gotwebd.conf.5
@@ -29,6 +29,23 @@ Paths mentioned in
 .Sq #
 are treated as comments and ignored.
 .Pp
+Macros can be defined that are later expanded in context.
+Macro names must start with a letter, digit, or underscore, and may
+contain any of those characters, but may not be reserved words.
+Macros are not expanded inside quotes.
+For example:
+.Bd -literal -offset indent
+lan_addr = "192.168.0.1"
+listen on $lan_addr
+.Ed
+.Pp
+Additional configuration files can be included with the
+.Ic include
+keyword, for example:
+.Bd -literal -offset indent
+include "/etc/gotwebd.conf.local"
+.Ed
+.Pp
 Paths mentioned in
 .Nm
 must be relative to
blob - 84e9b08cfad055d23554ebba81fc37fa15e8cdc9
file + gotwebd/parse.y
--- gotwebd/parse.y
+++ gotwebd/parse.y
@@ -59,8 +59,8 @@ struct file	*newfile(const char *, int);
 	int			 lineno;
 	int			 errors;
 } *file;
-struct file	*newfile(const char *, int);
-static void	 closefile(struct file *);
+struct file	*pushfile(const char *, int);
+static void	 popfile(struct file *);
 int		 check_file_secrecy(int, const char *);
 int		 yyparse(void);
 int		 yylex(void);
@@ -121,7 +121,7 @@ typedef struct {
 %token	LISTEN WWW_PATH MAX_REPOS SITE_NAME SITE_OWNER SITE_LINK LOGO
 %token	LOGO_URL SHOW_REPO_OWNER SHOW_REPO_AGE SHOW_REPO_DESCRIPTION
 %token	MAX_REPOS_DISPLAY REPOS_PATH MAX_COMMITS_DISPLAY ON ERROR
-%token	SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK
+%token	SHOW_SITE_OWNER SHOW_REPO_CLONEURL PORT PREFORK INCLUDE
 %token	UNIX_SOCKET UNIX_SOCKET_NAME SERVER CHROOT CUSTOM_CSS
 
 %token	<v.string>	STRING
@@ -131,12 +131,47 @@ grammar		:
 
 %%
 
-grammar		:
+grammar		: /* empty */
 		| grammar '\n'
+		| grammar include '\n'
+		| grammar varset '\n'
 		| grammar main '\n'
 		| grammar server '\n'
+		| grammar error '\n'		{ 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');
+		}
+
+varset		: STRING '=' STRING	{
+			char *s = $1;
+			while (*s++) {
+				if (isspace((unsigned char)*s)) {
+					yyerror("macro name cannot contain "
+					    "whitespace");
+					free($1);
+					free($3);
+					YYERROR;
+				}
+			}
+			if (symset($1, $3, 0) == -1)
+				fatal("cannot store variable");
+			free($1);
+			free($3);
+		}
+		;
+
 boolean		: STRING {
 			if (strcasecmp($1, "1") == 0 ||
 			    strcasecmp($1, "yes") == 0 ||
@@ -411,6 +446,7 @@ lookup(char *s)
 	static const struct keywords keywords[] = {
 		{ "chroot",			CHROOT },
 		{ "custom_css",			CUSTOM_CSS },
+		{ "include",			INCLUDE },
 		{ "listen",			LISTEN },
 		{ "logo",			LOGO },
 		{ "logo_url"	,		LOGO_URL },
@@ -713,7 +749,7 @@ newfile(const char *name, int secret)
 }
 
 struct file *
-newfile(const char *name, int secret)
+pushfile(const char *name, int secret)
 {
 	struct file *nfile;
 
@@ -746,7 +782,7 @@ closefile(struct file *xfile)
 }
 
 static void
-closefile(struct file *xfile)
+popfile(struct file *xfile)
 {
 	fclose(xfile->stream);
 	free(xfile->name);
@@ -770,7 +806,7 @@ parse_config(const char *filename, struct gotwebd *env
 
 	gotwebd = env;
 
-	file = newfile(filename, 0);
+	file = pushfile(filename, 0);
 	if (file == NULL) {
 		add_default_server();
 		sockets_parse_sockets(env);
@@ -780,7 +816,7 @@ parse_config(const char *filename, struct gotwebd *env
 
 	yyparse();
 	errors = file->errors;
-	closefile(file);
+	popfile(file);
 
 	/* Free macros and check which have not been used. */
 	TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {