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) {
139 | GOT_MAX_REPOS NUMBER {
141 gw_conf->got_max_repos = $2;
143 | GOT_SITE_NAME STRING {
144 gw_conf->got_site_name = strdup($2);
145 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) {
159 | GOT_SITE_LINK STRING {
160 gw_conf->got_site_link = strdup($2);
161 if (gw_conf->got_site_link == NULL) {
168 gw_conf->got_logo = strdup($2);
169 if (gw_conf->got_logo== NULL) {
175 | GOT_LOGO_URL STRING {
176 gw_conf->got_logo_url = strdup($2);
177 if (gw_conf->got_logo_url== NULL) {
183 | GOT_SHOW_SITE_OWNER boolean {
184 gw_conf->got_show_site_owner = $2;
186 | GOT_SHOW_REPO_OWNER boolean {
187 gw_conf->got_show_repo_owner = $2;
189 | GOT_SHOW_REPO_AGE boolean {
190 gw_conf->got_show_repo_age = $2;
192 | GOT_SHOW_REPO_DESCRIPTION boolean {
193 gw_conf->got_show_repo_description = $2;
195 | GOT_SHOW_REPO_CLONEURL boolean {
196 gw_conf->got_show_repo_cloneurl = $2;
198 | GOT_MAX_REPOS_DISPLAY NUMBER {
200 gw_conf->got_max_repos_display = $2;
202 | GOT_MAX_COMMITS_DISPLAY NUMBER {
204 gw_conf->got_max_commits_display = $2;
215 yyerror(const char *fmt, ...)
222 if (vasprintf(&msg, fmt, ap) == -1) {
223 gerror = got_error_from_errno("vasprintf");
227 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
228 gerror = got_error_from_errno("asprintf");
231 gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
238 kw_cmp(const void *k, const void *e)
240 return (strcmp(k, ((const struct keywords *)e)->k_name));
246 /* This has to be sorted always. */
247 static const struct keywords keywords[] = {
248 { "got_logo", GOT_LOGO },
249 { "got_logo_url", GOT_LOGO_URL },
250 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
251 { "got_max_repos", GOT_MAX_REPOS },
252 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
253 { "got_repos_path", GOT_REPOS_PATH },
254 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
255 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
256 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
257 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
258 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
259 { "got_site_link", GOT_SITE_LINK },
260 { "got_site_name", GOT_SITE_NAME },
261 { "got_site_owner", GOT_SITE_OWNER },
263 const struct keywords *p;
265 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
266 sizeof(keywords[0]), kw_cmp);
274 #define START_EXPAND 1
275 #define DONE_EXPAND 2
277 static int expanding;
285 if (file->ungetpos > 0)
286 c = file->ungetbuf[--file->ungetpos];
288 c = getc(file->stream);
290 if (c == START_EXPAND)
292 else if (c == DONE_EXPAND)
306 if ((c = igetc()) == EOF) {
307 yyerror("reached end of file while parsing "
309 if (file == topfile || popfile() == EOF)
316 while ((c = igetc()) == '\\') {
322 yylval.lineno = file->lineno;
328 * Fake EOL when hit EOF for the first time. This gets line
329 * count right if last line in included file is syntactically
330 * invalid and has no newline.
332 if (file->eof_reached == 0) {
333 file->eof_reached = 1;
337 if (file == topfile || popfile() == EOF)
351 if (file->ungetpos >= file->ungetsize) {
352 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
354 err(1, "%s", __func__);
356 file->ungetsize *= 2;
358 file->ungetbuf[file->ungetpos++] = c;
366 /* Skip to either EOF or the first real EOL. */
382 unsigned char buf[8096];
383 unsigned char *p, *val;
389 while ((c = lgetc(0)) == ' ' || c == '\t')
392 yylval.lineno = file->lineno;
394 while ((c = lgetc(0)) != '\n' && c != EOF)
396 if (c == '$' && !expanding) {
398 if ((c = lgetc(0)) == EOF)
401 if (p + 1 >= buf + sizeof(buf) - 1) {
402 yyerror("string too long");
405 if (isalnum(c) || c == '_') {
415 yyerror("macro '%s' not defined", buf);
418 p = val + strlen(val) - 1;
419 lungetc(DONE_EXPAND);
424 lungetc(START_EXPAND);
433 if ((c = lgetc(quotec)) == EOF)
438 } else if (c == '\\') {
439 if ((next = lgetc(quotec)) == EOF)
441 if (next == quotec || c == ' ' || c == '\t')
443 else if (next == '\n') {
448 } else if (c == quotec) {
451 } else if (c == '\0') {
452 yyerror("syntax error");
455 if (p + 1 >= buf + sizeof(buf) - 1) {
456 yyerror("string too long");
461 yylval.v.string = strdup(buf);
462 if (yylval.v.string == NULL)
463 err(1, "%s", __func__);
467 #define allowed_to_end_number(x) \
468 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
470 if (c == '-' || isdigit(c)) {
473 if ((unsigned)(p-buf) >= sizeof(buf)) {
474 yyerror("string too long");
477 } while ((c = lgetc(0)) != EOF && isdigit(c));
479 if (p == buf + 1 && buf[0] == '-')
481 if (c == EOF || allowed_to_end_number(c)) {
482 const char *errstr = NULL;
485 yylval.v.number = strtonum(buf, LLONG_MIN,
488 yyerror("\"%s\" invalid number: %s",
503 #define allowed_in_string(x) \
504 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
505 x != '{' && x != '}' && \
506 x != '!' && x != '=' && x != '#' && \
509 if (isalnum(c) || c == ':' || c == '_') {
512 if ((unsigned)(p-buf) >= sizeof(buf)) {
513 yyerror("string too long");
516 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
519 if ((token = lookup(buf)) == STRING)
520 if ((yylval.v.string = strdup(buf)) == NULL)
521 err(1, "%s", __func__);
525 yylval.lineno = file->lineno;
533 static const struct got_error*
534 pushfile(struct file **nfile, const char *name)
536 const struct got_error* error = NULL;
538 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
539 return got_error_from_errno2(__func__, "calloc");
540 if (((*nfile)->name = strdup(name)) == NULL) {
542 return got_error_from_errno2(__func__, "strdup");
544 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
546 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
547 return got_error_from_errno("asprintf");
548 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
549 free((*nfile)->name);
554 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
555 (*nfile)->ungetsize = 16;
556 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
557 if ((*nfile)->ungetbuf == NULL) {
558 fclose((*nfile)->stream);
559 free((*nfile)->name);
561 return got_error_from_errno2(__func__, "malloc");
563 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
570 struct file *prev = NULL;
572 TAILQ_REMOVE(&files, file, entry);
573 fclose(file->stream);
575 free(file->ungetbuf);
578 return (file ? 0 : EOF);
581 const struct got_error*
582 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
584 gw_conf = malloc(sizeof(struct gotweb_config));
585 if (gw_conf == NULL) {
586 gerror = got_error_from_errno("malloc");
589 gw_conf->got_repos_path = strdup(D_GOTPATH);
590 if (gw_conf->got_repos_path == NULL) {
591 gerror = got_error_from_errno("strdup");
594 gw_conf->got_site_name = strdup(D_SITENAME);
595 if (gw_conf->got_site_name == NULL) {
596 gerror = got_error_from_errno("strdup");
599 gw_conf->got_site_owner = strdup(D_SITEOWNER);
600 if (gw_conf->got_site_owner == NULL) {
601 gerror = got_error_from_errno("strdup");
604 gw_conf->got_site_link = strdup(D_SITELINK);
605 if (gw_conf->got_site_link == NULL) {
606 gerror = got_error_from_errno("strdup");
609 gw_conf->got_logo = strdup(D_GOTLOGO);
610 if (gw_conf->got_logo == NULL) {
611 gerror = got_error_from_errno("strdup");
614 gw_conf->got_logo_url = strdup(D_GOTURL);
615 if (gw_conf->got_logo_url == NULL) {
616 gerror = got_error_from_errno("strdup");
619 gw_conf->got_show_site_owner = D_SHOWSOWNER;
620 gw_conf->got_show_repo_owner = D_SHOWROWNER;
621 gw_conf->got_show_repo_age = D_SHOWAGE;
622 gw_conf->got_show_repo_description = D_SHOWDESC;
623 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
624 gw_conf->got_max_repos = D_MAXREPO;
625 gw_conf->got_max_repos_display = D_MAXREPODISP;
626 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
629 * We don't require that the gotweb config file exists
630 * So reset gerror if it doesn't exist and goto done.
632 gerror = pushfile(&file, filename);
633 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
648 symset(const char *nam, const char *val, int persist)
652 TAILQ_FOREACH(sym, &symhead, entry) {
653 if (strcmp(nam, sym->nam) == 0)
658 if (sym->persist == 1)
663 TAILQ_REMOVE(&symhead, sym, entry);
667 if ((sym = calloc(1, sizeof(*sym))) == NULL)
670 sym->nam = strdup(nam);
671 if (sym->nam == NULL) {
675 sym->val = strdup(val);
676 if (sym->val == NULL) {
682 sym->persist = persist;
683 TAILQ_INSERT_TAIL(&symhead, sym, entry);
688 cmdline_symset(char *s)
694 if ((val = strrchr(s, '=')) == NULL)
697 len = strlen(s) - strlen(val) + 1;
698 if ((sym = malloc(len)) == NULL)
699 errx(1, "cmdline_symset: malloc");
701 strlcpy(sym, s, len);
703 ret = symset(sym, val + 1, 1);
710 symget(const char *nam)
714 TAILQ_FOREACH(sym, &symhead, entry) {
715 if (strcmp(nam, sym->nam) == 0) {