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 **);
96 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES AUTHOR
97 %token <v.string> STRING
98 %token <v.number> NUMBER
99 %type <v.number> boolean portplain
100 %type <v.string> numberstring
104 grammar : /* empty */
106 | grammar author '\n'
107 | grammar remote '\n'
110 if (strcasecmp($1, "true") == 0 ||
111 strcasecmp($1, "yes") == 0)
113 else if (strcasecmp($1, "false") == 0 ||
114 strcasecmp($1, "no") == 0)
117 yyerror("invalid boolean value '%s'", $1);
124 numberstring : NUMBER {
126 if (asprintf(&s, "%lld", $1) == -1) {
127 yyerror("string: asprintf");
134 portplain : numberstring {
135 if (parseport($1, &$$) == -1) {
142 remoteopts2 : remoteopts2 remoteopts1 nl
145 remoteopts1 : REPOSITORY STRING {
146 remote->repository = strdup($2);
147 if (remote->repository == NULL) {
155 remote->server = strdup($2);
156 if (remote->server == NULL) {
164 remote->protocol = strdup($2);
165 if (remote->protocol == NULL) {
172 | MIRROR_REFERENCES boolean {
173 remote->mirror_references = $2;
179 remote : REMOTE STRING {
180 static const struct got_error* error;
182 error = new_remote(&remote);
185 yyerror("%s", error->msg);
188 remote->name = strdup($2);
189 if (remote->name == NULL) {
195 } '{' optnl remoteopts2 '}' {
196 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
197 gotconfig.nremotes++;
200 author : AUTHOR STRING {
201 gotconfig.author = strdup($2);
202 if (gotconfig.author == NULL) {
223 yyerror(const char *fmt, ...)
230 if (vasprintf(&msg, fmt, ap) == -1) {
231 gerror = got_error_from_errno("vasprintf");
235 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
237 gerror = got_error_from_errno("asprintf");
240 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
246 kw_cmp(const void *k, const void *e)
248 return (strcmp(k, ((const struct keywords *)e)->k_name));
254 /* This has to be sorted always. */
255 static const struct keywords keywords[] = {
257 {"mirror-references", MIRROR_REFERENCES},
259 {"protocol", PROTOCOL},
261 {"repository", REPOSITORY},
264 const struct keywords *p;
266 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
267 sizeof(keywords[0]), kw_cmp);
275 #define START_EXPAND 1
276 #define DONE_EXPAND 2
278 static int expanding;
286 if (file->ungetpos > 0)
287 c = file->ungetbuf[--file->ungetpos];
289 c = getc(file->stream);
291 if (c == START_EXPAND)
293 else if (c == DONE_EXPAND)
309 yyerror("reached end of file while parsing "
322 yylval.lineno = file->lineno;
335 if (file->ungetpos >= file->ungetsize) {
336 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
338 err(1, "%s", __func__);
340 file->ungetsize *= 2;
342 file->ungetbuf[file->ungetpos++] = c;
350 /* Skip to either EOF or the first real EOL. */
369 if (atoul(n, &ulval) == 0) {
371 yyerror("illegal port value %lu", ulval);
376 s = getservbyname(n, "tcp");
378 s = getservbyname(n, "udp");
380 yyerror("unknown port %s", n);
388 parseport(char *port, long long *pn)
390 if ((*pn = getservice(port)) == -1) {
401 unsigned char buf[8096];
402 unsigned char *p, *val;
409 while (c == ' ' || c == '\t')
410 c = lgetc(0); /* nothing */
412 yylval.lineno = file->lineno;
415 while (c != '\n' && c != EOF)
416 c = lgetc(0); /* nothing */
418 if (c == '$' && !expanding) {
424 if (p + 1 >= buf + sizeof(buf) - 1) {
425 yyerror("string too long");
428 if (isalnum(c) || c == '_') {
438 yyerror("macro '%s' not defined", buf);
441 p = val + strlen(val) - 1;
442 lungetc(DONE_EXPAND);
447 lungetc(START_EXPAND);
462 } else if (c == '\\') {
463 next = lgetc(quotec);
466 if (next == quotec || c == ' ' || c == '\t')
468 else if (next == '\n') {
473 } else if (c == quotec) {
476 } else if (c == '\0') {
477 yyerror("syntax error");
480 if (p + 1 >= buf + sizeof(buf) - 1) {
481 yyerror("string too long");
486 yylval.v.string = strdup(buf);
487 if (yylval.v.string == NULL)
488 err(1, "%s", __func__);
492 #define allowed_to_end_number(x) \
493 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
495 if (c == '-' || isdigit(c)) {
498 if ((unsigned)(p-buf) >= sizeof(buf)) {
499 yyerror("string too long");
503 } while (c != EOF && isdigit(c));
505 if (p == buf + 1 && buf[0] == '-')
507 if (c == EOF || allowed_to_end_number(c)) {
508 const char *errstr = NULL;
511 yylval.v.number = strtonum(buf, LLONG_MIN,
514 yyerror("\"%s\" invalid number: %s",
529 #define allowed_in_string(x) \
530 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
531 x != '{' && x != '}' && \
532 x != '!' && x != '=' && x != '#' && \
535 if (isalnum(c) || c == ':' || c == '_') {
538 if ((unsigned)(p-buf) >= sizeof(buf)) {
539 yyerror("string too long");
543 } while (c != EOF && (allowed_in_string(c)));
547 if (token == STRING) {
548 yylval.v.string = strdup(buf);
549 if (yylval.v.string == NULL)
550 err(1, "%s", __func__);
555 yylval.lineno = file->lineno;
563 static const struct got_error*
564 newfile(struct file **nfile, const char *filename, int *fd)
566 const struct got_error* error = NULL;
568 (*nfile) = calloc(1, sizeof(struct file));
569 if ((*nfile) == NULL)
570 return got_error_from_errno("calloc");
571 (*nfile)->stream = fdopen(*fd, "r");
572 if ((*nfile)->stream == NULL) {
573 error = got_error_from_errno("fdopen");
577 *fd = -1; /* Stream owns the file descriptor now. */
578 (*nfile)->name = filename;
579 (*nfile)->lineno = 1;
580 (*nfile)->ungetsize = 16;
581 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
582 if ((*nfile)->ungetbuf == NULL) {
583 error = got_error_from_errno("malloc");
584 fclose((*nfile)->stream);
591 static const struct got_error*
592 new_remote(struct gotconfig_remote_repo **remote)
594 const struct got_error *error = NULL;
596 *remote = calloc(1, sizeof(**remote));
598 error = got_error_from_errno("calloc");
603 closefile(struct file *file)
605 fclose(file->stream);
606 free(file->ungetbuf);
610 const struct got_error *
611 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
613 const struct got_error *err = NULL;
614 struct sym *sym, *next;
618 err = newfile(&file, filename, fd);
622 TAILQ_INIT(&gotconfig.remotes);
627 /* Free macros and check which have not been used. */
628 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
632 TAILQ_REMOVE(&symhead, sym, entry);
643 gotconfig_free(struct gotconfig *conf)
645 struct gotconfig_remote_repo *remote;
648 while (!TAILQ_EMPTY(&conf->remotes)) {
649 remote = TAILQ_FIRST(&conf->remotes);
650 TAILQ_REMOVE(&conf->remotes, remote, entry);
652 free(remote->repository);
653 free(remote->server);
654 free(remote->protocol);
660 symset(const char *nam, const char *val, int persist)
664 TAILQ_FOREACH(sym, &symhead, entry) {
665 if (strcmp(nam, sym->nam) == 0)
670 if (sym->persist == 1)
675 TAILQ_REMOVE(&symhead, sym, entry);
679 sym = calloc(1, sizeof(*sym));
683 sym->nam = strdup(nam);
684 if (sym->nam == NULL) {
688 sym->val = strdup(val);
689 if (sym->val == NULL) {
695 sym->persist = persist;
696 TAILQ_INSERT_TAIL(&symhead, sym, entry);
701 cmdline_symset(char *s)
707 val = strrchr(s, '=');
711 len = strlen(s) - strlen(val) + 1;
714 errx(1, "cmdline_symset: malloc");
716 strlcpy(sym, s, len);
718 ret = symset(sym, val + 1, 1);
725 symget(const char *nam)
729 TAILQ_FOREACH(sym, &symhead, entry) {
730 if (strcmp(nam, sym->nam) == 0) {
739 atoul(char *s, u_long *ulvalp)
745 ulval = strtoul(s, &ep, 0);
746 if (s[0] == '\0' || *ep != '\0')
748 if (errno == ERANGE && ulval == ULONG_MAX)