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 int cmdline_symset(char *);
78 char *symget(const char *);
80 static int atoul(char *, u_long *);
82 static const struct got_error* gerror;
83 static struct gotconfig_remote_repo *remote;
84 static struct gotconfig gotconfig;
85 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
86 static const struct got_error* new_fetch_config(struct fetch_config **);
87 static const struct got_error* new_send_config(struct send_config **);
93 struct node_branch *branch;
99 #if defined(__APPLE__) && !defined(YYSTYPE)
100 #warning "Setting YYSTYPE - is GNU Bison installed?"
101 #define YYSTYPE YYSTYPE
106 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
107 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
108 %token <v.string> STRING
109 %token <v.number> NUMBER
110 %type <v.number> boolean portplain
111 %type <v.string> numberstring
112 %type <v.branch> branch xbranch branch_list
113 %type <v.ref> ref xref ref_list
117 grammar : /* empty */
119 | grammar author '\n'
120 | grammar remote '\n'
123 if (strcasecmp($1, "true") == 0 ||
124 strcasecmp($1, "yes") == 0)
126 else if (strcasecmp($1, "false") == 0 ||
127 strcasecmp($1, "no") == 0)
130 yyerror("invalid boolean value '%s'", $1);
137 numberstring : NUMBER {
139 if (asprintf(&s, "%lld", $1) == -1) {
140 yyerror("string: asprintf");
147 portplain : numberstring {
148 if (parseport($1, &$$) == -1) {
155 branch : /* empty */ { $$ = NULL; }
156 | xbranch { $$ = $1; }
157 | '{' optnl branch_list '}' { $$ = $3; }
160 $$ = calloc(1, sizeof(struct node_branch));
165 $$->branch_name = $1;
169 branch_list : xbranch optnl { $$ = $1; }
170 | branch_list comma xbranch optnl {
176 ref : /* empty */ { $$ = NULL; }
178 | '{' optnl ref_list '}' { $$ = $3; }
181 $$ = calloc(1, sizeof(struct node_ref));
190 ref_list : xref optnl { $$ = $1; }
191 | ref_list comma xref optnl {
197 remoteopts2 : remoteopts2 remoteopts1 nl
200 remoteopts1 : REPOSITORY STRING {
201 remote->repository = $2;
207 remote->protocol = $2;
209 | MIRROR_REFERENCES boolean {
210 remote->mirror_references = $2;
212 | FETCH_ALL_BRANCHES boolean {
213 remote->fetch_all_branches = $2;
222 remote->fetch_ref = $2;
225 static const struct got_error* error;
227 if (remote->fetch_config != NULL) {
228 yyerror("fetch block already exists");
231 error = new_fetch_config(&remote->fetch_config);
233 yyerror("%s", error->msg);
236 } '{' optnl fetchempty '}'
238 static const struct got_error* error;
240 if (remote->send_config != NULL) {
241 yyerror("send block already exists");
244 error = new_send_config(&remote->send_config);
246 yyerror("%s", error->msg);
249 } '{' optnl sendempty '}'
251 fetchempty : /* empty */
254 fetchopts2 : fetchopts2 fetchopts1 nl
257 fetchopts1 : REPOSITORY STRING {
258 remote->fetch_config->repository = $2;
261 remote->fetch_config->server = $2;
264 remote->fetch_config->protocol = $2;
267 remote->fetch_config->port = $2;
270 remote->fetch_config->branch = $2;
273 sendempty : /* empty */
276 sendopts2 : sendopts2 sendopts1 nl
279 sendopts1 : REPOSITORY STRING {
280 remote->send_config->repository = $2;
283 remote->send_config->server = $2;
286 remote->send_config->protocol = $2;
289 remote->send_config->port = $2;
292 remote->send_config->branch = $2;
295 remote : REMOTE STRING {
296 static const struct got_error* error;
298 error = new_remote(&remote);
301 yyerror("%s", error->msg);
305 } '{' optnl remoteopts2 '}' {
306 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
307 gotconfig.nremotes++;
310 author : AUTHOR STRING {
311 gotconfig.author = $2;
330 yyerror(const char *fmt, ...)
337 if (vasprintf(&msg, fmt, ap) == -1) {
338 gerror = got_error_from_errno("vasprintf");
342 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
344 gerror = got_error_from_errno("asprintf");
347 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
352 kw_cmp(const void *k, const void *e)
354 return (strcmp(k, ((const struct keywords *)e)->k_name));
360 /* This has to be sorted always. */
361 static const struct keywords keywords[] = {
365 {"fetch-all-branches", FETCH_ALL_BRANCHES},
366 {"mirror-references", MIRROR_REFERENCES},
368 {"protocol", PROTOCOL},
369 {"reference", REFERENCE},
371 {"repository", REPOSITORY},
375 const struct keywords *p;
377 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
378 sizeof(keywords[0]), kw_cmp);
386 #define START_EXPAND 1
387 #define DONE_EXPAND 2
389 static int expanding;
397 if (file->ungetpos > 0)
398 c = file->ungetbuf[--file->ungetpos];
400 c = getc(file->stream);
402 if (c == START_EXPAND)
404 else if (c == DONE_EXPAND)
420 yyerror("reached end of file while parsing "
433 yylval.lineno = file->lineno;
446 if (file->ungetpos >= file->ungetsize) {
447 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
449 err(1, "%s", __func__);
451 file->ungetsize *= 2;
453 file->ungetbuf[file->ungetpos++] = c;
461 /* Skip to either EOF or the first real EOL. */
480 if (atoul(n, &ulval) == 0) {
481 if (ulval == 0 || ulval > 65535) {
482 yyerror("illegal port value %lu", ulval);
487 s = getservbyname(n, "tcp");
489 s = getservbyname(n, "udp");
491 yyerror("unknown port %s", n);
499 parseport(char *port, long long *pn)
501 if ((*pn = getservice(port)) == -1) {
520 while (c == ' ' || c == '\t')
521 c = lgetc(0); /* nothing */
523 yylval.lineno = file->lineno;
526 while (c != '\n' && c != EOF)
527 c = lgetc(0); /* nothing */
529 if (c == '$' && !expanding) {
535 if (p + 1 >= buf + sizeof(buf) - 1) {
536 yyerror("string too long");
539 if (isalnum(c) || c == '_') {
549 yyerror("macro '%s' not defined", buf);
552 p = val + strlen(val) - 1;
553 lungetc(DONE_EXPAND);
555 lungetc((unsigned char)*p);
558 lungetc(START_EXPAND);
573 } else if (c == '\\') {
574 next = lgetc(quotec);
577 if (next == quotec || c == ' ' || c == '\t')
579 else if (next == '\n') {
584 } else if (c == quotec) {
587 } else if (c == '\0') {
588 yyerror("syntax error");
591 if (p + 1 >= buf + sizeof(buf) - 1) {
592 yyerror("string too long");
597 yylval.v.string = strdup(buf);
598 if (yylval.v.string == NULL)
599 err(1, "%s", __func__);
603 #define allowed_to_end_number(x) \
604 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
606 if (c == '-' || isdigit(c)) {
609 if ((size_t)(p-buf) >= sizeof(buf)) {
610 yyerror("string too long");
614 } while (c != EOF && isdigit(c));
616 if (p == buf + 1 && buf[0] == '-')
618 if (c == EOF || allowed_to_end_number(c)) {
619 const char *errstr = NULL;
622 yylval.v.number = strtonum(buf, LLONG_MIN,
625 yyerror("\"%s\" invalid number: %s",
633 lungetc((unsigned char)*--p);
634 c = (unsigned char)*--p;
640 #define allowed_in_string(x) \
641 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
642 x != '{' && x != '}' && \
643 x != '!' && x != '=' && x != '#' && \
646 if (isalnum(c) || c == ':' || c == '_') {
649 if ((size_t)(p-buf) >= sizeof(buf)) {
650 yyerror("string too long");
654 } while (c != EOF && (allowed_in_string(c)));
658 if (token == STRING) {
659 yylval.v.string = strdup(buf);
660 if (yylval.v.string == NULL)
661 err(1, "%s", __func__);
666 yylval.lineno = file->lineno;
674 static const struct got_error*
675 newfile(struct file **nfile, const char *filename, int *fd)
677 const struct got_error* error = NULL;
679 (*nfile) = calloc(1, sizeof(struct file));
680 if ((*nfile) == NULL)
681 return got_error_from_errno("calloc");
682 (*nfile)->stream = fdopen(*fd, "r");
683 if ((*nfile)->stream == NULL) {
684 error = got_error_from_errno("fdopen");
688 *fd = -1; /* Stream owns the file descriptor now. */
689 (*nfile)->name = filename;
690 (*nfile)->lineno = 1;
691 (*nfile)->ungetsize = 16;
692 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
693 if ((*nfile)->ungetbuf == NULL) {
694 error = got_error_from_errno("malloc");
695 fclose((*nfile)->stream);
702 static const struct got_error*
703 new_remote(struct gotconfig_remote_repo **remote)
705 const struct got_error *error = NULL;
707 *remote = calloc(1, sizeof(**remote));
709 error = got_error_from_errno("calloc");
713 static const struct got_error*
714 new_fetch_config(struct fetch_config **fetch_config)
716 const struct got_error *error = NULL;
718 *fetch_config = calloc(1, sizeof(**fetch_config));
719 if (*fetch_config == NULL)
720 error = got_error_from_errno("calloc");
724 static const struct got_error*
725 new_send_config(struct send_config **send_config)
727 const struct got_error *error = NULL;
729 *send_config = calloc(1, sizeof(**send_config));
730 if (*send_config == NULL)
731 error = got_error_from_errno("calloc");
736 closefile(struct file *file)
738 fclose(file->stream);
739 free(file->ungetbuf);
743 const struct got_error *
744 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
746 const struct got_error *err = NULL;
747 struct sym *sym, *next;
751 err = newfile(&file, filename, fd);
755 TAILQ_INIT(&gotconfig.remotes);
760 /* Free macros and check which have not been used. */
761 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
765 TAILQ_REMOVE(&symhead, sym, entry);
776 free_fetch_config(struct fetch_config *fetch_config)
778 free(remote->fetch_config->repository);
779 free(remote->fetch_config->server);
780 free(remote->fetch_config->protocol);
781 free(remote->fetch_config);
785 free_send_config(struct send_config *send_config)
787 free(remote->send_config->repository);
788 free(remote->send_config->server);
789 free(remote->send_config->protocol);
790 free(remote->send_config);
794 gotconfig_free(struct gotconfig *conf)
796 struct gotconfig_remote_repo *remote;
799 while (!TAILQ_EMPTY(&conf->remotes)) {
800 remote = TAILQ_FIRST(&conf->remotes);
801 TAILQ_REMOVE(&conf->remotes, remote, entry);
802 if (remote->fetch_config != NULL)
803 free_fetch_config(remote->fetch_config);
804 if (remote->send_config != NULL)
805 free_send_config(remote->send_config);
807 free(remote->repository);
808 free(remote->server);
809 free(remote->protocol);
815 symset(const char *nam, const char *val, int persist)
819 TAILQ_FOREACH(sym, &symhead, entry) {
820 if (strcmp(nam, sym->nam) == 0)
825 if (sym->persist == 1)
830 TAILQ_REMOVE(&symhead, sym, entry);
834 sym = calloc(1, sizeof(*sym));
838 sym->nam = strdup(nam);
839 if (sym->nam == NULL) {
843 sym->val = strdup(val);
844 if (sym->val == NULL) {
850 sym->persist = persist;
851 TAILQ_INSERT_TAIL(&symhead, sym, entry);
856 cmdline_symset(char *s)
862 val = strrchr(s, '=');
866 len = strlen(s) - strlen(val) + 1;
869 errx(1, "cmdline_symset: malloc");
871 strlcpy(sym, s, len);
873 ret = symset(sym, val + 1, 1);
880 symget(const char *nam)
884 TAILQ_FOREACH(sym, &symhead, entry) {
885 if (strcmp(nam, sym->nam) == 0) {
894 atoul(char *s, u_long *ulvalp)
900 ulval = strtoul(s, &ep, 0);
901 if (s[0] == '\0' || *ep != '\0')
903 if (errno == ERANGE && ulval == ULONG_MAX)