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.
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.
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.
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>
29 fb863fa4 2020-06-22 tracey #include <netinet/in.h>
31 fb863fa4 2020-06-22 tracey #include <arpa/inet.h>
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>
46 fb863fa4 2020-06-22 tracey #include "got_error.h"
47 fb863fa4 2020-06-22 tracey #include "got.h"
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);
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;
78 fb863fa4 2020-06-22 tracey int persist;
79 fb863fa4 2020-06-22 tracey char *nam;
80 fb863fa4 2020-06-22 tracey char *val;
83 fb863fa4 2020-06-22 tracey int symset(const char *, const char *, int);
84 fb863fa4 2020-06-22 tracey char *symget(const char *);
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 **);
91 fb863fa4 2020-06-22 tracey typedef struct {
93 fb863fa4 2020-06-22 tracey int64_t number;
94 fb863fa4 2020-06-22 tracey char *string;
96 fb863fa4 2020-06-22 tracey int lineno;
97 fb863fa4 2020-06-22 tracey } YYSTYPE;
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
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'
112 fb863fa4 2020-06-22 tracey remoteopts2 : remoteopts2 remoteopts1 nl
113 fb863fa4 2020-06-22 tracey | remoteopts1 optnl
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");
122 fb863fa4 2020-06-22 tracey free($2);
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");
131 fb863fa4 2020-06-22 tracey free($2);
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");
140 fb863fa4 2020-06-22 tracey free($2);
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");
149 fb863fa4 2020-06-22 tracey free($2);
152 fb863fa4 2020-06-22 tracey remote : REMOTE STRING {
153 fb863fa4 2020-06-22 tracey static const struct got_error* error;
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);
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");
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);
172 fb863fa4 2020-06-22 tracey optnl : '\n' optnl
173 fb863fa4 2020-06-22 tracey | /* empty */
175 fb863fa4 2020-06-22 tracey nl : '\n' optnl
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;
185 fb863fa4 2020-06-22 tracey yyerror(const char *fmt, ...)
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;
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;
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);
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);
208 fb863fa4 2020-06-22 tracey kw_cmp(const void *k, const void *e)
210 fb863fa4 2020-06-22 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
214 fb863fa4 2020-06-22 tracey lookup(char *s)
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},
224 fb863fa4 2020-06-22 tracey const struct keywords *p;
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);
230 fb863fa4 2020-06-22 tracey return (p->k_val);
232 fb863fa4 2020-06-22 tracey return (STRING);
235 fb863fa4 2020-06-22 tracey #define START_EXPAND 1
236 fb863fa4 2020-06-22 tracey #define DONE_EXPAND 2
238 fb863fa4 2020-06-22 tracey static int expanding;
241 fb863fa4 2020-06-22 tracey igetc(void)
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];
249 fb863fa4 2020-06-22 tracey c = getc(file->stream);
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;
258 fb863fa4 2020-06-22 tracey return (c);
262 fb863fa4 2020-06-22 tracey lgetc(int quotec)
264 fb863fa4 2020-06-22 tracey int c, next;
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);
274 fb863fa4 2020-06-22 tracey return (c);
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;
283 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
284 fb863fa4 2020-06-22 tracey file->lineno++;
287 fb863fa4 2020-06-22 tracey if (c == EOF) {
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.
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');
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();
303 fb863fa4 2020-06-22 tracey return (c);
307 fb863fa4 2020-06-22 tracey lungetc(int c)
309 fb863fa4 2020-06-22 tracey if (c == EOF)
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;
319 fb863fa4 2020-06-22 tracey file->ungetbuf[file->ungetpos++] = c;
323 fb863fa4 2020-06-22 tracey findeol(void)
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++;
334 fb863fa4 2020-06-22 tracey if (c == EOF)
337 fb863fa4 2020-06-22 tracey return (ERROR);
341 fb863fa4 2020-06-22 tracey yylex(void)
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;
350 fb863fa4 2020-06-22 tracey while ((c = lgetc(0)) == ' ' || c == '\t')
351 fb863fa4 2020-06-22 tracey ; /* nothing */
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);
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());
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;
370 fb863fa4 2020-06-22 tracey *p = '\0';
371 fb863fa4 2020-06-22 tracey lungetc(c);
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());
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);
385 fb863fa4 2020-06-22 tracey lungetc(START_EXPAND);
386 fb863fa4 2020-06-22 tracey goto top;
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;
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';
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());
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());
420 fb863fa4 2020-06-22 tracey *p++ = c;
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);
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 == '=')
431 fb863fa4 2020-06-22 tracey if (c == '-' || isdigit(c)) {
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());
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;
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());
453 fb863fa4 2020-06-22 tracey return (NUMBER);
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);
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 != ','))
470 fb863fa4 2020-06-22 tracey if (isalnum(c) || c == ':' || c == '_') {
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());
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);
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++;
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);
494 fb863fa4 2020-06-22 tracey static const struct got_error*
495 fb863fa4 2020-06-22 tracey pushfile(struct file **nfile, const char *name)
497 fb863fa4 2020-06-22 tracey const struct got_error* error = NULL;
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");
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;
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");
524 fb863fa4 2020-06-22 tracey TAILQ_INSERT_TAIL(&files, (*nfile), entry);
525 fb863fa4 2020-06-22 tracey return error;
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;
532 fb863fa4 2020-06-22 tracey if (((*gotconfig) = calloc(1, sizeof(struct got_config_list_entry))) ==
534 fb863fa4 2020-06-22 tracey error = got_error_from_errno("calloc");
535 fb863fa4 2020-06-22 tracey return error;
539 fb863fa4 2020-06-22 tracey popfile(void)
541 fb863fa4 2020-06-22 tracey struct file *prev = NULL;
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);
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)
555 fb863fa4 2020-06-22 tracey struct sym *sym, *next;
557 fb863fa4 2020-06-22 tracey *conf_list = NULL;
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
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;
568 fb863fa4 2020-06-22 tracey else if (gerror)
569 fb863fa4 2020-06-22 tracey return gerror;
571 fb863fa4 2020-06-22 tracey TAILQ_INIT(&got_config_list);
572 fb863fa4 2020-06-22 tracey topfile = file;
574 fb863fa4 2020-06-22 tracey yyparse();
575 fb863fa4 2020-06-22 tracey popfile();
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);
587 fb863fa4 2020-06-22 tracey *conf_list = &got_config_list;
588 fb863fa4 2020-06-22 tracey return gerror;
592 fb863fa4 2020-06-22 tracey symset(const char *nam, const char *val, int persist)
594 fb863fa4 2020-06-22 tracey struct sym *sym;
596 fb863fa4 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
597 fb863fa4 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0)
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);
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);
611 fb863fa4 2020-06-22 tracey if ((sym = calloc(1, sizeof(*sym))) == NULL)
612 fb863fa4 2020-06-22 tracey return (-1);
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);
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);
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);
632 fb863fa4 2020-06-22 tracey cmdline_symset(char *s)
634 fb863fa4 2020-06-22 tracey char *sym, *val;
636 fb863fa4 2020-06-22 tracey size_t len;
638 fb863fa4 2020-06-22 tracey if ((val = strrchr(s, '=')) == NULL)
639 fb863fa4 2020-06-22 tracey return (-1);
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");
645 fb863fa4 2020-06-22 tracey strlcpy(sym, s, len);
647 fb863fa4 2020-06-22 tracey ret = symset(sym, val + 1, 1);
648 fb863fa4 2020-06-22 tracey free(sym);
650 fb863fa4 2020-06-22 tracey return (ret);
654 fb863fa4 2020-06-22 tracey symget(const char *nam)
656 fb863fa4 2020-06-22 tracey struct sym *sym;
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);
664 fb863fa4 2020-06-22 tracey return (NULL);