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 = $2;
197 | SERVER STRING {
198 remote->server = $2;
200 | PROTOCOL STRING {
201 remote->protocol = $2;
203 | MIRROR_REFERENCES boolean {
204 remote->mirror_references = $2;
206 | FETCH_ALL_BRANCHES boolean {
207 remote->fetch_all_branches = $2;
209 | PORT portplain {
210 remote->port = $2;
212 | BRANCH branch {
213 remote->branch = $2;
215 | REFERENCE ref {
216 remote->fetch_ref = $2;
218 | FETCH {
219 static const struct got_error* error;
221 if (remote->fetch_config != NULL) {
222 yyerror("fetch block already exists");
223 YYERROR;
225 error = new_fetch_config(&remote->fetch_config);
226 if (error) {
227 yyerror("%s", error->msg);
228 YYERROR;
230 } '{' optnl fetchempty '}'
231 | SEND {
232 static const struct got_error* error;
234 if (remote->send_config != NULL) {
235 yyerror("send block already exists");
236 YYERROR;
238 error = new_send_config(&remote->send_config);
239 if (error) {
240 yyerror("%s", error->msg);
241 YYERROR;
243 } '{' optnl sendempty '}'
245 fetchempty : /* empty */
246 | fetchopts2
248 fetchopts2 : fetchopts2 fetchopts1 nl
249 | fetchopts1 optnl
251 fetchopts1 : REPOSITORY STRING {
252 remote->fetch_config->repository = $2;
254 | SERVER STRING {
255 remote->fetch_config->server = $2;
257 | PROTOCOL STRING {
258 remote->fetch_config->protocol = $2;
260 | PORT portplain {
261 remote->fetch_config->port = $2;
263 | BRANCH branch {
264 remote->fetch_config->branch = $2;
267 sendempty : /* empty */
268 | sendopts2
270 sendopts2 : sendopts2 sendopts1 nl
271 | sendopts1 optnl
273 sendopts1 : REPOSITORY STRING {
274 remote->send_config->repository = $2;
276 | SERVER STRING {
277 remote->send_config->server = $2;
279 | PROTOCOL STRING {
280 remote->send_config->protocol = $2;
282 | PORT portplain {
283 remote->send_config->port = $2;
285 | BRANCH branch {
286 remote->send_config->branch = $2;
289 remote : REMOTE STRING {
290 static const struct got_error* error;
292 error = new_remote(&remote);
293 if (error) {
294 free($2);
295 yyerror("%s", error->msg);
296 YYERROR;
298 remote->name = $2;
299 } '{' optnl remoteopts2 '}' {
300 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
301 gotconfig.nremotes++;
304 author : AUTHOR STRING {
305 gotconfig.author = $2;
308 optnl : '\n' optnl
309 | /* empty */
311 nl : '\n' optnl
313 comma : ','
314 | /* empty */
316 %%
318 struct keywords {
319 const char *k_name;
320 int k_val;
321 };
323 int
324 yyerror(const char *fmt, ...)
326 va_list ap;
327 char *msg;
328 char *err = NULL;
330 va_start(ap, fmt);
331 if (vasprintf(&msg, fmt, ap) == -1) {
332 gerror = got_error_from_errno("vasprintf");
333 return 0;
335 va_end(ap);
336 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
337 msg) == -1) {
338 gerror = got_error_from_errno("asprintf");
339 return(0);
341 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
342 free(msg);
343 return(0);
345 int
346 kw_cmp(const void *k, const void *e)
348 return (strcmp(k, ((const struct keywords *)e)->k_name));
351 int
352 lookup(char *s)
354 /* This has to be sorted always. */
355 static const struct keywords keywords[] = {
356 {"author", AUTHOR},
357 {"branch", BRANCH},
358 {"fetch", FETCH},
359 {"fetch-all-branches", FETCH_ALL_BRANCHES},
360 {"mirror-references", MIRROR_REFERENCES},
361 {"port", PORT},
362 {"protocol", PROTOCOL},
363 {"reference", REFERENCE},
364 {"remote", REMOTE},
365 {"repository", REPOSITORY},
366 {"send", SEND},
367 {"server", SERVER},
368 };
369 const struct keywords *p;
371 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
372 sizeof(keywords[0]), kw_cmp);
374 if (p)
375 return (p->k_val);
376 else
377 return (STRING);
380 #define START_EXPAND 1
381 #define DONE_EXPAND 2
383 static int expanding;
385 int
386 igetc(void)
388 int c;
390 while (1) {
391 if (file->ungetpos > 0)
392 c = file->ungetbuf[--file->ungetpos];
393 else
394 c = getc(file->stream);
396 if (c == START_EXPAND)
397 expanding = 1;
398 else if (c == DONE_EXPAND)
399 expanding = 0;
400 else
401 break;
403 return (c);
406 int
407 lgetc(int quotec)
409 int c, next;
411 if (quotec) {
412 c = igetc();
413 if (c == EOF) {
414 yyerror("reached end of file while parsing "
415 "quoted string");
417 return (c);
420 c = igetc();
421 while (c == '\\') {
422 next = igetc();
423 if (next != '\n') {
424 c = next;
425 break;
427 yylval.lineno = file->lineno;
428 file->lineno++;
431 return (c);
434 void
435 lungetc(int c)
437 if (c == EOF)
438 return;
440 if (file->ungetpos >= file->ungetsize) {
441 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
442 if (p == NULL)
443 err(1, "%s", __func__);
444 file->ungetbuf = p;
445 file->ungetsize *= 2;
447 file->ungetbuf[file->ungetpos++] = c;
450 int
451 findeol(void)
453 int c;
455 /* Skip to either EOF or the first real EOL. */
456 while (1) {
457 c = lgetc(0);
458 if (c == '\n') {
459 file->lineno++;
460 break;
462 if (c == EOF)
463 break;
465 return (ERROR);
468 static long long
469 getservice(char *n)
471 struct servent *s;
472 u_long ulval;
474 if (atoul(n, &ulval) == 0) {
475 if (ulval == 0 || ulval > 65535) {
476 yyerror("illegal port value %lu", ulval);
477 return (-1);
479 return ulval;
480 } else {
481 s = getservbyname(n, "tcp");
482 if (s == NULL)
483 s = getservbyname(n, "udp");
484 if (s == NULL) {
485 yyerror("unknown port %s", n);
486 return (-1);
488 return (s->s_port);
492 static int
493 parseport(char *port, long long *pn)
495 if ((*pn = getservice(port)) == -1) {
496 *pn = 0LL;
497 return (-1);
499 return (0);
503 int
504 yylex(void)
506 char buf[8096];
507 char *p, *val;
508 int quotec, next, c;
509 int token;
511 top:
512 p = buf;
513 c = lgetc(0);
514 while (c == ' ' || c == '\t')
515 c = lgetc(0); /* nothing */
517 yylval.lineno = file->lineno;
518 if (c == '#') {
519 c = lgetc(0);
520 while (c != '\n' && c != EOF)
521 c = lgetc(0); /* nothing */
523 if (c == '$' && !expanding) {
524 while (1) {
525 c = lgetc(0);
526 if (c == EOF)
527 return (0);
529 if (p + 1 >= buf + sizeof(buf) - 1) {
530 yyerror("string too long");
531 return (findeol());
533 if (isalnum(c) || c == '_') {
534 *p++ = c;
535 continue;
537 *p = '\0';
538 lungetc(c);
539 break;
541 val = symget(buf);
542 if (val == NULL) {
543 yyerror("macro '%s' not defined", buf);
544 return (findeol());
546 p = val + strlen(val) - 1;
547 lungetc(DONE_EXPAND);
548 while (p >= val) {
549 lungetc((unsigned char)*p);
550 p--;
552 lungetc(START_EXPAND);
553 goto top;
556 switch (c) {
557 case '\'':
558 case '"':
559 quotec = c;
560 while (1) {
561 c = lgetc(quotec);
562 if (c == EOF)
563 return (0);
564 if (c == '\n') {
565 file->lineno++;
566 continue;
567 } else if (c == '\\') {
568 next = lgetc(quotec);
569 if (next == EOF)
570 return (0);
571 if (next == quotec || c == ' ' || c == '\t')
572 c = next;
573 else if (next == '\n') {
574 file->lineno++;
575 continue;
576 } else
577 lungetc(next);
578 } else if (c == quotec) {
579 *p = '\0';
580 break;
581 } else if (c == '\0') {
582 yyerror("syntax error");
583 return (findeol());
585 if (p + 1 >= buf + sizeof(buf) - 1) {
586 yyerror("string too long");
587 return (findeol());
589 *p++ = c;
591 yylval.v.string = strdup(buf);
592 if (yylval.v.string == NULL)
593 err(1, "%s", __func__);
594 return (STRING);
597 #define allowed_to_end_number(x) \
598 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
600 if (c == '-' || isdigit(c)) {
601 do {
602 *p++ = c;
603 if ((size_t)(p-buf) >= sizeof(buf)) {
604 yyerror("string too long");
605 return (findeol());
607 c = lgetc(0);
608 } while (c != EOF && isdigit(c));
609 lungetc(c);
610 if (p == buf + 1 && buf[0] == '-')
611 goto nodigits;
612 if (c == EOF || allowed_to_end_number(c)) {
613 const char *errstr = NULL;
615 *p = '\0';
616 yylval.v.number = strtonum(buf, LLONG_MIN,
617 LLONG_MAX, &errstr);
618 if (errstr) {
619 yyerror("\"%s\" invalid number: %s",
620 buf, errstr);
621 return (findeol());
623 return (NUMBER);
624 } else {
625 nodigits:
626 while (p > buf + 1)
627 lungetc((unsigned char)*--p);
628 c = (unsigned char)*--p;
629 if (c == '-')
630 return (c);
634 #define allowed_in_string(x) \
635 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
636 x != '{' && x != '}' && \
637 x != '!' && x != '=' && x != '#' && \
638 x != ','))
640 if (isalnum(c) || c == ':' || c == '_') {
641 do {
642 *p++ = c;
643 if ((size_t)(p-buf) >= sizeof(buf)) {
644 yyerror("string too long");
645 return (findeol());
647 c = lgetc(0);
648 } while (c != EOF && (allowed_in_string(c)));
649 lungetc(c);
650 *p = '\0';
651 token = lookup(buf);
652 if (token == STRING) {
653 yylval.v.string = strdup(buf);
654 if (yylval.v.string == NULL)
655 err(1, "%s", __func__);
657 return (token);
659 if (c == '\n') {
660 yylval.lineno = file->lineno;
661 file->lineno++;
663 if (c == EOF)
664 return (0);
665 return (c);
668 static const struct got_error*
669 newfile(struct file **nfile, const char *filename, int *fd)
671 const struct got_error* error = NULL;
673 (*nfile) = calloc(1, sizeof(struct file));
674 if ((*nfile) == NULL)
675 return got_error_from_errno("calloc");
676 (*nfile)->stream = fdopen(*fd, "r");
677 if ((*nfile)->stream == NULL) {
678 error = got_error_from_errno("fdopen");
679 free((*nfile));
680 return error;
682 *fd = -1; /* Stream owns the file descriptor now. */
683 (*nfile)->name = filename;
684 (*nfile)->lineno = 1;
685 (*nfile)->ungetsize = 16;
686 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
687 if ((*nfile)->ungetbuf == NULL) {
688 error = got_error_from_errno("malloc");
689 fclose((*nfile)->stream);
690 free((*nfile));
691 return error;
693 return NULL;
696 static const struct got_error*
697 new_remote(struct gotconfig_remote_repo **remote)
699 const struct got_error *error = NULL;
701 *remote = calloc(1, sizeof(**remote));
702 if (*remote == NULL)
703 error = got_error_from_errno("calloc");
704 return error;
707 static const struct got_error*
708 new_fetch_config(struct fetch_config **fetch_config)
710 const struct got_error *error = NULL;
712 *fetch_config = calloc(1, sizeof(**fetch_config));
713 if (*fetch_config == NULL)
714 error = got_error_from_errno("calloc");
715 return error;
718 static const struct got_error*
719 new_send_config(struct send_config **send_config)
721 const struct got_error *error = NULL;
723 *send_config = calloc(1, sizeof(**send_config));
724 if (*send_config == NULL)
725 error = got_error_from_errno("calloc");
726 return error;
729 static void
730 closefile(struct file *file)
732 fclose(file->stream);
733 free(file->ungetbuf);
734 free(file);
737 const struct got_error *
738 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
740 const struct got_error *err = NULL;
741 struct sym *sym, *next;
743 *conf = NULL;
745 err = newfile(&file, filename, fd);
746 if (err)
747 return err;
749 TAILQ_INIT(&gotconfig.remotes);
751 yyparse();
752 closefile(file);
754 /* Free macros and check which have not been used. */
755 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
756 if (!sym->persist) {
757 free(sym->nam);
758 free(sym->val);
759 TAILQ_REMOVE(&symhead, sym, entry);
760 free(sym);
764 if (gerror == NULL)
765 *conf = &gotconfig;
766 return gerror;
769 static void
770 free_fetch_config(struct fetch_config *fetch_config)
772 free(remote->fetch_config->repository);
773 free(remote->fetch_config->server);
774 free(remote->fetch_config->protocol);
775 free(remote->fetch_config);
778 static void
779 free_send_config(struct send_config *send_config)
781 free(remote->send_config->repository);
782 free(remote->send_config->server);
783 free(remote->send_config->protocol);
784 free(remote->send_config);
787 void
788 gotconfig_free(struct gotconfig *conf)
790 struct gotconfig_remote_repo *remote;
792 free(conf->author);
793 while (!TAILQ_EMPTY(&conf->remotes)) {
794 remote = TAILQ_FIRST(&conf->remotes);
795 TAILQ_REMOVE(&conf->remotes, remote, entry);
796 if (remote->fetch_config != NULL)
797 free_fetch_config(remote->fetch_config);
798 if (remote->send_config != NULL)
799 free_send_config(remote->send_config);
800 free(remote->name);
801 free(remote->repository);
802 free(remote->server);
803 free(remote->protocol);
804 free(remote);
808 int
809 symset(const char *nam, const char *val, int persist)
811 struct sym *sym;
813 TAILQ_FOREACH(sym, &symhead, entry) {
814 if (strcmp(nam, sym->nam) == 0)
815 break;
818 if (sym != NULL) {
819 if (sym->persist == 1)
820 return (0);
821 else {
822 free(sym->nam);
823 free(sym->val);
824 TAILQ_REMOVE(&symhead, sym, entry);
825 free(sym);
828 sym = calloc(1, sizeof(*sym));
829 if (sym == NULL)
830 return (-1);
832 sym->nam = strdup(nam);
833 if (sym->nam == NULL) {
834 free(sym);
835 return (-1);
837 sym->val = strdup(val);
838 if (sym->val == NULL) {
839 free(sym->nam);
840 free(sym);
841 return (-1);
843 sym->used = 0;
844 sym->persist = persist;
845 TAILQ_INSERT_TAIL(&symhead, sym, entry);
846 return (0);
849 int
850 cmdline_symset(char *s)
852 char *sym, *val;
853 int ret;
854 size_t len;
856 val = strrchr(s, '=');
857 if (val == NULL)
858 return (-1);
860 len = strlen(s) - strlen(val) + 1;
861 sym = malloc(len);
862 if (sym == NULL)
863 errx(1, "cmdline_symset: malloc");
865 strlcpy(sym, s, len);
867 ret = symset(sym, val + 1, 1);
868 free(sym);
870 return (ret);
873 char *
874 symget(const char *nam)
876 struct sym *sym;
878 TAILQ_FOREACH(sym, &symhead, entry) {
879 if (strcmp(nam, sym->nam) == 0) {
880 sym->used = 1;
881 return (sym->val);
884 return (NULL);
887 static int
888 atoul(char *s, u_long *ulvalp)
890 u_long ulval;
891 char *ep;
893 errno = 0;
894 ulval = strtoul(s, &ep, 0);
895 if (s[0] == '\0' || *ep != '\0')
896 return (-1);
897 if (errno == ERANGE && ulval == ULONG_MAX)
898 return (-1);
899 *ulvalp = ulval;
900 return (0);