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 {
237 static const struct got_error* error;
239 if (remote->fetch_repo != NULL) {
240 yyerror("fetch block already exists");
241 YYERROR;
243 error = new_fetch(&remote->fetch_repo);
244 if (error) {
245 yyerror("%s", error->msg);
246 YYERROR;
248 } '{' optnl fetchopts2 '}'
249 | SEND {
250 static const struct got_error* error;
252 if (remote->send_repo != NULL) {
253 yyerror("send block already exists");
254 YYERROR;
256 error = new_send(&remote->send_repo);
257 if (error) {
258 yyerror("%s", error->msg);
259 YYERROR;
261 } '{' optnl sendopts2 '}'
263 fetchopts2 : fetchopts2 fetchopts1 nl
264 | fetchopts1 optnl
266 fetchopts1 : /* empty */
267 | REPOSITORY STRING {
268 remote->fetch_repo->fetch_repository = strdup($2);
269 if (remote->fetch_repo->fetch_repository == NULL) {
270 free($2);
271 yyerror("strdup");
272 YYERROR;
274 free($2);
276 | SERVER STRING {
277 remote->fetch_repo->fetch_server = strdup($2);
278 if (remote->fetch_repo->fetch_server == NULL) {
279 free($2);
280 yyerror("strdup");
281 YYERROR;
283 free($2);
285 | PROTOCOL STRING {
286 remote->fetch_repo->fetch_protocol = strdup($2);
287 if (remote->fetch_repo->fetch_protocol == NULL) {
288 free($2);
289 yyerror("strdup");
290 YYERROR;
292 free($2);
294 | PORT portplain {
295 remote->fetch_repo->fetch_port = $2;
297 | BRANCH branch {
298 remote->fetch_repo->fetch_branch = $2;
301 sendopts2 : sendopts2 sendopts1 nl
302 | sendopts1 optnl
304 sendopts1 : /* empty */
305 | REPOSITORY STRING {
306 remote->send_repo->send_repository = strdup($2);
307 if (remote->send_repo->send_repository == NULL) {
308 free($2);
309 yyerror("strdup");
310 YYERROR;
312 free($2);
314 | SERVER STRING {
315 remote->send_repo->send_server = strdup($2);
316 if (remote->send_repo->send_server == NULL) {
317 free($2);
318 yyerror("strdup");
319 YYERROR;
321 free($2);
323 | PROTOCOL STRING {
324 remote->send_repo->send_protocol = strdup($2);
325 if (remote->send_repo->send_protocol == NULL) {
326 free($2);
327 yyerror("strdup");
328 YYERROR;
330 free($2);
332 | PORT portplain {
333 remote->send_repo->send_port = $2;
335 | BRANCH branch {
336 remote->send_repo->send_branch = $2;
339 remote : REMOTE STRING {
340 static const struct got_error* error;
342 error = new_remote(&remote);
343 if (error) {
344 free($2);
345 yyerror("%s", error->msg);
346 YYERROR;
348 remote->name = strdup($2);
349 if (remote->name == NULL) {
350 free($2);
351 yyerror("strdup");
352 YYERROR;
354 free($2);
355 } '{' optnl remoteopts2 '}' {
356 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
357 gotconfig.nremotes++;
360 author : AUTHOR STRING {
361 gotconfig.author = strdup($2);
362 if (gotconfig.author == NULL) {
363 free($2);
364 yyerror("strdup");
365 YYERROR;
367 free($2);
370 optnl : '\n' optnl
371 | /* empty */
373 nl : '\n' optnl
375 comma : ','
376 | /* empty */
378 %%
380 struct keywords {
381 const char *k_name;
382 int k_val;
383 };
385 int
386 yyerror(const char *fmt, ...)
388 va_list ap;
389 char *msg;
390 char *err = NULL;
392 va_start(ap, fmt);
393 if (vasprintf(&msg, fmt, ap) == -1) {
394 gerror = got_error_from_errno("vasprintf");
395 return 0;
397 va_end(ap);
398 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
399 msg) == -1) {
400 gerror = got_error_from_errno("asprintf");
401 return(0);
403 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
404 free(msg);
405 free(err);
406 return(0);
408 int
409 kw_cmp(const void *k, const void *e)
411 return (strcmp(k, ((const struct keywords *)e)->k_name));
414 int
415 lookup(char *s)
417 /* This has to be sorted always. */
418 static const struct keywords keywords[] = {
419 {"author", AUTHOR},
420 {"branch", BRANCH},
421 {"fetch", FETCH},
422 {"fetch-all-branches", FETCH_ALL_BRANCHES},
423 {"mirror-references", MIRROR_REFERENCES},
424 {"port", PORT},
425 {"protocol", PROTOCOL},
426 {"reference", REFERENCE},
427 {"remote", REMOTE},
428 {"repository", REPOSITORY},
429 {"send", SEND},
430 {"server", SERVER},
431 };
432 const struct keywords *p;
434 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
435 sizeof(keywords[0]), kw_cmp);
437 if (p)
438 return (p->k_val);
439 else
440 return (STRING);
443 #define START_EXPAND 1
444 #define DONE_EXPAND 2
446 static int expanding;
448 int
449 igetc(void)
451 int c;
453 while (1) {
454 if (file->ungetpos > 0)
455 c = file->ungetbuf[--file->ungetpos];
456 else
457 c = getc(file->stream);
459 if (c == START_EXPAND)
460 expanding = 1;
461 else if (c == DONE_EXPAND)
462 expanding = 0;
463 else
464 break;
466 return (c);
469 int
470 lgetc(int quotec)
472 int c, next;
474 if (quotec) {
475 c = igetc();
476 if (c == EOF) {
477 yyerror("reached end of file while parsing "
478 "quoted string");
480 return (c);
483 c = igetc();
484 while (c == '\\') {
485 next = igetc();
486 if (next != '\n') {
487 c = next;
488 break;
490 yylval.lineno = file->lineno;
491 file->lineno++;
494 return (c);
497 void
498 lungetc(int c)
500 if (c == EOF)
501 return;
503 if (file->ungetpos >= file->ungetsize) {
504 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
505 if (p == NULL)
506 err(1, "%s", __func__);
507 file->ungetbuf = p;
508 file->ungetsize *= 2;
510 file->ungetbuf[file->ungetpos++] = c;
513 int
514 findeol(void)
516 int c;
518 /* Skip to either EOF or the first real EOL. */
519 while (1) {
520 c = lgetc(0);
521 if (c == '\n') {
522 file->lineno++;
523 break;
525 if (c == EOF)
526 break;
528 return (ERROR);
531 static long long
532 getservice(char *n)
534 struct servent *s;
535 u_long ulval;
537 if (atoul(n, &ulval) == 0) {
538 if (ulval > 65535) {
539 yyerror("illegal port value %lu", ulval);
540 return (-1);
542 return ulval;
543 } else {
544 s = getservbyname(n, "tcp");
545 if (s == NULL)
546 s = getservbyname(n, "udp");
547 if (s == NULL) {
548 yyerror("unknown port %s", n);
549 return (-1);
551 return (s->s_port);
555 static int
556 parseport(char *port, long long *pn)
558 if ((*pn = getservice(port)) == -1) {
559 *pn = 0LL;
560 return (-1);
562 return (0);
566 int
567 yylex(void)
569 unsigned char buf[8096];
570 unsigned char *p, *val;
571 int quotec, next, c;
572 int token;
574 top:
575 p = buf;
576 c = lgetc(0);
577 while (c == ' ' || c == '\t')
578 c = lgetc(0); /* nothing */
580 yylval.lineno = file->lineno;
581 if (c == '#') {
582 c = lgetc(0);
583 while (c != '\n' && c != EOF)
584 c = lgetc(0); /* nothing */
586 if (c == '$' && !expanding) {
587 while (1) {
588 c = lgetc(0);
589 if (c == EOF)
590 return (0);
592 if (p + 1 >= buf + sizeof(buf) - 1) {
593 yyerror("string too long");
594 return (findeol());
596 if (isalnum(c) || c == '_') {
597 *p++ = c;
598 continue;
600 *p = '\0';
601 lungetc(c);
602 break;
604 val = symget(buf);
605 if (val == NULL) {
606 yyerror("macro '%s' not defined", buf);
607 return (findeol());
609 p = val + strlen(val) - 1;
610 lungetc(DONE_EXPAND);
611 while (p >= val) {
612 lungetc(*p);
613 p--;
615 lungetc(START_EXPAND);
616 goto top;
619 switch (c) {
620 case '\'':
621 case '"':
622 quotec = c;
623 while (1) {
624 c = lgetc(quotec);
625 if (c == EOF)
626 return (0);
627 if (c == '\n') {
628 file->lineno++;
629 continue;
630 } else if (c == '\\') {
631 next = lgetc(quotec);
632 if (next == EOF)
633 return (0);
634 if (next == quotec || c == ' ' || c == '\t')
635 c = next;
636 else if (next == '\n') {
637 file->lineno++;
638 continue;
639 } else
640 lungetc(next);
641 } else if (c == quotec) {
642 *p = '\0';
643 break;
644 } else if (c == '\0') {
645 yyerror("syntax error");
646 return (findeol());
648 if (p + 1 >= buf + sizeof(buf) - 1) {
649 yyerror("string too long");
650 return (findeol());
652 *p++ = c;
654 yylval.v.string = strdup(buf);
655 if (yylval.v.string == NULL)
656 err(1, "%s", __func__);
657 return (STRING);
660 #define allowed_to_end_number(x) \
661 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
663 if (c == '-' || isdigit(c)) {
664 do {
665 *p++ = c;
666 if ((unsigned)(p-buf) >= sizeof(buf)) {
667 yyerror("string too long");
668 return (findeol());
670 c = lgetc(0);
671 } while (c != EOF && isdigit(c));
672 lungetc(c);
673 if (p == buf + 1 && buf[0] == '-')
674 goto nodigits;
675 if (c == EOF || allowed_to_end_number(c)) {
676 const char *errstr = NULL;
678 *p = '\0';
679 yylval.v.number = strtonum(buf, LLONG_MIN,
680 LLONG_MAX, &errstr);
681 if (errstr) {
682 yyerror("\"%s\" invalid number: %s",
683 buf, errstr);
684 return (findeol());
686 return (NUMBER);
687 } else {
688 nodigits:
689 while (p > buf + 1)
690 lungetc(*--p);
691 c = *--p;
692 if (c == '-')
693 return (c);
697 #define allowed_in_string(x) \
698 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
699 x != '{' && x != '}' && \
700 x != '!' && x != '=' && x != '#' && \
701 x != ','))
703 if (isalnum(c) || c == ':' || c == '_') {
704 do {
705 *p++ = c;
706 if ((unsigned)(p-buf) >= sizeof(buf)) {
707 yyerror("string too long");
708 return (findeol());
710 c = lgetc(0);
711 } while (c != EOF && (allowed_in_string(c)));
712 lungetc(c);
713 *p = '\0';
714 token = lookup(buf);
715 if (token == STRING) {
716 yylval.v.string = strdup(buf);
717 if (yylval.v.string == NULL)
718 err(1, "%s", __func__);
720 return (token);
722 if (c == '\n') {
723 yylval.lineno = file->lineno;
724 file->lineno++;
726 if (c == EOF)
727 return (0);
728 return (c);
731 static const struct got_error*
732 newfile(struct file **nfile, const char *filename, int *fd)
734 const struct got_error* error = NULL;
736 (*nfile) = calloc(1, sizeof(struct file));
737 if ((*nfile) == NULL)
738 return got_error_from_errno("calloc");
739 (*nfile)->stream = fdopen(*fd, "r");
740 if ((*nfile)->stream == NULL) {
741 error = got_error_from_errno("fdopen");
742 free((*nfile));
743 return error;
745 *fd = -1; /* Stream owns the file descriptor now. */
746 (*nfile)->name = filename;
747 (*nfile)->lineno = 1;
748 (*nfile)->ungetsize = 16;
749 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
750 if ((*nfile)->ungetbuf == NULL) {
751 error = got_error_from_errno("malloc");
752 fclose((*nfile)->stream);
753 free((*nfile));
754 return error;
756 return NULL;
759 static const struct got_error*
760 new_remote(struct gotconfig_remote_repo **remote)
762 const struct got_error *error = NULL;
764 *remote = calloc(1, sizeof(**remote));
765 if (*remote == NULL)
766 error = got_error_from_errno("calloc");
767 return error;
770 static const struct got_error*
771 new_fetch(struct fetch_repo **fetch_repo)
773 const struct got_error *error = NULL;
775 *fetch_repo = calloc(1, sizeof(**fetch_repo));
776 if (*fetch_repo == NULL)
777 error = got_error_from_errno("calloc");
778 return error;
781 static const struct got_error*
782 new_send(struct send_repo **send_repo)
784 const struct got_error *error = NULL;
786 *send_repo = calloc(1, sizeof(**send_repo));
787 if (*send_repo == NULL)
788 error = got_error_from_errno("calloc");
789 return error;
792 static void
793 closefile(struct file *file)
795 fclose(file->stream);
796 free(file->ungetbuf);
797 free(file);
800 const struct got_error *
801 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
803 const struct got_error *err = NULL;
804 struct sym *sym, *next;
806 *conf = NULL;
808 err = newfile(&file, filename, fd);
809 if (err)
810 return err;
812 TAILQ_INIT(&gotconfig.remotes);
814 yyparse();
815 closefile(file);
817 /* Free macros and check which have not been used. */
818 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
819 if (!sym->persist) {
820 free(sym->nam);
821 free(sym->val);
822 TAILQ_REMOVE(&symhead, sym, entry);
823 free(sym);
827 if (gerror == NULL)
828 *conf = &gotconfig;
829 return gerror;
832 void
833 gotconfig_free(struct gotconfig *conf)
835 struct gotconfig_remote_repo *remote;
837 free(conf->author);
838 while (!TAILQ_EMPTY(&conf->remotes)) {
839 remote = TAILQ_FIRST(&conf->remotes);
840 TAILQ_REMOVE(&conf->remotes, remote, entry);
841 if (remote->fetch_repo != NULL) {
842 free(remote->fetch_repo->fetch_repository);
843 free(remote->fetch_repo->fetch_server);
844 free(remote->fetch_repo->fetch_protocol);
846 if (remote->send_repo != NULL) {
847 free(remote->send_repo->send_repository);
848 free(remote->send_repo->send_server);
849 free(remote->send_repo->send_protocol);
851 free(remote->name);
852 free(remote->repository);
853 free(remote->server);
854 free(remote->protocol);
855 free(remote);
859 int
860 symset(const char *nam, const char *val, int persist)
862 struct sym *sym;
864 TAILQ_FOREACH(sym, &symhead, entry) {
865 if (strcmp(nam, sym->nam) == 0)
866 break;
869 if (sym != NULL) {
870 if (sym->persist == 1)
871 return (0);
872 else {
873 free(sym->nam);
874 free(sym->val);
875 TAILQ_REMOVE(&symhead, sym, entry);
876 free(sym);
879 sym = calloc(1, sizeof(*sym));
880 if (sym == NULL)
881 return (-1);
883 sym->nam = strdup(nam);
884 if (sym->nam == NULL) {
885 free(sym);
886 return (-1);
888 sym->val = strdup(val);
889 if (sym->val == NULL) {
890 free(sym->nam);
891 free(sym);
892 return (-1);
894 sym->used = 0;
895 sym->persist = persist;
896 TAILQ_INSERT_TAIL(&symhead, sym, entry);
897 return (0);
900 int
901 cmdline_symset(char *s)
903 char *sym, *val;
904 int ret;
905 size_t len;
907 val = strrchr(s, '=');
908 if (val == NULL)
909 return (-1);
911 len = strlen(s) - strlen(val) + 1;
912 sym = malloc(len);
913 if (sym == NULL)
914 errx(1, "cmdline_symset: malloc");
916 strlcpy(sym, s, len);
918 ret = symset(sym, val + 1, 1);
919 free(sym);
921 return (ret);
924 char *
925 symget(const char *nam)
927 struct sym *sym;
929 TAILQ_FOREACH(sym, &symhead, entry) {
930 if (strcmp(nam, sym->nam) == 0) {
931 sym->used = 1;
932 return (sym->val);
935 return (NULL);
938 static int
939 atoul(char *s, u_long *ulvalp)
941 u_long ulval;
942 char *ep;
944 errno = 0;
945 ulval = strtoul(s, &ep, 0);
946 if (s[0] == '\0' || *ep != '\0')
947 return (-1);
948 if (errno == ERANGE && ulval == ULONG_MAX)
949 return (-1);
950 *ulvalp = ulval;
951 return (0);