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 = 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_config != NULL) {
240 yyerror("fetch block already exists");
243 error = new_fetch_config(&remote->fetch_config);
245 yyerror("%s", error->msg);
248 } '{' optnl fetchopts2 '}'
250 static const struct got_error* error;
252 if (remote->send_config != NULL) {
253 yyerror("send block already exists");
256 error = new_send_config(&remote->send_config);
258 yyerror("%s", error->msg);
261 } '{' optnl sendopts2 '}'
263 fetchopts2 : fetchopts2 fetchopts1 nl
266 fetchopts1 : REPOSITORY STRING {
267 remote->fetch_config->repository = strdup($2);
268 if (remote->fetch_config->repository == NULL) {
276 remote->fetch_config->server = strdup($2);
277 if (remote->fetch_config->server == NULL) {
285 remote->fetch_config->protocol = strdup($2);
286 if (remote->fetch_config->protocol == NULL) {
294 remote->fetch_config->port = $2;
297 remote->fetch_config->branch = $2;
300 sendopts2 : sendopts2 sendopts1 nl
303 sendopts1 : REPOSITORY STRING {
304 remote->send_config->repository = strdup($2);
305 if (remote->send_config->repository == NULL) {
313 remote->send_config->server = strdup($2);
314 if (remote->send_config->server == NULL) {
322 remote->send_config->protocol = strdup($2);
323 if (remote->send_config->protocol == NULL) {
331 remote->send_config->port = $2;
334 remote->send_config->branch = $2;
337 remote : REMOTE STRING {
338 static const struct got_error* error;
340 error = new_remote(&remote);
343 yyerror("%s", error->msg);
346 remote->name = strdup($2);
347 if (remote->name == NULL) {
353 } '{' optnl remoteopts2 '}' {
354 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
355 gotconfig.nremotes++;
358 author : AUTHOR STRING {
359 gotconfig.author = strdup($2);
360 if (gotconfig.author == NULL) {
384 yyerror(const char *fmt, ...)
391 if (vasprintf(&msg, fmt, ap) == -1) {
392 gerror = got_error_from_errno("vasprintf");
396 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
398 gerror = got_error_from_errno("asprintf");
401 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
407 kw_cmp(const void *k, const void *e)
409 return (strcmp(k, ((const struct keywords *)e)->k_name));
415 /* This has to be sorted always. */
416 static const struct keywords keywords[] = {
420 {"fetch-all-branches", FETCH_ALL_BRANCHES},
421 {"mirror-references", MIRROR_REFERENCES},
423 {"protocol", PROTOCOL},
424 {"reference", REFERENCE},
426 {"repository", REPOSITORY},
430 const struct keywords *p;
432 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
433 sizeof(keywords[0]), kw_cmp);
441 #define START_EXPAND 1
442 #define DONE_EXPAND 2
444 static int expanding;
452 if (file->ungetpos > 0)
453 c = file->ungetbuf[--file->ungetpos];
455 c = getc(file->stream);
457 if (c == START_EXPAND)
459 else if (c == DONE_EXPAND)
475 yyerror("reached end of file while parsing "
488 yylval.lineno = file->lineno;
501 if (file->ungetpos >= file->ungetsize) {
502 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
504 err(1, "%s", __func__);
506 file->ungetsize *= 2;
508 file->ungetbuf[file->ungetpos++] = c;
516 /* Skip to either EOF or the first real EOL. */
535 if (atoul(n, &ulval) == 0) {
536 if (ulval == 0 || ulval > 65535) {
537 yyerror("illegal port value %lu", ulval);
542 s = getservbyname(n, "tcp");
544 s = getservbyname(n, "udp");
546 yyerror("unknown port %s", n);
554 parseport(char *port, long long *pn)
556 if ((*pn = getservice(port)) == -1) {
567 unsigned char buf[8096];
568 unsigned char *p, *val;
575 while (c == ' ' || c == '\t')
576 c = lgetc(0); /* nothing */
578 yylval.lineno = file->lineno;
581 while (c != '\n' && c != EOF)
582 c = lgetc(0); /* nothing */
584 if (c == '$' && !expanding) {
590 if (p + 1 >= buf + sizeof(buf) - 1) {
591 yyerror("string too long");
594 if (isalnum(c) || c == '_') {
604 yyerror("macro '%s' not defined", buf);
607 p = val + strlen(val) - 1;
608 lungetc(DONE_EXPAND);
613 lungetc(START_EXPAND);
628 } else if (c == '\\') {
629 next = lgetc(quotec);
632 if (next == quotec || c == ' ' || c == '\t')
634 else if (next == '\n') {
639 } else if (c == quotec) {
642 } else if (c == '\0') {
643 yyerror("syntax error");
646 if (p + 1 >= buf + sizeof(buf) - 1) {
647 yyerror("string too long");
652 yylval.v.string = strdup(buf);
653 if (yylval.v.string == NULL)
654 err(1, "%s", __func__);
658 #define allowed_to_end_number(x) \
659 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
661 if (c == '-' || isdigit(c)) {
664 if ((unsigned)(p-buf) >= sizeof(buf)) {
665 yyerror("string too long");
669 } while (c != EOF && isdigit(c));
671 if (p == buf + 1 && buf[0] == '-')
673 if (c == EOF || allowed_to_end_number(c)) {
674 const char *errstr = NULL;
677 yylval.v.number = strtonum(buf, LLONG_MIN,
680 yyerror("\"%s\" invalid number: %s",
695 #define allowed_in_string(x) \
696 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
697 x != '{' && x != '}' && \
698 x != '!' && x != '=' && x != '#' && \
701 if (isalnum(c) || c == ':' || c == '_') {
704 if ((unsigned)(p-buf) >= sizeof(buf)) {
705 yyerror("string too long");
709 } while (c != EOF && (allowed_in_string(c)));
713 if (token == STRING) {
714 yylval.v.string = strdup(buf);
715 if (yylval.v.string == NULL)
716 err(1, "%s", __func__);
721 yylval.lineno = file->lineno;
729 static const struct got_error*
730 newfile(struct file **nfile, const char *filename, int *fd)
732 const struct got_error* error = NULL;
734 (*nfile) = calloc(1, sizeof(struct file));
735 if ((*nfile) == NULL)
736 return got_error_from_errno("calloc");
737 (*nfile)->stream = fdopen(*fd, "r");
738 if ((*nfile)->stream == NULL) {
739 error = got_error_from_errno("fdopen");
743 *fd = -1; /* Stream owns the file descriptor now. */
744 (*nfile)->name = filename;
745 (*nfile)->lineno = 1;
746 (*nfile)->ungetsize = 16;
747 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
748 if ((*nfile)->ungetbuf == NULL) {
749 error = got_error_from_errno("malloc");
750 fclose((*nfile)->stream);
757 static const struct got_error*
758 new_remote(struct gotconfig_remote_repo **remote)
760 const struct got_error *error = NULL;
762 *remote = calloc(1, sizeof(**remote));
764 error = got_error_from_errno("calloc");
768 static const struct got_error*
769 new_fetch_config(struct fetch_config **fetch_config)
771 const struct got_error *error = NULL;
773 *fetch_config = calloc(1, sizeof(**fetch_config));
774 if (*fetch_config == NULL)
775 error = got_error_from_errno("calloc");
779 static const struct got_error*
780 new_send_config(struct send_config **send_config)
782 const struct got_error *error = NULL;
784 *send_config = calloc(1, sizeof(**send_config));
785 if (*send_config == NULL)
786 error = got_error_from_errno("calloc");
791 closefile(struct file *file)
793 fclose(file->stream);
794 free(file->ungetbuf);
798 const struct got_error *
799 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
801 const struct got_error *err = NULL;
802 struct sym *sym, *next;
806 err = newfile(&file, filename, fd);
810 TAILQ_INIT(&gotconfig.remotes);
815 /* Free macros and check which have not been used. */
816 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
820 TAILQ_REMOVE(&symhead, sym, entry);
831 free_fetch_config(struct fetch_config *fetch_config)
833 free(remote->fetch_config->repository);
834 free(remote->fetch_config->server);
835 free(remote->fetch_config->protocol);
836 free(remote->fetch_config);
840 free_send_config(struct send_config *send_config)
842 free(remote->send_config->repository);
843 free(remote->send_config->server);
844 free(remote->send_config->protocol);
845 free(remote->send_config);
849 gotconfig_free(struct gotconfig *conf)
851 struct gotconfig_remote_repo *remote;
854 while (!TAILQ_EMPTY(&conf->remotes)) {
855 remote = TAILQ_FIRST(&conf->remotes);
856 TAILQ_REMOVE(&conf->remotes, remote, entry);
857 if (remote->fetch_config != NULL)
858 free_fetch_config(remote->fetch_config);
859 if (remote->send_config != NULL)
860 free_send_config(remote->send_config);
862 free(remote->repository);
863 free(remote->server);
864 free(remote->protocol);
870 symset(const char *nam, const char *val, int persist)
874 TAILQ_FOREACH(sym, &symhead, entry) {
875 if (strcmp(nam, sym->nam) == 0)
880 if (sym->persist == 1)
885 TAILQ_REMOVE(&symhead, sym, entry);
889 sym = calloc(1, sizeof(*sym));
893 sym->nam = strdup(nam);
894 if (sym->nam == NULL) {
898 sym->val = strdup(val);
899 if (sym->val == NULL) {
905 sym->persist = persist;
906 TAILQ_INSERT_TAIL(&symhead, sym, entry);
911 cmdline_symset(char *s)
917 val = strrchr(s, '=');
921 len = strlen(s) - strlen(val) + 1;
924 errx(1, "cmdline_symset: malloc");
926 strlcpy(sym, s, len);
928 ret = symset(sym, val + 1, 1);
935 symget(const char *nam)
939 TAILQ_FOREACH(sym, &symhead, entry) {
940 if (strcmp(nam, sym->nam) == 0) {
949 atoul(char *s, u_long *ulvalp)
955 ulval = strtoul(s, &ep, 0);
956 if (s[0] == '\0' || *ep != '\0')
958 if (errno == ERANGE && ulval == ULONG_MAX)