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_WWW_PATH STRING {
141 gw_conf->got_www_path = strdup($2);
142 if (gw_conf->got_www_path == NULL) {
149 | GOT_MAX_REPOS NUMBER {
151 gw_conf->got_max_repos = $2;
153 | GOT_SITE_NAME STRING {
154 gw_conf->got_site_name = strdup($2);
155 if (gw_conf->got_site_name == NULL) {
162 | GOT_SITE_OWNER STRING {
163 gw_conf->got_site_owner = strdup($2);
164 if (gw_conf->got_site_owner == NULL) {
171 | GOT_SITE_LINK STRING {
172 gw_conf->got_site_link = strdup($2);
173 if (gw_conf->got_site_link == NULL) {
181 gw_conf->got_logo = strdup($2);
182 if (gw_conf->got_logo== NULL) {
189 | GOT_LOGO_URL STRING {
190 gw_conf->got_logo_url = strdup($2);
191 if (gw_conf->got_logo_url== NULL) {
198 | GOT_SHOW_SITE_OWNER boolean {
199 gw_conf->got_show_site_owner = $2;
201 | GOT_SHOW_REPO_OWNER boolean {
202 gw_conf->got_show_repo_owner = $2;
204 | GOT_SHOW_REPO_AGE boolean {
205 gw_conf->got_show_repo_age = $2;
207 | GOT_SHOW_REPO_DESCRIPTION boolean {
208 gw_conf->got_show_repo_description = $2;
210 | GOT_SHOW_REPO_CLONEURL boolean {
211 gw_conf->got_show_repo_cloneurl = $2;
213 | GOT_MAX_REPOS_DISPLAY NUMBER {
215 gw_conf->got_max_repos_display = $2;
217 | GOT_MAX_COMMITS_DISPLAY NUMBER {
219 gw_conf->got_max_commits_display = $2;
230 yyerror(const char *fmt, ...)
237 if (vasprintf(&msg, fmt, ap) == -1) {
238 gerror = got_error_from_errno("vasprintf");
242 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
243 gerror = got_error_from_errno("asprintf");
246 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
253 kw_cmp(const void *k, const void *e)
255 return (strcmp(k, ((const struct keywords *)e)->k_name));
261 /* This has to be sorted always. */
262 static const struct keywords keywords[] = {
263 { "got_logo", GOT_LOGO },
264 { "got_logo_url", GOT_LOGO_URL },
265 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
266 { "got_max_repos", GOT_MAX_REPOS },
267 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
268 { "got_repos_path", GOT_REPOS_PATH },
269 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
270 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
271 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
272 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
273 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
274 { "got_site_link", GOT_SITE_LINK },
275 { "got_site_name", GOT_SITE_NAME },
276 { "got_site_owner", GOT_SITE_OWNER },
277 { "got_www_path", GOT_WWW_PATH },
279 const struct keywords *p;
281 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
282 sizeof(keywords[0]), kw_cmp);
290 #define START_EXPAND 1
291 #define DONE_EXPAND 2
293 static int expanding;
301 if (file->ungetpos > 0)
302 c = file->ungetbuf[--file->ungetpos];
304 c = getc(file->stream);
306 if (c == START_EXPAND)
308 else if (c == DONE_EXPAND)
322 if ((c = igetc()) == EOF) {
323 yyerror("reached end of file while parsing "
325 if (file == topfile || popfile() == EOF)
332 while ((c = igetc()) == '\\') {
338 yylval.lineno = file->lineno;
344 * Fake EOL when hit EOF for the first time. This gets line
345 * count right if last line in included file is syntactically
346 * invalid and has no newline.
348 if (file->eof_reached == 0) {
349 file->eof_reached = 1;
353 if (file == topfile || popfile() == EOF)
367 if (file->ungetpos >= file->ungetsize) {
368 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
370 err(1, "%s", __func__);
372 file->ungetsize *= 2;
374 file->ungetbuf[file->ungetpos++] = c;
382 /* Skip to either EOF or the first real EOL. */
398 unsigned char buf[8096];
399 unsigned char *p, *val;
405 while ((c = lgetc(0)) == ' ' || c == '\t')
408 yylval.lineno = file->lineno;
410 while ((c = lgetc(0)) != '\n' && c != EOF)
412 if (c == '$' && !expanding) {
414 if ((c = lgetc(0)) == EOF)
417 if (p + 1 >= buf + sizeof(buf) - 1) {
418 yyerror("string too long");
421 if (isalnum(c) || c == '_') {
431 yyerror("macro '%s' not defined", buf);
434 p = val + strlen(val) - 1;
435 lungetc(DONE_EXPAND);
440 lungetc(START_EXPAND);
449 if ((c = lgetc(quotec)) == EOF)
454 } else if (c == '\\') {
455 if ((next = lgetc(quotec)) == EOF)
457 if (next == quotec || c == ' ' || c == '\t')
459 else if (next == '\n') {
464 } else if (c == quotec) {
467 } else if (c == '\0') {
468 yyerror("syntax error");
471 if (p + 1 >= buf + sizeof(buf) - 1) {
472 yyerror("string too long");
477 yylval.v.string = strdup(buf);
478 if (yylval.v.string == NULL)
479 err(1, "%s", __func__);
483 #define allowed_to_end_number(x) \
484 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
486 if (c == '-' || isdigit(c)) {
489 if ((unsigned)(p-buf) >= sizeof(buf)) {
490 yyerror("string too long");
493 } while ((c = lgetc(0)) != EOF && isdigit(c));
495 if (p == buf + 1 && buf[0] == '-')
497 if (c == EOF || allowed_to_end_number(c)) {
498 const char *errstr = NULL;
501 yylval.v.number = strtonum(buf, LLONG_MIN,
504 yyerror("\"%s\" invalid number: %s",
519 #define allowed_in_string(x) \
520 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
521 x != '{' && x != '}' && \
522 x != '!' && x != '=' && x != '#' && \
525 if (isalnum(c) || c == ':' || c == '_') {
528 if ((unsigned)(p-buf) >= sizeof(buf)) {
529 yyerror("string too long");
532 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
535 if ((token = lookup(buf)) == STRING)
536 if ((yylval.v.string = strdup(buf)) == NULL)
537 err(1, "%s", __func__);
541 yylval.lineno = file->lineno;
549 static const struct got_error*
550 pushfile(struct file **nfile, const char *name)
552 const struct got_error* error = NULL;
554 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
555 return got_error_from_errno2(__func__, "calloc");
556 if (((*nfile)->name = strdup(name)) == NULL) {
558 return got_error_from_errno2(__func__, "strdup");
560 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
562 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
563 return got_error_from_errno("asprintf");
564 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
565 free((*nfile)->name);
570 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
571 (*nfile)->ungetsize = 16;
572 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
573 if ((*nfile)->ungetbuf == NULL) {
574 fclose((*nfile)->stream);
575 free((*nfile)->name);
577 return got_error_from_errno2(__func__, "malloc");
579 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
586 struct file *prev = NULL;
588 TAILQ_REMOVE(&files, file, entry);
589 fclose(file->stream);
591 free(file->ungetbuf);
594 return (file ? 0 : EOF);
597 const struct got_error*
598 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
600 gw_conf = malloc(sizeof(struct gotweb_config));
601 if (gw_conf == NULL) {
602 gerror = got_error_from_errno("malloc");
605 gw_conf->got_repos_path = strdup(D_GOTPATH);
606 if (gw_conf->got_repos_path == NULL) {
607 gerror = got_error_from_errno("strdup");
610 gw_conf->got_www_path = strdup(D_GOTWWW);
611 if (gw_conf->got_www_path == NULL) {
612 gerror = got_error_from_errno("strdup");
615 gw_conf->got_site_name = strdup(D_SITENAME);
616 if (gw_conf->got_site_name == NULL) {
617 gerror = got_error_from_errno("strdup");
620 gw_conf->got_site_owner = strdup(D_SITEOWNER);
621 if (gw_conf->got_site_owner == NULL) {
622 gerror = got_error_from_errno("strdup");
625 gw_conf->got_site_link = strdup(D_SITELINK);
626 if (gw_conf->got_site_link == NULL) {
627 gerror = got_error_from_errno("strdup");
630 gw_conf->got_logo = strdup(D_GOTLOGO);
631 if (gw_conf->got_logo == NULL) {
632 gerror = got_error_from_errno("strdup");
635 gw_conf->got_logo_url = strdup(D_GOTURL);
636 if (gw_conf->got_logo_url == NULL) {
637 gerror = got_error_from_errno("strdup");
640 gw_conf->got_show_site_owner = D_SHOWSOWNER;
641 gw_conf->got_show_repo_owner = D_SHOWROWNER;
642 gw_conf->got_show_repo_age = D_SHOWAGE;
643 gw_conf->got_show_repo_description = D_SHOWDESC;
644 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
645 gw_conf->got_max_repos = D_MAXREPO;
646 gw_conf->got_max_repos_display = D_MAXREPODISP;
647 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
650 * We don't require that the gotweb config file exists
651 * So reset gerror if it doesn't exist and goto done.
653 gerror = pushfile(&file, filename);
654 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
669 symset(const char *nam, const char *val, int persist)
673 TAILQ_FOREACH(sym, &symhead, entry) {
674 if (strcmp(nam, sym->nam) == 0)
679 if (sym->persist == 1)
684 TAILQ_REMOVE(&symhead, sym, entry);
688 if ((sym = calloc(1, sizeof(*sym))) == NULL)
691 sym->nam = strdup(nam);
692 if (sym->nam == NULL) {
696 sym->val = strdup(val);
697 if (sym->val == NULL) {
703 sym->persist = persist;
704 TAILQ_INSERT_TAIL(&symhead, sym, entry);
709 cmdline_symset(char *s)
715 if ((val = strrchr(s, '=')) == NULL)
718 len = strlen(s) - strlen(val) + 1;
719 if ((sym = malloc(len)) == NULL)
720 errx(1, "cmdline_symset: malloc");
722 strlcpy(sym, s, len);
724 ret = symset(sym, val + 1, 1);
731 symget(const char *nam)
735 TAILQ_FOREACH(sym, &symhead, entry) {
736 if (strcmp(nam, sym->nam) == 0) {