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"
58 static const struct got_error* newfile(struct file**, const char *);
59 static void closefile(struct file *);
62 int yyerror(const char *, ...)
63 __attribute__((__format__ (printf, 1, 2)))
64 __attribute__((__nonnull__ (1)));
65 int kw_cmp(const void *, const void *);
72 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
74 TAILQ_ENTRY(sym) entry;
81 int symset(const char *, const char *, int);
82 char *symget(const char *);
84 const struct got_error* gerror = NULL;
85 struct gotconfig_remote_repo *remote;
86 struct gotconfig gotconfig;
87 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
100 %token REMOTE REPOSITORY SERVER PROTOCOL USER
101 %token <v.string> STRING
102 %token <v.number> NUMBER
106 grammar : /* empty */
108 | grammar remote '\n'
110 remoteopts2 : remoteopts2 remoteopts1 nl
113 remoteopts1 : REPOSITORY STRING {
114 remote->repository = strdup($2);
115 if (remote->repository == NULL) {
123 remote->server = strdup($2);
124 if (remote->server == NULL) {
132 remote->protocol = strdup($2);
133 if (remote->protocol == NULL) {
141 remote->user = strdup($2);
142 if (remote->user == NULL) {
150 remote : REMOTE STRING {
151 static const struct got_error* error;
153 error = new_remote(&remote);
156 yyerror("%s", error->msg);
159 remote->name = strdup($2);
160 if (remote->name == NULL) {
166 } '{' optnl remoteopts2 '}' {
167 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
168 gotconfig.nremotes++;
184 yyerror(const char *fmt, ...)
191 if (vasprintf(&msg, fmt, ap) == -1) {
192 gerror = got_error_from_errno("vasprintf");
196 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
197 gerror = got_error_from_errno("asprintf");
200 gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
207 kw_cmp(const void *k, const void *e)
209 return (strcmp(k, ((const struct keywords *)e)->k_name));
215 /* This has to be sorted always. */
216 static const struct keywords keywords[] = {
217 {"protocol", PROTOCOL},
219 {"repository", REPOSITORY},
223 const struct keywords *p;
225 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
226 sizeof(keywords[0]), kw_cmp);
234 #define START_EXPAND 1
235 #define DONE_EXPAND 2
237 static int expanding;
245 if (file->ungetpos > 0)
246 c = file->ungetbuf[--file->ungetpos];
248 c = getc(file->stream);
250 if (c == START_EXPAND)
252 else if (c == DONE_EXPAND)
268 yyerror("reached end of file while parsing "
281 yylval.lineno = file->lineno;
294 if (file->ungetpos >= file->ungetsize) {
295 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
297 err(1, "%s", __func__);
299 file->ungetsize *= 2;
301 file->ungetbuf[file->ungetpos++] = c;
309 /* Skip to either EOF or the first real EOL. */
325 unsigned char buf[8096];
326 unsigned char *p, *val;
333 while (c == ' ' || c == '\t')
334 c = lgetc(0); /* nothing */
336 yylval.lineno = file->lineno;
339 while (c != '\n' && c != EOF)
340 c = lgetc(0); /* nothing */
342 if (c == '$' && !expanding) {
348 if (p + 1 >= buf + sizeof(buf) - 1) {
349 yyerror("string too long");
352 if (isalnum(c) || c == '_') {
362 yyerror("macro '%s' not defined", buf);
365 p = val + strlen(val) - 1;
366 lungetc(DONE_EXPAND);
371 lungetc(START_EXPAND);
386 } else if (c == '\\') {
387 next = lgetc(quotec);
390 if (next == quotec || c == ' ' || c == '\t')
392 else if (next == '\n') {
397 } else if (c == quotec) {
400 } else if (c == '\0') {
401 yyerror("syntax error");
404 if (p + 1 >= buf + sizeof(buf) - 1) {
405 yyerror("string too long");
410 yylval.v.string = strdup(buf);
411 if (yylval.v.string == NULL)
412 err(1, "%s", __func__);
416 #define allowed_to_end_number(x) \
417 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
419 if (c == '-' || isdigit(c)) {
422 if ((unsigned)(p-buf) >= sizeof(buf)) {
423 yyerror("string too long");
427 } while (c != EOF && isdigit(c));
429 if (p == buf + 1 && buf[0] == '-')
431 if (c == EOF || allowed_to_end_number(c)) {
432 const char *errstr = NULL;
435 yylval.v.number = strtonum(buf, LLONG_MIN,
438 yyerror("\"%s\" invalid number: %s",
453 #define allowed_in_string(x) \
454 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
455 x != '{' && x != '}' && \
456 x != '!' && x != '=' && x != '#' && \
459 if (isalnum(c) || c == ':' || c == '_') {
462 if ((unsigned)(p-buf) >= sizeof(buf)) {
463 yyerror("string too long");
467 } while (c != EOF && (allowed_in_string(c)));
471 if (token == STRING) {
472 yylval.v.string = strdup(buf);
473 if (yylval.v.string == NULL)
474 err(1, "%s", __func__);
479 yylval.lineno = file->lineno;
487 static const struct got_error*
488 newfile(struct file **nfile, const char *name)
490 const struct got_error* error = NULL;
492 (*nfile) = calloc(1, sizeof(struct file));
493 if ((*nfile) == NULL)
494 return got_error_from_errno2(__func__, "calloc");
495 (*nfile)->name = strdup(name);
496 if ((*nfile)->name == NULL) {
498 return got_error_from_errno2(__func__, "strdup");
500 (*nfile)->stream = fopen((*nfile)->name, "r");
501 if ((*nfile)->stream == NULL) {
503 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
504 return got_error_from_errno("asprintf");
505 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
506 free((*nfile)->name);
511 (*nfile)->lineno = 1;
512 (*nfile)->ungetsize = 16;
513 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
514 if ((*nfile)->ungetbuf == NULL) {
515 fclose((*nfile)->stream);
516 free((*nfile)->name);
518 return got_error_from_errno2(__func__, "malloc");
523 static const struct got_error*
524 new_remote(struct gotconfig_remote_repo **remote)
526 const struct got_error *error = NULL;
528 *remote = calloc(1, sizeof(**remote));
530 error = got_error_from_errno("calloc");
535 closefile(struct file *file)
537 fclose(file->stream);
539 free(file->ungetbuf);
543 const struct got_error*
544 gotconfig_parse(struct gotconfig **conf, const char *filename)
546 struct sym *sym, *next;
551 * We don't require that gotconfig exists
552 * So, null gerror and goto done
554 gerror = newfile(&file, filename);
555 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
561 TAILQ_INIT(&gotconfig.remotes);
566 /* Free macros and check which have not been used. */
567 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
571 TAILQ_REMOVE(&symhead, sym, entry);
581 gotconfig_free(struct gotconfig *conf)
583 struct gotconfig_remote_repo *remote;
585 while (!TAILQ_EMPTY(&conf->remotes)) {
586 remote = TAILQ_FIRST(&conf->remotes);
587 TAILQ_REMOVE(&conf->remotes, remote, entry);
589 free(remote->repository);
590 free(remote->server);
591 free(remote->protocol);
598 symset(const char *nam, const char *val, int persist)
602 TAILQ_FOREACH(sym, &symhead, entry) {
603 if (strcmp(nam, sym->nam) == 0)
608 if (sym->persist == 1)
613 TAILQ_REMOVE(&symhead, sym, entry);
617 sym = calloc(1, sizeof(*sym));
621 sym->nam = strdup(nam);
622 if (sym->nam == NULL) {
626 sym->val = strdup(val);
627 if (sym->val == NULL) {
633 sym->persist = persist;
634 TAILQ_INSERT_TAIL(&symhead, sym, entry);
639 cmdline_symset(char *s)
645 val = strrchr(s, '=');
649 len = strlen(s) - strlen(val) + 1;
652 errx(1, "cmdline_symset: malloc");
654 strlcpy(sym, s, len);
656 ret = symset(sym, val + 1, 1);
663 symget(const char *nam)
667 TAILQ_FOREACH(sym, &symhead, entry) {
668 if (strcmp(nam, sym->nam) == 0) {