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(struct fetch_repo **);
85 static const struct got_error* new_send(struct send_repo **);
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 = strdup($2);
196 if (remote->repository == NULL) {
204 remote->server = strdup($2);
205 if (remote->server == NULL) {
213 remote->protocol = strdup($2);
214 if (remote->protocol == NULL) {
221 | MIRROR_REFERENCES boolean {
222 remote->mirror_references = $2;
224 | FETCH_ALL_BRANCHES boolean {
225 remote->fetch_all_branches = $2;
239 fetchopts2 : fetchopts2 fetchopts1 nl
242 fetchopts1 : REPOSITORY STRING {
243 remote->fetch_repo->fetch_repository = strdup($2);
244 if (remote->fetch_repo->fetch_repository == NULL) {
252 remote->fetch_repo->fetch_server = strdup($2);
253 if (remote->fetch_repo->fetch_server == NULL) {
261 remote->fetch_repo->fetch_protocol = strdup($2);
262 if (remote->fetch_repo->fetch_protocol == NULL) {
270 remote->fetch_repo->fetch_port = $2;
273 remote->fetch_repo->fetch_branch = $2;
277 static const struct got_error* error;
279 if (remote->fetch_repo != NULL) {
280 yyerror("fetch block already exists");
283 error = new_fetch(&remote->fetch_repo);
285 yyerror("%s", error->msg);
288 } '{' optnl fetchopts2 '}'
290 sendopts2 : sendopts2 sendopts1 nl
293 sendopts1 : REPOSITORY STRING {
294 remote->send_repo->send_repository = strdup($2);
295 if (remote->send_repo->send_repository == NULL) {
303 remote->send_repo->send_server = strdup($2);
304 if (remote->send_repo->send_server == NULL) {
312 remote->send_repo->send_protocol = strdup($2);
313 if (remote->send_repo->send_protocol == NULL) {
321 remote->send_repo->send_port = $2;
324 remote->send_repo->send_branch = $2;
328 static const struct got_error* error;
330 if (remote->send_repo != NULL) {
331 yyerror("send block already exists");
334 error = new_send(&remote->send_repo);
336 yyerror("%s", error->msg);
339 } '{' optnl sendopts2 '}'
341 remote : REMOTE STRING {
342 static const struct got_error* error;
344 error = new_remote(&remote);
347 yyerror("%s", error->msg);
350 remote->name = strdup($2);
351 if (remote->name == NULL) {
357 } '{' optnl remoteopts2 '}' {
358 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
359 gotconfig.nremotes++;
362 author : AUTHOR STRING {
363 gotconfig.author = strdup($2);
364 if (gotconfig.author == NULL) {
388 yyerror(const char *fmt, ...)
395 if (vasprintf(&msg, fmt, ap) == -1) {
396 gerror = got_error_from_errno("vasprintf");
400 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
402 gerror = got_error_from_errno("asprintf");
405 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
411 kw_cmp(const void *k, const void *e)
413 return (strcmp(k, ((const struct keywords *)e)->k_name));
419 /* This has to be sorted always. */
420 static const struct keywords keywords[] = {
424 {"fetch-all-branches", FETCH_ALL_BRANCHES},
425 {"mirror-references", MIRROR_REFERENCES},
427 {"protocol", PROTOCOL},
428 {"reference", REFERENCE},
430 {"repository", REPOSITORY},
434 const struct keywords *p;
436 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
437 sizeof(keywords[0]), kw_cmp);
445 #define START_EXPAND 1
446 #define DONE_EXPAND 2
448 static int expanding;
456 if (file->ungetpos > 0)
457 c = file->ungetbuf[--file->ungetpos];
459 c = getc(file->stream);
461 if (c == START_EXPAND)
463 else if (c == DONE_EXPAND)
479 yyerror("reached end of file while parsing "
492 yylval.lineno = file->lineno;
505 if (file->ungetpos >= file->ungetsize) {
506 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
508 err(1, "%s", __func__);
510 file->ungetsize *= 2;
512 file->ungetbuf[file->ungetpos++] = c;
520 /* Skip to either EOF or the first real EOL. */
539 if (atoul(n, &ulval) == 0) {
541 yyerror("illegal port value %lu", ulval);
546 s = getservbyname(n, "tcp");
548 s = getservbyname(n, "udp");
550 yyerror("unknown port %s", n);
558 parseport(char *port, long long *pn)
560 if ((*pn = getservice(port)) == -1) {
571 unsigned char buf[8096];
572 unsigned char *p, *val;
579 while (c == ' ' || c == '\t')
580 c = lgetc(0); /* nothing */
582 yylval.lineno = file->lineno;
585 while (c != '\n' && c != EOF)
586 c = lgetc(0); /* nothing */
588 if (c == '$' && !expanding) {
594 if (p + 1 >= buf + sizeof(buf) - 1) {
595 yyerror("string too long");
598 if (isalnum(c) || c == '_') {
608 yyerror("macro '%s' not defined", buf);
611 p = val + strlen(val) - 1;
612 lungetc(DONE_EXPAND);
617 lungetc(START_EXPAND);
632 } else if (c == '\\') {
633 next = lgetc(quotec);
636 if (next == quotec || c == ' ' || c == '\t')
638 else if (next == '\n') {
643 } else if (c == quotec) {
646 } else if (c == '\0') {
647 yyerror("syntax error");
650 if (p + 1 >= buf + sizeof(buf) - 1) {
651 yyerror("string too long");
656 yylval.v.string = strdup(buf);
657 if (yylval.v.string == NULL)
658 err(1, "%s", __func__);
662 #define allowed_to_end_number(x) \
663 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
665 if (c == '-' || isdigit(c)) {
668 if ((unsigned)(p-buf) >= sizeof(buf)) {
669 yyerror("string too long");
673 } while (c != EOF && isdigit(c));
675 if (p == buf + 1 && buf[0] == '-')
677 if (c == EOF || allowed_to_end_number(c)) {
678 const char *errstr = NULL;
681 yylval.v.number = strtonum(buf, LLONG_MIN,
684 yyerror("\"%s\" invalid number: %s",
699 #define allowed_in_string(x) \
700 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
701 x != '{' && x != '}' && \
702 x != '!' && x != '=' && x != '#' && \
705 if (isalnum(c) || c == ':' || c == '_') {
708 if ((unsigned)(p-buf) >= sizeof(buf)) {
709 yyerror("string too long");
713 } while (c != EOF && (allowed_in_string(c)));
717 if (token == STRING) {
718 yylval.v.string = strdup(buf);
719 if (yylval.v.string == NULL)
720 err(1, "%s", __func__);
725 yylval.lineno = file->lineno;
733 static const struct got_error*
734 newfile(struct file **nfile, const char *filename, int *fd)
736 const struct got_error* error = NULL;
738 (*nfile) = calloc(1, sizeof(struct file));
739 if ((*nfile) == NULL)
740 return got_error_from_errno("calloc");
741 (*nfile)->stream = fdopen(*fd, "r");
742 if ((*nfile)->stream == NULL) {
743 error = got_error_from_errno("fdopen");
747 *fd = -1; /* Stream owns the file descriptor now. */
748 (*nfile)->name = filename;
749 (*nfile)->lineno = 1;
750 (*nfile)->ungetsize = 16;
751 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
752 if ((*nfile)->ungetbuf == NULL) {
753 error = got_error_from_errno("malloc");
754 fclose((*nfile)->stream);
761 static const struct got_error*
762 new_remote(struct gotconfig_remote_repo **remote)
764 const struct got_error *error = NULL;
766 *remote = calloc(1, sizeof(**remote));
768 error = got_error_from_errno("calloc");
772 static const struct got_error*
773 new_fetch(struct fetch_repo **fetch_repo)
775 const struct got_error *error = NULL;
777 *fetch_repo = calloc(1, sizeof(**fetch_repo));
778 if (*fetch_repo == NULL)
779 error = got_error_from_errno("calloc");
783 static const struct got_error*
784 new_send(struct send_repo **send_repo)
786 const struct got_error *error = NULL;
788 *send_repo = calloc(1, sizeof(**send_repo));
789 if (*send_repo == NULL)
790 error = got_error_from_errno("calloc");
795 closefile(struct file *file)
797 fclose(file->stream);
798 free(file->ungetbuf);
802 const struct got_error *
803 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
805 const struct got_error *err = NULL;
806 struct sym *sym, *next;
810 err = newfile(&file, filename, fd);
814 TAILQ_INIT(&gotconfig.remotes);
819 /* Free macros and check which have not been used. */
820 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
824 TAILQ_REMOVE(&symhead, sym, entry);
835 gotconfig_free(struct gotconfig *conf)
837 struct gotconfig_remote_repo *remote;
840 while (!TAILQ_EMPTY(&conf->remotes)) {
841 remote = TAILQ_FIRST(&conf->remotes);
842 TAILQ_REMOVE(&conf->remotes, remote, entry);
843 if (remote->fetch_repo != NULL) {
844 free(remote->fetch_repo->fetch_name);
845 free(remote->fetch_repo->fetch_repository);
846 free(remote->fetch_repo->fetch_server);
847 free(remote->fetch_repo->fetch_protocol);
849 if (remote->send_repo != NULL) {
850 free(remote->send_repo->send_name);
851 free(remote->send_repo->send_repository);
852 free(remote->send_repo->send_server);
853 free(remote->send_repo->send_protocol);
856 free(remote->repository);
857 free(remote->server);
858 free(remote->protocol);
864 symset(const char *nam, const char *val, int persist)
868 TAILQ_FOREACH(sym, &symhead, entry) {
869 if (strcmp(nam, sym->nam) == 0)
874 if (sym->persist == 1)
879 TAILQ_REMOVE(&symhead, sym, entry);
883 sym = calloc(1, sizeof(*sym));
887 sym->nam = strdup(nam);
888 if (sym->nam == NULL) {
892 sym->val = strdup(val);
893 if (sym->val == NULL) {
899 sym->persist = persist;
900 TAILQ_INSERT_TAIL(&symhead, sym, entry);
905 cmdline_symset(char *s)
911 val = strrchr(s, '=');
915 len = strlen(s) - strlen(val) + 1;
918 errx(1, "cmdline_symset: malloc");
920 strlcpy(sym, s, len);
922 ret = symset(sym, val + 1, 1);
929 symget(const char *nam)
933 TAILQ_FOREACH(sym, &symhead, entry) {
934 if (strcmp(nam, sym->nam) == 0) {
943 atoul(char *s, u_long *ulvalp)
949 ulval = strtoul(s, &ep, 0);
950 if (s[0] == '\0' || *ep != '\0')
952 if (errno == ERANGE && ulval == ULONG_MAX)