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 static struct file {
50 fb863fa4 2020-06-22 tracey FILE *stream;
51 fb863fa4 2020-06-22 tracey char *name;
52 fb863fa4 2020-06-22 tracey size_t ungetpos;
53 fb863fa4 2020-06-22 tracey size_t ungetsize;
54 fb863fa4 2020-06-22 tracey u_char *ungetbuf;
55 fb863fa4 2020-06-22 tracey int eof_reached;
56 fb863fa4 2020-06-22 tracey int lineno;
58 6d7260fd 2020-08-03 stsp static const struct got_error* newfile(struct file**, const char *);
59 6d7260fd 2020-08-03 stsp static void closefile(struct file *);
60 fb863fa4 2020-06-22 tracey int yyparse(void);
61 fb863fa4 2020-06-22 tracey int yylex(void);
62 fb863fa4 2020-06-22 tracey int yyerror(const char *, ...)
63 fb863fa4 2020-06-22 tracey __attribute__((__format__ (printf, 1, 2)))
64 fb863fa4 2020-06-22 tracey __attribute__((__nonnull__ (1)));
65 fb863fa4 2020-06-22 tracey int kw_cmp(const void *, const void *);
66 fb863fa4 2020-06-22 tracey int lookup(char *);
67 fb863fa4 2020-06-22 tracey int igetc(void);
68 fb863fa4 2020-06-22 tracey int lgetc(int);
69 fb863fa4 2020-06-22 tracey void lungetc(int);
70 fb863fa4 2020-06-22 tracey int findeol(void);
72 fb863fa4 2020-06-22 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
73 fb863fa4 2020-06-22 tracey struct sym {
74 fb863fa4 2020-06-22 tracey TAILQ_ENTRY(sym) entry;
76 fb863fa4 2020-06-22 tracey int persist;
77 fb863fa4 2020-06-22 tracey char *nam;
78 fb863fa4 2020-06-22 tracey char *val;
81 fb863fa4 2020-06-22 tracey int symset(const char *, const char *, int);
82 fb863fa4 2020-06-22 tracey char *symget(const char *);
84 fb863fa4 2020-06-22 tracey const struct got_error* gerror = NULL;
85 9e6e8e60 2020-08-02 stsp struct gotconfig_remote_repo *remote;
86 9e6e8e60 2020-08-02 stsp struct gotconfig gotconfig;
87 9e6e8e60 2020-08-02 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
89 fb863fa4 2020-06-22 tracey typedef struct {
91 fb863fa4 2020-06-22 tracey int64_t number;
92 fb863fa4 2020-06-22 tracey char *string;
94 fb863fa4 2020-06-22 tracey int lineno;
95 fb863fa4 2020-06-22 tracey } YYSTYPE;
99 fb863fa4 2020-06-22 tracey %token ERROR
100 fb863fa4 2020-06-22 tracey %token REMOTE REPOSITORY SERVER PROTOCOL USER
101 fb863fa4 2020-06-22 tracey %token <v.string> STRING
102 fb863fa4 2020-06-22 tracey %token <v.number> NUMBER
106 fb863fa4 2020-06-22 tracey grammar : /* empty */
107 fb863fa4 2020-06-22 tracey | grammar '\n'
108 fb863fa4 2020-06-22 tracey | grammar remote '\n'
110 fb863fa4 2020-06-22 tracey remoteopts2 : remoteopts2 remoteopts1 nl
111 fb863fa4 2020-06-22 tracey | remoteopts1 optnl
113 fb863fa4 2020-06-22 tracey remoteopts1 : REPOSITORY STRING {
114 9e6e8e60 2020-08-02 stsp remote->repository = strdup($2);
115 9e6e8e60 2020-08-02 stsp if (remote->repository == NULL) {
116 fb863fa4 2020-06-22 tracey free($2);
117 fb863fa4 2020-06-22 tracey yyerror("strdup");
120 fb863fa4 2020-06-22 tracey free($2);
122 fb863fa4 2020-06-22 tracey | SERVER STRING {
123 9e6e8e60 2020-08-02 stsp remote->server = strdup($2);
124 9e6e8e60 2020-08-02 stsp if (remote->server == NULL) {
125 fb863fa4 2020-06-22 tracey free($2);
126 fb863fa4 2020-06-22 tracey yyerror("strdup");
129 fb863fa4 2020-06-22 tracey free($2);
131 fb863fa4 2020-06-22 tracey | PROTOCOL STRING {
132 9e6e8e60 2020-08-02 stsp remote->protocol = strdup($2);
133 9e6e8e60 2020-08-02 stsp if (remote->protocol == NULL) {
134 fb863fa4 2020-06-22 tracey free($2);
135 fb863fa4 2020-06-22 tracey yyerror("strdup");
138 fb863fa4 2020-06-22 tracey free($2);
140 fb863fa4 2020-06-22 tracey | USER STRING {
141 9e6e8e60 2020-08-02 stsp remote->user = strdup($2);
142 9e6e8e60 2020-08-02 stsp if (remote->user == NULL) {
143 fb863fa4 2020-06-22 tracey free($2);
144 fb863fa4 2020-06-22 tracey yyerror("strdup");
147 fb863fa4 2020-06-22 tracey free($2);
150 fb863fa4 2020-06-22 tracey remote : REMOTE STRING {
151 fb863fa4 2020-06-22 tracey static const struct got_error* error;
153 9e6e8e60 2020-08-02 stsp error = new_remote(&remote);
154 fb863fa4 2020-06-22 tracey if (error) {
155 fb863fa4 2020-06-22 tracey free($2);
156 fb863fa4 2020-06-22 tracey yyerror("%s", error->msg);
159 9e6e8e60 2020-08-02 stsp remote->name = strdup($2);
160 9e6e8e60 2020-08-02 stsp if (remote->name == NULL) {
161 fb863fa4 2020-06-22 tracey free($2);
162 fb863fa4 2020-06-22 tracey yyerror("strdup");
165 fb863fa4 2020-06-22 tracey free($2);
166 fb863fa4 2020-06-22 tracey } '{' optnl remoteopts2 '}' {
167 9e6e8e60 2020-08-02 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
168 9e6e8e60 2020-08-02 stsp gotconfig.nremotes++;
171 fb863fa4 2020-06-22 tracey optnl : '\n' optnl
172 fb863fa4 2020-06-22 tracey | /* empty */
174 fb863fa4 2020-06-22 tracey nl : '\n' optnl
178 fb863fa4 2020-06-22 tracey struct keywords {
179 fb863fa4 2020-06-22 tracey const char *k_name;
180 fb863fa4 2020-06-22 tracey int k_val;
184 fb863fa4 2020-06-22 tracey yyerror(const char *fmt, ...)
186 fb863fa4 2020-06-22 tracey va_list ap;
187 fb863fa4 2020-06-22 tracey char *msg;
188 fb863fa4 2020-06-22 tracey char *err = NULL;
190 fb863fa4 2020-06-22 tracey va_start(ap, fmt);
191 fb863fa4 2020-06-22 tracey if (vasprintf(&msg, fmt, ap) == -1) {
192 fb863fa4 2020-06-22 tracey gerror = got_error_from_errno("vasprintf");
193 fb863fa4 2020-06-22 tracey return 0;
195 fb863fa4 2020-06-22 tracey va_end(ap);
196 fb863fa4 2020-06-22 tracey if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
197 fb863fa4 2020-06-22 tracey gerror = got_error_from_errno("asprintf");
198 fb863fa4 2020-06-22 tracey return(0);
200 fb863fa4 2020-06-22 tracey gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
201 fb863fa4 2020-06-22 tracey free(msg);
202 fb863fa4 2020-06-22 tracey free(err);
203 fb863fa4 2020-06-22 tracey return(0);
207 fb863fa4 2020-06-22 tracey kw_cmp(const void *k, const void *e)
209 fb863fa4 2020-06-22 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
213 fb863fa4 2020-06-22 tracey lookup(char *s)
215 fb863fa4 2020-06-22 tracey /* This has to be sorted always. */
216 fb863fa4 2020-06-22 tracey static const struct keywords keywords[] = {
217 fb863fa4 2020-06-22 tracey {"protocol", PROTOCOL},
218 fb863fa4 2020-06-22 tracey {"remote", REMOTE},
219 fb863fa4 2020-06-22 tracey {"repository", REPOSITORY},
220 fb863fa4 2020-06-22 tracey {"server", SERVER},
221 fb863fa4 2020-06-22 tracey {"user", USER},
223 fb863fa4 2020-06-22 tracey const struct keywords *p;
225 fb863fa4 2020-06-22 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
226 fb863fa4 2020-06-22 tracey sizeof(keywords[0]), kw_cmp);
229 fb863fa4 2020-06-22 tracey return (p->k_val);
231 fb863fa4 2020-06-22 tracey return (STRING);
234 fb863fa4 2020-06-22 tracey #define START_EXPAND 1
235 fb863fa4 2020-06-22 tracey #define DONE_EXPAND 2
237 fb863fa4 2020-06-22 tracey static int expanding;
240 fb863fa4 2020-06-22 tracey igetc(void)
244 fb863fa4 2020-06-22 tracey while (1) {
245 fb863fa4 2020-06-22 tracey if (file->ungetpos > 0)
246 fb863fa4 2020-06-22 tracey c = file->ungetbuf[--file->ungetpos];
248 fb863fa4 2020-06-22 tracey c = getc(file->stream);
250 fb863fa4 2020-06-22 tracey if (c == START_EXPAND)
251 fb863fa4 2020-06-22 tracey expanding = 1;
252 fb863fa4 2020-06-22 tracey else if (c == DONE_EXPAND)
253 fb863fa4 2020-06-22 tracey expanding = 0;
257 fb863fa4 2020-06-22 tracey return (c);
261 fb863fa4 2020-06-22 tracey lgetc(int quotec)
263 fb863fa4 2020-06-22 tracey int c, next;
265 fb863fa4 2020-06-22 tracey if (quotec) {
266 51b930a3 2020-08-02 tracey c = igetc();
267 51b930a3 2020-08-02 tracey if (c == EOF) {
268 fb863fa4 2020-06-22 tracey yyerror("reached end of file while parsing "
269 fb863fa4 2020-06-22 tracey "quoted string");
271 fb863fa4 2020-06-22 tracey return (c);
274 51b930a3 2020-08-02 tracey c = igetc();
275 51b930a3 2020-08-02 tracey while (c == '\\') {
276 fb863fa4 2020-06-22 tracey next = igetc();
277 fb863fa4 2020-06-22 tracey if (next != '\n') {
278 fb863fa4 2020-06-22 tracey c = next;
281 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
282 fb863fa4 2020-06-22 tracey file->lineno++;
285 fb863fa4 2020-06-22 tracey return (c);
289 fb863fa4 2020-06-22 tracey lungetc(int c)
291 fb863fa4 2020-06-22 tracey if (c == EOF)
294 fb863fa4 2020-06-22 tracey if (file->ungetpos >= file->ungetsize) {
295 fb863fa4 2020-06-22 tracey void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
296 fb863fa4 2020-06-22 tracey if (p == NULL)
297 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
298 fb863fa4 2020-06-22 tracey file->ungetbuf = p;
299 fb863fa4 2020-06-22 tracey file->ungetsize *= 2;
301 fb863fa4 2020-06-22 tracey file->ungetbuf[file->ungetpos++] = c;
305 fb863fa4 2020-06-22 tracey findeol(void)
309 fb863fa4 2020-06-22 tracey /* Skip to either EOF or the first real EOL. */
310 fb863fa4 2020-06-22 tracey while (1) {
311 fb863fa4 2020-06-22 tracey c = lgetc(0);
312 fb863fa4 2020-06-22 tracey if (c == '\n') {
313 fb863fa4 2020-06-22 tracey file->lineno++;
316 fb863fa4 2020-06-22 tracey if (c == EOF)
319 fb863fa4 2020-06-22 tracey return (ERROR);
323 fb863fa4 2020-06-22 tracey yylex(void)
325 fb863fa4 2020-06-22 tracey unsigned char buf[8096];
326 fb863fa4 2020-06-22 tracey unsigned char *p, *val;
327 fb863fa4 2020-06-22 tracey int quotec, next, c;
328 fb863fa4 2020-06-22 tracey int token;
332 51b930a3 2020-08-02 tracey c = lgetc(0);
333 51b930a3 2020-08-02 tracey while (c == ' ' || c == '\t')
334 51b930a3 2020-08-02 tracey c = lgetc(0); /* nothing */
336 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
337 51b930a3 2020-08-02 tracey if (c == '#') {
338 51b930a3 2020-08-02 tracey c = lgetc(0);
339 51b930a3 2020-08-02 tracey while (c != '\n' && c != EOF)
340 51b930a3 2020-08-02 tracey c = lgetc(0); /* nothing */
342 fb863fa4 2020-06-22 tracey if (c == '$' && !expanding) {
343 fb863fa4 2020-06-22 tracey while (1) {
344 51b930a3 2020-08-02 tracey c = lgetc(0);
345 51b930a3 2020-08-02 tracey if (c == EOF)
346 fb863fa4 2020-06-22 tracey return (0);
348 fb863fa4 2020-06-22 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
349 fb863fa4 2020-06-22 tracey yyerror("string too long");
350 fb863fa4 2020-06-22 tracey return (findeol());
352 fb863fa4 2020-06-22 tracey if (isalnum(c) || c == '_') {
353 fb863fa4 2020-06-22 tracey *p++ = c;
354 fb863fa4 2020-06-22 tracey continue;
356 fb863fa4 2020-06-22 tracey *p = '\0';
357 fb863fa4 2020-06-22 tracey lungetc(c);
360 fb863fa4 2020-06-22 tracey val = symget(buf);
361 fb863fa4 2020-06-22 tracey if (val == NULL) {
362 fb863fa4 2020-06-22 tracey yyerror("macro '%s' not defined", buf);
363 fb863fa4 2020-06-22 tracey return (findeol());
365 fb863fa4 2020-06-22 tracey p = val + strlen(val) - 1;
366 fb863fa4 2020-06-22 tracey lungetc(DONE_EXPAND);
367 fb863fa4 2020-06-22 tracey while (p >= val) {
368 fb863fa4 2020-06-22 tracey lungetc(*p);
371 fb863fa4 2020-06-22 tracey lungetc(START_EXPAND);
372 fb863fa4 2020-06-22 tracey goto top;
375 fb863fa4 2020-06-22 tracey switch (c) {
376 fb863fa4 2020-06-22 tracey case '\'':
377 fb863fa4 2020-06-22 tracey case '"':
378 fb863fa4 2020-06-22 tracey quotec = c;
379 fb863fa4 2020-06-22 tracey while (1) {
380 51b930a3 2020-08-02 tracey c = lgetc(quotec);
381 51b930a3 2020-08-02 tracey if (c == EOF)
382 fb863fa4 2020-06-22 tracey return (0);
383 fb863fa4 2020-06-22 tracey if (c == '\n') {
384 fb863fa4 2020-06-22 tracey file->lineno++;
385 fb863fa4 2020-06-22 tracey continue;
386 fb863fa4 2020-06-22 tracey } else if (c == '\\') {
387 51b930a3 2020-08-02 tracey next = lgetc(quotec);
388 51b930a3 2020-08-02 tracey if (next == EOF)
389 fb863fa4 2020-06-22 tracey return (0);
390 fb863fa4 2020-06-22 tracey if (next == quotec || c == ' ' || c == '\t')
391 fb863fa4 2020-06-22 tracey c = next;
392 fb863fa4 2020-06-22 tracey else if (next == '\n') {
393 fb863fa4 2020-06-22 tracey file->lineno++;
394 fb863fa4 2020-06-22 tracey continue;
396 fb863fa4 2020-06-22 tracey lungetc(next);
397 fb863fa4 2020-06-22 tracey } else if (c == quotec) {
398 fb863fa4 2020-06-22 tracey *p = '\0';
400 fb863fa4 2020-06-22 tracey } else if (c == '\0') {
401 fb863fa4 2020-06-22 tracey yyerror("syntax error");
402 fb863fa4 2020-06-22 tracey return (findeol());
404 fb863fa4 2020-06-22 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
405 fb863fa4 2020-06-22 tracey yyerror("string too long");
406 fb863fa4 2020-06-22 tracey return (findeol());
408 fb863fa4 2020-06-22 tracey *p++ = c;
410 fb863fa4 2020-06-22 tracey yylval.v.string = strdup(buf);
411 fb863fa4 2020-06-22 tracey if (yylval.v.string == NULL)
412 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
413 fb863fa4 2020-06-22 tracey return (STRING);
416 fb863fa4 2020-06-22 tracey #define allowed_to_end_number(x) \
417 fb863fa4 2020-06-22 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
419 fb863fa4 2020-06-22 tracey if (c == '-' || isdigit(c)) {
421 fb863fa4 2020-06-22 tracey *p++ = c;
422 fb863fa4 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
423 fb863fa4 2020-06-22 tracey yyerror("string too long");
424 fb863fa4 2020-06-22 tracey return (findeol());
426 51b930a3 2020-08-02 tracey c = lgetc(0);
427 51b930a3 2020-08-02 tracey } while (c != EOF && isdigit(c));
428 fb863fa4 2020-06-22 tracey lungetc(c);
429 fb863fa4 2020-06-22 tracey if (p == buf + 1 && buf[0] == '-')
430 fb863fa4 2020-06-22 tracey goto nodigits;
431 fb863fa4 2020-06-22 tracey if (c == EOF || allowed_to_end_number(c)) {
432 fb863fa4 2020-06-22 tracey const char *errstr = NULL;
434 fb863fa4 2020-06-22 tracey *p = '\0';
435 fb863fa4 2020-06-22 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
436 fb863fa4 2020-06-22 tracey LLONG_MAX, &errstr);
437 fb863fa4 2020-06-22 tracey if (errstr) {
438 fb863fa4 2020-06-22 tracey yyerror("\"%s\" invalid number: %s",
439 fb863fa4 2020-06-22 tracey buf, errstr);
440 fb863fa4 2020-06-22 tracey return (findeol());
442 fb863fa4 2020-06-22 tracey return (NUMBER);
444 fb863fa4 2020-06-22 tracey nodigits:
445 fb863fa4 2020-06-22 tracey while (p > buf + 1)
446 fb863fa4 2020-06-22 tracey lungetc(*--p);
447 fb863fa4 2020-06-22 tracey c = *--p;
448 fb863fa4 2020-06-22 tracey if (c == '-')
449 fb863fa4 2020-06-22 tracey return (c);
453 fb863fa4 2020-06-22 tracey #define allowed_in_string(x) \
454 fb863fa4 2020-06-22 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
455 fb863fa4 2020-06-22 tracey x != '{' && x != '}' && \
456 fb863fa4 2020-06-22 tracey x != '!' && x != '=' && x != '#' && \
457 fb863fa4 2020-06-22 tracey x != ','))
459 fb863fa4 2020-06-22 tracey if (isalnum(c) || c == ':' || c == '_') {
461 fb863fa4 2020-06-22 tracey *p++ = c;
462 fb863fa4 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
463 fb863fa4 2020-06-22 tracey yyerror("string too long");
464 fb863fa4 2020-06-22 tracey return (findeol());
466 51b930a3 2020-08-02 tracey c = lgetc(0);
467 51b930a3 2020-08-02 tracey } while (c != EOF && (allowed_in_string(c)));
468 fb863fa4 2020-06-22 tracey lungetc(c);
469 fb863fa4 2020-06-22 tracey *p = '\0';
470 51b930a3 2020-08-02 tracey token = lookup(buf);
471 51b930a3 2020-08-02 tracey if (token == STRING) {
472 51b930a3 2020-08-02 tracey yylval.v.string = strdup(buf);
473 51b930a3 2020-08-02 tracey if (yylval.v.string == NULL)
474 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
476 fb863fa4 2020-06-22 tracey return (token);
478 fb863fa4 2020-06-22 tracey if (c == '\n') {
479 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
480 fb863fa4 2020-06-22 tracey file->lineno++;
482 fb863fa4 2020-06-22 tracey if (c == EOF)
483 fb863fa4 2020-06-22 tracey return (0);
484 fb863fa4 2020-06-22 tracey return (c);
487 fb863fa4 2020-06-22 tracey static const struct got_error*
488 6d7260fd 2020-08-03 stsp newfile(struct file **nfile, const char *name)
490 fb863fa4 2020-06-22 tracey const struct got_error* error = NULL;
492 51b930a3 2020-08-02 tracey (*nfile) = calloc(1, sizeof(struct file));
493 51b930a3 2020-08-02 tracey if ((*nfile) == NULL)
494 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "calloc");
495 51b930a3 2020-08-02 tracey (*nfile)->name = strdup(name);
496 51b930a3 2020-08-02 tracey if ((*nfile)->name == NULL) {
497 fb863fa4 2020-06-22 tracey free(nfile);
498 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "strdup");
500 51b930a3 2020-08-02 tracey (*nfile)->stream = fopen((*nfile)->name, "r");
501 51b930a3 2020-08-02 tracey if ((*nfile)->stream == NULL) {
502 fb863fa4 2020-06-22 tracey char *msg = NULL;
503 fb863fa4 2020-06-22 tracey if (asprintf(&msg, "%s", (*nfile)->name) == -1)
504 fb863fa4 2020-06-22 tracey return got_error_from_errno("asprintf");
505 fb863fa4 2020-06-22 tracey error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
506 fb863fa4 2020-06-22 tracey free((*nfile)->name);
507 fb863fa4 2020-06-22 tracey free((*nfile));
508 fb863fa4 2020-06-22 tracey free(msg);
509 fb863fa4 2020-06-22 tracey return error;
511 6d7260fd 2020-08-03 stsp (*nfile)->lineno = 1;
512 fb863fa4 2020-06-22 tracey (*nfile)->ungetsize = 16;
513 fb863fa4 2020-06-22 tracey (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
514 fb863fa4 2020-06-22 tracey if ((*nfile)->ungetbuf == NULL) {
515 fb863fa4 2020-06-22 tracey fclose((*nfile)->stream);
516 fb863fa4 2020-06-22 tracey free((*nfile)->name);
517 fb863fa4 2020-06-22 tracey free((*nfile));
518 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "malloc");
520 fb863fa4 2020-06-22 tracey return error;
523 fb863fa4 2020-06-22 tracey static const struct got_error*
524 9e6e8e60 2020-08-02 stsp new_remote(struct gotconfig_remote_repo **remote)
526 9e6e8e60 2020-08-02 stsp const struct got_error *error = NULL;
528 9e6e8e60 2020-08-02 stsp *remote = calloc(1, sizeof(**remote));
529 9e6e8e60 2020-08-02 stsp if (*remote == NULL)
530 fb863fa4 2020-06-22 tracey error = got_error_from_errno("calloc");
531 fb863fa4 2020-06-22 tracey return error;
534 6d7260fd 2020-08-03 stsp static void
535 6d7260fd 2020-08-03 stsp closefile(struct file *file)
537 fb863fa4 2020-06-22 tracey fclose(file->stream);
538 fb863fa4 2020-06-22 tracey free(file->name);
539 fb863fa4 2020-06-22 tracey free(file->ungetbuf);
540 fb863fa4 2020-06-22 tracey free(file);
543 fb863fa4 2020-06-22 tracey const struct got_error*
544 9e6e8e60 2020-08-02 stsp gotconfig_parse(struct gotconfig **conf, const char *filename)
546 fb863fa4 2020-06-22 tracey struct sym *sym, *next;
548 9e6e8e60 2020-08-02 stsp *conf = NULL;
551 fb863fa4 2020-06-22 tracey * We don't require that gotconfig exists
552 fb863fa4 2020-06-22 tracey * So, null gerror and goto done
554 6d7260fd 2020-08-03 stsp gerror = newfile(&file, filename);
555 fb863fa4 2020-06-22 tracey if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
556 fb863fa4 2020-06-22 tracey gerror = NULL;
557 fb863fa4 2020-06-22 tracey goto done;
558 9e6e8e60 2020-08-02 stsp } else if (gerror)
559 fb863fa4 2020-06-22 tracey return gerror;
561 9e6e8e60 2020-08-02 stsp TAILQ_INIT(&gotconfig.remotes);
563 fb863fa4 2020-06-22 tracey yyparse();
564 6d7260fd 2020-08-03 stsp closefile(file);
566 fb863fa4 2020-06-22 tracey /* Free macros and check which have not been used. */
567 fb863fa4 2020-06-22 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
568 fb863fa4 2020-06-22 tracey if (!sym->persist) {
569 fb863fa4 2020-06-22 tracey free(sym->nam);
570 fb863fa4 2020-06-22 tracey free(sym->val);
571 fb863fa4 2020-06-22 tracey TAILQ_REMOVE(&symhead, sym, entry);
572 fb863fa4 2020-06-22 tracey free(sym);
576 9e6e8e60 2020-08-02 stsp *conf = &gotconfig;
577 fb863fa4 2020-06-22 tracey return gerror;
581 9e6e8e60 2020-08-02 stsp gotconfig_free(struct gotconfig *conf)
583 9e6e8e60 2020-08-02 stsp struct gotconfig_remote_repo *remote;
585 9e6e8e60 2020-08-02 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
586 9e6e8e60 2020-08-02 stsp remote = TAILQ_FIRST(&conf->remotes);
587 9e6e8e60 2020-08-02 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
588 9e6e8e60 2020-08-02 stsp free(remote->name);
589 9e6e8e60 2020-08-02 stsp free(remote->repository);
590 9e6e8e60 2020-08-02 stsp free(remote->server);
591 9e6e8e60 2020-08-02 stsp free(remote->protocol);
592 9e6e8e60 2020-08-02 stsp free(remote->user);
593 9e6e8e60 2020-08-02 stsp free(remote);
598 fb863fa4 2020-06-22 tracey symset(const char *nam, const char *val, int persist)
600 fb863fa4 2020-06-22 tracey struct sym *sym;
602 fb863fa4 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
603 fb863fa4 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0)
607 fb863fa4 2020-06-22 tracey if (sym != NULL) {
608 fb863fa4 2020-06-22 tracey if (sym->persist == 1)
609 fb863fa4 2020-06-22 tracey return (0);
611 fb863fa4 2020-06-22 tracey free(sym->nam);
612 fb863fa4 2020-06-22 tracey free(sym->val);
613 fb863fa4 2020-06-22 tracey TAILQ_REMOVE(&symhead, sym, entry);
614 fb863fa4 2020-06-22 tracey free(sym);
617 51b930a3 2020-08-02 tracey sym = calloc(1, sizeof(*sym));
618 51b930a3 2020-08-02 tracey if (sym == NULL)
619 fb863fa4 2020-06-22 tracey return (-1);
621 fb863fa4 2020-06-22 tracey sym->nam = strdup(nam);
622 fb863fa4 2020-06-22 tracey if (sym->nam == NULL) {
623 fb863fa4 2020-06-22 tracey free(sym);
624 fb863fa4 2020-06-22 tracey return (-1);
626 fb863fa4 2020-06-22 tracey sym->val = strdup(val);
627 fb863fa4 2020-06-22 tracey if (sym->val == NULL) {
628 fb863fa4 2020-06-22 tracey free(sym->nam);
629 fb863fa4 2020-06-22 tracey free(sym);
630 fb863fa4 2020-06-22 tracey return (-1);
632 fb863fa4 2020-06-22 tracey sym->used = 0;
633 fb863fa4 2020-06-22 tracey sym->persist = persist;
634 fb863fa4 2020-06-22 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
635 fb863fa4 2020-06-22 tracey return (0);
639 fb863fa4 2020-06-22 tracey cmdline_symset(char *s)
641 fb863fa4 2020-06-22 tracey char *sym, *val;
643 fb863fa4 2020-06-22 tracey size_t len;
645 51b930a3 2020-08-02 tracey val = strrchr(s, '=');
646 51b930a3 2020-08-02 tracey if (val == NULL)
647 fb863fa4 2020-06-22 tracey return (-1);
649 fb863fa4 2020-06-22 tracey len = strlen(s) - strlen(val) + 1;
650 51b930a3 2020-08-02 tracey sym = malloc(len);
651 51b930a3 2020-08-02 tracey if (sym == NULL)
652 fb863fa4 2020-06-22 tracey errx(1, "cmdline_symset: malloc");
654 fb863fa4 2020-06-22 tracey strlcpy(sym, s, len);
656 fb863fa4 2020-06-22 tracey ret = symset(sym, val + 1, 1);
657 fb863fa4 2020-06-22 tracey free(sym);
659 fb863fa4 2020-06-22 tracey return (ret);
663 fb863fa4 2020-06-22 tracey symget(const char *nam)
665 fb863fa4 2020-06-22 tracey struct sym *sym;
667 fb863fa4 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
668 fb863fa4 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0) {
669 fb863fa4 2020-06-22 tracey sym->used = 1;
670 fb863fa4 2020-06-22 tracey return (sym->val);
673 fb863fa4 2020-06-22 tracey return (NULL);