Commit 21e14e69 authored by Nils Goroll's avatar Nils Goroll

Add += config file operator to avoid very long lines

... and allow for comments inbetween format definitions

Also internalize CONF_Add as conf_Add as it is not used outside config.c
parent 77e6fdc1
...@@ -450,12 +450,18 @@ The syntax of a configuration file is simply:: ...@@ -450,12 +450,18 @@ The syntax of a configuration file is simply::
# comment # comment
<param> = <value> <param> = <value>
<param> += <value>
The ``<value>`` is all of the data from the first non-whitespace The ``<value>`` is all of the data from the first non-whitespace
character after the equals sign up to the last non-whitespace character after the equals sign up to the last non-whitespace
character on the line. Comments begin with the hash character and character on the line. Comments begin with the hash character and
extend to the end of the line. There are no continuation lines. extend to the end of the line. There are no continuation lines.
The ``=`` operator assigns, ``+=`` appends to a previous value with a
single space character inbetween the previous and appended
value. ``+=`` is only supported for parameters ``cformat``,
``bformat``, ``rformat`` and ``syslog.ident``.
All of the config parameters have default values, and some of them All of the config parameters have default values, and some of them
correspond to command-line options, as shown below. correspond to command-line options, as shown below.
......
...@@ -107,14 +107,22 @@ conf_getDouble(const char *rval, double *d) ...@@ -107,14 +107,22 @@ conf_getDouble(const char *rval, double *d)
return(0); \ return(0); \
} }
#define confVSB(name,fld) \ #define confVSB(name,fld, op) \
if (strcmp(lval, (name)) == 0) { \ if (strcmp(lval, (name)) == 0) { \
VSB_clear(config.fld); \ if (strcmp(op, "=") == 0) \
VSB_clear(config.fld); \
else if (strcmp(op, "+=") == 0) \
VSB_cat(config.fld, " "); \
else \
INCOMPL(); \
VSB_cat(config.fld, rval); \ VSB_cat(config.fld, rval); \
VSB_finish(config.fld); \
return(0); \ return(0); \
} }
#define finiVSB(fld) \
VSB_finish(config.fld)
/* XXX: need confNonNegative? */ /* XXX: need confNonNegative? */
#define confUnsigned(name,fld) \ #define confUnsigned(name,fld) \
...@@ -137,20 +145,23 @@ conf_getDouble(const char *rval, double *d) ...@@ -137,20 +145,23 @@ conf_getDouble(const char *rval, double *d)
return(0); \ return(0); \
} }
int static int
CONF_Add(const char *lval, const char *rval) conf_Add(const char *lval, const char *op, const char *rval)
{ {
int ret; int ret;
confVSB("cformat", cformat, op);
confVSB("bformat", bformat, op);
confVSB("rformat", rformat, op);
confVSB("syslog.ident", syslog_ident, op);
if (strcmp(op, "="))
return ENOTSUP;
confString("log.file", log_file); confString("log.file", log_file);
confString("output.file", output_file); confString("output.file", output_file);
confString("varnish.bindump", varnish_bindump); confString("varnish.bindump", varnish_bindump);
confVSB("cformat", cformat);
confVSB("bformat", bformat);
confVSB("rformat", rformat);
confVSB("syslog.ident", syslog_ident);
confUnsigned("max.reclen", max_reclen); confUnsigned("max.reclen", max_reclen);
confUnsigned("chunk_size", chunk_size); confUnsigned("chunk_size", chunk_size);
confUnsigned("max.data", max_data); confUnsigned("max.data", max_data);
...@@ -175,24 +186,30 @@ CONF_Add(const char *lval, const char *rval) ...@@ -175,24 +186,30 @@ CONF_Add(const char *lval, const char *rval)
} }
static int static int
conf_ParseLine(char *ptr, char **lval, char **rval) conf_ParseLine(char *ptr, char **lval, char **op, char **rval)
{ {
char *endlval; char *endlval;
*lval = ptr; *lval = ptr;
while(*++ptr && !isspace(*ptr) && *ptr != '=') while(*++ptr && !isspace(*ptr) && *ptr != '=' && *ptr != '+')
; ;
if (*ptr == '\0') if (*ptr == '\0')
return(1); return(1);
endlval = ptr; endlval = ptr;
while(isspace(*ptr) && *++ptr) while(isspace(*ptr))
; ptr++;
if (ptr == '\0' || *ptr != '=') if (*ptr == '+' && ptr[1] == '=') {
return(1); *op = "+=";
while(*++ptr && isspace(*ptr)) ptr += 2;
; }
if (ptr == '\0') else if (*ptr == '=') {
*op = "=";
ptr++;
}
else
return(1); return(1);
while(isspace(*ptr))
ptr++;
*endlval = '\0'; *endlval = '\0';
*rval = ptr; *rval = ptr;
return(0); return(0);
...@@ -209,14 +226,10 @@ CONF_Init(void) ...@@ -209,14 +226,10 @@ CONF_Init(void)
config.cformat = VSB_new_auto(); config.cformat = VSB_new_auto();
VSB_cat(config.cformat, DEFAULT_CFORMAT); VSB_cat(config.cformat, DEFAULT_CFORMAT);
VSB_finish(config.cformat);
config.bformat = VSB_new_auto(); config.bformat = VSB_new_auto();
VSB_finish(config.bformat);
config.rformat = VSB_new_auto(); config.rformat = VSB_new_auto();
VSB_finish(config.rformat);
config.syslog_ident = VSB_new_auto(); config.syslog_ident = VSB_new_auto();
VSB_cat(config.syslog_ident, "varnishevent"); VSB_cat(config.syslog_ident, "varnishevent");
VSB_finish(config.syslog_ident);
config.syslog_facility = LOG_LOCAL0; config.syslog_facility = LOG_LOCAL0;
...@@ -232,6 +245,15 @@ CONF_Init(void) ...@@ -232,6 +245,15 @@ CONF_Init(void)
config.reader_timeout = 0.; config.reader_timeout = 0.;
} }
void
CONF_Fini(void)
{
finiVSB(cformat);
finiVSB(bformat);
finiVSB(rformat);
finiVSB(syslog_ident);
}
static int static int
conf_get_line(char *line, FILE *in) conf_get_line(char *line, FILE *in)
{ {
...@@ -285,20 +307,21 @@ CONF_ReadFile(const char *file) { ...@@ -285,20 +307,21 @@ CONF_ReadFile(const char *file) {
VSB_cat(orig, ptr); VSB_cat(orig, ptr);
VSB_finish(orig); VSB_finish(orig);
char *lval, *rval; char *lval, *op, *rval;
if (conf_ParseLine(ptr, &lval, &rval) != 0) { if (conf_ParseLine(ptr, &lval, &op, &rval) != 0) {
fprintf(stderr, "Cannot parse %s line %d: '%s'\n", file, linenum, fprintf(stderr, "Cannot parse %s line %d: '%s'\n", file, linenum,
VSB_data(orig)); VSB_data(orig));
return(-1); return(-1);
} }
int ret; int ret;
if ((ret = CONF_Add((const char *) lval, (const char *) rval)) != 0) { if ((ret = conf_Add(lval, op, rval)) != 0) {
fprintf(stderr, "Error in %s line %d (%s): '%s'\n", file, linenum, fprintf(stderr, "Error in %s line %d (%s): '%s'\n", file, linenum,
strerror(ret), VSB_data(orig)); strerror(ret), VSB_data(orig));
return(-1); return(-1);
} }
} }
int ret = 0; int ret = 0;
if (ferror(in)) { if (ferror(in)) {
fprintf(stderr, "Error reading file %s (errno %d: %s)\n", file, errno, fprintf(stderr, "Error reading file %s (errno %d: %s)\n", file, errno,
......
...@@ -64,6 +64,7 @@ static char ...@@ -64,6 +64,7 @@ static char
MAZ(LOG_Open("test_data")); MAZ(LOG_Open("test_data"));
CONF_Init(); CONF_Init();
CONF_Fini();
MAZ(FMT_Init(fmterr)); MAZ(FMT_Init(fmterr));
VMASSERT((err = DATA_Init()) == 0, "DATA_Init: %s", strerror(err)); VMASSERT((err = DATA_Init()) == 0, "DATA_Init: %s", strerror(err));
......
...@@ -216,6 +216,7 @@ static const char ...@@ -216,6 +216,7 @@ static const char
printf("... initializing format tests\n"); printf("... initializing format tests\n");
CONF_Init(); CONF_Init();
CONF_Fini();
status = FMT_Init(err); status = FMT_Init(err);
VMASSERT(status == 0, "FMT_Init: %s", err); VMASSERT(status == 0, "FMT_Init: %s", err);
......
...@@ -2,8 +2,41 @@ ...@@ -2,8 +2,41 @@
monitor.interval = 0 monitor.interval = 0
cformat=%b %d %D %H %h %I %{Host}i %{Connection}i %{User-Agent}i %{X-Forwarded-For}i %{Accept-Ranges}o %{Age}o %{Connection}o %{Content-Encoding}o %{Content-Length}o %{Content-Type}o %{Date}o %{Last-Modified}o %{Server}o %{Transfer-Encoding}o %{Via}o %{X-Varnish}o %l %m %O %q %r %s %t %{%F-%T}t %U %u %{Varnish:time_firstbyte}x %{Varnish:hitmiss}x %{Varnish:handling}x %{tag:Begin}x %{tag:Debug}x %{tag:End}x %{tag:Gzip}x %{tag:Hit}x %{tag:Length}x %{tag:Link}x %{tag:ReqAcct}x %{tag:ReqStart}x %{tag:RespProtocol}x %{tag:ReqMethod}x %{tag:ReqURL}x %{tag:ReqProtocol}x %{tag:RespReason}x %{tag:RespStatus}x %{tag:Timestamp:Req}x %{tag:ReqAcct[5]}x %{tag:Timestamp:Req[2]}x %{VSL:Begin}x %{VSL:Debug}x %{VSL:End}x %{VSL:Gzip}x %{VSL:Hit}x %{VSL:Length}x %{VSL:Link}x %{VSL:ReqAcct}x %{VSL:ReqStart}x %{VSL:RespProtocol}x %{VSL:ReqMethod}x %{VSL:ReqURL}x %{VSL:ReqProtocol}x %{VSL:RespReason}x %{VSL:RespStatus}x %{VSL:Timestamp:Req}x %{VSL:ReqAcct[5]}x %{VSL:Timestamp:Req[2]}x %{VSL:Timestamp}x %{vxid}x %{Varnish:vxid}x %{pvxid}x %{Varnish:side}x cformat=%b %d %D %H %h %I %{Host}i %{Connection}i %{User-Agent}i
cformat+=%{X-Forwarded-For}i %{Accept-Ranges}o %{Age}o %{Connection}o
cformat+=%{Content-Encoding}o %{Content-Length}o %{Content-Type}o %{Date}o
cformat+=%{Last-Modified}o %{Server}o %{Transfer-Encoding}o %{Via}o
cformat+=%{X-Varnish}o %l %m %O %q %r %s %t %{%F-%T}t %U %u
cformat+=%{Varnish:time_firstbyte}x %{Varnish:hitmiss}x %{Varnish:handling}x
cformat+=%{tag:Begin}x %{tag:Debug}x %{tag:End}x %{tag:Gzip}x %{tag:Hit}x
cformat+=%{tag:Length}x %{tag:Link}x %{tag:ReqAcct}x %{tag:ReqStart}x
cformat+=%{tag:RespProtocol}x %{tag:ReqMethod}x %{tag:ReqURL}x
cformat+=%{tag:ReqProtocol}x %{tag:RespReason}x %{tag:RespStatus}x
cformat+=%{tag:Timestamp:Req}x %{tag:ReqAcct[5]}x %{tag:Timestamp:Req[2]}x
cformat+=%{VSL:Begin}x %{VSL:Debug}x %{VSL:End}x %{VSL:Gzip}x %{VSL:Hit}x
cformat+=%{VSL:Length}x %{VSL:Link}x %{VSL:ReqAcct}x %{VSL:ReqStart}x
cformat+=%{VSL:RespProtocol}x %{VSL:ReqMethod}x %{VSL:ReqURL}x
cformat+=%{VSL:ReqProtocol}x %{VSL:RespReason}x %{VSL:RespStatus}x
cformat+=%{VSL:Timestamp:Req}x %{VSL:ReqAcct[5]}x %{VSL:Timestamp:Req[2]}x
cformat+=%{VSL:Timestamp}x %{vxid}x %{Varnish:vxid}x %{pvxid}x %{Varnish:side}x
bformat=%b %d %D %H %h %I %{Accept-Encoding}i %{Host}i %{User-Agent}i %{X-Forwarded-For}i %{X-Varnish}i %{Accept-Ranges}o %{Connection}o %{Content-Encoding}o %{Content-Length}o %{Content-Type}o %{Date}o %{ETag}o %{Last-Modified}o %{Server}o %{Transfer-Encoding}o %{Vary}o %l %m %O %q %r %s %t %{%F-%T}t %U %u %{Varnish:time_firstbyte}x %{tag:Backend}x %{tag:BackendOpen}x %{tag:BackendClose}x %{tag:BackendReuse}x %{tag:Begin}x %{tag:BereqAcct}x %{tag:Length}x %{tag:BerespProtocol}x %{tag:BerespReason}x %{tag:BerespStatus}x %{tag:BereqProtocol}x %{tag:BereqMethod}x %{tag:BereqURL}x %{tag:Debug}x %{tag:End}x %{tag:Timestamp:Bereq}x %{tag:BereqAcct[5]}x %{tag:Timestamp:Bereq[2]}x %{VSL:Backend}x %{VSL:BackendOpen}x %{VSL:BackendClose}x %{VSL:BackendReuse}x %{VSL:Begin}x %{VSL:BereqAcct}x %{VSL:Length}x %{VSL:BerespProtocol}x %{VSL:BerespReason}x %{VSL:BerespStatus}x %{VSL:BereqProtocol}x %{VSL:BereqMethod}x %{VSL:BereqURL}x %{VSL:Debug}x %{VSL:End}x %{VSL:Timestamp:Bereq}x %{VSL:BereqAcct[5]}x %{VSL:Timestamp:Bereq[2]}x %{VSL:Timestamp}x %{vxid}x %{Varnish:vxid}x %{pvxid}x %{Varnish:side}x bformat=%b %d %D %H %h %I %{Accept-Encoding}i %{Host}i %{User-Agent}i
bformat+=%{X-Forwarded-For}i %{X-Varnish}i %{Accept-Ranges}o %{Connection}o
bformat+=%{Content-Encoding}o %{Content-Length}o %{Content-Type}o %{Date}o
bformat+=%{ETag}o %{Last-Modified}o %{Server}o %{Transfer-Encoding}o %{Vary}o
bformat+=%l %m %O %q %r %s %t %{%F-%T}t %U %u %{Varnish:time_firstbyte}x
bformat+=%{tag:Backend}x %{tag:BackendOpen}x %{tag:BackendClose}x
bformat+=%{tag:BackendReuse}x %{tag:Begin}x %{tag:BereqAcct}x %{tag:Length}x
bformat+=%{tag:BerespProtocol}x %{tag:BerespReason}x %{tag:BerespStatus}x
bformat+=%{tag:BereqProtocol}x %{tag:BereqMethod}x %{tag:BereqURL}x
bformat+=%{tag:Debug}x %{tag:End}x %{tag:Timestamp:Bereq}x %{tag:BereqAcct[5]}x
bformat+=%{tag:Timestamp:Bereq[2]}x %{VSL:Backend}x %{VSL:BackendOpen}x
bformat+=%{VSL:BackendClose}x %{VSL:BackendReuse}x %{VSL:Begin}x
bformat+=%{VSL:BereqAcct}x %{VSL:Length}x %{VSL:BerespProtocol}x
bformat+=%{VSL:BerespReason}x %{VSL:BerespStatus}x %{VSL:BereqProtocol}x
bformat+=%{VSL:BereqMethod}x %{VSL:BereqURL}x %{VSL:Debug}x %{VSL:End}x
bformat+=%{VSL:Timestamp:Bereq}x %{VSL:BereqAcct[5]}x
bformat+=%{VSL:Timestamp:Bereq[2]}x %{VSL:Timestamp}x %{vxid}x %{Varnish:vxid}x
bformat+=%{pvxid}x %{Varnish:side}x
log.file = test.log log.file = test.log
...@@ -627,7 +627,6 @@ main(int argc, char *argv[]) ...@@ -627,7 +627,6 @@ main(int argc, char *argv[])
case 'F': case 'F':
VSB_clear(config.cformat); VSB_clear(config.cformat);
VSB_cat(config.cformat, optarg); VSB_cat(config.cformat, optarg);
VSB_finish(config.cformat);
break; break;
case 'D': case 'D':
#ifdef HAVE_DAEMON #ifdef HAVE_DAEMON
...@@ -702,6 +701,8 @@ main(int argc, char *argv[]) ...@@ -702,6 +701,8 @@ main(int argc, char *argv[])
} }
} }
CONF_Fini();
if (!EMPTY(config.varnish_bindump) && (n_arg)) { if (!EMPTY(config.varnish_bindump) && (n_arg)) {
fprintf(stderr, "Cannot specify -r/varnish.bindump together with -n\n"); fprintf(stderr, "Cannot specify -r/varnish.bindump together with -n\n");
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
......
...@@ -215,7 +215,7 @@ int RDR_Waiting(void); ...@@ -215,7 +215,7 @@ int RDR_Waiting(void);
/* config.c */ /* config.c */
void CONF_Init(void); void CONF_Init(void);
int CONF_Add(const char *lval, const char *rval); void CONF_Fini(void);
int CONF_ReadFile(const char *file); int CONF_ReadFile(const char *file);
void CONF_Dump(void); void CONF_Dump(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment