portable: add back sys/queue.h
[got-portable.git] / gotweb / parse.y
blob6676eb0a0649ddeb36686b874f1c193eb6fead0a
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.
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/queue.h>
28 #include <ctype.h>
29 #include <err.h>
30 #include <limits.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include "got_error.h"
37 #include "gotweb.h"
39 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
40 static struct file {
41 TAILQ_ENTRY(file) entry;
42 FILE *stream;
43 char *name;
44 size_t ungetpos;
45 size_t ungetsize;
46 u_char *ungetbuf;
47 int eof_reached;
48 int lineno;
49 } *file, *topfile;
50 static const struct got_error* pushfile(struct file**, const char *);
51 int popfile(void);
52 int yyparse(void);
53 int yylex(void);
54 int yyerror(const char *, ...)
55 __attribute__((__format__ (printf, 1, 2)))
56 __attribute__((__nonnull__ (1)));
57 int kw_cmp(const void *, const void *);
58 int lookup(char *);
59 int igetc(void);
60 int lgetc(int);
61 void lungetc(int);
62 int findeol(void);
64 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
65 struct sym {
66 TAILQ_ENTRY(sym) entry;
67 int used;
68 int persist;
69 char *nam;
70 char *val;
73 int symset(const char *, const char *, int);
74 int cmdline_symset(char *);
75 char *symget(const char *);
77 const struct got_error* gerror = NULL;
78 struct gotweb_config *gw_conf;
80 typedef struct {
81 union {
82 int64_t number;
83 char *string;
84 } v;
85 int lineno;
86 } YYSTYPE;
90 %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
91 %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
92 %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
93 %token GOT_MAX_COMMITS_DISPLAY ERROR GOT_SHOW_SITE_OWNER
94 %token GOT_SHOW_REPO_CLONEURL
95 %token <v.string> STRING
96 %token <v.number> NUMBER
97 %type <v.number> boolean
100 grammar : /* empty */
101 | grammar '\n'
102 | grammar main '\n'
105 boolean : STRING {
106 if (strcasecmp($1, "true") == 0 ||
107 strcasecmp($1, "on") == 0 ||
108 strcasecmp($1, "yes") == 0)
109 $$ = 1;
110 else if (strcasecmp($1, "false") == 0 ||
111 strcasecmp($1, "off") == 0 ||
112 strcasecmp($1, "no") == 0)
113 $$ = 0;
114 else {
115 yyerror("invalid boolean value '%s'", $1);
116 free($1);
117 YYERROR;
119 free($1);
122 main : GOT_REPOS_PATH STRING {
123 gw_conf->got_repos_path = $2;
125 | GOT_WWW_PATH STRING {
126 gw_conf->got_www_path = $2;
128 | GOT_MAX_REPOS NUMBER {
129 if ($2 > 0)
130 gw_conf->got_max_repos = $2;
132 | GOT_SITE_NAME STRING {
133 gw_conf->got_site_name = $2;
135 | GOT_SITE_OWNER STRING {
136 gw_conf->got_site_owner = $2;
138 | GOT_SITE_LINK STRING {
139 gw_conf->got_site_link = $2;
141 | GOT_LOGO STRING {
142 gw_conf->got_logo = $2;
144 | GOT_LOGO_URL STRING {
145 gw_conf->got_logo_url = $2;
147 | GOT_SHOW_SITE_OWNER boolean {
148 gw_conf->got_show_site_owner = $2;
150 | GOT_SHOW_REPO_OWNER boolean {
151 gw_conf->got_show_repo_owner = $2;
153 | GOT_SHOW_REPO_AGE boolean {
154 gw_conf->got_show_repo_age = $2;
156 | GOT_SHOW_REPO_DESCRIPTION boolean {
157 gw_conf->got_show_repo_description = $2;
159 | GOT_SHOW_REPO_CLONEURL boolean {
160 gw_conf->got_show_repo_cloneurl = $2;
162 | GOT_MAX_REPOS_DISPLAY NUMBER {
163 if ($2 > 0)
164 gw_conf->got_max_repos_display = $2;
166 | GOT_MAX_COMMITS_DISPLAY NUMBER {
167 if ($2 > 0)
168 gw_conf->got_max_commits_display = $2;
173 struct keywords {
174 const char *k_name;
175 int k_val;
179 yyerror(const char *fmt, ...)
181 va_list ap;
182 char *msg;
183 char *err = NULL;
185 va_start(ap, fmt);
186 if (vasprintf(&msg, fmt, ap) == -1) {
187 gerror = got_error_from_errno("vasprintf");
188 return 0;
190 va_end(ap);
191 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
192 gerror = got_error_from_errno("asprintf");
193 return(0);
195 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
196 free(msg);
197 return(0);
201 kw_cmp(const void *k, const void *e)
203 return (strcmp(k, ((const struct keywords *)e)->k_name));
207 lookup(char *s)
209 /* This has to be sorted always. */
210 static const struct keywords keywords[] = {
211 { "got_logo", GOT_LOGO },
212 { "got_logo_url", GOT_LOGO_URL },
213 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
214 { "got_max_repos", GOT_MAX_REPOS },
215 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
216 { "got_repos_path", GOT_REPOS_PATH },
217 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
218 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
219 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
220 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
221 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
222 { "got_site_link", GOT_SITE_LINK },
223 { "got_site_name", GOT_SITE_NAME },
224 { "got_site_owner", GOT_SITE_OWNER },
225 { "got_www_path", GOT_WWW_PATH },
227 const struct keywords *p;
229 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
230 sizeof(keywords[0]), kw_cmp);
232 if (p)
233 return (p->k_val);
234 else
235 return (STRING);
238 #define START_EXPAND 1
239 #define DONE_EXPAND 2
241 static int expanding;
244 igetc(void)
246 int c;
248 while (1) {
249 if (file->ungetpos > 0)
250 c = file->ungetbuf[--file->ungetpos];
251 else
252 c = getc(file->stream);
254 if (c == START_EXPAND)
255 expanding = 1;
256 else if (c == DONE_EXPAND)
257 expanding = 0;
258 else
259 break;
261 return (c);
265 lgetc(int quotec)
267 int c, next;
269 if (quotec) {
270 if ((c = igetc()) == EOF) {
271 yyerror("reached end of file while parsing "
272 "quoted string");
273 if (file == topfile || popfile() == EOF)
274 return (EOF);
275 return (quotec);
277 return (c);
280 while ((c = igetc()) == '\\') {
281 next = igetc();
282 if (next != '\n') {
283 c = next;
284 break;
286 yylval.lineno = file->lineno;
287 file->lineno++;
290 if (c == EOF) {
292 * Fake EOL when hit EOF for the first time. This gets line
293 * count right if last line in included file is syntactically
294 * invalid and has no newline.
296 if (file->eof_reached == 0) {
297 file->eof_reached = 1;
298 return ('\n');
300 while (c == EOF) {
301 if (file == topfile || popfile() == EOF)
302 return (EOF);
303 c = igetc();
306 return (c);
309 void
310 lungetc(int c)
312 if (c == EOF)
313 return;
315 if (file->ungetpos >= file->ungetsize) {
316 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
317 if (p == NULL)
318 err(1, "%s", __func__);
319 file->ungetbuf = p;
320 file->ungetsize *= 2;
322 file->ungetbuf[file->ungetpos++] = c;
326 findeol(void)
328 int c;
330 /* Skip to either EOF or the first real EOL. */
331 while (1) {
332 c = lgetc(0);
333 if (c == '\n') {
334 file->lineno++;
335 break;
337 if (c == EOF)
338 break;
340 return (ERROR);
344 yylex(void)
346 char buf[8096];
347 char *p, *val;
348 int quotec, next, c;
349 int token;
351 top:
352 p = buf;
353 while ((c = lgetc(0)) == ' ' || c == '\t')
354 ; /* nothing */
356 yylval.lineno = file->lineno;
357 if (c == '#')
358 while ((c = lgetc(0)) != '\n' && c != EOF)
359 ; /* nothing */
360 if (c == '$' && !expanding) {
361 while (1) {
362 if ((c = lgetc(0)) == EOF)
363 return (0);
365 if (p + 1 >= buf + sizeof(buf) - 1) {
366 yyerror("string too long");
367 return (findeol());
369 if (isalnum(c) || c == '_') {
370 *p++ = c;
371 continue;
373 *p = '\0';
374 lungetc(c);
375 break;
377 val = symget(buf);
378 if (val == NULL) {
379 yyerror("macro '%s' not defined", buf);
380 return (findeol());
382 p = val + strlen(val) - 1;
383 lungetc(DONE_EXPAND);
384 while (p >= val) {
385 lungetc((unsigned char)*p);
386 p--;
388 lungetc(START_EXPAND);
389 goto top;
392 switch (c) {
393 case '\'':
394 case '"':
395 quotec = c;
396 while (1) {
397 if ((c = lgetc(quotec)) == EOF)
398 return (0);
399 if (c == '\n') {
400 file->lineno++;
401 continue;
402 } else if (c == '\\') {
403 if ((next = lgetc(quotec)) == EOF)
404 return (0);
405 if (next == quotec || c == ' ' || c == '\t')
406 c = next;
407 else if (next == '\n') {
408 file->lineno++;
409 continue;
410 } else
411 lungetc(next);
412 } else if (c == quotec) {
413 *p = '\0';
414 break;
415 } else if (c == '\0') {
416 yyerror("syntax error");
417 return (findeol());
419 if (p + 1 >= buf + sizeof(buf) - 1) {
420 yyerror("string too long");
421 return (findeol());
423 *p++ = c;
425 yylval.v.string = strdup(buf);
426 if (yylval.v.string == NULL)
427 err(1, "%s", __func__);
428 return (STRING);
431 #define allowed_to_end_number(x) \
432 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
434 if (c == '-' || isdigit(c)) {
435 do {
436 *p++ = c;
437 if ((size_t)(p-buf) >= sizeof(buf)) {
438 yyerror("string too long");
439 return (findeol());
441 } while ((c = lgetc(0)) != EOF && isdigit(c));
442 lungetc(c);
443 if (p == buf + 1 && buf[0] == '-')
444 goto nodigits;
445 if (c == EOF || allowed_to_end_number(c)) {
446 const char *errstr = NULL;
448 *p = '\0';
449 yylval.v.number = strtonum(buf, LLONG_MIN,
450 LLONG_MAX, &errstr);
451 if (errstr) {
452 yyerror("\"%s\" invalid number: %s",
453 buf, errstr);
454 return (findeol());
456 return (NUMBER);
457 } else {
458 nodigits:
459 while (p > buf + 1)
460 lungetc((unsigned char)*--p);
461 c = (unsigned char)*--p;
462 if (c == '-')
463 return (c);
467 #define allowed_in_string(x) \
468 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
469 x != '{' && x != '}' && \
470 x != '!' && x != '=' && x != '#' && \
471 x != ','))
473 if (isalnum(c) || c == ':' || c == '_') {
474 do {
475 *p++ = c;
476 if ((size_t)(p-buf) >= sizeof(buf)) {
477 yyerror("string too long");
478 return (findeol());
480 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
481 lungetc(c);
482 *p = '\0';
483 if ((token = lookup(buf)) == STRING)
484 if ((yylval.v.string = strdup(buf)) == NULL)
485 err(1, "%s", __func__);
486 return (token);
488 if (c == '\n') {
489 yylval.lineno = file->lineno;
490 file->lineno++;
492 if (c == EOF)
493 return (0);
494 return (c);
497 static const struct got_error*
498 pushfile(struct file **nfile, const char *name)
500 const struct got_error* error = NULL;
502 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
503 return got_error_from_errno2(__func__, "calloc");
504 if (((*nfile)->name = strdup(name)) == NULL) {
505 free(nfile);
506 return got_error_from_errno2(__func__, "strdup");
508 if (((*nfile)->stream = fopen((*nfile)->name, "re")) == NULL) {
509 char *msg = NULL;
510 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
511 return got_error_from_errno("asprintf");
512 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
513 free((*nfile)->name);
514 free((*nfile));
515 free(msg);
516 return error;
518 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
519 (*nfile)->ungetsize = 16;
520 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
521 if ((*nfile)->ungetbuf == NULL) {
522 fclose((*nfile)->stream);
523 free((*nfile)->name);
524 free((*nfile));
525 return got_error_from_errno2(__func__, "malloc");
527 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
528 return error;
532 popfile(void)
534 struct file *prev = NULL;
536 TAILQ_REMOVE(&files, file, entry);
537 fclose(file->stream);
538 free(file->name);
539 free(file->ungetbuf);
540 free(file);
541 file = prev;
542 return (file ? 0 : EOF);
545 const struct got_error*
546 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
548 gw_conf = malloc(sizeof(struct gotweb_config));
549 if (gw_conf == NULL) {
550 gerror = got_error_from_errno("malloc");
551 goto done;
553 gw_conf->got_repos_path = strdup(D_GOTPATH);
554 if (gw_conf->got_repos_path == NULL) {
555 gerror = got_error_from_errno("strdup");
556 goto done;
558 gw_conf->got_www_path = strdup(D_GOTWWW);
559 if (gw_conf->got_www_path == NULL) {
560 gerror = got_error_from_errno("strdup");
561 goto done;
563 gw_conf->got_site_name = strdup(D_SITENAME);
564 if (gw_conf->got_site_name == NULL) {
565 gerror = got_error_from_errno("strdup");
566 goto done;
568 gw_conf->got_site_owner = strdup(D_SITEOWNER);
569 if (gw_conf->got_site_owner == NULL) {
570 gerror = got_error_from_errno("strdup");
571 goto done;
573 gw_conf->got_site_link = strdup(D_SITELINK);
574 if (gw_conf->got_site_link == NULL) {
575 gerror = got_error_from_errno("strdup");
576 goto done;
578 gw_conf->got_logo = strdup(D_GOTLOGO);
579 if (gw_conf->got_logo == NULL) {
580 gerror = got_error_from_errno("strdup");
581 goto done;
583 gw_conf->got_logo_url = strdup(D_GOTURL);
584 if (gw_conf->got_logo_url == NULL) {
585 gerror = got_error_from_errno("strdup");
586 goto done;
588 gw_conf->got_show_site_owner = D_SHOWSOWNER;
589 gw_conf->got_show_repo_owner = D_SHOWROWNER;
590 gw_conf->got_show_repo_age = D_SHOWAGE;
591 gw_conf->got_show_repo_description = D_SHOWDESC;
592 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
593 gw_conf->got_max_repos = D_MAXREPO;
594 gw_conf->got_max_repos_display = D_MAXREPODISP;
595 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
598 * We don't require that the gotweb config file exists
599 * So reset gerror if it doesn't exist and goto done.
601 gerror = pushfile(&file, filename);
602 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
603 gerror = NULL;
604 goto done;
605 } else if (gerror)
606 return gerror;
607 topfile = file;
609 yyparse();
610 popfile();
611 done:
612 *gconf = gw_conf;
613 return gerror;
617 symset(const char *nam, const char *val, int persist)
619 struct sym *sym;
621 TAILQ_FOREACH(sym, &symhead, entry) {
622 if (strcmp(nam, sym->nam) == 0)
623 break;
626 if (sym != NULL) {
627 if (sym->persist == 1)
628 return (0);
629 else {
630 free(sym->nam);
631 free(sym->val);
632 TAILQ_REMOVE(&symhead, sym, entry);
633 free(sym);
636 if ((sym = calloc(1, sizeof(*sym))) == NULL)
637 return (-1);
639 sym->nam = strdup(nam);
640 if (sym->nam == NULL) {
641 free(sym);
642 return (-1);
644 sym->val = strdup(val);
645 if (sym->val == NULL) {
646 free(sym->nam);
647 free(sym);
648 return (-1);
650 sym->used = 0;
651 sym->persist = persist;
652 TAILQ_INSERT_TAIL(&symhead, sym, entry);
653 return (0);
657 cmdline_symset(char *s)
659 char *sym, *val;
660 int ret;
661 size_t len;
663 if ((val = strrchr(s, '=')) == NULL)
664 return (-1);
666 len = strlen(s) - strlen(val) + 1;
667 if ((sym = malloc(len)) == NULL)
668 errx(1, "cmdline_symset: malloc");
670 strlcpy(sym, s, len);
672 ret = symset(sym, val + 1, 1);
673 free(sym);
675 return (ret);
678 char *
679 symget(const char *nam)
681 struct sym *sym;
683 TAILQ_FOREACH(sym, &symhead, entry) {
684 if (strcmp(nam, sym->nam) == 0) {
685 sym->used = 1;
686 return (sym->val);
689 return (NULL);