2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@openbsd.org>
3 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
31 #include <arpa/inet.h>
46 #include "got_error.h"
49 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
51 TAILQ_ENTRY(file) entry;
60 static const struct got_error* pushfile(struct file**, const char *);
64 int yyerror(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 TAILQ_ENTRY(sym) entry;
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
86 const struct got_error* gerror = NULL;
87 struct gotweb_config *gw_conf;
99 %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
100 %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
101 %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
102 %token GOT_MAX_COMMITS_DISPLAY ON ERROR GOT_SHOW_SITE_OWNER
103 %token GOT_SHOW_REPO_CLONEURL
104 %token <v.string> STRING
105 %token <v.number> NUMBER
106 %type <v.number> boolean
109 grammar : /* empty */
115 if (strcasecmp($1, "true") == 0 ||
116 strcasecmp($1, "yes") == 0)
118 else if (strcasecmp($1, "false") == 0 ||
119 strcasecmp($1, "off") == 0 ||
120 strcasecmp($1, "no") == 0)
123 yyerror("invalid boolean value '%s'", $1);
131 main : GOT_REPOS_PATH STRING {
132 gw_conf->got_repos_path = strdup($2);
133 if (gw_conf->got_repos_path== NULL) {
140 | GOT_MAX_REPOS NUMBER {
142 gw_conf->got_max_repos = $2;
144 | GOT_SITE_NAME STRING {
145 gw_conf->got_site_name = strdup($2);
146 if (gw_conf->got_site_name == NULL) {
153 | GOT_SITE_OWNER STRING {
154 gw_conf->got_site_owner = strdup($2);
155 if (gw_conf->got_site_owner == NULL) {
162 | GOT_SITE_LINK STRING {
163 gw_conf->got_site_link = strdup($2);
164 if (gw_conf->got_site_link == NULL) {
172 gw_conf->got_logo = strdup($2);
173 if (gw_conf->got_logo== NULL) {
180 | GOT_LOGO_URL STRING {
181 gw_conf->got_logo_url = strdup($2);
182 if (gw_conf->got_logo_url== NULL) {
189 | GOT_SHOW_SITE_OWNER boolean {
190 gw_conf->got_show_site_owner = $2;
192 | GOT_SHOW_REPO_OWNER boolean {
193 gw_conf->got_show_repo_owner = $2;
195 | GOT_SHOW_REPO_AGE boolean {
196 gw_conf->got_show_repo_age = $2;
198 | GOT_SHOW_REPO_DESCRIPTION boolean {
199 gw_conf->got_show_repo_description = $2;
201 | GOT_SHOW_REPO_CLONEURL boolean {
202 gw_conf->got_show_repo_cloneurl = $2;
204 | GOT_MAX_REPOS_DISPLAY NUMBER {
206 gw_conf->got_max_repos_display = $2;
208 | GOT_MAX_COMMITS_DISPLAY NUMBER {
210 gw_conf->got_max_commits_display = $2;
221 yyerror(const char *fmt, ...)
228 if (vasprintf(&msg, fmt, ap) == -1) {
229 gerror = got_error_from_errno("vasprintf");
233 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
234 gerror = got_error_from_errno("asprintf");
237 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
244 kw_cmp(const void *k, const void *e)
246 return (strcmp(k, ((const struct keywords *)e)->k_name));
252 /* This has to be sorted always. */
253 static const struct keywords keywords[] = {
254 { "got_logo", GOT_LOGO },
255 { "got_logo_url", GOT_LOGO_URL },
256 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
257 { "got_max_repos", GOT_MAX_REPOS },
258 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
259 { "got_repos_path", GOT_REPOS_PATH },
260 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
261 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
262 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
263 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
264 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
265 { "got_site_link", GOT_SITE_LINK },
266 { "got_site_name", GOT_SITE_NAME },
267 { "got_site_owner", GOT_SITE_OWNER },
269 const struct keywords *p;
271 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
272 sizeof(keywords[0]), kw_cmp);
280 #define START_EXPAND 1
281 #define DONE_EXPAND 2
283 static int expanding;
291 if (file->ungetpos > 0)
292 c = file->ungetbuf[--file->ungetpos];
294 c = getc(file->stream);
296 if (c == START_EXPAND)
298 else if (c == DONE_EXPAND)
312 if ((c = igetc()) == EOF) {
313 yyerror("reached end of file while parsing "
315 if (file == topfile || popfile() == EOF)
322 while ((c = igetc()) == '\\') {
328 yylval.lineno = file->lineno;
334 * Fake EOL when hit EOF for the first time. This gets line
335 * count right if last line in included file is syntactically
336 * invalid and has no newline.
338 if (file->eof_reached == 0) {
339 file->eof_reached = 1;
343 if (file == topfile || popfile() == EOF)
357 if (file->ungetpos >= file->ungetsize) {
358 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
360 err(1, "%s", __func__);
362 file->ungetsize *= 2;
364 file->ungetbuf[file->ungetpos++] = c;
372 /* Skip to either EOF or the first real EOL. */
388 unsigned char buf[8096];
389 unsigned char *p, *val;
395 while ((c = lgetc(0)) == ' ' || c == '\t')
398 yylval.lineno = file->lineno;
400 while ((c = lgetc(0)) != '\n' && c != EOF)
402 if (c == '$' && !expanding) {
404 if ((c = lgetc(0)) == EOF)
407 if (p + 1 >= buf + sizeof(buf) - 1) {
408 yyerror("string too long");
411 if (isalnum(c) || c == '_') {
421 yyerror("macro '%s' not defined", buf);
424 p = val + strlen(val) - 1;
425 lungetc(DONE_EXPAND);
430 lungetc(START_EXPAND);
439 if ((c = lgetc(quotec)) == EOF)
444 } else if (c == '\\') {
445 if ((next = lgetc(quotec)) == EOF)
447 if (next == quotec || c == ' ' || c == '\t')
449 else if (next == '\n') {
454 } else if (c == quotec) {
457 } else if (c == '\0') {
458 yyerror("syntax error");
461 if (p + 1 >= buf + sizeof(buf) - 1) {
462 yyerror("string too long");
467 yylval.v.string = strdup(buf);
468 if (yylval.v.string == NULL)
469 err(1, "%s", __func__);
473 #define allowed_to_end_number(x) \
474 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
476 if (c == '-' || isdigit(c)) {
479 if ((unsigned)(p-buf) >= sizeof(buf)) {
480 yyerror("string too long");
483 } while ((c = lgetc(0)) != EOF && isdigit(c));
485 if (p == buf + 1 && buf[0] == '-')
487 if (c == EOF || allowed_to_end_number(c)) {
488 const char *errstr = NULL;
491 yylval.v.number = strtonum(buf, LLONG_MIN,
494 yyerror("\"%s\" invalid number: %s",
509 #define allowed_in_string(x) \
510 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
511 x != '{' && x != '}' && \
512 x != '!' && x != '=' && x != '#' && \
515 if (isalnum(c) || c == ':' || c == '_') {
518 if ((unsigned)(p-buf) >= sizeof(buf)) {
519 yyerror("string too long");
522 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
525 if ((token = lookup(buf)) == STRING)
526 if ((yylval.v.string = strdup(buf)) == NULL)
527 err(1, "%s", __func__);
531 yylval.lineno = file->lineno;
539 static const struct got_error*
540 pushfile(struct file **nfile, const char *name)
542 const struct got_error* error = NULL;
544 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
545 return got_error_from_errno2(__func__, "calloc");
546 if (((*nfile)->name = strdup(name)) == NULL) {
548 return got_error_from_errno2(__func__, "strdup");
550 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
552 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
553 return got_error_from_errno("asprintf");
554 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
555 free((*nfile)->name);
560 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
561 (*nfile)->ungetsize = 16;
562 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
563 if ((*nfile)->ungetbuf == NULL) {
564 fclose((*nfile)->stream);
565 free((*nfile)->name);
567 return got_error_from_errno2(__func__, "malloc");
569 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
576 struct file *prev = NULL;
578 TAILQ_REMOVE(&files, file, entry);
579 fclose(file->stream);
581 free(file->ungetbuf);
584 return (file ? 0 : EOF);
587 const struct got_error*
588 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
590 gw_conf = malloc(sizeof(struct gotweb_config));
591 if (gw_conf == NULL) {
592 gerror = got_error_from_errno("malloc");
595 gw_conf->got_repos_path = strdup(D_GOTPATH);
596 if (gw_conf->got_repos_path == NULL) {
597 gerror = got_error_from_errno("strdup");
600 gw_conf->got_site_name = strdup(D_SITENAME);
601 if (gw_conf->got_site_name == NULL) {
602 gerror = got_error_from_errno("strdup");
605 gw_conf->got_site_owner = strdup(D_SITEOWNER);
606 if (gw_conf->got_site_owner == NULL) {
607 gerror = got_error_from_errno("strdup");
610 gw_conf->got_site_link = strdup(D_SITELINK);
611 if (gw_conf->got_site_link == NULL) {
612 gerror = got_error_from_errno("strdup");
615 gw_conf->got_logo = strdup(D_GOTLOGO);
616 if (gw_conf->got_logo == NULL) {
617 gerror = got_error_from_errno("strdup");
620 gw_conf->got_logo_url = strdup(D_GOTURL);
621 if (gw_conf->got_logo_url == NULL) {
622 gerror = got_error_from_errno("strdup");
625 gw_conf->got_show_site_owner = D_SHOWSOWNER;
626 gw_conf->got_show_repo_owner = D_SHOWROWNER;
627 gw_conf->got_show_repo_age = D_SHOWAGE;
628 gw_conf->got_show_repo_description = D_SHOWDESC;
629 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
630 gw_conf->got_max_repos = D_MAXREPO;
631 gw_conf->got_max_repos_display = D_MAXREPODISP;
632 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
635 * We don't require that the gotweb config file exists
636 * So reset gerror if it doesn't exist and goto done.
638 gerror = pushfile(&file, filename);
639 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
654 symset(const char *nam, const char *val, int persist)
658 TAILQ_FOREACH(sym, &symhead, entry) {
659 if (strcmp(nam, sym->nam) == 0)
664 if (sym->persist == 1)
669 TAILQ_REMOVE(&symhead, sym, entry);
673 if ((sym = calloc(1, sizeof(*sym))) == NULL)
676 sym->nam = strdup(nam);
677 if (sym->nam == NULL) {
681 sym->val = strdup(val);
682 if (sym->val == NULL) {
688 sym->persist = persist;
689 TAILQ_INSERT_TAIL(&symhead, sym, entry);
694 cmdline_symset(char *s)
700 if ((val = strrchr(s, '=')) == NULL)
703 len = strlen(s) - strlen(val) + 1;
704 if ((sym = malloc(len)) == NULL)
705 errx(1, "cmdline_symset: malloc");
707 strlcpy(sym, s, len);
709 ret = symset(sym, val + 1, 1);
716 symget(const char *nam)
720 TAILQ_FOREACH(sym, &symhead, entry) {
721 if (strcmp(nam, sym->nam) == 0) {