2 * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
3 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
31 #include <arpa/inet.h>
46 #include "got_error.h"
49 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
51 TAILQ_ENTRY(file) entry;
60 static const struct got_error* pushfile(struct file**, const char *);
64 int yyerror(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
76 TAILQ_ENTRY(sym) entry;
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
86 const struct got_error* gerror = NULL;
87 struct gotconfig_remote_repo *remote;
88 struct gotconfig gotconfig;
89 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
102 %token REMOTE REPOSITORY SERVER PROTOCOL USER
103 %token <v.string> STRING
104 %token <v.number> NUMBER
108 grammar : /* empty */
110 | grammar remote '\n'
112 remoteopts2 : remoteopts2 remoteopts1 nl
115 remoteopts1 : REPOSITORY STRING {
116 remote->repository = strdup($2);
117 if (remote->repository == NULL) {
125 remote->server = strdup($2);
126 if (remote->server == NULL) {
134 remote->protocol = strdup($2);
135 if (remote->protocol == NULL) {
143 remote->user = strdup($2);
144 if (remote->user == NULL) {
152 remote : REMOTE STRING {
153 static const struct got_error* error;
155 error = new_remote(&remote);
158 yyerror("%s", error->msg);
161 remote->name = strdup($2);
162 if (remote->name == NULL) {
168 } '{' optnl remoteopts2 '}' {
169 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
170 gotconfig.nremotes++;
186 yyerror(const char *fmt, ...)
193 if (vasprintf(&msg, fmt, ap) == -1) {
194 gerror = got_error_from_errno("vasprintf");
198 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
199 gerror = got_error_from_errno("asprintf");
202 gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
209 kw_cmp(const void *k, const void *e)
211 return (strcmp(k, ((const struct keywords *)e)->k_name));
217 /* This has to be sorted always. */
218 static const struct keywords keywords[] = {
219 {"protocol", PROTOCOL},
221 {"repository", REPOSITORY},
225 const struct keywords *p;
227 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
228 sizeof(keywords[0]), kw_cmp);
236 #define START_EXPAND 1
237 #define DONE_EXPAND 2
239 static int expanding;
247 if (file->ungetpos > 0)
248 c = file->ungetbuf[--file->ungetpos];
250 c = getc(file->stream);
252 if (c == START_EXPAND)
254 else if (c == DONE_EXPAND)
268 if ((c = igetc()) == EOF) {
269 yyerror("reached end of file while parsing "
271 if (file == topfile || popfile() == EOF)
278 while ((c = igetc()) == '\\') {
284 yylval.lineno = file->lineno;
290 * Fake EOL when hit EOF for the first time. This gets line
291 * count right if last line in included file is syntactically
292 * invalid and has no newline.
294 if (file->eof_reached == 0) {
295 file->eof_reached = 1;
299 if (file == topfile || popfile() == EOF)
313 if (file->ungetpos >= file->ungetsize) {
314 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
316 err(1, "%s", __func__);
318 file->ungetsize *= 2;
320 file->ungetbuf[file->ungetpos++] = c;
328 /* Skip to either EOF or the first real EOL. */
344 unsigned char buf[8096];
345 unsigned char *p, *val;
351 while ((c = lgetc(0)) == ' ' || c == '\t')
354 yylval.lineno = file->lineno;
356 while ((c = lgetc(0)) != '\n' && c != EOF)
358 if (c == '$' && !expanding) {
360 if ((c = lgetc(0)) == EOF)
363 if (p + 1 >= buf + sizeof(buf) - 1) {
364 yyerror("string too long");
367 if (isalnum(c) || c == '_') {
377 yyerror("macro '%s' not defined", buf);
380 p = val + strlen(val) - 1;
381 lungetc(DONE_EXPAND);
386 lungetc(START_EXPAND);
395 if ((c = lgetc(quotec)) == EOF)
400 } else if (c == '\\') {
401 if ((next = lgetc(quotec)) == EOF)
403 if (next == quotec || c == ' ' || c == '\t')
405 else if (next == '\n') {
410 } else if (c == quotec) {
413 } else if (c == '\0') {
414 yyerror("syntax error");
417 if (p + 1 >= buf + sizeof(buf) - 1) {
418 yyerror("string too long");
423 yylval.v.string = strdup(buf);
424 if (yylval.v.string == NULL)
425 err(1, "%s", __func__);
429 #define allowed_to_end_number(x) \
430 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
432 if (c == '-' || isdigit(c)) {
435 if ((unsigned)(p-buf) >= sizeof(buf)) {
436 yyerror("string too long");
439 } while ((c = lgetc(0)) != EOF && isdigit(c));
441 if (p == buf + 1 && buf[0] == '-')
443 if (c == EOF || allowed_to_end_number(c)) {
444 const char *errstr = NULL;
447 yylval.v.number = strtonum(buf, LLONG_MIN,
450 yyerror("\"%s\" invalid number: %s",
465 #define allowed_in_string(x) \
466 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
467 x != '{' && x != '}' && \
468 x != '!' && x != '=' && x != '#' && \
471 if (isalnum(c) || c == ':' || c == '_') {
474 if ((unsigned)(p-buf) >= sizeof(buf)) {
475 yyerror("string too long");
478 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
481 if ((token = lookup(buf)) == STRING)
482 if ((yylval.v.string = strdup(buf)) == NULL)
483 err(1, "%s", __func__);
487 yylval.lineno = file->lineno;
495 static const struct got_error*
496 pushfile(struct file **nfile, const char *name)
498 const struct got_error* error = NULL;
500 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
501 return got_error_from_errno2(__func__, "calloc");
502 if (((*nfile)->name = strdup(name)) == NULL) {
504 return got_error_from_errno2(__func__, "strdup");
506 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
508 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
509 return got_error_from_errno("asprintf");
510 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
511 free((*nfile)->name);
516 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
517 (*nfile)->ungetsize = 16;
518 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
519 if ((*nfile)->ungetbuf == NULL) {
520 fclose((*nfile)->stream);
521 free((*nfile)->name);
523 return got_error_from_errno2(__func__, "malloc");
525 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
529 static const struct got_error*
530 new_remote(struct gotconfig_remote_repo **remote)
532 const struct got_error *error = NULL;
534 *remote = calloc(1, sizeof(**remote));
536 error = got_error_from_errno("calloc");
543 struct file *prev = NULL;
545 TAILQ_REMOVE(&files, file, entry);
546 fclose(file->stream);
548 free(file->ungetbuf);
551 return (file ? 0 : EOF);
554 const struct got_error*
555 gotconfig_parse(struct gotconfig **conf, const char *filename)
557 struct sym *sym, *next;
562 * We don't require that gotconfig exists
563 * So, null gerror and goto done
565 gerror = pushfile(&file, filename);
566 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
572 TAILQ_INIT(&gotconfig.remotes);
578 /* Free macros and check which have not been used. */
579 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
583 TAILQ_REMOVE(&symhead, sym, entry);
593 gotconfig_free(struct gotconfig *conf)
595 struct gotconfig_remote_repo *remote;
597 while (!TAILQ_EMPTY(&conf->remotes)) {
598 remote = TAILQ_FIRST(&conf->remotes);
599 TAILQ_REMOVE(&conf->remotes, remote, entry);
601 free(remote->repository);
602 free(remote->server);
603 free(remote->protocol);
610 symset(const char *nam, const char *val, int persist)
614 TAILQ_FOREACH(sym, &symhead, entry) {
615 if (strcmp(nam, sym->nam) == 0)
620 if (sym->persist == 1)
625 TAILQ_REMOVE(&symhead, sym, entry);
629 if ((sym = calloc(1, sizeof(*sym))) == NULL)
632 sym->nam = strdup(nam);
633 if (sym->nam == NULL) {
637 sym->val = strdup(val);
638 if (sym->val == NULL) {
644 sym->persist = persist;
645 TAILQ_INSERT_TAIL(&symhead, sym, entry);
650 cmdline_symset(char *s)
656 if ((val = strrchr(s, '=')) == NULL)
659 len = strlen(s) - strlen(val) + 1;
660 if ((sym = malloc(len)) == NULL)
661 errx(1, "cmdline_symset: malloc");
663 strlcpy(sym, s, len);
665 ret = symset(sym, val + 1, 1);
672 symget(const char *nam)
676 TAILQ_FOREACH(sym, &symhead, entry) {
677 if (strcmp(nam, sym->nam) == 0) {