Blob


1 /*
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.
7 *
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.
11 *
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.
19 */
21 %{
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <ctype.h>
26 #include <err.h>
27 #include <limits.h>
28 #include <stdarg.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "gotweb.h"
36 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
37 static struct file {
38 TAILQ_ENTRY(file) entry;
39 FILE *stream;
40 char *name;
41 int lineno;
42 int errors;
43 const struct got_error* error;
44 } *file, *topfile;
45 struct file *pushfile(const char *);
46 int popfile(void);
47 int yyparse(void);
48 int yylex(void);
49 int yyerror(const char *, ...)
50 __attribute__((__format__ (printf, 1, 2)))
51 __attribute__((__nonnull__ (1)));
52 int kw_cmp(const void *, const void *);
53 int lookup(char *);
54 int lgetc(int);
55 int lungetc(int);
56 int findeol(void);
58 static const struct got_error* gerror = NULL;
59 char *syn_err;
61 struct gotweb_conf *gw_conf;
63 typedef struct {
64 union {
65 int64_t number;
66 char *string;
67 } v;
68 int lineno;
69 } YYSTYPE;
71 %}
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
81 %%
83 grammar : /* empty */
84 | grammar '\n'
85 | grammar main '\n'
86 | grammar error '\n' { file->errors++; }
87 ;
89 boolean : STRING {
90 if (strcasecmp($1, "true") == 0 ||
91 strcasecmp($1, "yes") == 0)
92 $$ = 1;
93 else if (strcasecmp($1, "false") == 0 ||
94 strcasecmp($1, "off") == 0 ||
95 strcasecmp($1, "no") == 0)
96 $$ = 0;
97 else {
98 yyerror("invalid boolean value '%s'", $1);
99 free($1);
100 YYERROR;
102 free($1);
104 | ON { $$ = 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_MAX_REPOS NUMBER {
112 if ($2 > 0)
113 gw_conf->got_max_repos = $2;
115 | GOT_SITE_NAME STRING {
116 if ((gw_conf->got_site_name = strdup($2)) == NULL)
117 errx(1, "out of memory");
119 | GOT_SITE_OWNER STRING {
120 if ((gw_conf->got_site_owner = strdup($2)) == NULL)
121 errx(1, "out of memory");
123 | GOT_SITE_LINK STRING {
124 if ((gw_conf->got_site_link = strdup($2)) == NULL)
125 errx(1, "out of memory");
127 | GOT_LOGO STRING {
128 if ((gw_conf->got_logo = strdup($2)) == NULL)
129 errx(1, "out of memory");
131 | GOT_LOGO_URL STRING {
132 if ((gw_conf->got_logo_url = strdup($2)) == NULL)
133 errx(1, "out of memory");
135 | GOT_SHOW_SITE_OWNER boolean {
136 gw_conf->got_show_site_owner = $2;
138 | GOT_SHOW_REPO_OWNER boolean {
139 gw_conf->got_show_repo_owner = $2;
141 | GOT_SHOW_REPO_AGE boolean { gw_conf->got_show_repo_age = $2; }
142 | GOT_SHOW_REPO_DESCRIPTION boolean {
143 gw_conf->got_show_repo_description = $2;
145 | GOT_SHOW_REPO_CLONEURL boolean {
146 gw_conf->got_show_repo_cloneurl = $2;
148 | GOT_MAX_REPOS_DISPLAY NUMBER {
149 if ($2 > 0)
150 gw_conf->got_max_repos_display = $2;
152 | GOT_MAX_COMMITS_DISPLAY NUMBER {
153 if ($2 > 0)
154 gw_conf->got_max_commits_display = $2;
158 %%
160 struct keywords {
161 const char *k_name;
162 int k_val;
163 };
165 int
166 yyerror(const char *fmt, ...)
168 va_list ap;
169 char *msg = NULL;
170 static char err_msg[512];
172 file->errors++;
173 va_start(ap, fmt);
174 if (vasprintf(&msg, fmt, ap) == -1)
175 errx(1, "yyerror vasprintf");
176 va_end(ap);
177 snprintf(err_msg, sizeof(err_msg), "%s:%d: %s", file->name,
178 yylval.lineno, msg);
179 gerror = got_error_from_errno2("parse_error", err_msg);
181 free(msg);
182 return (0);
185 int
186 kw_cmp(const void *k, const void *e)
188 return (strcmp(k, ((const struct keywords *)e)->k_name));
191 int
192 lookup(char *s)
194 /* this has to be sorted always */
195 static const struct keywords keywords[] = {
196 { "got_logo", GOT_LOGO },
197 { "got_logo_url", GOT_LOGO_URL },
198 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
199 { "got_max_repos", GOT_MAX_REPOS },
200 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
201 { "got_repos_path", GOT_REPOS_PATH },
202 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
203 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
204 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
205 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
206 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
207 { "got_site_link", GOT_SITE_LINK },
208 { "got_site_name", GOT_SITE_NAME },
209 { "got_site_owner", GOT_SITE_OWNER },
210 };
211 const struct keywords *p;
213 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
214 sizeof(keywords[0]), kw_cmp);
216 if (p)
217 return (p->k_val);
218 else
219 return (STRING);
222 #define MAXPUSHBACK 128
224 u_char *parsebuf;
225 int parseindex;
226 u_char pushback_buffer[MAXPUSHBACK];
227 int pushback_index = 0;
229 int
230 lgetc(int quotec)
232 int c, next;
234 if (parsebuf) {
235 /* Read character from the parsebuffer instead of input. */
236 if (parseindex >= 0) {
237 c = parsebuf[parseindex++];
238 if (c != '\0')
239 return (c);
240 parsebuf = NULL;
241 } else
242 parseindex++;
245 if (pushback_index)
246 return (pushback_buffer[--pushback_index]);
248 if (quotec) {
249 if ((c = getc(file->stream)) == EOF) {
250 yyerror("reached end of file while parsing "
251 "quoted string");
252 if (file == topfile || popfile() == EOF)
253 return (EOF);
254 return (quotec);
256 return (c);
259 while ((c = getc(file->stream)) == '\\') {
260 next = getc(file->stream);
261 if (next != '\n') {
262 c = next;
263 break;
265 yylval.lineno = file->lineno;
266 file->lineno++;
269 while (c == EOF) {
270 if (file == topfile || popfile() == EOF)
271 return (EOF);
272 c = getc(file->stream);
274 return (c);
277 int
278 lungetc(int c)
280 if (c == EOF)
281 return (EOF);
282 if (parsebuf) {
283 parseindex--;
284 if (parseindex >= 0)
285 return (c);
287 if (pushback_index < MAXPUSHBACK-1)
288 return (pushback_buffer[pushback_index++] = c);
289 else
290 return (EOF);
293 int
294 findeol(void)
296 int c;
298 parsebuf = NULL;
300 /* skip to either EOF or the first real EOL */
301 while (1) {
302 if (pushback_index)
303 c = pushback_buffer[--pushback_index];
304 else
305 c = lgetc(0);
306 if (c == '\n') {
307 file->lineno++;
308 break;
310 if (c == EOF)
311 break;
313 return (ERROR);
316 int
317 yylex(void)
319 u_char buf[8096];
320 u_char *p;
321 int quotec, next, c;
322 int token;
324 p = buf;
325 while ((c = lgetc(0)) == ' ' || c == '\t')
326 ; /* nothing */
328 yylval.lineno = file->lineno;
329 if (c == '#')
330 while ((c = lgetc(0)) != '\n' && c != EOF)
331 ; /* nothing */
333 switch (c) {
334 case '\'':
335 case '"':
336 quotec = c;
337 while (1) {
338 if ((c = lgetc(quotec)) == EOF)
339 return (0);
340 if (c == '\n') {
341 file->lineno++;
342 continue;
343 } else if (c == '\\') {
344 if ((next = lgetc(quotec)) == EOF)
345 return (0);
346 if (next == quotec || next == ' ' ||
347 next == '\t')
348 c = next;
349 else if (next == '\n') {
350 file->lineno++;
351 continue;
352 } else
353 lungetc(next);
354 } else if (c == quotec) {
355 *p = '\0';
356 break;
357 } else if (c == '\0') {
358 yyerror("syntax error");
359 return (findeol());
361 if (p + 1 >= buf + sizeof(buf) - 1) {
362 yyerror("string too long");
363 return (findeol());
365 *p++ = c;
367 yylval.v.string = strdup(buf);
368 if (yylval.v.string == NULL)
369 errx(1, "yylex: strdup");
370 return (STRING);
373 #define allowed_to_end_number(x) \
374 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
376 if (c == '-' || isdigit(c)) {
377 do {
378 *p++ = c;
379 if ((size_t)(p-buf) >= sizeof(buf)) {
380 yyerror("string too long");
381 return (findeol());
383 } while ((c = lgetc(0)) != EOF && isdigit(c));
384 lungetc(c);
385 if (p == buf + 1 && buf[0] == '-')
386 goto nodigits;
387 if (c == EOF || allowed_to_end_number(c)) {
388 const char *errstr = NULL;
390 *p = '\0';
391 yylval.v.number = strtonum(buf, LLONG_MIN,
392 LLONG_MAX, &errstr);
393 if (errstr) {
394 yyerror("\"%s\" invalid number: %s",
395 buf, errstr);
396 return (findeol());
398 return (NUMBER);
399 } else {
400 nodigits:
401 while (p > buf + 1)
402 lungetc(*--p);
403 c = *--p;
404 if (c == '-')
405 return (c);
409 #define allowed_in_string(x) \
410 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
411 x != '{' && x != '}' && x != '<' && x != '>' && \
412 x != '!' && x != '=' && x != '/' && x != '#' && \
413 x != ','))
415 if (isalnum(c) || c == ':' || c == '_' || c == '*') {
416 do {
417 *p++ = c;
418 if ((size_t)(p-buf) >= sizeof(buf)) {
419 yyerror("string too long");
420 return (findeol());
422 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
423 lungetc(c);
424 *p = '\0';
425 if ((token = lookup(buf)) == STRING)
426 if ((yylval.v.string = strdup(buf)) == NULL)
427 errx(1, "yylex: strdup");
428 return (token);
430 if (c == '\n') {
431 yylval.lineno = file->lineno;
432 file->lineno++;
434 if (c == EOF)
435 return (0);
436 return (c);
439 struct file *
440 pushfile(const char *name)
442 struct file *nfile;
444 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
445 gerror = got_error(GOT_ERR_NO_SPACE);
446 return (NULL);
448 if ((nfile->name = strdup(name)) == NULL) {
449 gerror = got_error(GOT_ERR_NO_SPACE);
450 free(nfile);
451 return (NULL);
453 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
454 gerror = got_error_from_errno2("parse_conf", nfile->name);
455 free(nfile->name);
456 free(nfile);
457 return (NULL);
459 nfile->lineno = 1;
460 TAILQ_INSERT_TAIL(&files, nfile, entry);
461 return (nfile);
464 int
465 popfile(void)
467 struct file *prev;
469 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
470 prev->errors += file->errors;
472 TAILQ_REMOVE(&files, file, entry);
473 fclose(file->stream);
474 free(file->name);
475 free(file);
476 file = prev;
477 return (file ? 0 : EOF);
480 const struct got_error*
481 parse_conf(const char *filename, struct gotweb_conf *gconf)
483 static const struct got_error* error = NULL;
485 gw_conf = gconf;
486 if ((gw_conf->got_repos_path = strdup(D_GOTPATH)) == NULL)
487 err(1, "strdup");
488 if ((gw_conf->got_site_name = strdup(D_SITENAME)) == NULL)
489 err(1, "strdup");
490 if ((gw_conf->got_site_owner = strdup(D_SITEOWNER)) == NULL)
491 err(1, "strdup");
492 if ((gw_conf->got_site_link = strdup(D_SITELINK)) == NULL)
493 err(1, "strdup");
494 if ((gw_conf->got_logo = strdup(D_GOTLOGO)) == NULL)
495 err(1, "strdup");
496 if ((gw_conf->got_logo_url = strdup(D_GOTURL)) == NULL)
497 err(1, "strdup");
498 gw_conf->got_show_site_owner = D_SHOWSOWNER;
499 gw_conf->got_show_repo_owner = D_SHOWROWNER;
500 gw_conf->got_show_repo_age = D_SHOWAGE;
501 gw_conf->got_show_repo_description = D_SHOWDESC;
502 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
503 gw_conf->got_max_repos = D_MAXREPO;
504 gw_conf->got_max_repos_display = D_MAXREPODISP;
505 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
506 if ((file = pushfile(filename)) == NULL) {
507 goto done;
509 topfile = file;
511 yyparse();
512 popfile();
513 if (gerror)
514 error = gerror;
515 done:
516 return error;