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;
97 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
99 %token <v.string> STRING
100 %token <v.number> NUMBER
101 %type <v.number> boolean portplain
102 %type <v.string> numberstring
103 %type <v.branch> branch xbranch branch_list
107 grammar : /* empty */
109 | grammar author '\n'
110 | grammar remote '\n'
113 if (strcasecmp($1, "true") == 0 ||
114 strcasecmp($1, "yes") == 0)
116 else if (strcasecmp($1, "false") == 0 ||
117 strcasecmp($1, "no") == 0)
120 yyerror("invalid boolean value '%s'", $1);
127 numberstring : NUMBER {
129 if (asprintf(&s, "%lld", $1) == -1) {
130 yyerror("string: asprintf");
137 portplain : numberstring {
138 if (parseport($1, &$$) == -1) {
145 branch : /* empty */ { $$ = NULL; }
146 | xbranch { $$ = $1; }
147 | '{' optnl branch_list '}' { $$ = $3; }
150 $$ = calloc(1, sizeof(struct node_branch));
155 $$->branch_name = $1;
159 branch_list : xbranch optnl { $$ = $1; }
160 | branch_list comma xbranch optnl {
167 remoteopts2 : remoteopts2 remoteopts1 nl
170 remoteopts1 : REPOSITORY STRING {
171 remote->repository = strdup($2);
172 if (remote->repository == NULL) {
180 remote->server = strdup($2);
181 if (remote->server == NULL) {
189 remote->protocol = strdup($2);
190 if (remote->protocol == NULL) {
197 | MIRROR_REFERENCES boolean {
198 remote->mirror_references = $2;
207 remote : REMOTE STRING {
208 static const struct got_error* error;
210 error = new_remote(&remote);
213 yyerror("%s", error->msg);
216 remote->name = strdup($2);
217 if (remote->name == NULL) {
223 } '{' optnl remoteopts2 '}' {
224 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
225 gotconfig.nremotes++;
228 author : AUTHOR STRING {
229 gotconfig.author = strdup($2);
230 if (gotconfig.author == NULL) {
254 yyerror(const char *fmt, ...)
261 if (vasprintf(&msg, fmt, ap) == -1) {
262 gerror = got_error_from_errno("vasprintf");
266 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
268 gerror = got_error_from_errno("asprintf");
271 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
277 kw_cmp(const void *k, const void *e)
279 return (strcmp(k, ((const struct keywords *)e)->k_name));
285 /* This has to be sorted always. */
286 static const struct keywords keywords[] = {
289 {"mirror-references", MIRROR_REFERENCES},
291 {"protocol", PROTOCOL},
293 {"repository", REPOSITORY},
296 const struct keywords *p;
298 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
299 sizeof(keywords[0]), kw_cmp);
307 #define START_EXPAND 1
308 #define DONE_EXPAND 2
310 static int expanding;
318 if (file->ungetpos > 0)
319 c = file->ungetbuf[--file->ungetpos];
321 c = getc(file->stream);
323 if (c == START_EXPAND)
325 else if (c == DONE_EXPAND)
341 yyerror("reached end of file while parsing "
354 yylval.lineno = file->lineno;
367 if (file->ungetpos >= file->ungetsize) {
368 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
370 err(1, "%s", __func__);
372 file->ungetsize *= 2;
374 file->ungetbuf[file->ungetpos++] = c;
382 /* Skip to either EOF or the first real EOL. */
401 if (atoul(n, &ulval) == 0) {
403 yyerror("illegal port value %lu", ulval);
408 s = getservbyname(n, "tcp");
410 s = getservbyname(n, "udp");
412 yyerror("unknown port %s", n);
420 parseport(char *port, long long *pn)
422 if ((*pn = getservice(port)) == -1) {
433 unsigned char buf[8096];
434 unsigned char *p, *val;
441 while (c == ' ' || c == '\t')
442 c = lgetc(0); /* nothing */
444 yylval.lineno = file->lineno;
447 while (c != '\n' && c != EOF)
448 c = lgetc(0); /* nothing */
450 if (c == '$' && !expanding) {
456 if (p + 1 >= buf + sizeof(buf) - 1) {
457 yyerror("string too long");
460 if (isalnum(c) || c == '_') {
470 yyerror("macro '%s' not defined", buf);
473 p = val + strlen(val) - 1;
474 lungetc(DONE_EXPAND);
479 lungetc(START_EXPAND);
494 } else if (c == '\\') {
495 next = lgetc(quotec);
498 if (next == quotec || c == ' ' || c == '\t')
500 else if (next == '\n') {
505 } else if (c == quotec) {
508 } else if (c == '\0') {
509 yyerror("syntax error");
512 if (p + 1 >= buf + sizeof(buf) - 1) {
513 yyerror("string too long");
518 yylval.v.string = strdup(buf);
519 if (yylval.v.string == NULL)
520 err(1, "%s", __func__);
524 #define allowed_to_end_number(x) \
525 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
527 if (c == '-' || isdigit(c)) {
530 if ((unsigned)(p-buf) >= sizeof(buf)) {
531 yyerror("string too long");
535 } while (c != EOF && isdigit(c));
537 if (p == buf + 1 && buf[0] == '-')
539 if (c == EOF || allowed_to_end_number(c)) {
540 const char *errstr = NULL;
543 yylval.v.number = strtonum(buf, LLONG_MIN,
546 yyerror("\"%s\" invalid number: %s",
561 #define allowed_in_string(x) \
562 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
563 x != '{' && x != '}' && \
564 x != '!' && x != '=' && x != '#' && \
567 if (isalnum(c) || c == ':' || c == '_') {
570 if ((unsigned)(p-buf) >= sizeof(buf)) {
571 yyerror("string too long");
575 } while (c != EOF && (allowed_in_string(c)));
579 if (token == STRING) {
580 yylval.v.string = strdup(buf);
581 if (yylval.v.string == NULL)
582 err(1, "%s", __func__);
587 yylval.lineno = file->lineno;
595 static const struct got_error*
596 newfile(struct file **nfile, const char *filename, int *fd)
598 const struct got_error* error = NULL;
600 (*nfile) = calloc(1, sizeof(struct file));
601 if ((*nfile) == NULL)
602 return got_error_from_errno("calloc");
603 (*nfile)->stream = fdopen(*fd, "r");
604 if ((*nfile)->stream == NULL) {
605 error = got_error_from_errno("fdopen");
609 *fd = -1; /* Stream owns the file descriptor now. */
610 (*nfile)->name = filename;
611 (*nfile)->lineno = 1;
612 (*nfile)->ungetsize = 16;
613 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
614 if ((*nfile)->ungetbuf == NULL) {
615 error = got_error_from_errno("malloc");
616 fclose((*nfile)->stream);
623 static const struct got_error*
624 new_remote(struct gotconfig_remote_repo **remote)
626 const struct got_error *error = NULL;
628 *remote = calloc(1, sizeof(**remote));
630 error = got_error_from_errno("calloc");
635 closefile(struct file *file)
637 fclose(file->stream);
638 free(file->ungetbuf);
642 const struct got_error *
643 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
645 const struct got_error *err = NULL;
646 struct sym *sym, *next;
650 err = newfile(&file, filename, fd);
654 TAILQ_INIT(&gotconfig.remotes);
659 /* Free macros and check which have not been used. */
660 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
664 TAILQ_REMOVE(&symhead, sym, entry);
675 gotconfig_free(struct gotconfig *conf)
677 struct gotconfig_remote_repo *remote;
680 while (!TAILQ_EMPTY(&conf->remotes)) {
681 remote = TAILQ_FIRST(&conf->remotes);
682 TAILQ_REMOVE(&conf->remotes, remote, entry);
684 free(remote->repository);
685 free(remote->server);
686 free(remote->protocol);
692 symset(const char *nam, const char *val, int persist)
696 TAILQ_FOREACH(sym, &symhead, entry) {
697 if (strcmp(nam, sym->nam) == 0)
702 if (sym->persist == 1)
707 TAILQ_REMOVE(&symhead, sym, entry);
711 sym = calloc(1, sizeof(*sym));
715 sym->nam = strdup(nam);
716 if (sym->nam == NULL) {
720 sym->val = strdup(val);
721 if (sym->val == NULL) {
727 sym->persist = persist;
728 TAILQ_INSERT_TAIL(&symhead, sym, entry);
733 cmdline_symset(char *s)
739 val = strrchr(s, '=');
743 len = strlen(s) - strlen(val) + 1;
746 errx(1, "cmdline_symset: malloc");
748 strlcpy(sym, s, len);
750 ret = symset(sym, val + 1, 1);
757 symget(const char *nam)
761 TAILQ_FOREACH(sym, &symhead, entry) {
762 if (strcmp(nam, sym->nam) == 0) {
771 atoul(char *s, u_long *ulvalp)
777 ulval = strtoul(s, &ep, 0);
778 if (s[0] == '\0' || *ep != '\0')
780 if (errno == ERANGE && ulval == ULONG_MAX)