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>
35 #include "got_error.h"
38 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
40 TAILQ_ENTRY(file) entry;
49 static const struct got_error* pushfile(struct file**, const char *);
53 int yyerror(const char *, ...)
54 __attribute__((__format__ (printf, 1, 2)))
55 __attribute__((__nonnull__ (1)));
56 int kw_cmp(const void *, const void *);
63 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
65 TAILQ_ENTRY(sym) entry;
72 int symset(const char *, const char *, int);
73 char *symget(const char *);
75 const struct got_error* gerror = NULL;
76 struct gotweb_config *gw_conf;
88 %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
89 %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
90 %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
91 %token GOT_MAX_COMMITS_DISPLAY ERROR GOT_SHOW_SITE_OWNER
92 %token GOT_SHOW_REPO_CLONEURL
93 %token <v.string> STRING
94 %token <v.number> NUMBER
95 %type <v.number> boolean
104 if (strcasecmp($1, "true") == 0 ||
105 strcasecmp($1, "on") == 0 ||
106 strcasecmp($1, "yes") == 0)
108 else if (strcasecmp($1, "false") == 0 ||
109 strcasecmp($1, "off") == 0 ||
110 strcasecmp($1, "no") == 0)
113 yyerror("invalid boolean value '%s'", $1);
120 main : GOT_REPOS_PATH STRING {
121 gw_conf->got_repos_path = strdup($2);
122 if (gw_conf->got_repos_path == NULL) {
129 | GOT_WWW_PATH STRING {
130 gw_conf->got_www_path = strdup($2);
131 if (gw_conf->got_www_path == NULL) {
138 | GOT_MAX_REPOS NUMBER {
140 gw_conf->got_max_repos = $2;
142 | GOT_SITE_NAME STRING {
143 gw_conf->got_site_name = strdup($2);
144 if (gw_conf->got_site_name == NULL) {
151 | GOT_SITE_OWNER STRING {
152 gw_conf->got_site_owner = strdup($2);
153 if (gw_conf->got_site_owner == NULL) {
160 | GOT_SITE_LINK STRING {
161 gw_conf->got_site_link = strdup($2);
162 if (gw_conf->got_site_link == NULL) {
170 gw_conf->got_logo = strdup($2);
171 if (gw_conf->got_logo== NULL) {
178 | GOT_LOGO_URL STRING {
179 gw_conf->got_logo_url = strdup($2);
180 if (gw_conf->got_logo_url== NULL) {
187 | GOT_SHOW_SITE_OWNER boolean {
188 gw_conf->got_show_site_owner = $2;
190 | GOT_SHOW_REPO_OWNER boolean {
191 gw_conf->got_show_repo_owner = $2;
193 | GOT_SHOW_REPO_AGE boolean {
194 gw_conf->got_show_repo_age = $2;
196 | GOT_SHOW_REPO_DESCRIPTION boolean {
197 gw_conf->got_show_repo_description = $2;
199 | GOT_SHOW_REPO_CLONEURL boolean {
200 gw_conf->got_show_repo_cloneurl = $2;
202 | GOT_MAX_REPOS_DISPLAY NUMBER {
204 gw_conf->got_max_repos_display = $2;
206 | GOT_MAX_COMMITS_DISPLAY NUMBER {
208 gw_conf->got_max_commits_display = $2;
219 yyerror(const char *fmt, ...)
226 if (vasprintf(&msg, fmt, ap) == -1) {
227 gerror = got_error_from_errno("vasprintf");
231 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
232 gerror = got_error_from_errno("asprintf");
235 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
242 kw_cmp(const void *k, const void *e)
244 return (strcmp(k, ((const struct keywords *)e)->k_name));
250 /* This has to be sorted always. */
251 static const struct keywords keywords[] = {
252 { "got_logo", GOT_LOGO },
253 { "got_logo_url", GOT_LOGO_URL },
254 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
255 { "got_max_repos", GOT_MAX_REPOS },
256 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
257 { "got_repos_path", GOT_REPOS_PATH },
258 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
259 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
260 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
261 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
262 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
263 { "got_site_link", GOT_SITE_LINK },
264 { "got_site_name", GOT_SITE_NAME },
265 { "got_site_owner", GOT_SITE_OWNER },
266 { "got_www_path", GOT_WWW_PATH },
268 const struct keywords *p;
270 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
271 sizeof(keywords[0]), kw_cmp);
279 #define START_EXPAND 1
280 #define DONE_EXPAND 2
282 static int expanding;
290 if (file->ungetpos > 0)
291 c = file->ungetbuf[--file->ungetpos];
293 c = getc(file->stream);
295 if (c == START_EXPAND)
297 else if (c == DONE_EXPAND)
311 if ((c = igetc()) == EOF) {
312 yyerror("reached end of file while parsing "
314 if (file == topfile || popfile() == EOF)
321 while ((c = igetc()) == '\\') {
327 yylval.lineno = file->lineno;
333 * Fake EOL when hit EOF for the first time. This gets line
334 * count right if last line in included file is syntactically
335 * invalid and has no newline.
337 if (file->eof_reached == 0) {
338 file->eof_reached = 1;
342 if (file == topfile || popfile() == EOF)
356 if (file->ungetpos >= file->ungetsize) {
357 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
359 err(1, "%s", __func__);
361 file->ungetsize *= 2;
363 file->ungetbuf[file->ungetpos++] = c;
371 /* Skip to either EOF or the first real EOL. */
387 unsigned char buf[8096];
388 unsigned char *p, *val;
394 while ((c = lgetc(0)) == ' ' || c == '\t')
397 yylval.lineno = file->lineno;
399 while ((c = lgetc(0)) != '\n' && c != EOF)
401 if (c == '$' && !expanding) {
403 if ((c = lgetc(0)) == EOF)
406 if (p + 1 >= buf + sizeof(buf) - 1) {
407 yyerror("string too long");
410 if (isalnum(c) || c == '_') {
420 yyerror("macro '%s' not defined", buf);
423 p = val + strlen(val) - 1;
424 lungetc(DONE_EXPAND);
429 lungetc(START_EXPAND);
438 if ((c = lgetc(quotec)) == EOF)
443 } else if (c == '\\') {
444 if ((next = lgetc(quotec)) == EOF)
446 if (next == quotec || c == ' ' || c == '\t')
448 else if (next == '\n') {
453 } else if (c == quotec) {
456 } else if (c == '\0') {
457 yyerror("syntax error");
460 if (p + 1 >= buf + sizeof(buf) - 1) {
461 yyerror("string too long");
466 yylval.v.string = strdup(buf);
467 if (yylval.v.string == NULL)
468 err(1, "%s", __func__);
472 #define allowed_to_end_number(x) \
473 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
475 if (c == '-' || isdigit(c)) {
478 if ((unsigned)(p-buf) >= sizeof(buf)) {
479 yyerror("string too long");
482 } while ((c = lgetc(0)) != EOF && isdigit(c));
484 if (p == buf + 1 && buf[0] == '-')
486 if (c == EOF || allowed_to_end_number(c)) {
487 const char *errstr = NULL;
490 yylval.v.number = strtonum(buf, LLONG_MIN,
493 yyerror("\"%s\" invalid number: %s",
508 #define allowed_in_string(x) \
509 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
510 x != '{' && x != '}' && \
511 x != '!' && x != '=' && x != '#' && \
514 if (isalnum(c) || c == ':' || c == '_') {
517 if ((unsigned)(p-buf) >= sizeof(buf)) {
518 yyerror("string too long");
521 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
524 if ((token = lookup(buf)) == STRING)
525 if ((yylval.v.string = strdup(buf)) == NULL)
526 err(1, "%s", __func__);
530 yylval.lineno = file->lineno;
538 static const struct got_error*
539 pushfile(struct file **nfile, const char *name)
541 const struct got_error* error = NULL;
543 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
544 return got_error_from_errno2(__func__, "calloc");
545 if (((*nfile)->name = strdup(name)) == NULL) {
547 return got_error_from_errno2(__func__, "strdup");
549 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
551 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
552 return got_error_from_errno("asprintf");
553 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
554 free((*nfile)->name);
559 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
560 (*nfile)->ungetsize = 16;
561 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
562 if ((*nfile)->ungetbuf == NULL) {
563 fclose((*nfile)->stream);
564 free((*nfile)->name);
566 return got_error_from_errno2(__func__, "malloc");
568 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
575 struct file *prev = NULL;
577 TAILQ_REMOVE(&files, file, entry);
578 fclose(file->stream);
580 free(file->ungetbuf);
583 return (file ? 0 : EOF);
586 const struct got_error*
587 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
589 gw_conf = malloc(sizeof(struct gotweb_config));
590 if (gw_conf == NULL) {
591 gerror = got_error_from_errno("malloc");
594 gw_conf->got_repos_path = strdup(D_GOTPATH);
595 if (gw_conf->got_repos_path == NULL) {
596 gerror = got_error_from_errno("strdup");
599 gw_conf->got_www_path = strdup(D_GOTWWW);
600 if (gw_conf->got_www_path == NULL) {
601 gerror = got_error_from_errno("strdup");
604 gw_conf->got_site_name = strdup(D_SITENAME);
605 if (gw_conf->got_site_name == NULL) {
606 gerror = got_error_from_errno("strdup");
609 gw_conf->got_site_owner = strdup(D_SITEOWNER);
610 if (gw_conf->got_site_owner == NULL) {
611 gerror = got_error_from_errno("strdup");
614 gw_conf->got_site_link = strdup(D_SITELINK);
615 if (gw_conf->got_site_link == NULL) {
616 gerror = got_error_from_errno("strdup");
619 gw_conf->got_logo = strdup(D_GOTLOGO);
620 if (gw_conf->got_logo == NULL) {
621 gerror = got_error_from_errno("strdup");
624 gw_conf->got_logo_url = strdup(D_GOTURL);
625 if (gw_conf->got_logo_url == NULL) {
626 gerror = got_error_from_errno("strdup");
629 gw_conf->got_show_site_owner = D_SHOWSOWNER;
630 gw_conf->got_show_repo_owner = D_SHOWROWNER;
631 gw_conf->got_show_repo_age = D_SHOWAGE;
632 gw_conf->got_show_repo_description = D_SHOWDESC;
633 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
634 gw_conf->got_max_repos = D_MAXREPO;
635 gw_conf->got_max_repos_display = D_MAXREPODISP;
636 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
639 * We don't require that the gotweb config file exists
640 * So reset gerror if it doesn't exist and goto done.
642 gerror = pushfile(&file, filename);
643 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
658 symset(const char *nam, const char *val, int persist)
662 TAILQ_FOREACH(sym, &symhead, entry) {
663 if (strcmp(nam, sym->nam) == 0)
668 if (sym->persist == 1)
673 TAILQ_REMOVE(&symhead, sym, entry);
677 if ((sym = calloc(1, sizeof(*sym))) == NULL)
680 sym->nam = strdup(nam);
681 if (sym->nam == NULL) {
685 sym->val = strdup(val);
686 if (sym->val == NULL) {
692 sym->persist = persist;
693 TAILQ_INSERT_TAIL(&symhead, sym, entry);
698 cmdline_symset(char *s)
704 if ((val = strrchr(s, '=')) == NULL)
707 len = strlen(s) - strlen(val) + 1;
708 if ((sym = malloc(len)) == NULL)
709 errx(1, "cmdline_symset: malloc");
711 strlcpy(sym, s, len);
713 ret = symset(sym, val + 1, 1);
720 symget(const char *nam)
724 TAILQ_FOREACH(sym, &symhead, entry) {
725 if (strcmp(nam, sym->nam) == 0) {