2 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
3 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
6 * parses config files used by the original (Perl) mogstored
8 #include "cmogstored.h"
10 #include "listen_parser.h"
12 static char *mystrdup(const char *key, char *mark_beg, const char *p)
14 size_t mark_len = p - mark_beg;
15 mark_beg[mark_len] = 0;
16 if (strlen(mark_beg) != mark_len) {
17 syslog(LOG_ERR, "NUL character in `%s' value", key);
21 return xstrdup(mark_beg);
26 include listen_parser_common "listen_parser_common.rl";
29 ignored_line := ( (any-'\n')* eor ) @ { fgoto main; };
31 comment = lws* ("#"(any-'\n')*);
32 comment_line = comment eor;
35 path = ((any - space)+) > { mark_beg = fpc; };
37 mgmtlisten = lws* "mgmtlisten" sep listen comment* (eor) > {
38 a = mog_listen_parse_internal(mark_beg, mark_len,
39 port_beg, port_len, sa_family);
44 httplisten = lws* "httplisten" sep listen comment* eor > {
45 a = mog_listen_parse_internal(mark_beg, mark_len,
46 port_beg, port_len, sa_family);
50 httpgetlisten = lws* "httpgetlisten" sep listen comment* eor > {
51 a = mog_listen_parse_internal(mark_beg, mark_len,
52 port_beg, port_len, sa_family);
54 cfg->httpgetlisten = a;
56 docroot = lws* "docroot" sep path (comment* eor) > {
57 /* delay realpath(3) until svc init, symlinks may change */
58 cfg->docroot = mystrdup("docroot", mark_beg, fpc);
59 if (!cfg->docroot) return -1;
61 pidfile = lws* "pidfile" sep path (comment* eor) > {
62 /* delay realpath(3) until svc init, symlinks may change */
63 cfg->pidfile = mystrdup("pidfile", mark_beg, fpc);
64 if (!cfg->pidfile) return -1;
66 daemonize_compat = lws* "daemonize" comment* eor > {
68 "\"daemonize\" in was supported in error in the past and now deprecated");
70 "use \"daemonize = 1\" in your config file instead");
71 cfg->daemonize = true;
73 daemonize = lws* "daemonize" sep
74 ((any - space)+) > { mark_beg = fpc; }
76 mark_len = fpc - mark_beg;
77 mark_beg[mark_len] = 0;
79 /* special case for compatibility with mogstored */
80 cfg->daemonize = strcmp("0", mark_beg);
82 maxconns = lws* "maxconns" sep
83 (digit+) > { mark_beg = fpc; }
85 mark_len = fpc - mark_beg;
86 mark_beg[mark_len] = 0;
88 cfg->maxconns = strtoul(mark_beg, NULL, 10);
91 "failed to parse: maxconns = %s - %m",
96 server = lws* "server" sep
97 (alpha+) > { mark_beg = fpc; }
99 cfg->server = mystrdup("server", mark_beg, fpc);
100 if (!cfg->server) return -1;
101 mog_cfg_check_server(cfg);
103 serverbin = lws* "serverbin" sep path
105 char *tmp = mystrdup("serverbin", mark_beg, fpc);
107 warn("W: serverbin = %s ignored", tmp);
110 main := (mgmtlisten | httplisten | httpgetlisten |
111 pidfile | docroot | daemonize | daemonize_compat | maxconns |
112 server | serverbin ) +
121 /* this is a one-shot parser, no need to stream local config files */
122 int mog_cfg_parse(struct mog_cfg *cfg, char *buf, size_t len)
124 char *p, *pe, *eof = NULL;
125 char *mark_beg = NULL;
126 char *port_beg = NULL;
129 sa_family_t sa_family = AF_INET;
130 struct mog_addrinfo *a;
140 if (cs == cfg_parser_error)
143 assert(p <= pe && "buffer overflow after cfg parse");