Blame


1 2c251c14 2020-01-15 tracey /*
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.
7 2c251c14 2020-01-15 tracey *
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.
11 2c251c14 2020-01-15 tracey *
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.
19 2c251c14 2020-01-15 tracey */
20 2c251c14 2020-01-15 tracey
21 2c251c14 2020-01-15 tracey %{
22 2c251c14 2020-01-15 tracey #include <sys/types.h>
23 2c251c14 2020-01-15 tracey #include <sys/queue.h>
24 2c251c14 2020-01-15 tracey
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>
33 2c251c14 2020-01-15 tracey
34 2c251c14 2020-01-15 tracey #include "gotweb.h"
35 2c251c14 2020-01-15 tracey
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);
57 2c251c14 2020-01-15 tracey
58 2c251c14 2020-01-15 tracey static const struct got_error* gerror = NULL;
59 2c251c14 2020-01-15 tracey char *syn_err;
60 2c251c14 2020-01-15 tracey
61 2c251c14 2020-01-15 tracey struct gotweb_conf *gw_conf;
62 2c251c14 2020-01-15 tracey
63 2c251c14 2020-01-15 tracey typedef struct {
64 2c251c14 2020-01-15 tracey union {
65 2c251c14 2020-01-15 tracey int64_t number;
66 2c251c14 2020-01-15 tracey char *string;
67 2c251c14 2020-01-15 tracey } v;
68 2c251c14 2020-01-15 tracey int lineno;
69 2c251c14 2020-01-15 tracey } YYSTYPE;
70 2c251c14 2020-01-15 tracey
71 2c251c14 2020-01-15 tracey %}
72 2c251c14 2020-01-15 tracey
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
81 2c251c14 2020-01-15 tracey %%
82 2c251c14 2020-01-15 tracey
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++; }
87 2c251c14 2020-01-15 tracey ;
88 2c251c14 2020-01-15 tracey
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)
92 2c251c14 2020-01-15 tracey $$ = 1;
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)
96 2c251c14 2020-01-15 tracey $$ = 0;
97 2c251c14 2020-01-15 tracey else {
98 2c251c14 2020-01-15 tracey yyerror("invalid boolean value '%s'", $1);
99 2c251c14 2020-01-15 tracey free($1);
100 2c251c14 2020-01-15 tracey YYERROR;
101 2c251c14 2020-01-15 tracey }
102 2c251c14 2020-01-15 tracey free($1);
103 2c251c14 2020-01-15 tracey }
104 2c251c14 2020-01-15 tracey | ON { $$ = 1; }
105 2c251c14 2020-01-15 tracey ;
106 2c251c14 2020-01-15 tracey
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");
110 54415d85 2020-01-15 tracey }
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;
114 2c251c14 2020-01-15 tracey }
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");
118 2c251c14 2020-01-15 tracey }
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");
122 2c251c14 2020-01-15 tracey }
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");
126 2c251c14 2020-01-15 tracey }
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");
130 2c251c14 2020-01-15 tracey }
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");
134 2c251c14 2020-01-15 tracey }
135 2c251c14 2020-01-15 tracey | GOT_SHOW_SITE_OWNER boolean {
136 2c251c14 2020-01-15 tracey gw_conf->got_show_site_owner = $2;
137 2c251c14 2020-01-15 tracey }
138 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_OWNER boolean {
139 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_owner = $2;
140 2c251c14 2020-01-15 tracey }
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;
144 2c251c14 2020-01-15 tracey }
145 2c251c14 2020-01-15 tracey | GOT_SHOW_REPO_CLONEURL boolean {
146 2c251c14 2020-01-15 tracey gw_conf->got_show_repo_cloneurl = $2;
147 2c251c14 2020-01-15 tracey }
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;
151 2c251c14 2020-01-15 tracey }
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;
155 2c251c14 2020-01-15 tracey }
156 54415d85 2020-01-15 tracey ;
157 2c251c14 2020-01-15 tracey
158 2c251c14 2020-01-15 tracey %%
159 2c251c14 2020-01-15 tracey
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;
163 2c251c14 2020-01-15 tracey };
164 2c251c14 2020-01-15 tracey
165 2c251c14 2020-01-15 tracey int
166 2c251c14 2020-01-15 tracey yyerror(const char *fmt, ...)
167 2c251c14 2020-01-15 tracey {
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];
171 2c251c14 2020-01-15 tracey
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);
180 2c251c14 2020-01-15 tracey
181 2c251c14 2020-01-15 tracey free(msg);
182 2c251c14 2020-01-15 tracey return (0);
183 2c251c14 2020-01-15 tracey }
184 2c251c14 2020-01-15 tracey
185 2c251c14 2020-01-15 tracey int
186 2c251c14 2020-01-15 tracey kw_cmp(const void *k, const void *e)
187 2c251c14 2020-01-15 tracey {
188 2c251c14 2020-01-15 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
189 2c251c14 2020-01-15 tracey }
190 2c251c14 2020-01-15 tracey
191 2c251c14 2020-01-15 tracey int
192 2c251c14 2020-01-15 tracey lookup(char *s)
193 2c251c14 2020-01-15 tracey {
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 },
210 2c251c14 2020-01-15 tracey };
211 2c251c14 2020-01-15 tracey const struct keywords *p;
212 2c251c14 2020-01-15 tracey
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);
215 2c251c14 2020-01-15 tracey
216 2c251c14 2020-01-15 tracey if (p)
217 2c251c14 2020-01-15 tracey return (p->k_val);
218 2c251c14 2020-01-15 tracey else
219 2c251c14 2020-01-15 tracey return (STRING);
220 2c251c14 2020-01-15 tracey }
221 2c251c14 2020-01-15 tracey
222 2c251c14 2020-01-15 tracey #define MAXPUSHBACK 128
223 2c251c14 2020-01-15 tracey
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;
228 2c251c14 2020-01-15 tracey
229 2c251c14 2020-01-15 tracey int
230 2c251c14 2020-01-15 tracey lgetc(int quotec)
231 2c251c14 2020-01-15 tracey {
232 2c251c14 2020-01-15 tracey int c, next;
233 2c251c14 2020-01-15 tracey
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;
241 2c251c14 2020-01-15 tracey } else
242 2c251c14 2020-01-15 tracey parseindex++;
243 2c251c14 2020-01-15 tracey }
244 2c251c14 2020-01-15 tracey
245 2c251c14 2020-01-15 tracey if (pushback_index)
246 2c251c14 2020-01-15 tracey return (pushback_buffer[--pushback_index]);
247 2c251c14 2020-01-15 tracey
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);
255 2c251c14 2020-01-15 tracey }
256 2c251c14 2020-01-15 tracey return (c);
257 2c251c14 2020-01-15 tracey }
258 2c251c14 2020-01-15 tracey
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;
263 2c251c14 2020-01-15 tracey break;
264 2c251c14 2020-01-15 tracey }
265 2c251c14 2020-01-15 tracey yylval.lineno = file->lineno;
266 2c251c14 2020-01-15 tracey file->lineno++;
267 2c251c14 2020-01-15 tracey }
268 2c251c14 2020-01-15 tracey
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);
273 2c251c14 2020-01-15 tracey }
274 2c251c14 2020-01-15 tracey return (c);
275 2c251c14 2020-01-15 tracey }
276 2c251c14 2020-01-15 tracey
277 2c251c14 2020-01-15 tracey int
278 2c251c14 2020-01-15 tracey lungetc(int c)
279 2c251c14 2020-01-15 tracey {
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);
286 2c251c14 2020-01-15 tracey }
287 2c251c14 2020-01-15 tracey if (pushback_index < MAXPUSHBACK-1)
288 2c251c14 2020-01-15 tracey return (pushback_buffer[pushback_index++] = c);
289 2c251c14 2020-01-15 tracey else
290 2c251c14 2020-01-15 tracey return (EOF);
291 2c251c14 2020-01-15 tracey }
292 2c251c14 2020-01-15 tracey
293 2c251c14 2020-01-15 tracey int
294 2c251c14 2020-01-15 tracey findeol(void)
295 2c251c14 2020-01-15 tracey {
296 2c251c14 2020-01-15 tracey int c;
297 2c251c14 2020-01-15 tracey
298 2c251c14 2020-01-15 tracey parsebuf = NULL;
299 2c251c14 2020-01-15 tracey
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];
304 2c251c14 2020-01-15 tracey else
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++;
308 2c251c14 2020-01-15 tracey break;
309 2c251c14 2020-01-15 tracey }
310 2c251c14 2020-01-15 tracey if (c == EOF)
311 2c251c14 2020-01-15 tracey break;
312 2c251c14 2020-01-15 tracey }
313 2c251c14 2020-01-15 tracey return (ERROR);
314 2c251c14 2020-01-15 tracey }
315 2c251c14 2020-01-15 tracey
316 2c251c14 2020-01-15 tracey int
317 2c251c14 2020-01-15 tracey yylex(void)
318 2c251c14 2020-01-15 tracey {
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;
323 2c251c14 2020-01-15 tracey
324 2c251c14 2020-01-15 tracey p = buf;
325 2c251c14 2020-01-15 tracey while ((c = lgetc(0)) == ' ' || c == '\t')
326 2c251c14 2020-01-15 tracey ; /* nothing */
327 2c251c14 2020-01-15 tracey
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 */
332 2c251c14 2020-01-15 tracey
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;
352 2c251c14 2020-01-15 tracey } else
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';
356 2c251c14 2020-01-15 tracey break;
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());
360 2c251c14 2020-01-15 tracey }
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());
364 2c251c14 2020-01-15 tracey }
365 2c251c14 2020-01-15 tracey *p++ = c;
366 2c251c14 2020-01-15 tracey }
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);
371 2c251c14 2020-01-15 tracey }
372 2c251c14 2020-01-15 tracey
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 == '=')
375 2c251c14 2020-01-15 tracey
376 2c251c14 2020-01-15 tracey if (c == '-' || isdigit(c)) {
377 2c251c14 2020-01-15 tracey do {
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());
382 2c251c14 2020-01-15 tracey }
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;
389 2c251c14 2020-01-15 tracey
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());
397 2c251c14 2020-01-15 tracey }
398 2c251c14 2020-01-15 tracey return (NUMBER);
399 2c251c14 2020-01-15 tracey } else {
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);
406 2c251c14 2020-01-15 tracey }
407 2c251c14 2020-01-15 tracey }
408 2c251c14 2020-01-15 tracey
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 != ','))
414 2c251c14 2020-01-15 tracey
415 2c251c14 2020-01-15 tracey if (isalnum(c) || c == ':' || c == '_' || c == '*') {
416 2c251c14 2020-01-15 tracey do {
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());
421 2c251c14 2020-01-15 tracey }
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);
429 2c251c14 2020-01-15 tracey }
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++;
433 2c251c14 2020-01-15 tracey }
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);
437 2c251c14 2020-01-15 tracey }
438 2c251c14 2020-01-15 tracey
439 2c251c14 2020-01-15 tracey struct file *
440 2c251c14 2020-01-15 tracey pushfile(const char *name)
441 2c251c14 2020-01-15 tracey {
442 2c251c14 2020-01-15 tracey struct file *nfile;
443 2c251c14 2020-01-15 tracey
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);
447 2c251c14 2020-01-15 tracey }
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);
452 2c251c14 2020-01-15 tracey }
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);
458 2c251c14 2020-01-15 tracey }
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);
462 2c251c14 2020-01-15 tracey }
463 2c251c14 2020-01-15 tracey
464 2c251c14 2020-01-15 tracey int
465 2c251c14 2020-01-15 tracey popfile(void)
466 2c251c14 2020-01-15 tracey {
467 2c251c14 2020-01-15 tracey struct file *prev;
468 2c251c14 2020-01-15 tracey
469 2c251c14 2020-01-15 tracey if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
470 2c251c14 2020-01-15 tracey prev->errors += file->errors;
471 2c251c14 2020-01-15 tracey
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);
478 2c251c14 2020-01-15 tracey }
479 2c251c14 2020-01-15 tracey
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)
482 2c251c14 2020-01-15 tracey {
483 2c251c14 2020-01-15 tracey static const struct got_error* error = NULL;
484 2c251c14 2020-01-15 tracey
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;
508 2c251c14 2020-01-15 tracey }
509 2c251c14 2020-01-15 tracey topfile = file;
510 2c251c14 2020-01-15 tracey
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;
515 2c251c14 2020-01-15 tracey done:
516 2c251c14 2020-01-15 tracey return error;
517 2c251c14 2020-01-15 tracey }