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>
26 #include <sys/queue.h>
28 #include <netdb.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "got_error.h"
40 #include "gotconfig.h"
42 static struct file {
43 FILE *stream;
44 const char *name;
45 size_t ungetpos;
46 size_t ungetsize;
47 u_char *ungetbuf;
48 int eof_reached;
49 int lineno;
50 } *file;
51 static const struct got_error* newfile(struct file**, const char *, int *);
52 static void closefile(struct file *);
53 int yyparse(void);
54 int yylex(void);
55 int yyerror(const char *, ...)
56 __attribute__((__format__ (printf, 1, 2)))
57 __attribute__((__nonnull__ (1)));
58 int kw_cmp(const void *, const void *);
59 int lookup(char *);
60 int igetc(void);
61 int lgetc(int);
62 void lungetc(int);
63 int findeol(void);
64 static int parseport(char *, long long *);
66 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
67 struct sym {
68 TAILQ_ENTRY(sym) entry;
69 int used;
70 int persist;
71 char *nam;
72 char *val;
73 };
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(struct fetch_repo **);
85 static const struct got_error* new_send(struct send_repo **);
87 typedef struct {
88 union {
89 long long number;
90 char *string;
91 struct node_branch *branch;
92 struct node_ref *ref;
93 } v;
94 int lineno;
95 } YYSTYPE;
97 %}
99 %token ERROR
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
109 %%
111 grammar : /* empty */
112 | grammar '\n'
113 | grammar author '\n'
114 | grammar remote '\n'
116 boolean : STRING {
117 if (strcasecmp($1, "true") == 0 ||
118 strcasecmp($1, "yes") == 0)
119 $$ = 1;
120 else if (strcasecmp($1, "false") == 0 ||
121 strcasecmp($1, "no") == 0)
122 $$ = 0;
123 else {
124 yyerror("invalid boolean value '%s'", $1);
125 free($1);
126 YYERROR;
128 free($1);
131 numberstring : NUMBER {
132 char *s;
133 if (asprintf(&s, "%lld", $1) == -1) {
134 yyerror("string: asprintf");
135 YYERROR;
137 $$ = s;
139 | STRING
141 portplain : numberstring {
142 if (parseport($1, &$$) == -1) {
143 free($1);
144 YYERROR;
146 free($1);
149 branch : /* empty */ { $$ = NULL; }
150 | xbranch { $$ = $1; }
151 | '{' optnl branch_list '}' { $$ = $3; }
153 xbranch : STRING {
154 $$ = calloc(1, sizeof(struct node_branch));
155 if ($$ == NULL) {
156 yyerror("calloc");
157 YYERROR;
159 $$->branch_name = $1;
160 $$->tail = $$;
163 branch_list : xbranch optnl { $$ = $1; }
164 | branch_list comma xbranch optnl {
165 $1->tail->next = $3;
166 $1->tail = $3;
167 $$ = $1;
170 ref : /* empty */ { $$ = NULL; }
171 | xref { $$ = $1; }
172 | '{' optnl ref_list '}' { $$ = $3; }
174 xref : STRING {
175 $$ = calloc(1, sizeof(struct node_ref));
176 if ($$ == NULL) {
177 yyerror("calloc");
178 YYERROR;
180 $$->ref_name = $1;
181 $$->tail = $$;
184 ref_list : xref optnl { $$ = $1; }
185 | ref_list comma xref optnl {
186 $1->tail->next = $3;
187 $1->tail = $3;
188 $$ = $1;
191 remoteopts2 : remoteopts2 remoteopts1 nl
192 | remoteopts1 optnl
194 remoteopts1 : REPOSITORY STRING {
195 remote->repository = strdup($2);
196 if (remote->repository == NULL) {
197 free($2);
198 yyerror("strdup");
199 YYERROR;
201 free($2);
203 | SERVER STRING {
204 remote->server = strdup($2);
205 if (remote->server == NULL) {
206 free($2);
207 yyerror("strdup");
208 YYERROR;
210 free($2);
212 | PROTOCOL STRING {
213 remote->protocol = strdup($2);
214 if (remote->protocol == NULL) {
215 free($2);
216 yyerror("strdup");
217 YYERROR;
219 free($2);
221 | MIRROR_REFERENCES boolean {
222 remote->mirror_references = $2;
224 | FETCH_ALL_BRANCHES boolean {
225 remote->fetch_all_branches = $2;
227 | PORT portplain {
228 remote->port = $2;
230 | BRANCH branch {
231 remote->branch = $2;
233 | REFERENCE ref {
234 remote->ref = $2;
236 | FETCH fetch
237 | SEND send
239 fetchopts2 : fetchopts2 fetchopts1 nl
240 | fetchopts1 optnl
242 fetchopts1 : REPOSITORY STRING {
243 remote->fetch_repo->fetch_repository = strdup($2);
244 if (remote->fetch_repo->fetch_repository == NULL) {
245 free($2);
246 yyerror("strdup");
247 YYERROR;
249 free($2);
251 | SERVER STRING {
252 remote->fetch_repo->fetch_server = strdup($2);
253 if (remote->fetch_repo->fetch_server == NULL) {
254 free($2);
255 yyerror("strdup");
256 YYERROR;
258 free($2);
260 | PROTOCOL STRING {
261 remote->fetch_repo->fetch_protocol = strdup($2);
262 if (remote->fetch_repo->fetch_protocol == NULL) {
263 free($2);
264 yyerror("strdup");
265 YYERROR;
267 free($2);
269 | PORT portplain {
270 remote->fetch_repo->fetch_port = $2;
272 | BRANCH branch {
273 remote->fetch_repo->fetch_branch = $2;
276 fetch : {
277 static const struct got_error* error;
279 if (remote->fetch_repo != NULL) {
280 yyerror("fetch block already exists");
281 YYERROR;
283 error = new_fetch(&remote->fetch_repo);
284 if (error) {
285 yyerror("%s", error->msg);
286 YYERROR;
288 } '{' optnl fetchopts2 '}'
290 sendopts2 : sendopts2 sendopts1 nl
291 | sendopts1 optnl
293 sendopts1 : REPOSITORY STRING {
294 remote->send_repo->send_repository = strdup($2);
295 if (remote->send_repo->send_repository == NULL) {
296 free($2);
297 yyerror("strdup");
298 YYERROR;
300 free($2);
302 | SERVER STRING {
303 remote->send_repo->send_server = strdup($2);
304 if (remote->send_repo->send_server == NULL) {
305 free($2);
306 yyerror("strdup");
307 YYERROR;
309 free($2);
311 | PROTOCOL STRING {
312 remote->send_repo->send_protocol = strdup($2);
313 if (remote->send_repo->send_protocol == NULL) {
314 free($2);
315 yyerror("strdup");
316 YYERROR;
318 free($2);
320 | PORT portplain {
321 remote->send_repo->send_port = $2;
323 | BRANCH branch {
324 remote->send_repo->send_branch = $2;
327 send : {
328 static const struct got_error* error;
330 if (remote->send_repo != NULL) {
331 yyerror("send block already exists");
332 YYERROR;
334 error = new_send(&remote->send_repo);
335 if (error) {
336 yyerror("%s", error->msg);
337 YYERROR;
339 } '{' optnl sendopts2 '}'
341 remote : REMOTE STRING {
342 static const struct got_error* error;
344 error = new_remote(&remote);
345 if (error) {
346 free($2);
347 yyerror("%s", error->msg);
348 YYERROR;
350 remote->name = strdup($2);
351 if (remote->name == NULL) {
352 free($2);
353 yyerror("strdup");
354 YYERROR;
356 free($2);
357 } '{' optnl remoteopts2 '}' {
358 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
359 gotconfig.nremotes++;
362 author : AUTHOR STRING {
363 gotconfig.author = strdup($2);
364 if (gotconfig.author == NULL) {
365 free($2);
366 yyerror("strdup");
367 YYERROR;
369 free($2);
372 optnl : '\n' optnl
373 | /* empty */
375 nl : '\n' optnl
377 comma : ','
378 | /* empty */
380 %%
382 struct keywords {
383 const char *k_name;
384 int k_val;
385 };
387 int
388 yyerror(const char *fmt, ...)
390 va_list ap;
391 char *msg;
392 char *err = NULL;
394 va_start(ap, fmt);
395 if (vasprintf(&msg, fmt, ap) == -1) {
396 gerror = got_error_from_errno("vasprintf");
397 return 0;
399 va_end(ap);
400 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
401 msg) == -1) {
402 gerror = got_error_from_errno("asprintf");
403 return(0);
405 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
406 free(msg);
407 free(err);
408 return(0);
410 int
411 kw_cmp(const void *k, const void *e)
413 return (strcmp(k, ((const struct keywords *)e)->k_name));
416 int
417 lookup(char *s)
419 /* This has to be sorted always. */
420 static const struct keywords keywords[] = {
421 {"author", AUTHOR},
422 {"branch", BRANCH},
423 {"fetch", FETCH},
424 {"fetch-all-branches", FETCH_ALL_BRANCHES},
425 {"mirror-references", MIRROR_REFERENCES},
426 {"port", PORT},
427 {"protocol", PROTOCOL},
428 {"reference", REFERENCE},
429 {"remote", REMOTE},
430 {"repository", REPOSITORY},
431 {"send", SEND},
432 {"server", SERVER},
433 };
434 const struct keywords *p;
436 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
437 sizeof(keywords[0]), kw_cmp);
439 if (p)
440 return (p->k_val);
441 else
442 return (STRING);
445 #define START_EXPAND 1
446 #define DONE_EXPAND 2
448 static int expanding;
450 int
451 igetc(void)
453 int c;
455 while (1) {
456 if (file->ungetpos > 0)
457 c = file->ungetbuf[--file->ungetpos];
458 else
459 c = getc(file->stream);
461 if (c == START_EXPAND)
462 expanding = 1;
463 else if (c == DONE_EXPAND)
464 expanding = 0;
465 else
466 break;
468 return (c);
471 int
472 lgetc(int quotec)
474 int c, next;
476 if (quotec) {
477 c = igetc();
478 if (c == EOF) {
479 yyerror("reached end of file while parsing "
480 "quoted string");
482 return (c);
485 c = igetc();
486 while (c == '\\') {
487 next = igetc();
488 if (next != '\n') {
489 c = next;
490 break;
492 yylval.lineno = file->lineno;
493 file->lineno++;
496 return (c);
499 void
500 lungetc(int c)
502 if (c == EOF)
503 return;
505 if (file->ungetpos >= file->ungetsize) {
506 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
507 if (p == NULL)
508 err(1, "%s", __func__);
509 file->ungetbuf = p;
510 file->ungetsize *= 2;
512 file->ungetbuf[file->ungetpos++] = c;
515 int
516 findeol(void)
518 int c;
520 /* Skip to either EOF or the first real EOL. */
521 while (1) {
522 c = lgetc(0);
523 if (c == '\n') {
524 file->lineno++;
525 break;
527 if (c == EOF)
528 break;
530 return (ERROR);
533 static long long
534 getservice(char *n)
536 struct servent *s;
537 u_long ulval;
539 if (atoul(n, &ulval) == 0) {
540 if (ulval > 65535) {
541 yyerror("illegal port value %lu", ulval);
542 return (-1);
544 return ulval;
545 } else {
546 s = getservbyname(n, "tcp");
547 if (s == NULL)
548 s = getservbyname(n, "udp");
549 if (s == NULL) {
550 yyerror("unknown port %s", n);
551 return (-1);
553 return (s->s_port);
557 static int
558 parseport(char *port, long long *pn)
560 if ((*pn = getservice(port)) == -1) {
561 *pn = 0LL;
562 return (-1);
564 return (0);
568 int
569 yylex(void)
571 unsigned char buf[8096];
572 unsigned char *p, *val;
573 int quotec, next, c;
574 int token;
576 top:
577 p = buf;
578 c = lgetc(0);
579 while (c == ' ' || c == '\t')
580 c = lgetc(0); /* nothing */
582 yylval.lineno = file->lineno;
583 if (c == '#') {
584 c = lgetc(0);
585 while (c != '\n' && c != EOF)
586 c = lgetc(0); /* nothing */
588 if (c == '$' && !expanding) {
589 while (1) {
590 c = lgetc(0);
591 if (c == EOF)
592 return (0);
594 if (p + 1 >= buf + sizeof(buf) - 1) {
595 yyerror("string too long");
596 return (findeol());
598 if (isalnum(c) || c == '_') {
599 *p++ = c;
600 continue;
602 *p = '\0';
603 lungetc(c);
604 break;
606 val = symget(buf);
607 if (val == NULL) {
608 yyerror("macro '%s' not defined", buf);
609 return (findeol());
611 p = val + strlen(val) - 1;
612 lungetc(DONE_EXPAND);
613 while (p >= val) {
614 lungetc(*p);
615 p--;
617 lungetc(START_EXPAND);
618 goto top;
621 switch (c) {
622 case '\'':
623 case '"':
624 quotec = c;
625 while (1) {
626 c = lgetc(quotec);
627 if (c == EOF)
628 return (0);
629 if (c == '\n') {
630 file->lineno++;
631 continue;
632 } else if (c == '\\') {
633 next = lgetc(quotec);
634 if (next == EOF)
635 return (0);
636 if (next == quotec || c == ' ' || c == '\t')
637 c = next;
638 else if (next == '\n') {
639 file->lineno++;
640 continue;
641 } else
642 lungetc(next);
643 } else if (c == quotec) {
644 *p = '\0';
645 break;
646 } else if (c == '\0') {
647 yyerror("syntax error");
648 return (findeol());
650 if (p + 1 >= buf + sizeof(buf) - 1) {
651 yyerror("string too long");
652 return (findeol());
654 *p++ = c;
656 yylval.v.string = strdup(buf);
657 if (yylval.v.string == NULL)
658 err(1, "%s", __func__);
659 return (STRING);
662 #define allowed_to_end_number(x) \
663 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
665 if (c == '-' || isdigit(c)) {
666 do {
667 *p++ = c;
668 if ((unsigned)(p-buf) >= sizeof(buf)) {
669 yyerror("string too long");
670 return (findeol());
672 c = lgetc(0);
673 } while (c != EOF && isdigit(c));
674 lungetc(c);
675 if (p == buf + 1 && buf[0] == '-')
676 goto nodigits;
677 if (c == EOF || allowed_to_end_number(c)) {
678 const char *errstr = NULL;
680 *p = '\0';
681 yylval.v.number = strtonum(buf, LLONG_MIN,
682 LLONG_MAX, &errstr);
683 if (errstr) {
684 yyerror("\"%s\" invalid number: %s",
685 buf, errstr);
686 return (findeol());
688 return (NUMBER);
689 } else {
690 nodigits:
691 while (p > buf + 1)
692 lungetc(*--p);
693 c = *--p;
694 if (c == '-')
695 return (c);
699 #define allowed_in_string(x) \
700 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
701 x != '{' && x != '}' && \
702 x != '!' && x != '=' && x != '#' && \
703 x != ','))
705 if (isalnum(c) || c == ':' || c == '_') {
706 do {
707 *p++ = c;
708 if ((unsigned)(p-buf) >= sizeof(buf)) {
709 yyerror("string too long");
710 return (findeol());
712 c = lgetc(0);
713 } while (c != EOF && (allowed_in_string(c)));
714 lungetc(c);
715 *p = '\0';
716 token = lookup(buf);
717 if (token == STRING) {
718 yylval.v.string = strdup(buf);
719 if (yylval.v.string == NULL)
720 err(1, "%s", __func__);
722 return (token);
724 if (c == '\n') {
725 yylval.lineno = file->lineno;
726 file->lineno++;
728 if (c == EOF)
729 return (0);
730 return (c);
733 static const struct got_error*
734 newfile(struct file **nfile, const char *filename, int *fd)
736 const struct got_error* error = NULL;
738 (*nfile) = calloc(1, sizeof(struct file));
739 if ((*nfile) == NULL)
740 return got_error_from_errno("calloc");
741 (*nfile)->stream = fdopen(*fd, "r");
742 if ((*nfile)->stream == NULL) {
743 error = got_error_from_errno("fdopen");
744 free((*nfile));
745 return error;
747 *fd = -1; /* Stream owns the file descriptor now. */
748 (*nfile)->name = filename;
749 (*nfile)->lineno = 1;
750 (*nfile)->ungetsize = 16;
751 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
752 if ((*nfile)->ungetbuf == NULL) {
753 error = got_error_from_errno("malloc");
754 fclose((*nfile)->stream);
755 free((*nfile));
756 return error;
758 return NULL;
761 static const struct got_error*
762 new_remote(struct gotconfig_remote_repo **remote)
764 const struct got_error *error = NULL;
766 *remote = calloc(1, sizeof(**remote));
767 if (*remote == NULL)
768 error = got_error_from_errno("calloc");
769 return error;
772 static const struct got_error*
773 new_fetch(struct fetch_repo **fetch_repo)
775 const struct got_error *error = NULL;
777 *fetch_repo = calloc(1, sizeof(**fetch_repo));
778 if (*fetch_repo == NULL)
779 error = got_error_from_errno("calloc");
780 return error;
783 static const struct got_error*
784 new_send(struct send_repo **send_repo)
786 const struct got_error *error = NULL;
788 *send_repo = calloc(1, sizeof(**send_repo));
789 if (*send_repo == NULL)
790 error = got_error_from_errno("calloc");
791 return error;
794 static void
795 closefile(struct file *file)
797 fclose(file->stream);
798 free(file->ungetbuf);
799 free(file);
802 const struct got_error *
803 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
805 const struct got_error *err = NULL;
806 struct sym *sym, *next;
808 *conf = NULL;
810 err = newfile(&file, filename, fd);
811 if (err)
812 return err;
814 TAILQ_INIT(&gotconfig.remotes);
816 yyparse();
817 closefile(file);
819 /* Free macros and check which have not been used. */
820 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
821 if (!sym->persist) {
822 free(sym->nam);
823 free(sym->val);
824 TAILQ_REMOVE(&symhead, sym, entry);
825 free(sym);
829 if (gerror == NULL)
830 *conf = &gotconfig;
831 return gerror;
834 void
835 gotconfig_free(struct gotconfig *conf)
837 struct gotconfig_remote_repo *remote;
839 free(conf->author);
840 while (!TAILQ_EMPTY(&conf->remotes)) {
841 remote = TAILQ_FIRST(&conf->remotes);
842 TAILQ_REMOVE(&conf->remotes, remote, entry);
843 if (remote->fetch_repo != NULL) {
844 free(remote->fetch_repo->fetch_name);
845 free(remote->fetch_repo->fetch_repository);
846 free(remote->fetch_repo->fetch_server);
847 free(remote->fetch_repo->fetch_protocol);
849 if (remote->send_repo != NULL) {
850 free(remote->send_repo->send_name);
851 free(remote->send_repo->send_repository);
852 free(remote->send_repo->send_server);
853 free(remote->send_repo->send_protocol);
855 free(remote->name);
856 free(remote->repository);
857 free(remote->server);
858 free(remote->protocol);
859 free(remote);
863 int
864 symset(const char *nam, const char *val, int persist)
866 struct sym *sym;
868 TAILQ_FOREACH(sym, &symhead, entry) {
869 if (strcmp(nam, sym->nam) == 0)
870 break;
873 if (sym != NULL) {
874 if (sym->persist == 1)
875 return (0);
876 else {
877 free(sym->nam);
878 free(sym->val);
879 TAILQ_REMOVE(&symhead, sym, entry);
880 free(sym);
883 sym = calloc(1, sizeof(*sym));
884 if (sym == NULL)
885 return (-1);
887 sym->nam = strdup(nam);
888 if (sym->nam == NULL) {
889 free(sym);
890 return (-1);
892 sym->val = strdup(val);
893 if (sym->val == NULL) {
894 free(sym->nam);
895 free(sym);
896 return (-1);
898 sym->used = 0;
899 sym->persist = persist;
900 TAILQ_INSERT_TAIL(&symhead, sym, entry);
901 return (0);
904 int
905 cmdline_symset(char *s)
907 char *sym, *val;
908 int ret;
909 size_t len;
911 val = strrchr(s, '=');
912 if (val == NULL)
913 return (-1);
915 len = strlen(s) - strlen(val) + 1;
916 sym = malloc(len);
917 if (sym == NULL)
918 errx(1, "cmdline_symset: malloc");
920 strlcpy(sym, s, len);
922 ret = symset(sym, val + 1, 1);
923 free(sym);
925 return (ret);
928 char *
929 symget(const char *nam)
931 struct sym *sym;
933 TAILQ_FOREACH(sym, &symhead, entry) {
934 if (strcmp(nam, sym->nam) == 0) {
935 sym->used = 1;
936 return (sym->val);
939 return (NULL);
942 static int
943 atoul(char *s, u_long *ulvalp)
945 u_long ulval;
946 char *ep;
948 errno = 0;
949 ulval = strtoul(s, &ep, 0);
950 if (s[0] == '\0' || *ep != '\0')
951 return (-1);
952 if (errno == ERANGE && ulval == ULONG_MAX)
953 return (-1);
954 *ulvalp = ulval;
955 return (0);