Blob


1 /*
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.
10 *
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.
14 *
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.
22 */
24 %{
25 #include <sys/types.h>
27 #include <netdb.h>
29 #include <ctype.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "gotconfig.h"
43 static struct file {
44 FILE *stream;
45 const char *name;
46 size_t ungetpos;
47 size_t ungetsize;
48 u_char *ungetbuf;
49 int eof_reached;
50 int lineno;
51 } *file;
52 static const struct got_error* newfile(struct file**, const char *, int *);
53 static void closefile(struct file *);
54 int yyparse(void);
55 int yylex(void);
56 int yyerror(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
60 int lookup(char *);
61 int igetc(void);
62 int lgetc(int);
63 void lungetc(int);
64 int findeol(void);
65 static int parseport(char *, long long *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 struct sym {
69 TAILQ_ENTRY(sym) entry;
70 int used;
71 int persist;
72 char *nam;
73 char *val;
74 };
76 int symset(const char *, const char *, int);
77 char *symget(const char *);
79 static int atoul(char *, u_long *);
81 static const struct got_error* gerror;
82 static struct gotconfig_remote_repo *remote;
83 static struct gotconfig gotconfig;
84 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
85 static const struct got_error* new_fetch_config(struct fetch_config **);
86 static const struct got_error* new_send_config(struct send_config **);
88 typedef struct {
89 union {
90 long long number;
91 char *string;
92 struct node_branch *branch;
93 struct node_ref *ref;
94 } v;
95 int lineno;
96 } YYSTYPE;
98 #if defined(__APPLE__) && !defined(YYSTYPE)
99 #warning "Setting YYSTYPE - is GNU Bison installed?"
100 #define YYSTYPE YYSTYPE
101 #endif
102 %}
104 %token ERROR
105 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
106 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
107 %token <v.string> STRING
108 %token <v.number> NUMBER
109 %type <v.number> boolean portplain
110 %type <v.string> numberstring
111 %type <v.branch> branch xbranch branch_list
112 %type <v.ref> ref xref ref_list
114 %%
116 grammar : /* empty */
117 | grammar '\n'
118 | grammar author '\n'
119 | grammar remote '\n'
121 boolean : STRING {
122 if (strcasecmp($1, "true") == 0 ||
123 strcasecmp($1, "yes") == 0)
124 $$ = 1;
125 else if (strcasecmp($1, "false") == 0 ||
126 strcasecmp($1, "no") == 0)
127 $$ = 0;
128 else {
129 yyerror("invalid boolean value '%s'", $1);
130 free($1);
131 YYERROR;
133 free($1);
136 numberstring : NUMBER {
137 char *s;
138 if (asprintf(&s, "%lld", $1) == -1) {
139 yyerror("string: asprintf");
140 YYERROR;
142 $$ = s;
144 | STRING
146 portplain : numberstring {
147 if (parseport($1, &$$) == -1) {
148 free($1);
149 YYERROR;
151 free($1);
154 branch : /* empty */ { $$ = NULL; }
155 | xbranch { $$ = $1; }
156 | '{' optnl branch_list '}' { $$ = $3; }
158 xbranch : STRING {
159 $$ = calloc(1, sizeof(struct node_branch));
160 if ($$ == NULL) {
161 yyerror("calloc");
162 YYERROR;
164 $$->branch_name = $1;
165 $$->tail = $$;
168 branch_list : xbranch optnl { $$ = $1; }
169 | branch_list comma xbranch optnl {
170 $1->tail->next = $3;
171 $1->tail = $3;
172 $$ = $1;
175 ref : /* empty */ { $$ = NULL; }
176 | xref { $$ = $1; }
177 | '{' optnl ref_list '}' { $$ = $3; }
179 xref : STRING {
180 $$ = calloc(1, sizeof(struct node_ref));
181 if ($$ == NULL) {
182 yyerror("calloc");
183 YYERROR;
185 $$->ref_name = $1;
186 $$->tail = $$;
189 ref_list : xref optnl { $$ = $1; }
190 | ref_list comma xref optnl {
191 $1->tail->next = $3;
192 $1->tail = $3;
193 $$ = $1;
196 remoteopts2 : remoteopts2 remoteopts1 nl
197 | remoteopts1 optnl
199 remoteopts1 : REPOSITORY STRING {
200 remote->repository = $2;
202 | SERVER STRING {
203 remote->server = $2;
205 | PROTOCOL STRING {
206 remote->protocol = $2;
208 | MIRROR_REFERENCES boolean {
209 remote->mirror_references = $2;
211 | FETCH_ALL_BRANCHES boolean {
212 remote->fetch_all_branches = $2;
214 | PORT portplain {
215 remote->port = $2;
217 | BRANCH branch {
218 remote->branch = $2;
220 | REFERENCE ref {
221 remote->fetch_ref = $2;
223 | FETCH {
224 static const struct got_error* error;
226 if (remote->fetch_config != NULL) {
227 yyerror("fetch block already exists");
228 YYERROR;
230 error = new_fetch_config(&remote->fetch_config);
231 if (error) {
232 yyerror("%s", error->msg);
233 YYERROR;
235 } '{' optnl fetchempty '}'
236 | SEND {
237 static const struct got_error* error;
239 if (remote->send_config != NULL) {
240 yyerror("send block already exists");
241 YYERROR;
243 error = new_send_config(&remote->send_config);
244 if (error) {
245 yyerror("%s", error->msg);
246 YYERROR;
248 } '{' optnl sendempty '}'
250 fetchempty : /* empty */
251 | fetchopts2
253 fetchopts2 : fetchopts2 fetchopts1 nl
254 | fetchopts1 optnl
256 fetchopts1 : REPOSITORY STRING {
257 remote->fetch_config->repository = $2;
259 | SERVER STRING {
260 remote->fetch_config->server = $2;
262 | PROTOCOL STRING {
263 remote->fetch_config->protocol = $2;
265 | PORT portplain {
266 remote->fetch_config->port = $2;
268 | BRANCH branch {
269 remote->fetch_config->branch = $2;
272 sendempty : /* empty */
273 | sendopts2
275 sendopts2 : sendopts2 sendopts1 nl
276 | sendopts1 optnl
278 sendopts1 : REPOSITORY STRING {
279 remote->send_config->repository = $2;
281 | SERVER STRING {
282 remote->send_config->server = $2;
284 | PROTOCOL STRING {
285 remote->send_config->protocol = $2;
287 | PORT portplain {
288 remote->send_config->port = $2;
290 | BRANCH branch {
291 remote->send_config->branch = $2;
294 remote : REMOTE STRING {
295 static const struct got_error* error;
297 error = new_remote(&remote);
298 if (error) {
299 free($2);
300 yyerror("%s", error->msg);
301 YYERROR;
303 remote->name = $2;
304 } '{' optnl remoteopts2 '}' {
305 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
306 gotconfig.nremotes++;
309 author : AUTHOR STRING {
310 gotconfig.author = $2;
313 optnl : '\n' optnl
314 | /* empty */
316 nl : '\n' optnl
318 comma : ','
319 | /* empty */
321 %%
323 struct keywords {
324 const char *k_name;
325 int k_val;
326 };
328 int
329 yyerror(const char *fmt, ...)
331 va_list ap;
332 char *msg;
333 char *err = NULL;
335 va_start(ap, fmt);
336 if (vasprintf(&msg, fmt, ap) == -1) {
337 gerror = got_error_from_errno("vasprintf");
338 return 0;
340 va_end(ap);
341 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
342 msg) == -1) {
343 gerror = got_error_from_errno("asprintf");
344 return(0);
346 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
347 free(msg);
348 return(0);
350 int
351 kw_cmp(const void *k, const void *e)
353 return (strcmp(k, ((const struct keywords *)e)->k_name));
356 int
357 lookup(char *s)
359 /* This has to be sorted always. */
360 static const struct keywords keywords[] = {
361 {"author", AUTHOR},
362 {"branch", BRANCH},
363 {"fetch", FETCH},
364 {"fetch-all-branches", FETCH_ALL_BRANCHES},
365 {"mirror-references", MIRROR_REFERENCES},
366 {"port", PORT},
367 {"protocol", PROTOCOL},
368 {"reference", REFERENCE},
369 {"remote", REMOTE},
370 {"repository", REPOSITORY},
371 {"send", SEND},
372 {"server", SERVER},
373 };
374 const struct keywords *p;
376 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
377 sizeof(keywords[0]), kw_cmp);
379 if (p)
380 return (p->k_val);
381 else
382 return (STRING);
385 #define START_EXPAND 1
386 #define DONE_EXPAND 2
388 static int expanding;
390 int
391 igetc(void)
393 int c;
395 while (1) {
396 if (file->ungetpos > 0)
397 c = file->ungetbuf[--file->ungetpos];
398 else
399 c = getc(file->stream);
401 if (c == START_EXPAND)
402 expanding = 1;
403 else if (c == DONE_EXPAND)
404 expanding = 0;
405 else
406 break;
408 return (c);
411 int
412 lgetc(int quotec)
414 int c, next;
416 if (quotec) {
417 c = igetc();
418 if (c == EOF) {
419 yyerror("reached end of file while parsing "
420 "quoted string");
422 return (c);
425 c = igetc();
426 while (c == '\\') {
427 next = igetc();
428 if (next != '\n') {
429 c = next;
430 break;
432 yylval.lineno = file->lineno;
433 file->lineno++;
436 return (c);
439 void
440 lungetc(int c)
442 if (c == EOF)
443 return;
445 if (file->ungetpos >= file->ungetsize) {
446 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
447 if (p == NULL)
448 err(1, "%s", __func__);
449 file->ungetbuf = p;
450 file->ungetsize *= 2;
452 file->ungetbuf[file->ungetpos++] = c;
455 int
456 findeol(void)
458 int c;
460 /* Skip to either EOF or the first real EOL. */
461 while (1) {
462 c = lgetc(0);
463 if (c == '\n') {
464 file->lineno++;
465 break;
467 if (c == EOF)
468 break;
470 return (ERROR);
473 static long long
474 getservice(char *n)
476 struct servent *s;
477 u_long ulval;
479 if (atoul(n, &ulval) == 0) {
480 if (ulval == 0 || ulval > 65535) {
481 yyerror("illegal port value %lu", ulval);
482 return (-1);
484 return ulval;
485 } else {
486 s = getservbyname(n, "tcp");
487 if (s == NULL)
488 s = getservbyname(n, "udp");
489 if (s == NULL) {
490 yyerror("unknown port %s", n);
491 return (-1);
493 return (s->s_port);
497 static int
498 parseport(char *port, long long *pn)
500 if ((*pn = getservice(port)) == -1) {
501 *pn = 0LL;
502 return (-1);
504 return (0);
508 int
509 yylex(void)
511 char buf[8096];
512 char *p, *val;
513 int quotec, next, c;
514 int token;
516 top:
517 p = buf;
518 c = lgetc(0);
519 while (c == ' ' || c == '\t')
520 c = lgetc(0); /* nothing */
522 yylval.lineno = file->lineno;
523 if (c == '#') {
524 c = lgetc(0);
525 while (c != '\n' && c != EOF)
526 c = lgetc(0); /* nothing */
528 if (c == '$' && !expanding) {
529 while (1) {
530 c = lgetc(0);
531 if (c == EOF)
532 return (0);
534 if (p + 1 >= buf + sizeof(buf) - 1) {
535 yyerror("string too long");
536 return (findeol());
538 if (isalnum(c) || c == '_') {
539 *p++ = c;
540 continue;
542 *p = '\0';
543 lungetc(c);
544 break;
546 val = symget(buf);
547 if (val == NULL) {
548 yyerror("macro '%s' not defined", buf);
549 return (findeol());
551 p = val + strlen(val) - 1;
552 lungetc(DONE_EXPAND);
553 while (p >= val) {
554 lungetc((unsigned char)*p);
555 p--;
557 lungetc(START_EXPAND);
558 goto top;
561 switch (c) {
562 case '\'':
563 case '"':
564 quotec = c;
565 while (1) {
566 c = lgetc(quotec);
567 if (c == EOF)
568 return (0);
569 if (c == '\n') {
570 file->lineno++;
571 continue;
572 } else if (c == '\\') {
573 next = lgetc(quotec);
574 if (next == EOF)
575 return (0);
576 if (next == quotec || c == ' ' || c == '\t')
577 c = next;
578 else if (next == '\n') {
579 file->lineno++;
580 continue;
581 } else
582 lungetc(next);
583 } else if (c == quotec) {
584 *p = '\0';
585 break;
586 } else if (c == '\0') {
587 yyerror("syntax error");
588 return (findeol());
590 if (p + 1 >= buf + sizeof(buf) - 1) {
591 yyerror("string too long");
592 return (findeol());
594 *p++ = c;
596 yylval.v.string = strdup(buf);
597 if (yylval.v.string == NULL)
598 err(1, "%s", __func__);
599 return (STRING);
602 #define allowed_to_end_number(x) \
603 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
605 if (c == '-' || isdigit(c)) {
606 do {
607 *p++ = c;
608 if ((size_t)(p-buf) >= sizeof(buf)) {
609 yyerror("string too long");
610 return (findeol());
612 c = lgetc(0);
613 } while (c != EOF && isdigit(c));
614 lungetc(c);
615 if (p == buf + 1 && buf[0] == '-')
616 goto nodigits;
617 if (c == EOF || allowed_to_end_number(c)) {
618 const char *errstr = NULL;
620 *p = '\0';
621 yylval.v.number = strtonum(buf, LLONG_MIN,
622 LLONG_MAX, &errstr);
623 if (errstr) {
624 yyerror("\"%s\" invalid number: %s",
625 buf, errstr);
626 return (findeol());
628 return (NUMBER);
629 } else {
630 nodigits:
631 while (p > buf + 1)
632 lungetc((unsigned char)*--p);
633 c = (unsigned char)*--p;
634 if (c == '-')
635 return (c);
639 #define allowed_in_string(x) \
640 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
641 x != '{' && x != '}' && \
642 x != '!' && x != '=' && x != '#' && \
643 x != ','))
645 if (isalnum(c) || c == ':' || c == '_') {
646 do {
647 *p++ = c;
648 if ((size_t)(p-buf) >= sizeof(buf)) {
649 yyerror("string too long");
650 return (findeol());
652 c = lgetc(0);
653 } while (c != EOF && (allowed_in_string(c)));
654 lungetc(c);
655 *p = '\0';
656 token = lookup(buf);
657 if (token == STRING) {
658 yylval.v.string = strdup(buf);
659 if (yylval.v.string == NULL)
660 err(1, "%s", __func__);
662 return (token);
664 if (c == '\n') {
665 yylval.lineno = file->lineno;
666 file->lineno++;
668 if (c == EOF)
669 return (0);
670 return (c);
673 static const struct got_error*
674 newfile(struct file **nfile, const char *filename, int *fd)
676 const struct got_error* error = NULL;
678 (*nfile) = calloc(1, sizeof(struct file));
679 if ((*nfile) == NULL)
680 return got_error_from_errno("calloc");
681 (*nfile)->stream = fdopen(*fd, "r");
682 if ((*nfile)->stream == NULL) {
683 error = got_error_from_errno("fdopen");
684 free((*nfile));
685 return error;
687 *fd = -1; /* Stream owns the file descriptor now. */
688 (*nfile)->name = filename;
689 (*nfile)->lineno = 1;
690 (*nfile)->ungetsize = 16;
691 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
692 if ((*nfile)->ungetbuf == NULL) {
693 error = got_error_from_errno("malloc");
694 fclose((*nfile)->stream);
695 free((*nfile));
696 return error;
698 return NULL;
701 static const struct got_error*
702 new_remote(struct gotconfig_remote_repo **remote)
704 const struct got_error *error = NULL;
706 *remote = calloc(1, sizeof(**remote));
707 if (*remote == NULL)
708 error = got_error_from_errno("calloc");
709 return error;
712 static const struct got_error*
713 new_fetch_config(struct fetch_config **fetch_config)
715 const struct got_error *error = NULL;
717 *fetch_config = calloc(1, sizeof(**fetch_config));
718 if (*fetch_config == NULL)
719 error = got_error_from_errno("calloc");
720 return error;
723 static const struct got_error*
724 new_send_config(struct send_config **send_config)
726 const struct got_error *error = NULL;
728 *send_config = calloc(1, sizeof(**send_config));
729 if (*send_config == NULL)
730 error = got_error_from_errno("calloc");
731 return error;
734 static void
735 closefile(struct file *file)
737 fclose(file->stream);
738 free(file->ungetbuf);
739 free(file);
742 const struct got_error *
743 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
745 const struct got_error *err = NULL;
746 struct sym *sym, *next;
748 *conf = NULL;
750 err = newfile(&file, filename, fd);
751 if (err)
752 return err;
754 TAILQ_INIT(&gotconfig.remotes);
756 yyparse();
757 closefile(file);
759 /* Free macros and check which have not been used. */
760 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
761 if (!sym->persist) {
762 free(sym->nam);
763 free(sym->val);
764 TAILQ_REMOVE(&symhead, sym, entry);
765 free(sym);
769 if (gerror == NULL)
770 *conf = &gotconfig;
771 return gerror;
774 static void
775 free_fetch_config(struct fetch_config *fetch_config)
777 free(remote->fetch_config->repository);
778 free(remote->fetch_config->server);
779 free(remote->fetch_config->protocol);
780 free(remote->fetch_config);
783 static void
784 free_send_config(struct send_config *send_config)
786 free(remote->send_config->repository);
787 free(remote->send_config->server);
788 free(remote->send_config->protocol);
789 free(remote->send_config);
792 void
793 gotconfig_free(struct gotconfig *conf)
795 struct gotconfig_remote_repo *remote;
797 free(conf->author);
798 while (!TAILQ_EMPTY(&conf->remotes)) {
799 remote = TAILQ_FIRST(&conf->remotes);
800 TAILQ_REMOVE(&conf->remotes, remote, entry);
801 if (remote->fetch_config != NULL)
802 free_fetch_config(remote->fetch_config);
803 if (remote->send_config != NULL)
804 free_send_config(remote->send_config);
805 free(remote->name);
806 free(remote->repository);
807 free(remote->server);
808 free(remote->protocol);
809 free(remote);
813 int
814 symset(const char *nam, const char *val, int persist)
816 struct sym *sym;
818 TAILQ_FOREACH(sym, &symhead, entry) {
819 if (strcmp(nam, sym->nam) == 0)
820 break;
823 if (sym != NULL) {
824 if (sym->persist == 1)
825 return (0);
826 else {
827 free(sym->nam);
828 free(sym->val);
829 TAILQ_REMOVE(&symhead, sym, entry);
830 free(sym);
833 sym = calloc(1, sizeof(*sym));
834 if (sym == NULL)
835 return (-1);
837 sym->nam = strdup(nam);
838 if (sym->nam == NULL) {
839 free(sym);
840 return (-1);
842 sym->val = strdup(val);
843 if (sym->val == NULL) {
844 free(sym->nam);
845 free(sym);
846 return (-1);
848 sym->used = 0;
849 sym->persist = persist;
850 TAILQ_INSERT_TAIL(&symhead, sym, entry);
851 return (0);
854 int
855 cmdline_symset(char *s)
857 char *sym, *val;
858 int ret;
859 size_t len;
861 val = strrchr(s, '=');
862 if (val == NULL)
863 return (-1);
865 len = strlen(s) - strlen(val) + 1;
866 sym = malloc(len);
867 if (sym == NULL)
868 errx(1, "cmdline_symset: malloc");
870 strlcpy(sym, s, len);
872 ret = symset(sym, val + 1, 1);
873 free(sym);
875 return (ret);
878 char *
879 symget(const char *nam)
881 struct sym *sym;
883 TAILQ_FOREACH(sym, &symhead, entry) {
884 if (strcmp(nam, sym->nam) == 0) {
885 sym->used = 1;
886 return (sym->val);
889 return (NULL);
892 static int
893 atoul(char *s, u_long *ulvalp)
895 u_long ulval;
896 char *ep;
898 errno = 0;
899 ulval = strtoul(s, &ep, 0);
900 if (s[0] == '\0' || *ep != '\0')
901 return (-1);
902 if (errno == ERANGE && ulval == ULONG_MAX)
903 return (-1);
904 *ulvalp = ulval;
905 return (0);