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;
234 remote->fetch_ref = $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 fetchempty '}'
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 sendempty '}'
263 fetchempty : /* empty */
266 fetchopts2 : fetchopts2 fetchopts1 nl
269 fetchopts1 : REPOSITORY STRING {
270 remote->fetch_config->repository = strdup($2);
271 if (remote->fetch_config->repository == NULL) {
279 remote->fetch_config->server = strdup($2);
280 if (remote->fetch_config->server == NULL) {
288 remote->fetch_config->protocol = strdup($2);
289 if (remote->fetch_config->protocol == NULL) {
297 remote->fetch_config->port = $2;
300 remote->fetch_config->branch = $2;
303 sendempty : /* empty */
306 sendopts2 : sendopts2 sendopts1 nl
309 sendopts1 : REPOSITORY STRING {
310 remote->send_config->repository = strdup($2);
311 if (remote->send_config->repository == NULL) {
319 remote->send_config->server = strdup($2);
320 if (remote->send_config->server == NULL) {
328 remote->send_config->protocol = strdup($2);
329 if (remote->send_config->protocol == NULL) {
337 remote->send_config->port = $2;
340 remote->send_config->branch = $2;
343 remote : REMOTE STRING {
344 static const struct got_error* error;
346 error = new_remote(&remote);
349 yyerror("%s", error->msg);
352 remote->name = strdup($2);
353 if (remote->name == NULL) {
359 } '{' optnl remoteopts2 '}' {
360 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
361 gotconfig.nremotes++;
364 author : AUTHOR STRING {
365 gotconfig.author = strdup($2);
366 if (gotconfig.author == NULL) {
390 yyerror(const char *fmt, ...)
397 if (vasprintf(&msg, fmt, ap) == -1) {
398 gerror = got_error_from_errno("vasprintf");
402 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
404 gerror = got_error_from_errno("asprintf");
407 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
413 kw_cmp(const void *k, const void *e)
415 return (strcmp(k, ((const struct keywords *)e)->k_name));
421 /* This has to be sorted always. */
422 static const struct keywords keywords[] = {
426 {"fetch-all-branches", FETCH_ALL_BRANCHES},
427 {"mirror-references", MIRROR_REFERENCES},
429 {"protocol", PROTOCOL},
430 {"reference", REFERENCE},
432 {"repository", REPOSITORY},
436 const struct keywords *p;
438 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
439 sizeof(keywords[0]), kw_cmp);
447 #define START_EXPAND 1
448 #define DONE_EXPAND 2
450 static int expanding;
458 if (file->ungetpos > 0)
459 c = file->ungetbuf[--file->ungetpos];
461 c = getc(file->stream);
463 if (c == START_EXPAND)
465 else if (c == DONE_EXPAND)
481 yyerror("reached end of file while parsing "
494 yylval.lineno = file->lineno;
507 if (file->ungetpos >= file->ungetsize) {
508 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
510 err(1, "%s", __func__);
512 file->ungetsize *= 2;
514 file->ungetbuf[file->ungetpos++] = c;
522 /* Skip to either EOF or the first real EOL. */
541 if (atoul(n, &ulval) == 0) {
542 if (ulval == 0 || ulval > 65535) {
543 yyerror("illegal port value %lu", ulval);
548 s = getservbyname(n, "tcp");
550 s = getservbyname(n, "udp");
552 yyerror("unknown port %s", n);
560 parseport(char *port, long long *pn)
562 if ((*pn = getservice(port)) == -1) {
573 unsigned char buf[8096];
574 unsigned char *p, *val;
581 while (c == ' ' || c == '\t')
582 c = lgetc(0); /* nothing */
584 yylval.lineno = file->lineno;
587 while (c != '\n' && c != EOF)
588 c = lgetc(0); /* nothing */
590 if (c == '$' && !expanding) {
596 if (p + 1 >= buf + sizeof(buf) - 1) {
597 yyerror("string too long");
600 if (isalnum(c) || c == '_') {
610 yyerror("macro '%s' not defined", buf);
613 p = val + strlen(val) - 1;
614 lungetc(DONE_EXPAND);
619 lungetc(START_EXPAND);
634 } else if (c == '\\') {
635 next = lgetc(quotec);
638 if (next == quotec || c == ' ' || c == '\t')
640 else if (next == '\n') {
645 } else if (c == quotec) {
648 } else if (c == '\0') {
649 yyerror("syntax error");
652 if (p + 1 >= buf + sizeof(buf) - 1) {
653 yyerror("string too long");
658 yylval.v.string = strdup(buf);
659 if (yylval.v.string == NULL)
660 err(1, "%s", __func__);
664 #define allowed_to_end_number(x) \
665 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
667 if (c == '-' || isdigit(c)) {
670 if ((unsigned)(p-buf) >= sizeof(buf)) {
671 yyerror("string too long");
675 } while (c != EOF && isdigit(c));
677 if (p == buf + 1 && buf[0] == '-')
679 if (c == EOF || allowed_to_end_number(c)) {
680 const char *errstr = NULL;
683 yylval.v.number = strtonum(buf, LLONG_MIN,
686 yyerror("\"%s\" invalid number: %s",
701 #define allowed_in_string(x) \
702 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
703 x != '{' && x != '}' && \
704 x != '!' && x != '=' && x != '#' && \
707 if (isalnum(c) || c == ':' || c == '_') {
710 if ((unsigned)(p-buf) >= sizeof(buf)) {
711 yyerror("string too long");
715 } while (c != EOF && (allowed_in_string(c)));
719 if (token == STRING) {
720 yylval.v.string = strdup(buf);
721 if (yylval.v.string == NULL)
722 err(1, "%s", __func__);
727 yylval.lineno = file->lineno;
735 static const struct got_error*
736 newfile(struct file **nfile, const char *filename, int *fd)
738 const struct got_error* error = NULL;
740 (*nfile) = calloc(1, sizeof(struct file));
741 if ((*nfile) == NULL)
742 return got_error_from_errno("calloc");
743 (*nfile)->stream = fdopen(*fd, "r");
744 if ((*nfile)->stream == NULL) {
745 error = got_error_from_errno("fdopen");
749 *fd = -1; /* Stream owns the file descriptor now. */
750 (*nfile)->name = filename;
751 (*nfile)->lineno = 1;
752 (*nfile)->ungetsize = 16;
753 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
754 if ((*nfile)->ungetbuf == NULL) {
755 error = got_error_from_errno("malloc");
756 fclose((*nfile)->stream);
763 static const struct got_error*
764 new_remote(struct gotconfig_remote_repo **remote)
766 const struct got_error *error = NULL;
768 *remote = calloc(1, sizeof(**remote));
770 error = got_error_from_errno("calloc");
774 static const struct got_error*
775 new_fetch_config(struct fetch_config **fetch_config)
777 const struct got_error *error = NULL;
779 *fetch_config = calloc(1, sizeof(**fetch_config));
780 if (*fetch_config == NULL)
781 error = got_error_from_errno("calloc");
785 static const struct got_error*
786 new_send_config(struct send_config **send_config)
788 const struct got_error *error = NULL;
790 *send_config = calloc(1, sizeof(**send_config));
791 if (*send_config == NULL)
792 error = got_error_from_errno("calloc");
797 closefile(struct file *file)
799 fclose(file->stream);
800 free(file->ungetbuf);
804 const struct got_error *
805 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
807 const struct got_error *err = NULL;
808 struct sym *sym, *next;
812 err = newfile(&file, filename, fd);
816 TAILQ_INIT(&gotconfig.remotes);
821 /* Free macros and check which have not been used. */
822 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
826 TAILQ_REMOVE(&symhead, sym, entry);
837 free_fetch_config(struct fetch_config *fetch_config)
839 free(remote->fetch_config->repository);
840 free(remote->fetch_config->server);
841 free(remote->fetch_config->protocol);
842 free(remote->fetch_config);
846 free_send_config(struct send_config *send_config)
848 free(remote->send_config->repository);
849 free(remote->send_config->server);
850 free(remote->send_config->protocol);
851 free(remote->send_config);
855 gotconfig_free(struct gotconfig *conf)
857 struct gotconfig_remote_repo *remote;
860 while (!TAILQ_EMPTY(&conf->remotes)) {
861 remote = TAILQ_FIRST(&conf->remotes);
862 TAILQ_REMOVE(&conf->remotes, remote, entry);
863 if (remote->fetch_config != NULL)
864 free_fetch_config(remote->fetch_config);
865 if (remote->send_config != NULL)
866 free_send_config(remote->send_config);
868 free(remote->repository);
869 free(remote->server);
870 free(remote->protocol);
876 symset(const char *nam, const char *val, int persist)
880 TAILQ_FOREACH(sym, &symhead, entry) {
881 if (strcmp(nam, sym->nam) == 0)
886 if (sym->persist == 1)
891 TAILQ_REMOVE(&symhead, sym, entry);
895 sym = calloc(1, sizeof(*sym));
899 sym->nam = strdup(nam);
900 if (sym->nam == NULL) {
904 sym->val = strdup(val);
905 if (sym->val == NULL) {
911 sym->persist = persist;
912 TAILQ_INSERT_TAIL(&symhead, sym, entry);
917 cmdline_symset(char *s)
923 val = strrchr(s, '=');
927 len = strlen(s) - strlen(val) + 1;
930 errx(1, "cmdline_symset: malloc");
932 strlcpy(sym, s, len);
934 ret = symset(sym, val + 1, 1);
941 symget(const char *nam)
945 TAILQ_FOREACH(sym, &symhead, entry) {
946 if (strcmp(nam, sym->nam) == 0) {
955 atoul(char *s, u_long *ulvalp)
961 ulval = strtoul(s, &ep, 0);
962 if (s[0] == '\0' || *ep != '\0')
964 if (errno == ERANGE && ulval == ULONG_MAX)