2 257add31 2020-09-09 stsp * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
3 257add31 2020-09-09 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 257add31 2020-09-09 stsp * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 257add31 2020-09-09 stsp * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 257add31 2020-09-09 stsp * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 257add31 2020-09-09 stsp * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 257add31 2020-09-09 stsp * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 257add31 2020-09-09 stsp * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 257add31 2020-09-09 stsp * Permission to use, copy, modify, and distribute this software for any
12 257add31 2020-09-09 stsp * purpose with or without fee is hereby granted, provided that the above
13 257add31 2020-09-09 stsp * copyright notice and this permission notice appear in all copies.
15 257add31 2020-09-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 257add31 2020-09-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 257add31 2020-09-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 257add31 2020-09-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 257add31 2020-09-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 257add31 2020-09-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 257add31 2020-09-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 257add31 2020-09-09 stsp #include <sys/types.h>
26 257add31 2020-09-09 stsp #include <sys/queue.h>
27 257add31 2020-09-09 stsp #include <sys/socket.h>
28 257add31 2020-09-09 stsp #include <sys/stat.h>
30 257add31 2020-09-09 stsp #include <netinet/in.h>
32 257add31 2020-09-09 stsp #include <arpa/inet.h>
34 257add31 2020-09-09 stsp #include <netdb.h>
36 257add31 2020-09-09 stsp #include <ctype.h>
37 257add31 2020-09-09 stsp #include <err.h>
38 257add31 2020-09-09 stsp #include <errno.h>
39 257add31 2020-09-09 stsp #include <event.h>
40 257add31 2020-09-09 stsp #include <ifaddrs.h>
41 257add31 2020-09-09 stsp #include <imsg.h>
42 257add31 2020-09-09 stsp #include <limits.h>
43 257add31 2020-09-09 stsp #include <stdarg.h>
44 257add31 2020-09-09 stsp #include <stdio.h>
45 257add31 2020-09-09 stsp #include <string.h>
46 257add31 2020-09-09 stsp #include <syslog.h>
47 257add31 2020-09-09 stsp #include <unistd.h>
49 257add31 2020-09-09 stsp #include "got_error.h"
50 257add31 2020-09-09 stsp #include "gotconfig.h"
52 257add31 2020-09-09 stsp static struct file {
53 257add31 2020-09-09 stsp FILE *stream;
54 257add31 2020-09-09 stsp const char *name;
55 257add31 2020-09-09 stsp size_t ungetpos;
56 257add31 2020-09-09 stsp size_t ungetsize;
57 257add31 2020-09-09 stsp u_char *ungetbuf;
58 257add31 2020-09-09 stsp int eof_reached;
61 257add31 2020-09-09 stsp static const struct got_error* newfile(struct file**, const char *, int *);
62 257add31 2020-09-09 stsp static void closefile(struct file *);
63 257add31 2020-09-09 stsp int yyparse(void);
64 257add31 2020-09-09 stsp int yylex(void);
65 257add31 2020-09-09 stsp int yyerror(const char *, ...)
66 257add31 2020-09-09 stsp __attribute__((__format__ (printf, 1, 2)))
67 257add31 2020-09-09 stsp __attribute__((__nonnull__ (1)));
68 257add31 2020-09-09 stsp int kw_cmp(const void *, const void *);
69 257add31 2020-09-09 stsp int lookup(char *);
70 257add31 2020-09-09 stsp int igetc(void);
71 257add31 2020-09-09 stsp int lgetc(int);
72 257add31 2020-09-09 stsp void lungetc(int);
73 257add31 2020-09-09 stsp int findeol(void);
74 257add31 2020-09-09 stsp static int parseport(char *, long long *);
76 257add31 2020-09-09 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
77 257add31 2020-09-09 stsp struct sym {
78 257add31 2020-09-09 stsp TAILQ_ENTRY(sym) entry;
80 257add31 2020-09-09 stsp int persist;
85 257add31 2020-09-09 stsp int symset(const char *, const char *, int);
86 257add31 2020-09-09 stsp char *symget(const char *);
88 257add31 2020-09-09 stsp static int atoul(char *, u_long *);
90 257add31 2020-09-09 stsp static const struct got_error* gerror;
91 257add31 2020-09-09 stsp static struct gotconfig_remote_repo *remote;
92 257add31 2020-09-09 stsp static struct gotconfig gotconfig;
93 257add31 2020-09-09 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
95 257add31 2020-09-09 stsp typedef struct {
97 257add31 2020-09-09 stsp int64_t number;
98 257add31 2020-09-09 stsp char *string;
100 257add31 2020-09-09 stsp int lineno;
105 257add31 2020-09-09 stsp %token ERROR
106 257add31 2020-09-09 stsp %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES AUTHOR
107 257add31 2020-09-09 stsp %token <v.string> STRING
108 257add31 2020-09-09 stsp %token <v.number> NUMBER
109 257add31 2020-09-09 stsp %type <v.number> boolean portplain
110 257add31 2020-09-09 stsp %type <v.string> numberstring
114 257add31 2020-09-09 stsp grammar : /* empty */
115 257add31 2020-09-09 stsp | grammar '\n'
116 257add31 2020-09-09 stsp | grammar author '\n'
117 257add31 2020-09-09 stsp | grammar remote '\n'
119 257add31 2020-09-09 stsp boolean : STRING {
120 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
121 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
123 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
124 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
127 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
134 257add31 2020-09-09 stsp numberstring : NUMBER {
136 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
137 257add31 2020-09-09 stsp yyerror("string: asprintf");
144 257add31 2020-09-09 stsp portplain : numberstring {
145 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
152 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
153 257add31 2020-09-09 stsp | remoteopts1 optnl
155 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
156 257add31 2020-09-09 stsp remote->repository = strdup($2);
157 257add31 2020-09-09 stsp if (remote->repository == NULL) {
159 257add31 2020-09-09 stsp yyerror("strdup");
164 257add31 2020-09-09 stsp | SERVER STRING {
165 257add31 2020-09-09 stsp remote->server = strdup($2);
166 257add31 2020-09-09 stsp if (remote->server == NULL) {
168 257add31 2020-09-09 stsp yyerror("strdup");
173 257add31 2020-09-09 stsp | PROTOCOL STRING {
174 257add31 2020-09-09 stsp remote->protocol = strdup($2);
175 257add31 2020-09-09 stsp if (remote->protocol == NULL) {
177 257add31 2020-09-09 stsp yyerror("strdup");
182 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
183 257add31 2020-09-09 stsp remote->mirror_references = $2;
185 257add31 2020-09-09 stsp | PORT portplain {
186 257add31 2020-09-09 stsp remote->port = $2;
189 257add31 2020-09-09 stsp remote : REMOTE STRING {
190 257add31 2020-09-09 stsp static const struct got_error* error;
192 257add31 2020-09-09 stsp error = new_remote(&remote);
193 257add31 2020-09-09 stsp if (error) {
195 257add31 2020-09-09 stsp yyerror("%s", error->msg);
198 257add31 2020-09-09 stsp remote->name = strdup($2);
199 257add31 2020-09-09 stsp if (remote->name == NULL) {
201 257add31 2020-09-09 stsp yyerror("strdup");
205 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
206 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
207 257add31 2020-09-09 stsp gotconfig.nremotes++;
210 257add31 2020-09-09 stsp author : AUTHOR STRING {
211 257add31 2020-09-09 stsp gotconfig.author = strdup($2);
212 257add31 2020-09-09 stsp if (gotconfig.author == NULL) {
214 257add31 2020-09-09 stsp yyerror("strdup");
220 257add31 2020-09-09 stsp optnl : '\n' optnl
221 257add31 2020-09-09 stsp | /* empty */
223 257add31 2020-09-09 stsp nl : '\n' optnl
227 257add31 2020-09-09 stsp struct keywords {
228 257add31 2020-09-09 stsp const char *k_name;
233 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
235 257add31 2020-09-09 stsp va_list ap;
237 257add31 2020-09-09 stsp char *err = NULL;
239 257add31 2020-09-09 stsp va_start(ap, fmt);
240 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
241 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
244 257add31 2020-09-09 stsp va_end(ap);
245 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
246 257add31 2020-09-09 stsp msg) == -1) {
247 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
250 257add31 2020-09-09 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
256 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
258 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
262 257add31 2020-09-09 stsp lookup(char *s)
264 257add31 2020-09-09 stsp /* This has to be sorted always. */
265 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
266 257add31 2020-09-09 stsp {"author", AUTHOR},
267 257add31 2020-09-09 stsp {"mirror-references", MIRROR_REFERENCES},
268 257add31 2020-09-09 stsp {"port", PORT},
269 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
270 257add31 2020-09-09 stsp {"remote", REMOTE},
271 257add31 2020-09-09 stsp {"repository", REPOSITORY},
272 257add31 2020-09-09 stsp {"server", SERVER},
274 257add31 2020-09-09 stsp const struct keywords *p;
276 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
277 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
280 257add31 2020-09-09 stsp return (p->k_val);
282 257add31 2020-09-09 stsp return (STRING);
285 257add31 2020-09-09 stsp #define START_EXPAND 1
286 257add31 2020-09-09 stsp #define DONE_EXPAND 2
288 257add31 2020-09-09 stsp static int expanding;
291 257add31 2020-09-09 stsp igetc(void)
295 257add31 2020-09-09 stsp while (1) {
296 257add31 2020-09-09 stsp if (file->ungetpos > 0)
297 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
299 257add31 2020-09-09 stsp c = getc(file->stream);
301 257add31 2020-09-09 stsp if (c == START_EXPAND)
302 257add31 2020-09-09 stsp expanding = 1;
303 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
304 257add31 2020-09-09 stsp expanding = 0;
308 257add31 2020-09-09 stsp return (c);
312 257add31 2020-09-09 stsp lgetc(int quotec)
314 257add31 2020-09-09 stsp int c, next;
316 257add31 2020-09-09 stsp if (quotec) {
317 257add31 2020-09-09 stsp c = igetc();
318 257add31 2020-09-09 stsp if (c == EOF) {
319 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
320 257add31 2020-09-09 stsp "quoted string");
322 257add31 2020-09-09 stsp return (c);
325 257add31 2020-09-09 stsp c = igetc();
326 257add31 2020-09-09 stsp while (c == '\\') {
327 257add31 2020-09-09 stsp next = igetc();
328 257add31 2020-09-09 stsp if (next != '\n') {
332 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
333 257add31 2020-09-09 stsp file->lineno++;
336 257add31 2020-09-09 stsp return (c);
340 257add31 2020-09-09 stsp lungetc(int c)
342 257add31 2020-09-09 stsp if (c == EOF)
345 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
346 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
347 257add31 2020-09-09 stsp if (p == NULL)
348 257add31 2020-09-09 stsp err(1, "%s", __func__);
349 257add31 2020-09-09 stsp file->ungetbuf = p;
350 257add31 2020-09-09 stsp file->ungetsize *= 2;
352 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
356 257add31 2020-09-09 stsp findeol(void)
360 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
361 257add31 2020-09-09 stsp while (1) {
362 257add31 2020-09-09 stsp c = lgetc(0);
363 257add31 2020-09-09 stsp if (c == '\n') {
364 257add31 2020-09-09 stsp file->lineno++;
367 257add31 2020-09-09 stsp if (c == EOF)
370 257add31 2020-09-09 stsp return (ERROR);
373 257add31 2020-09-09 stsp static long long
374 257add31 2020-09-09 stsp getservice(char *n)
376 257add31 2020-09-09 stsp struct servent *s;
377 257add31 2020-09-09 stsp u_long ulval;
379 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
380 257add31 2020-09-09 stsp if (ulval > 65535) {
381 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
382 257add31 2020-09-09 stsp return (-1);
384 257add31 2020-09-09 stsp return ulval;
386 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
387 257add31 2020-09-09 stsp if (s == NULL)
388 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
389 257add31 2020-09-09 stsp if (s == NULL) {
390 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
391 257add31 2020-09-09 stsp return (-1);
393 257add31 2020-09-09 stsp return (s->s_port);
398 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
400 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
402 257add31 2020-09-09 stsp return (-1);
404 257add31 2020-09-09 stsp return (0);
409 257add31 2020-09-09 stsp yylex(void)
411 257add31 2020-09-09 stsp unsigned char buf[8096];
412 257add31 2020-09-09 stsp unsigned char *p, *val;
413 257add31 2020-09-09 stsp int quotec, next, c;
418 257add31 2020-09-09 stsp c = lgetc(0);
419 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
420 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
422 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
423 257add31 2020-09-09 stsp if (c == '#') {
424 257add31 2020-09-09 stsp c = lgetc(0);
425 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
426 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
428 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
429 257add31 2020-09-09 stsp while (1) {
430 257add31 2020-09-09 stsp c = lgetc(0);
431 257add31 2020-09-09 stsp if (c == EOF)
432 257add31 2020-09-09 stsp return (0);
434 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
435 257add31 2020-09-09 stsp yyerror("string too long");
436 257add31 2020-09-09 stsp return (findeol());
438 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
443 257add31 2020-09-09 stsp lungetc(c);
446 257add31 2020-09-09 stsp val = symget(buf);
447 257add31 2020-09-09 stsp if (val == NULL) {
448 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
449 257add31 2020-09-09 stsp return (findeol());
451 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
452 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
453 257add31 2020-09-09 stsp while (p >= val) {
454 257add31 2020-09-09 stsp lungetc(*p);
457 257add31 2020-09-09 stsp lungetc(START_EXPAND);
461 257add31 2020-09-09 stsp switch (c) {
464 257add31 2020-09-09 stsp quotec = c;
465 257add31 2020-09-09 stsp while (1) {
466 257add31 2020-09-09 stsp c = lgetc(quotec);
467 257add31 2020-09-09 stsp if (c == EOF)
468 257add31 2020-09-09 stsp return (0);
469 257add31 2020-09-09 stsp if (c == '\n') {
470 257add31 2020-09-09 stsp file->lineno++;
472 257add31 2020-09-09 stsp } else if (c == '\\') {
473 257add31 2020-09-09 stsp next = lgetc(quotec);
474 257add31 2020-09-09 stsp if (next == EOF)
475 257add31 2020-09-09 stsp return (0);
476 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
478 257add31 2020-09-09 stsp else if (next == '\n') {
479 257add31 2020-09-09 stsp file->lineno++;
482 257add31 2020-09-09 stsp lungetc(next);
483 257add31 2020-09-09 stsp } else if (c == quotec) {
486 257add31 2020-09-09 stsp } else if (c == '\0') {
487 257add31 2020-09-09 stsp yyerror("syntax error");
488 257add31 2020-09-09 stsp return (findeol());
490 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
491 257add31 2020-09-09 stsp yyerror("string too long");
492 257add31 2020-09-09 stsp return (findeol());
496 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
497 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
498 257add31 2020-09-09 stsp err(1, "%s", __func__);
499 257add31 2020-09-09 stsp return (STRING);
502 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
503 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
505 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
508 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
509 257add31 2020-09-09 stsp yyerror("string too long");
510 257add31 2020-09-09 stsp return (findeol());
512 257add31 2020-09-09 stsp c = lgetc(0);
513 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
514 257add31 2020-09-09 stsp lungetc(c);
515 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
516 257add31 2020-09-09 stsp goto nodigits;
517 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
518 257add31 2020-09-09 stsp const char *errstr = NULL;
521 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
522 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
523 257add31 2020-09-09 stsp if (errstr) {
524 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
525 257add31 2020-09-09 stsp buf, errstr);
526 257add31 2020-09-09 stsp return (findeol());
528 257add31 2020-09-09 stsp return (NUMBER);
531 257add31 2020-09-09 stsp while (p > buf + 1)
532 257add31 2020-09-09 stsp lungetc(*--p);
534 257add31 2020-09-09 stsp if (c == '-')
535 257add31 2020-09-09 stsp return (c);
539 257add31 2020-09-09 stsp #define allowed_in_string(x) \
540 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
541 257add31 2020-09-09 stsp x != '{' && x != '}' && \
542 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
545 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
548 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
549 257add31 2020-09-09 stsp yyerror("string too long");
550 257add31 2020-09-09 stsp return (findeol());
552 257add31 2020-09-09 stsp c = lgetc(0);
553 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
554 257add31 2020-09-09 stsp lungetc(c);
556 257add31 2020-09-09 stsp token = lookup(buf);
557 257add31 2020-09-09 stsp if (token == STRING) {
558 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
559 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
560 257add31 2020-09-09 stsp err(1, "%s", __func__);
562 257add31 2020-09-09 stsp return (token);
564 257add31 2020-09-09 stsp if (c == '\n') {
565 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
566 257add31 2020-09-09 stsp file->lineno++;
568 257add31 2020-09-09 stsp if (c == EOF)
569 257add31 2020-09-09 stsp return (0);
570 257add31 2020-09-09 stsp return (c);
573 257add31 2020-09-09 stsp static const struct got_error*
574 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
576 257add31 2020-09-09 stsp const struct got_error* error = NULL;
578 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
579 257add31 2020-09-09 stsp if ((*nfile) == NULL)
580 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
581 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
582 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
583 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
584 257add31 2020-09-09 stsp free((*nfile));
585 257add31 2020-09-09 stsp return error;
587 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
588 257add31 2020-09-09 stsp (*nfile)->name = filename;
589 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
590 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
591 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
592 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
593 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
594 257add31 2020-09-09 stsp fclose((*nfile)->stream);
595 257add31 2020-09-09 stsp free((*nfile));
596 257add31 2020-09-09 stsp return error;
598 257add31 2020-09-09 stsp return NULL;
601 257add31 2020-09-09 stsp static const struct got_error*
602 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
604 257add31 2020-09-09 stsp const struct got_error *error = NULL;
606 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
607 257add31 2020-09-09 stsp if (*remote == NULL)
608 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
609 257add31 2020-09-09 stsp return error;
612 257add31 2020-09-09 stsp static void
613 257add31 2020-09-09 stsp closefile(struct file *file)
615 257add31 2020-09-09 stsp fclose(file->stream);
616 257add31 2020-09-09 stsp free(file->ungetbuf);
617 257add31 2020-09-09 stsp free(file);
620 257add31 2020-09-09 stsp const struct got_error *
621 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
623 257add31 2020-09-09 stsp const struct got_error *err = NULL;
624 257add31 2020-09-09 stsp struct sym *sym, *next;
626 257add31 2020-09-09 stsp *conf = NULL;
628 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
630 257add31 2020-09-09 stsp return err;
632 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
635 257add31 2020-09-09 stsp closefile(file);
637 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
638 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
639 257add31 2020-09-09 stsp if (!sym->persist) {
640 257add31 2020-09-09 stsp free(sym->nam);
641 257add31 2020-09-09 stsp free(sym->val);
642 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
647 257add31 2020-09-09 stsp if (gerror == NULL)
648 257add31 2020-09-09 stsp *conf = &gotconfig;
649 257add31 2020-09-09 stsp return gerror;
653 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
655 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
657 257add31 2020-09-09 stsp free(conf->author);
658 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
659 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
660 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
661 257add31 2020-09-09 stsp free(remote->name);
662 257add31 2020-09-09 stsp free(remote->repository);
663 257add31 2020-09-09 stsp free(remote->server);
664 257add31 2020-09-09 stsp free(remote->protocol);
665 257add31 2020-09-09 stsp free(remote);
670 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
672 257add31 2020-09-09 stsp struct sym *sym;
674 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
675 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
679 257add31 2020-09-09 stsp if (sym != NULL) {
680 257add31 2020-09-09 stsp if (sym->persist == 1)
681 257add31 2020-09-09 stsp return (0);
683 257add31 2020-09-09 stsp free(sym->nam);
684 257add31 2020-09-09 stsp free(sym->val);
685 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
689 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
690 257add31 2020-09-09 stsp if (sym == NULL)
691 257add31 2020-09-09 stsp return (-1);
693 257add31 2020-09-09 stsp sym->nam = strdup(nam);
694 257add31 2020-09-09 stsp if (sym->nam == NULL) {
696 257add31 2020-09-09 stsp return (-1);
698 257add31 2020-09-09 stsp sym->val = strdup(val);
699 257add31 2020-09-09 stsp if (sym->val == NULL) {
700 257add31 2020-09-09 stsp free(sym->nam);
702 257add31 2020-09-09 stsp return (-1);
704 257add31 2020-09-09 stsp sym->used = 0;
705 257add31 2020-09-09 stsp sym->persist = persist;
706 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
707 257add31 2020-09-09 stsp return (0);
711 257add31 2020-09-09 stsp cmdline_symset(char *s)
713 257add31 2020-09-09 stsp char *sym, *val;
715 257add31 2020-09-09 stsp size_t len;
717 257add31 2020-09-09 stsp val = strrchr(s, '=');
718 257add31 2020-09-09 stsp if (val == NULL)
719 257add31 2020-09-09 stsp return (-1);
721 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
722 257add31 2020-09-09 stsp sym = malloc(len);
723 257add31 2020-09-09 stsp if (sym == NULL)
724 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
726 257add31 2020-09-09 stsp strlcpy(sym, s, len);
728 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
731 257add31 2020-09-09 stsp return (ret);
735 257add31 2020-09-09 stsp symget(const char *nam)
737 257add31 2020-09-09 stsp struct sym *sym;
739 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
740 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
741 257add31 2020-09-09 stsp sym->used = 1;
742 257add31 2020-09-09 stsp return (sym->val);
745 257add31 2020-09-09 stsp return (NULL);
749 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
751 257add31 2020-09-09 stsp u_long ulval;
755 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
756 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
757 257add31 2020-09-09 stsp return (-1);
758 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
759 257add31 2020-09-09 stsp return (-1);
760 257add31 2020-09-09 stsp *ulvalp = ulval;
761 257add31 2020-09-09 stsp return (0);