Blob


1 /*
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.
9 *
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.
13 *
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.
21 */
23 %{
24 #include <sys/types.h>
25 #include <sys/queue.h>
27 #include <ctype.h>
28 #include <err.h>
29 #include <limits.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "got_error.h"
36 #include "gotweb.h"
38 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
39 static struct file {
40 TAILQ_ENTRY(file) entry;
41 FILE *stream;
42 char *name;
43 size_t ungetpos;
44 size_t ungetsize;
45 u_char *ungetbuf;
46 int eof_reached;
47 int lineno;
48 } *file, *topfile;
49 static const struct got_error* pushfile(struct file**, const char *);
50 int popfile(void);
51 int yyparse(void);
52 int yylex(void);
53 int yyerror(const char *, ...)
54 __attribute__((__format__ (printf, 1, 2)))
55 __attribute__((__nonnull__ (1)));
56 int kw_cmp(const void *, const void *);
57 int lookup(char *);
58 int igetc(void);
59 int lgetc(int);
60 void lungetc(int);
61 int findeol(void);
63 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
64 struct sym {
65 TAILQ_ENTRY(sym) entry;
66 int used;
67 int persist;
68 char *nam;
69 char *val;
70 };
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;
79 typedef struct {
80 union {
81 int64_t number;
82 char *string;
83 } v;
84 int lineno;
85 } YYSTYPE;
87 %}
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
97 %%
99 grammar : /* empty */
100 | grammar '\n'
101 | grammar main '\n'
104 boolean : STRING {
105 if (strcasecmp($1, "true") == 0 ||
106 strcasecmp($1, "on") == 0 ||
107 strcasecmp($1, "yes") == 0)
108 $$ = 1;
109 else if (strcasecmp($1, "false") == 0 ||
110 strcasecmp($1, "off") == 0 ||
111 strcasecmp($1, "no") == 0)
112 $$ = 0;
113 else {
114 yyerror("invalid boolean value '%s'", $1);
115 free($1);
116 YYERROR;
118 free($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 {
128 if ($2 > 0)
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;
140 | GOT_LOGO STRING {
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 {
162 if ($2 > 0)
163 gw_conf->got_max_repos_display = $2;
165 | GOT_MAX_COMMITS_DISPLAY NUMBER {
166 if ($2 > 0)
167 gw_conf->got_max_commits_display = $2;
170 %%
172 struct keywords {
173 const char *k_name;
174 int k_val;
175 };
177 int
178 yyerror(const char *fmt, ...)
180 va_list ap;
181 char *msg;
182 char *err = NULL;
184 va_start(ap, fmt);
185 if (vasprintf(&msg, fmt, ap) == -1) {
186 gerror = got_error_from_errno("vasprintf");
187 return 0;
189 va_end(ap);
190 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
191 gerror = got_error_from_errno("asprintf");
192 return(0);
194 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
195 free(msg);
196 return(0);
199 int
200 kw_cmp(const void *k, const void *e)
202 return (strcmp(k, ((const struct keywords *)e)->k_name));
205 int
206 lookup(char *s)
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 },
225 };
226 const struct keywords *p;
228 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
229 sizeof(keywords[0]), kw_cmp);
231 if (p)
232 return (p->k_val);
233 else
234 return (STRING);
237 #define START_EXPAND 1
238 #define DONE_EXPAND 2
240 static int expanding;
242 int
243 igetc(void)
245 int c;
247 while (1) {
248 if (file->ungetpos > 0)
249 c = file->ungetbuf[--file->ungetpos];
250 else
251 c = getc(file->stream);
253 if (c == START_EXPAND)
254 expanding = 1;
255 else if (c == DONE_EXPAND)
256 expanding = 0;
257 else
258 break;
260 return (c);
263 int
264 lgetc(int quotec)
266 int c, next;
268 if (quotec) {
269 if ((c = igetc()) == EOF) {
270 yyerror("reached end of file while parsing "
271 "quoted string");
272 if (file == topfile || popfile() == EOF)
273 return (EOF);
274 return (quotec);
276 return (c);
279 while ((c = igetc()) == '\\') {
280 next = igetc();
281 if (next != '\n') {
282 c = next;
283 break;
285 yylval.lineno = file->lineno;
286 file->lineno++;
289 if (c == EOF) {
290 /*
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.
294 */
295 if (file->eof_reached == 0) {
296 file->eof_reached = 1;
297 return ('\n');
299 while (c == EOF) {
300 if (file == topfile || popfile() == EOF)
301 return (EOF);
302 c = igetc();
305 return (c);
308 void
309 lungetc(int c)
311 if (c == EOF)
312 return;
314 if (file->ungetpos >= file->ungetsize) {
315 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
316 if (p == NULL)
317 err(1, "%s", __func__);
318 file->ungetbuf = p;
319 file->ungetsize *= 2;
321 file->ungetbuf[file->ungetpos++] = c;
324 int
325 findeol(void)
327 int c;
329 /* Skip to either EOF or the first real EOL. */
330 while (1) {
331 c = lgetc(0);
332 if (c == '\n') {
333 file->lineno++;
334 break;
336 if (c == EOF)
337 break;
339 return (ERROR);
342 int
343 yylex(void)
345 char buf[8096];
346 char *p, *val;
347 int quotec, next, c;
348 int token;
350 top:
351 p = buf;
352 while ((c = lgetc(0)) == ' ' || c == '\t')
353 ; /* nothing */
355 yylval.lineno = file->lineno;
356 if (c == '#')
357 while ((c = lgetc(0)) != '\n' && c != EOF)
358 ; /* nothing */
359 if (c == '$' && !expanding) {
360 while (1) {
361 if ((c = lgetc(0)) == EOF)
362 return (0);
364 if (p + 1 >= buf + sizeof(buf) - 1) {
365 yyerror("string too long");
366 return (findeol());
368 if (isalnum(c) || c == '_') {
369 *p++ = c;
370 continue;
372 *p = '\0';
373 lungetc(c);
374 break;
376 val = symget(buf);
377 if (val == NULL) {
378 yyerror("macro '%s' not defined", buf);
379 return (findeol());
381 p = val + strlen(val) - 1;
382 lungetc(DONE_EXPAND);
383 while (p >= val) {
384 lungetc((unsigned char)*p);
385 p--;
387 lungetc(START_EXPAND);
388 goto top;
391 switch (c) {
392 case '\'':
393 case '"':
394 quotec = c;
395 while (1) {
396 if ((c = lgetc(quotec)) == EOF)
397 return (0);
398 if (c == '\n') {
399 file->lineno++;
400 continue;
401 } else if (c == '\\') {
402 if ((next = lgetc(quotec)) == EOF)
403 return (0);
404 if (next == quotec || c == ' ' || c == '\t')
405 c = next;
406 else if (next == '\n') {
407 file->lineno++;
408 continue;
409 } else
410 lungetc(next);
411 } else if (c == quotec) {
412 *p = '\0';
413 break;
414 } else if (c == '\0') {
415 yyerror("syntax error");
416 return (findeol());
418 if (p + 1 >= buf + sizeof(buf) - 1) {
419 yyerror("string too long");
420 return (findeol());
422 *p++ = c;
424 yylval.v.string = strdup(buf);
425 if (yylval.v.string == NULL)
426 err(1, "%s", __func__);
427 return (STRING);
430 #define allowed_to_end_number(x) \
431 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
433 if (c == '-' || isdigit(c)) {
434 do {
435 *p++ = c;
436 if ((size_t)(p-buf) >= sizeof(buf)) {
437 yyerror("string too long");
438 return (findeol());
440 } while ((c = lgetc(0)) != EOF && isdigit(c));
441 lungetc(c);
442 if (p == buf + 1 && buf[0] == '-')
443 goto nodigits;
444 if (c == EOF || allowed_to_end_number(c)) {
445 const char *errstr = NULL;
447 *p = '\0';
448 yylval.v.number = strtonum(buf, LLONG_MIN,
449 LLONG_MAX, &errstr);
450 if (errstr) {
451 yyerror("\"%s\" invalid number: %s",
452 buf, errstr);
453 return (findeol());
455 return (NUMBER);
456 } else {
457 nodigits:
458 while (p > buf + 1)
459 lungetc((unsigned char)*--p);
460 c = (unsigned char)*--p;
461 if (c == '-')
462 return (c);
466 #define allowed_in_string(x) \
467 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
468 x != '{' && x != '}' && \
469 x != '!' && x != '=' && x != '#' && \
470 x != ','))
472 if (isalnum(c) || c == ':' || c == '_') {
473 do {
474 *p++ = c;
475 if ((size_t)(p-buf) >= sizeof(buf)) {
476 yyerror("string too long");
477 return (findeol());
479 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
480 lungetc(c);
481 *p = '\0';
482 if ((token = lookup(buf)) == STRING)
483 if ((yylval.v.string = strdup(buf)) == NULL)
484 err(1, "%s", __func__);
485 return (token);
487 if (c == '\n') {
488 yylval.lineno = file->lineno;
489 file->lineno++;
491 if (c == EOF)
492 return (0);
493 return (c);
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) {
504 free(nfile);
505 return got_error_from_errno2(__func__, "strdup");
507 if (((*nfile)->stream = fopen((*nfile)->name, "re")) == NULL) {
508 char *msg = 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);
513 free((*nfile));
514 free(msg);
515 return error;
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);
523 free((*nfile));
524 return got_error_from_errno2(__func__, "malloc");
526 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
527 return error;
530 int
531 popfile(void)
533 struct file *prev = NULL;
535 TAILQ_REMOVE(&files, file, entry);
536 fclose(file->stream);
537 free(file->name);
538 free(file->ungetbuf);
539 free(file);
540 file = prev;
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");
550 goto done;
552 gw_conf->got_repos_path = strdup(D_GOTPATH);
553 if (gw_conf->got_repos_path == NULL) {
554 gerror = got_error_from_errno("strdup");
555 goto done;
557 gw_conf->got_www_path = strdup(D_GOTWWW);
558 if (gw_conf->got_www_path == NULL) {
559 gerror = got_error_from_errno("strdup");
560 goto done;
562 gw_conf->got_site_name = strdup(D_SITENAME);
563 if (gw_conf->got_site_name == NULL) {
564 gerror = got_error_from_errno("strdup");
565 goto done;
567 gw_conf->got_site_owner = strdup(D_SITEOWNER);
568 if (gw_conf->got_site_owner == NULL) {
569 gerror = got_error_from_errno("strdup");
570 goto done;
572 gw_conf->got_site_link = strdup(D_SITELINK);
573 if (gw_conf->got_site_link == NULL) {
574 gerror = got_error_from_errno("strdup");
575 goto done;
577 gw_conf->got_logo = strdup(D_GOTLOGO);
578 if (gw_conf->got_logo == NULL) {
579 gerror = got_error_from_errno("strdup");
580 goto done;
582 gw_conf->got_logo_url = strdup(D_GOTURL);
583 if (gw_conf->got_logo_url == NULL) {
584 gerror = got_error_from_errno("strdup");
585 goto done;
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;
596 /*
597 * We don't require that the gotweb config file exists
598 * So reset gerror if it doesn't exist and goto done.
599 */
600 gerror = pushfile(&file, filename);
601 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
602 gerror = NULL;
603 goto done;
604 } else if (gerror)
605 return gerror;
606 topfile = file;
608 yyparse();
609 popfile();
610 done:
611 *gconf = gw_conf;
612 return gerror;
615 int
616 symset(const char *nam, const char *val, int persist)
618 struct sym *sym;
620 TAILQ_FOREACH(sym, &symhead, entry) {
621 if (strcmp(nam, sym->nam) == 0)
622 break;
625 if (sym != NULL) {
626 if (sym->persist == 1)
627 return (0);
628 else {
629 free(sym->nam);
630 free(sym->val);
631 TAILQ_REMOVE(&symhead, sym, entry);
632 free(sym);
635 if ((sym = calloc(1, sizeof(*sym))) == NULL)
636 return (-1);
638 sym->nam = strdup(nam);
639 if (sym->nam == NULL) {
640 free(sym);
641 return (-1);
643 sym->val = strdup(val);
644 if (sym->val == NULL) {
645 free(sym->nam);
646 free(sym);
647 return (-1);
649 sym->used = 0;
650 sym->persist = persist;
651 TAILQ_INSERT_TAIL(&symhead, sym, entry);
652 return (0);
655 int
656 cmdline_symset(char *s)
658 char *sym, *val;
659 int ret;
660 size_t len;
662 if ((val = strrchr(s, '=')) == NULL)
663 return (-1);
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);
672 free(sym);
674 return (ret);
677 char *
678 symget(const char *nam)
680 struct sym *sym;
682 TAILQ_FOREACH(sym, &symhead, entry) {
683 if (strcmp(nam, sym->nam) == 0) {
684 sym->used = 1;
685 return (sym->val);
688 return (NULL);