2 * Copyright (c) 2020 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 **);
89 struct node_branch *branch;
98 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
99 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE
100 %token <v.string> STRING
101 %token <v.number> NUMBER
102 %type <v.number> boolean portplain
103 %type <v.string> numberstring
104 %type <v.branch> branch xbranch branch_list
105 %type <v.ref> ref xref ref_list
109 grammar : /* empty */
111 | grammar author '\n'
112 | grammar remote '\n'
115 if (strcasecmp($1, "true") == 0 ||
116 strcasecmp($1, "yes") == 0)
118 else if (strcasecmp($1, "false") == 0 ||
119 strcasecmp($1, "no") == 0)
122 yyerror("invalid boolean value '%s'", $1);
129 numberstring : NUMBER {
131 if (asprintf(&s, "%lld", $1) == -1) {
132 yyerror("string: asprintf");
139 portplain : numberstring {
140 if (parseport($1, &$$) == -1) {
147 branch : /* empty */ { $$ = NULL; }
148 | xbranch { $$ = $1; }
149 | '{' optnl branch_list '}' { $$ = $3; }
152 $$ = calloc(1, sizeof(struct node_branch));
157 $$->branch_name = $1;
161 branch_list : xbranch optnl { $$ = $1; }
162 | branch_list comma xbranch optnl {
168 ref : /* empty */ { $$ = NULL; }
170 | '{' optnl ref_list '}' { $$ = $3; }
173 $$ = calloc(1, sizeof(struct node_ref));
182 ref_list : xref optnl { $$ = $1; }
183 | ref_list comma xref optnl {
189 remoteopts2 : remoteopts2 remoteopts1 nl
192 remoteopts1 : REPOSITORY STRING {
193 remote->repository = strdup($2);
194 if (remote->repository == NULL) {
202 remote->server = strdup($2);
203 if (remote->server == NULL) {
211 remote->protocol = strdup($2);
212 if (remote->protocol == NULL) {
219 | MIRROR_REFERENCES boolean {
220 remote->mirror_references = $2;
222 | FETCH_ALL_BRANCHES boolean {
223 remote->fetch_all_branches = $2;
235 remote : REMOTE STRING {
236 static const struct got_error* error;
238 error = new_remote(&remote);
241 yyerror("%s", error->msg);
244 remote->name = strdup($2);
245 if (remote->name == NULL) {
251 } '{' optnl remoteopts2 '}' {
252 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
253 gotconfig.nremotes++;
256 author : AUTHOR STRING {
257 gotconfig.author = strdup($2);
258 if (gotconfig.author == NULL) {
282 yyerror(const char *fmt, ...)
289 if (vasprintf(&msg, fmt, ap) == -1) {
290 gerror = got_error_from_errno("vasprintf");
294 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
296 gerror = got_error_from_errno("asprintf");
299 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
305 kw_cmp(const void *k, const void *e)
307 return (strcmp(k, ((const struct keywords *)e)->k_name));
313 /* This has to be sorted always. */
314 static const struct keywords keywords[] = {
317 {"fetch-all-branches", FETCH_ALL_BRANCHES},
318 {"mirror-references", MIRROR_REFERENCES},
320 {"protocol", PROTOCOL},
321 {"reference", REFERENCE},
323 {"repository", REPOSITORY},
326 const struct keywords *p;
328 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
329 sizeof(keywords[0]), kw_cmp);
337 #define START_EXPAND 1
338 #define DONE_EXPAND 2
340 static int expanding;
348 if (file->ungetpos > 0)
349 c = file->ungetbuf[--file->ungetpos];
351 c = getc(file->stream);
353 if (c == START_EXPAND)
355 else if (c == DONE_EXPAND)
371 yyerror("reached end of file while parsing "
384 yylval.lineno = file->lineno;
397 if (file->ungetpos >= file->ungetsize) {
398 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
400 err(1, "%s", __func__);
402 file->ungetsize *= 2;
404 file->ungetbuf[file->ungetpos++] = c;
412 /* Skip to either EOF or the first real EOL. */
431 if (atoul(n, &ulval) == 0) {
433 yyerror("illegal port value %lu", ulval);
438 s = getservbyname(n, "tcp");
440 s = getservbyname(n, "udp");
442 yyerror("unknown port %s", n);
450 parseport(char *port, long long *pn)
452 if ((*pn = getservice(port)) == -1) {
463 unsigned char buf[8096];
464 unsigned char *p, *val;
471 while (c == ' ' || c == '\t')
472 c = lgetc(0); /* nothing */
474 yylval.lineno = file->lineno;
477 while (c != '\n' && c != EOF)
478 c = lgetc(0); /* nothing */
480 if (c == '$' && !expanding) {
486 if (p + 1 >= buf + sizeof(buf) - 1) {
487 yyerror("string too long");
490 if (isalnum(c) || c == '_') {
500 yyerror("macro '%s' not defined", buf);
503 p = val + strlen(val) - 1;
504 lungetc(DONE_EXPAND);
509 lungetc(START_EXPAND);
524 } else if (c == '\\') {
525 next = lgetc(quotec);
528 if (next == quotec || c == ' ' || c == '\t')
530 else if (next == '\n') {
535 } else if (c == quotec) {
538 } else if (c == '\0') {
539 yyerror("syntax error");
542 if (p + 1 >= buf + sizeof(buf) - 1) {
543 yyerror("string too long");
548 yylval.v.string = strdup(buf);
549 if (yylval.v.string == NULL)
550 err(1, "%s", __func__);
554 #define allowed_to_end_number(x) \
555 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
557 if (c == '-' || isdigit(c)) {
560 if ((unsigned)(p-buf) >= sizeof(buf)) {
561 yyerror("string too long");
565 } while (c != EOF && isdigit(c));
567 if (p == buf + 1 && buf[0] == '-')
569 if (c == EOF || allowed_to_end_number(c)) {
570 const char *errstr = NULL;
573 yylval.v.number = strtonum(buf, LLONG_MIN,
576 yyerror("\"%s\" invalid number: %s",
591 #define allowed_in_string(x) \
592 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
593 x != '{' && x != '}' && \
594 x != '!' && x != '=' && x != '#' && \
597 if (isalnum(c) || c == ':' || c == '_') {
600 if ((unsigned)(p-buf) >= sizeof(buf)) {
601 yyerror("string too long");
605 } while (c != EOF && (allowed_in_string(c)));
609 if (token == STRING) {
610 yylval.v.string = strdup(buf);
611 if (yylval.v.string == NULL)
612 err(1, "%s", __func__);
617 yylval.lineno = file->lineno;
625 static const struct got_error*
626 newfile(struct file **nfile, const char *filename, int *fd)
628 const struct got_error* error = NULL;
630 (*nfile) = calloc(1, sizeof(struct file));
631 if ((*nfile) == NULL)
632 return got_error_from_errno("calloc");
633 (*nfile)->stream = fdopen(*fd, "r");
634 if ((*nfile)->stream == NULL) {
635 error = got_error_from_errno("fdopen");
639 *fd = -1; /* Stream owns the file descriptor now. */
640 (*nfile)->name = filename;
641 (*nfile)->lineno = 1;
642 (*nfile)->ungetsize = 16;
643 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
644 if ((*nfile)->ungetbuf == NULL) {
645 error = got_error_from_errno("malloc");
646 fclose((*nfile)->stream);
653 static const struct got_error*
654 new_remote(struct gotconfig_remote_repo **remote)
656 const struct got_error *error = NULL;
658 *remote = calloc(1, sizeof(**remote));
660 error = got_error_from_errno("calloc");
665 closefile(struct file *file)
667 fclose(file->stream);
668 free(file->ungetbuf);
672 const struct got_error *
673 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
675 const struct got_error *err = NULL;
676 struct sym *sym, *next;
680 err = newfile(&file, filename, fd);
684 TAILQ_INIT(&gotconfig.remotes);
689 /* Free macros and check which have not been used. */
690 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
694 TAILQ_REMOVE(&symhead, sym, entry);
705 gotconfig_free(struct gotconfig *conf)
707 struct gotconfig_remote_repo *remote;
710 while (!TAILQ_EMPTY(&conf->remotes)) {
711 remote = TAILQ_FIRST(&conf->remotes);
712 TAILQ_REMOVE(&conf->remotes, remote, entry);
714 free(remote->repository);
715 free(remote->server);
716 free(remote->protocol);
722 symset(const char *nam, const char *val, int persist)
726 TAILQ_FOREACH(sym, &symhead, entry) {
727 if (strcmp(nam, sym->nam) == 0)
732 if (sym->persist == 1)
737 TAILQ_REMOVE(&symhead, sym, entry);
741 sym = calloc(1, sizeof(*sym));
745 sym->nam = strdup(nam);
746 if (sym->nam == NULL) {
750 sym->val = strdup(val);
751 if (sym->val == NULL) {
757 sym->persist = persist;
758 TAILQ_INSERT_TAIL(&symhead, sym, entry);
763 cmdline_symset(char *s)
769 val = strrchr(s, '=');
773 len = strlen(s) - strlen(val) + 1;
776 errx(1, "cmdline_symset: malloc");
778 strlcpy(sym, s, len);
780 ret = symset(sym, val + 1, 1);
787 symget(const char *nam)
791 TAILQ_FOREACH(sym, &symhead, entry) {
792 if (strcmp(nam, sym->nam) == 0) {
801 atoul(char *s, u_long *ulvalp)
807 ulval = strtoul(s, &ep, 0);
808 if (s[0] == '\0' || *ep != '\0')
810 if (errno == ERANGE && ulval == ULONG_MAX)