Blob


1 /* -*- mode: fundamental; indent-tabs-mode: t; -*- */
2 %{
4 /*
5 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
20 #include <err.h>
21 #include <stdio.h>
23 #include "gmid.h"
25 /*
26 * #define YYDEBUG 1
27 * int yydebug = 1;
28 */
30 struct vhost *host = &hosts[0];
31 size_t ihost = 0;
33 struct location *loc = &hosts[0].locations[0];
34 size_t iloc = 0;
36 int goterror = 0;
37 const char *config_path;
39 void yyerror(const char*);
40 int parse_portno(const char*);
41 void parse_conf(const char*);
43 %}
45 /* for bison: */
46 /* %define parse.error verbose */
48 %union {
49 char *str;
50 int num;
51 }
53 %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE
54 %token TCHROOT TUSER TSERVER
55 %token TLOCATION TCERT TKEY TROOT TCGI TLANG TINDEX TAUTO
56 %token TERR
58 %token <str> TSTRING
59 %token <num> TNUM
60 %token <num> TBOOL
62 %%
64 conf : options vhosts ;
66 options : /* empty */
67 | options option
68 ;
70 option : TIPV6 TBOOL { conf.ipv6 = $2; }
71 | TPORT TNUM { conf.port = $2; }
72 | TPROTOCOLS TSTRING {
73 if (tls_config_parse_protocols(&conf.protos, $2) == -1)
74 errx(1, "invalid protocols string \"%s\"", $2);
75 }
76 | TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
77 | TCHROOT TSTRING { conf.chroot = $2; }
78 | TUSER TSTRING { conf.user = $2; }
79 ;
81 vhosts : /* empty */
82 | vhosts vhost
83 ;
85 vhost : TSERVER TSTRING '{' servopts locations '}' {
86 host->locations[0].match = (char*)"*";
87 host->domain = $2;
89 if (host->cert == NULL || host->key == NULL ||
90 host->dir == NULL)
91 errx(1, "invalid vhost definition: %s", $2);
93 if (++ihost == HOSTSLEN)
94 errx(1, "too much vhosts defined");
96 host++;
97 loc = &host->locations[0];
98 iloc = 0;
99 }
100 | error '}' { yyerror("error in server directive"); }
103 servopts : /* empty */
104 | servopts servopt
107 servopt : TCERT TSTRING { host->cert = $2; }
108 | TKEY TSTRING { host->key = $2; }
109 | TROOT TSTRING { host->dir = $2; }
110 | TCGI TSTRING {
111 host->cgi = $2;
112 /* drop the starting '/', if any */
113 if (*host->cgi == '/')
114 host->cgi++;
116 | locopt
119 locations : /* empty */
120 | locations location
123 location : TLOCATION TSTRING '{' locopts '}' {
124 loc->match = $2;
125 if (++iloc == LOCLEN)
126 errx(1, "too much location rules defined");
127 loc++;
129 | error '}'
132 locopts : /* empty */
133 | locopts locopt
136 locopt : TDEFAULT TTYPE TSTRING {
137 if (loc->default_mime != NULL)
138 yyerror("`default type' specified more than once");
139 loc->default_mime = $3;
141 | TLANG TSTRING {
142 if (loc->lang != NULL)
143 yyerror("`lang' specified more than once");
144 loc->lang = $2;
146 | TINDEX TSTRING {
147 if (loc->index != NULL)
148 yyerror("`index' specified more than once");
149 loc->index = $2;
151 | TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
154 %%
156 void
157 yyerror(const char *msg)
159 goterror = 1;
160 fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
163 int
164 parse_portno(const char *p)
166 const char *errstr;
167 int n;
169 n = strtonum(p, 0, UINT16_MAX, &errstr);
170 if (errstr != NULL)
171 errx(1, "port number is %s: %s", errstr, p);
172 return n;
175 void
176 parse_conf(const char *path)
178 config_path = path;
179 if ((yyin = fopen(path, "r")) == NULL)
180 fatal("cannot open config %s", path);
181 yyparse();
182 fclose(yyin);
184 if (goterror)
185 exit(1);