add a test for 'got merge' which covers a no-op merge situation
[got-portable.git] / gotweb / parse.y
blob91c2400e3b093930979f449682fcffa6fbf7f3f4
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>
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;
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;
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
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 = $2;
123 | GOT_WWW_PATH STRING {
124 gw_conf->got_www_path = $2;
126 | GOT_MAX_REPOS NUMBER {
127 if ($2 > 0)
128 gw_conf->got_max_repos = $2;
130 | GOT_SITE_NAME STRING {
131 gw_conf->got_site_name = $2;
133 | GOT_SITE_OWNER STRING {
134 gw_conf->got_site_owner = $2;
136 | GOT_SITE_LINK STRING {
137 gw_conf->got_site_link = $2;
139 | GOT_LOGO STRING {
140 gw_conf->got_logo = $2;
142 | GOT_LOGO_URL STRING {
143 gw_conf->got_logo_url = $2;
145 | GOT_SHOW_SITE_OWNER boolean {
146 gw_conf->got_show_site_owner = $2;
148 | GOT_SHOW_REPO_OWNER boolean {
149 gw_conf->got_show_repo_owner = $2;
151 | GOT_SHOW_REPO_AGE boolean {
152 gw_conf->got_show_repo_age = $2;
154 | GOT_SHOW_REPO_DESCRIPTION boolean {
155 gw_conf->got_show_repo_description = $2;
157 | GOT_SHOW_REPO_CLONEURL boolean {
158 gw_conf->got_show_repo_cloneurl = $2;
160 | GOT_MAX_REPOS_DISPLAY NUMBER {
161 if ($2 > 0)
162 gw_conf->got_max_repos_display = $2;
164 | GOT_MAX_COMMITS_DISPLAY NUMBER {
165 if ($2 > 0)
166 gw_conf->got_max_commits_display = $2;
171 struct keywords {
172 const char *k_name;
173 int k_val;
177 yyerror(const char *fmt, ...)
179 va_list ap;
180 char *msg;
181 char *err = NULL;
183 va_start(ap, fmt);
184 if (vasprintf(&msg, fmt, ap) == -1) {
185 gerror = got_error_from_errno("vasprintf");
186 return 0;
188 va_end(ap);
189 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
190 gerror = got_error_from_errno("asprintf");
191 return(0);
193 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
194 free(msg);
195 return(0);
199 kw_cmp(const void *k, const void *e)
201 return (strcmp(k, ((const struct keywords *)e)->k_name));
205 lookup(char *s)
207 /* This has to be sorted always. */
208 static const struct keywords keywords[] = {
209 { "got_logo", GOT_LOGO },
210 { "got_logo_url", GOT_LOGO_URL },
211 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
212 { "got_max_repos", GOT_MAX_REPOS },
213 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
214 { "got_repos_path", GOT_REPOS_PATH },
215 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
216 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
217 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
218 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
219 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
220 { "got_site_link", GOT_SITE_LINK },
221 { "got_site_name", GOT_SITE_NAME },
222 { "got_site_owner", GOT_SITE_OWNER },
223 { "got_www_path", GOT_WWW_PATH },
225 const struct keywords *p;
227 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
228 sizeof(keywords[0]), kw_cmp);
230 if (p)
231 return (p->k_val);
232 else
233 return (STRING);
236 #define START_EXPAND 1
237 #define DONE_EXPAND 2
239 static int expanding;
242 igetc(void)
244 int c;
246 while (1) {
247 if (file->ungetpos > 0)
248 c = file->ungetbuf[--file->ungetpos];
249 else
250 c = getc(file->stream);
252 if (c == START_EXPAND)
253 expanding = 1;
254 else if (c == DONE_EXPAND)
255 expanding = 0;
256 else
257 break;
259 return (c);
263 lgetc(int quotec)
265 int c, next;
267 if (quotec) {
268 if ((c = igetc()) == EOF) {
269 yyerror("reached end of file while parsing "
270 "quoted string");
271 if (file == topfile || popfile() == EOF)
272 return (EOF);
273 return (quotec);
275 return (c);
278 while ((c = igetc()) == '\\') {
279 next = igetc();
280 if (next != '\n') {
281 c = next;
282 break;
284 yylval.lineno = file->lineno;
285 file->lineno++;
288 if (c == EOF) {
290 * Fake EOL when hit EOF for the first time. This gets line
291 * count right if last line in included file is syntactically
292 * invalid and has no newline.
294 if (file->eof_reached == 0) {
295 file->eof_reached = 1;
296 return ('\n');
298 while (c == EOF) {
299 if (file == topfile || popfile() == EOF)
300 return (EOF);
301 c = igetc();
304 return (c);
307 void
308 lungetc(int c)
310 if (c == EOF)
311 return;
313 if (file->ungetpos >= file->ungetsize) {
314 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
315 if (p == NULL)
316 err(1, "%s", __func__);
317 file->ungetbuf = p;
318 file->ungetsize *= 2;
320 file->ungetbuf[file->ungetpos++] = c;
324 findeol(void)
326 int c;
328 /* Skip to either EOF or the first real EOL. */
329 while (1) {
330 c = lgetc(0);
331 if (c == '\n') {
332 file->lineno++;
333 break;
335 if (c == EOF)
336 break;
338 return (ERROR);
342 yylex(void)
344 unsigned char buf[8096];
345 unsigned char *p, *val;
346 int quotec, next, c;
347 int token;
349 top:
350 p = buf;
351 while ((c = lgetc(0)) == ' ' || c == '\t')
352 ; /* nothing */
354 yylval.lineno = file->lineno;
355 if (c == '#')
356 while ((c = lgetc(0)) != '\n' && c != EOF)
357 ; /* nothing */
358 if (c == '$' && !expanding) {
359 while (1) {
360 if ((c = lgetc(0)) == EOF)
361 return (0);
363 if (p + 1 >= buf + sizeof(buf) - 1) {
364 yyerror("string too long");
365 return (findeol());
367 if (isalnum(c) || c == '_') {
368 *p++ = c;
369 continue;
371 *p = '\0';
372 lungetc(c);
373 break;
375 val = symget(buf);
376 if (val == NULL) {
377 yyerror("macro '%s' not defined", buf);
378 return (findeol());
380 p = val + strlen(val) - 1;
381 lungetc(DONE_EXPAND);
382 while (p >= val) {
383 lungetc(*p);
384 p--;
386 lungetc(START_EXPAND);
387 goto top;
390 switch (c) {
391 case '\'':
392 case '"':
393 quotec = c;
394 while (1) {
395 if ((c = lgetc(quotec)) == EOF)
396 return (0);
397 if (c == '\n') {
398 file->lineno++;
399 continue;
400 } else if (c == '\\') {
401 if ((next = lgetc(quotec)) == EOF)
402 return (0);
403 if (next == quotec || c == ' ' || c == '\t')
404 c = next;
405 else if (next == '\n') {
406 file->lineno++;
407 continue;
408 } else
409 lungetc(next);
410 } else if (c == quotec) {
411 *p = '\0';
412 break;
413 } else if (c == '\0') {
414 yyerror("syntax error");
415 return (findeol());
417 if (p + 1 >= buf + sizeof(buf) - 1) {
418 yyerror("string too long");
419 return (findeol());
421 *p++ = c;
423 yylval.v.string = strdup(buf);
424 if (yylval.v.string == NULL)
425 err(1, "%s", __func__);
426 return (STRING);
429 #define allowed_to_end_number(x) \
430 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
432 if (c == '-' || isdigit(c)) {
433 do {
434 *p++ = c;
435 if ((unsigned)(p-buf) >= sizeof(buf)) {
436 yyerror("string too long");
437 return (findeol());
439 } while ((c = lgetc(0)) != EOF && isdigit(c));
440 lungetc(c);
441 if (p == buf + 1 && buf[0] == '-')
442 goto nodigits;
443 if (c == EOF || allowed_to_end_number(c)) {
444 const char *errstr = NULL;
446 *p = '\0';
447 yylval.v.number = strtonum(buf, LLONG_MIN,
448 LLONG_MAX, &errstr);
449 if (errstr) {
450 yyerror("\"%s\" invalid number: %s",
451 buf, errstr);
452 return (findeol());
454 return (NUMBER);
455 } else {
456 nodigits:
457 while (p > buf + 1)
458 lungetc(*--p);
459 c = *--p;
460 if (c == '-')
461 return (c);
465 #define allowed_in_string(x) \
466 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
467 x != '{' && x != '}' && \
468 x != '!' && x != '=' && x != '#' && \
469 x != ','))
471 if (isalnum(c) || c == ':' || c == '_') {
472 do {
473 *p++ = c;
474 if ((unsigned)(p-buf) >= sizeof(buf)) {
475 yyerror("string too long");
476 return (findeol());
478 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
479 lungetc(c);
480 *p = '\0';
481 if ((token = lookup(buf)) == STRING)
482 if ((yylval.v.string = strdup(buf)) == NULL)
483 err(1, "%s", __func__);
484 return (token);
486 if (c == '\n') {
487 yylval.lineno = file->lineno;
488 file->lineno++;
490 if (c == EOF)
491 return (0);
492 return (c);
495 static const struct got_error*
496 pushfile(struct file **nfile, const char *name)
498 const struct got_error* error = NULL;
500 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
501 return got_error_from_errno2(__func__, "calloc");
502 if (((*nfile)->name = strdup(name)) == NULL) {
503 free(nfile);
504 return got_error_from_errno2(__func__, "strdup");
506 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
507 char *msg = NULL;
508 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
509 return got_error_from_errno("asprintf");
510 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
511 free((*nfile)->name);
512 free((*nfile));
513 free(msg);
514 return error;
516 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
517 (*nfile)->ungetsize = 16;
518 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
519 if ((*nfile)->ungetbuf == NULL) {
520 fclose((*nfile)->stream);
521 free((*nfile)->name);
522 free((*nfile));
523 return got_error_from_errno2(__func__, "malloc");
525 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
526 return error;
530 popfile(void)
532 struct file *prev = NULL;
534 TAILQ_REMOVE(&files, file, entry);
535 fclose(file->stream);
536 free(file->name);
537 free(file->ungetbuf);
538 free(file);
539 file = prev;
540 return (file ? 0 : EOF);
543 const struct got_error*
544 parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
546 gw_conf = malloc(sizeof(struct gotweb_config));
547 if (gw_conf == NULL) {
548 gerror = got_error_from_errno("malloc");
549 goto done;
551 gw_conf->got_repos_path = strdup(D_GOTPATH);
552 if (gw_conf->got_repos_path == NULL) {
553 gerror = got_error_from_errno("strdup");
554 goto done;
556 gw_conf->got_www_path = strdup(D_GOTWWW);
557 if (gw_conf->got_www_path == NULL) {
558 gerror = got_error_from_errno("strdup");
559 goto done;
561 gw_conf->got_site_name = strdup(D_SITENAME);
562 if (gw_conf->got_site_name == NULL) {
563 gerror = got_error_from_errno("strdup");
564 goto done;
566 gw_conf->got_site_owner = strdup(D_SITEOWNER);
567 if (gw_conf->got_site_owner == NULL) {
568 gerror = got_error_from_errno("strdup");
569 goto done;
571 gw_conf->got_site_link = strdup(D_SITELINK);
572 if (gw_conf->got_site_link == NULL) {
573 gerror = got_error_from_errno("strdup");
574 goto done;
576 gw_conf->got_logo = strdup(D_GOTLOGO);
577 if (gw_conf->got_logo == NULL) {
578 gerror = got_error_from_errno("strdup");
579 goto done;
581 gw_conf->got_logo_url = strdup(D_GOTURL);
582 if (gw_conf->got_logo_url == NULL) {
583 gerror = got_error_from_errno("strdup");
584 goto done;
586 gw_conf->got_show_site_owner = D_SHOWSOWNER;
587 gw_conf->got_show_repo_owner = D_SHOWROWNER;
588 gw_conf->got_show_repo_age = D_SHOWAGE;
589 gw_conf->got_show_repo_description = D_SHOWDESC;
590 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
591 gw_conf->got_max_repos = D_MAXREPO;
592 gw_conf->got_max_repos_display = D_MAXREPODISP;
593 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
596 * We don't require that the gotweb config file exists
597 * So reset gerror if it doesn't exist and goto done.
599 gerror = pushfile(&file, filename);
600 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
601 gerror = NULL;
602 goto done;
603 } else if (gerror)
604 return gerror;
605 topfile = file;
607 yyparse();
608 popfile();
609 done:
610 *gconf = gw_conf;
611 return gerror;
615 symset(const char *nam, const char *val, int persist)
617 struct sym *sym;
619 TAILQ_FOREACH(sym, &symhead, entry) {
620 if (strcmp(nam, sym->nam) == 0)
621 break;
624 if (sym != NULL) {
625 if (sym->persist == 1)
626 return (0);
627 else {
628 free(sym->nam);
629 free(sym->val);
630 TAILQ_REMOVE(&symhead, sym, entry);
631 free(sym);
634 if ((sym = calloc(1, sizeof(*sym))) == NULL)
635 return (-1);
637 sym->nam = strdup(nam);
638 if (sym->nam == NULL) {
639 free(sym);
640 return (-1);
642 sym->val = strdup(val);
643 if (sym->val == NULL) {
644 free(sym->nam);
645 free(sym);
646 return (-1);
648 sym->used = 0;
649 sym->persist = persist;
650 TAILQ_INSERT_TAIL(&symhead, sym, entry);
651 return (0);
655 cmdline_symset(char *s)
657 char *sym, *val;
658 int ret;
659 size_t len;
661 if ((val = strrchr(s, '=')) == NULL)
662 return (-1);
664 len = strlen(s) - strlen(val) + 1;
665 if ((sym = malloc(len)) == NULL)
666 errx(1, "cmdline_symset: malloc");
668 strlcpy(sym, s, len);
670 ret = symset(sym, val + 1, 1);
671 free(sym);
673 return (ret);
676 char *
677 symget(const char *nam)
679 struct sym *sym;
681 TAILQ_FOREACH(sym, &symhead, entry) {
682 if (strcmp(nam, sym->nam) == 0) {
683 sym->used = 1;
684 return (sym->val);
687 return (NULL);