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>
28 257add31 2020-09-09 stsp #include <netdb.h>
30 257add31 2020-09-09 stsp #include <ctype.h>
31 257add31 2020-09-09 stsp #include <err.h>
32 257add31 2020-09-09 stsp #include <errno.h>
33 257add31 2020-09-09 stsp #include <limits.h>
34 257add31 2020-09-09 stsp #include <stdarg.h>
35 257add31 2020-09-09 stsp #include <stdio.h>
36 8de9818a 2020-09-14 naddy #include <stdlib.h>
37 257add31 2020-09-09 stsp #include <string.h>
39 257add31 2020-09-09 stsp #include "got_error.h"
40 257add31 2020-09-09 stsp #include "gotconfig.h"
42 257add31 2020-09-09 stsp static struct file {
43 257add31 2020-09-09 stsp FILE *stream;
44 257add31 2020-09-09 stsp const char *name;
45 257add31 2020-09-09 stsp size_t ungetpos;
46 257add31 2020-09-09 stsp size_t ungetsize;
47 257add31 2020-09-09 stsp u_char *ungetbuf;
48 257add31 2020-09-09 stsp int eof_reached;
51 257add31 2020-09-09 stsp static const struct got_error* newfile(struct file**, const char *, int *);
52 257add31 2020-09-09 stsp static void closefile(struct file *);
53 257add31 2020-09-09 stsp int yyparse(void);
54 257add31 2020-09-09 stsp int yylex(void);
55 257add31 2020-09-09 stsp int yyerror(const char *, ...)
56 257add31 2020-09-09 stsp __attribute__((__format__ (printf, 1, 2)))
57 257add31 2020-09-09 stsp __attribute__((__nonnull__ (1)));
58 257add31 2020-09-09 stsp int kw_cmp(const void *, const void *);
59 257add31 2020-09-09 stsp int lookup(char *);
60 257add31 2020-09-09 stsp int igetc(void);
61 257add31 2020-09-09 stsp int lgetc(int);
62 257add31 2020-09-09 stsp void lungetc(int);
63 257add31 2020-09-09 stsp int findeol(void);
64 257add31 2020-09-09 stsp static int parseport(char *, long long *);
66 257add31 2020-09-09 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
67 257add31 2020-09-09 stsp struct sym {
68 257add31 2020-09-09 stsp TAILQ_ENTRY(sym) entry;
70 257add31 2020-09-09 stsp int persist;
75 257add31 2020-09-09 stsp int symset(const char *, const char *, int);
76 257add31 2020-09-09 stsp char *symget(const char *);
78 257add31 2020-09-09 stsp static int atoul(char *, u_long *);
80 257add31 2020-09-09 stsp static const struct got_error* gerror;
81 257add31 2020-09-09 stsp static struct gotconfig_remote_repo *remote;
82 257add31 2020-09-09 stsp static struct gotconfig gotconfig;
83 257add31 2020-09-09 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
85 257add31 2020-09-09 stsp typedef struct {
87 257add31 2020-09-09 stsp int64_t number;
88 257add31 2020-09-09 stsp char *string;
95 257add31 2020-09-09 stsp %token ERROR
96 257add31 2020-09-09 stsp %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES AUTHOR
97 257add31 2020-09-09 stsp %token <v.string> STRING
98 257add31 2020-09-09 stsp %token <v.number> NUMBER
99 257add31 2020-09-09 stsp %type <v.number> boolean portplain
100 257add31 2020-09-09 stsp %type <v.string> numberstring
104 257add31 2020-09-09 stsp grammar : /* empty */
105 257add31 2020-09-09 stsp | grammar '\n'
106 257add31 2020-09-09 stsp | grammar author '\n'
107 257add31 2020-09-09 stsp | grammar remote '\n'
109 257add31 2020-09-09 stsp boolean : STRING {
110 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
111 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
113 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
114 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
117 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
124 257add31 2020-09-09 stsp numberstring : NUMBER {
126 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
127 257add31 2020-09-09 stsp yyerror("string: asprintf");
134 257add31 2020-09-09 stsp portplain : numberstring {
135 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
142 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
143 257add31 2020-09-09 stsp | remoteopts1 optnl
145 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
146 257add31 2020-09-09 stsp remote->repository = strdup($2);
147 257add31 2020-09-09 stsp if (remote->repository == NULL) {
149 257add31 2020-09-09 stsp yyerror("strdup");
154 257add31 2020-09-09 stsp | SERVER STRING {
155 257add31 2020-09-09 stsp remote->server = strdup($2);
156 257add31 2020-09-09 stsp if (remote->server == NULL) {
158 257add31 2020-09-09 stsp yyerror("strdup");
163 257add31 2020-09-09 stsp | PROTOCOL STRING {
164 257add31 2020-09-09 stsp remote->protocol = strdup($2);
165 257add31 2020-09-09 stsp if (remote->protocol == NULL) {
167 257add31 2020-09-09 stsp yyerror("strdup");
172 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
173 257add31 2020-09-09 stsp remote->mirror_references = $2;
175 257add31 2020-09-09 stsp | PORT portplain {
176 257add31 2020-09-09 stsp remote->port = $2;
179 257add31 2020-09-09 stsp remote : REMOTE STRING {
180 257add31 2020-09-09 stsp static const struct got_error* error;
182 257add31 2020-09-09 stsp error = new_remote(&remote);
183 257add31 2020-09-09 stsp if (error) {
185 257add31 2020-09-09 stsp yyerror("%s", error->msg);
188 257add31 2020-09-09 stsp remote->name = strdup($2);
189 257add31 2020-09-09 stsp if (remote->name == NULL) {
191 257add31 2020-09-09 stsp yyerror("strdup");
195 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
196 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
197 257add31 2020-09-09 stsp gotconfig.nremotes++;
200 257add31 2020-09-09 stsp author : AUTHOR STRING {
201 257add31 2020-09-09 stsp gotconfig.author = strdup($2);
202 257add31 2020-09-09 stsp if (gotconfig.author == NULL) {
204 257add31 2020-09-09 stsp yyerror("strdup");
210 257add31 2020-09-09 stsp optnl : '\n' optnl
211 257add31 2020-09-09 stsp | /* empty */
213 257add31 2020-09-09 stsp nl : '\n' optnl
217 257add31 2020-09-09 stsp struct keywords {
218 257add31 2020-09-09 stsp const char *k_name;
223 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
225 257add31 2020-09-09 stsp va_list ap;
227 257add31 2020-09-09 stsp char *err = NULL;
229 257add31 2020-09-09 stsp va_start(ap, fmt);
230 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
231 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
234 257add31 2020-09-09 stsp va_end(ap);
235 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
236 257add31 2020-09-09 stsp msg) == -1) {
237 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
240 257add31 2020-09-09 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
246 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
248 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
252 257add31 2020-09-09 stsp lookup(char *s)
254 257add31 2020-09-09 stsp /* This has to be sorted always. */
255 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
256 257add31 2020-09-09 stsp {"author", AUTHOR},
257 257add31 2020-09-09 stsp {"mirror-references", MIRROR_REFERENCES},
258 257add31 2020-09-09 stsp {"port", PORT},
259 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
260 257add31 2020-09-09 stsp {"remote", REMOTE},
261 257add31 2020-09-09 stsp {"repository", REPOSITORY},
262 257add31 2020-09-09 stsp {"server", SERVER},
264 257add31 2020-09-09 stsp const struct keywords *p;
266 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
267 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
270 257add31 2020-09-09 stsp return (p->k_val);
272 257add31 2020-09-09 stsp return (STRING);
275 257add31 2020-09-09 stsp #define START_EXPAND 1
276 257add31 2020-09-09 stsp #define DONE_EXPAND 2
278 257add31 2020-09-09 stsp static int expanding;
281 257add31 2020-09-09 stsp igetc(void)
285 257add31 2020-09-09 stsp while (1) {
286 257add31 2020-09-09 stsp if (file->ungetpos > 0)
287 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
289 257add31 2020-09-09 stsp c = getc(file->stream);
291 257add31 2020-09-09 stsp if (c == START_EXPAND)
292 257add31 2020-09-09 stsp expanding = 1;
293 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
294 257add31 2020-09-09 stsp expanding = 0;
298 257add31 2020-09-09 stsp return (c);
302 257add31 2020-09-09 stsp lgetc(int quotec)
304 257add31 2020-09-09 stsp int c, next;
306 257add31 2020-09-09 stsp if (quotec) {
307 257add31 2020-09-09 stsp c = igetc();
308 257add31 2020-09-09 stsp if (c == EOF) {
309 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
310 257add31 2020-09-09 stsp "quoted string");
312 257add31 2020-09-09 stsp return (c);
315 257add31 2020-09-09 stsp c = igetc();
316 257add31 2020-09-09 stsp while (c == '\\') {
317 257add31 2020-09-09 stsp next = igetc();
318 257add31 2020-09-09 stsp if (next != '\n') {
322 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
323 257add31 2020-09-09 stsp file->lineno++;
326 257add31 2020-09-09 stsp return (c);
330 257add31 2020-09-09 stsp lungetc(int c)
332 257add31 2020-09-09 stsp if (c == EOF)
335 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
336 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
337 257add31 2020-09-09 stsp if (p == NULL)
338 257add31 2020-09-09 stsp err(1, "%s", __func__);
339 257add31 2020-09-09 stsp file->ungetbuf = p;
340 257add31 2020-09-09 stsp file->ungetsize *= 2;
342 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
346 257add31 2020-09-09 stsp findeol(void)
350 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
351 257add31 2020-09-09 stsp while (1) {
352 257add31 2020-09-09 stsp c = lgetc(0);
353 257add31 2020-09-09 stsp if (c == '\n') {
354 257add31 2020-09-09 stsp file->lineno++;
357 257add31 2020-09-09 stsp if (c == EOF)
360 257add31 2020-09-09 stsp return (ERROR);
363 257add31 2020-09-09 stsp static long long
364 257add31 2020-09-09 stsp getservice(char *n)
366 257add31 2020-09-09 stsp struct servent *s;
367 257add31 2020-09-09 stsp u_long ulval;
369 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
370 257add31 2020-09-09 stsp if (ulval > 65535) {
371 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
372 257add31 2020-09-09 stsp return (-1);
374 257add31 2020-09-09 stsp return ulval;
376 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
377 257add31 2020-09-09 stsp if (s == NULL)
378 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
379 257add31 2020-09-09 stsp if (s == NULL) {
380 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
381 257add31 2020-09-09 stsp return (-1);
383 257add31 2020-09-09 stsp return (s->s_port);
388 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
390 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
392 257add31 2020-09-09 stsp return (-1);
394 257add31 2020-09-09 stsp return (0);
399 257add31 2020-09-09 stsp yylex(void)
401 257add31 2020-09-09 stsp unsigned char buf[8096];
402 257add31 2020-09-09 stsp unsigned char *p, *val;
403 257add31 2020-09-09 stsp int quotec, next, c;
408 257add31 2020-09-09 stsp c = lgetc(0);
409 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
410 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
412 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
413 257add31 2020-09-09 stsp if (c == '#') {
414 257add31 2020-09-09 stsp c = lgetc(0);
415 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
416 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
418 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
419 257add31 2020-09-09 stsp while (1) {
420 257add31 2020-09-09 stsp c = lgetc(0);
421 257add31 2020-09-09 stsp if (c == EOF)
422 257add31 2020-09-09 stsp return (0);
424 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
425 257add31 2020-09-09 stsp yyerror("string too long");
426 257add31 2020-09-09 stsp return (findeol());
428 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
433 257add31 2020-09-09 stsp lungetc(c);
436 257add31 2020-09-09 stsp val = symget(buf);
437 257add31 2020-09-09 stsp if (val == NULL) {
438 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
439 257add31 2020-09-09 stsp return (findeol());
441 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
442 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
443 257add31 2020-09-09 stsp while (p >= val) {
444 257add31 2020-09-09 stsp lungetc(*p);
447 257add31 2020-09-09 stsp lungetc(START_EXPAND);
451 257add31 2020-09-09 stsp switch (c) {
454 257add31 2020-09-09 stsp quotec = c;
455 257add31 2020-09-09 stsp while (1) {
456 257add31 2020-09-09 stsp c = lgetc(quotec);
457 257add31 2020-09-09 stsp if (c == EOF)
458 257add31 2020-09-09 stsp return (0);
459 257add31 2020-09-09 stsp if (c == '\n') {
460 257add31 2020-09-09 stsp file->lineno++;
462 257add31 2020-09-09 stsp } else if (c == '\\') {
463 257add31 2020-09-09 stsp next = lgetc(quotec);
464 257add31 2020-09-09 stsp if (next == EOF)
465 257add31 2020-09-09 stsp return (0);
466 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
468 257add31 2020-09-09 stsp else if (next == '\n') {
469 257add31 2020-09-09 stsp file->lineno++;
472 257add31 2020-09-09 stsp lungetc(next);
473 257add31 2020-09-09 stsp } else if (c == quotec) {
476 257add31 2020-09-09 stsp } else if (c == '\0') {
477 257add31 2020-09-09 stsp yyerror("syntax error");
478 257add31 2020-09-09 stsp return (findeol());
480 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
481 257add31 2020-09-09 stsp yyerror("string too long");
482 257add31 2020-09-09 stsp return (findeol());
486 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
487 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
488 257add31 2020-09-09 stsp err(1, "%s", __func__);
489 257add31 2020-09-09 stsp return (STRING);
492 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
493 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
495 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
498 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
499 257add31 2020-09-09 stsp yyerror("string too long");
500 257add31 2020-09-09 stsp return (findeol());
502 257add31 2020-09-09 stsp c = lgetc(0);
503 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
504 257add31 2020-09-09 stsp lungetc(c);
505 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
506 257add31 2020-09-09 stsp goto nodigits;
507 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
508 257add31 2020-09-09 stsp const char *errstr = NULL;
511 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
512 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
513 257add31 2020-09-09 stsp if (errstr) {
514 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
515 257add31 2020-09-09 stsp buf, errstr);
516 257add31 2020-09-09 stsp return (findeol());
518 257add31 2020-09-09 stsp return (NUMBER);
521 257add31 2020-09-09 stsp while (p > buf + 1)
522 257add31 2020-09-09 stsp lungetc(*--p);
524 257add31 2020-09-09 stsp if (c == '-')
525 257add31 2020-09-09 stsp return (c);
529 257add31 2020-09-09 stsp #define allowed_in_string(x) \
530 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
531 257add31 2020-09-09 stsp x != '{' && x != '}' && \
532 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
535 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
538 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
539 257add31 2020-09-09 stsp yyerror("string too long");
540 257add31 2020-09-09 stsp return (findeol());
542 257add31 2020-09-09 stsp c = lgetc(0);
543 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
544 257add31 2020-09-09 stsp lungetc(c);
546 257add31 2020-09-09 stsp token = lookup(buf);
547 257add31 2020-09-09 stsp if (token == STRING) {
548 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
549 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
550 257add31 2020-09-09 stsp err(1, "%s", __func__);
552 257add31 2020-09-09 stsp return (token);
554 257add31 2020-09-09 stsp if (c == '\n') {
555 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
556 257add31 2020-09-09 stsp file->lineno++;
558 257add31 2020-09-09 stsp if (c == EOF)
559 257add31 2020-09-09 stsp return (0);
560 257add31 2020-09-09 stsp return (c);
563 257add31 2020-09-09 stsp static const struct got_error*
564 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
566 257add31 2020-09-09 stsp const struct got_error* error = NULL;
568 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
569 257add31 2020-09-09 stsp if ((*nfile) == NULL)
570 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
571 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
572 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
573 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
574 257add31 2020-09-09 stsp free((*nfile));
575 257add31 2020-09-09 stsp return error;
577 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
578 257add31 2020-09-09 stsp (*nfile)->name = filename;
579 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
580 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
581 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
582 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
583 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
584 257add31 2020-09-09 stsp fclose((*nfile)->stream);
585 257add31 2020-09-09 stsp free((*nfile));
586 257add31 2020-09-09 stsp return error;
588 257add31 2020-09-09 stsp return NULL;
591 257add31 2020-09-09 stsp static const struct got_error*
592 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
594 257add31 2020-09-09 stsp const struct got_error *error = NULL;
596 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
597 257add31 2020-09-09 stsp if (*remote == NULL)
598 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
599 257add31 2020-09-09 stsp return error;
602 257add31 2020-09-09 stsp static void
603 257add31 2020-09-09 stsp closefile(struct file *file)
605 257add31 2020-09-09 stsp fclose(file->stream);
606 257add31 2020-09-09 stsp free(file->ungetbuf);
607 257add31 2020-09-09 stsp free(file);
610 257add31 2020-09-09 stsp const struct got_error *
611 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
613 257add31 2020-09-09 stsp const struct got_error *err = NULL;
614 257add31 2020-09-09 stsp struct sym *sym, *next;
616 257add31 2020-09-09 stsp *conf = NULL;
618 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
620 257add31 2020-09-09 stsp return err;
622 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
625 257add31 2020-09-09 stsp closefile(file);
627 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
628 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
629 257add31 2020-09-09 stsp if (!sym->persist) {
630 257add31 2020-09-09 stsp free(sym->nam);
631 257add31 2020-09-09 stsp free(sym->val);
632 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
637 257add31 2020-09-09 stsp if (gerror == NULL)
638 257add31 2020-09-09 stsp *conf = &gotconfig;
639 257add31 2020-09-09 stsp return gerror;
643 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
645 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
647 257add31 2020-09-09 stsp free(conf->author);
648 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
649 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
650 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
651 257add31 2020-09-09 stsp free(remote->name);
652 257add31 2020-09-09 stsp free(remote->repository);
653 257add31 2020-09-09 stsp free(remote->server);
654 257add31 2020-09-09 stsp free(remote->protocol);
655 257add31 2020-09-09 stsp free(remote);
660 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
662 257add31 2020-09-09 stsp struct sym *sym;
664 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
665 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
669 257add31 2020-09-09 stsp if (sym != NULL) {
670 257add31 2020-09-09 stsp if (sym->persist == 1)
671 257add31 2020-09-09 stsp return (0);
673 257add31 2020-09-09 stsp free(sym->nam);
674 257add31 2020-09-09 stsp free(sym->val);
675 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
679 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
680 257add31 2020-09-09 stsp if (sym == NULL)
681 257add31 2020-09-09 stsp return (-1);
683 257add31 2020-09-09 stsp sym->nam = strdup(nam);
684 257add31 2020-09-09 stsp if (sym->nam == NULL) {
686 257add31 2020-09-09 stsp return (-1);
688 257add31 2020-09-09 stsp sym->val = strdup(val);
689 257add31 2020-09-09 stsp if (sym->val == NULL) {
690 257add31 2020-09-09 stsp free(sym->nam);
692 257add31 2020-09-09 stsp return (-1);
694 257add31 2020-09-09 stsp sym->used = 0;
695 257add31 2020-09-09 stsp sym->persist = persist;
696 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
697 257add31 2020-09-09 stsp return (0);
701 257add31 2020-09-09 stsp cmdline_symset(char *s)
703 257add31 2020-09-09 stsp char *sym, *val;
705 257add31 2020-09-09 stsp size_t len;
707 257add31 2020-09-09 stsp val = strrchr(s, '=');
708 257add31 2020-09-09 stsp if (val == NULL)
709 257add31 2020-09-09 stsp return (-1);
711 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
712 257add31 2020-09-09 stsp sym = malloc(len);
713 257add31 2020-09-09 stsp if (sym == NULL)
714 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
716 257add31 2020-09-09 stsp strlcpy(sym, s, len);
718 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
721 257add31 2020-09-09 stsp return (ret);
725 257add31 2020-09-09 stsp symget(const char *nam)
727 257add31 2020-09-09 stsp struct sym *sym;
729 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
730 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
731 257add31 2020-09-09 stsp sym->used = 1;
732 257add31 2020-09-09 stsp return (sym->val);
735 257add31 2020-09-09 stsp return (NULL);
739 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
741 257add31 2020-09-09 stsp u_long ulval;
745 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
746 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
747 257add31 2020-09-09 stsp return (-1);
748 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
749 257add31 2020-09-09 stsp return (-1);
750 257add31 2020-09-09 stsp *ulvalp = ulval;
751 257add31 2020-09-09 stsp return (0);