commit 47b307cd821b00964d3c5aea35c86689df2fe26d from: Omar Polo date: Sun Oct 02 15:22:04 2022 UTC gotwebd.conf: add syntax for defining macros and document them macros are already supported by parse.y but can only be defined with the -D flag. This adds the ``usual'' syntax for macros: varname = string. While here, fix the markup of the -D flag in the SYNOPSIS and sync the global `grammar' yacc rule with other parse.y grammars in base: - add the /* empty */ comment - increment file->errors on errors ok stsp@, "sure, go for it" tracey@ commit - e02b422b6f7736de0e851b3f8fad812e77e9c6b4 commit + 47b307cd821b00964d3c5aea35c86689df2fe26d blob - db41a6d395d3330ea9e08eca17fb286d7186eba5 blob + 76ea436987a66394ecc52ea2dbee229a0fd42e1b --- 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 @@ provides the following options: .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 blob + 82d73b6a9ce6b79982f543208398c56d6464e96d --- gotwebd/gotwebd.conf.5 +++ gotwebd/gotwebd.conf.5 @@ -29,6 +29,16 @@ Any lines beginning with a .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 Paths mentioned in .Nm must be relative to blob - 84e9b08cfad055d23554ebba81fc37fa15e8cdc9 blob + a343be03a8846c19af54d78c6da13c96303c3dde --- gotwebd/parse.y +++ gotwebd/parse.y @@ -131,10 +131,30 @@ typedef struct { %% -grammar : +grammar : /* empty */ | grammar '\n' + | grammar varset '\n' | grammar main '\n' | grammar server '\n' + | grammar error '\n' { file->errors++; } + ; + +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 {