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>
26 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "gotconfig.h"
51 static const struct got_error* newfile(struct file**, const char *, int *);
52 static void closefile(struct file *);
55 int yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
64 static int parseport(char *, long long *);
66 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 TAILQ_ENTRY(sym) entry;
75 int symset(const char *, const char *, int);
76 char *symget(const char *);
78 static int atoul(char *, u_long *);
80 static const struct got_error* gerror;
81 static struct gotconfig_remote_repo *remote;
82 static struct gotconfig gotconfig;
83 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
84 static const struct got_error* new_fetch_config(struct fetch_config **);
85 static const struct got_error* new_send_config(struct send_config **);
91 struct node_branch *branch;
100 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
101 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
102 %token <v.string> STRING
103 %token <v.number> NUMBER
104 %type <v.number> boolean portplain
105 %type <v.string> numberstring
106 %type <v.branch> branch xbranch branch_list
107 %type <v.ref> ref xref ref_list
111 grammar : /* empty */
113 | grammar author '\n'
114 | grammar remote '\n'
117 if (strcasecmp($1, "true") == 0 ||
118 strcasecmp($1, "yes") == 0)
120 else if (strcasecmp($1, "false") == 0 ||
121 strcasecmp($1, "no") == 0)
124 yyerror("invalid boolean value '%s'", $1);
131 numberstring : NUMBER {
133 if (asprintf(&s, "%lld", $1) == -1) {
134 yyerror("string: asprintf");
141 portplain : numberstring {
142 if (parseport($1, &$$) == -1) {
149 branch : /* empty */ { $$ = NULL; }
150 | xbranch { $$ = $1; }
151 | '{' optnl branch_list '}' { $$ = $3; }
154 $$ = calloc(1, sizeof(struct node_branch));
159 $$->branch_name = $1;
163 branch_list : xbranch optnl { $$ = $1; }
164 | branch_list comma xbranch optnl {
170 ref : /* empty */ { $$ = NULL; }
172 | '{' optnl ref_list '}' { $$ = $3; }
175 $$ = calloc(1, sizeof(struct node_ref));
184 ref_list : xref optnl { $$ = $1; }
185 | ref_list comma xref optnl {
191 remoteopts2 : remoteopts2 remoteopts1 nl
194 remoteopts1 : REPOSITORY STRING {
195 remote->repository = $2;
201 remote->protocol = $2;
203 | MIRROR_REFERENCES boolean {
204 remote->mirror_references = $2;
206 | FETCH_ALL_BRANCHES boolean {
207 remote->fetch_all_branches = $2;
216 remote->fetch_ref = $2;
219 static const struct got_error* error;
221 if (remote->fetch_config != NULL) {
222 yyerror("fetch block already exists");
225 error = new_fetch_config(&remote->fetch_config);
227 yyerror("%s", error->msg);
230 } '{' optnl fetchempty '}'
232 static const struct got_error* error;
234 if (remote->send_config != NULL) {
235 yyerror("send block already exists");
238 error = new_send_config(&remote->send_config);
240 yyerror("%s", error->msg);
243 } '{' optnl sendempty '}'
245 fetchempty : /* empty */
248 fetchopts2 : fetchopts2 fetchopts1 nl
251 fetchopts1 : REPOSITORY STRING {
252 remote->fetch_config->repository = $2;
255 remote->fetch_config->server = $2;
258 remote->fetch_config->protocol = $2;
261 remote->fetch_config->port = $2;
264 remote->fetch_config->branch = $2;
267 sendempty : /* empty */
270 sendopts2 : sendopts2 sendopts1 nl
273 sendopts1 : REPOSITORY STRING {
274 remote->send_config->repository = $2;
277 remote->send_config->server = $2;
280 remote->send_config->protocol = $2;
283 remote->send_config->port = $2;
286 remote->send_config->branch = $2;
289 remote : REMOTE STRING {
290 static const struct got_error* error;
292 error = new_remote(&remote);
295 yyerror("%s", error->msg);
299 } '{' optnl remoteopts2 '}' {
300 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
301 gotconfig.nremotes++;
304 author : AUTHOR STRING {
305 gotconfig.author = $2;
324 yyerror(const char *fmt, ...)
331 if (vasprintf(&msg, fmt, ap) == -1) {
332 gerror = got_error_from_errno("vasprintf");
336 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
338 gerror = got_error_from_errno("asprintf");
341 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
346 kw_cmp(const void *k, const void *e)
348 return (strcmp(k, ((const struct keywords *)e)->k_name));
354 /* This has to be sorted always. */
355 static const struct keywords keywords[] = {
359 {"fetch-all-branches", FETCH_ALL_BRANCHES},
360 {"mirror-references", MIRROR_REFERENCES},
362 {"protocol", PROTOCOL},
363 {"reference", REFERENCE},
365 {"repository", REPOSITORY},
369 const struct keywords *p;
371 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
372 sizeof(keywords[0]), kw_cmp);
380 #define START_EXPAND 1
381 #define DONE_EXPAND 2
383 static int expanding;
391 if (file->ungetpos > 0)
392 c = file->ungetbuf[--file->ungetpos];
394 c = getc(file->stream);
396 if (c == START_EXPAND)
398 else if (c == DONE_EXPAND)
414 yyerror("reached end of file while parsing "
427 yylval.lineno = file->lineno;
440 if (file->ungetpos >= file->ungetsize) {
441 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
443 err(1, "%s", __func__);
445 file->ungetsize *= 2;
447 file->ungetbuf[file->ungetpos++] = c;
455 /* Skip to either EOF or the first real EOL. */
474 if (atoul(n, &ulval) == 0) {
475 if (ulval == 0 || ulval > 65535) {
476 yyerror("illegal port value %lu", ulval);
481 s = getservbyname(n, "tcp");
483 s = getservbyname(n, "udp");
485 yyerror("unknown port %s", n);
493 parseport(char *port, long long *pn)
495 if ((*pn = getservice(port)) == -1) {
514 while (c == ' ' || c == '\t')
515 c = lgetc(0); /* nothing */
517 yylval.lineno = file->lineno;
520 while (c != '\n' && c != EOF)
521 c = lgetc(0); /* nothing */
523 if (c == '$' && !expanding) {
529 if (p + 1 >= buf + sizeof(buf) - 1) {
530 yyerror("string too long");
533 if (isalnum(c) || c == '_') {
543 yyerror("macro '%s' not defined", buf);
546 p = val + strlen(val) - 1;
547 lungetc(DONE_EXPAND);
552 lungetc(START_EXPAND);
567 } else if (c == '\\') {
568 next = lgetc(quotec);
571 if (next == quotec || c == ' ' || c == '\t')
573 else if (next == '\n') {
578 } else if (c == quotec) {
581 } else if (c == '\0') {
582 yyerror("syntax error");
585 if (p + 1 >= buf + sizeof(buf) - 1) {
586 yyerror("string too long");
591 yylval.v.string = strdup(buf);
592 if (yylval.v.string == NULL)
593 err(1, "%s", __func__);
597 #define allowed_to_end_number(x) \
598 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
600 if (c == '-' || isdigit(c)) {
603 if ((size_t)(p-buf) >= sizeof(buf)) {
604 yyerror("string too long");
608 } while (c != EOF && isdigit(c));
610 if (p == buf + 1 && buf[0] == '-')
612 if (c == EOF || allowed_to_end_number(c)) {
613 const char *errstr = NULL;
616 yylval.v.number = strtonum(buf, LLONG_MIN,
619 yyerror("\"%s\" invalid number: %s",
634 #define allowed_in_string(x) \
635 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
636 x != '{' && x != '}' && \
637 x != '!' && x != '=' && x != '#' && \
640 if (isalnum(c) || c == ':' || c == '_') {
643 if ((size_t)(p-buf) >= sizeof(buf)) {
644 yyerror("string too long");
648 } while (c != EOF && (allowed_in_string(c)));
652 if (token == STRING) {
653 yylval.v.string = strdup(buf);
654 if (yylval.v.string == NULL)
655 err(1, "%s", __func__);
660 yylval.lineno = file->lineno;
668 static const struct got_error*
669 newfile(struct file **nfile, const char *filename, int *fd)
671 const struct got_error* error = NULL;
673 (*nfile) = calloc(1, sizeof(struct file));
674 if ((*nfile) == NULL)
675 return got_error_from_errno("calloc");
676 (*nfile)->stream = fdopen(*fd, "r");
677 if ((*nfile)->stream == NULL) {
678 error = got_error_from_errno("fdopen");
682 *fd = -1; /* Stream owns the file descriptor now. */
683 (*nfile)->name = filename;
684 (*nfile)->lineno = 1;
685 (*nfile)->ungetsize = 16;
686 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
687 if ((*nfile)->ungetbuf == NULL) {
688 error = got_error_from_errno("malloc");
689 fclose((*nfile)->stream);
696 static const struct got_error*
697 new_remote(struct gotconfig_remote_repo **remote)
699 const struct got_error *error = NULL;
701 *remote = calloc(1, sizeof(**remote));
703 error = got_error_from_errno("calloc");
707 static const struct got_error*
708 new_fetch_config(struct fetch_config **fetch_config)
710 const struct got_error *error = NULL;
712 *fetch_config = calloc(1, sizeof(**fetch_config));
713 if (*fetch_config == NULL)
714 error = got_error_from_errno("calloc");
718 static const struct got_error*
719 new_send_config(struct send_config **send_config)
721 const struct got_error *error = NULL;
723 *send_config = calloc(1, sizeof(**send_config));
724 if (*send_config == NULL)
725 error = got_error_from_errno("calloc");
730 closefile(struct file *file)
732 fclose(file->stream);
733 free(file->ungetbuf);
737 const struct got_error *
738 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
740 const struct got_error *err = NULL;
741 struct sym *sym, *next;
745 err = newfile(&file, filename, fd);
749 TAILQ_INIT(&gotconfig.remotes);
754 /* Free macros and check which have not been used. */
755 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
759 TAILQ_REMOVE(&symhead, sym, entry);
770 free_fetch_config(struct fetch_config *fetch_config)
772 free(remote->fetch_config->repository);
773 free(remote->fetch_config->server);
774 free(remote->fetch_config->protocol);
775 free(remote->fetch_config);
779 free_send_config(struct send_config *send_config)
781 free(remote->send_config->repository);
782 free(remote->send_config->server);
783 free(remote->send_config->protocol);
784 free(remote->send_config);
788 gotconfig_free(struct gotconfig *conf)
790 struct gotconfig_remote_repo *remote;
793 while (!TAILQ_EMPTY(&conf->remotes)) {
794 remote = TAILQ_FIRST(&conf->remotes);
795 TAILQ_REMOVE(&conf->remotes, remote, entry);
796 if (remote->fetch_config != NULL)
797 free_fetch_config(remote->fetch_config);
798 if (remote->send_config != NULL)
799 free_send_config(remote->send_config);
801 free(remote->repository);
802 free(remote->server);
803 free(remote->protocol);
809 symset(const char *nam, const char *val, int persist)
813 TAILQ_FOREACH(sym, &symhead, entry) {
814 if (strcmp(nam, sym->nam) == 0)
819 if (sym->persist == 1)
824 TAILQ_REMOVE(&symhead, sym, entry);
828 sym = calloc(1, sizeof(*sym));
832 sym->nam = strdup(nam);
833 if (sym->nam == NULL) {
837 sym->val = strdup(val);
838 if (sym->val == NULL) {
844 sym->persist = persist;
845 TAILQ_INSERT_TAIL(&symhead, sym, entry);
850 cmdline_symset(char *s)
856 val = strrchr(s, '=');
860 len = strlen(s) - strlen(val) + 1;
863 errx(1, "cmdline_symset: malloc");
865 strlcpy(sym, s, len);
867 ret = symset(sym, val + 1, 1);
874 symget(const char *nam)
878 TAILQ_FOREACH(sym, &symhead, entry) {
879 if (strcmp(nam, sym->nam) == 0) {
888 atoul(char *s, u_long *ulvalp)
894 ulval = strtoul(s, &ep, 0);
895 if (s[0] == '\0' || *ep != '\0')
897 if (errno == ERANGE && ulval == ULONG_MAX)