Blame


1 fb863fa4 2020-06-22 tracey /*
2 c34ec417 2020-06-22 tracey * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
3 fb863fa4 2020-06-22 tracey * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 fb863fa4 2020-06-22 tracey * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 fb863fa4 2020-06-22 tracey * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 fb863fa4 2020-06-22 tracey * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 fb863fa4 2020-06-22 tracey * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 fb863fa4 2020-06-22 tracey * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 fb863fa4 2020-06-22 tracey *
10 fb863fa4 2020-06-22 tracey * Permission to use, copy, modify, and distribute this software for any
11 fb863fa4 2020-06-22 tracey * purpose with or without fee is hereby granted, provided that the above
12 fb863fa4 2020-06-22 tracey * copyright notice and this permission notice appear in all copies.
13 fb863fa4 2020-06-22 tracey *
14 fb863fa4 2020-06-22 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 fb863fa4 2020-06-22 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 fb863fa4 2020-06-22 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 fb863fa4 2020-06-22 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 fb863fa4 2020-06-22 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 fb863fa4 2020-06-22 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 fb863fa4 2020-06-22 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 fb863fa4 2020-06-22 tracey */
22 fb863fa4 2020-06-22 tracey
23 fb863fa4 2020-06-22 tracey %{
24 fb863fa4 2020-06-22 tracey #include <sys/types.h>
25 fb863fa4 2020-06-22 tracey #include <sys/queue.h>
26 fb863fa4 2020-06-22 tracey #include <sys/socket.h>
27 fb863fa4 2020-06-22 tracey #include <sys/stat.h>
28 fb863fa4 2020-06-22 tracey
29 fb863fa4 2020-06-22 tracey #include <netinet/in.h>
30 fb863fa4 2020-06-22 tracey
31 fb863fa4 2020-06-22 tracey #include <arpa/inet.h>
32 fb863fa4 2020-06-22 tracey
33 fb863fa4 2020-06-22 tracey #include <ctype.h>
34 fb863fa4 2020-06-22 tracey #include <err.h>
35 fb863fa4 2020-06-22 tracey #include <errno.h>
36 fb863fa4 2020-06-22 tracey #include <event.h>
37 fb863fa4 2020-06-22 tracey #include <ifaddrs.h>
38 fb863fa4 2020-06-22 tracey #include <imsg.h>
39 fb863fa4 2020-06-22 tracey #include <limits.h>
40 fb863fa4 2020-06-22 tracey #include <stdarg.h>
41 fb863fa4 2020-06-22 tracey #include <stdio.h>
42 fb863fa4 2020-06-22 tracey #include <string.h>
43 fb863fa4 2020-06-22 tracey #include <syslog.h>
44 fb863fa4 2020-06-22 tracey #include <unistd.h>
45 fb863fa4 2020-06-22 tracey
46 fb863fa4 2020-06-22 tracey #include "got_error.h"
47 fb863fa4 2020-06-22 tracey #include "got.h"
48 fb863fa4 2020-06-22 tracey
49 fb863fa4 2020-06-22 tracey TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
50 fb863fa4 2020-06-22 tracey static struct file {
51 fb863fa4 2020-06-22 tracey TAILQ_ENTRY(file) entry;
52 fb863fa4 2020-06-22 tracey FILE *stream;
53 fb863fa4 2020-06-22 tracey char *name;
54 fb863fa4 2020-06-22 tracey size_t ungetpos;
55 fb863fa4 2020-06-22 tracey size_t ungetsize;
56 fb863fa4 2020-06-22 tracey u_char *ungetbuf;
57 fb863fa4 2020-06-22 tracey int eof_reached;
58 fb863fa4 2020-06-22 tracey int lineno;
59 fb863fa4 2020-06-22 tracey } *file, *topfile;
60 fb863fa4 2020-06-22 tracey static const struct got_error* pushfile(struct file**, const char *);
61 fb863fa4 2020-06-22 tracey int popfile(void);
62 fb863fa4 2020-06-22 tracey int yyparse(void);
63 fb863fa4 2020-06-22 tracey int yylex(void);
64 fb863fa4 2020-06-22 tracey int yyerror(const char *, ...)
65 fb863fa4 2020-06-22 tracey __attribute__((__format__ (printf, 1, 2)))
66 fb863fa4 2020-06-22 tracey __attribute__((__nonnull__ (1)));
67 fb863fa4 2020-06-22 tracey int kw_cmp(const void *, const void *);
68 fb863fa4 2020-06-22 tracey int lookup(char *);
69 fb863fa4 2020-06-22 tracey int igetc(void);
70 fb863fa4 2020-06-22 tracey int lgetc(int);
71 fb863fa4 2020-06-22 tracey void lungetc(int);
72 fb863fa4 2020-06-22 tracey int findeol(void);
73 fb863fa4 2020-06-22 tracey
74 fb863fa4 2020-06-22 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 fb863fa4 2020-06-22 tracey struct sym {
76 fb863fa4 2020-06-22 tracey TAILQ_ENTRY(sym) entry;
77 fb863fa4 2020-06-22 tracey int used;
78 fb863fa4 2020-06-22 tracey int persist;
79 fb863fa4 2020-06-22 tracey char *nam;
80 fb863fa4 2020-06-22 tracey char *val;
81 fb863fa4 2020-06-22 tracey };
82 fb863fa4 2020-06-22 tracey
83 fb863fa4 2020-06-22 tracey int symset(const char *, const char *, int);
84 fb863fa4 2020-06-22 tracey char *symget(const char *);
85 fb863fa4 2020-06-22 tracey
86 fb863fa4 2020-06-22 tracey const struct got_error* gerror = NULL;
87 fb863fa4 2020-06-22 tracey struct got_config_list_entry *gotconfig;
88 fb863fa4 2020-06-22 tracey struct got_config_list got_config_list;
89 fb863fa4 2020-06-22 tracey static const struct got_error* new_remote(struct got_config_list_entry **);
90 fb863fa4 2020-06-22 tracey
91 fb863fa4 2020-06-22 tracey typedef struct {
92 fb863fa4 2020-06-22 tracey union {
93 fb863fa4 2020-06-22 tracey int64_t number;
94 fb863fa4 2020-06-22 tracey char *string;
95 fb863fa4 2020-06-22 tracey } v;
96 fb863fa4 2020-06-22 tracey int lineno;
97 fb863fa4 2020-06-22 tracey } YYSTYPE;
98 fb863fa4 2020-06-22 tracey
99 fb863fa4 2020-06-22 tracey %}
100 fb863fa4 2020-06-22 tracey
101 fb863fa4 2020-06-22 tracey %token ERROR
102 fb863fa4 2020-06-22 tracey %token REMOTE REPOSITORY SERVER PROTOCOL USER
103 fb863fa4 2020-06-22 tracey %token <v.string> STRING
104 fb863fa4 2020-06-22 tracey %token <v.number> NUMBER
105 fb863fa4 2020-06-22 tracey
106 fb863fa4 2020-06-22 tracey %%
107 fb863fa4 2020-06-22 tracey
108 fb863fa4 2020-06-22 tracey grammar : /* empty */
109 fb863fa4 2020-06-22 tracey | grammar '\n'
110 fb863fa4 2020-06-22 tracey | grammar remote '\n'
111 fb863fa4 2020-06-22 tracey ;
112 fb863fa4 2020-06-22 tracey remoteopts2 : remoteopts2 remoteopts1 nl
113 fb863fa4 2020-06-22 tracey | remoteopts1 optnl
114 fb863fa4 2020-06-22 tracey ;
115 fb863fa4 2020-06-22 tracey remoteopts1 : REPOSITORY STRING {
116 fb863fa4 2020-06-22 tracey gotconfig->repository = strdup($2);
117 fb863fa4 2020-06-22 tracey if (gotconfig->repository == NULL) {
118 fb863fa4 2020-06-22 tracey free($2);
119 fb863fa4 2020-06-22 tracey yyerror("strdup");
120 fb863fa4 2020-06-22 tracey YYERROR;
121 fb863fa4 2020-06-22 tracey }
122 fb863fa4 2020-06-22 tracey free($2);
123 fb863fa4 2020-06-22 tracey }
124 fb863fa4 2020-06-22 tracey | SERVER STRING {
125 fb863fa4 2020-06-22 tracey gotconfig->server = strdup($2);
126 fb863fa4 2020-06-22 tracey if (gotconfig->server == NULL) {
127 fb863fa4 2020-06-22 tracey free($2);
128 fb863fa4 2020-06-22 tracey yyerror("strdup");
129 fb863fa4 2020-06-22 tracey YYERROR;
130 fb863fa4 2020-06-22 tracey }
131 fb863fa4 2020-06-22 tracey free($2);
132 fb863fa4 2020-06-22 tracey }
133 fb863fa4 2020-06-22 tracey | PROTOCOL STRING {
134 fb863fa4 2020-06-22 tracey gotconfig->protocol = strdup($2);
135 fb863fa4 2020-06-22 tracey if (gotconfig->protocol == NULL) {
136 fb863fa4 2020-06-22 tracey free($2);
137 fb863fa4 2020-06-22 tracey yyerror("strdup");
138 fb863fa4 2020-06-22 tracey YYERROR;
139 fb863fa4 2020-06-22 tracey }
140 fb863fa4 2020-06-22 tracey free($2);
141 fb863fa4 2020-06-22 tracey }
142 fb863fa4 2020-06-22 tracey | USER STRING {
143 fb863fa4 2020-06-22 tracey gotconfig->user = strdup($2);
144 fb863fa4 2020-06-22 tracey if (gotconfig->user == NULL) {
145 fb863fa4 2020-06-22 tracey free($2);
146 fb863fa4 2020-06-22 tracey yyerror("strdup");
147 fb863fa4 2020-06-22 tracey YYERROR;
148 fb863fa4 2020-06-22 tracey }
149 fb863fa4 2020-06-22 tracey free($2);
150 fb863fa4 2020-06-22 tracey }
151 fb863fa4 2020-06-22 tracey ;
152 fb863fa4 2020-06-22 tracey remote : REMOTE STRING {
153 fb863fa4 2020-06-22 tracey static const struct got_error* error;
154 fb863fa4 2020-06-22 tracey
155 fb863fa4 2020-06-22 tracey error = new_remote(&gotconfig);
156 fb863fa4 2020-06-22 tracey if (error) {
157 fb863fa4 2020-06-22 tracey free($2);
158 fb863fa4 2020-06-22 tracey yyerror("%s", error->msg);
159 fb863fa4 2020-06-22 tracey YYERROR;
160 fb863fa4 2020-06-22 tracey }
161 fb863fa4 2020-06-22 tracey gotconfig->remote = strdup($2);
162 fb863fa4 2020-06-22 tracey if (gotconfig->remote == NULL) {
163 fb863fa4 2020-06-22 tracey free($2);
164 fb863fa4 2020-06-22 tracey yyerror("strdup");
165 fb863fa4 2020-06-22 tracey YYERROR;
166 fb863fa4 2020-06-22 tracey }
167 fb863fa4 2020-06-22 tracey free($2);
168 fb863fa4 2020-06-22 tracey } '{' optnl remoteopts2 '}' {
169 fb863fa4 2020-06-22 tracey TAILQ_INSERT_TAIL(&got_config_list, gotconfig, entry);
170 fb863fa4 2020-06-22 tracey }
171 fb863fa4 2020-06-22 tracey ;
172 fb863fa4 2020-06-22 tracey optnl : '\n' optnl
173 fb863fa4 2020-06-22 tracey | /* empty */
174 fb863fa4 2020-06-22 tracey ;
175 fb863fa4 2020-06-22 tracey nl : '\n' optnl
176 fb863fa4 2020-06-22 tracey ;
177 fb863fa4 2020-06-22 tracey %%
178 fb863fa4 2020-06-22 tracey
179 fb863fa4 2020-06-22 tracey struct keywords {
180 fb863fa4 2020-06-22 tracey const char *k_name;
181 fb863fa4 2020-06-22 tracey int k_val;
182 fb863fa4 2020-06-22 tracey };
183 fb863fa4 2020-06-22 tracey
184 fb863fa4 2020-06-22 tracey int
185 fb863fa4 2020-06-22 tracey yyerror(const char *fmt, ...)
186 fb863fa4 2020-06-22 tracey {
187 fb863fa4 2020-06-22 tracey va_list ap;
188 fb863fa4 2020-06-22 tracey char *msg;
189 fb863fa4 2020-06-22 tracey char *err = NULL;
190 fb863fa4 2020-06-22 tracey
191 fb863fa4 2020-06-22 tracey va_start(ap, fmt);
192 fb863fa4 2020-06-22 tracey if (vasprintf(&msg, fmt, ap) == -1) {
193 fb863fa4 2020-06-22 tracey gerror = got_error_from_errno("vasprintf");
194 fb863fa4 2020-06-22 tracey return 0;
195 fb863fa4 2020-06-22 tracey }
196 fb863fa4 2020-06-22 tracey va_end(ap);
197 fb863fa4 2020-06-22 tracey if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
198 fb863fa4 2020-06-22 tracey gerror = got_error_from_errno("asprintf");
199 fb863fa4 2020-06-22 tracey return(0);
200 fb863fa4 2020-06-22 tracey }
201 fb863fa4 2020-06-22 tracey gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
202 fb863fa4 2020-06-22 tracey free(msg);
203 fb863fa4 2020-06-22 tracey free(err);
204 fb863fa4 2020-06-22 tracey return(0);
205 fb863fa4 2020-06-22 tracey }
206 fb863fa4 2020-06-22 tracey
207 fb863fa4 2020-06-22 tracey int
208 fb863fa4 2020-06-22 tracey kw_cmp(const void *k, const void *e)
209 fb863fa4 2020-06-22 tracey {
210 fb863fa4 2020-06-22 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
211 fb863fa4 2020-06-22 tracey }
212 fb863fa4 2020-06-22 tracey
213 fb863fa4 2020-06-22 tracey int
214 fb863fa4 2020-06-22 tracey lookup(char *s)
215 fb863fa4 2020-06-22 tracey {
216 fb863fa4 2020-06-22 tracey /* This has to be sorted always. */
217 fb863fa4 2020-06-22 tracey static const struct keywords keywords[] = {
218 fb863fa4 2020-06-22 tracey {"protocol", PROTOCOL},
219 fb863fa4 2020-06-22 tracey {"remote", REMOTE},
220 fb863fa4 2020-06-22 tracey {"repository", REPOSITORY},
221 fb863fa4 2020-06-22 tracey {"server", SERVER},
222 fb863fa4 2020-06-22 tracey {"user", USER},
223 fb863fa4 2020-06-22 tracey };
224 fb863fa4 2020-06-22 tracey const struct keywords *p;
225 fb863fa4 2020-06-22 tracey
226 fb863fa4 2020-06-22 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
227 fb863fa4 2020-06-22 tracey sizeof(keywords[0]), kw_cmp);
228 fb863fa4 2020-06-22 tracey
229 fb863fa4 2020-06-22 tracey if (p)
230 fb863fa4 2020-06-22 tracey return (p->k_val);
231 fb863fa4 2020-06-22 tracey else
232 fb863fa4 2020-06-22 tracey return (STRING);
233 fb863fa4 2020-06-22 tracey }
234 fb863fa4 2020-06-22 tracey
235 fb863fa4 2020-06-22 tracey #define START_EXPAND 1
236 fb863fa4 2020-06-22 tracey #define DONE_EXPAND 2
237 fb863fa4 2020-06-22 tracey
238 fb863fa4 2020-06-22 tracey static int expanding;
239 fb863fa4 2020-06-22 tracey
240 fb863fa4 2020-06-22 tracey int
241 fb863fa4 2020-06-22 tracey igetc(void)
242 fb863fa4 2020-06-22 tracey {
243 fb863fa4 2020-06-22 tracey int c;
244 fb863fa4 2020-06-22 tracey
245 fb863fa4 2020-06-22 tracey while (1) {
246 fb863fa4 2020-06-22 tracey if (file->ungetpos > 0)
247 fb863fa4 2020-06-22 tracey c = file->ungetbuf[--file->ungetpos];
248 fb863fa4 2020-06-22 tracey else
249 fb863fa4 2020-06-22 tracey c = getc(file->stream);
250 fb863fa4 2020-06-22 tracey
251 fb863fa4 2020-06-22 tracey if (c == START_EXPAND)
252 fb863fa4 2020-06-22 tracey expanding = 1;
253 fb863fa4 2020-06-22 tracey else if (c == DONE_EXPAND)
254 fb863fa4 2020-06-22 tracey expanding = 0;
255 fb863fa4 2020-06-22 tracey else
256 fb863fa4 2020-06-22 tracey break;
257 fb863fa4 2020-06-22 tracey }
258 fb863fa4 2020-06-22 tracey return (c);
259 fb863fa4 2020-06-22 tracey }
260 fb863fa4 2020-06-22 tracey
261 fb863fa4 2020-06-22 tracey int
262 fb863fa4 2020-06-22 tracey lgetc(int quotec)
263 fb863fa4 2020-06-22 tracey {
264 fb863fa4 2020-06-22 tracey int c, next;
265 fb863fa4 2020-06-22 tracey
266 fb863fa4 2020-06-22 tracey if (quotec) {
267 fb863fa4 2020-06-22 tracey if ((c = igetc()) == EOF) {
268 fb863fa4 2020-06-22 tracey yyerror("reached end of file while parsing "
269 fb863fa4 2020-06-22 tracey "quoted string");
270 fb863fa4 2020-06-22 tracey if (file == topfile || popfile() == EOF)
271 fb863fa4 2020-06-22 tracey return (EOF);
272 fb863fa4 2020-06-22 tracey return (quotec);
273 fb863fa4 2020-06-22 tracey }
274 fb863fa4 2020-06-22 tracey return (c);
275 fb863fa4 2020-06-22 tracey }
276 fb863fa4 2020-06-22 tracey
277 fb863fa4 2020-06-22 tracey while ((c = igetc()) == '\\') {
278 fb863fa4 2020-06-22 tracey next = igetc();
279 fb863fa4 2020-06-22 tracey if (next != '\n') {
280 fb863fa4 2020-06-22 tracey c = next;
281 fb863fa4 2020-06-22 tracey break;
282 fb863fa4 2020-06-22 tracey }
283 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
284 fb863fa4 2020-06-22 tracey file->lineno++;
285 fb863fa4 2020-06-22 tracey }
286 fb863fa4 2020-06-22 tracey
287 fb863fa4 2020-06-22 tracey if (c == EOF) {
288 fb863fa4 2020-06-22 tracey /*
289 fb863fa4 2020-06-22 tracey * Fake EOL when hit EOF for the first time. This gets line
290 fb863fa4 2020-06-22 tracey * count right if last line in included file is syntactically
291 fb863fa4 2020-06-22 tracey * invalid and has no newline.
292 fb863fa4 2020-06-22 tracey */
293 fb863fa4 2020-06-22 tracey if (file->eof_reached == 0) {
294 fb863fa4 2020-06-22 tracey file->eof_reached = 1;
295 fb863fa4 2020-06-22 tracey return ('\n');
296 fb863fa4 2020-06-22 tracey }
297 fb863fa4 2020-06-22 tracey while (c == EOF) {
298 fb863fa4 2020-06-22 tracey if (file == topfile || popfile() == EOF)
299 fb863fa4 2020-06-22 tracey return (EOF);
300 fb863fa4 2020-06-22 tracey c = igetc();
301 fb863fa4 2020-06-22 tracey }
302 fb863fa4 2020-06-22 tracey }
303 fb863fa4 2020-06-22 tracey return (c);
304 fb863fa4 2020-06-22 tracey }
305 fb863fa4 2020-06-22 tracey
306 fb863fa4 2020-06-22 tracey void
307 fb863fa4 2020-06-22 tracey lungetc(int c)
308 fb863fa4 2020-06-22 tracey {
309 fb863fa4 2020-06-22 tracey if (c == EOF)
310 fb863fa4 2020-06-22 tracey return;
311 fb863fa4 2020-06-22 tracey
312 fb863fa4 2020-06-22 tracey if (file->ungetpos >= file->ungetsize) {
313 fb863fa4 2020-06-22 tracey void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
314 fb863fa4 2020-06-22 tracey if (p == NULL)
315 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
316 fb863fa4 2020-06-22 tracey file->ungetbuf = p;
317 fb863fa4 2020-06-22 tracey file->ungetsize *= 2;
318 fb863fa4 2020-06-22 tracey }
319 fb863fa4 2020-06-22 tracey file->ungetbuf[file->ungetpos++] = c;
320 fb863fa4 2020-06-22 tracey }
321 fb863fa4 2020-06-22 tracey
322 fb863fa4 2020-06-22 tracey int
323 fb863fa4 2020-06-22 tracey findeol(void)
324 fb863fa4 2020-06-22 tracey {
325 fb863fa4 2020-06-22 tracey int c;
326 fb863fa4 2020-06-22 tracey
327 fb863fa4 2020-06-22 tracey /* Skip to either EOF or the first real EOL. */
328 fb863fa4 2020-06-22 tracey while (1) {
329 fb863fa4 2020-06-22 tracey c = lgetc(0);
330 fb863fa4 2020-06-22 tracey if (c == '\n') {
331 fb863fa4 2020-06-22 tracey file->lineno++;
332 fb863fa4 2020-06-22 tracey break;
333 fb863fa4 2020-06-22 tracey }
334 fb863fa4 2020-06-22 tracey if (c == EOF)
335 fb863fa4 2020-06-22 tracey break;
336 fb863fa4 2020-06-22 tracey }
337 fb863fa4 2020-06-22 tracey return (ERROR);
338 fb863fa4 2020-06-22 tracey }
339 fb863fa4 2020-06-22 tracey
340 fb863fa4 2020-06-22 tracey int
341 fb863fa4 2020-06-22 tracey yylex(void)
342 fb863fa4 2020-06-22 tracey {
343 fb863fa4 2020-06-22 tracey unsigned char buf[8096];
344 fb863fa4 2020-06-22 tracey unsigned char *p, *val;
345 fb863fa4 2020-06-22 tracey int quotec, next, c;
346 fb863fa4 2020-06-22 tracey int token;
347 fb863fa4 2020-06-22 tracey
348 fb863fa4 2020-06-22 tracey top:
349 fb863fa4 2020-06-22 tracey p = buf;
350 fb863fa4 2020-06-22 tracey while ((c = lgetc(0)) == ' ' || c == '\t')
351 fb863fa4 2020-06-22 tracey ; /* nothing */
352 fb863fa4 2020-06-22 tracey
353 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
354 fb863fa4 2020-06-22 tracey if (c == '#')
355 fb863fa4 2020-06-22 tracey while ((c = lgetc(0)) != '\n' && c != EOF)
356 fb863fa4 2020-06-22 tracey ; /* nothing */
357 fb863fa4 2020-06-22 tracey if (c == '$' && !expanding) {
358 fb863fa4 2020-06-22 tracey while (1) {
359 fb863fa4 2020-06-22 tracey if ((c = lgetc(0)) == EOF)
360 fb863fa4 2020-06-22 tracey return (0);
361 fb863fa4 2020-06-22 tracey
362 fb863fa4 2020-06-22 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
363 fb863fa4 2020-06-22 tracey yyerror("string too long");
364 fb863fa4 2020-06-22 tracey return (findeol());
365 fb863fa4 2020-06-22 tracey }
366 fb863fa4 2020-06-22 tracey if (isalnum(c) || c == '_') {
367 fb863fa4 2020-06-22 tracey *p++ = c;
368 fb863fa4 2020-06-22 tracey continue;
369 fb863fa4 2020-06-22 tracey }
370 fb863fa4 2020-06-22 tracey *p = '\0';
371 fb863fa4 2020-06-22 tracey lungetc(c);
372 fb863fa4 2020-06-22 tracey break;
373 fb863fa4 2020-06-22 tracey }
374 fb863fa4 2020-06-22 tracey val = symget(buf);
375 fb863fa4 2020-06-22 tracey if (val == NULL) {
376 fb863fa4 2020-06-22 tracey yyerror("macro '%s' not defined", buf);
377 fb863fa4 2020-06-22 tracey return (findeol());
378 fb863fa4 2020-06-22 tracey }
379 fb863fa4 2020-06-22 tracey p = val + strlen(val) - 1;
380 fb863fa4 2020-06-22 tracey lungetc(DONE_EXPAND);
381 fb863fa4 2020-06-22 tracey while (p >= val) {
382 fb863fa4 2020-06-22 tracey lungetc(*p);
383 fb863fa4 2020-06-22 tracey p--;
384 fb863fa4 2020-06-22 tracey }
385 fb863fa4 2020-06-22 tracey lungetc(START_EXPAND);
386 fb863fa4 2020-06-22 tracey goto top;
387 fb863fa4 2020-06-22 tracey }
388 fb863fa4 2020-06-22 tracey
389 fb863fa4 2020-06-22 tracey switch (c) {
390 fb863fa4 2020-06-22 tracey case '\'':
391 fb863fa4 2020-06-22 tracey case '"':
392 fb863fa4 2020-06-22 tracey quotec = c;
393 fb863fa4 2020-06-22 tracey while (1) {
394 fb863fa4 2020-06-22 tracey if ((c = lgetc(quotec)) == EOF)
395 fb863fa4 2020-06-22 tracey return (0);
396 fb863fa4 2020-06-22 tracey if (c == '\n') {
397 fb863fa4 2020-06-22 tracey file->lineno++;
398 fb863fa4 2020-06-22 tracey continue;
399 fb863fa4 2020-06-22 tracey } else if (c == '\\') {
400 fb863fa4 2020-06-22 tracey if ((next = lgetc(quotec)) == EOF)
401 fb863fa4 2020-06-22 tracey return (0);
402 fb863fa4 2020-06-22 tracey if (next == quotec || c == ' ' || c == '\t')
403 fb863fa4 2020-06-22 tracey c = next;
404 fb863fa4 2020-06-22 tracey else if (next == '\n') {
405 fb863fa4 2020-06-22 tracey file->lineno++;
406 fb863fa4 2020-06-22 tracey continue;
407 fb863fa4 2020-06-22 tracey } else
408 fb863fa4 2020-06-22 tracey lungetc(next);
409 fb863fa4 2020-06-22 tracey } else if (c == quotec) {
410 fb863fa4 2020-06-22 tracey *p = '\0';
411 fb863fa4 2020-06-22 tracey break;
412 fb863fa4 2020-06-22 tracey } else if (c == '\0') {
413 fb863fa4 2020-06-22 tracey yyerror("syntax error");
414 fb863fa4 2020-06-22 tracey return (findeol());
415 fb863fa4 2020-06-22 tracey }
416 fb863fa4 2020-06-22 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
417 fb863fa4 2020-06-22 tracey yyerror("string too long");
418 fb863fa4 2020-06-22 tracey return (findeol());
419 fb863fa4 2020-06-22 tracey }
420 fb863fa4 2020-06-22 tracey *p++ = c;
421 fb863fa4 2020-06-22 tracey }
422 fb863fa4 2020-06-22 tracey yylval.v.string = strdup(buf);
423 fb863fa4 2020-06-22 tracey if (yylval.v.string == NULL)
424 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
425 fb863fa4 2020-06-22 tracey return (STRING);
426 fb863fa4 2020-06-22 tracey }
427 fb863fa4 2020-06-22 tracey
428 fb863fa4 2020-06-22 tracey #define allowed_to_end_number(x) \
429 fb863fa4 2020-06-22 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
430 fb863fa4 2020-06-22 tracey
431 fb863fa4 2020-06-22 tracey if (c == '-' || isdigit(c)) {
432 fb863fa4 2020-06-22 tracey do {
433 fb863fa4 2020-06-22 tracey *p++ = c;
434 fb863fa4 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
435 fb863fa4 2020-06-22 tracey yyerror("string too long");
436 fb863fa4 2020-06-22 tracey return (findeol());
437 fb863fa4 2020-06-22 tracey }
438 fb863fa4 2020-06-22 tracey } while ((c = lgetc(0)) != EOF && isdigit(c));
439 fb863fa4 2020-06-22 tracey lungetc(c);
440 fb863fa4 2020-06-22 tracey if (p == buf + 1 && buf[0] == '-')
441 fb863fa4 2020-06-22 tracey goto nodigits;
442 fb863fa4 2020-06-22 tracey if (c == EOF || allowed_to_end_number(c)) {
443 fb863fa4 2020-06-22 tracey const char *errstr = NULL;
444 fb863fa4 2020-06-22 tracey
445 fb863fa4 2020-06-22 tracey *p = '\0';
446 fb863fa4 2020-06-22 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
447 fb863fa4 2020-06-22 tracey LLONG_MAX, &errstr);
448 fb863fa4 2020-06-22 tracey if (errstr) {
449 fb863fa4 2020-06-22 tracey yyerror("\"%s\" invalid number: %s",
450 fb863fa4 2020-06-22 tracey buf, errstr);
451 fb863fa4 2020-06-22 tracey return (findeol());
452 fb863fa4 2020-06-22 tracey }
453 fb863fa4 2020-06-22 tracey return (NUMBER);
454 fb863fa4 2020-06-22 tracey } else {
455 fb863fa4 2020-06-22 tracey nodigits:
456 fb863fa4 2020-06-22 tracey while (p > buf + 1)
457 fb863fa4 2020-06-22 tracey lungetc(*--p);
458 fb863fa4 2020-06-22 tracey c = *--p;
459 fb863fa4 2020-06-22 tracey if (c == '-')
460 fb863fa4 2020-06-22 tracey return (c);
461 fb863fa4 2020-06-22 tracey }
462 fb863fa4 2020-06-22 tracey }
463 fb863fa4 2020-06-22 tracey
464 fb863fa4 2020-06-22 tracey #define allowed_in_string(x) \
465 fb863fa4 2020-06-22 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
466 fb863fa4 2020-06-22 tracey x != '{' && x != '}' && \
467 fb863fa4 2020-06-22 tracey x != '!' && x != '=' && x != '#' && \
468 fb863fa4 2020-06-22 tracey x != ','))
469 fb863fa4 2020-06-22 tracey
470 fb863fa4 2020-06-22 tracey if (isalnum(c) || c == ':' || c == '_') {
471 fb863fa4 2020-06-22 tracey do {
472 fb863fa4 2020-06-22 tracey *p++ = c;
473 fb863fa4 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
474 fb863fa4 2020-06-22 tracey yyerror("string too long");
475 fb863fa4 2020-06-22 tracey return (findeol());
476 fb863fa4 2020-06-22 tracey }
477 fb863fa4 2020-06-22 tracey } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
478 fb863fa4 2020-06-22 tracey lungetc(c);
479 fb863fa4 2020-06-22 tracey *p = '\0';
480 fb863fa4 2020-06-22 tracey if ((token = lookup(buf)) == STRING)
481 fb863fa4 2020-06-22 tracey if ((yylval.v.string = strdup(buf)) == NULL)
482 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
483 fb863fa4 2020-06-22 tracey return (token);
484 fb863fa4 2020-06-22 tracey }
485 fb863fa4 2020-06-22 tracey if (c == '\n') {
486 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
487 fb863fa4 2020-06-22 tracey file->lineno++;
488 fb863fa4 2020-06-22 tracey }
489 fb863fa4 2020-06-22 tracey if (c == EOF)
490 fb863fa4 2020-06-22 tracey return (0);
491 fb863fa4 2020-06-22 tracey return (c);
492 fb863fa4 2020-06-22 tracey }
493 fb863fa4 2020-06-22 tracey
494 fb863fa4 2020-06-22 tracey static const struct got_error*
495 fb863fa4 2020-06-22 tracey pushfile(struct file **nfile, const char *name)
496 fb863fa4 2020-06-22 tracey {
497 fb863fa4 2020-06-22 tracey const struct got_error* error = NULL;
498 fb863fa4 2020-06-22 tracey
499 fb863fa4 2020-06-22 tracey if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
500 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "calloc");
501 fb863fa4 2020-06-22 tracey if (((*nfile)->name = strdup(name)) == NULL) {
502 fb863fa4 2020-06-22 tracey free(nfile);
503 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "strdup");
504 fb863fa4 2020-06-22 tracey }
505 fb863fa4 2020-06-22 tracey if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
506 fb863fa4 2020-06-22 tracey char *msg = NULL;
507 fb863fa4 2020-06-22 tracey if (asprintf(&msg, "%s", (*nfile)->name) == -1)
508 fb863fa4 2020-06-22 tracey return got_error_from_errno("asprintf");
509 fb863fa4 2020-06-22 tracey error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
510 fb863fa4 2020-06-22 tracey free((*nfile)->name);
511 fb863fa4 2020-06-22 tracey free((*nfile));
512 fb863fa4 2020-06-22 tracey free(msg);
513 fb863fa4 2020-06-22 tracey return error;
514 fb863fa4 2020-06-22 tracey }
515 fb863fa4 2020-06-22 tracey (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
516 fb863fa4 2020-06-22 tracey (*nfile)->ungetsize = 16;
517 fb863fa4 2020-06-22 tracey (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
518 fb863fa4 2020-06-22 tracey if ((*nfile)->ungetbuf == NULL) {
519 fb863fa4 2020-06-22 tracey fclose((*nfile)->stream);
520 fb863fa4 2020-06-22 tracey free((*nfile)->name);
521 fb863fa4 2020-06-22 tracey free((*nfile));
522 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "malloc");
523 fb863fa4 2020-06-22 tracey }
524 fb863fa4 2020-06-22 tracey TAILQ_INSERT_TAIL(&files, (*nfile), entry);
525 fb863fa4 2020-06-22 tracey return error;
526 fb863fa4 2020-06-22 tracey }
527 fb863fa4 2020-06-22 tracey
528 fb863fa4 2020-06-22 tracey static const struct got_error*
529 fb863fa4 2020-06-22 tracey new_remote(struct got_config_list_entry **gotconfig) {
530 fb863fa4 2020-06-22 tracey const struct got_error* error = NULL;
531 fb863fa4 2020-06-22 tracey
532 fb863fa4 2020-06-22 tracey if (((*gotconfig) = calloc(1, sizeof(struct got_config_list_entry))) ==
533 fb863fa4 2020-06-22 tracey NULL)
534 fb863fa4 2020-06-22 tracey error = got_error_from_errno("calloc");
535 fb863fa4 2020-06-22 tracey return error;
536 fb863fa4 2020-06-22 tracey }
537 fb863fa4 2020-06-22 tracey
538 fb863fa4 2020-06-22 tracey int
539 fb863fa4 2020-06-22 tracey popfile(void)
540 fb863fa4 2020-06-22 tracey {
541 fb863fa4 2020-06-22 tracey struct file *prev = NULL;
542 fb863fa4 2020-06-22 tracey
543 fb863fa4 2020-06-22 tracey TAILQ_REMOVE(&files, file, entry);
544 fb863fa4 2020-06-22 tracey fclose(file->stream);
545 fb863fa4 2020-06-22 tracey free(file->name);
546 fb863fa4 2020-06-22 tracey free(file->ungetbuf);
547 fb863fa4 2020-06-22 tracey free(file);
548 fb863fa4 2020-06-22 tracey file = prev;
549 fb863fa4 2020-06-22 tracey return (file ? 0 : EOF);
550 fb863fa4 2020-06-22 tracey }
551 fb863fa4 2020-06-22 tracey
552 fb863fa4 2020-06-22 tracey const struct got_error*
553 fb863fa4 2020-06-22 tracey parse_got_config(struct got_config_list **conf_list, char *filename)
554 fb863fa4 2020-06-22 tracey {
555 fb863fa4 2020-06-22 tracey struct sym *sym, *next;
556 fb863fa4 2020-06-22 tracey
557 fb863fa4 2020-06-22 tracey *conf_list = NULL;
558 fb863fa4 2020-06-22 tracey
559 fb863fa4 2020-06-22 tracey /*
560 fb863fa4 2020-06-22 tracey * We don't require that gotconfig exists
561 fb863fa4 2020-06-22 tracey * So, null gerror and goto done
562 fb863fa4 2020-06-22 tracey */
563 fb863fa4 2020-06-22 tracey gerror = pushfile(&file, filename);
564 fb863fa4 2020-06-22 tracey if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
565 fb863fa4 2020-06-22 tracey gerror = NULL;
566 fb863fa4 2020-06-22 tracey goto done;
567 fb863fa4 2020-06-22 tracey }
568 fb863fa4 2020-06-22 tracey else if (gerror)
569 fb863fa4 2020-06-22 tracey return gerror;
570 fb863fa4 2020-06-22 tracey
571 fb863fa4 2020-06-22 tracey TAILQ_INIT(&got_config_list);
572 fb863fa4 2020-06-22 tracey topfile = file;
573 fb863fa4 2020-06-22 tracey
574 fb863fa4 2020-06-22 tracey yyparse();
575 fb863fa4 2020-06-22 tracey popfile();
576 fb863fa4 2020-06-22 tracey
577 fb863fa4 2020-06-22 tracey /* Free macros and check which have not been used. */
578 fb863fa4 2020-06-22 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
579 fb863fa4 2020-06-22 tracey if (!sym->persist) {
580 fb863fa4 2020-06-22 tracey free(sym->nam);
581 fb863fa4 2020-06-22 tracey free(sym->val);
582 fb863fa4 2020-06-22 tracey TAILQ_REMOVE(&symhead, sym, entry);
583 fb863fa4 2020-06-22 tracey free(sym);
584 fb863fa4 2020-06-22 tracey }
585 fb863fa4 2020-06-22 tracey }
586 fb863fa4 2020-06-22 tracey done:
587 fb863fa4 2020-06-22 tracey *conf_list = &got_config_list;
588 fb863fa4 2020-06-22 tracey return gerror;
589 fb863fa4 2020-06-22 tracey }
590 fb863fa4 2020-06-22 tracey
591 fb863fa4 2020-06-22 tracey int
592 fb863fa4 2020-06-22 tracey symset(const char *nam, const char *val, int persist)
593 fb863fa4 2020-06-22 tracey {
594 fb863fa4 2020-06-22 tracey struct sym *sym;
595 fb863fa4 2020-06-22 tracey
596 fb863fa4 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
597 fb863fa4 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0)
598 fb863fa4 2020-06-22 tracey break;
599 fb863fa4 2020-06-22 tracey }
600 fb863fa4 2020-06-22 tracey
601 fb863fa4 2020-06-22 tracey if (sym != NULL) {
602 fb863fa4 2020-06-22 tracey if (sym->persist == 1)
603 fb863fa4 2020-06-22 tracey return (0);
604 fb863fa4 2020-06-22 tracey else {
605 fb863fa4 2020-06-22 tracey free(sym->nam);
606 fb863fa4 2020-06-22 tracey free(sym->val);
607 fb863fa4 2020-06-22 tracey TAILQ_REMOVE(&symhead, sym, entry);
608 fb863fa4 2020-06-22 tracey free(sym);
609 fb863fa4 2020-06-22 tracey }
610 fb863fa4 2020-06-22 tracey }
611 fb863fa4 2020-06-22 tracey if ((sym = calloc(1, sizeof(*sym))) == NULL)
612 fb863fa4 2020-06-22 tracey return (-1);
613 fb863fa4 2020-06-22 tracey
614 fb863fa4 2020-06-22 tracey sym->nam = strdup(nam);
615 fb863fa4 2020-06-22 tracey if (sym->nam == NULL) {
616 fb863fa4 2020-06-22 tracey free(sym);
617 fb863fa4 2020-06-22 tracey return (-1);
618 fb863fa4 2020-06-22 tracey }
619 fb863fa4 2020-06-22 tracey sym->val = strdup(val);
620 fb863fa4 2020-06-22 tracey if (sym->val == NULL) {
621 fb863fa4 2020-06-22 tracey free(sym->nam);
622 fb863fa4 2020-06-22 tracey free(sym);
623 fb863fa4 2020-06-22 tracey return (-1);
624 fb863fa4 2020-06-22 tracey }
625 fb863fa4 2020-06-22 tracey sym->used = 0;
626 fb863fa4 2020-06-22 tracey sym->persist = persist;
627 fb863fa4 2020-06-22 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
628 fb863fa4 2020-06-22 tracey return (0);
629 fb863fa4 2020-06-22 tracey }
630 fb863fa4 2020-06-22 tracey
631 fb863fa4 2020-06-22 tracey int
632 fb863fa4 2020-06-22 tracey cmdline_symset(char *s)
633 fb863fa4 2020-06-22 tracey {
634 fb863fa4 2020-06-22 tracey char *sym, *val;
635 fb863fa4 2020-06-22 tracey int ret;
636 fb863fa4 2020-06-22 tracey size_t len;
637 fb863fa4 2020-06-22 tracey
638 fb863fa4 2020-06-22 tracey if ((val = strrchr(s, '=')) == NULL)
639 fb863fa4 2020-06-22 tracey return (-1);
640 fb863fa4 2020-06-22 tracey
641 fb863fa4 2020-06-22 tracey len = strlen(s) - strlen(val) + 1;
642 fb863fa4 2020-06-22 tracey if ((sym = malloc(len)) == NULL)
643 fb863fa4 2020-06-22 tracey errx(1, "cmdline_symset: malloc");
644 fb863fa4 2020-06-22 tracey
645 fb863fa4 2020-06-22 tracey strlcpy(sym, s, len);
646 fb863fa4 2020-06-22 tracey
647 fb863fa4 2020-06-22 tracey ret = symset(sym, val + 1, 1);
648 fb863fa4 2020-06-22 tracey free(sym);
649 fb863fa4 2020-06-22 tracey
650 fb863fa4 2020-06-22 tracey return (ret);
651 fb863fa4 2020-06-22 tracey }
652 fb863fa4 2020-06-22 tracey
653 fb863fa4 2020-06-22 tracey char *
654 fb863fa4 2020-06-22 tracey symget(const char *nam)
655 fb863fa4 2020-06-22 tracey {
656 fb863fa4 2020-06-22 tracey struct sym *sym;
657 fb863fa4 2020-06-22 tracey
658 fb863fa4 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
659 fb863fa4 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0) {
660 fb863fa4 2020-06-22 tracey sym->used = 1;
661 fb863fa4 2020-06-22 tracey return (sym->val);
662 fb863fa4 2020-06-22 tracey }
663 fb863fa4 2020-06-22 tracey }
664 fb863fa4 2020-06-22 tracey return (NULL);
665 fb863fa4 2020-06-22 tracey }