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 int cmdline_symset(char *);
74 char *symget(const char *);
76 const struct got_error* gerror = NULL;
77 struct gotweb_config *gw_conf;
89 %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
90 %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
91 %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
92 %token GOT_MAX_COMMITS_DISPLAY ERROR GOT_SHOW_SITE_OWNER
93 %token GOT_SHOW_REPO_CLONEURL
94 %token <v.string> STRING
95 %token <v.number> NUMBER
96 %type <v.number> boolean
105 if (strcasecmp($1, "true") == 0 ||
106 strcasecmp($1, "on") == 0 ||
107 strcasecmp($1, "yes") == 0)
109 else if (strcasecmp($1, "false") == 0 ||
110 strcasecmp($1, "off") == 0 ||
111 strcasecmp($1, "no") == 0)
114 yyerror("invalid boolean value '%s'", $1);
121 main : GOT_REPOS_PATH STRING {
122 gw_conf->got_repos_path = $2;
124 | GOT_WWW_PATH STRING {
125 gw_conf->got_www_path = $2;
127 | GOT_MAX_REPOS NUMBER {
129 gw_conf->got_max_repos = $2;
131 | GOT_SITE_NAME STRING {
132 gw_conf->got_site_name = $2;
134 | GOT_SITE_OWNER STRING {
135 gw_conf->got_site_owner = $2;
137 | GOT_SITE_LINK STRING {
138 gw_conf->got_site_link = $2;
141 gw_conf->got_logo = $2;
143 | GOT_LOGO_URL STRING {
144 gw_conf->got_logo_url = $2;
146 | GOT_SHOW_SITE_OWNER boolean {
147 gw_conf->got_show_site_owner = $2;
149 | GOT_SHOW_REPO_OWNER boolean {
150 gw_conf->got_show_repo_owner = $2;
152 | GOT_SHOW_REPO_AGE boolean {
153 gw_conf->got_show_repo_age = $2;
155 | GOT_SHOW_REPO_DESCRIPTION boolean {
156 gw_conf->got_show_repo_description = $2;
158 | GOT_SHOW_REPO_CLONEURL boolean {
159 gw_conf->got_show_repo_cloneurl = $2;
161 | GOT_MAX_REPOS_DISPLAY NUMBER {
163 gw_conf->got_max_repos_display = $2;
165 | GOT_MAX_COMMITS_DISPLAY NUMBER {
167 gw_conf->got_max_commits_display = $2;
178 yyerror(const char *fmt, ...)
185 if (vasprintf(&msg, fmt, ap) == -1) {
186 gerror = got_error_from_errno("vasprintf");
190 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
191 gerror = got_error_from_errno("asprintf");
194 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
200 kw_cmp(const void *k, const void *e)
202 return (strcmp(k, ((const struct keywords *)e)->k_name));
208 /* This has to be sorted always. */
209 static const struct keywords keywords[] = {
210 { "got_logo", GOT_LOGO },
211 { "got_logo_url", GOT_LOGO_URL },
212 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
213 { "got_max_repos", GOT_MAX_REPOS },
214 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
215 { "got_repos_path", GOT_REPOS_PATH },
216 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
217 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
218 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
219 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
220 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
221 { "got_site_link", GOT_SITE_LINK },
222 { "got_site_name", GOT_SITE_NAME },
223 { "got_site_owner", GOT_SITE_OWNER },
224 { "got_www_path", GOT_WWW_PATH },
226 const struct keywords *p;
228 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
229 sizeof(keywords[0]), kw_cmp);
237 #define START_EXPAND 1
238 #define DONE_EXPAND 2
240 static int expanding;
248 if (file->ungetpos > 0)
249 c = file->ungetbuf[--file->ungetpos];
251 c = getc(file->stream);
253 if (c == START_EXPAND)
255 else if (c == DONE_EXPAND)
269 if ((c = igetc()) == EOF) {
270 yyerror("reached end of file while parsing "
272 if (file == topfile || popfile() == EOF)
279 while ((c = igetc()) == '\\') {
285 yylval.lineno = file->lineno;
291 * Fake EOL when hit EOF for the first time. This gets line
292 * count right if last line in included file is syntactically
293 * invalid and has no newline.
295 if (file->eof_reached == 0) {
296 file->eof_reached = 1;
300 if (file == topfile || popfile() == EOF)
314 if (file->ungetpos >= file->ungetsize) {
315 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
317 err(1, "%s", __func__);
319 file->ungetsize *= 2;
321 file->ungetbuf[file->ungetpos++] = c;
329 /* Skip to either EOF or the first real EOL. */
352 while ((c = lgetc(0)) == ' ' || c == '\t')
355 yylval.lineno = file->lineno;
357 while ((c = lgetc(0)) != '\n' && c != EOF)
359 if (c == '$' && !expanding) {
361 if ((c = lgetc(0)) == EOF)
364 if (p + 1 >= buf + sizeof(buf) - 1) {
365 yyerror("string too long");
368 if (isalnum(c) || c == '_') {
378 yyerror("macro '%s' not defined", buf);
381 p = val + strlen(val) - 1;
382 lungetc(DONE_EXPAND);
384 lungetc((unsigned char)*p);
387 lungetc(START_EXPAND);
396 if ((c = lgetc(quotec)) == EOF)
401 } else if (c == '\\') {
402 if ((next = lgetc(quotec)) == EOF)
404 if (next == quotec || c == ' ' || c == '\t')
406 else if (next == '\n') {
411 } else if (c == quotec) {
414 } else if (c == '\0') {
415 yyerror("syntax error");
418 if (p + 1 >= buf + sizeof(buf) - 1) {
419 yyerror("string too long");
424 yylval.v.string = strdup(buf);
425 if (yylval.v.string == NULL)
426 err(1, "%s", __func__);
430 #define allowed_to_end_number(x) \
431 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
433 if (c == '-' || isdigit(c)) {
436 if ((size_t)(p-buf) >= sizeof(buf)) {
437 yyerror("string too long");
440 } while ((c = lgetc(0)) != EOF && isdigit(c));
442 if (p == buf + 1 && buf[0] == '-')
444 if (c == EOF || allowed_to_end_number(c)) {
445 const char *errstr = NULL;
448 yylval.v.number = strtonum(buf, LLONG_MIN,
451 yyerror("\"%s\" invalid number: %s",
459 lungetc((unsigned char)*--p);
460 c = (unsigned char)*--p;
466 #define allowed_in_string(x) \
467 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
468 x != '{' && x != '}' && \
469 x != '!' && x != '=' && x != '#' && \
472 if (isalnum(c) || c == ':' || c == '_') {
475 if ((size_t)(p-buf) >= sizeof(buf)) {
476 yyerror("string too long");
479 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
482 if ((token = lookup(buf)) == STRING)
483 if ((yylval.v.string = strdup(buf)) == NULL)
484 err(1, "%s", __func__);
488 yylval.lineno = file->lineno;
496 static const struct got_error*
497 pushfile(struct file **nfile, const char *name)
499 const struct got_error* error = NULL;
501 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
502 return got_error_from_errno2(__func__, "calloc");
503 if (((*nfile)->name = strdup(name)) == NULL) {
505 return got_error_from_errno2(__func__, "strdup");
507 if (((*nfile)->stream = fopen((*nfile)->name, "re")) == NULL) {
509 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
510 return got_error_from_errno("asprintf");
511 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
512 free((*nfile)->name);
517 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
518 (*nfile)->ungetsize = 16;
519 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
520 if ((*nfile)->ungetbuf == NULL) {
521 fclose((*nfile)->stream);
522 free((*nfile)->name);
524 return got_error_from_errno2(__func__, "malloc");
526 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
533 struct file *prev = NULL;
535 TAILQ_REMOVE(&files, file, entry);
536 fclose(file->stream);
538 free(file->ungetbuf);
541 return (file ? 0 : EOF);
544 const struct got_error*
545 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
547 gw_conf = malloc(sizeof(struct gotweb_config));
548 if (gw_conf == NULL) {
549 gerror = got_error_from_errno("malloc");
552 gw_conf->got_repos_path = strdup(D_GOTPATH);
553 if (gw_conf->got_repos_path == NULL) {
554 gerror = got_error_from_errno("strdup");
557 gw_conf->got_www_path = strdup(D_GOTWWW);
558 if (gw_conf->got_www_path == NULL) {
559 gerror = got_error_from_errno("strdup");
562 gw_conf->got_site_name = strdup(D_SITENAME);
563 if (gw_conf->got_site_name == NULL) {
564 gerror = got_error_from_errno("strdup");
567 gw_conf->got_site_owner = strdup(D_SITEOWNER);
568 if (gw_conf->got_site_owner == NULL) {
569 gerror = got_error_from_errno("strdup");
572 gw_conf->got_site_link = strdup(D_SITELINK);
573 if (gw_conf->got_site_link == NULL) {
574 gerror = got_error_from_errno("strdup");
577 gw_conf->got_logo = strdup(D_GOTLOGO);
578 if (gw_conf->got_logo == NULL) {
579 gerror = got_error_from_errno("strdup");
582 gw_conf->got_logo_url = strdup(D_GOTURL);
583 if (gw_conf->got_logo_url == NULL) {
584 gerror = got_error_from_errno("strdup");
587 gw_conf->got_show_site_owner = D_SHOWSOWNER;
588 gw_conf->got_show_repo_owner = D_SHOWROWNER;
589 gw_conf->got_show_repo_age = D_SHOWAGE;
590 gw_conf->got_show_repo_description = D_SHOWDESC;
591 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
592 gw_conf->got_max_repos = D_MAXREPO;
593 gw_conf->got_max_repos_display = D_MAXREPODISP;
594 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
597 * We don't require that the gotweb config file exists
598 * So reset gerror if it doesn't exist and goto done.
600 gerror = pushfile(&file, filename);
601 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
616 symset(const char *nam, const char *val, int persist)
620 TAILQ_FOREACH(sym, &symhead, entry) {
621 if (strcmp(nam, sym->nam) == 0)
626 if (sym->persist == 1)
631 TAILQ_REMOVE(&symhead, sym, entry);
635 if ((sym = calloc(1, sizeof(*sym))) == NULL)
638 sym->nam = strdup(nam);
639 if (sym->nam == NULL) {
643 sym->val = strdup(val);
644 if (sym->val == NULL) {
650 sym->persist = persist;
651 TAILQ_INSERT_TAIL(&symhead, sym, entry);
656 cmdline_symset(char *s)
662 if ((val = strrchr(s, '=')) == NULL)
665 len = strlen(s) - strlen(val) + 1;
666 if ((sym = malloc(len)) == NULL)
667 errx(1, "cmdline_symset: malloc");
669 strlcpy(sym, s, len);
671 ret = symset(sym, val + 1, 1);
678 symget(const char *nam)
682 TAILQ_FOREACH(sym, &symhead, entry) {
683 if (strcmp(nam, sym->nam) == 0) {