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 char *symget(const char *);
75 const struct got_error* gerror = NULL;
76 struct gotweb_config *gw_conf;
78 typedef struct {
79 union {
80 int64_t number;
81 char *string;
82 } v;
83 int lineno;
84 } YYSTYPE;
86 %}
88 %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
89 %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
90 %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
91 %token GOT_MAX_COMMITS_DISPLAY ERROR GOT_SHOW_SITE_OWNER
92 %token GOT_SHOW_REPO_CLONEURL
93 %token <v.string> STRING
94 %token <v.number> NUMBER
95 %type <v.number> boolean
96 %%
98 grammar : /* empty */
99 | grammar '\n'
100 | grammar main '\n'
103 boolean : STRING {
104 if (strcasecmp($1, "true") == 0 ||
105 strcasecmp($1, "on") == 0 ||
106 strcasecmp($1, "yes") == 0)
107 $$ = 1;
108 else if (strcasecmp($1, "false") == 0 ||
109 strcasecmp($1, "off") == 0 ||
110 strcasecmp($1, "no") == 0)
111 $$ = 0;
112 else {
113 yyerror("invalid boolean value '%s'", $1);
114 free($1);
115 YYERROR;
117 free($1);
120 main : GOT_REPOS_PATH STRING {
121 gw_conf->got_repos_path = strdup($2);
122 if (gw_conf->got_repos_path == NULL) {
123 free($2);
124 yyerror("strdup");
125 YYERROR;
127 free($2);
129 | GOT_WWW_PATH STRING {
130 gw_conf->got_www_path = strdup($2);
131 if (gw_conf->got_www_path == NULL) {
132 free($2);
133 yyerror("strdup");
134 YYERROR;
136 free($2);
138 | GOT_MAX_REPOS NUMBER {
139 if ($2 > 0)
140 gw_conf->got_max_repos = $2;
142 | GOT_SITE_NAME STRING {
143 gw_conf->got_site_name = strdup($2);
144 if (gw_conf->got_site_name == NULL) {
145 free($2);
146 yyerror("strdup");
147 YYERROR;
149 free($2);
151 | GOT_SITE_OWNER STRING {
152 gw_conf->got_site_owner = strdup($2);
153 if (gw_conf->got_site_owner == NULL) {
154 free($2);
155 yyerror("strdup");
156 YYERROR;
158 free($2);
160 | GOT_SITE_LINK STRING {
161 gw_conf->got_site_link = strdup($2);
162 if (gw_conf->got_site_link == NULL) {
163 free($2);
164 yyerror("strdup");
165 YYERROR;
167 free($2);
169 | GOT_LOGO STRING {
170 gw_conf->got_logo = strdup($2);
171 if (gw_conf->got_logo== NULL) {
172 free($2);
173 yyerror("strdup");
174 YYERROR;
176 free($2);
178 | GOT_LOGO_URL STRING {
179 gw_conf->got_logo_url = strdup($2);
180 if (gw_conf->got_logo_url== NULL) {
181 free($2);
182 yyerror("strdup");
183 YYERROR;
185 free($2);
187 | GOT_SHOW_SITE_OWNER boolean {
188 gw_conf->got_show_site_owner = $2;
190 | GOT_SHOW_REPO_OWNER boolean {
191 gw_conf->got_show_repo_owner = $2;
193 | GOT_SHOW_REPO_AGE boolean {
194 gw_conf->got_show_repo_age = $2;
196 | GOT_SHOW_REPO_DESCRIPTION boolean {
197 gw_conf->got_show_repo_description = $2;
199 | GOT_SHOW_REPO_CLONEURL boolean {
200 gw_conf->got_show_repo_cloneurl = $2;
202 | GOT_MAX_REPOS_DISPLAY NUMBER {
203 if ($2 > 0)
204 gw_conf->got_max_repos_display = $2;
206 | GOT_MAX_COMMITS_DISPLAY NUMBER {
207 if ($2 > 0)
208 gw_conf->got_max_commits_display = $2;
211 %%
213 struct keywords {
214 const char *k_name;
215 int k_val;
216 };
218 int
219 yyerror(const char *fmt, ...)
221 va_list ap;
222 char *msg;
223 char *err = NULL;
225 va_start(ap, fmt);
226 if (vasprintf(&msg, fmt, ap) == -1) {
227 gerror = got_error_from_errno("vasprintf");
228 return 0;
230 va_end(ap);
231 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
232 gerror = got_error_from_errno("asprintf");
233 return(0);
235 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
236 free(msg);
237 free(err);
238 return(0);
241 int
242 kw_cmp(const void *k, const void *e)
244 return (strcmp(k, ((const struct keywords *)e)->k_name));
247 int
248 lookup(char *s)
250 /* This has to be sorted always. */
251 static const struct keywords keywords[] = {
252 { "got_logo", GOT_LOGO },
253 { "got_logo_url", GOT_LOGO_URL },
254 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
255 { "got_max_repos", GOT_MAX_REPOS },
256 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
257 { "got_repos_path", GOT_REPOS_PATH },
258 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
259 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
260 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
261 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
262 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
263 { "got_site_link", GOT_SITE_LINK },
264 { "got_site_name", GOT_SITE_NAME },
265 { "got_site_owner", GOT_SITE_OWNER },
266 { "got_www_path", GOT_WWW_PATH },
267 };
268 const struct keywords *p;
270 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
271 sizeof(keywords[0]), kw_cmp);
273 if (p)
274 return (p->k_val);
275 else
276 return (STRING);
279 #define START_EXPAND 1
280 #define DONE_EXPAND 2
282 static int expanding;
284 int
285 igetc(void)
287 int c;
289 while (1) {
290 if (file->ungetpos > 0)
291 c = file->ungetbuf[--file->ungetpos];
292 else
293 c = getc(file->stream);
295 if (c == START_EXPAND)
296 expanding = 1;
297 else if (c == DONE_EXPAND)
298 expanding = 0;
299 else
300 break;
302 return (c);
305 int
306 lgetc(int quotec)
308 int c, next;
310 if (quotec) {
311 if ((c = igetc()) == EOF) {
312 yyerror("reached end of file while parsing "
313 "quoted string");
314 if (file == topfile || popfile() == EOF)
315 return (EOF);
316 return (quotec);
318 return (c);
321 while ((c = igetc()) == '\\') {
322 next = igetc();
323 if (next != '\n') {
324 c = next;
325 break;
327 yylval.lineno = file->lineno;
328 file->lineno++;
331 if (c == EOF) {
332 /*
333 * Fake EOL when hit EOF for the first time. This gets line
334 * count right if last line in included file is syntactically
335 * invalid and has no newline.
336 */
337 if (file->eof_reached == 0) {
338 file->eof_reached = 1;
339 return ('\n');
341 while (c == EOF) {
342 if (file == topfile || popfile() == EOF)
343 return (EOF);
344 c = igetc();
347 return (c);
350 void
351 lungetc(int c)
353 if (c == EOF)
354 return;
356 if (file->ungetpos >= file->ungetsize) {
357 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
358 if (p == NULL)
359 err(1, "%s", __func__);
360 file->ungetbuf = p;
361 file->ungetsize *= 2;
363 file->ungetbuf[file->ungetpos++] = c;
366 int
367 findeol(void)
369 int c;
371 /* Skip to either EOF or the first real EOL. */
372 while (1) {
373 c = lgetc(0);
374 if (c == '\n') {
375 file->lineno++;
376 break;
378 if (c == EOF)
379 break;
381 return (ERROR);
384 int
385 yylex(void)
387 unsigned char buf[8096];
388 unsigned char *p, *val;
389 int quotec, next, c;
390 int token;
392 top:
393 p = buf;
394 while ((c = lgetc(0)) == ' ' || c == '\t')
395 ; /* nothing */
397 yylval.lineno = file->lineno;
398 if (c == '#')
399 while ((c = lgetc(0)) != '\n' && c != EOF)
400 ; /* nothing */
401 if (c == '$' && !expanding) {
402 while (1) {
403 if ((c = lgetc(0)) == EOF)
404 return (0);
406 if (p + 1 >= buf + sizeof(buf) - 1) {
407 yyerror("string too long");
408 return (findeol());
410 if (isalnum(c) || c == '_') {
411 *p++ = c;
412 continue;
414 *p = '\0';
415 lungetc(c);
416 break;
418 val = symget(buf);
419 if (val == NULL) {
420 yyerror("macro '%s' not defined", buf);
421 return (findeol());
423 p = val + strlen(val) - 1;
424 lungetc(DONE_EXPAND);
425 while (p >= val) {
426 lungetc(*p);
427 p--;
429 lungetc(START_EXPAND);
430 goto top;
433 switch (c) {
434 case '\'':
435 case '"':
436 quotec = c;
437 while (1) {
438 if ((c = lgetc(quotec)) == EOF)
439 return (0);
440 if (c == '\n') {
441 file->lineno++;
442 continue;
443 } else if (c == '\\') {
444 if ((next = lgetc(quotec)) == EOF)
445 return (0);
446 if (next == quotec || c == ' ' || c == '\t')
447 c = next;
448 else if (next == '\n') {
449 file->lineno++;
450 continue;
451 } else
452 lungetc(next);
453 } else if (c == quotec) {
454 *p = '\0';
455 break;
456 } else if (c == '\0') {
457 yyerror("syntax error");
458 return (findeol());
460 if (p + 1 >= buf + sizeof(buf) - 1) {
461 yyerror("string too long");
462 return (findeol());
464 *p++ = c;
466 yylval.v.string = strdup(buf);
467 if (yylval.v.string == NULL)
468 err(1, "%s", __func__);
469 return (STRING);
472 #define allowed_to_end_number(x) \
473 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
475 if (c == '-' || isdigit(c)) {
476 do {
477 *p++ = c;
478 if ((unsigned)(p-buf) >= sizeof(buf)) {
479 yyerror("string too long");
480 return (findeol());
482 } while ((c = lgetc(0)) != EOF && isdigit(c));
483 lungetc(c);
484 if (p == buf + 1 && buf[0] == '-')
485 goto nodigits;
486 if (c == EOF || allowed_to_end_number(c)) {
487 const char *errstr = NULL;
489 *p = '\0';
490 yylval.v.number = strtonum(buf, LLONG_MIN,
491 LLONG_MAX, &errstr);
492 if (errstr) {
493 yyerror("\"%s\" invalid number: %s",
494 buf, errstr);
495 return (findeol());
497 return (NUMBER);
498 } else {
499 nodigits:
500 while (p > buf + 1)
501 lungetc(*--p);
502 c = *--p;
503 if (c == '-')
504 return (c);
508 #define allowed_in_string(x) \
509 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
510 x != '{' && x != '}' && \
511 x != '!' && x != '=' && x != '#' && \
512 x != ','))
514 if (isalnum(c) || c == ':' || c == '_') {
515 do {
516 *p++ = c;
517 if ((unsigned)(p-buf) >= sizeof(buf)) {
518 yyerror("string too long");
519 return (findeol());
521 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
522 lungetc(c);
523 *p = '\0';
524 if ((token = lookup(buf)) == STRING)
525 if ((yylval.v.string = strdup(buf)) == NULL)
526 err(1, "%s", __func__);
527 return (token);
529 if (c == '\n') {
530 yylval.lineno = file->lineno;
531 file->lineno++;
533 if (c == EOF)
534 return (0);
535 return (c);
538 static const struct got_error*
539 pushfile(struct file **nfile, const char *name)
541 const struct got_error* error = NULL;
543 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
544 return got_error_from_errno2(__func__, "calloc");
545 if (((*nfile)->name = strdup(name)) == NULL) {
546 free(nfile);
547 return got_error_from_errno2(__func__, "strdup");
549 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
550 char *msg = NULL;
551 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
552 return got_error_from_errno("asprintf");
553 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
554 free((*nfile)->name);
555 free((*nfile));
556 free(msg);
557 return error;
559 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
560 (*nfile)->ungetsize = 16;
561 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
562 if ((*nfile)->ungetbuf == NULL) {
563 fclose((*nfile)->stream);
564 free((*nfile)->name);
565 free((*nfile));
566 return got_error_from_errno2(__func__, "malloc");
568 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
569 return error;
572 int
573 popfile(void)
575 struct file *prev = NULL;
577 TAILQ_REMOVE(&files, file, entry);
578 fclose(file->stream);
579 free(file->name);
580 free(file->ungetbuf);
581 free(file);
582 file = prev;
583 return (file ? 0 : EOF);
586 const struct got_error*
587 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
589 gw_conf = malloc(sizeof(struct gotweb_config));
590 if (gw_conf == NULL) {
591 gerror = got_error_from_errno("malloc");
592 goto done;
594 gw_conf->got_repos_path = strdup(D_GOTPATH);
595 if (gw_conf->got_repos_path == NULL) {
596 gerror = got_error_from_errno("strdup");
597 goto done;
599 gw_conf->got_www_path = strdup(D_GOTWWW);
600 if (gw_conf->got_www_path == NULL) {
601 gerror = got_error_from_errno("strdup");
602 goto done;
604 gw_conf->got_site_name = strdup(D_SITENAME);
605 if (gw_conf->got_site_name == NULL) {
606 gerror = got_error_from_errno("strdup");
607 goto done;
609 gw_conf->got_site_owner = strdup(D_SITEOWNER);
610 if (gw_conf->got_site_owner == NULL) {
611 gerror = got_error_from_errno("strdup");
612 goto done;
614 gw_conf->got_site_link = strdup(D_SITELINK);
615 if (gw_conf->got_site_link == NULL) {
616 gerror = got_error_from_errno("strdup");
617 goto done;
619 gw_conf->got_logo = strdup(D_GOTLOGO);
620 if (gw_conf->got_logo == NULL) {
621 gerror = got_error_from_errno("strdup");
622 goto done;
624 gw_conf->got_logo_url = strdup(D_GOTURL);
625 if (gw_conf->got_logo_url == NULL) {
626 gerror = got_error_from_errno("strdup");
627 goto done;
629 gw_conf->got_show_site_owner = D_SHOWSOWNER;
630 gw_conf->got_show_repo_owner = D_SHOWROWNER;
631 gw_conf->got_show_repo_age = D_SHOWAGE;
632 gw_conf->got_show_repo_description = D_SHOWDESC;
633 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
634 gw_conf->got_max_repos = D_MAXREPO;
635 gw_conf->got_max_repos_display = D_MAXREPODISP;
636 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
638 /*
639 * We don't require that the gotweb config file exists
640 * So reset gerror if it doesn't exist and goto done.
641 */
642 gerror = pushfile(&file, filename);
643 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
644 gerror = NULL;
645 goto done;
646 } else if (gerror)
647 return gerror;
648 topfile = file;
650 yyparse();
651 popfile();
652 done:
653 *gconf = gw_conf;
654 return gerror;
657 int
658 symset(const char *nam, const char *val, int persist)
660 struct sym *sym;
662 TAILQ_FOREACH(sym, &symhead, entry) {
663 if (strcmp(nam, sym->nam) == 0)
664 break;
667 if (sym != NULL) {
668 if (sym->persist == 1)
669 return (0);
670 else {
671 free(sym->nam);
672 free(sym->val);
673 TAILQ_REMOVE(&symhead, sym, entry);
674 free(sym);
677 if ((sym = calloc(1, sizeof(*sym))) == NULL)
678 return (-1);
680 sym->nam = strdup(nam);
681 if (sym->nam == NULL) {
682 free(sym);
683 return (-1);
685 sym->val = strdup(val);
686 if (sym->val == NULL) {
687 free(sym->nam);
688 free(sym);
689 return (-1);
691 sym->used = 0;
692 sym->persist = persist;
693 TAILQ_INSERT_TAIL(&symhead, sym, entry);
694 return (0);
697 int
698 cmdline_symset(char *s)
700 char *sym, *val;
701 int ret;
702 size_t len;
704 if ((val = strrchr(s, '=')) == NULL)
705 return (-1);
707 len = strlen(s) - strlen(val) + 1;
708 if ((sym = malloc(len)) == NULL)
709 errx(1, "cmdline_symset: malloc");
711 strlcpy(sym, s, len);
713 ret = symset(sym, val + 1, 1);
714 free(sym);
716 return (ret);
719 char *
720 symget(const char *nam)
722 struct sym *sym;
724 TAILQ_FOREACH(sym, &symhead, entry) {
725 if (strcmp(nam, sym->nam) == 0) {
726 sym->used = 1;
727 return (sym->val);
730 return (NULL);