2 c34ec417 2020-06-22 tracey * Copyright (c) 2019, 2020 Tracey Emery <tracey@openbsd.org>
3 c34ec417 2020-06-22 tracey * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 c34ec417 2020-06-22 tracey * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 2c251c14 2020-01-15 tracey * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 2c251c14 2020-01-15 tracey * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 2c251c14 2020-01-15 tracey * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 2c251c14 2020-01-15 tracey * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 2c251c14 2020-01-15 tracey * Permission to use, copy, modify, and distribute this software for any
11 2c251c14 2020-01-15 tracey * purpose with or without fee is hereby granted, provided that the above
12 2c251c14 2020-01-15 tracey * copyright notice and this permission notice appear in all copies.
14 2c251c14 2020-01-15 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 2c251c14 2020-01-15 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 2c251c14 2020-01-15 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 2c251c14 2020-01-15 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 2c251c14 2020-01-15 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 2c251c14 2020-01-15 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 2c251c14 2020-01-15 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 2c251c14 2020-01-15 tracey #include <sys/types.h>
25 2c251c14 2020-01-15 tracey #include <sys/queue.h>
27 2c251c14 2020-01-15 tracey #include <ctype.h>
28 2c251c14 2020-01-15 tracey #include <err.h>
29 2c251c14 2020-01-15 tracey #include <limits.h>
30 2c251c14 2020-01-15 tracey #include <stdarg.h>
31 2c251c14 2020-01-15 tracey #include <stdio.h>
32 cad0b9e8 2020-09-25 naddy #include <stdlib.h>
33 2c251c14 2020-01-15 tracey #include <string.h>
35 c34ec417 2020-06-22 tracey #include "got_error.h"
36 2c251c14 2020-01-15 tracey #include "gotweb.h"
38 2c251c14 2020-01-15 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
39 2c251c14 2020-01-15 tracey static struct file {
40 2c251c14 2020-01-15 tracey TAILQ_ENTRY(file) entry;
41 2c251c14 2020-01-15 tracey FILE *stream;
42 2c251c14 2020-01-15 tracey char *name;
43 c34ec417 2020-06-22 tracey size_t ungetpos;
44 c34ec417 2020-06-22 tracey size_t ungetsize;
45 c34ec417 2020-06-22 tracey u_char *ungetbuf;
46 c34ec417 2020-06-22 tracey int eof_reached;
47 2c251c14 2020-01-15 tracey int lineno;
48 2c251c14 2020-01-15 tracey } *file, *topfile;
49 c34ec417 2020-06-22 tracey static const struct got_error* pushfile(struct file**, const char *);
50 2c251c14 2020-01-15 tracey int popfile(void);
51 2c251c14 2020-01-15 tracey int yyparse(void);
52 2c251c14 2020-01-15 tracey int yylex(void);
53 2c251c14 2020-01-15 tracey int yyerror(const char *, ...)
54 2c251c14 2020-01-15 tracey __attribute__((__format__ (printf, 1, 2)))
55 2c251c14 2020-01-15 tracey __attribute__((__nonnull__ (1)));
56 2c251c14 2020-01-15 tracey int kw_cmp(const void *, const void *);
57 2c251c14 2020-01-15 tracey int lookup(char *);
58 c34ec417 2020-06-22 tracey int igetc(void);
59 2c251c14 2020-01-15 tracey int lgetc(int);
60 c34ec417 2020-06-22 tracey void lungetc(int);
61 2c251c14 2020-01-15 tracey int findeol(void);
63 c34ec417 2020-06-22 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
64 c34ec417 2020-06-22 tracey struct sym {
65 c34ec417 2020-06-22 tracey TAILQ_ENTRY(sym) entry;
67 c34ec417 2020-06-22 tracey int persist;
68 c34ec417 2020-06-22 tracey char *nam;
69 c34ec417 2020-06-22 tracey char *val;
72 c34ec417 2020-06-22 tracey int symset(const char *, const char *, int);
73 c34ec417 2020-06-22 tracey char *symget(const char *);
75 c34ec417 2020-06-22 tracey const struct got_error* gerror = NULL;
76 f71c0a3a 2020-06-23 tracey struct gotweb_config *gw_conf;
78 2c251c14 2020-01-15 tracey typedef struct {
80 c34ec417 2020-06-22 tracey int64_t number;
81 c34ec417 2020-06-22 tracey char *string;
83 2c251c14 2020-01-15 tracey int lineno;
84 2c251c14 2020-01-15 tracey } YYSTYPE;
88 2c251c14 2020-01-15 tracey %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
89 2c251c14 2020-01-15 tracey %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
90 2c251c14 2020-01-15 tracey %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
91 a03d32e0 2020-11-09 tracey %token GOT_MAX_COMMITS_DISPLAY ERROR GOT_SHOW_SITE_OWNER
92 2c251c14 2020-01-15 tracey %token GOT_SHOW_REPO_CLONEURL
93 c34ec417 2020-06-22 tracey %token <v.string> STRING
94 c34ec417 2020-06-22 tracey %token <v.number> NUMBER
95 c34ec417 2020-06-22 tracey %type <v.number> boolean
98 2c251c14 2020-01-15 tracey grammar : /* empty */
99 2c251c14 2020-01-15 tracey | grammar '\n'
100 2c251c14 2020-01-15 tracey | grammar main '\n'
103 c34ec417 2020-06-22 tracey boolean : STRING {
104 2c251c14 2020-01-15 tracey if (strcasecmp($1, "true") == 0 ||
105 a03d32e0 2020-11-09 tracey strcasecmp($1, "on") == 0 ||
106 2c251c14 2020-01-15 tracey strcasecmp($1, "yes") == 0)
108 2c251c14 2020-01-15 tracey else if (strcasecmp($1, "false") == 0 ||
109 2c251c14 2020-01-15 tracey strcasecmp($1, "off") == 0 ||
110 2c251c14 2020-01-15 tracey strcasecmp($1, "no") == 0)
113 2c251c14 2020-01-15 tracey yyerror("invalid boolean value '%s'", $1);
114 2c251c14 2020-01-15 tracey free($1);
117 2c251c14 2020-01-15 tracey free($1);
120 2c251c14 2020-01-15 tracey main : GOT_REPOS_PATH STRING {
121 f71c0a3a 2020-06-23 tracey gw_conf->got_repos_path = strdup($2);
122 9f6f3943 2020-09-24 tracey if (gw_conf->got_repos_path == NULL) {
123 c34ec417 2020-06-22 tracey free($2);
124 c34ec417 2020-06-22 tracey yyerror("strdup");
127 ff21961d 2020-07-29 tracey free($2);
129 9f6f3943 2020-09-24 tracey | GOT_WWW_PATH STRING {
130 9f6f3943 2020-09-24 tracey gw_conf->got_www_path = strdup($2);
131 9f6f3943 2020-09-24 tracey if (gw_conf->got_www_path == NULL) {
132 9f6f3943 2020-09-24 tracey free($2);
133 9f6f3943 2020-09-24 tracey yyerror("strdup");
136 9f6f3943 2020-09-24 tracey free($2);
138 2c251c14 2020-01-15 tracey | GOT_MAX_REPOS NUMBER {
139 2c251c14 2020-01-15 tracey if ($2 > 0)
140 f71c0a3a 2020-06-23 tracey gw_conf->got_max_repos = $2;
142 2c251c14 2020-01-15 tracey | GOT_SITE_NAME STRING {
143 f71c0a3a 2020-06-23 tracey gw_conf->got_site_name = strdup($2);
144 f71c0a3a 2020-06-23 tracey if (gw_conf->got_site_name == NULL) {
145 c34ec417 2020-06-22 tracey free($2);
146 c34ec417 2020-06-22 tracey yyerror("strdup");
149 ff21961d 2020-07-29 tracey free($2);
151 2c251c14 2020-01-15 tracey | GOT_SITE_OWNER STRING {
152 f71c0a3a 2020-06-23 tracey gw_conf->got_site_owner = strdup($2);
153 f71c0a3a 2020-06-23 tracey if (gw_conf->got_site_owner == NULL) {
154 c34ec417 2020-06-22 tracey free($2);
155 c34ec417 2020-06-22 tracey yyerror("strdup");
158 ff21961d 2020-07-29 tracey free($2);
160 2c251c14 2020-01-15 tracey | GOT_SITE_LINK STRING {
161 f71c0a3a 2020-06-23 tracey gw_conf->got_site_link = strdup($2);
162 f71c0a3a 2020-06-23 tracey if (gw_conf->got_site_link == NULL) {
163 c34ec417 2020-06-22 tracey free($2);
164 c34ec417 2020-06-22 tracey yyerror("strdup");
167 ff21961d 2020-07-29 tracey free($2);
169 2c251c14 2020-01-15 tracey | GOT_LOGO STRING {
170 f71c0a3a 2020-06-23 tracey gw_conf->got_logo = strdup($2);
171 f71c0a3a 2020-06-23 tracey if (gw_conf->got_logo== NULL) {
172 c34ec417 2020-06-22 tracey free($2);
173 c34ec417 2020-06-22 tracey yyerror("strdup");
176 ff21961d 2020-07-29 tracey free($2);
178 2c251c14 2020-01-15 tracey | GOT_LOGO_URL STRING {
179 f71c0a3a 2020-06-23 tracey gw_conf->got_logo_url = strdup($2);
180 f71c0a3a 2020-06-23 tracey if (gw_conf->got_logo_url== NULL) {
181 c34ec417 2020-06-22 tracey free($2);
182 c34ec417 2020-06-22 tracey yyerror("strdup");
185 ff21961d 2020-07-29 tracey free($2);
187 2c251c14 2020-01-15 tracey | GOT_SHOW_SITE_OWNER boolean {
188 f71c0a3a 2020-06-23 tracey gw_conf->got_show_site_owner = $2;
190 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_OWNER boolean {
191 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_owner = $2;
193 c34ec417 2020-06-22 tracey | GOT_SHOW_REPO_AGE boolean {
194 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_age = $2;
196 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_DESCRIPTION boolean {
197 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_description = $2;
199 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_CLONEURL boolean {
200 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_cloneurl = $2;
202 2c251c14 2020-01-15 tracey | GOT_MAX_REPOS_DISPLAY NUMBER {
203 2c251c14 2020-01-15 tracey if ($2 > 0)
204 f71c0a3a 2020-06-23 tracey gw_conf->got_max_repos_display = $2;
206 2c251c14 2020-01-15 tracey | GOT_MAX_COMMITS_DISPLAY NUMBER {
207 2c251c14 2020-01-15 tracey if ($2 > 0)
208 f71c0a3a 2020-06-23 tracey gw_conf->got_max_commits_display = $2;
213 2c251c14 2020-01-15 tracey struct keywords {
214 2c251c14 2020-01-15 tracey const char *k_name;
215 2c251c14 2020-01-15 tracey int k_val;
219 2c251c14 2020-01-15 tracey yyerror(const char *fmt, ...)
221 2c251c14 2020-01-15 tracey va_list ap;
222 c34ec417 2020-06-22 tracey char *msg;
223 c34ec417 2020-06-22 tracey char *err = NULL;
225 2c251c14 2020-01-15 tracey va_start(ap, fmt);
226 c34ec417 2020-06-22 tracey if (vasprintf(&msg, fmt, ap) == -1) {
227 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("vasprintf");
228 c34ec417 2020-06-22 tracey return 0;
230 2c251c14 2020-01-15 tracey va_end(ap);
231 c34ec417 2020-06-22 tracey if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
232 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("asprintf");
233 c34ec417 2020-06-22 tracey return(0);
235 257add31 2020-09-09 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
236 2c251c14 2020-01-15 tracey free(msg);
237 c34ec417 2020-06-22 tracey free(err);
238 c34ec417 2020-06-22 tracey return(0);
242 2c251c14 2020-01-15 tracey kw_cmp(const void *k, const void *e)
244 2c251c14 2020-01-15 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
248 2c251c14 2020-01-15 tracey lookup(char *s)
250 c34ec417 2020-06-22 tracey /* This has to be sorted always. */
251 2c251c14 2020-01-15 tracey static const struct keywords keywords[] = {
252 2c251c14 2020-01-15 tracey { "got_logo", GOT_LOGO },
253 2c251c14 2020-01-15 tracey { "got_logo_url", GOT_LOGO_URL },
254 2c251c14 2020-01-15 tracey { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
255 2c251c14 2020-01-15 tracey { "got_max_repos", GOT_MAX_REPOS },
256 2c251c14 2020-01-15 tracey { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
257 2c251c14 2020-01-15 tracey { "got_repos_path", GOT_REPOS_PATH },
258 2c251c14 2020-01-15 tracey { "got_show_repo_age", GOT_SHOW_REPO_AGE },
259 2c251c14 2020-01-15 tracey { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
260 2c251c14 2020-01-15 tracey { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
261 2c251c14 2020-01-15 tracey { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
262 2c251c14 2020-01-15 tracey { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
263 2c251c14 2020-01-15 tracey { "got_site_link", GOT_SITE_LINK },
264 2c251c14 2020-01-15 tracey { "got_site_name", GOT_SITE_NAME },
265 2c251c14 2020-01-15 tracey { "got_site_owner", GOT_SITE_OWNER },
266 9f6f3943 2020-09-24 tracey { "got_www_path", GOT_WWW_PATH },
268 2c251c14 2020-01-15 tracey const struct keywords *p;
270 2c251c14 2020-01-15 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
271 2c251c14 2020-01-15 tracey sizeof(keywords[0]), kw_cmp);
274 2c251c14 2020-01-15 tracey return (p->k_val);
276 2c251c14 2020-01-15 tracey return (STRING);
279 c34ec417 2020-06-22 tracey #define START_EXPAND 1
280 c34ec417 2020-06-22 tracey #define DONE_EXPAND 2
282 c34ec417 2020-06-22 tracey static int expanding;
285 c34ec417 2020-06-22 tracey igetc(void)
289 c34ec417 2020-06-22 tracey while (1) {
290 c34ec417 2020-06-22 tracey if (file->ungetpos > 0)
291 c34ec417 2020-06-22 tracey c = file->ungetbuf[--file->ungetpos];
293 c34ec417 2020-06-22 tracey c = getc(file->stream);
295 c34ec417 2020-06-22 tracey if (c == START_EXPAND)
296 c34ec417 2020-06-22 tracey expanding = 1;
297 c34ec417 2020-06-22 tracey else if (c == DONE_EXPAND)
298 c34ec417 2020-06-22 tracey expanding = 0;
302 c34ec417 2020-06-22 tracey return (c);
306 c34ec417 2020-06-22 tracey lgetc(int quotec)
308 c34ec417 2020-06-22 tracey int c, next;
310 2c251c14 2020-01-15 tracey if (quotec) {
311 c34ec417 2020-06-22 tracey if ((c = igetc()) == EOF) {
312 2c251c14 2020-01-15 tracey yyerror("reached end of file while parsing "
313 2c251c14 2020-01-15 tracey "quoted string");
314 2c251c14 2020-01-15 tracey if (file == topfile || popfile() == EOF)
315 2c251c14 2020-01-15 tracey return (EOF);
316 2c251c14 2020-01-15 tracey return (quotec);
318 2c251c14 2020-01-15 tracey return (c);
321 c34ec417 2020-06-22 tracey while ((c = igetc()) == '\\') {
322 c34ec417 2020-06-22 tracey next = igetc();
323 2c251c14 2020-01-15 tracey if (next != '\n') {
324 2c251c14 2020-01-15 tracey c = next;
327 2c251c14 2020-01-15 tracey yylval.lineno = file->lineno;
328 2c251c14 2020-01-15 tracey file->lineno++;
331 c34ec417 2020-06-22 tracey if (c == EOF) {
333 c34ec417 2020-06-22 tracey * Fake EOL when hit EOF for the first time. This gets line
334 c34ec417 2020-06-22 tracey * count right if last line in included file is syntactically
335 c34ec417 2020-06-22 tracey * invalid and has no newline.
337 c34ec417 2020-06-22 tracey if (file->eof_reached == 0) {
338 c34ec417 2020-06-22 tracey file->eof_reached = 1;
339 c34ec417 2020-06-22 tracey return ('\n');
341 c34ec417 2020-06-22 tracey while (c == EOF) {
342 c34ec417 2020-06-22 tracey if (file == topfile || popfile() == EOF)
343 c34ec417 2020-06-22 tracey return (EOF);
344 c34ec417 2020-06-22 tracey c = igetc();
347 2c251c14 2020-01-15 tracey return (c);
351 2c251c14 2020-01-15 tracey lungetc(int c)
353 2c251c14 2020-01-15 tracey if (c == EOF)
356 c34ec417 2020-06-22 tracey if (file->ungetpos >= file->ungetsize) {
357 c34ec417 2020-06-22 tracey void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
358 c34ec417 2020-06-22 tracey if (p == NULL)
359 c34ec417 2020-06-22 tracey err(1, "%s", __func__);
360 c34ec417 2020-06-22 tracey file->ungetbuf = p;
361 c34ec417 2020-06-22 tracey file->ungetsize *= 2;
363 c34ec417 2020-06-22 tracey file->ungetbuf[file->ungetpos++] = c;
367 2c251c14 2020-01-15 tracey findeol(void)
371 c34ec417 2020-06-22 tracey /* Skip to either EOF or the first real EOL. */
372 2c251c14 2020-01-15 tracey while (1) {
373 c34ec417 2020-06-22 tracey c = lgetc(0);
374 2c251c14 2020-01-15 tracey if (c == '\n') {
375 2c251c14 2020-01-15 tracey file->lineno++;
378 2c251c14 2020-01-15 tracey if (c == EOF)
381 2c251c14 2020-01-15 tracey return (ERROR);
385 2c251c14 2020-01-15 tracey yylex(void)
387 c34ec417 2020-06-22 tracey unsigned char buf[8096];
388 c34ec417 2020-06-22 tracey unsigned char *p, *val;
389 c34ec417 2020-06-22 tracey int quotec, next, c;
390 c34ec417 2020-06-22 tracey int token;
394 2c251c14 2020-01-15 tracey while ((c = lgetc(0)) == ' ' || c == '\t')
395 2c251c14 2020-01-15 tracey ; /* nothing */
397 2c251c14 2020-01-15 tracey yylval.lineno = file->lineno;
398 2c251c14 2020-01-15 tracey if (c == '#')
399 2c251c14 2020-01-15 tracey while ((c = lgetc(0)) != '\n' && c != EOF)
400 2c251c14 2020-01-15 tracey ; /* nothing */
401 c34ec417 2020-06-22 tracey if (c == '$' && !expanding) {
402 c34ec417 2020-06-22 tracey while (1) {
403 c34ec417 2020-06-22 tracey if ((c = lgetc(0)) == EOF)
404 c34ec417 2020-06-22 tracey return (0);
406 c34ec417 2020-06-22 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
407 c34ec417 2020-06-22 tracey yyerror("string too long");
408 c34ec417 2020-06-22 tracey return (findeol());
410 c34ec417 2020-06-22 tracey if (isalnum(c) || c == '_') {
411 c34ec417 2020-06-22 tracey *p++ = c;
412 c34ec417 2020-06-22 tracey continue;
414 c34ec417 2020-06-22 tracey *p = '\0';
415 c34ec417 2020-06-22 tracey lungetc(c);
418 c34ec417 2020-06-22 tracey val = symget(buf);
419 c34ec417 2020-06-22 tracey if (val == NULL) {
420 c34ec417 2020-06-22 tracey yyerror("macro '%s' not defined", buf);
421 c34ec417 2020-06-22 tracey return (findeol());
423 c34ec417 2020-06-22 tracey p = val + strlen(val) - 1;
424 c34ec417 2020-06-22 tracey lungetc(DONE_EXPAND);
425 c34ec417 2020-06-22 tracey while (p >= val) {
426 c34ec417 2020-06-22 tracey lungetc(*p);
429 c34ec417 2020-06-22 tracey lungetc(START_EXPAND);
430 c34ec417 2020-06-22 tracey goto top;
433 2c251c14 2020-01-15 tracey switch (c) {
434 2c251c14 2020-01-15 tracey case '\'':
435 2c251c14 2020-01-15 tracey case '"':
436 2c251c14 2020-01-15 tracey quotec = c;
437 2c251c14 2020-01-15 tracey while (1) {
438 2c251c14 2020-01-15 tracey if ((c = lgetc(quotec)) == EOF)
439 2c251c14 2020-01-15 tracey return (0);
440 2c251c14 2020-01-15 tracey if (c == '\n') {
441 2c251c14 2020-01-15 tracey file->lineno++;
442 2c251c14 2020-01-15 tracey continue;
443 2c251c14 2020-01-15 tracey } else if (c == '\\') {
444 2c251c14 2020-01-15 tracey if ((next = lgetc(quotec)) == EOF)
445 2c251c14 2020-01-15 tracey return (0);
446 c34ec417 2020-06-22 tracey if (next == quotec || c == ' ' || c == '\t')
447 2c251c14 2020-01-15 tracey c = next;
448 2c251c14 2020-01-15 tracey else if (next == '\n') {
449 2c251c14 2020-01-15 tracey file->lineno++;
450 2c251c14 2020-01-15 tracey continue;
452 2c251c14 2020-01-15 tracey lungetc(next);
453 2c251c14 2020-01-15 tracey } else if (c == quotec) {
454 2c251c14 2020-01-15 tracey *p = '\0';
456 2c251c14 2020-01-15 tracey } else if (c == '\0') {
457 2c251c14 2020-01-15 tracey yyerror("syntax error");
458 2c251c14 2020-01-15 tracey return (findeol());
460 2c251c14 2020-01-15 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
461 2c251c14 2020-01-15 tracey yyerror("string too long");
462 2c251c14 2020-01-15 tracey return (findeol());
464 2c251c14 2020-01-15 tracey *p++ = c;
466 2c251c14 2020-01-15 tracey yylval.v.string = strdup(buf);
467 2c251c14 2020-01-15 tracey if (yylval.v.string == NULL)
468 c34ec417 2020-06-22 tracey err(1, "%s", __func__);
469 2c251c14 2020-01-15 tracey return (STRING);
472 2c251c14 2020-01-15 tracey #define allowed_to_end_number(x) \
473 2c251c14 2020-01-15 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
475 2c251c14 2020-01-15 tracey if (c == '-' || isdigit(c)) {
477 2c251c14 2020-01-15 tracey *p++ = c;
478 c34ec417 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
479 2c251c14 2020-01-15 tracey yyerror("string too long");
480 2c251c14 2020-01-15 tracey return (findeol());
482 2c251c14 2020-01-15 tracey } while ((c = lgetc(0)) != EOF && isdigit(c));
483 2c251c14 2020-01-15 tracey lungetc(c);
484 2c251c14 2020-01-15 tracey if (p == buf + 1 && buf[0] == '-')
485 2c251c14 2020-01-15 tracey goto nodigits;
486 2c251c14 2020-01-15 tracey if (c == EOF || allowed_to_end_number(c)) {
487 2c251c14 2020-01-15 tracey const char *errstr = NULL;
489 2c251c14 2020-01-15 tracey *p = '\0';
490 2c251c14 2020-01-15 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
491 2c251c14 2020-01-15 tracey LLONG_MAX, &errstr);
492 2c251c14 2020-01-15 tracey if (errstr) {
493 2c251c14 2020-01-15 tracey yyerror("\"%s\" invalid number: %s",
494 2c251c14 2020-01-15 tracey buf, errstr);
495 2c251c14 2020-01-15 tracey return (findeol());
497 2c251c14 2020-01-15 tracey return (NUMBER);
499 2c251c14 2020-01-15 tracey nodigits:
500 2c251c14 2020-01-15 tracey while (p > buf + 1)
501 2c251c14 2020-01-15 tracey lungetc(*--p);
502 2c251c14 2020-01-15 tracey c = *--p;
503 2c251c14 2020-01-15 tracey if (c == '-')
504 2c251c14 2020-01-15 tracey return (c);
508 2c251c14 2020-01-15 tracey #define allowed_in_string(x) \
509 2c251c14 2020-01-15 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
510 c34ec417 2020-06-22 tracey x != '{' && x != '}' && \
511 c34ec417 2020-06-22 tracey x != '!' && x != '=' && x != '#' && \
512 2c251c14 2020-01-15 tracey x != ','))
514 c34ec417 2020-06-22 tracey if (isalnum(c) || c == ':' || c == '_') {
516 2c251c14 2020-01-15 tracey *p++ = c;
517 c34ec417 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
518 2c251c14 2020-01-15 tracey yyerror("string too long");
519 2c251c14 2020-01-15 tracey return (findeol());
521 2c251c14 2020-01-15 tracey } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
522 2c251c14 2020-01-15 tracey lungetc(c);
523 2c251c14 2020-01-15 tracey *p = '\0';
524 2c251c14 2020-01-15 tracey if ((token = lookup(buf)) == STRING)
525 2c251c14 2020-01-15 tracey if ((yylval.v.string = strdup(buf)) == NULL)
526 c34ec417 2020-06-22 tracey err(1, "%s", __func__);
527 2c251c14 2020-01-15 tracey return (token);
529 2c251c14 2020-01-15 tracey if (c == '\n') {
530 2c251c14 2020-01-15 tracey yylval.lineno = file->lineno;
531 2c251c14 2020-01-15 tracey file->lineno++;
533 2c251c14 2020-01-15 tracey if (c == EOF)
534 2c251c14 2020-01-15 tracey return (0);
535 2c251c14 2020-01-15 tracey return (c);
538 c34ec417 2020-06-22 tracey static const struct got_error*
539 c34ec417 2020-06-22 tracey pushfile(struct file **nfile, const char *name)
541 c34ec417 2020-06-22 tracey const struct got_error* error = NULL;
543 c34ec417 2020-06-22 tracey if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
544 c34ec417 2020-06-22 tracey return got_error_from_errno2(__func__, "calloc");
545 c34ec417 2020-06-22 tracey if (((*nfile)->name = strdup(name)) == NULL) {
546 2c251c14 2020-01-15 tracey free(nfile);
547 c34ec417 2020-06-22 tracey return got_error_from_errno2(__func__, "strdup");
549 c34ec417 2020-06-22 tracey if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
550 c34ec417 2020-06-22 tracey char *msg = NULL;
551 c34ec417 2020-06-22 tracey if (asprintf(&msg, "%s", (*nfile)->name) == -1)
552 c34ec417 2020-06-22 tracey return got_error_from_errno("asprintf");
553 c34ec417 2020-06-22 tracey error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
554 c34ec417 2020-06-22 tracey free((*nfile)->name);
555 c34ec417 2020-06-22 tracey free((*nfile));
556 c34ec417 2020-06-22 tracey free(msg);
557 c34ec417 2020-06-22 tracey return error;
559 c34ec417 2020-06-22 tracey (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
560 c34ec417 2020-06-22 tracey (*nfile)->ungetsize = 16;
561 c34ec417 2020-06-22 tracey (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
562 c34ec417 2020-06-22 tracey if ((*nfile)->ungetbuf == NULL) {
563 c34ec417 2020-06-22 tracey fclose((*nfile)->stream);
564 c34ec417 2020-06-22 tracey free((*nfile)->name);
565 c34ec417 2020-06-22 tracey free((*nfile));
566 c34ec417 2020-06-22 tracey return got_error_from_errno2(__func__, "malloc");
568 c34ec417 2020-06-22 tracey TAILQ_INSERT_TAIL(&files, (*nfile), entry);
569 c34ec417 2020-06-22 tracey return error;
573 2c251c14 2020-01-15 tracey popfile(void)
575 c34ec417 2020-06-22 tracey struct file *prev = NULL;
577 2c251c14 2020-01-15 tracey TAILQ_REMOVE(&files, file, entry);
578 2c251c14 2020-01-15 tracey fclose(file->stream);
579 2c251c14 2020-01-15 tracey free(file->name);
580 c34ec417 2020-06-22 tracey free(file->ungetbuf);
581 2c251c14 2020-01-15 tracey free(file);
582 2c251c14 2020-01-15 tracey file = prev;
583 2c251c14 2020-01-15 tracey return (file ? 0 : EOF);
586 2c251c14 2020-01-15 tracey const struct got_error*
587 c34ec417 2020-06-22 tracey parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
589 f71c0a3a 2020-06-23 tracey gw_conf = malloc(sizeof(struct gotweb_config));
590 f71c0a3a 2020-06-23 tracey if (gw_conf == NULL) {
591 f71c0a3a 2020-06-23 tracey gerror = got_error_from_errno("malloc");
592 f71c0a3a 2020-06-23 tracey goto done;
594 f71c0a3a 2020-06-23 tracey gw_conf->got_repos_path = strdup(D_GOTPATH);
595 f71c0a3a 2020-06-23 tracey if (gw_conf->got_repos_path == NULL) {
596 9f6f3943 2020-09-24 tracey gerror = got_error_from_errno("strdup");
597 9f6f3943 2020-09-24 tracey goto done;
599 9f6f3943 2020-09-24 tracey gw_conf->got_www_path = strdup(D_GOTWWW);
600 9f6f3943 2020-09-24 tracey if (gw_conf->got_www_path == NULL) {
601 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("strdup");
602 2c251c14 2020-01-15 tracey goto done;
604 f71c0a3a 2020-06-23 tracey gw_conf->got_site_name = strdup(D_SITENAME);
605 f71c0a3a 2020-06-23 tracey if (gw_conf->got_site_name == NULL) {
606 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("strdup");
607 c34ec417 2020-06-22 tracey goto done;
609 f71c0a3a 2020-06-23 tracey gw_conf->got_site_owner = strdup(D_SITEOWNER);
610 f71c0a3a 2020-06-23 tracey if (gw_conf->got_site_owner == NULL) {
611 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("strdup");
612 c34ec417 2020-06-22 tracey goto done;
614 f71c0a3a 2020-06-23 tracey gw_conf->got_site_link = strdup(D_SITELINK);
615 f71c0a3a 2020-06-23 tracey if (gw_conf->got_site_link == NULL) {
616 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("strdup");
617 c34ec417 2020-06-22 tracey goto done;
619 f71c0a3a 2020-06-23 tracey gw_conf->got_logo = strdup(D_GOTLOGO);
620 f71c0a3a 2020-06-23 tracey if (gw_conf->got_logo == NULL) {
621 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("strdup");
622 c34ec417 2020-06-22 tracey goto done;
624 f71c0a3a 2020-06-23 tracey gw_conf->got_logo_url = strdup(D_GOTURL);
625 f71c0a3a 2020-06-23 tracey if (gw_conf->got_logo_url == NULL) {
626 c34ec417 2020-06-22 tracey gerror = got_error_from_errno("strdup");
627 c34ec417 2020-06-22 tracey goto done;
629 f71c0a3a 2020-06-23 tracey gw_conf->got_show_site_owner = D_SHOWSOWNER;
630 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_owner = D_SHOWROWNER;
631 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_age = D_SHOWAGE;
632 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_description = D_SHOWDESC;
633 f71c0a3a 2020-06-23 tracey gw_conf->got_show_repo_cloneurl = D_SHOWURL;
634 f71c0a3a 2020-06-23 tracey gw_conf->got_max_repos = D_MAXREPO;
635 f71c0a3a 2020-06-23 tracey gw_conf->got_max_repos_display = D_MAXREPODISP;
636 f71c0a3a 2020-06-23 tracey gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
639 c34ec417 2020-06-22 tracey * We don't require that the gotweb config file exists
640 c34ec417 2020-06-22 tracey * So reset gerror if it doesn't exist and goto done.
642 c34ec417 2020-06-22 tracey gerror = pushfile(&file, filename);
643 c34ec417 2020-06-22 tracey if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
644 c34ec417 2020-06-22 tracey gerror = NULL;
645 c34ec417 2020-06-22 tracey goto done;
646 c34ec417 2020-06-22 tracey } else if (gerror)
647 c34ec417 2020-06-22 tracey return gerror;
648 2c251c14 2020-01-15 tracey topfile = file;
650 2c251c14 2020-01-15 tracey yyparse();
651 2c251c14 2020-01-15 tracey popfile();
653 f71c0a3a 2020-06-23 tracey *gconf = gw_conf;
654 c34ec417 2020-06-22 tracey return gerror;
658 c34ec417 2020-06-22 tracey symset(const char *nam, const char *val, int persist)
660 c34ec417 2020-06-22 tracey struct sym *sym;
662 c34ec417 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
663 c34ec417 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0)
667 c34ec417 2020-06-22 tracey if (sym != NULL) {
668 c34ec417 2020-06-22 tracey if (sym->persist == 1)
669 c34ec417 2020-06-22 tracey return (0);
671 c34ec417 2020-06-22 tracey free(sym->nam);
672 c34ec417 2020-06-22 tracey free(sym->val);
673 c34ec417 2020-06-22 tracey TAILQ_REMOVE(&symhead, sym, entry);
674 c34ec417 2020-06-22 tracey free(sym);
677 c34ec417 2020-06-22 tracey if ((sym = calloc(1, sizeof(*sym))) == NULL)
678 c34ec417 2020-06-22 tracey return (-1);
680 c34ec417 2020-06-22 tracey sym->nam = strdup(nam);
681 c34ec417 2020-06-22 tracey if (sym->nam == NULL) {
682 c34ec417 2020-06-22 tracey free(sym);
683 c34ec417 2020-06-22 tracey return (-1);
685 c34ec417 2020-06-22 tracey sym->val = strdup(val);
686 c34ec417 2020-06-22 tracey if (sym->val == NULL) {
687 c34ec417 2020-06-22 tracey free(sym->nam);
688 c34ec417 2020-06-22 tracey free(sym);
689 c34ec417 2020-06-22 tracey return (-1);
691 c34ec417 2020-06-22 tracey sym->used = 0;
692 c34ec417 2020-06-22 tracey sym->persist = persist;
693 c34ec417 2020-06-22 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
694 c34ec417 2020-06-22 tracey return (0);
698 c34ec417 2020-06-22 tracey cmdline_symset(char *s)
700 c34ec417 2020-06-22 tracey char *sym, *val;
702 c34ec417 2020-06-22 tracey size_t len;
704 c34ec417 2020-06-22 tracey if ((val = strrchr(s, '=')) == NULL)
705 c34ec417 2020-06-22 tracey return (-1);
707 c34ec417 2020-06-22 tracey len = strlen(s) - strlen(val) + 1;
708 c34ec417 2020-06-22 tracey if ((sym = malloc(len)) == NULL)
709 c34ec417 2020-06-22 tracey errx(1, "cmdline_symset: malloc");
711 c34ec417 2020-06-22 tracey strlcpy(sym, s, len);
713 c34ec417 2020-06-22 tracey ret = symset(sym, val + 1, 1);
714 c34ec417 2020-06-22 tracey free(sym);
716 c34ec417 2020-06-22 tracey return (ret);
720 c34ec417 2020-06-22 tracey symget(const char *nam)
722 c34ec417 2020-06-22 tracey struct sym *sym;
724 c34ec417 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
725 c34ec417 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0) {
726 c34ec417 2020-06-22 tracey sym->used = 1;
727 c34ec417 2020-06-22 tracey return (sym->val);
730 c34ec417 2020-06-22 tracey return (NULL);