2 * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
3 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include <sys/types.h>
26 #include <sys/queue.h>
27 #include <sys/socket.h>
30 #include <netinet/in.h>
32 #include <arpa/inet.h>
49 #include "got_error.h"
50 #include "gotconfig.h"
61 static const struct got_error* newfile(struct file**, const char *, int *);
62 static void closefile(struct file *);
65 int yyerror(const char *, ...)
66 __attribute__((__format__ (printf, 1, 2)))
67 __attribute__((__nonnull__ (1)));
68 int kw_cmp(const void *, const void *);
74 static int parseport(char *, long long *);
76 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
78 TAILQ_ENTRY(sym) entry;
85 int symset(const char *, const char *, int);
86 char *symget(const char *);
88 static int atoul(char *, u_long *);
90 static const struct got_error* gerror;
91 static struct gotconfig_remote_repo *remote;
92 static struct gotconfig gotconfig;
93 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
106 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES AUTHOR
107 %token <v.string> STRING
108 %token <v.number> NUMBER
109 %type <v.number> boolean portplain
110 %type <v.string> numberstring
114 grammar : /* empty */
116 | grammar author '\n'
117 | grammar remote '\n'
120 if (strcasecmp($1, "true") == 0 ||
121 strcasecmp($1, "yes") == 0)
123 else if (strcasecmp($1, "false") == 0 ||
124 strcasecmp($1, "no") == 0)
127 yyerror("invalid boolean value '%s'", $1);
134 numberstring : NUMBER {
136 if (asprintf(&s, "%lld", $1) == -1) {
137 yyerror("string: asprintf");
144 portplain : numberstring {
145 if (parseport($1, &$$) == -1) {
152 remoteopts2 : remoteopts2 remoteopts1 nl
155 remoteopts1 : REPOSITORY STRING {
156 remote->repository = strdup($2);
157 if (remote->repository == NULL) {
165 remote->server = strdup($2);
166 if (remote->server == NULL) {
174 remote->protocol = strdup($2);
175 if (remote->protocol == NULL) {
182 | MIRROR_REFERENCES boolean {
183 remote->mirror_references = $2;
189 remote : REMOTE STRING {
190 static const struct got_error* error;
192 error = new_remote(&remote);
195 yyerror("%s", error->msg);
198 remote->name = strdup($2);
199 if (remote->name == NULL) {
205 } '{' optnl remoteopts2 '}' {
206 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
207 gotconfig.nremotes++;
210 author : AUTHOR STRING {
211 gotconfig.author = strdup($2);
212 if (gotconfig.author == NULL) {
233 yyerror(const char *fmt, ...)
240 if (vasprintf(&msg, fmt, ap) == -1) {
241 gerror = got_error_from_errno("vasprintf");
245 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
247 gerror = got_error_from_errno("asprintf");
250 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
256 kw_cmp(const void *k, const void *e)
258 return (strcmp(k, ((const struct keywords *)e)->k_name));
264 /* This has to be sorted always. */
265 static const struct keywords keywords[] = {
267 {"mirror-references", MIRROR_REFERENCES},
269 {"protocol", PROTOCOL},
271 {"repository", REPOSITORY},
274 const struct keywords *p;
276 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
277 sizeof(keywords[0]), kw_cmp);
285 #define START_EXPAND 1
286 #define DONE_EXPAND 2
288 static int expanding;
296 if (file->ungetpos > 0)
297 c = file->ungetbuf[--file->ungetpos];
299 c = getc(file->stream);
301 if (c == START_EXPAND)
303 else if (c == DONE_EXPAND)
319 yyerror("reached end of file while parsing "
332 yylval.lineno = file->lineno;
345 if (file->ungetpos >= file->ungetsize) {
346 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
348 err(1, "%s", __func__);
350 file->ungetsize *= 2;
352 file->ungetbuf[file->ungetpos++] = c;
360 /* Skip to either EOF or the first real EOL. */
379 if (atoul(n, &ulval) == 0) {
381 yyerror("illegal port value %lu", ulval);
386 s = getservbyname(n, "tcp");
388 s = getservbyname(n, "udp");
390 yyerror("unknown port %s", n);
398 parseport(char *port, long long *pn)
400 if ((*pn = getservice(port)) == -1) {
411 unsigned char buf[8096];
412 unsigned char *p, *val;
419 while (c == ' ' || c == '\t')
420 c = lgetc(0); /* nothing */
422 yylval.lineno = file->lineno;
425 while (c != '\n' && c != EOF)
426 c = lgetc(0); /* nothing */
428 if (c == '$' && !expanding) {
434 if (p + 1 >= buf + sizeof(buf) - 1) {
435 yyerror("string too long");
438 if (isalnum(c) || c == '_') {
448 yyerror("macro '%s' not defined", buf);
451 p = val + strlen(val) - 1;
452 lungetc(DONE_EXPAND);
457 lungetc(START_EXPAND);
472 } else if (c == '\\') {
473 next = lgetc(quotec);
476 if (next == quotec || c == ' ' || c == '\t')
478 else if (next == '\n') {
483 } else if (c == quotec) {
486 } else if (c == '\0') {
487 yyerror("syntax error");
490 if (p + 1 >= buf + sizeof(buf) - 1) {
491 yyerror("string too long");
496 yylval.v.string = strdup(buf);
497 if (yylval.v.string == NULL)
498 err(1, "%s", __func__);
502 #define allowed_to_end_number(x) \
503 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
505 if (c == '-' || isdigit(c)) {
508 if ((unsigned)(p-buf) >= sizeof(buf)) {
509 yyerror("string too long");
513 } while (c != EOF && isdigit(c));
515 if (p == buf + 1 && buf[0] == '-')
517 if (c == EOF || allowed_to_end_number(c)) {
518 const char *errstr = NULL;
521 yylval.v.number = strtonum(buf, LLONG_MIN,
524 yyerror("\"%s\" invalid number: %s",
539 #define allowed_in_string(x) \
540 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
541 x != '{' && x != '}' && \
542 x != '!' && x != '=' && x != '#' && \
545 if (isalnum(c) || c == ':' || c == '_') {
548 if ((unsigned)(p-buf) >= sizeof(buf)) {
549 yyerror("string too long");
553 } while (c != EOF && (allowed_in_string(c)));
557 if (token == STRING) {
558 yylval.v.string = strdup(buf);
559 if (yylval.v.string == NULL)
560 err(1, "%s", __func__);
565 yylval.lineno = file->lineno;
573 static const struct got_error*
574 newfile(struct file **nfile, const char *filename, int *fd)
576 const struct got_error* error = NULL;
578 (*nfile) = calloc(1, sizeof(struct file));
579 if ((*nfile) == NULL)
580 return got_error_from_errno("calloc");
581 (*nfile)->stream = fdopen(*fd, "r");
582 if ((*nfile)->stream == NULL) {
583 error = got_error_from_errno("fdopen");
587 *fd = -1; /* Stream owns the file descriptor now. */
588 (*nfile)->name = filename;
589 (*nfile)->lineno = 1;
590 (*nfile)->ungetsize = 16;
591 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
592 if ((*nfile)->ungetbuf == NULL) {
593 error = got_error_from_errno("malloc");
594 fclose((*nfile)->stream);
601 static const struct got_error*
602 new_remote(struct gotconfig_remote_repo **remote)
604 const struct got_error *error = NULL;
606 *remote = calloc(1, sizeof(**remote));
608 error = got_error_from_errno("calloc");
613 closefile(struct file *file)
615 fclose(file->stream);
616 free(file->ungetbuf);
620 const struct got_error *
621 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
623 const struct got_error *err = NULL;
624 struct sym *sym, *next;
628 err = newfile(&file, filename, fd);
632 TAILQ_INIT(&gotconfig.remotes);
637 /* Free macros and check which have not been used. */
638 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
642 TAILQ_REMOVE(&symhead, sym, entry);
653 gotconfig_free(struct gotconfig *conf)
655 struct gotconfig_remote_repo *remote;
658 while (!TAILQ_EMPTY(&conf->remotes)) {
659 remote = TAILQ_FIRST(&conf->remotes);
660 TAILQ_REMOVE(&conf->remotes, remote, entry);
662 free(remote->repository);
663 free(remote->server);
664 free(remote->protocol);
670 symset(const char *nam, const char *val, int persist)
674 TAILQ_FOREACH(sym, &symhead, entry) {
675 if (strcmp(nam, sym->nam) == 0)
680 if (sym->persist == 1)
685 TAILQ_REMOVE(&symhead, sym, entry);
689 sym = calloc(1, sizeof(*sym));
693 sym->nam = strdup(nam);
694 if (sym->nam == NULL) {
698 sym->val = strdup(val);
699 if (sym->val == NULL) {
705 sym->persist = persist;
706 TAILQ_INSERT_TAIL(&symhead, sym, entry);
711 cmdline_symset(char *s)
717 val = strrchr(s, '=');
721 len = strlen(s) - strlen(val) + 1;
724 errx(1, "cmdline_symset: malloc");
726 strlcpy(sym, s, len);
728 ret = symset(sym, val + 1, 1);
735 symget(const char *nam)
739 TAILQ_FOREACH(sym, &symhead, entry) {
740 if (strcmp(nam, sym->nam) == 0) {
749 atoul(char *s, u_long *ulvalp)
755 ulval = strtoul(s, &ep, 0);
756 if (s[0] == '\0' || *ep != '\0')
758 if (errno == ERANGE && ulval == ULONG_MAX)