2 2c251c14 2020-01-15 tracey * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
3 2c251c14 2020-01-15 tracey * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
4 2c251c14 2020-01-15 tracey * Copyright (c) 2001 Markus Friedl. All rights reserved.
5 2c251c14 2020-01-15 tracey * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
6 2c251c14 2020-01-15 tracey * Copyright (c) 2001 Theo de Raadt. All rights reserved.
8 2c251c14 2020-01-15 tracey * Permission to use, copy, modify, and distribute this software for any
9 2c251c14 2020-01-15 tracey * purpose with or without fee is hereby granted, provided that the above
10 2c251c14 2020-01-15 tracey * copyright notice and this permission notice appear in all copies.
12 2c251c14 2020-01-15 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 2c251c14 2020-01-15 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 2c251c14 2020-01-15 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 2c251c14 2020-01-15 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 2c251c14 2020-01-15 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 2c251c14 2020-01-15 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 2c251c14 2020-01-15 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 2c251c14 2020-01-15 tracey #include <sys/types.h>
23 2c251c14 2020-01-15 tracey #include <sys/queue.h>
25 2c251c14 2020-01-15 tracey #include <ctype.h>
26 2c251c14 2020-01-15 tracey #include <err.h>
27 2c251c14 2020-01-15 tracey #include <limits.h>
28 2c251c14 2020-01-15 tracey #include <stdarg.h>
29 2c251c14 2020-01-15 tracey #include <stdbool.h>
30 2c251c14 2020-01-15 tracey #include <stdio.h>
31 2c251c14 2020-01-15 tracey #include <stdlib.h>
32 2c251c14 2020-01-15 tracey #include <string.h>
34 2c251c14 2020-01-15 tracey #include "gotweb.h"
36 2c251c14 2020-01-15 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
37 2c251c14 2020-01-15 tracey static struct file {
38 2c251c14 2020-01-15 tracey TAILQ_ENTRY(file) entry;
39 2c251c14 2020-01-15 tracey FILE *stream;
40 2c251c14 2020-01-15 tracey char *name;
41 2c251c14 2020-01-15 tracey int lineno;
42 2c251c14 2020-01-15 tracey int errors;
43 2c251c14 2020-01-15 tracey const struct got_error* error;
44 2c251c14 2020-01-15 tracey } *file, *topfile;
45 2c251c14 2020-01-15 tracey struct file *pushfile(const char *);
46 2c251c14 2020-01-15 tracey int popfile(void);
47 2c251c14 2020-01-15 tracey int yyparse(void);
48 2c251c14 2020-01-15 tracey int yylex(void);
49 2c251c14 2020-01-15 tracey int yyerror(const char *, ...)
50 2c251c14 2020-01-15 tracey __attribute__((__format__ (printf, 1, 2)))
51 2c251c14 2020-01-15 tracey __attribute__((__nonnull__ (1)));
52 2c251c14 2020-01-15 tracey int kw_cmp(const void *, const void *);
53 2c251c14 2020-01-15 tracey int lookup(char *);
54 2c251c14 2020-01-15 tracey int lgetc(int);
55 2c251c14 2020-01-15 tracey int lungetc(int);
56 2c251c14 2020-01-15 tracey int findeol(void);
58 2c251c14 2020-01-15 tracey static const struct got_error* gerror = NULL;
59 2c251c14 2020-01-15 tracey char *syn_err;
61 2c251c14 2020-01-15 tracey struct gotweb_conf *gw_conf;
63 2c251c14 2020-01-15 tracey typedef struct {
65 2c251c14 2020-01-15 tracey int64_t number;
66 2c251c14 2020-01-15 tracey char *string;
68 2c251c14 2020-01-15 tracey int lineno;
69 2c251c14 2020-01-15 tracey } YYSTYPE;
73 2c251c14 2020-01-15 tracey %token GOT_WWW_PATH GOT_MAX_REPOS GOT_SITE_NAME GOT_SITE_OWNER GOT_SITE_LINK
74 2c251c14 2020-01-15 tracey %token GOT_LOGO GOT_LOGO_URL GOT_SHOW_REPO_OWNER GOT_SHOW_REPO_AGE
75 2c251c14 2020-01-15 tracey %token GOT_SHOW_REPO_DESCRIPTION GOT_MAX_REPOS_DISPLAY GOT_REPOS_PATH
76 2c251c14 2020-01-15 tracey %token GOT_MAX_COMMITS_DISPLAY ON ERROR GOT_SHOW_SITE_OWNER
77 2c251c14 2020-01-15 tracey %token GOT_SHOW_REPO_CLONEURL
78 2c251c14 2020-01-15 tracey %token <v.string> STRING
79 2c251c14 2020-01-15 tracey %token <v.number> NUMBER
80 2c251c14 2020-01-15 tracey %type <v.number> boolean
83 2c251c14 2020-01-15 tracey grammar : /* empty */
84 2c251c14 2020-01-15 tracey | grammar '\n'
85 2c251c14 2020-01-15 tracey | grammar main '\n'
86 2c251c14 2020-01-15 tracey | grammar error '\n' { file->errors++; }
89 2c251c14 2020-01-15 tracey boolean : STRING {
90 2c251c14 2020-01-15 tracey if (strcasecmp($1, "true") == 0 ||
91 2c251c14 2020-01-15 tracey strcasecmp($1, "yes") == 0)
93 2c251c14 2020-01-15 tracey else if (strcasecmp($1, "false") == 0 ||
94 2c251c14 2020-01-15 tracey strcasecmp($1, "off") == 0 ||
95 2c251c14 2020-01-15 tracey strcasecmp($1, "no") == 0)
98 2c251c14 2020-01-15 tracey yyerror("invalid boolean value '%s'", $1);
102 2c251c14 2020-01-15 tracey free($1);
104 2c251c14 2020-01-15 tracey | ON { $$ = 1; }
107 2c251c14 2020-01-15 tracey main : GOT_REPOS_PATH STRING {
108 54415d85 2020-01-15 tracey if ((gw_conf->got_repos_path = strdup($2)) == NULL)
109 2c251c14 2020-01-15 tracey errx(1, "out of memory");
111 2c251c14 2020-01-15 tracey | GOT_MAX_REPOS NUMBER {
112 2c251c14 2020-01-15 tracey if ($2 > 0)
113 2c251c14 2020-01-15 tracey gw_conf->got_max_repos = $2;
115 2c251c14 2020-01-15 tracey | GOT_SITE_NAME STRING {
116 54415d85 2020-01-15 tracey if ((gw_conf->got_site_name = strdup($2)) == NULL)
117 2c251c14 2020-01-15 tracey errx(1, "out of memory");
119 2c251c14 2020-01-15 tracey | GOT_SITE_OWNER STRING {
120 54415d85 2020-01-15 tracey if ((gw_conf->got_site_owner = strdup($2)) == NULL)
121 2c251c14 2020-01-15 tracey errx(1, "out of memory");
123 2c251c14 2020-01-15 tracey | GOT_SITE_LINK STRING {
124 54415d85 2020-01-15 tracey if ((gw_conf->got_site_link = strdup($2)) == NULL)
125 2c251c14 2020-01-15 tracey errx(1, "out of memory");
127 2c251c14 2020-01-15 tracey | GOT_LOGO STRING {
128 54415d85 2020-01-15 tracey if ((gw_conf->got_logo = strdup($2)) == NULL)
129 2c251c14 2020-01-15 tracey errx(1, "out of memory");
131 2c251c14 2020-01-15 tracey | GOT_LOGO_URL STRING {
132 54415d85 2020-01-15 tracey if ((gw_conf->got_logo_url = strdup($2)) == NULL)
133 2c251c14 2020-01-15 tracey errx(1, "out of memory");
135 2c251c14 2020-01-15 tracey | GOT_SHOW_SITE_OWNER boolean {
136 2c251c14 2020-01-15 tracey gw_conf->got_show_site_owner = $2;
138 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_OWNER boolean {
139 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_owner = $2;
141 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_AGE boolean { gw_conf->got_show_repo_age = $2; }
142 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_DESCRIPTION boolean {
143 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_description = $2;
145 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_CLONEURL boolean {
146 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_cloneurl = $2;
148 2c251c14 2020-01-15 tracey | GOT_MAX_REPOS_DISPLAY NUMBER {
149 2c251c14 2020-01-15 tracey if ($2 > 0)
150 2c251c14 2020-01-15 tracey gw_conf->got_max_repos_display = $2;
152 2c251c14 2020-01-15 tracey | GOT_MAX_COMMITS_DISPLAY NUMBER {
153 2c251c14 2020-01-15 tracey if ($2 > 0)
154 2c251c14 2020-01-15 tracey gw_conf->got_max_commits_display = $2;
160 2c251c14 2020-01-15 tracey struct keywords {
161 2c251c14 2020-01-15 tracey const char *k_name;
162 2c251c14 2020-01-15 tracey int k_val;
166 2c251c14 2020-01-15 tracey yyerror(const char *fmt, ...)
168 2c251c14 2020-01-15 tracey va_list ap;
169 2c251c14 2020-01-15 tracey char *msg = NULL;
170 2c251c14 2020-01-15 tracey static char err_msg[512];
172 2c251c14 2020-01-15 tracey file->errors++;
173 2c251c14 2020-01-15 tracey va_start(ap, fmt);
174 2c251c14 2020-01-15 tracey if (vasprintf(&msg, fmt, ap) == -1)
175 2c251c14 2020-01-15 tracey errx(1, "yyerror vasprintf");
176 2c251c14 2020-01-15 tracey va_end(ap);
177 2c251c14 2020-01-15 tracey snprintf(err_msg, sizeof(err_msg), "%s:%d: %s", file->name,
178 2c251c14 2020-01-15 tracey yylval.lineno, msg);
179 2c251c14 2020-01-15 tracey gerror = got_error_from_errno2("parse_error", err_msg);
181 2c251c14 2020-01-15 tracey free(msg);
182 2c251c14 2020-01-15 tracey return (0);
186 2c251c14 2020-01-15 tracey kw_cmp(const void *k, const void *e)
188 2c251c14 2020-01-15 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
192 2c251c14 2020-01-15 tracey lookup(char *s)
194 2c251c14 2020-01-15 tracey /* this has to be sorted always */
195 2c251c14 2020-01-15 tracey static const struct keywords keywords[] = {
196 2c251c14 2020-01-15 tracey { "got_logo", GOT_LOGO },
197 2c251c14 2020-01-15 tracey { "got_logo_url", GOT_LOGO_URL },
198 2c251c14 2020-01-15 tracey { "got_max_commits_display", GOT_MAX_COMMITS_DISPLAY },
199 2c251c14 2020-01-15 tracey { "got_max_repos", GOT_MAX_REPOS },
200 2c251c14 2020-01-15 tracey { "got_max_repos_display", GOT_MAX_REPOS_DISPLAY },
201 2c251c14 2020-01-15 tracey { "got_repos_path", GOT_REPOS_PATH },
202 2c251c14 2020-01-15 tracey { "got_show_repo_age", GOT_SHOW_REPO_AGE },
203 2c251c14 2020-01-15 tracey { "got_show_repo_cloneurl", GOT_SHOW_REPO_CLONEURL },
204 2c251c14 2020-01-15 tracey { "got_show_repo_description", GOT_SHOW_REPO_DESCRIPTION },
205 2c251c14 2020-01-15 tracey { "got_show_repo_owner", GOT_SHOW_REPO_OWNER },
206 2c251c14 2020-01-15 tracey { "got_show_site_owner", GOT_SHOW_SITE_OWNER },
207 2c251c14 2020-01-15 tracey { "got_site_link", GOT_SITE_LINK },
208 2c251c14 2020-01-15 tracey { "got_site_name", GOT_SITE_NAME },
209 2c251c14 2020-01-15 tracey { "got_site_owner", GOT_SITE_OWNER },
211 2c251c14 2020-01-15 tracey const struct keywords *p;
213 2c251c14 2020-01-15 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
214 2c251c14 2020-01-15 tracey sizeof(keywords[0]), kw_cmp);
217 2c251c14 2020-01-15 tracey return (p->k_val);
219 2c251c14 2020-01-15 tracey return (STRING);
222 2c251c14 2020-01-15 tracey #define MAXPUSHBACK 128
224 2c251c14 2020-01-15 tracey u_char *parsebuf;
225 2c251c14 2020-01-15 tracey int parseindex;
226 2c251c14 2020-01-15 tracey u_char pushback_buffer[MAXPUSHBACK];
227 2c251c14 2020-01-15 tracey int pushback_index = 0;
230 2c251c14 2020-01-15 tracey lgetc(int quotec)
232 2c251c14 2020-01-15 tracey int c, next;
234 2c251c14 2020-01-15 tracey if (parsebuf) {
235 2c251c14 2020-01-15 tracey /* Read character from the parsebuffer instead of input. */
236 2c251c14 2020-01-15 tracey if (parseindex >= 0) {
237 2c251c14 2020-01-15 tracey c = parsebuf[parseindex++];
238 2c251c14 2020-01-15 tracey if (c != '\0')
239 2c251c14 2020-01-15 tracey return (c);
240 2c251c14 2020-01-15 tracey parsebuf = NULL;
242 2c251c14 2020-01-15 tracey parseindex++;
245 2c251c14 2020-01-15 tracey if (pushback_index)
246 2c251c14 2020-01-15 tracey return (pushback_buffer[--pushback_index]);
248 2c251c14 2020-01-15 tracey if (quotec) {
249 2c251c14 2020-01-15 tracey if ((c = getc(file->stream)) == EOF) {
250 2c251c14 2020-01-15 tracey yyerror("reached end of file while parsing "
251 2c251c14 2020-01-15 tracey "quoted string");
252 2c251c14 2020-01-15 tracey if (file == topfile || popfile() == EOF)
253 2c251c14 2020-01-15 tracey return (EOF);
254 2c251c14 2020-01-15 tracey return (quotec);
256 2c251c14 2020-01-15 tracey return (c);
259 2c251c14 2020-01-15 tracey while ((c = getc(file->stream)) == '\\') {
260 2c251c14 2020-01-15 tracey next = getc(file->stream);
261 2c251c14 2020-01-15 tracey if (next != '\n') {
262 2c251c14 2020-01-15 tracey c = next;
265 2c251c14 2020-01-15 tracey yylval.lineno = file->lineno;
266 2c251c14 2020-01-15 tracey file->lineno++;
269 2c251c14 2020-01-15 tracey while (c == EOF) {
270 2c251c14 2020-01-15 tracey if (file == topfile || popfile() == EOF)
271 2c251c14 2020-01-15 tracey return (EOF);
272 2c251c14 2020-01-15 tracey c = getc(file->stream);
274 2c251c14 2020-01-15 tracey return (c);
278 2c251c14 2020-01-15 tracey lungetc(int c)
280 2c251c14 2020-01-15 tracey if (c == EOF)
281 2c251c14 2020-01-15 tracey return (EOF);
282 2c251c14 2020-01-15 tracey if (parsebuf) {
283 2c251c14 2020-01-15 tracey parseindex--;
284 2c251c14 2020-01-15 tracey if (parseindex >= 0)
285 2c251c14 2020-01-15 tracey return (c);
287 2c251c14 2020-01-15 tracey if (pushback_index < MAXPUSHBACK-1)
288 2c251c14 2020-01-15 tracey return (pushback_buffer[pushback_index++] = c);
290 2c251c14 2020-01-15 tracey return (EOF);
294 2c251c14 2020-01-15 tracey findeol(void)
298 2c251c14 2020-01-15 tracey parsebuf = NULL;
300 2c251c14 2020-01-15 tracey /* skip to either EOF or the first real EOL */
301 2c251c14 2020-01-15 tracey while (1) {
302 2c251c14 2020-01-15 tracey if (pushback_index)
303 2c251c14 2020-01-15 tracey c = pushback_buffer[--pushback_index];
305 2c251c14 2020-01-15 tracey c = lgetc(0);
306 2c251c14 2020-01-15 tracey if (c == '\n') {
307 2c251c14 2020-01-15 tracey file->lineno++;
310 2c251c14 2020-01-15 tracey if (c == EOF)
313 2c251c14 2020-01-15 tracey return (ERROR);
317 2c251c14 2020-01-15 tracey yylex(void)
319 2c251c14 2020-01-15 tracey u_char buf[8096];
320 2c251c14 2020-01-15 tracey u_char *p;
321 2c251c14 2020-01-15 tracey int quotec, next, c;
322 2c251c14 2020-01-15 tracey int token;
325 2c251c14 2020-01-15 tracey while ((c = lgetc(0)) == ' ' || c == '\t')
326 2c251c14 2020-01-15 tracey ; /* nothing */
328 2c251c14 2020-01-15 tracey yylval.lineno = file->lineno;
329 2c251c14 2020-01-15 tracey if (c == '#')
330 2c251c14 2020-01-15 tracey while ((c = lgetc(0)) != '\n' && c != EOF)
331 2c251c14 2020-01-15 tracey ; /* nothing */
333 2c251c14 2020-01-15 tracey switch (c) {
334 2c251c14 2020-01-15 tracey case '\'':
335 2c251c14 2020-01-15 tracey case '"':
336 2c251c14 2020-01-15 tracey quotec = c;
337 2c251c14 2020-01-15 tracey while (1) {
338 2c251c14 2020-01-15 tracey if ((c = lgetc(quotec)) == EOF)
339 2c251c14 2020-01-15 tracey return (0);
340 2c251c14 2020-01-15 tracey if (c == '\n') {
341 2c251c14 2020-01-15 tracey file->lineno++;
342 2c251c14 2020-01-15 tracey continue;
343 2c251c14 2020-01-15 tracey } else if (c == '\\') {
344 2c251c14 2020-01-15 tracey if ((next = lgetc(quotec)) == EOF)
345 2c251c14 2020-01-15 tracey return (0);
346 2c251c14 2020-01-15 tracey if (next == quotec || next == ' ' ||
347 2c251c14 2020-01-15 tracey next == '\t')
348 2c251c14 2020-01-15 tracey c = next;
349 2c251c14 2020-01-15 tracey else if (next == '\n') {
350 2c251c14 2020-01-15 tracey file->lineno++;
351 2c251c14 2020-01-15 tracey continue;
353 2c251c14 2020-01-15 tracey lungetc(next);
354 2c251c14 2020-01-15 tracey } else if (c == quotec) {
355 2c251c14 2020-01-15 tracey *p = '\0';
357 2c251c14 2020-01-15 tracey } else if (c == '\0') {
358 2c251c14 2020-01-15 tracey yyerror("syntax error");
359 2c251c14 2020-01-15 tracey return (findeol());
361 2c251c14 2020-01-15 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
362 2c251c14 2020-01-15 tracey yyerror("string too long");
363 2c251c14 2020-01-15 tracey return (findeol());
365 2c251c14 2020-01-15 tracey *p++ = c;
367 2c251c14 2020-01-15 tracey yylval.v.string = strdup(buf);
368 2c251c14 2020-01-15 tracey if (yylval.v.string == NULL)
369 2c251c14 2020-01-15 tracey errx(1, "yylex: strdup");
370 2c251c14 2020-01-15 tracey return (STRING);
373 2c251c14 2020-01-15 tracey #define allowed_to_end_number(x) \
374 2c251c14 2020-01-15 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
376 2c251c14 2020-01-15 tracey if (c == '-' || isdigit(c)) {
378 2c251c14 2020-01-15 tracey *p++ = c;
379 2c251c14 2020-01-15 tracey if ((size_t)(p-buf) >= sizeof(buf)) {
380 2c251c14 2020-01-15 tracey yyerror("string too long");
381 2c251c14 2020-01-15 tracey return (findeol());
383 2c251c14 2020-01-15 tracey } while ((c = lgetc(0)) != EOF && isdigit(c));
384 2c251c14 2020-01-15 tracey lungetc(c);
385 2c251c14 2020-01-15 tracey if (p == buf + 1 && buf[0] == '-')
386 2c251c14 2020-01-15 tracey goto nodigits;
387 2c251c14 2020-01-15 tracey if (c == EOF || allowed_to_end_number(c)) {
388 2c251c14 2020-01-15 tracey const char *errstr = NULL;
390 2c251c14 2020-01-15 tracey *p = '\0';
391 2c251c14 2020-01-15 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
392 2c251c14 2020-01-15 tracey LLONG_MAX, &errstr);
393 2c251c14 2020-01-15 tracey if (errstr) {
394 2c251c14 2020-01-15 tracey yyerror("\"%s\" invalid number: %s",
395 2c251c14 2020-01-15 tracey buf, errstr);
396 2c251c14 2020-01-15 tracey return (findeol());
398 2c251c14 2020-01-15 tracey return (NUMBER);
400 2c251c14 2020-01-15 tracey nodigits:
401 2c251c14 2020-01-15 tracey while (p > buf + 1)
402 2c251c14 2020-01-15 tracey lungetc(*--p);
403 2c251c14 2020-01-15 tracey c = *--p;
404 2c251c14 2020-01-15 tracey if (c == '-')
405 2c251c14 2020-01-15 tracey return (c);
409 2c251c14 2020-01-15 tracey #define allowed_in_string(x) \
410 2c251c14 2020-01-15 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
411 2c251c14 2020-01-15 tracey x != '{' && x != '}' && x != '<' && x != '>' && \
412 2c251c14 2020-01-15 tracey x != '!' && x != '=' && x != '/' && x != '#' && \
413 2c251c14 2020-01-15 tracey x != ','))
415 2c251c14 2020-01-15 tracey if (isalnum(c) || c == ':' || c == '_' || c == '*') {
417 2c251c14 2020-01-15 tracey *p++ = c;
418 2c251c14 2020-01-15 tracey if ((size_t)(p-buf) >= sizeof(buf)) {
419 2c251c14 2020-01-15 tracey yyerror("string too long");
420 2c251c14 2020-01-15 tracey return (findeol());
422 2c251c14 2020-01-15 tracey } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
423 2c251c14 2020-01-15 tracey lungetc(c);
424 2c251c14 2020-01-15 tracey *p = '\0';
425 2c251c14 2020-01-15 tracey if ((token = lookup(buf)) == STRING)
426 2c251c14 2020-01-15 tracey if ((yylval.v.string = strdup(buf)) == NULL)
427 2c251c14 2020-01-15 tracey errx(1, "yylex: strdup");
428 2c251c14 2020-01-15 tracey return (token);
430 2c251c14 2020-01-15 tracey if (c == '\n') {
431 2c251c14 2020-01-15 tracey yylval.lineno = file->lineno;
432 2c251c14 2020-01-15 tracey file->lineno++;
434 2c251c14 2020-01-15 tracey if (c == EOF)
435 2c251c14 2020-01-15 tracey return (0);
436 2c251c14 2020-01-15 tracey return (c);
439 2c251c14 2020-01-15 tracey struct file *
440 2c251c14 2020-01-15 tracey pushfile(const char *name)
442 2c251c14 2020-01-15 tracey struct file *nfile;
444 2c251c14 2020-01-15 tracey if ((nfile = calloc(1, sizeof(struct file))) == NULL) {
445 2c251c14 2020-01-15 tracey gerror = got_error(GOT_ERR_NO_SPACE);
446 2c251c14 2020-01-15 tracey return (NULL);
448 2c251c14 2020-01-15 tracey if ((nfile->name = strdup(name)) == NULL) {
449 2c251c14 2020-01-15 tracey gerror = got_error(GOT_ERR_NO_SPACE);
450 2c251c14 2020-01-15 tracey free(nfile);
451 2c251c14 2020-01-15 tracey return (NULL);
453 2c251c14 2020-01-15 tracey if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
454 2c251c14 2020-01-15 tracey gerror = got_error_from_errno2("parse_conf", nfile->name);
455 2c251c14 2020-01-15 tracey free(nfile->name);
456 2c251c14 2020-01-15 tracey free(nfile);
457 2c251c14 2020-01-15 tracey return (NULL);
459 2c251c14 2020-01-15 tracey nfile->lineno = 1;
460 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&files, nfile, entry);
461 2c251c14 2020-01-15 tracey return (nfile);
465 2c251c14 2020-01-15 tracey popfile(void)
467 2c251c14 2020-01-15 tracey struct file *prev;
469 2c251c14 2020-01-15 tracey if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
470 2c251c14 2020-01-15 tracey prev->errors += file->errors;
472 2c251c14 2020-01-15 tracey TAILQ_REMOVE(&files, file, entry);
473 2c251c14 2020-01-15 tracey fclose(file->stream);
474 2c251c14 2020-01-15 tracey free(file->name);
475 2c251c14 2020-01-15 tracey free(file);
476 2c251c14 2020-01-15 tracey file = prev;
477 2c251c14 2020-01-15 tracey return (file ? 0 : EOF);
480 2c251c14 2020-01-15 tracey const struct got_error*
481 2c251c14 2020-01-15 tracey parse_conf(const char *filename, struct gotweb_conf *gconf)
483 2c251c14 2020-01-15 tracey static const struct got_error* error = NULL;
485 2c251c14 2020-01-15 tracey gw_conf = gconf;
486 54415d85 2020-01-15 tracey if ((gw_conf->got_repos_path = strdup(D_GOTPATH)) == NULL)
487 54415d85 2020-01-15 tracey err(1, "strdup");
488 54415d85 2020-01-15 tracey if ((gw_conf->got_site_name = strdup(D_SITENAME)) == NULL)
489 54415d85 2020-01-15 tracey err(1, "strdup");
490 54415d85 2020-01-15 tracey if ((gw_conf->got_site_owner = strdup(D_SITEOWNER)) == NULL)
491 54415d85 2020-01-15 tracey err(1, "strdup");
492 54415d85 2020-01-15 tracey if ((gw_conf->got_site_link = strdup(D_SITELINK)) == NULL)
493 54415d85 2020-01-15 tracey err(1, "strdup");
494 54415d85 2020-01-15 tracey if ((gw_conf->got_logo = strdup(D_GOTLOGO)) == NULL)
495 54415d85 2020-01-15 tracey err(1, "strdup");
496 54415d85 2020-01-15 tracey if ((gw_conf->got_logo_url = strdup(D_GOTURL)) == NULL)
497 54415d85 2020-01-15 tracey err(1, "strdup");
498 2c251c14 2020-01-15 tracey gw_conf->got_show_site_owner = D_SHOWSOWNER;
499 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_owner = D_SHOWROWNER;
500 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_age = D_SHOWAGE;
501 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_description = D_SHOWDESC;
502 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_cloneurl = D_SHOWURL;
503 2c251c14 2020-01-15 tracey gw_conf->got_max_repos = D_MAXREPO;
504 2c251c14 2020-01-15 tracey gw_conf->got_max_repos_display = D_MAXREPODISP;
505 2c251c14 2020-01-15 tracey gw_conf->got_max_commits_display = D_MAXCOMMITDISP;
506 2c251c14 2020-01-15 tracey if ((file = pushfile(filename)) == NULL) {
507 2c251c14 2020-01-15 tracey goto done;
509 2c251c14 2020-01-15 tracey topfile = file;
511 2c251c14 2020-01-15 tracey yyparse();
512 2c251c14 2020-01-15 tracey popfile();
513 2c251c14 2020-01-15 tracey if (gerror)
514 2c251c14 2020-01-15 tracey error = gerror;
516 2c251c14 2020-01-15 tracey return error;