2 * Copyright (c) 2020, 2021 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>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "gotconfig.h"
52 static const struct got_error* newfile(struct file**, const char *, int *);
53 static void closefile(struct file *);
56 int yyerror(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
65 static int parseport(char *, long long *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
69 TAILQ_ENTRY(sym) entry;
76 int symset(const char *, const char *, int);
77 char *symget(const char *);
79 static int atoul(char *, u_long *);
81 static const struct got_error* gerror;
82 static struct gotconfig_remote_repo *remote;
83 static struct gotconfig gotconfig;
84 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
85 static const struct got_error* new_fetch_config(struct fetch_config **);
86 static const struct got_error* new_send_config(struct send_config **);
92 struct node_branch *branch;
98 #if defined(__APPLE__) && !defined(YYSTYPE)
99 #warning "Setting YYSTYPE - is GNU Bison installed?"
100 #define YYSTYPE YYSTYPE
105 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
106 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
107 %token <v.string> STRING
108 %token <v.number> NUMBER
109 %type <v.number> boolean portplain
110 %type <v.string> numberstring
111 %type <v.branch> branch xbranch branch_list
112 %type <v.ref> ref xref ref_list
116 grammar : /* empty */
118 | grammar author '\n'
119 | grammar remote '\n'
122 if (strcasecmp($1, "true") == 0 ||
123 strcasecmp($1, "yes") == 0)
125 else if (strcasecmp($1, "false") == 0 ||
126 strcasecmp($1, "no") == 0)
129 yyerror("invalid boolean value '%s'", $1);
136 numberstring : NUMBER {
138 if (asprintf(&s, "%lld", $1) == -1) {
139 yyerror("string: asprintf");
146 portplain : numberstring {
147 if (parseport($1, &$$) == -1) {
154 branch : /* empty */ { $$ = NULL; }
155 | xbranch { $$ = $1; }
156 | '{' optnl branch_list '}' { $$ = $3; }
159 $$ = calloc(1, sizeof(struct node_branch));
164 $$->branch_name = $1;
168 branch_list : xbranch optnl { $$ = $1; }
169 | branch_list comma xbranch optnl {
175 ref : /* empty */ { $$ = NULL; }
177 | '{' optnl ref_list '}' { $$ = $3; }
180 $$ = calloc(1, sizeof(struct node_ref));
189 ref_list : xref optnl { $$ = $1; }
190 | ref_list comma xref optnl {
196 remoteopts2 : remoteopts2 remoteopts1 nl
199 remoteopts1 : REPOSITORY STRING {
200 remote->repository = $2;
206 remote->protocol = $2;
208 | MIRROR_REFERENCES boolean {
209 remote->mirror_references = $2;
211 | FETCH_ALL_BRANCHES boolean {
212 remote->fetch_all_branches = $2;
221 remote->fetch_ref = $2;
224 static const struct got_error* error;
226 if (remote->fetch_config != NULL) {
227 yyerror("fetch block already exists");
230 error = new_fetch_config(&remote->fetch_config);
232 yyerror("%s", error->msg);
235 } '{' optnl fetchempty '}'
237 static const struct got_error* error;
239 if (remote->send_config != NULL) {
240 yyerror("send block already exists");
243 error = new_send_config(&remote->send_config);
245 yyerror("%s", error->msg);
248 } '{' optnl sendempty '}'
250 fetchempty : /* empty */
253 fetchopts2 : fetchopts2 fetchopts1 nl
256 fetchopts1 : REPOSITORY STRING {
257 remote->fetch_config->repository = $2;
260 remote->fetch_config->server = $2;
263 remote->fetch_config->protocol = $2;
266 remote->fetch_config->port = $2;
269 remote->fetch_config->branch = $2;
272 sendempty : /* empty */
275 sendopts2 : sendopts2 sendopts1 nl
278 sendopts1 : REPOSITORY STRING {
279 remote->send_config->repository = $2;
282 remote->send_config->server = $2;
285 remote->send_config->protocol = $2;
288 remote->send_config->port = $2;
291 remote->send_config->branch = $2;
294 remote : REMOTE STRING {
295 static const struct got_error* error;
297 error = new_remote(&remote);
300 yyerror("%s", error->msg);
304 } '{' optnl remoteopts2 '}' {
305 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
306 gotconfig.nremotes++;
309 author : AUTHOR STRING {
310 gotconfig.author = $2;
329 yyerror(const char *fmt, ...)
336 if (vasprintf(&msg, fmt, ap) == -1) {
337 gerror = got_error_from_errno("vasprintf");
341 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
343 gerror = got_error_from_errno("asprintf");
346 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
351 kw_cmp(const void *k, const void *e)
353 return (strcmp(k, ((const struct keywords *)e)->k_name));
359 /* This has to be sorted always. */
360 static const struct keywords keywords[] = {
364 {"fetch-all-branches", FETCH_ALL_BRANCHES},
365 {"mirror-references", MIRROR_REFERENCES},
367 {"protocol", PROTOCOL},
368 {"reference", REFERENCE},
370 {"repository", REPOSITORY},
374 const struct keywords *p;
376 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
377 sizeof(keywords[0]), kw_cmp);
385 #define START_EXPAND 1
386 #define DONE_EXPAND 2
388 static int expanding;
396 if (file->ungetpos > 0)
397 c = file->ungetbuf[--file->ungetpos];
399 c = getc(file->stream);
401 if (c == START_EXPAND)
403 else if (c == DONE_EXPAND)
419 yyerror("reached end of file while parsing "
432 yylval.lineno = file->lineno;
445 if (file->ungetpos >= file->ungetsize) {
446 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
448 err(1, "%s", __func__);
450 file->ungetsize *= 2;
452 file->ungetbuf[file->ungetpos++] = c;
460 /* Skip to either EOF or the first real EOL. */
479 if (atoul(n, &ulval) == 0) {
480 if (ulval == 0 || ulval > 65535) {
481 yyerror("illegal port value %lu", ulval);
486 s = getservbyname(n, "tcp");
488 s = getservbyname(n, "udp");
490 yyerror("unknown port %s", n);
498 parseport(char *port, long long *pn)
500 if ((*pn = getservice(port)) == -1) {
519 while (c == ' ' || c == '\t')
520 c = lgetc(0); /* nothing */
522 yylval.lineno = file->lineno;
525 while (c != '\n' && c != EOF)
526 c = lgetc(0); /* nothing */
528 if (c == '$' && !expanding) {
534 if (p + 1 >= buf + sizeof(buf) - 1) {
535 yyerror("string too long");
538 if (isalnum(c) || c == '_') {
548 yyerror("macro '%s' not defined", buf);
551 p = val + strlen(val) - 1;
552 lungetc(DONE_EXPAND);
554 lungetc((unsigned char)*p);
557 lungetc(START_EXPAND);
572 } else if (c == '\\') {
573 next = lgetc(quotec);
576 if (next == quotec || c == ' ' || c == '\t')
578 else if (next == '\n') {
583 } else if (c == quotec) {
586 } else if (c == '\0') {
587 yyerror("syntax error");
590 if (p + 1 >= buf + sizeof(buf) - 1) {
591 yyerror("string too long");
596 yylval.v.string = strdup(buf);
597 if (yylval.v.string == NULL)
598 err(1, "%s", __func__);
602 #define allowed_to_end_number(x) \
603 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
605 if (c == '-' || isdigit(c)) {
608 if ((size_t)(p-buf) >= sizeof(buf)) {
609 yyerror("string too long");
613 } while (c != EOF && isdigit(c));
615 if (p == buf + 1 && buf[0] == '-')
617 if (c == EOF || allowed_to_end_number(c)) {
618 const char *errstr = NULL;
621 yylval.v.number = strtonum(buf, LLONG_MIN,
624 yyerror("\"%s\" invalid number: %s",
632 lungetc((unsigned char)*--p);
633 c = (unsigned char)*--p;
639 #define allowed_in_string(x) \
640 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
641 x != '{' && x != '}' && \
642 x != '!' && x != '=' && x != '#' && \
645 if (isalnum(c) || c == ':' || c == '_') {
648 if ((size_t)(p-buf) >= sizeof(buf)) {
649 yyerror("string too long");
653 } while (c != EOF && (allowed_in_string(c)));
657 if (token == STRING) {
658 yylval.v.string = strdup(buf);
659 if (yylval.v.string == NULL)
660 err(1, "%s", __func__);
665 yylval.lineno = file->lineno;
673 static const struct got_error*
674 newfile(struct file **nfile, const char *filename, int *fd)
676 const struct got_error* error = NULL;
678 (*nfile) = calloc(1, sizeof(struct file));
679 if ((*nfile) == NULL)
680 return got_error_from_errno("calloc");
681 (*nfile)->stream = fdopen(*fd, "r");
682 if ((*nfile)->stream == NULL) {
683 error = got_error_from_errno("fdopen");
687 *fd = -1; /* Stream owns the file descriptor now. */
688 (*nfile)->name = filename;
689 (*nfile)->lineno = 1;
690 (*nfile)->ungetsize = 16;
691 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
692 if ((*nfile)->ungetbuf == NULL) {
693 error = got_error_from_errno("malloc");
694 fclose((*nfile)->stream);
701 static const struct got_error*
702 new_remote(struct gotconfig_remote_repo **remote)
704 const struct got_error *error = NULL;
706 *remote = calloc(1, sizeof(**remote));
708 error = got_error_from_errno("calloc");
712 static const struct got_error*
713 new_fetch_config(struct fetch_config **fetch_config)
715 const struct got_error *error = NULL;
717 *fetch_config = calloc(1, sizeof(**fetch_config));
718 if (*fetch_config == NULL)
719 error = got_error_from_errno("calloc");
723 static const struct got_error*
724 new_send_config(struct send_config **send_config)
726 const struct got_error *error = NULL;
728 *send_config = calloc(1, sizeof(**send_config));
729 if (*send_config == NULL)
730 error = got_error_from_errno("calloc");
735 closefile(struct file *file)
737 fclose(file->stream);
738 free(file->ungetbuf);
742 const struct got_error *
743 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
745 const struct got_error *err = NULL;
746 struct sym *sym, *next;
750 err = newfile(&file, filename, fd);
754 TAILQ_INIT(&gotconfig.remotes);
759 /* Free macros and check which have not been used. */
760 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
764 TAILQ_REMOVE(&symhead, sym, entry);
775 free_fetch_config(struct fetch_config *fetch_config)
777 free(remote->fetch_config->repository);
778 free(remote->fetch_config->server);
779 free(remote->fetch_config->protocol);
780 free(remote->fetch_config);
784 free_send_config(struct send_config *send_config)
786 free(remote->send_config->repository);
787 free(remote->send_config->server);
788 free(remote->send_config->protocol);
789 free(remote->send_config);
793 gotconfig_free(struct gotconfig *conf)
795 struct gotconfig_remote_repo *remote;
798 while (!TAILQ_EMPTY(&conf->remotes)) {
799 remote = TAILQ_FIRST(&conf->remotes);
800 TAILQ_REMOVE(&conf->remotes, remote, entry);
801 if (remote->fetch_config != NULL)
802 free_fetch_config(remote->fetch_config);
803 if (remote->send_config != NULL)
804 free_send_config(remote->send_config);
806 free(remote->repository);
807 free(remote->server);
808 free(remote->protocol);
814 symset(const char *nam, const char *val, int persist)
818 TAILQ_FOREACH(sym, &symhead, entry) {
819 if (strcmp(nam, sym->nam) == 0)
824 if (sym->persist == 1)
829 TAILQ_REMOVE(&symhead, sym, entry);
833 sym = calloc(1, sizeof(*sym));
837 sym->nam = strdup(nam);
838 if (sym->nam == NULL) {
842 sym->val = strdup(val);
843 if (sym->val == NULL) {
849 sym->persist = persist;
850 TAILQ_INSERT_TAIL(&symhead, sym, entry);
855 cmdline_symset(char *s)
861 val = strrchr(s, '=');
865 len = strlen(s) - strlen(val) + 1;
868 errx(1, "cmdline_symset: malloc");
870 strlcpy(sym, s, len);
872 ret = symset(sym, val + 1, 1);
879 symget(const char *nam)
883 TAILQ_FOREACH(sym, &symhead, entry) {
884 if (strcmp(nam, sym->nam) == 0) {
893 atoul(char *s, u_long *ulvalp)
899 ulval = strtoul(s, &ep, 0);
900 if (s[0] == '\0' || *ep != '\0')
902 if (errno == ERANGE && ulval == ULONG_MAX)