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;
237 static const struct got_error* error;
239 if (remote->fetch_repo != NULL) {
240 yyerror("fetch block already exists");
243 error = new_fetch(&remote->fetch_repo);
245 yyerror("%s", error->msg);
248 } '{' optnl fetchopts2 '}'
250 static const struct got_error* error;
252 if (remote->send_repo != NULL) {
253 yyerror("send block already exists");
256 error = new_send(&remote->send_repo);
258 yyerror("%s", error->msg);
261 } '{' optnl sendopts2 '}'
263 fetchopts2 : fetchopts2 fetchopts1 nl
266 fetchopts1 : /* empty */
267 | REPOSITORY STRING {
268 remote->fetch_repo->fetch_repository = strdup($2);
269 if (remote->fetch_repo->fetch_repository == NULL) {
277 remote->fetch_repo->fetch_server = strdup($2);
278 if (remote->fetch_repo->fetch_server == NULL) {
286 remote->fetch_repo->fetch_protocol = strdup($2);
287 if (remote->fetch_repo->fetch_protocol == NULL) {
295 remote->fetch_repo->fetch_port = $2;
298 remote->fetch_repo->fetch_branch = $2;
301 sendopts2 : sendopts2 sendopts1 nl
304 sendopts1 : /* empty */
305 | REPOSITORY STRING {
306 remote->send_repo->send_repository = strdup($2);
307 if (remote->send_repo->send_repository == NULL) {
315 remote->send_repo->send_server = strdup($2);
316 if (remote->send_repo->send_server == NULL) {
324 remote->send_repo->send_protocol = strdup($2);
325 if (remote->send_repo->send_protocol == NULL) {
333 remote->send_repo->send_port = $2;
336 remote->send_repo->send_branch = $2;
339 remote : REMOTE STRING {
340 static const struct got_error* error;
342 error = new_remote(&remote);
345 yyerror("%s", error->msg);
348 remote->name = strdup($2);
349 if (remote->name == NULL) {
355 } '{' optnl remoteopts2 '}' {
356 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
357 gotconfig.nremotes++;
360 author : AUTHOR STRING {
361 gotconfig.author = strdup($2);
362 if (gotconfig.author == NULL) {
386 yyerror(const char *fmt, ...)
393 if (vasprintf(&msg, fmt, ap) == -1) {
394 gerror = got_error_from_errno("vasprintf");
398 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
400 gerror = got_error_from_errno("asprintf");
403 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
409 kw_cmp(const void *k, const void *e)
411 return (strcmp(k, ((const struct keywords *)e)->k_name));
417 /* This has to be sorted always. */
418 static const struct keywords keywords[] = {
422 {"fetch-all-branches", FETCH_ALL_BRANCHES},
423 {"mirror-references", MIRROR_REFERENCES},
425 {"protocol", PROTOCOL},
426 {"reference", REFERENCE},
428 {"repository", REPOSITORY},
432 const struct keywords *p;
434 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
435 sizeof(keywords[0]), kw_cmp);
443 #define START_EXPAND 1
444 #define DONE_EXPAND 2
446 static int expanding;
454 if (file->ungetpos > 0)
455 c = file->ungetbuf[--file->ungetpos];
457 c = getc(file->stream);
459 if (c == START_EXPAND)
461 else if (c == DONE_EXPAND)
477 yyerror("reached end of file while parsing "
490 yylval.lineno = file->lineno;
503 if (file->ungetpos >= file->ungetsize) {
504 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
506 err(1, "%s", __func__);
508 file->ungetsize *= 2;
510 file->ungetbuf[file->ungetpos++] = c;
518 /* Skip to either EOF or the first real EOL. */
537 if (atoul(n, &ulval) == 0) {
539 yyerror("illegal port value %lu", ulval);
544 s = getservbyname(n, "tcp");
546 s = getservbyname(n, "udp");
548 yyerror("unknown port %s", n);
556 parseport(char *port, long long *pn)
558 if ((*pn = getservice(port)) == -1) {
569 unsigned char buf[8096];
570 unsigned char *p, *val;
577 while (c == ' ' || c == '\t')
578 c = lgetc(0); /* nothing */
580 yylval.lineno = file->lineno;
583 while (c != '\n' && c != EOF)
584 c = lgetc(0); /* nothing */
586 if (c == '$' && !expanding) {
592 if (p + 1 >= buf + sizeof(buf) - 1) {
593 yyerror("string too long");
596 if (isalnum(c) || c == '_') {
606 yyerror("macro '%s' not defined", buf);
609 p = val + strlen(val) - 1;
610 lungetc(DONE_EXPAND);
615 lungetc(START_EXPAND);
630 } else if (c == '\\') {
631 next = lgetc(quotec);
634 if (next == quotec || c == ' ' || c == '\t')
636 else if (next == '\n') {
641 } else if (c == quotec) {
644 } else if (c == '\0') {
645 yyerror("syntax error");
648 if (p + 1 >= buf + sizeof(buf) - 1) {
649 yyerror("string too long");
654 yylval.v.string = strdup(buf);
655 if (yylval.v.string == NULL)
656 err(1, "%s", __func__);
660 #define allowed_to_end_number(x) \
661 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
663 if (c == '-' || isdigit(c)) {
666 if ((unsigned)(p-buf) >= sizeof(buf)) {
667 yyerror("string too long");
671 } while (c != EOF && isdigit(c));
673 if (p == buf + 1 && buf[0] == '-')
675 if (c == EOF || allowed_to_end_number(c)) {
676 const char *errstr = NULL;
679 yylval.v.number = strtonum(buf, LLONG_MIN,
682 yyerror("\"%s\" invalid number: %s",
697 #define allowed_in_string(x) \
698 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
699 x != '{' && x != '}' && \
700 x != '!' && x != '=' && x != '#' && \
703 if (isalnum(c) || c == ':' || c == '_') {
706 if ((unsigned)(p-buf) >= sizeof(buf)) {
707 yyerror("string too long");
711 } while (c != EOF && (allowed_in_string(c)));
715 if (token == STRING) {
716 yylval.v.string = strdup(buf);
717 if (yylval.v.string == NULL)
718 err(1, "%s", __func__);
723 yylval.lineno = file->lineno;
731 static const struct got_error*
732 newfile(struct file **nfile, const char *filename, int *fd)
734 const struct got_error* error = NULL;
736 (*nfile) = calloc(1, sizeof(struct file));
737 if ((*nfile) == NULL)
738 return got_error_from_errno("calloc");
739 (*nfile)->stream = fdopen(*fd, "r");
740 if ((*nfile)->stream == NULL) {
741 error = got_error_from_errno("fdopen");
745 *fd = -1; /* Stream owns the file descriptor now. */
746 (*nfile)->name = filename;
747 (*nfile)->lineno = 1;
748 (*nfile)->ungetsize = 16;
749 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
750 if ((*nfile)->ungetbuf == NULL) {
751 error = got_error_from_errno("malloc");
752 fclose((*nfile)->stream);
759 static const struct got_error*
760 new_remote(struct gotconfig_remote_repo **remote)
762 const struct got_error *error = NULL;
764 *remote = calloc(1, sizeof(**remote));
766 error = got_error_from_errno("calloc");
770 static const struct got_error*
771 new_fetch(struct fetch_repo **fetch_repo)
773 const struct got_error *error = NULL;
775 *fetch_repo = calloc(1, sizeof(**fetch_repo));
776 if (*fetch_repo == NULL)
777 error = got_error_from_errno("calloc");
781 static const struct got_error*
782 new_send(struct send_repo **send_repo)
784 const struct got_error *error = NULL;
786 *send_repo = calloc(1, sizeof(**send_repo));
787 if (*send_repo == NULL)
788 error = got_error_from_errno("calloc");
793 closefile(struct file *file)
795 fclose(file->stream);
796 free(file->ungetbuf);
800 const struct got_error *
801 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
803 const struct got_error *err = NULL;
804 struct sym *sym, *next;
808 err = newfile(&file, filename, fd);
812 TAILQ_INIT(&gotconfig.remotes);
817 /* Free macros and check which have not been used. */
818 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
822 TAILQ_REMOVE(&symhead, sym, entry);
833 gotconfig_free(struct gotconfig *conf)
835 struct gotconfig_remote_repo *remote;
838 while (!TAILQ_EMPTY(&conf->remotes)) {
839 remote = TAILQ_FIRST(&conf->remotes);
840 TAILQ_REMOVE(&conf->remotes, remote, entry);
841 if (remote->fetch_repo != NULL) {
842 free(remote->fetch_repo->fetch_repository);
843 free(remote->fetch_repo->fetch_server);
844 free(remote->fetch_repo->fetch_protocol);
846 if (remote->send_repo != NULL) {
847 free(remote->send_repo->send_repository);
848 free(remote->send_repo->send_server);
849 free(remote->send_repo->send_protocol);
852 free(remote->repository);
853 free(remote->server);
854 free(remote->protocol);
860 symset(const char *nam, const char *val, int persist)
864 TAILQ_FOREACH(sym, &symhead, entry) {
865 if (strcmp(nam, sym->nam) == 0)
870 if (sym->persist == 1)
875 TAILQ_REMOVE(&symhead, sym, entry);
879 sym = calloc(1, sizeof(*sym));
883 sym->nam = strdup(nam);
884 if (sym->nam == NULL) {
888 sym->val = strdup(val);
889 if (sym->val == NULL) {
895 sym->persist = persist;
896 TAILQ_INSERT_TAIL(&symhead, sym, entry);
901 cmdline_symset(char *s)
907 val = strrchr(s, '=');
911 len = strlen(s) - strlen(val) + 1;
914 errx(1, "cmdline_symset: malloc");
916 strlcpy(sym, s, len);
918 ret = symset(sym, val + 1, 1);
925 symget(const char *nam)
929 TAILQ_FOREACH(sym, &symhead, entry) {
930 if (strcmp(nam, sym->nam) == 0) {
939 atoul(char *s, u_long *ulvalp)
945 ulval = strtoul(s, &ep, 0);
946 if (s[0] == '\0' || *ep != '\0')
948 if (errno == ERANGE && ulval == ULONG_MAX)