2 * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
4 * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
6 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
23 #include <sys/queue.h>
36 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
38 TAILQ_ENTRY(file) entry;
43 const struct got_error* error;
45 struct file *pushfile(const char *);
49 int yyerror(const char *, ...)
50 __attribute__((__format__ (printf, 1, 2)))
51 __attribute__((__nonnull__ (1)));
52 int kw_cmp(const void *, const void *);
58 static const struct got_error* gerror = NULL;
61 struct gotweb_conf *gw_conf;
73 %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
74 %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
75 %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
76 %token GOT_MAX_COMMITS_DISPLAY ON ERROR GOT_SHOW_SITE_OWNER
77 %token GOT_SHOW_REPO_CLONEURL
78 %token <v.string> STRING
79 %token <v.number> NUMBER
80 %type <v.number> boolean
86 | grammar error '\n' { file->errors++; }
90 if (strcasecmp($1, "true") == 0 ||
91 strcasecmp($1, "yes") == 0)
93 else if (strcasecmp($1, "false") == 0 ||
94 strcasecmp($1, "off") == 0 ||
95 strcasecmp($1, "no") == 0)
98 yyerror("invalid boolean value '%s'", $1);
107 main : GOT_REPOS_PATH STRING {
108 if ((gw_conf->got_repos_path = strdup($2)) == NULL)
109 errx(1, "out of memory");
111 | GOT_WWW_PATH STRING {
112 if ((gw_conf->got_www_path = strdup($2)) == NULL)
113 errx(1, "out of memory");
115 | GOT_MAX_REPOS NUMBER {
117 gw_conf->got_max_repos = $2;
119 | GOT_SITE_NAME STRING {
120 if ((gw_conf->got_site_name = strdup($2)) == NULL)
121 errx(1, "out of memory");
123 | GOT_SITE_OWNER STRING {
124 if ((gw_conf->got_site_owner = strdup($2)) == NULL)
125 errx(1, "out of memory");
127 | GOT_SITE_LINK STRING {
128 if ((gw_conf->got_site_link = strdup($2)) == NULL)
129 errx(1, "out of memory");
132 if ((gw_conf->got_logo = strdup($2)) == NULL)
133 errx(1, "out of memory");
135 | GOT_LOGO_URL STRING {
136 if ((gw_conf->got_logo_url = strdup($2)) == NULL)
137 errx(1, "out of memory");
139 | GOT_SHOW_SITE_OWNER boolean {
140 gw_conf->got_show_site_owner = $2;
142 | GOT_SHOW_REPO_OWNER boolean {
143 gw_conf->got_show_repo_owner = $2;
145 | GOT_SHOW_REPO_AGE boolean { gw_conf->got_show_repo_age = $2; }
146 | GOT_SHOW_REPO_DESCRIPTION boolean {
147 gw_conf->got_show_repo_description = $2;
149 | GOT_SHOW_REPO_CLONEURL boolean {
150 gw_conf->got_show_repo_cloneurl = $2;
152 | GOT_MAX_REPOS_DISPLAY NUMBER {
154 gw_conf->got_max_repos_display = $2;
156 | GOT_MAX_COMMITS_DISPLAY NUMBER {
158 gw_conf->got_max_commits_display = $2;
170 yyerror(const char *fmt, ...)
174 static char err_msg[512];
178 if (vasprintf(&msg, fmt, ap) == -1)
179 errx(1, "yyerror vasprintf");
181 snprintf(err_msg, sizeof(err_msg), "%s:%d: %s", file->name,
183 gerror = got_error_from_errno2("parse_error", err_msg);
190 kw_cmp(const void *k, const void *e)
192 return (strcmp(k, ((const struct keywords *)e)->k_name));
198 /* this has to be sorted always */
199 static const struct keywords keywords[] = {
200 { "got_logo", GOT_LOGO },
201 { "got_logo_url", GOT_LOGO_URL },
202 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
203 { "got_max_repos", GOT_MAX_REPOS },
204 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
205 { "got_repos_path", GOT_REPOS_PATH },
206 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
207 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
208 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
209 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
210 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
211 { "got_site_link", GOT_SITE_LINK },
212 { "got_site_name", GOT_SITE_NAME },
213 { "got_site_owner", GOT_SITE_OWNER },
214 { "got_www_path", GOT_WWW_PATH },
216 const struct keywords *p;
218 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
219 sizeof(keywords[0]), kw_cmp);
227 #define MAXPUSHBACK 128
231 u_char pushback_buffer[MAXPUSHBACK];
232 int pushback_index = 0;
240 /* Read character from the parsebuffer instead of input. */
241 if (parseindex >= 0) {
242 c = parsebuf[parseindex++];
251 return (pushback_buffer[--pushback_index]);
254 if ((c = getc(file->stream)) == EOF) {
255 yyerror("reached end of file while parsing "
257 if (file == topfile || popfile() == EOF)
264 while ((c = getc(file->stream)) == '\\') {
265 next = getc(file->stream);
270 yylval.lineno = file->lineno;
275 if (file == topfile || popfile() == EOF)
277 c = getc(file->stream);
292 if (pushback_index < MAXPUSHBACK-1)
293 return (pushback_buffer[pushback_index++] = c);
305 /* skip to either EOF or the first real EOL */
308 c = pushback_buffer[--pushback_index];
330 while ((c = lgetc(0)) == ' ' || c == '\t')
333 yylval.lineno = file->lineno;
335 while ((c = lgetc(0)) != '\n' && c != EOF)
343 if ((c = lgetc(quotec)) == EOF)
348 } else if (c == '\\') {
349 if ((next = lgetc(quotec)) == EOF)
351 if (next == quotec || next == ' ' ||
354 else if (next == '\n') {
359 } else if (c == quotec) {
362 } else if (c == '\0') {
363 yyerror("syntax error");
366 if (p + 1 >= buf + sizeof(buf) - 1) {
367 yyerror("string too long");
372 yylval.v.string = strdup(buf);
373 if (yylval.v.string == NULL)
374 errx(1, "yylex: strdup");
378 #define allowed_to_end_number(x) \
379 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
381 if (c == '-' || isdigit(c)) {
384 if ((size_t)(p-buf) >= sizeof(buf)) {
385 yyerror("string too long");
388 } while ((c = lgetc(0)) != EOF && isdigit(c));
390 if (p == buf + 1 && buf[0] == '-')
392 if (c == EOF || allowed_to_end_number(c)) {
393 const char *errstr = NULL;
396 yylval.v.number = strtonum(buf, LLONG_MIN,
399 yyerror("\"%s\" invalid number: %s",
414 #define allowed_in_string(x) \
415 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
416 x != '{' && x != '}' && x != '<' && x != '>' && \
417 x != '!' && x != '=' && x != '/' && x != '#' && \
420 if (isalnum(c) || c == ':' || c == '_' || c == '*') {
423 if ((size_t)(p-buf) >= sizeof(buf)) {
424 yyerror("string too long");
427 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
430 if ((token = lookup(buf)) == STRING)
431 if ((yylval.v.string = strdup(buf)) == NULL)
432 errx(1, "yylex: strdup");
436 yylval.lineno = file->lineno;
445 pushfile(const char *name)
449 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
450 gerror = got_error(GOT_ERR_NO_SPACE);
453 if ((nfile->name = strdup(name)) == NULL) {
454 gerror = got_error(GOT_ERR_NO_SPACE);
458 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
459 gerror = got_error_from_errno2("parse_conf", nfile->name);
465 TAILQ_INSERT_TAIL(&files, nfile, entry);
474 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
475 prev->errors += file->errors;
477 TAILQ_REMOVE(&files, file, entry);
478 fclose(file->stream);
482 return (file ? 0 : EOF);
485 const struct got_error*
486 parse_conf(const char *filename, struct gotweb_conf *gconf)
488 static const struct got_error* error = NULL;
491 if ((gw_conf->got_repos_path = strdup(D_GOTPATH)) == NULL)
492 errx(1, "out of memory");
493 if ((gw_conf->got_www_path = strdup(D_GOTWWW)) == NULL)
494 errx(1, "out of memory");
495 if ((gw_conf->got_site_name = strdup(D_SITENAME)) == NULL)
496 errx(1, "out of memory");
497 if ((gw_conf->got_site_owner = strdup(D_SITEOWNER)) == NULL)
498 errx(1, "out of memory");
499 if ((gw_conf->got_site_link = strdup(D_SITELINK)) == NULL)
500 errx(1, "out of memory");
501 if ((gw_conf->got_logo = strdup(D_GOTLOGO)) == NULL)
502 errx(1, "out of memory");
503 if ((gw_conf->got_logo_url = strdup(D_GOTURL)) == NULL)
504 errx(1, "out of memory");
505 gw_conf->got_show_site_owner = D_SHOWSOWNER;
506 gw_conf->got_show_repo_owner = D_SHOWROWNER;
507 gw_conf->got_show_repo_age = D_SHOWAGE;
508 gw_conf->got_show_repo_description = D_SHOWDESC;
509 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
510 gw_conf->got_max_repos = D_MAXREPO;
511 gw_conf->got_max_repos_display = D_MAXREPODISP;
512 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
513 if ((file = pushfile(filename)) == NULL) {
514 error = got_error_from_errno2("parse_conf", GOTWEB_CONF);