Blame


1 15902770 2021-01-15 op %{
2 15902770 2021-01-15 op
3 15902770 2021-01-15 op /*
4 15902770 2021-01-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 15902770 2021-01-15 op *
6 15902770 2021-01-15 op * Permission to use, copy, modify, and distribute this software for any
7 15902770 2021-01-15 op * purpose with or without fee is hereby granted, provided that the above
8 15902770 2021-01-15 op * copyright notice and this permission notice appear in all copies.
9 15902770 2021-01-15 op *
10 15902770 2021-01-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 15902770 2021-01-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 15902770 2021-01-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 15902770 2021-01-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 15902770 2021-01-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 15902770 2021-01-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 15902770 2021-01-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 15902770 2021-01-15 op */
18 15902770 2021-01-15 op
19 74f0778b 2021-06-16 op #include <ctype.h>
20 002a84a1 2021-02-10 op #include <errno.h>
21 6abda252 2021-02-06 op #include <stdarg.h>
22 15902770 2021-01-15 op #include <stdio.h>
23 74f0778b 2021-06-16 op #include <stdlib.h>
24 32693ee6 2021-01-28 op #include <string.h>
25 15902770 2021-01-15 op
26 15902770 2021-01-15 op #include "gmid.h"
27 15902770 2021-01-15 op
28 74f0778b 2021-06-16 op FILE *yyfp;
29 74f0778b 2021-06-16 op
30 ef129b08 2021-06-16 op typedef struct {
31 ef129b08 2021-06-16 op union {
32 ef129b08 2021-06-16 op char *str;
33 ef129b08 2021-06-16 op int num;
34 8235a81c 2021-06-29 op } v;
35 ef129b08 2021-06-16 op int lineno;
36 ef129b08 2021-06-16 op int colno;
37 ef129b08 2021-06-16 op } yystype;
38 ef129b08 2021-06-16 op #define YYSTYPE yystype
39 ef129b08 2021-06-16 op
40 15902770 2021-01-15 op /*
41 15902770 2021-01-15 op * #define YYDEBUG 1
42 15902770 2021-01-15 op * int yydebug = 1;
43 15902770 2021-01-15 op */
44 15902770 2021-01-15 op
45 3b21cca3 2021-06-29 op /*
46 3b21cca3 2021-06-29 op * The idea behind this implementation of macros is from rad/parse.y
47 3b21cca3 2021-06-29 op */
48 3b21cca3 2021-06-29 op TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
49 3b21cca3 2021-06-29 op struct sym {
50 3b21cca3 2021-06-29 op TAILQ_ENTRY(sym) entry;
51 3b21cca3 2021-06-29 op int used;
52 3b21cca3 2021-06-29 op int persist;
53 3b21cca3 2021-06-29 op char *name;
54 3b21cca3 2021-06-29 op char *val;
55 3b21cca3 2021-06-29 op };
56 3b21cca3 2021-06-29 op
57 ca21e100 2021-02-04 op struct vhost *host;
58 ca21e100 2021-02-04 op struct location *loc;
59 15902770 2021-01-15 op
60 74f0778b 2021-06-16 op static int goterror;
61 15902770 2021-01-15 op
62 b8e64ccd 2021-03-31 op static struct vhost *new_vhost(void);
63 b8e64ccd 2021-03-31 op static struct location *new_location(void);
64 b8e64ccd 2021-03-31 op
65 6abda252 2021-02-06 op void yyerror(const char*, ...);
66 d93c8191 2021-07-09 op int kw_cmp(const void *, const void *);
67 74f0778b 2021-06-16 op static int yylex(void);
68 13ed2fb6 2021-01-27 op int parse_portno(const char*);
69 13ed2fb6 2021-01-27 op void parse_conf(const char*);
70 e17642a7 2021-02-01 op char *ensure_absolute_path(char*);
71 6abda252 2021-02-06 op int check_block_code(int);
72 6abda252 2021-02-06 op char *check_block_fmt(char*);
73 6abda252 2021-02-06 op int check_strip_no(int);
74 a709ddf5 2021-02-07 op int check_prefork_num(int);
75 49b73ba1 2021-02-10 op void advance_loc(void);
76 c705ecb1 2021-05-03 op void only_once(const void*, const char*);
77 8ad1c570 2021-05-09 op void only_oncei(int, const char*);
78 8ad1c570 2021-05-09 op int fastcgi_conf(char *, char *, char *);
79 c92b802b 2021-06-11 op void add_param(char *, char *, int);
80 3b21cca3 2021-06-29 op int symset(const char *, const char *, int);
81 3b21cca3 2021-06-29 op char *symget(const char *);
82 13ed2fb6 2021-01-27 op
83 15902770 2021-01-15 op %}
84 15902770 2021-01-15 op
85 15902770 2021-01-15 op /* for bison: */
86 15902770 2021-01-15 op /* %define parse.error verbose */
87 15902770 2021-01-15 op
88 7252049d 2021-06-29 op %token TIPV6 TPORT TPROTOCOLS TMIME TDEFAULT TTYPE TCHROOT TUSER TSERVER
89 7252049d 2021-06-29 op %token TPREFORK TLOCATION TCERT TKEY TROOT TCGI TENV TLANG TLOG TINDEX TAUTO
90 7252049d 2021-06-29 op %token TSTRIP TBLOCK TRETURN TENTRYPOINT TREQUIRE TCLIENT TCA TALIAS TTCP
91 efacb859 2021-07-09 op %token TFASTCGI TSPAWN TPARAM TMAP TTOEXT
92 d06d6f4b 2021-04-29 op
93 7252049d 2021-06-29 op %token TERR
94 7252049d 2021-06-29 op
95 7252049d 2021-06-29 op %token <v.str> TSTRING
96 7252049d 2021-06-29 op %token <v.num> TNUM
97 7252049d 2021-06-29 op %token <v.num> TBOOL
98 15902770 2021-01-15 op
99 7252049d 2021-06-29 op %type <v.str> string
100 98f52178 2021-06-29 op
101 15902770 2021-01-15 op %%
102 15902770 2021-01-15 op
103 6b86655a 2021-06-29 op conf : /* empty */
104 6b86655a 2021-06-29 op | conf var
105 6b86655a 2021-06-29 op | conf option
106 6b86655a 2021-06-29 op | conf vhost
107 3b21cca3 2021-06-29 op ;
108 3b21cca3 2021-06-29 op
109 98f52178 2021-06-29 op string : string TSTRING {
110 98f52178 2021-06-29 op if (asprintf(&$$, "%s%s", $1, $2) == -1) {
111 98f52178 2021-06-29 op free($1);
112 98f52178 2021-06-29 op free($2);
113 98f52178 2021-06-29 op yyerror("string: asprintf: %s", strerror(errno));
114 98f52178 2021-06-29 op YYERROR;
115 98f52178 2021-06-29 op }
116 98f52178 2021-06-29 op free($1);
117 98f52178 2021-06-29 op free($2);
118 98f52178 2021-06-29 op }
119 98f52178 2021-06-29 op | TSTRING
120 98f52178 2021-06-29 op ;
121 98f52178 2021-06-29 op
122 98f52178 2021-06-29 op var : TSTRING '=' string {
123 3b21cca3 2021-06-29 op char *s = $1;
124 3b21cca3 2021-06-29 op while (*s++) {
125 3b21cca3 2021-06-29 op if (isspace(*s)) {
126 3b21cca3 2021-06-29 op yyerror("macro name cannot contain "
127 3b21cca3 2021-06-29 op "whitespaces");
128 3b21cca3 2021-06-29 op free($1);
129 3b21cca3 2021-06-29 op free($3);
130 3b21cca3 2021-06-29 op YYERROR;
131 3b21cca3 2021-06-29 op }
132 3b21cca3 2021-06-29 op }
133 3b21cca3 2021-06-29 op symset($1, $3, 0);
134 3b21cca3 2021-06-29 op free($1);
135 3b21cca3 2021-06-29 op free($3);
136 3b21cca3 2021-06-29 op }
137 15902770 2021-01-15 op ;
138 15902770 2021-01-15 op
139 98f52178 2021-06-29 op option : TCHROOT string { conf.chroot = $2; }
140 eb59f87e 2021-02-09 op | TIPV6 TBOOL { conf.ipv6 = $2; }
141 d19951cf 2021-07-09 op | TMIME TSTRING string {
142 d19951cf 2021-07-09 op fprintf(stderr, "%s:%d: `mime MIME EXT' is deprecated and "
143 d19951cf 2021-07-09 op "will be removed in a future version, "
144 d19951cf 2021-07-09 op "please use the new syntax: `map MIME to-ext EXT'",
145 d19951cf 2021-07-09 op config_path, yylval.lineno+1);
146 d19951cf 2021-07-09 op add_mime(&conf.mime, $2, $3);
147 d19951cf 2021-07-09 op }
148 ff954a3e 2021-07-09 op | TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
149 15902770 2021-01-15 op | TPORT TNUM { conf.port = $2; }
150 eb59f87e 2021-02-09 op | TPREFORK TNUM { conf.prefork = check_prefork_num($2); }
151 98f52178 2021-06-29 op | TPROTOCOLS string {
152 5bc3c98e 2021-01-15 op if (tls_config_parse_protocols(&conf.protos, $2) == -1)
153 002a84a1 2021-02-10 op yyerror("invalid protocols string \"%s\"", $2);
154 5bc3c98e 2021-01-15 op }
155 98f52178 2021-06-29 op | TUSER string { conf.user = $2; }
156 15902770 2021-01-15 op ;
157 15902770 2021-01-15 op
158 98f52178 2021-06-29 op vhost : TSERVER string {
159 b8e64ccd 2021-03-31 op host = new_vhost();
160 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
161 b8e64ccd 2021-03-31 op
162 b8e64ccd 2021-03-31 op loc = new_location();
163 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
164 b8e64ccd 2021-03-31 op
165 b8e64ccd 2021-03-31 op loc->match = xstrdup("*");
166 15902770 2021-01-15 op host->domain = $2;
167 15902770 2021-01-15 op
168 cbeee4ca 2021-01-28 op if (strstr($2, "xn--") != NULL) {
169 ef129b08 2021-06-16 op warnx("%s:%d:%d \"%s\" looks like punycode: "
170 415ac7a2 2021-01-28 op "you should use the decoded hostname.",
171 ef129b08 2021-06-16 op config_path, yylval.lineno+1, yylval.colno,
172 ef129b08 2021-06-16 op $2);
173 cbeee4ca 2021-01-28 op }
174 b8e64ccd 2021-03-31 op } '{' servopts locations '}' {
175 cbeee4ca 2021-01-28 op
176 fdea6aa0 2021-04-30 op if (host->cert == NULL || host->key == NULL)
177 002a84a1 2021-02-10 op yyerror("invalid vhost definition: %s", $2);
178 15902770 2021-01-15 op }
179 15902770 2021-01-15 op | error '}' { yyerror("error in server directive"); }
180 15902770 2021-01-15 op ;
181 15902770 2021-01-15 op
182 15902770 2021-01-15 op servopts : /* empty */
183 15902770 2021-01-15 op | servopts servopt
184 15902770 2021-01-15 op ;
185 15902770 2021-01-15 op
186 98f52178 2021-06-29 op servopt : TALIAS string {
187 cc8c2901 2021-04-29 op struct alist *a;
188 cc8c2901 2021-04-29 op
189 cc8c2901 2021-04-29 op a = xcalloc(1, sizeof(*a));
190 cc8c2901 2021-04-29 op a->alias = $2;
191 cc8c2901 2021-04-29 op if (TAILQ_EMPTY(&host->aliases))
192 cc8c2901 2021-04-29 op TAILQ_INSERT_HEAD(&host->aliases, a, aliases);
193 cc8c2901 2021-04-29 op else
194 cc8c2901 2021-04-29 op TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
195 cc8c2901 2021-04-29 op }
196 98f52178 2021-06-29 op | TCERT string {
197 c705ecb1 2021-05-03 op only_once(host->cert, "cert");
198 c705ecb1 2021-05-03 op host->cert = ensure_absolute_path($2);
199 c705ecb1 2021-05-03 op }
200 98f52178 2021-06-29 op | TCGI string {
201 c705ecb1 2021-05-03 op only_once(host->cgi, "cgi");
202 15902770 2021-01-15 op /* drop the starting '/', if any */
203 709f4c94 2021-02-04 op if (*$2 == '/')
204 709f4c94 2021-02-04 op memmove($2, $2+1, strlen($2));
205 709f4c94 2021-02-04 op host->cgi = $2;
206 05c23a54 2021-01-19 op }
207 98f52178 2021-06-29 op | TENTRYPOINT string {
208 c705ecb1 2021-05-03 op only_once(host->entrypoint, "entrypoint");
209 e3ddf390 2021-02-06 op while (*$2 == '/')
210 e3ddf390 2021-02-06 op memmove($2, $2+1, strlen($2));
211 e3ddf390 2021-02-06 op host->entrypoint = $2;
212 e3ddf390 2021-02-06 op }
213 efacb859 2021-07-09 op | TENV string '=' string {
214 762b9b99 2021-07-09 op add_param($2, $4, 1);
215 9cc630aa 2021-04-28 op }
216 98f52178 2021-06-29 op | TKEY string {
217 c705ecb1 2021-05-03 op only_once(host->key, "key");
218 c705ecb1 2021-05-03 op host->key = ensure_absolute_path($2);
219 c92b802b 2021-06-11 op }
220 efacb859 2021-07-09 op | TPARAM string '=' string {
221 762b9b99 2021-07-09 op add_param($2, $4, 0);
222 c705ecb1 2021-05-03 op }
223 c8b74339 2021-01-24 op | locopt
224 c8b74339 2021-01-24 op ;
225 c8b74339 2021-01-24 op
226 c8b74339 2021-01-24 op locations : /* empty */
227 c8b74339 2021-01-24 op | locations location
228 c8b74339 2021-01-24 op ;
229 c8b74339 2021-01-24 op
230 98f52178 2021-06-29 op location : TLOCATION { advance_loc(); } string '{' locopts '}' {
231 49b73ba1 2021-02-10 op /* drop the starting '/' if any */
232 49b73ba1 2021-02-10 op if (*$3 == '/')
233 49b73ba1 2021-02-10 op memmove($3, $3+1, strlen($3));
234 49b73ba1 2021-02-10 op loc->match = $3;
235 6119e13e 2021-01-19 op }
236 c8b74339 2021-01-24 op | error '}'
237 c8b74339 2021-01-24 op ;
238 c8b74339 2021-01-24 op
239 c8b74339 2021-01-24 op locopts : /* empty */
240 c8b74339 2021-01-24 op | locopts locopt
241 c8b74339 2021-01-24 op ;
242 c8b74339 2021-01-24 op
243 eb59f87e 2021-02-09 op locopt : TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
244 98f52178 2021-06-29 op | TBLOCK TRETURN TNUM string {
245 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
246 6abda252 2021-02-06 op loc->block_fmt = check_block_fmt($4);
247 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
248 6abda252 2021-02-06 op }
249 6abda252 2021-02-06 op | TBLOCK TRETURN TNUM {
250 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
251 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
252 6abda252 2021-02-06 op loc->block_code = check_block_code($3);
253 6abda252 2021-02-06 op if ($3 >= 30 && $3 < 40)
254 6abda252 2021-02-06 op yyerror("missing `meta' for block return %d", $3);
255 6abda252 2021-02-06 op }
256 6abda252 2021-02-06 op | TBLOCK {
257 c705ecb1 2021-05-03 op only_once(loc->block_fmt, "block");
258 6abda252 2021-02-06 op loc->block_fmt = xstrdup("temporary failure");
259 6abda252 2021-02-06 op loc->block_code = 40;
260 6abda252 2021-02-06 op }
261 98f52178 2021-06-29 op | TDEFAULT TTYPE string {
262 c705ecb1 2021-05-03 op only_once(loc->default_mime, "default type");
263 eb59f87e 2021-02-09 op loc->default_mime = $3;
264 eb59f87e 2021-02-09 op }
265 0d047efc 2021-05-24 op | TFASTCGI fastcgi
266 98f52178 2021-06-29 op | TINDEX string {
267 c705ecb1 2021-05-03 op only_once(loc->index, "index");
268 eb59f87e 2021-02-09 op loc->index = $2;
269 eb59f87e 2021-02-09 op }
270 98f52178 2021-06-29 op | TLANG string {
271 c705ecb1 2021-05-03 op only_once(loc->lang, "lang");
272 eb59f87e 2021-02-09 op loc->lang = $2;
273 eb59f87e 2021-02-09 op }
274 793835cb 2021-02-23 op | TLOG TBOOL { loc->disable_log = !$2; }
275 98f52178 2021-06-29 op | TREQUIRE TCLIENT TCA string {
276 c705ecb1 2021-05-03 op only_once(loc->reqca, "require client ca");
277 adbe6a64 2021-04-30 op ensure_absolute_path($4);
278 02be96c6 2021-02-09 op if ((loc->reqca = load_ca($4)) == NULL)
279 02be96c6 2021-02-09 op yyerror("couldn't load ca cert: %s", $4);
280 02be96c6 2021-02-09 op free($4);
281 fdea6aa0 2021-04-30 op }
282 98f52178 2021-06-29 op | TROOT string {
283 c705ecb1 2021-05-03 op only_once(loc->dir, "root");
284 fdea6aa0 2021-04-30 op loc->dir = ensure_absolute_path($2);
285 02be96c6 2021-02-09 op }
286 eb59f87e 2021-02-09 op | TSTRIP TNUM { loc->strip = check_strip_no($2); }
287 15902770 2021-01-15 op ;
288 13ed2fb6 2021-01-27 op
289 98f52178 2021-06-29 op fastcgi : TSPAWN string {
290 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
291 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf(NULL, NULL, $2);
292 0d047efc 2021-05-24 op }
293 98f52178 2021-06-29 op | string {
294 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
295 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($1, NULL, NULL);
296 0d047efc 2021-05-24 op }
297 762b9b99 2021-07-09 op | TTCP string TPORT TNUM {
298 0d047efc 2021-05-24 op char *c;
299 762b9b99 2021-07-09 op if (asprintf(&c, "%d", $4) == -1)
300 0d047efc 2021-05-24 op err(1, "asprintf");
301 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
302 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, c, NULL);
303 0d047efc 2021-05-24 op }
304 98f52178 2021-06-29 op | TTCP string {
305 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
306 0d047efc 2021-05-24 op loc->fcgi = fastcgi_conf($2, xstrdup("9000"), NULL);
307 0d047efc 2021-05-24 op }
308 762b9b99 2021-07-09 op | TTCP string TPORT string {
309 0d047efc 2021-05-24 op only_oncei(loc->fcgi, "fastcgi");
310 762b9b99 2021-07-09 op loc->fcgi = fastcgi_conf($2, $4, NULL);
311 0d047efc 2021-05-24 op }
312 0d047efc 2021-05-24 op ;
313 0d047efc 2021-05-24 op
314 13ed2fb6 2021-01-27 op %%
315 b8e64ccd 2021-03-31 op
316 b8e64ccd 2021-03-31 op static struct vhost *
317 b8e64ccd 2021-03-31 op new_vhost(void)
318 b8e64ccd 2021-03-31 op {
319 b8e64ccd 2021-03-31 op return xcalloc(1, sizeof(struct vhost));
320 b8e64ccd 2021-03-31 op }
321 13ed2fb6 2021-01-27 op
322 b8e64ccd 2021-03-31 op static struct location *
323 b8e64ccd 2021-03-31 op new_location(void)
324 b8e64ccd 2021-03-31 op {
325 fdea6aa0 2021-04-30 op struct location *l;
326 fdea6aa0 2021-04-30 op
327 fdea6aa0 2021-04-30 op l = xcalloc(1, sizeof(*l));
328 fdea6aa0 2021-04-30 op l->dirfd = -1;
329 8ad1c570 2021-05-09 op l->fcgi = -1;
330 fdea6aa0 2021-04-30 op return l;
331 b8e64ccd 2021-03-31 op }
332 b8e64ccd 2021-03-31 op
333 13ed2fb6 2021-01-27 op void
334 6abda252 2021-02-06 op yyerror(const char *msg, ...)
335 13ed2fb6 2021-01-27 op {
336 6abda252 2021-02-06 op va_list ap;
337 6abda252 2021-02-06 op
338 13ed2fb6 2021-01-27 op goterror = 1;
339 6abda252 2021-02-06 op
340 6abda252 2021-02-06 op va_start(ap, msg);
341 ef129b08 2021-06-16 op fprintf(stderr, "%s:%d: ", config_path, yylval.lineno);
342 6abda252 2021-02-06 op vfprintf(stderr, msg, ap);
343 a1373913 2021-02-07 op fprintf(stderr, "\n");
344 6abda252 2021-02-06 op va_end(ap);
345 74f0778b 2021-06-16 op }
346 74f0778b 2021-06-16 op
347 74f0778b 2021-06-16 op static struct keyword {
348 74f0778b 2021-06-16 op const char *word;
349 74f0778b 2021-06-16 op int token;
350 74f0778b 2021-06-16 op } keywords[] = {
351 d93c8191 2021-07-09 op /* these MUST be sorted */
352 74f0778b 2021-06-16 op {"alias", TALIAS},
353 74f0778b 2021-06-16 op {"auto", TAUTO},
354 74f0778b 2021-06-16 op {"block", TBLOCK},
355 74f0778b 2021-06-16 op {"ca", TCA},
356 74f0778b 2021-06-16 op {"cert", TCERT},
357 74f0778b 2021-06-16 op {"cgi", TCGI},
358 74f0778b 2021-06-16 op {"chroot", TCHROOT},
359 74f0778b 2021-06-16 op {"client", TCLIENT},
360 74f0778b 2021-06-16 op {"default", TDEFAULT},
361 74f0778b 2021-06-16 op {"entrypoint", TENTRYPOINT},
362 74f0778b 2021-06-16 op {"env", TENV},
363 74f0778b 2021-06-16 op {"fastcgi", TFASTCGI},
364 74f0778b 2021-06-16 op {"index", TINDEX},
365 74f0778b 2021-06-16 op {"ipv6", TIPV6},
366 74f0778b 2021-06-16 op {"key", TKEY},
367 74f0778b 2021-06-16 op {"lang", TLANG},
368 74f0778b 2021-06-16 op {"location", TLOCATION},
369 74f0778b 2021-06-16 op {"log", TLOG},
370 d19951cf 2021-07-09 op {"map", TMAP},
371 74f0778b 2021-06-16 op {"mime", TMIME},
372 74f0778b 2021-06-16 op {"param", TPARAM},
373 74f0778b 2021-06-16 op {"port", TPORT},
374 74f0778b 2021-06-16 op {"prefork", TPREFORK},
375 74f0778b 2021-06-16 op {"protocols", TPROTOCOLS},
376 74f0778b 2021-06-16 op {"require", TREQUIRE},
377 74f0778b 2021-06-16 op {"return", TRETURN},
378 74f0778b 2021-06-16 op {"root", TROOT},
379 74f0778b 2021-06-16 op {"server", TSERVER},
380 74f0778b 2021-06-16 op {"spawn", TSPAWN},
381 74f0778b 2021-06-16 op {"strip", TSTRIP},
382 74f0778b 2021-06-16 op {"tcp", TTCP},
383 d19951cf 2021-07-09 op {"to-ext", TTOEXT},
384 74f0778b 2021-06-16 op {"type", TTYPE},
385 74f0778b 2021-06-16 op {"user", TUSER},
386 74f0778b 2021-06-16 op };
387 74f0778b 2021-06-16 op
388 d93c8191 2021-07-09 op int
389 d93c8191 2021-07-09 op kw_cmp(const void *k, const void *e)
390 d93c8191 2021-07-09 op {
391 d93c8191 2021-07-09 op return strcmp(k, ((struct keyword *)e)->word);
392 d93c8191 2021-07-09 op }
393 d93c8191 2021-07-09 op
394 74f0778b 2021-06-16 op /*
395 74f0778b 2021-06-16 op * Taken an adapted from doas' parse.y
396 74f0778b 2021-06-16 op */
397 74f0778b 2021-06-16 op static int
398 74f0778b 2021-06-16 op yylex(void)
399 74f0778b 2021-06-16 op {
400 d93c8191 2021-07-09 op struct keyword *kw;
401 3b21cca3 2021-06-29 op char buf[8096], *ebuf, *p, *str, *v, *val;
402 74f0778b 2021-06-16 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
403 d93c8191 2021-07-09 op size_t len;
404 74f0778b 2021-06-16 op
405 74f0778b 2021-06-16 op p = buf;
406 74f0778b 2021-06-16 op ebuf = buf + sizeof(buf);
407 74f0778b 2021-06-16 op
408 74f0778b 2021-06-16 op repeat:
409 74f0778b 2021-06-16 op /* skip whitespace first */
410 74f0778b 2021-06-16 op for (c = getc(yyfp); isspace(c); c = getc(yyfp)) {
411 ef129b08 2021-06-16 op yylval.colno++;
412 74f0778b 2021-06-16 op if (c == '\n') {
413 ef129b08 2021-06-16 op yylval.lineno++;
414 ef129b08 2021-06-16 op yylval.colno = 0;
415 74f0778b 2021-06-16 op }
416 74f0778b 2021-06-16 op }
417 74f0778b 2021-06-16 op
418 74f0778b 2021-06-16 op /* check for special one-character constructions */
419 74f0778b 2021-06-16 op switch (c) {
420 74f0778b 2021-06-16 op case '{':
421 74f0778b 2021-06-16 op case '}':
422 74f0778b 2021-06-16 op return c;
423 74f0778b 2021-06-16 op case '#':
424 74f0778b 2021-06-16 op /* skip comments; NUL is allowed; no continuation */
425 74f0778b 2021-06-16 op while ((c = getc(yyfp)) != '\n')
426 74f0778b 2021-06-16 op if (c == EOF)
427 74f0778b 2021-06-16 op goto eof;
428 ef129b08 2021-06-16 op yylval.colno = 0;
429 ef129b08 2021-06-16 op yylval.lineno++;
430 74f0778b 2021-06-16 op goto repeat;
431 3b21cca3 2021-06-29 op case '=':
432 efacb859 2021-07-09 op return c;
433 74f0778b 2021-06-16 op case EOF:
434 74f0778b 2021-06-16 op goto eof;
435 74f0778b 2021-06-16 op }
436 74f0778b 2021-06-16 op
437 74f0778b 2021-06-16 op /* parsing next word */
438 ef129b08 2021-06-16 op for (;; c = getc(yyfp), yylval.colno++) {
439 74f0778b 2021-06-16 op switch (c) {
440 74f0778b 2021-06-16 op case '\0':
441 74f0778b 2021-06-16 op yyerror("unallowed character NULL in column %d",
442 ef129b08 2021-06-16 op yylval.colno+1);
443 74f0778b 2021-06-16 op escape = 0;
444 74f0778b 2021-06-16 op continue;
445 74f0778b 2021-06-16 op case '\\':
446 74f0778b 2021-06-16 op escape = !escape;
447 74f0778b 2021-06-16 op if (escape)
448 74f0778b 2021-06-16 op continue;
449 74f0778b 2021-06-16 op break;
450 3b21cca3 2021-06-29 op
451 3b21cca3 2021-06-29 op /* expand macros in-place */
452 3b21cca3 2021-06-29 op case '$':
453 bfb076ed 2021-06-29 op if (!escape && !quotes) {
454 3b21cca3 2021-06-29 op v = p;
455 3b21cca3 2021-06-29 op while (1) {
456 3b21cca3 2021-06-29 op if ((c = getc(yyfp)) == EOF) {
457 3b21cca3 2021-06-29 op yyerror("EOF during macro expansion");
458 3b21cca3 2021-06-29 op return 0;
459 3b21cca3 2021-06-29 op }
460 3b21cca3 2021-06-29 op if (p + 1 >= ebuf - 1) {
461 3b21cca3 2021-06-29 op yyerror("string too long");
462 3b21cca3 2021-06-29 op return 0;
463 3b21cca3 2021-06-29 op }
464 3b21cca3 2021-06-29 op if (isalnum(c) || c == '_') {
465 3b21cca3 2021-06-29 op *p++ = c;
466 3b21cca3 2021-06-29 op continue;
467 3b21cca3 2021-06-29 op }
468 3b21cca3 2021-06-29 op *p = 0;
469 3b21cca3 2021-06-29 op break;
470 3b21cca3 2021-06-29 op }
471 3b21cca3 2021-06-29 op p = v;
472 3b21cca3 2021-06-29 op if ((val = symget(p)) == NULL) {
473 3b21cca3 2021-06-29 op yyerror("macro '%s' not defined", v);
474 bfb076ed 2021-06-29 op return TERR;
475 3b21cca3 2021-06-29 op }
476 3b21cca3 2021-06-29 op len = strlen(val);
477 3b21cca3 2021-06-29 op if (p + len >= ebuf - 1) {
478 3b21cca3 2021-06-29 op yyerror("after macro-expansion, "
479 3b21cca3 2021-06-29 op "string too long");
480 bfb076ed 2021-06-29 op return TERR;
481 3b21cca3 2021-06-29 op }
482 3b21cca3 2021-06-29 op *p = '\0';
483 3b21cca3 2021-06-29 op strlcat(p, val, ebuf - p);
484 3b21cca3 2021-06-29 op p += len;
485 bfb076ed 2021-06-29 op nonkw = 1;
486 bfb076ed 2021-06-29 op goto eow;
487 3b21cca3 2021-06-29 op }
488 3b21cca3 2021-06-29 op break;
489 74f0778b 2021-06-16 op case '\n':
490 74f0778b 2021-06-16 op if (quotes)
491 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
492 ef129b08 2021-06-16 op yylval.colno+1);
493 74f0778b 2021-06-16 op if (escape) {
494 74f0778b 2021-06-16 op nonkw = 1;
495 74f0778b 2021-06-16 op escape = 0;
496 ef129b08 2021-06-16 op yylval.colno = 0;
497 ef129b08 2021-06-16 op yylval.lineno++;
498 74f0778b 2021-06-16 op }
499 74f0778b 2021-06-16 op goto eow;
500 74f0778b 2021-06-16 op case EOF:
501 74f0778b 2021-06-16 op if (escape)
502 74f0778b 2021-06-16 op yyerror("unterminated escape in column %d",
503 ef129b08 2021-06-16 op yylval.colno);
504 74f0778b 2021-06-16 op if (quotes)
505 74f0778b 2021-06-16 op yyerror("unterminated quotes in column %d",
506 74f0778b 2021-06-16 op qpos+1);
507 74f0778b 2021-06-16 op goto eow;
508 74f0778b 2021-06-16 op case '{':
509 74f0778b 2021-06-16 op case '}':
510 74f0778b 2021-06-16 op case '#':
511 74f0778b 2021-06-16 op case ' ':
512 74f0778b 2021-06-16 op case '\t':
513 74f0778b 2021-06-16 op if (!escape && !quotes)
514 74f0778b 2021-06-16 op goto eow;
515 74f0778b 2021-06-16 op break;
516 74f0778b 2021-06-16 op case '"':
517 74f0778b 2021-06-16 op if (!escape) {
518 74f0778b 2021-06-16 op quotes = !quotes;
519 74f0778b 2021-06-16 op if (quotes) {
520 74f0778b 2021-06-16 op nonkw = 1;
521 ef129b08 2021-06-16 op qpos = yylval.colno;
522 74f0778b 2021-06-16 op }
523 74f0778b 2021-06-16 op continue;
524 74f0778b 2021-06-16 op }
525 74f0778b 2021-06-16 op }
526 74f0778b 2021-06-16 op *p++ = c;
527 74f0778b 2021-06-16 op if (p == ebuf) {
528 74f0778b 2021-06-16 op yyerror("line too long");
529 74f0778b 2021-06-16 op p = buf;
530 74f0778b 2021-06-16 op }
531 74f0778b 2021-06-16 op escape = 0;
532 74f0778b 2021-06-16 op }
533 74f0778b 2021-06-16 op
534 74f0778b 2021-06-16 op eow:
535 74f0778b 2021-06-16 op *p = 0;
536 74f0778b 2021-06-16 op if (c != EOF)
537 74f0778b 2021-06-16 op ungetc(c, yyfp);
538 74f0778b 2021-06-16 op if (p == buf) {
539 74f0778b 2021-06-16 op /*
540 74f0778b 2021-06-16 op * There could be a number of reason for empty buffer,
541 74f0778b 2021-06-16 op * and we handle all of them here, to avoid cluttering
542 74f0778b 2021-06-16 op * the main loop.
543 74f0778b 2021-06-16 op */
544 74f0778b 2021-06-16 op if (c == EOF)
545 74f0778b 2021-06-16 op goto eof;
546 74f0778b 2021-06-16 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
547 74f0778b 2021-06-16 op goto repeat;
548 74f0778b 2021-06-16 op }
549 74f0778b 2021-06-16 op if (!nonkw) {
550 d93c8191 2021-07-09 op kw = bsearch(buf, keywords, sizeof(keywords)/sizeof(keywords[0]),
551 d93c8191 2021-07-09 op sizeof(keywords[0]), kw_cmp);
552 d93c8191 2021-07-09 op if (kw != NULL)
553 d93c8191 2021-07-09 op return kw->token;
554 74f0778b 2021-06-16 op }
555 74f0778b 2021-06-16 op c = *buf;
556 74f0778b 2021-06-16 op if (!nonkw && (c == '-' || isdigit(c))) {
557 8235a81c 2021-06-29 op yylval.v.num = parse_portno(buf);
558 74f0778b 2021-06-16 op return TNUM;
559 74f0778b 2021-06-16 op }
560 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "on")) {
561 8235a81c 2021-06-29 op yylval.v.num = 1;
562 74f0778b 2021-06-16 op return TBOOL;
563 74f0778b 2021-06-16 op }
564 74f0778b 2021-06-16 op if (!nonkw && !strcmp(buf, "off")) {
565 8235a81c 2021-06-29 op yylval.v.num = 0;
566 74f0778b 2021-06-16 op return TBOOL;
567 74f0778b 2021-06-16 op }
568 74f0778b 2021-06-16 op if ((str = strdup(buf)) == NULL)
569 74f0778b 2021-06-16 op err(1, "%s", __func__);
570 8235a81c 2021-06-29 op yylval.v.str = str;
571 74f0778b 2021-06-16 op return TSTRING;
572 74f0778b 2021-06-16 op
573 74f0778b 2021-06-16 op eof:
574 74f0778b 2021-06-16 op if (ferror(yyfp))
575 74f0778b 2021-06-16 op yyerror("input error reading config");
576 74f0778b 2021-06-16 op return 0;
577 13ed2fb6 2021-01-27 op }
578 13ed2fb6 2021-01-27 op
579 13ed2fb6 2021-01-27 op int
580 13ed2fb6 2021-01-27 op parse_portno(const char *p)
581 13ed2fb6 2021-01-27 op {
582 13ed2fb6 2021-01-27 op const char *errstr;
583 13ed2fb6 2021-01-27 op int n;
584 13ed2fb6 2021-01-27 op
585 13ed2fb6 2021-01-27 op n = strtonum(p, 0, UINT16_MAX, &errstr);
586 13ed2fb6 2021-01-27 op if (errstr != NULL)
587 2d34f732 2021-02-10 op yyerror("port number is %s: %s", errstr, p);
588 13ed2fb6 2021-01-27 op return n;
589 13ed2fb6 2021-01-27 op }
590 13ed2fb6 2021-01-27 op
591 13ed2fb6 2021-01-27 op void
592 13ed2fb6 2021-01-27 op parse_conf(const char *path)
593 13ed2fb6 2021-01-27 op {
594 3b21cca3 2021-06-29 op struct sym *sym, *next;
595 3b21cca3 2021-06-29 op
596 13ed2fb6 2021-01-27 op config_path = path;
597 74f0778b 2021-06-16 op if ((yyfp = fopen(path, "r")) == NULL)
598 48b69cb2 2021-04-28 op err(1, "cannot open config: %s", path);
599 13ed2fb6 2021-01-27 op yyparse();
600 74f0778b 2021-06-16 op fclose(yyfp);
601 13ed2fb6 2021-01-27 op
602 13ed2fb6 2021-01-27 op if (goterror)
603 13ed2fb6 2021-01-27 op exit(1);
604 b8e64ccd 2021-03-31 op
605 b8e64ccd 2021-03-31 op if (TAILQ_FIRST(&hosts)->domain == NULL)
606 48b69cb2 2021-04-28 op errx(1, "no vhost defined in %s", path);
607 3b21cca3 2021-06-29 op
608 3b21cca3 2021-06-29 op /* free unused macros */
609 3b21cca3 2021-06-29 op TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
610 3b21cca3 2021-06-29 op /* TODO: warn if !sym->used */
611 3b21cca3 2021-06-29 op if (!sym->persist) {
612 3b21cca3 2021-06-29 op free(sym->name);
613 3b21cca3 2021-06-29 op free(sym->val);
614 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
615 3b21cca3 2021-06-29 op free(sym);
616 3b21cca3 2021-06-29 op }
617 3b21cca3 2021-06-29 op }
618 13ed2fb6 2021-01-27 op }
619 e17642a7 2021-02-01 op
620 e17642a7 2021-02-01 op char *
621 e17642a7 2021-02-01 op ensure_absolute_path(char *path)
622 e17642a7 2021-02-01 op {
623 e17642a7 2021-02-01 op if (path == NULL || *path != '/')
624 adbe6a64 2021-04-30 op yyerror("not an absolute path: %s", path);
625 e17642a7 2021-02-01 op return path;
626 e17642a7 2021-02-01 op }
627 6abda252 2021-02-06 op
628 6abda252 2021-02-06 op int
629 6abda252 2021-02-06 op check_block_code(int n)
630 6abda252 2021-02-06 op {
631 6abda252 2021-02-06 op if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
632 6abda252 2021-02-06 op yyerror("invalid block code %d", n);
633 6abda252 2021-02-06 op return n;
634 6abda252 2021-02-06 op }
635 6abda252 2021-02-06 op
636 6abda252 2021-02-06 op char *
637 6abda252 2021-02-06 op check_block_fmt(char *fmt)
638 6abda252 2021-02-06 op {
639 6abda252 2021-02-06 op char *s;
640 6abda252 2021-02-06 op
641 6abda252 2021-02-06 op for (s = fmt; *s; ++s) {
642 6abda252 2021-02-06 op if (*s != '%')
643 6abda252 2021-02-06 op continue;
644 6abda252 2021-02-06 op switch (*++s) {
645 6abda252 2021-02-06 op case '%':
646 6abda252 2021-02-06 op case 'p':
647 6abda252 2021-02-06 op case 'q':
648 6abda252 2021-02-06 op case 'P':
649 6abda252 2021-02-06 op case 'N':
650 6abda252 2021-02-06 op break;
651 6abda252 2021-02-06 op default:
652 6abda252 2021-02-06 op yyerror("invalid format specifier %%%c", *s);
653 6abda252 2021-02-06 op }
654 6abda252 2021-02-06 op }
655 6abda252 2021-02-06 op
656 6abda252 2021-02-06 op return fmt;
657 6abda252 2021-02-06 op }
658 6abda252 2021-02-06 op
659 6abda252 2021-02-06 op int
660 6abda252 2021-02-06 op check_strip_no(int n)
661 6abda252 2021-02-06 op {
662 6abda252 2021-02-06 op if (n <= 0)
663 6abda252 2021-02-06 op yyerror("invalid strip number %d", n);
664 a709ddf5 2021-02-07 op return n;
665 a709ddf5 2021-02-07 op }
666 a709ddf5 2021-02-07 op
667 a709ddf5 2021-02-07 op int
668 a709ddf5 2021-02-07 op check_prefork_num(int n)
669 a709ddf5 2021-02-07 op {
670 2c3e53da 2021-03-03 op if (n <= 0 || n >= PROC_MAX)
671 a709ddf5 2021-02-07 op yyerror("invalid prefork number %d", n);
672 6abda252 2021-02-06 op return n;
673 6abda252 2021-02-06 op }
674 49b73ba1 2021-02-10 op
675 49b73ba1 2021-02-10 op void
676 49b73ba1 2021-02-10 op advance_loc(void)
677 49b73ba1 2021-02-10 op {
678 b8e64ccd 2021-03-31 op loc = new_location();
679 b8e64ccd 2021-03-31 op TAILQ_INSERT_TAIL(&host->locations, loc, locations);
680 49b73ba1 2021-02-10 op }
681 c705ecb1 2021-05-03 op
682 c705ecb1 2021-05-03 op void
683 c705ecb1 2021-05-03 op only_once(const void *ptr, const char *name)
684 c705ecb1 2021-05-03 op {
685 c705ecb1 2021-05-03 op if (ptr != NULL)
686 c705ecb1 2021-05-03 op yyerror("`%s' specified more than once", name);
687 c705ecb1 2021-05-03 op }
688 8ad1c570 2021-05-09 op
689 8ad1c570 2021-05-09 op void
690 8ad1c570 2021-05-09 op only_oncei(int i, const char *name)
691 8ad1c570 2021-05-09 op {
692 8ad1c570 2021-05-09 op if (i != -1)
693 8ad1c570 2021-05-09 op yyerror("`%s' specified more than once", name);
694 8ad1c570 2021-05-09 op }
695 8ad1c570 2021-05-09 op
696 8ad1c570 2021-05-09 op int
697 8ad1c570 2021-05-09 op fastcgi_conf(char *path, char *port, char *prog)
698 8ad1c570 2021-05-09 op {
699 8ad1c570 2021-05-09 op struct fcgi *f;
700 8ad1c570 2021-05-09 op int i;
701 8ad1c570 2021-05-09 op
702 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
703 8ad1c570 2021-05-09 op f = &fcgi[i];
704 fafc6849 2021-06-29 op
705 8ad1c570 2021-05-09 op if (f->path == NULL) {
706 8ad1c570 2021-05-09 op f->id = i;
707 8ad1c570 2021-05-09 op f->path = path;
708 8ad1c570 2021-05-09 op f->port = port;
709 8ad1c570 2021-05-09 op f->prog = prog;
710 8ad1c570 2021-05-09 op return i;
711 8ad1c570 2021-05-09 op }
712 8ad1c570 2021-05-09 op
713 8ad1c570 2021-05-09 op /* XXX: what to do with prog? */
714 8ad1c570 2021-05-09 op if (!strcmp(f->path, path) &&
715 8ad1c570 2021-05-09 op ((port == NULL && f->port == NULL) ||
716 8ad1c570 2021-05-09 op !strcmp(f->port, port))) {
717 8ad1c570 2021-05-09 op free(path);
718 8ad1c570 2021-05-09 op free(port);
719 8ad1c570 2021-05-09 op return i;
720 8ad1c570 2021-05-09 op }
721 8ad1c570 2021-05-09 op }
722 8ad1c570 2021-05-09 op
723 8ad1c570 2021-05-09 op yyerror("too much `fastcgi' rules defined.");
724 8ad1c570 2021-05-09 op return -1;
725 c92b802b 2021-06-11 op }
726 c92b802b 2021-06-11 op
727 c92b802b 2021-06-11 op void
728 c92b802b 2021-06-11 op add_param(char *name, char *val, int env)
729 c92b802b 2021-06-11 op {
730 c92b802b 2021-06-11 op struct envlist *e;
731 c92b802b 2021-06-11 op struct envhead *h;
732 c92b802b 2021-06-11 op
733 c92b802b 2021-06-11 op if (env)
734 c92b802b 2021-06-11 op h = &host->env;
735 c92b802b 2021-06-11 op else
736 c92b802b 2021-06-11 op h = &host->params;
737 c92b802b 2021-06-11 op
738 c92b802b 2021-06-11 op e = xcalloc(1, sizeof(*e));
739 c92b802b 2021-06-11 op e->name = name;
740 c92b802b 2021-06-11 op e->value = val;
741 c92b802b 2021-06-11 op if (TAILQ_EMPTY(h))
742 c92b802b 2021-06-11 op TAILQ_INSERT_HEAD(h, e, envs);
743 c92b802b 2021-06-11 op else
744 c92b802b 2021-06-11 op TAILQ_INSERT_TAIL(h, e, envs);
745 3b21cca3 2021-06-29 op }
746 3b21cca3 2021-06-29 op
747 3b21cca3 2021-06-29 op int
748 3b21cca3 2021-06-29 op symset(const char *name, const char *val, int persist)
749 3b21cca3 2021-06-29 op {
750 3b21cca3 2021-06-29 op struct sym *sym;
751 3b21cca3 2021-06-29 op
752 3b21cca3 2021-06-29 op TAILQ_FOREACH(sym, &symhead, entry) {
753 3b21cca3 2021-06-29 op if (!strcmp(name, sym->name))
754 3b21cca3 2021-06-29 op break;
755 3b21cca3 2021-06-29 op }
756 3b21cca3 2021-06-29 op
757 3b21cca3 2021-06-29 op if (sym != NULL) {
758 3b21cca3 2021-06-29 op if (sym->persist)
759 3b21cca3 2021-06-29 op return 0;
760 3b21cca3 2021-06-29 op else {
761 3b21cca3 2021-06-29 op free(sym->name);
762 3b21cca3 2021-06-29 op free(sym->val);
763 3b21cca3 2021-06-29 op TAILQ_REMOVE(&symhead, sym, entry);
764 3b21cca3 2021-06-29 op free(sym);
765 3b21cca3 2021-06-29 op }
766 3b21cca3 2021-06-29 op }
767 3b21cca3 2021-06-29 op
768 3b21cca3 2021-06-29 op sym = xcalloc(1, sizeof(*sym));
769 3b21cca3 2021-06-29 op sym->name = xstrdup(name);
770 3b21cca3 2021-06-29 op sym->val = xstrdup(val);
771 3b21cca3 2021-06-29 op sym->used = 0;
772 3b21cca3 2021-06-29 op sym->persist = persist;
773 3b21cca3 2021-06-29 op
774 3b21cca3 2021-06-29 op TAILQ_INSERT_TAIL(&symhead, sym, entry);
775 3b21cca3 2021-06-29 op return 0;
776 8ad1c570 2021-05-09 op }
777 3b21cca3 2021-06-29 op
778 3b21cca3 2021-06-29 op int
779 3b21cca3 2021-06-29 op cmdline_symset(char *s)
780 3b21cca3 2021-06-29 op {
781 3b21cca3 2021-06-29 op char *sym, *val;
782 3b21cca3 2021-06-29 op int ret;
783 3b21cca3 2021-06-29 op
784 3b21cca3 2021-06-29 op if ((val = strrchr(s, '=')) == NULL)
785 3b21cca3 2021-06-29 op return -1;
786 3b21cca3 2021-06-29 op sym = xcalloc(1, val - s + 1);
787 3b21cca3 2021-06-29 op memcpy(sym, s, val - s);
788 3b21cca3 2021-06-29 op ret = symset(sym, val + 1, 1);
789 3b21cca3 2021-06-29 op free(sym);
790 3b21cca3 2021-06-29 op return ret;
791 3b21cca3 2021-06-29 op }
792 3b21cca3 2021-06-29 op
793 3b21cca3 2021-06-29 op char *
794 3b21cca3 2021-06-29 op symget(const char *name)
795 3b21cca3 2021-06-29 op {
796 3b21cca3 2021-06-29 op struct sym *sym;
797 3b21cca3 2021-06-29 op
798 3b21cca3 2021-06-29 op TAILQ_FOREACH(sym, &symhead, entry) {
799 3b21cca3 2021-06-29 op if (!strcmp(name, sym->name)) {
800 3b21cca3 2021-06-29 op sym->used = 1;
801 3b21cca3 2021-06-29 op return sym->val;
802 3b21cca3 2021-06-29 op }
803 3b21cca3 2021-06-29 op }
804 3b21cca3 2021-06-29 op
805 3b21cca3 2021-06-29 op return NULL;
806 3b21cca3 2021-06-29 op }