portable: include sha{1,2} portably
[got-portable.git] / libexec / got-read-gotconfig / parse.y
blob081c519fdcfb2ad018a4a0b2f954a5bbe6571115
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.
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.
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.
25 #include "got_compat.h"
27 #include <sys/types.h>
28 #include <sys/queue.h>
30 #include <netdb.h>
32 #include <ctype.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "got_error.h"
42 #include "gotconfig.h"
44 static struct file {
45 FILE *stream;
46 const char *name;
47 size_t ungetpos;
48 size_t ungetsize;
49 u_char *ungetbuf;
50 int eof_reached;
51 int lineno;
52 } *file;
53 static const struct got_error* newfile(struct file**, const char *, int *);
54 static void closefile(struct file *);
55 int yyparse(void);
56 int yylex(void);
57 int yyerror(const char *, ...)
58 __attribute__((__format__ (printf, 1, 2)))
59 __attribute__((__nonnull__ (1)));
60 int kw_cmp(const void *, const void *);
61 int lookup(char *);
62 int igetc(void);
63 int lgetc(int);
64 void lungetc(int);
65 int findeol(void);
66 static int parseport(char *, long long *);
68 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
69 struct sym {
70 TAILQ_ENTRY(sym) entry;
71 int used;
72 int persist;
73 char *nam;
74 char *val;
77 int symset(const char *, const char *, int);
78 int cmdline_symset(char *);
79 char *symget(const char *);
81 static int atoul(char *, u_long *);
83 static const struct got_error* gerror;
84 static struct gotconfig_remote_repo *remote;
85 static struct gotconfig gotconfig;
86 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
87 static const struct got_error* new_fetch_config(struct fetch_config **);
88 static const struct got_error* new_send_config(struct send_config **);
90 typedef struct {
91 union {
92 long long number;
93 char *string;
94 struct node_branch *branch;
95 struct node_ref *ref;
96 } v;
97 int lineno;
98 } YYSTYPE;
100 #if defined(__APPLE__) && !defined(YYSTYPE)
101 #warning "Setting YYSTYPE - is GNU Bison installed?"
102 #define YYSTYPE YYSTYPE
103 #endif
106 %token ERROR
107 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
108 %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS SIGNER_ID FETCH_ALL_BRANCHES
109 %token REFERENCE FETCH SEND
110 %token <v.string> STRING
111 %token <v.number> NUMBER
112 %type <v.number> boolean portplain
113 %type <v.string> numberstring
114 %type <v.branch> branch xbranch branch_list
115 %type <v.ref> ref xref ref_list
119 grammar : /* empty */
120 | grammar '\n'
121 | grammar author '\n'
122 | grammar remote '\n'
123 | grammar allowed_signers '\n'
124 | grammar revoked_signers '\n'
125 | grammar signer_id '\n'
127 boolean : STRING {
128 if (strcasecmp($1, "true") == 0 ||
129 strcasecmp($1, "yes") == 0)
130 $$ = 1;
131 else if (strcasecmp($1, "false") == 0 ||
132 strcasecmp($1, "no") == 0)
133 $$ = 0;
134 else {
135 yyerror("invalid boolean value '%s'", $1);
136 free($1);
137 YYERROR;
139 free($1);
142 numberstring : NUMBER {
143 char *s;
144 if (asprintf(&s, "%lld", $1) == -1) {
145 yyerror("string: asprintf");
146 YYERROR;
148 $$ = s;
150 | STRING
152 portplain : numberstring {
153 if (parseport($1, &$$) == -1) {
154 free($1);
155 YYERROR;
157 free($1);
160 branch : /* empty */ { $$ = NULL; }
161 | xbranch { $$ = $1; }
162 | '{' optnl branch_list '}' { $$ = $3; }
164 xbranch : STRING {
165 $$ = calloc(1, sizeof(struct node_branch));
166 if ($$ == NULL) {
167 yyerror("calloc");
168 YYERROR;
170 $$->branch_name = $1;
171 $$->tail = $$;
174 branch_list : xbranch optnl { $$ = $1; }
175 | branch_list comma xbranch optnl {
176 $1->tail->next = $3;
177 $1->tail = $3;
178 $$ = $1;
181 ref : /* empty */ { $$ = NULL; }
182 | xref { $$ = $1; }
183 | '{' optnl ref_list '}' { $$ = $3; }
185 xref : STRING {
186 $$ = calloc(1, sizeof(struct node_ref));
187 if ($$ == NULL) {
188 yyerror("calloc");
189 YYERROR;
191 $$->ref_name = $1;
192 $$->tail = $$;
195 ref_list : xref optnl { $$ = $1; }
196 | ref_list comma xref optnl {
197 $1->tail->next = $3;
198 $1->tail = $3;
199 $$ = $1;
202 remoteopts2 : remoteopts2 remoteopts1 nl
203 | remoteopts1 optnl
205 remoteopts1 : REPOSITORY STRING {
206 remote->repository = $2;
208 | SERVER STRING {
209 remote->server = $2;
211 | PROTOCOL STRING {
212 remote->protocol = $2;
214 | MIRROR_REFERENCES boolean {
215 remote->mirror_references = $2;
217 | FETCH_ALL_BRANCHES boolean {
218 remote->fetch_all_branches = $2;
220 | PORT portplain {
221 remote->port = $2;
223 | BRANCH branch {
224 remote->branch = $2;
226 | REFERENCE ref {
227 remote->fetch_ref = $2;
229 | FETCH {
230 static const struct got_error* error;
232 if (remote->fetch_config != NULL) {
233 yyerror("fetch block already exists");
234 YYERROR;
236 error = new_fetch_config(&remote->fetch_config);
237 if (error) {
238 yyerror("%s", error->msg);
239 YYERROR;
241 } '{' optnl fetchempty '}'
242 | SEND {
243 static const struct got_error* error;
245 if (remote->send_config != NULL) {
246 yyerror("send block already exists");
247 YYERROR;
249 error = new_send_config(&remote->send_config);
250 if (error) {
251 yyerror("%s", error->msg);
252 YYERROR;
254 } '{' optnl sendempty '}'
256 fetchempty : /* empty */
257 | fetchopts2
259 fetchopts2 : fetchopts2 fetchopts1 nl
260 | fetchopts1 optnl
262 fetchopts1 : REPOSITORY STRING {
263 remote->fetch_config->repository = $2;
265 | SERVER STRING {
266 remote->fetch_config->server = $2;
268 | PROTOCOL STRING {
269 remote->fetch_config->protocol = $2;
271 | PORT portplain {
272 remote->fetch_config->port = $2;
274 | BRANCH branch {
275 remote->fetch_config->branch = $2;
278 sendempty : /* empty */
279 | sendopts2
281 sendopts2 : sendopts2 sendopts1 nl
282 | sendopts1 optnl
284 sendopts1 : REPOSITORY STRING {
285 remote->send_config->repository = $2;
287 | SERVER STRING {
288 remote->send_config->server = $2;
290 | PROTOCOL STRING {
291 remote->send_config->protocol = $2;
293 | PORT portplain {
294 remote->send_config->port = $2;
296 | BRANCH branch {
297 remote->send_config->branch = $2;
300 remote : REMOTE STRING {
301 static const struct got_error* error;
303 error = new_remote(&remote);
304 if (error) {
305 free($2);
306 yyerror("%s", error->msg);
307 YYERROR;
309 remote->name = $2;
310 } '{' optnl remoteopts2 '}' {
311 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
312 gotconfig.nremotes++;
315 author : AUTHOR STRING {
316 gotconfig.author = $2;
319 allowed_signers : ALLOWED_SIGNERS STRING {
320 gotconfig.allowed_signers_file = $2;
323 revoked_signers : REVOKED_SIGNERS STRING {
324 gotconfig.revoked_signers_file = $2;
327 signer_id : SIGNER_ID STRING {
328 gotconfig.signer_id = $2;
331 optnl : '\n' optnl
332 | /* empty */
334 nl : '\n' optnl
336 comma : ','
337 | /* empty */
341 struct keywords {
342 const char *k_name;
343 int k_val;
347 yyerror(const char *fmt, ...)
349 va_list ap;
350 char *msg;
351 char *err = NULL;
353 va_start(ap, fmt);
354 if (vasprintf(&msg, fmt, ap) == -1) {
355 gerror = got_error_from_errno("vasprintf");
356 return 0;
358 va_end(ap);
359 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
360 msg) == -1) {
361 gerror = got_error_from_errno("asprintf");
362 return(0);
364 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
365 free(msg);
366 return(0);
369 kw_cmp(const void *k, const void *e)
371 return (strcmp(k, ((const struct keywords *)e)->k_name));
375 lookup(char *s)
377 /* This has to be sorted always. */
378 static const struct keywords keywords[] = {
379 {"allowed_signers", ALLOWED_SIGNERS},
380 {"author", AUTHOR},
381 {"branch", BRANCH},
382 {"fetch", FETCH},
383 {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
384 {"fetch_all_branches", FETCH_ALL_BRANCHES},
385 {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
386 {"mirror_references", MIRROR_REFERENCES},
387 {"port", PORT},
388 {"protocol", PROTOCOL},
389 {"reference", REFERENCE},
390 {"remote", REMOTE},
391 {"repository", REPOSITORY},
392 {"revoked_signers", REVOKED_SIGNERS},
393 {"send", SEND},
394 {"server", SERVER},
395 {"signer_id", SIGNER_ID},
397 const struct keywords *p;
399 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
400 sizeof(keywords[0]), kw_cmp);
402 if (p)
403 return (p->k_val);
404 else
405 return (STRING);
408 #define START_EXPAND 1
409 #define DONE_EXPAND 2
411 static int expanding;
414 igetc(void)
416 int c;
418 while (1) {
419 if (file->ungetpos > 0)
420 c = file->ungetbuf[--file->ungetpos];
421 else
422 c = getc(file->stream);
424 if (c == START_EXPAND)
425 expanding = 1;
426 else if (c == DONE_EXPAND)
427 expanding = 0;
428 else
429 break;
431 return (c);
435 lgetc(int quotec)
437 int c, next;
439 if (quotec) {
440 c = igetc();
441 if (c == EOF) {
442 yyerror("reached end of file while parsing "
443 "quoted string");
445 return (c);
448 c = igetc();
449 while (c == '\\') {
450 next = igetc();
451 if (next != '\n') {
452 c = next;
453 break;
455 yylval.lineno = file->lineno;
456 file->lineno++;
459 return (c);
462 void
463 lungetc(int c)
465 if (c == EOF)
466 return;
468 if (file->ungetpos >= file->ungetsize) {
469 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
470 if (p == NULL)
471 err(1, "%s", __func__);
472 file->ungetbuf = p;
473 file->ungetsize *= 2;
475 file->ungetbuf[file->ungetpos++] = c;
479 findeol(void)
481 int c;
483 /* Skip to either EOF or the first real EOL. */
484 while (1) {
485 c = lgetc(0);
486 if (c == '\n') {
487 file->lineno++;
488 break;
490 if (c == EOF)
491 break;
493 return (ERROR);
496 static long long
497 getservice(char *n)
499 struct servent *s;
500 u_long ulval;
502 if (atoul(n, &ulval) == 0) {
503 if (ulval == 0 || ulval > 65535) {
504 yyerror("illegal port value %lu", ulval);
505 return (-1);
507 return ulval;
508 } else {
509 s = getservbyname(n, "tcp");
510 if (s == NULL)
511 s = getservbyname(n, "udp");
512 if (s == NULL) {
513 yyerror("unknown port %s", n);
514 return (-1);
516 return (s->s_port);
520 static int
521 parseport(char *port, long long *pn)
523 if ((*pn = getservice(port)) == -1) {
524 *pn = 0LL;
525 return (-1);
527 return (0);
532 yylex(void)
534 char buf[8096];
535 char *p, *val;
536 int quotec, next, c;
537 int token;
539 top:
540 p = buf;
541 c = lgetc(0);
542 while (c == ' ' || c == '\t')
543 c = lgetc(0); /* nothing */
545 yylval.lineno = file->lineno;
546 if (c == '#') {
547 c = lgetc(0);
548 while (c != '\n' && c != EOF)
549 c = lgetc(0); /* nothing */
551 if (c == '$' && !expanding) {
552 while (1) {
553 c = lgetc(0);
554 if (c == EOF)
555 return (0);
557 if (p + 1 >= buf + sizeof(buf) - 1) {
558 yyerror("string too long");
559 return (findeol());
561 if (isalnum(c) || c == '_') {
562 *p++ = c;
563 continue;
565 *p = '\0';
566 lungetc(c);
567 break;
569 val = symget(buf);
570 if (val == NULL) {
571 yyerror("macro '%s' not defined", buf);
572 return (findeol());
574 p = val + strlen(val) - 1;
575 lungetc(DONE_EXPAND);
576 while (p >= val) {
577 lungetc((unsigned char)*p);
578 p--;
580 lungetc(START_EXPAND);
581 goto top;
584 switch (c) {
585 case '\'':
586 case '"':
587 quotec = c;
588 while (1) {
589 c = lgetc(quotec);
590 if (c == EOF)
591 return (0);
592 if (c == '\n') {
593 file->lineno++;
594 continue;
595 } else if (c == '\\') {
596 next = lgetc(quotec);
597 if (next == EOF)
598 return (0);
599 if (next == quotec || c == ' ' || c == '\t')
600 c = next;
601 else if (next == '\n') {
602 file->lineno++;
603 continue;
604 } else
605 lungetc(next);
606 } else if (c == quotec) {
607 *p = '\0';
608 break;
609 } else if (c == '\0') {
610 yyerror("syntax error");
611 return (findeol());
613 if (p + 1 >= buf + sizeof(buf) - 1) {
614 yyerror("string too long");
615 return (findeol());
617 *p++ = c;
619 yylval.v.string = strdup(buf);
620 if (yylval.v.string == NULL)
621 err(1, "%s", __func__);
622 return (STRING);
625 #define allowed_to_end_number(x) \
626 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
628 if (c == '-' || isdigit(c)) {
629 do {
630 *p++ = c;
631 if ((size_t)(p-buf) >= sizeof(buf)) {
632 yyerror("string too long");
633 return (findeol());
635 c = lgetc(0);
636 } while (c != EOF && isdigit(c));
637 lungetc(c);
638 if (p == buf + 1 && buf[0] == '-')
639 goto nodigits;
640 if (c == EOF || allowed_to_end_number(c)) {
641 const char *errstr = NULL;
643 *p = '\0';
644 yylval.v.number = strtonum(buf, LLONG_MIN,
645 LLONG_MAX, &errstr);
646 if (errstr) {
647 yyerror("\"%s\" invalid number: %s",
648 buf, errstr);
649 return (findeol());
651 return (NUMBER);
652 } else {
653 nodigits:
654 while (p > buf + 1)
655 lungetc((unsigned char)*--p);
656 c = (unsigned char)*--p;
657 if (c == '-')
658 return (c);
662 #define allowed_in_string(x) \
663 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
664 x != '{' && x != '}' && \
665 x != '!' && x != '=' && x != '#' && \
666 x != ','))
668 if (isalnum(c) || c == ':' || c == '_') {
669 do {
670 *p++ = c;
671 if ((size_t)(p-buf) >= sizeof(buf)) {
672 yyerror("string too long");
673 return (findeol());
675 c = lgetc(0);
676 } while (c != EOF && (allowed_in_string(c)));
677 lungetc(c);
678 *p = '\0';
679 token = lookup(buf);
680 if (token == STRING) {
681 yylval.v.string = strdup(buf);
682 if (yylval.v.string == NULL)
683 err(1, "%s", __func__);
685 return (token);
687 if (c == '\n') {
688 yylval.lineno = file->lineno;
689 file->lineno++;
691 if (c == EOF)
692 return (0);
693 return (c);
696 static const struct got_error*
697 newfile(struct file **nfile, const char *filename, int *fd)
699 const struct got_error* error = NULL;
701 (*nfile) = calloc(1, sizeof(struct file));
702 if ((*nfile) == NULL)
703 return got_error_from_errno("calloc");
704 (*nfile)->stream = fdopen(*fd, "r");
705 if ((*nfile)->stream == NULL) {
706 error = got_error_from_errno("fdopen");
707 free((*nfile));
708 return error;
710 *fd = -1; /* Stream owns the file descriptor now. */
711 (*nfile)->name = filename;
712 (*nfile)->lineno = 1;
713 (*nfile)->ungetsize = 16;
714 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
715 if ((*nfile)->ungetbuf == NULL) {
716 error = got_error_from_errno("malloc");
717 fclose((*nfile)->stream);
718 free((*nfile));
719 return error;
721 return NULL;
724 static const struct got_error*
725 new_remote(struct gotconfig_remote_repo **remote)
727 const struct got_error *error = NULL;
729 *remote = calloc(1, sizeof(**remote));
730 if (*remote == NULL)
731 error = got_error_from_errno("calloc");
732 return error;
735 static const struct got_error*
736 new_fetch_config(struct fetch_config **fetch_config)
738 const struct got_error *error = NULL;
740 *fetch_config = calloc(1, sizeof(**fetch_config));
741 if (*fetch_config == NULL)
742 error = got_error_from_errno("calloc");
743 return error;
746 static const struct got_error*
747 new_send_config(struct send_config **send_config)
749 const struct got_error *error = NULL;
751 *send_config = calloc(1, sizeof(**send_config));
752 if (*send_config == NULL)
753 error = got_error_from_errno("calloc");
754 return error;
757 static void
758 closefile(struct file *file)
760 fclose(file->stream);
761 free(file->ungetbuf);
762 free(file);
765 const struct got_error *
766 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
768 const struct got_error *err = NULL;
769 struct sym *sym, *next;
771 *conf = NULL;
773 err = newfile(&file, filename, fd);
774 if (err)
775 return err;
777 TAILQ_INIT(&gotconfig.remotes);
779 yyparse();
780 closefile(file);
782 /* Free macros and check which have not been used. */
783 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
784 if (!sym->persist) {
785 free(sym->nam);
786 free(sym->val);
787 TAILQ_REMOVE(&symhead, sym, entry);
788 free(sym);
792 if (gerror == NULL)
793 *conf = &gotconfig;
794 return gerror;
797 static void
798 free_fetch_config(struct fetch_config *fetch_config)
800 free(remote->fetch_config->repository);
801 free(remote->fetch_config->server);
802 free(remote->fetch_config->protocol);
803 free(remote->fetch_config);
806 static void
807 free_send_config(struct send_config *send_config)
809 free(remote->send_config->repository);
810 free(remote->send_config->server);
811 free(remote->send_config->protocol);
812 free(remote->send_config);
815 void
816 gotconfig_free(struct gotconfig *conf)
818 struct gotconfig_remote_repo *remote;
820 free(conf->author);
821 free(conf->allowed_signers_file);
822 free(conf->revoked_signers_file);
823 free(conf->signer_id);
824 while (!TAILQ_EMPTY(&conf->remotes)) {
825 remote = TAILQ_FIRST(&conf->remotes);
826 TAILQ_REMOVE(&conf->remotes, remote, entry);
827 if (remote->fetch_config != NULL)
828 free_fetch_config(remote->fetch_config);
829 if (remote->send_config != NULL)
830 free_send_config(remote->send_config);
831 free(remote->name);
832 free(remote->repository);
833 free(remote->server);
834 free(remote->protocol);
835 free(remote);
840 symset(const char *nam, const char *val, int persist)
842 struct sym *sym;
844 TAILQ_FOREACH(sym, &symhead, entry) {
845 if (strcmp(nam, sym->nam) == 0)
846 break;
849 if (sym != NULL) {
850 if (sym->persist == 1)
851 return (0);
852 else {
853 free(sym->nam);
854 free(sym->val);
855 TAILQ_REMOVE(&symhead, sym, entry);
856 free(sym);
859 sym = calloc(1, sizeof(*sym));
860 if (sym == NULL)
861 return (-1);
863 sym->nam = strdup(nam);
864 if (sym->nam == NULL) {
865 free(sym);
866 return (-1);
868 sym->val = strdup(val);
869 if (sym->val == NULL) {
870 free(sym->nam);
871 free(sym);
872 return (-1);
874 sym->used = 0;
875 sym->persist = persist;
876 TAILQ_INSERT_TAIL(&symhead, sym, entry);
877 return (0);
881 cmdline_symset(char *s)
883 char *sym, *val;
884 int ret;
885 size_t len;
887 val = strrchr(s, '=');
888 if (val == NULL)
889 return (-1);
891 len = strlen(s) - strlen(val) + 1;
892 sym = malloc(len);
893 if (sym == NULL)
894 errx(1, "cmdline_symset: malloc");
896 strlcpy(sym, s, len);
898 ret = symset(sym, val + 1, 1);
899 free(sym);
901 return (ret);
904 char *
905 symget(const char *nam)
907 struct sym *sym;
909 TAILQ_FOREACH(sym, &symhead, entry) {
910 if (strcmp(nam, sym->nam) == 0) {
911 sym->used = 1;
912 return (sym->val);
915 return (NULL);
918 static int
919 atoul(char *s, u_long *ulvalp)
921 u_long ulval;
922 char *ep;
924 errno = 0;
925 ulval = strtoul(s, &ep, 0);
926 if (s[0] == '\0' || *ep != '\0')
927 return (-1);
928 if (errno == ERANGE && ulval == ULONG_MAX)
929 return (-1);
930 *ulvalp = ulval;
931 return (0);