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;
101 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
102 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
103 %token <v.string> STRING
104 %token <v.number> NUMBER
105 %type <v.number> boolean portplain
106 %type <v.string> numberstring
107 %type <v.branch> branch xbranch branch_list
108 %type <v.ref> ref xref ref_list
112 grammar : /* empty */
114 | grammar author '\n'
115 | grammar remote '\n'
118 if (strcasecmp($1, "true") == 0 ||
119 strcasecmp($1, "yes") == 0)
121 else if (strcasecmp($1, "false") == 0 ||
122 strcasecmp($1, "no") == 0)
125 yyerror("invalid boolean value '%s'", $1);
132 numberstring : NUMBER {
134 if (asprintf(&s, "%lld", $1) == -1) {
135 yyerror("string: asprintf");
142 portplain : numberstring {
143 if (parseport($1, &$$) == -1) {
150 branch : /* empty */ { $$ = NULL; }
151 | xbranch { $$ = $1; }
152 | '{' optnl branch_list '}' { $$ = $3; }
155 $$ = calloc(1, sizeof(struct node_branch));
160 $$->branch_name = $1;
164 branch_list : xbranch optnl { $$ = $1; }
165 | branch_list comma xbranch optnl {
171 ref : /* empty */ { $$ = NULL; }
173 | '{' optnl ref_list '}' { $$ = $3; }
176 $$ = calloc(1, sizeof(struct node_ref));
185 ref_list : xref optnl { $$ = $1; }
186 | ref_list comma xref optnl {
192 remoteopts2 : remoteopts2 remoteopts1 nl
195 remoteopts1 : REPOSITORY STRING {
196 remote->repository = $2;
202 remote->protocol = $2;
204 | MIRROR_REFERENCES boolean {
205 remote->mirror_references = $2;
207 | FETCH_ALL_BRANCHES boolean {
208 remote->fetch_all_branches = $2;
217 remote->fetch_ref = $2;
220 static const struct got_error* error;
222 if (remote->fetch_config != NULL) {
223 yyerror("fetch block already exists");
226 error = new_fetch_config(&remote->fetch_config);
228 yyerror("%s", error->msg);
231 } '{' optnl fetchempty '}'
233 static const struct got_error* error;
235 if (remote->send_config != NULL) {
236 yyerror("send block already exists");
239 error = new_send_config(&remote->send_config);
241 yyerror("%s", error->msg);
244 } '{' optnl sendempty '}'
246 fetchempty : /* empty */
249 fetchopts2 : fetchopts2 fetchopts1 nl
252 fetchopts1 : REPOSITORY STRING {
253 remote->fetch_config->repository = $2;
256 remote->fetch_config->server = $2;
259 remote->fetch_config->protocol = $2;
262 remote->fetch_config->port = $2;
265 remote->fetch_config->branch = $2;
268 sendempty : /* empty */
271 sendopts2 : sendopts2 sendopts1 nl
274 sendopts1 : REPOSITORY STRING {
275 remote->send_config->repository = $2;
278 remote->send_config->server = $2;
281 remote->send_config->protocol = $2;
284 remote->send_config->port = $2;
287 remote->send_config->branch = $2;
290 remote : REMOTE STRING {
291 static const struct got_error* error;
293 error = new_remote(&remote);
296 yyerror("%s", error->msg);
300 } '{' optnl remoteopts2 '}' {
301 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
302 gotconfig.nremotes++;
305 author : AUTHOR STRING {
306 gotconfig.author = $2;
325 yyerror(const char *fmt, ...)
332 if (vasprintf(&msg, fmt, ap) == -1) {
333 gerror = got_error_from_errno("vasprintf");
337 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
339 gerror = got_error_from_errno("asprintf");
342 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
347 kw_cmp(const void *k, const void *e)
349 return (strcmp(k, ((const struct keywords *)e)->k_name));
355 /* This has to be sorted always. */
356 static const struct keywords keywords[] = {
360 {"fetch-all-branches", FETCH_ALL_BRANCHES},
361 {"mirror-references", MIRROR_REFERENCES},
363 {"protocol", PROTOCOL},
364 {"reference", REFERENCE},
366 {"repository", REPOSITORY},
370 const struct keywords *p;
372 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
373 sizeof(keywords[0]), kw_cmp);
381 #define START_EXPAND 1
382 #define DONE_EXPAND 2
384 static int expanding;
392 if (file->ungetpos > 0)
393 c = file->ungetbuf[--file->ungetpos];
395 c = getc(file->stream);
397 if (c == START_EXPAND)
399 else if (c == DONE_EXPAND)
415 yyerror("reached end of file while parsing "
428 yylval.lineno = file->lineno;
441 if (file->ungetpos >= file->ungetsize) {
442 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
444 err(1, "%s", __func__);
446 file->ungetsize *= 2;
448 file->ungetbuf[file->ungetpos++] = c;
456 /* Skip to either EOF or the first real EOL. */
475 if (atoul(n, &ulval) == 0) {
476 if (ulval == 0 || ulval > 65535) {
477 yyerror("illegal port value %lu", ulval);
482 s = getservbyname(n, "tcp");
484 s = getservbyname(n, "udp");
486 yyerror("unknown port %s", n);
494 parseport(char *port, long long *pn)
496 if ((*pn = getservice(port)) == -1) {
515 while (c == ' ' || c == '\t')
516 c = lgetc(0); /* nothing */
518 yylval.lineno = file->lineno;
521 while (c != '\n' && c != EOF)
522 c = lgetc(0); /* nothing */
524 if (c == '$' && !expanding) {
530 if (p + 1 >= buf + sizeof(buf) - 1) {
531 yyerror("string too long");
534 if (isalnum(c) || c == '_') {
544 yyerror("macro '%s' not defined", buf);
547 p = val + strlen(val) - 1;
548 lungetc(DONE_EXPAND);
550 lungetc((unsigned char)*p);
553 lungetc(START_EXPAND);
568 } else if (c == '\\') {
569 next = lgetc(quotec);
572 if (next == quotec || c == ' ' || c == '\t')
574 else if (next == '\n') {
579 } else if (c == quotec) {
582 } else if (c == '\0') {
583 yyerror("syntax error");
586 if (p + 1 >= buf + sizeof(buf) - 1) {
587 yyerror("string too long");
592 yylval.v.string = strdup(buf);
593 if (yylval.v.string == NULL)
594 err(1, "%s", __func__);
598 #define allowed_to_end_number(x) \
599 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
601 if (c == '-' || isdigit(c)) {
604 if ((size_t)(p-buf) >= sizeof(buf)) {
605 yyerror("string too long");
609 } while (c != EOF && isdigit(c));
611 if (p == buf + 1 && buf[0] == '-')
613 if (c == EOF || allowed_to_end_number(c)) {
614 const char *errstr = NULL;
617 yylval.v.number = strtonum(buf, LLONG_MIN,
620 yyerror("\"%s\" invalid number: %s",
628 lungetc((unsigned char)*--p);
629 c = (unsigned char)*--p;
635 #define allowed_in_string(x) \
636 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
637 x != '{' && x != '}' && \
638 x != '!' && x != '=' && x != '#' && \
641 if (isalnum(c) || c == ':' || c == '_') {
644 if ((size_t)(p-buf) >= sizeof(buf)) {
645 yyerror("string too long");
649 } while (c != EOF && (allowed_in_string(c)));
653 if (token == STRING) {
654 yylval.v.string = strdup(buf);
655 if (yylval.v.string == NULL)
656 err(1, "%s", __func__);
661 yylval.lineno = file->lineno;
669 static const struct got_error*
670 newfile(struct file **nfile, const char *filename, int *fd)
672 const struct got_error* error = NULL;
674 (*nfile) = calloc(1, sizeof(struct file));
675 if ((*nfile) == NULL)
676 return got_error_from_errno("calloc");
677 (*nfile)->stream = fdopen(*fd, "r");
678 if ((*nfile)->stream == NULL) {
679 error = got_error_from_errno("fdopen");
683 *fd = -1; /* Stream owns the file descriptor now. */
684 (*nfile)->name = filename;
685 (*nfile)->lineno = 1;
686 (*nfile)->ungetsize = 16;
687 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
688 if ((*nfile)->ungetbuf == NULL) {
689 error = got_error_from_errno("malloc");
690 fclose((*nfile)->stream);
697 static const struct got_error*
698 new_remote(struct gotconfig_remote_repo **remote)
700 const struct got_error *error = NULL;
702 *remote = calloc(1, sizeof(**remote));
704 error = got_error_from_errno("calloc");
708 static const struct got_error*
709 new_fetch_config(struct fetch_config **fetch_config)
711 const struct got_error *error = NULL;
713 *fetch_config = calloc(1, sizeof(**fetch_config));
714 if (*fetch_config == NULL)
715 error = got_error_from_errno("calloc");
719 static const struct got_error*
720 new_send_config(struct send_config **send_config)
722 const struct got_error *error = NULL;
724 *send_config = calloc(1, sizeof(**send_config));
725 if (*send_config == NULL)
726 error = got_error_from_errno("calloc");
731 closefile(struct file *file)
733 fclose(file->stream);
734 free(file->ungetbuf);
738 const struct got_error *
739 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
741 const struct got_error *err = NULL;
742 struct sym *sym, *next;
746 err = newfile(&file, filename, fd);
750 TAILQ_INIT(&gotconfig.remotes);
755 /* Free macros and check which have not been used. */
756 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
760 TAILQ_REMOVE(&symhead, sym, entry);
771 free_fetch_config(struct fetch_config *fetch_config)
773 free(remote->fetch_config->repository);
774 free(remote->fetch_config->server);
775 free(remote->fetch_config->protocol);
776 free(remote->fetch_config);
780 free_send_config(struct send_config *send_config)
782 free(remote->send_config->repository);
783 free(remote->send_config->server);
784 free(remote->send_config->protocol);
785 free(remote->send_config);
789 gotconfig_free(struct gotconfig *conf)
791 struct gotconfig_remote_repo *remote;
794 while (!TAILQ_EMPTY(&conf->remotes)) {
795 remote = TAILQ_FIRST(&conf->remotes);
796 TAILQ_REMOVE(&conf->remotes, remote, entry);
797 if (remote->fetch_config != NULL)
798 free_fetch_config(remote->fetch_config);
799 if (remote->send_config != NULL)
800 free_send_config(remote->send_config);
802 free(remote->repository);
803 free(remote->server);
804 free(remote->protocol);
810 symset(const char *nam, const char *val, int persist)
814 TAILQ_FOREACH(sym, &symhead, entry) {
815 if (strcmp(nam, sym->nam) == 0)
820 if (sym->persist == 1)
825 TAILQ_REMOVE(&symhead, sym, entry);
829 sym = calloc(1, sizeof(*sym));
833 sym->nam = strdup(nam);
834 if (sym->nam == NULL) {
838 sym->val = strdup(val);
839 if (sym->val == NULL) {
845 sym->persist = persist;
846 TAILQ_INSERT_TAIL(&symhead, sym, entry);
851 cmdline_symset(char *s)
857 val = strrchr(s, '=');
861 len = strlen(s) - strlen(val) + 1;
864 errx(1, "cmdline_symset: malloc");
866 strlcpy(sym, s, len);
868 ret = symset(sym, val + 1, 1);
875 symget(const char *nam)
879 TAILQ_FOREACH(sym, &symhead, entry) {
880 if (strcmp(nam, sym->nam) == 0) {
889 atoul(char *s, u_long *ulvalp)
895 ulval = strtoul(s, &ep, 0);
896 if (s[0] == '\0' || *ep != '\0')
898 if (errno == ERANGE && ulval == ULONG_MAX)