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_config(struct fetch_config **);
85 static const struct got_error* new_send_config(struct send_config **);
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_config != NULL) {
240 yyerror("fetch block already exists");
241 YYERROR;
243 error = new_fetch_config(&remote->fetch_config);
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_config != NULL) {
253 yyerror("send block already exists");
254 YYERROR;
256 error = new_send_config(&remote->send_config);
257 if (error) {
258 yyerror("%s", error->msg);
259 YYERROR;
261 } '{' optnl sendopts2 '}'
263 fetchopts2 : fetchopts2 fetchopts1 nl
264 | fetchopts1 optnl
266 fetchopts1 : REPOSITORY STRING {
267 remote->fetch_config->repository = strdup($2);
268 if (remote->fetch_config->repository == NULL) {
269 free($2);
270 yyerror("strdup");
271 YYERROR;
273 free($2);
275 | SERVER STRING {
276 remote->fetch_config->server = strdup($2);
277 if (remote->fetch_config->server == NULL) {
278 free($2);
279 yyerror("strdup");
280 YYERROR;
282 free($2);
284 | PROTOCOL STRING {
285 remote->fetch_config->protocol = strdup($2);
286 if (remote->fetch_config->protocol == NULL) {
287 free($2);
288 yyerror("strdup");
289 YYERROR;
291 free($2);
293 | PORT portplain {
294 remote->fetch_config->port = $2;
296 | BRANCH branch {
297 remote->fetch_config->branch = $2;
300 sendopts2 : sendopts2 sendopts1 nl
301 | sendopts1 optnl
303 sendopts1 : REPOSITORY STRING {
304 remote->send_config->repository = strdup($2);
305 if (remote->send_config->repository == NULL) {
306 free($2);
307 yyerror("strdup");
308 YYERROR;
310 free($2);
312 | SERVER STRING {
313 remote->send_config->server = strdup($2);
314 if (remote->send_config->server == NULL) {
315 free($2);
316 yyerror("strdup");
317 YYERROR;
319 free($2);
321 | PROTOCOL STRING {
322 remote->send_config->protocol = strdup($2);
323 if (remote->send_config->protocol == NULL) {
324 free($2);
325 yyerror("strdup");
326 YYERROR;
328 free($2);
330 | PORT portplain {
331 remote->send_config->port = $2;
333 | BRANCH branch {
334 remote->send_config->branch = $2;
337 remote : REMOTE STRING {
338 static const struct got_error* error;
340 error = new_remote(&remote);
341 if (error) {
342 free($2);
343 yyerror("%s", error->msg);
344 YYERROR;
346 remote->name = strdup($2);
347 if (remote->name == NULL) {
348 free($2);
349 yyerror("strdup");
350 YYERROR;
352 free($2);
353 } '{' optnl remoteopts2 '}' {
354 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
355 gotconfig.nremotes++;
358 author : AUTHOR STRING {
359 gotconfig.author = strdup($2);
360 if (gotconfig.author == NULL) {
361 free($2);
362 yyerror("strdup");
363 YYERROR;
365 free($2);
368 optnl : '\n' optnl
369 | /* empty */
371 nl : '\n' optnl
373 comma : ','
374 | /* empty */
376 %%
378 struct keywords {
379 const char *k_name;
380 int k_val;
381 };
383 int
384 yyerror(const char *fmt, ...)
386 va_list ap;
387 char *msg;
388 char *err = NULL;
390 va_start(ap, fmt);
391 if (vasprintf(&msg, fmt, ap) == -1) {
392 gerror = got_error_from_errno("vasprintf");
393 return 0;
395 va_end(ap);
396 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
397 msg) == -1) {
398 gerror = got_error_from_errno("asprintf");
399 return(0);
401 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
402 free(msg);
403 free(err);
404 return(0);
406 int
407 kw_cmp(const void *k, const void *e)
409 return (strcmp(k, ((const struct keywords *)e)->k_name));
412 int
413 lookup(char *s)
415 /* This has to be sorted always. */
416 static const struct keywords keywords[] = {
417 {"author", AUTHOR},
418 {"branch", BRANCH},
419 {"fetch", FETCH},
420 {"fetch-all-branches", FETCH_ALL_BRANCHES},
421 {"mirror-references", MIRROR_REFERENCES},
422 {"port", PORT},
423 {"protocol", PROTOCOL},
424 {"reference", REFERENCE},
425 {"remote", REMOTE},
426 {"repository", REPOSITORY},
427 {"send", SEND},
428 {"server", SERVER},
429 };
430 const struct keywords *p;
432 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
433 sizeof(keywords[0]), kw_cmp);
435 if (p)
436 return (p->k_val);
437 else
438 return (STRING);
441 #define START_EXPAND 1
442 #define DONE_EXPAND 2
444 static int expanding;
446 int
447 igetc(void)
449 int c;
451 while (1) {
452 if (file->ungetpos > 0)
453 c = file->ungetbuf[--file->ungetpos];
454 else
455 c = getc(file->stream);
457 if (c == START_EXPAND)
458 expanding = 1;
459 else if (c == DONE_EXPAND)
460 expanding = 0;
461 else
462 break;
464 return (c);
467 int
468 lgetc(int quotec)
470 int c, next;
472 if (quotec) {
473 c = igetc();
474 if (c == EOF) {
475 yyerror("reached end of file while parsing "
476 "quoted string");
478 return (c);
481 c = igetc();
482 while (c == '\\') {
483 next = igetc();
484 if (next != '\n') {
485 c = next;
486 break;
488 yylval.lineno = file->lineno;
489 file->lineno++;
492 return (c);
495 void
496 lungetc(int c)
498 if (c == EOF)
499 return;
501 if (file->ungetpos >= file->ungetsize) {
502 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
503 if (p == NULL)
504 err(1, "%s", __func__);
505 file->ungetbuf = p;
506 file->ungetsize *= 2;
508 file->ungetbuf[file->ungetpos++] = c;
511 int
512 findeol(void)
514 int c;
516 /* Skip to either EOF or the first real EOL. */
517 while (1) {
518 c = lgetc(0);
519 if (c == '\n') {
520 file->lineno++;
521 break;
523 if (c == EOF)
524 break;
526 return (ERROR);
529 static long long
530 getservice(char *n)
532 struct servent *s;
533 u_long ulval;
535 if (atoul(n, &ulval) == 0) {
536 if (ulval == 0 || ulval > 65535) {
537 yyerror("illegal port value %lu", ulval);
538 return (-1);
540 return ulval;
541 } else {
542 s = getservbyname(n, "tcp");
543 if (s == NULL)
544 s = getservbyname(n, "udp");
545 if (s == NULL) {
546 yyerror("unknown port %s", n);
547 return (-1);
549 return (s->s_port);
553 static int
554 parseport(char *port, long long *pn)
556 if ((*pn = getservice(port)) == -1) {
557 *pn = 0LL;
558 return (-1);
560 return (0);
564 int
565 yylex(void)
567 unsigned char buf[8096];
568 unsigned char *p, *val;
569 int quotec, next, c;
570 int token;
572 top:
573 p = buf;
574 c = lgetc(0);
575 while (c == ' ' || c == '\t')
576 c = lgetc(0); /* nothing */
578 yylval.lineno = file->lineno;
579 if (c == '#') {
580 c = lgetc(0);
581 while (c != '\n' && c != EOF)
582 c = lgetc(0); /* nothing */
584 if (c == '$' && !expanding) {
585 while (1) {
586 c = lgetc(0);
587 if (c == EOF)
588 return (0);
590 if (p + 1 >= buf + sizeof(buf) - 1) {
591 yyerror("string too long");
592 return (findeol());
594 if (isalnum(c) || c == '_') {
595 *p++ = c;
596 continue;
598 *p = '\0';
599 lungetc(c);
600 break;
602 val = symget(buf);
603 if (val == NULL) {
604 yyerror("macro '%s' not defined", buf);
605 return (findeol());
607 p = val + strlen(val) - 1;
608 lungetc(DONE_EXPAND);
609 while (p >= val) {
610 lungetc(*p);
611 p--;
613 lungetc(START_EXPAND);
614 goto top;
617 switch (c) {
618 case '\'':
619 case '"':
620 quotec = c;
621 while (1) {
622 c = lgetc(quotec);
623 if (c == EOF)
624 return (0);
625 if (c == '\n') {
626 file->lineno++;
627 continue;
628 } else if (c == '\\') {
629 next = lgetc(quotec);
630 if (next == EOF)
631 return (0);
632 if (next == quotec || c == ' ' || c == '\t')
633 c = next;
634 else if (next == '\n') {
635 file->lineno++;
636 continue;
637 } else
638 lungetc(next);
639 } else if (c == quotec) {
640 *p = '\0';
641 break;
642 } else if (c == '\0') {
643 yyerror("syntax error");
644 return (findeol());
646 if (p + 1 >= buf + sizeof(buf) - 1) {
647 yyerror("string too long");
648 return (findeol());
650 *p++ = c;
652 yylval.v.string = strdup(buf);
653 if (yylval.v.string == NULL)
654 err(1, "%s", __func__);
655 return (STRING);
658 #define allowed_to_end_number(x) \
659 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
661 if (c == '-' || isdigit(c)) {
662 do {
663 *p++ = c;
664 if ((unsigned)(p-buf) >= sizeof(buf)) {
665 yyerror("string too long");
666 return (findeol());
668 c = lgetc(0);
669 } while (c != EOF && isdigit(c));
670 lungetc(c);
671 if (p == buf + 1 && buf[0] == '-')
672 goto nodigits;
673 if (c == EOF || allowed_to_end_number(c)) {
674 const char *errstr = NULL;
676 *p = '\0';
677 yylval.v.number = strtonum(buf, LLONG_MIN,
678 LLONG_MAX, &errstr);
679 if (errstr) {
680 yyerror("\"%s\" invalid number: %s",
681 buf, errstr);
682 return (findeol());
684 return (NUMBER);
685 } else {
686 nodigits:
687 while (p > buf + 1)
688 lungetc(*--p);
689 c = *--p;
690 if (c == '-')
691 return (c);
695 #define allowed_in_string(x) \
696 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
697 x != '{' && x != '}' && \
698 x != '!' && x != '=' && x != '#' && \
699 x != ','))
701 if (isalnum(c) || c == ':' || c == '_') {
702 do {
703 *p++ = c;
704 if ((unsigned)(p-buf) >= sizeof(buf)) {
705 yyerror("string too long");
706 return (findeol());
708 c = lgetc(0);
709 } while (c != EOF && (allowed_in_string(c)));
710 lungetc(c);
711 *p = '\0';
712 token = lookup(buf);
713 if (token == STRING) {
714 yylval.v.string = strdup(buf);
715 if (yylval.v.string == NULL)
716 err(1, "%s", __func__);
718 return (token);
720 if (c == '\n') {
721 yylval.lineno = file->lineno;
722 file->lineno++;
724 if (c == EOF)
725 return (0);
726 return (c);
729 static const struct got_error*
730 newfile(struct file **nfile, const char *filename, int *fd)
732 const struct got_error* error = NULL;
734 (*nfile) = calloc(1, sizeof(struct file));
735 if ((*nfile) == NULL)
736 return got_error_from_errno("calloc");
737 (*nfile)->stream = fdopen(*fd, "r");
738 if ((*nfile)->stream == NULL) {
739 error = got_error_from_errno("fdopen");
740 free((*nfile));
741 return error;
743 *fd = -1; /* Stream owns the file descriptor now. */
744 (*nfile)->name = filename;
745 (*nfile)->lineno = 1;
746 (*nfile)->ungetsize = 16;
747 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
748 if ((*nfile)->ungetbuf == NULL) {
749 error = got_error_from_errno("malloc");
750 fclose((*nfile)->stream);
751 free((*nfile));
752 return error;
754 return NULL;
757 static const struct got_error*
758 new_remote(struct gotconfig_remote_repo **remote)
760 const struct got_error *error = NULL;
762 *remote = calloc(1, sizeof(**remote));
763 if (*remote == NULL)
764 error = got_error_from_errno("calloc");
765 return error;
768 static const struct got_error*
769 new_fetch_config(struct fetch_config **fetch_config)
771 const struct got_error *error = NULL;
773 *fetch_config = calloc(1, sizeof(**fetch_config));
774 if (*fetch_config == NULL)
775 error = got_error_from_errno("calloc");
776 return error;
779 static const struct got_error*
780 new_send_config(struct send_config **send_config)
782 const struct got_error *error = NULL;
784 *send_config = calloc(1, sizeof(**send_config));
785 if (*send_config == NULL)
786 error = got_error_from_errno("calloc");
787 return error;
790 static void
791 closefile(struct file *file)
793 fclose(file->stream);
794 free(file->ungetbuf);
795 free(file);
798 const struct got_error *
799 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
801 const struct got_error *err = NULL;
802 struct sym *sym, *next;
804 *conf = NULL;
806 err = newfile(&file, filename, fd);
807 if (err)
808 return err;
810 TAILQ_INIT(&gotconfig.remotes);
812 yyparse();
813 closefile(file);
815 /* Free macros and check which have not been used. */
816 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
817 if (!sym->persist) {
818 free(sym->nam);
819 free(sym->val);
820 TAILQ_REMOVE(&symhead, sym, entry);
821 free(sym);
825 if (gerror == NULL)
826 *conf = &gotconfig;
827 return gerror;
830 static void
831 free_fetch_config(struct fetch_config *fetch_config)
833 free(remote->fetch_config->repository);
834 free(remote->fetch_config->server);
835 free(remote->fetch_config->protocol);
836 free(remote->fetch_config);
839 static void
840 free_send_config(struct send_config *send_config)
842 free(remote->send_config->repository);
843 free(remote->send_config->server);
844 free(remote->send_config->protocol);
845 free(remote->send_config);
848 void
849 gotconfig_free(struct gotconfig *conf)
851 struct gotconfig_remote_repo *remote;
853 free(conf->author);
854 while (!TAILQ_EMPTY(&conf->remotes)) {
855 remote = TAILQ_FIRST(&conf->remotes);
856 TAILQ_REMOVE(&conf->remotes, remote, entry);
857 if (remote->fetch_config != NULL)
858 free_fetch_config(remote->fetch_config);
859 if (remote->send_config != NULL)
860 free_send_config(remote->send_config);
861 free(remote->name);
862 free(remote->repository);
863 free(remote->server);
864 free(remote->protocol);
865 free(remote);
869 int
870 symset(const char *nam, const char *val, int persist)
872 struct sym *sym;
874 TAILQ_FOREACH(sym, &symhead, entry) {
875 if (strcmp(nam, sym->nam) == 0)
876 break;
879 if (sym != NULL) {
880 if (sym->persist == 1)
881 return (0);
882 else {
883 free(sym->nam);
884 free(sym->val);
885 TAILQ_REMOVE(&symhead, sym, entry);
886 free(sym);
889 sym = calloc(1, sizeof(*sym));
890 if (sym == NULL)
891 return (-1);
893 sym->nam = strdup(nam);
894 if (sym->nam == NULL) {
895 free(sym);
896 return (-1);
898 sym->val = strdup(val);
899 if (sym->val == NULL) {
900 free(sym->nam);
901 free(sym);
902 return (-1);
904 sym->used = 0;
905 sym->persist = persist;
906 TAILQ_INSERT_TAIL(&symhead, sym, entry);
907 return (0);
910 int
911 cmdline_symset(char *s)
913 char *sym, *val;
914 int ret;
915 size_t len;
917 val = strrchr(s, '=');
918 if (val == NULL)
919 return (-1);
921 len = strlen(s) - strlen(val) + 1;
922 sym = malloc(len);
923 if (sym == NULL)
924 errx(1, "cmdline_symset: malloc");
926 strlcpy(sym, s, len);
928 ret = symset(sym, val + 1, 1);
929 free(sym);
931 return (ret);
934 char *
935 symget(const char *nam)
937 struct sym *sym;
939 TAILQ_FOREACH(sym, &symhead, entry) {
940 if (strcmp(nam, sym->nam) == 0) {
941 sym->used = 1;
942 return (sym->val);
945 return (NULL);
948 static int
949 atoul(char *s, u_long *ulvalp)
951 u_long ulval;
952 char *ep;
954 errno = 0;
955 ulval = strtoul(s, &ep, 0);
956 if (s[0] == '\0' || *ep != '\0')
957 return (-1);
958 if (errno == ERANGE && ulval == ULONG_MAX)
959 return (-1);
960 *ulvalp = ulval;
961 return (0);