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_WWW_PATH STRING {
112 if ((gw_conf->got_www_path = strdup($2)) == NULL)
113 errx(1, "out of memory");
115 | GOT_MAX_REPOS NUMBER {
116 if ($2 > 0)
117 gw_conf->got_max_repos = $2;
119 | GOT_SITE_NAME STRING {
120 if ((gw_conf->got_site_name = strdup($2)) == NULL)
121 errx(1, "out of memory");
123 | GOT_SITE_OWNER STRING {
124 if ((gw_conf->got_site_owner = strdup($2)) == NULL)
125 errx(1, "out of memory");
127 | GOT_SITE_LINK STRING {
128 if ((gw_conf->got_site_link = strdup($2)) == NULL)
129 errx(1, "out of memory");
131 | GOT_LOGO STRING {
132 if ((gw_conf->got_logo = strdup($2)) == NULL)
133 errx(1, "out of memory");
135 | GOT_LOGO_URL STRING {
136 if ((gw_conf->got_logo_url = strdup($2)) == NULL)
137 errx(1, "out of memory");
139 | GOT_SHOW_SITE_OWNER boolean {
140 gw_conf->got_show_site_owner = $2;
142 | GOT_SHOW_REPO_OWNER boolean {
143 gw_conf->got_show_repo_owner = $2;
145 | GOT_SHOW_REPO_AGE boolean { gw_conf->got_show_repo_age = $2; }
146 | GOT_SHOW_REPO_DESCRIPTION boolean {
147 gw_conf->got_show_repo_description = $2;
149 | GOT_SHOW_REPO_CLONEURL boolean {
150 gw_conf->got_show_repo_cloneurl = $2;
152 | GOT_MAX_REPOS_DISPLAY NUMBER {
153 if ($2 > 0)
154 gw_conf->got_max_repos_display = $2;
156 | GOT_MAX_COMMITS_DISPLAY NUMBER {
157 if ($2 > 0)
158 gw_conf->got_max_commits_display = $2;
162 %%
164 struct keywords {
165 const char *k_name;
166 int k_val;
167 };
169 int
170 yyerror(const char *fmt, ...)
172 va_list ap;
173 char *msg = NULL;
174 static char err_msg[512];
176 file->errors++;
177 va_start(ap, fmt);
178 if (vasprintf(&msg, fmt, ap) == -1)
179 errx(1, "yyerror vasprintf");
180 va_end(ap);
181 snprintf(err_msg, sizeof(err_msg), "%s:%d: %s", file->name,
182 yylval.lineno, msg);
183 gerror = got_error_from_errno2("parse_error", err_msg);
185 free(msg);
186 return (0);
189 int
190 kw_cmp(const void *k, const void *e)
192 return (strcmp(k, ((const struct keywords *)e)->k_name));
195 int
196 lookup(char *s)
198 /* this has to be sorted always */
199 static const struct keywords keywords[] = {
200 { "got_logo", GOT_LOGO },
201 { "got_logo_url", GOT_LOGO_URL },
202 { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
203 { "got_max_repos", GOT_MAX_REPOS },
204 { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
205 { "got_repos_path", GOT_REPOS_PATH },
206 { "got_show_repo_age", GOT_SHOW_REPO_AGE },
207 { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
208 { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
209 { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
210 { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
211 { "got_site_link", GOT_SITE_LINK },
212 { "got_site_name", GOT_SITE_NAME },
213 { "got_site_owner", GOT_SITE_OWNER },
214 { "got_www_path", GOT_WWW_PATH },
215 };
216 const struct keywords *p;
218 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
219 sizeof(keywords[0]), kw_cmp);
221 if (p)
222 return (p->k_val);
223 else
224 return (STRING);
227 #define MAXPUSHBACK 128
229 u_char *parsebuf;
230 int parseindex;
231 u_char pushback_buffer[MAXPUSHBACK];
232 int pushback_index = 0;
234 int
235 lgetc(int quotec)
237 int c, next;
239 if (parsebuf) {
240 /* Read character from the parsebuffer instead of input. */
241 if (parseindex >= 0) {
242 c = parsebuf[parseindex++];
243 if (c != '\0')
244 return (c);
245 parsebuf = NULL;
246 } else
247 parseindex++;
250 if (pushback_index)
251 return (pushback_buffer[--pushback_index]);
253 if (quotec) {
254 if ((c = getc(file->stream)) == EOF) {
255 yyerror("reached end of file while parsing "
256 "quoted string");
257 if (file == topfile || popfile() == EOF)
258 return (EOF);
259 return (quotec);
261 return (c);
264 while ((c = getc(file->stream)) == '\\') {
265 next = getc(file->stream);
266 if (next != '\n') {
267 c = next;
268 break;
270 yylval.lineno = file->lineno;
271 file->lineno++;
274 while (c == EOF) {
275 if (file == topfile || popfile() == EOF)
276 return (EOF);
277 c = getc(file->stream);
279 return (c);
282 int
283 lungetc(int c)
285 if (c == EOF)
286 return (EOF);
287 if (parsebuf) {
288 parseindex--;
289 if (parseindex >= 0)
290 return (c);
292 if (pushback_index < MAXPUSHBACK-1)
293 return (pushback_buffer[pushback_index++] = c);
294 else
295 return (EOF);
298 int
299 findeol(void)
301 int c;
303 parsebuf = NULL;
305 /* skip to either EOF or the first real EOL */
306 while (1) {
307 if (pushback_index)
308 c = pushback_buffer[--pushback_index];
309 else
310 c = lgetc(0);
311 if (c == '\n') {
312 file->lineno++;
313 break;
315 if (c == EOF)
316 break;
318 return (ERROR);
321 int
322 yylex(void)
324 u_char buf[8096];
325 u_char *p;
326 int quotec, next, c;
327 int token;
329 p = buf;
330 while ((c = lgetc(0)) == ' ' || c == '\t')
331 ; /* nothing */
333 yylval.lineno = file->lineno;
334 if (c == '#')
335 while ((c = lgetc(0)) != '\n' && c != EOF)
336 ; /* nothing */
338 switch (c) {
339 case '\'':
340 case '"':
341 quotec = c;
342 while (1) {
343 if ((c = lgetc(quotec)) == EOF)
344 return (0);
345 if (c == '\n') {
346 file->lineno++;
347 continue;
348 } else if (c == '\\') {
349 if ((next = lgetc(quotec)) == EOF)
350 return (0);
351 if (next == quotec || next == ' ' ||
352 next == '\t')
353 c = next;
354 else if (next == '\n') {
355 file->lineno++;
356 continue;
357 } else
358 lungetc(next);
359 } else if (c == quotec) {
360 *p = '\0';
361 break;
362 } else if (c == '\0') {
363 yyerror("syntax error");
364 return (findeol());
366 if (p + 1 >= buf + sizeof(buf) - 1) {
367 yyerror("string too long");
368 return (findeol());
370 *p++ = c;
372 yylval.v.string = strdup(buf);
373 if (yylval.v.string == NULL)
374 errx(1, "yylex: strdup");
375 return (STRING);
378 #define allowed_to_end_number(x) \
379 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
381 if (c == '-' || isdigit(c)) {
382 do {
383 *p++ = c;
384 if ((size_t)(p-buf) >= sizeof(buf)) {
385 yyerror("string too long");
386 return (findeol());
388 } while ((c = lgetc(0)) != EOF && isdigit(c));
389 lungetc(c);
390 if (p == buf + 1 && buf[0] == '-')
391 goto nodigits;
392 if (c == EOF || allowed_to_end_number(c)) {
393 const char *errstr = NULL;
395 *p = '\0';
396 yylval.v.number = strtonum(buf, LLONG_MIN,
397 LLONG_MAX, &errstr);
398 if (errstr) {
399 yyerror("\"%s\" invalid number: %s",
400 buf, errstr);
401 return (findeol());
403 return (NUMBER);
404 } else {
405 nodigits:
406 while (p > buf + 1)
407 lungetc(*--p);
408 c = *--p;
409 if (c == '-')
410 return (c);
414 #define allowed_in_string(x) \
415 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
416 x != '{' && x != '}' && x != '<' && x != '>' && \
417 x != '!' && x != '=' && x != '/' && x != '#' && \
418 x != ','))
420 if (isalnum(c) || c == ':' || c == '_' || c == '*') {
421 do {
422 *p++ = c;
423 if ((size_t)(p-buf) >= sizeof(buf)) {
424 yyerror("string too long");
425 return (findeol());
427 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
428 lungetc(c);
429 *p = '\0';
430 if ((token = lookup(buf)) == STRING)
431 if ((yylval.v.string = strdup(buf)) == NULL)
432 errx(1, "yylex: strdup");
433 return (token);
435 if (c == '\n') {
436 yylval.lineno = file->lineno;
437 file->lineno++;
439 if (c == EOF)
440 return (0);
441 return (c);
444 struct file *
445 pushfile(const char *name)
447 struct file *nfile;
449 if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
450 gerror = got_error(GOT_ERR_NO_SPACE);
451 return (NULL);
453 if ((nfile->name = strdup(name)) == NULL) {
454 gerror = got_error(GOT_ERR_NO_SPACE);
455 free(nfile);
456 return (NULL);
458 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
459 gerror = got_error_from_errno2("parse_conf", nfile->name);
460 free(nfile->name);
461 free(nfile);
462 return (NULL);
464 nfile->lineno = 1;
465 TAILQ_INSERT_TAIL(&files, nfile, entry);
466 return (nfile);
469 int
470 popfile(void)
472 struct file *prev;
474 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
475 prev->errors += file->errors;
477 TAILQ_REMOVE(&files, file, entry);
478 fclose(file->stream);
479 free(file->name);
480 free(file);
481 file = prev;
482 return (file ? 0 : EOF);
485 const struct got_error*
486 parse_conf(const char *filename, struct gotweb_conf *gconf)
488 static const struct got_error* error = NULL;
490 gw_conf = gconf;
491 if ((gw_conf->got_repos_path = strdup(D_GOTPATH)) == NULL)
492 err(1, "strdup");
493 if ((gw_conf->got_www_path = strdup(D_GOTWWW)) == NULL)
494 err(1, "strdup");
495 if ((gw_conf->got_site_name = strdup(D_SITENAME)) == NULL)
496 err(1, "strdup");
497 if ((gw_conf->got_site_owner = strdup(D_SITEOWNER)) == NULL)
498 err(1, "strdup");
499 if ((gw_conf->got_site_link = strdup(D_SITELINK)) == NULL)
500 err(1, "strdup");
501 if ((gw_conf->got_logo = strdup(D_GOTLOGO)) == NULL)
502 err(1, "strdup");
503 if ((gw_conf->got_logo_url = strdup(D_GOTURL)) == NULL)
504 err(1, "strdup");
505 gw_conf->got_show_site_owner = D_SHOWSOWNER;
506 gw_conf->got_show_repo_owner = D_SHOWROWNER;
507 gw_conf->got_show_repo_age = D_SHOWAGE;
508 gw_conf->got_show_repo_description = D_SHOWDESC;
509 gw_conf->got_show_repo_cloneurl = D_SHOWURL;
510 gw_conf->got_max_repos = D_MAXREPO;
511 gw_conf->got_max_repos_display = D_MAXREPODISP;
512 gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
513 if ((file = pushfile(filename)) == NULL) {
514 goto done;
516 topfile = file;
518 yyparse();
519 popfile();
520 if (gerror)
521 error = gerror;
522 done:
523 return error;