sync with OpenBSD parse.y
[got-portable.git] / libexec / got-read-gotconfig / parse.y
blobdeabda5d8aec84464b9d75cbacf2ae77e665af58
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 <sys/types.h>
27 #include <netdb.h>
29 #include <ctype.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "got_compat.h"
40 #include "got_error.h"
41 #include "gotconfig.h"
43 static struct file {
44 FILE *stream;
45 const char *name;
46 size_t ungetpos;
47 size_t ungetsize;
48 u_char *ungetbuf;
49 int eof_reached;
50 int lineno;
51 } *file;
52 static const struct got_error* newfile(struct file**, const char *, int *);
53 static void closefile(struct file *);
54 int yyparse(void);
55 int yylex(void);
56 int yyerror(const char *, ...)
57 __attribute__((__format__ (printf, 1, 2)))
58 __attribute__((__nonnull__ (1)));
59 int kw_cmp(const void *, const void *);
60 int lookup(char *);
61 int igetc(void);
62 int lgetc(int);
63 void lungetc(int);
64 int findeol(void);
65 static int parseport(char *, long long *);
67 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 struct sym {
69 TAILQ_ENTRY(sym) entry;
70 int used;
71 int persist;
72 char *nam;
73 char *val;
76 int symset(const char *, const char *, int);
77 char *symget(const char *);
79 static int atoul(char *, u_long *);
81 static const struct got_error* gerror;
82 static struct gotconfig_remote_repo *remote;
83 static struct gotconfig gotconfig;
84 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
85 static const struct got_error* new_fetch_config(struct fetch_config **);
86 static const struct got_error* new_send_config(struct send_config **);
88 typedef struct {
89 union {
90 long long number;
91 char *string;
92 struct node_branch *branch;
93 struct node_ref *ref;
94 } v;
95 int lineno;
96 } YYSTYPE;
100 %token ERROR
101 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
102 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
103 %token <v.string> STRING
104 %token <v.number> NUMBER
105 %type <v.number> boolean portplain
106 %type <v.string> numberstring
107 %type <v.branch> branch xbranch branch_list
108 %type <v.ref> ref xref ref_list
112 grammar : /* empty */
113 | grammar '\n'
114 | grammar author '\n'
115 | grammar remote '\n'
117 boolean : STRING {
118 if (strcasecmp($1, "true") == 0 ||
119 strcasecmp($1, "yes") == 0)
120 $$ = 1;
121 else if (strcasecmp($1, "false") == 0 ||
122 strcasecmp($1, "no") == 0)
123 $$ = 0;
124 else {
125 yyerror("invalid boolean value '%s'", $1);
126 free($1);
127 YYERROR;
129 free($1);
132 numberstring : NUMBER {
133 char *s;
134 if (asprintf(&s, "%lld", $1) == -1) {
135 yyerror("string: asprintf");
136 YYERROR;
138 $$ = s;
140 | STRING
142 portplain : numberstring {
143 if (parseport($1, &$$) == -1) {
144 free($1);
145 YYERROR;
147 free($1);
150 branch : /* empty */ { $$ = NULL; }
151 | xbranch { $$ = $1; }
152 | '{' optnl branch_list '}' { $$ = $3; }
154 xbranch : STRING {
155 $$ = calloc(1, sizeof(struct node_branch));
156 if ($$ == NULL) {
157 yyerror("calloc");
158 YYERROR;
160 $$->branch_name = $1;
161 $$->tail = $$;
164 branch_list : xbranch optnl { $$ = $1; }
165 | branch_list comma xbranch optnl {
166 $1->tail->next = $3;
167 $1->tail = $3;
168 $$ = $1;
171 ref : /* empty */ { $$ = NULL; }
172 | xref { $$ = $1; }
173 | '{' optnl ref_list '}' { $$ = $3; }
175 xref : STRING {
176 $$ = calloc(1, sizeof(struct node_ref));
177 if ($$ == NULL) {
178 yyerror("calloc");
179 YYERROR;
181 $$->ref_name = $1;
182 $$->tail = $$;
185 ref_list : xref optnl { $$ = $1; }
186 | ref_list comma xref optnl {
187 $1->tail->next = $3;
188 $1->tail = $3;
189 $$ = $1;
192 remoteopts2 : remoteopts2 remoteopts1 nl
193 | remoteopts1 optnl
195 remoteopts1 : REPOSITORY STRING {
196 remote->repository = $2;
198 | SERVER STRING {
199 remote->server = $2;
201 | PROTOCOL STRING {
202 remote->protocol = $2;
204 | MIRROR_REFERENCES boolean {
205 remote->mirror_references = $2;
207 | FETCH_ALL_BRANCHES boolean {
208 remote->fetch_all_branches = $2;
210 | PORT portplain {
211 remote->port = $2;
213 | BRANCH branch {
214 remote->branch = $2;
216 | REFERENCE ref {
217 remote->fetch_ref = $2;
219 | FETCH {
220 static const struct got_error* error;
222 if (remote->fetch_config != NULL) {
223 yyerror("fetch block already exists");
224 YYERROR;
226 error = new_fetch_config(&remote->fetch_config);
227 if (error) {
228 yyerror("%s", error->msg);
229 YYERROR;
231 } '{' optnl fetchempty '}'
232 | SEND {
233 static const struct got_error* error;
235 if (remote->send_config != NULL) {
236 yyerror("send block already exists");
237 YYERROR;
239 error = new_send_config(&remote->send_config);
240 if (error) {
241 yyerror("%s", error->msg);
242 YYERROR;
244 } '{' optnl sendempty '}'
246 fetchempty : /* empty */
247 | fetchopts2
249 fetchopts2 : fetchopts2 fetchopts1 nl
250 | fetchopts1 optnl
252 fetchopts1 : REPOSITORY STRING {
253 remote->fetch_config->repository = $2;
255 | SERVER STRING {
256 remote->fetch_config->server = $2;
258 | PROTOCOL STRING {
259 remote->fetch_config->protocol = $2;
261 | PORT portplain {
262 remote->fetch_config->port = $2;
264 | BRANCH branch {
265 remote->fetch_config->branch = $2;
268 sendempty : /* empty */
269 | sendopts2
271 sendopts2 : sendopts2 sendopts1 nl
272 | sendopts1 optnl
274 sendopts1 : REPOSITORY STRING {
275 remote->send_config->repository = $2;
277 | SERVER STRING {
278 remote->send_config->server = $2;
280 | PROTOCOL STRING {
281 remote->send_config->protocol = $2;
283 | PORT portplain {
284 remote->send_config->port = $2;
286 | BRANCH branch {
287 remote->send_config->branch = $2;
290 remote : REMOTE STRING {
291 static const struct got_error* error;
293 error = new_remote(&remote);
294 if (error) {
295 free($2);
296 yyerror("%s", error->msg);
297 YYERROR;
299 remote->name = $2;
300 } '{' optnl remoteopts2 '}' {
301 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
302 gotconfig.nremotes++;
305 author : AUTHOR STRING {
306 gotconfig.author = $2;
309 optnl : '\n' optnl
310 | /* empty */
312 nl : '\n' optnl
314 comma : ','
315 | /* empty */
319 struct keywords {
320 const char *k_name;
321 int k_val;
325 yyerror(const char *fmt, ...)
327 va_list ap;
328 char *msg;
329 char *err = NULL;
331 va_start(ap, fmt);
332 if (vasprintf(&msg, fmt, ap) == -1) {
333 gerror = got_error_from_errno("vasprintf");
334 return 0;
336 va_end(ap);
337 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
338 msg) == -1) {
339 gerror = got_error_from_errno("asprintf");
340 return(0);
342 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
343 free(msg);
344 return(0);
347 kw_cmp(const void *k, const void *e)
349 return (strcmp(k, ((const struct keywords *)e)->k_name));
353 lookup(char *s)
355 /* This has to be sorted always. */
356 static const struct keywords keywords[] = {
357 {"author", AUTHOR},
358 {"branch", BRANCH},
359 {"fetch", FETCH},
360 {"fetch-all-branches", FETCH_ALL_BRANCHES},
361 {"mirror-references", MIRROR_REFERENCES},
362 {"port", PORT},
363 {"protocol", PROTOCOL},
364 {"reference", REFERENCE},
365 {"remote", REMOTE},
366 {"repository", REPOSITORY},
367 {"send", SEND},
368 {"server", SERVER},
370 const struct keywords *p;
372 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
373 sizeof(keywords[0]), kw_cmp);
375 if (p)
376 return (p->k_val);
377 else
378 return (STRING);
381 #define START_EXPAND 1
382 #define DONE_EXPAND 2
384 static int expanding;
387 igetc(void)
389 int c;
391 while (1) {
392 if (file->ungetpos > 0)
393 c = file->ungetbuf[--file->ungetpos];
394 else
395 c = getc(file->stream);
397 if (c == START_EXPAND)
398 expanding = 1;
399 else if (c == DONE_EXPAND)
400 expanding = 0;
401 else
402 break;
404 return (c);
408 lgetc(int quotec)
410 int c, next;
412 if (quotec) {
413 c = igetc();
414 if (c == EOF) {
415 yyerror("reached end of file while parsing "
416 "quoted string");
418 return (c);
421 c = igetc();
422 while (c == '\\') {
423 next = igetc();
424 if (next != '\n') {
425 c = next;
426 break;
428 yylval.lineno = file->lineno;
429 file->lineno++;
432 return (c);
435 void
436 lungetc(int c)
438 if (c == EOF)
439 return;
441 if (file->ungetpos >= file->ungetsize) {
442 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
443 if (p == NULL)
444 err(1, "%s", __func__);
445 file->ungetbuf = p;
446 file->ungetsize *= 2;
448 file->ungetbuf[file->ungetpos++] = c;
452 findeol(void)
454 int c;
456 /* Skip to either EOF or the first real EOL. */
457 while (1) {
458 c = lgetc(0);
459 if (c == '\n') {
460 file->lineno++;
461 break;
463 if (c == EOF)
464 break;
466 return (ERROR);
469 static long long
470 getservice(char *n)
472 struct servent *s;
473 u_long ulval;
475 if (atoul(n, &ulval) == 0) {
476 if (ulval == 0 || ulval > 65535) {
477 yyerror("illegal port value %lu", ulval);
478 return (-1);
480 return ulval;
481 } else {
482 s = getservbyname(n, "tcp");
483 if (s == NULL)
484 s = getservbyname(n, "udp");
485 if (s == NULL) {
486 yyerror("unknown port %s", n);
487 return (-1);
489 return (s->s_port);
493 static int
494 parseport(char *port, long long *pn)
496 if ((*pn = getservice(port)) == -1) {
497 *pn = 0LL;
498 return (-1);
500 return (0);
505 yylex(void)
507 char buf[8096];
508 char *p, *val;
509 int quotec, next, c;
510 int token;
512 top:
513 p = buf;
514 c = lgetc(0);
515 while (c == ' ' || c == '\t')
516 c = lgetc(0); /* nothing */
518 yylval.lineno = file->lineno;
519 if (c == '#') {
520 c = lgetc(0);
521 while (c != '\n' && c != EOF)
522 c = lgetc(0); /* nothing */
524 if (c == '$' && !expanding) {
525 while (1) {
526 c = lgetc(0);
527 if (c == EOF)
528 return (0);
530 if (p + 1 >= buf + sizeof(buf) - 1) {
531 yyerror("string too long");
532 return (findeol());
534 if (isalnum(c) || c == '_') {
535 *p++ = c;
536 continue;
538 *p = '\0';
539 lungetc(c);
540 break;
542 val = symget(buf);
543 if (val == NULL) {
544 yyerror("macro '%s' not defined", buf);
545 return (findeol());
547 p = val + strlen(val) - 1;
548 lungetc(DONE_EXPAND);
549 while (p >= val) {
550 lungetc((unsigned char)*p);
551 p--;
553 lungetc(START_EXPAND);
554 goto top;
557 switch (c) {
558 case '\'':
559 case '"':
560 quotec = c;
561 while (1) {
562 c = lgetc(quotec);
563 if (c == EOF)
564 return (0);
565 if (c == '\n') {
566 file->lineno++;
567 continue;
568 } else if (c == '\\') {
569 next = lgetc(quotec);
570 if (next == EOF)
571 return (0);
572 if (next == quotec || c == ' ' || c == '\t')
573 c = next;
574 else if (next == '\n') {
575 file->lineno++;
576 continue;
577 } else
578 lungetc(next);
579 } else if (c == quotec) {
580 *p = '\0';
581 break;
582 } else if (c == '\0') {
583 yyerror("syntax error");
584 return (findeol());
586 if (p + 1 >= buf + sizeof(buf) - 1) {
587 yyerror("string too long");
588 return (findeol());
590 *p++ = c;
592 yylval.v.string = strdup(buf);
593 if (yylval.v.string == NULL)
594 err(1, "%s", __func__);
595 return (STRING);
598 #define allowed_to_end_number(x) \
599 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
601 if (c == '-' || isdigit(c)) {
602 do {
603 *p++ = c;
604 if ((size_t)(p-buf) >= sizeof(buf)) {
605 yyerror("string too long");
606 return (findeol());
608 c = lgetc(0);
609 } while (c != EOF && isdigit(c));
610 lungetc(c);
611 if (p == buf + 1 && buf[0] == '-')
612 goto nodigits;
613 if (c == EOF || allowed_to_end_number(c)) {
614 const char *errstr = NULL;
616 *p = '\0';
617 yylval.v.number = strtonum(buf, LLONG_MIN,
618 LLONG_MAX, &errstr);
619 if (errstr) {
620 yyerror("\"%s\" invalid number: %s",
621 buf, errstr);
622 return (findeol());
624 return (NUMBER);
625 } else {
626 nodigits:
627 while (p > buf + 1)
628 lungetc((unsigned char)*--p);
629 c = (unsigned char)*--p;
630 if (c == '-')
631 return (c);
635 #define allowed_in_string(x) \
636 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
637 x != '{' && x != '}' && \
638 x != '!' && x != '=' && x != '#' && \
639 x != ','))
641 if (isalnum(c) || c == ':' || c == '_') {
642 do {
643 *p++ = c;
644 if ((size_t)(p-buf) >= sizeof(buf)) {
645 yyerror("string too long");
646 return (findeol());
648 c = lgetc(0);
649 } while (c != EOF && (allowed_in_string(c)));
650 lungetc(c);
651 *p = '\0';
652 token = lookup(buf);
653 if (token == STRING) {
654 yylval.v.string = strdup(buf);
655 if (yylval.v.string == NULL)
656 err(1, "%s", __func__);
658 return (token);
660 if (c == '\n') {
661 yylval.lineno = file->lineno;
662 file->lineno++;
664 if (c == EOF)
665 return (0);
666 return (c);
669 static const struct got_error*
670 newfile(struct file **nfile, const char *filename, int *fd)
672 const struct got_error* error = NULL;
674 (*nfile) = calloc(1, sizeof(struct file));
675 if ((*nfile) == NULL)
676 return got_error_from_errno("calloc");
677 (*nfile)->stream = fdopen(*fd, "r");
678 if ((*nfile)->stream == NULL) {
679 error = got_error_from_errno("fdopen");
680 free((*nfile));
681 return error;
683 *fd = -1; /* Stream owns the file descriptor now. */
684 (*nfile)->name = filename;
685 (*nfile)->lineno = 1;
686 (*nfile)->ungetsize = 16;
687 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
688 if ((*nfile)->ungetbuf == NULL) {
689 error = got_error_from_errno("malloc");
690 fclose((*nfile)->stream);
691 free((*nfile));
692 return error;
694 return NULL;
697 static const struct got_error*
698 new_remote(struct gotconfig_remote_repo **remote)
700 const struct got_error *error = NULL;
702 *remote = calloc(1, sizeof(**remote));
703 if (*remote == NULL)
704 error = got_error_from_errno("calloc");
705 return error;
708 static const struct got_error*
709 new_fetch_config(struct fetch_config **fetch_config)
711 const struct got_error *error = NULL;
713 *fetch_config = calloc(1, sizeof(**fetch_config));
714 if (*fetch_config == NULL)
715 error = got_error_from_errno("calloc");
716 return error;
719 static const struct got_error*
720 new_send_config(struct send_config **send_config)
722 const struct got_error *error = NULL;
724 *send_config = calloc(1, sizeof(**send_config));
725 if (*send_config == NULL)
726 error = got_error_from_errno("calloc");
727 return error;
730 static void
731 closefile(struct file *file)
733 fclose(file->stream);
734 free(file->ungetbuf);
735 free(file);
738 const struct got_error *
739 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
741 const struct got_error *err = NULL;
742 struct sym *sym, *next;
744 *conf = NULL;
746 err = newfile(&file, filename, fd);
747 if (err)
748 return err;
750 TAILQ_INIT(&gotconfig.remotes);
752 yyparse();
753 closefile(file);
755 /* Free macros and check which have not been used. */
756 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
757 if (!sym->persist) {
758 free(sym->nam);
759 free(sym->val);
760 TAILQ_REMOVE(&symhead, sym, entry);
761 free(sym);
765 if (gerror == NULL)
766 *conf = &gotconfig;
767 return gerror;
770 static void
771 free_fetch_config(struct fetch_config *fetch_config)
773 free(remote->fetch_config->repository);
774 free(remote->fetch_config->server);
775 free(remote->fetch_config->protocol);
776 free(remote->fetch_config);
779 static void
780 free_send_config(struct send_config *send_config)
782 free(remote->send_config->repository);
783 free(remote->send_config->server);
784 free(remote->send_config->protocol);
785 free(remote->send_config);
788 void
789 gotconfig_free(struct gotconfig *conf)
791 struct gotconfig_remote_repo *remote;
793 free(conf->author);
794 while (!TAILQ_EMPTY(&conf->remotes)) {
795 remote = TAILQ_FIRST(&conf->remotes);
796 TAILQ_REMOVE(&conf->remotes, remote, entry);
797 if (remote->fetch_config != NULL)
798 free_fetch_config(remote->fetch_config);
799 if (remote->send_config != NULL)
800 free_send_config(remote->send_config);
801 free(remote->name);
802 free(remote->repository);
803 free(remote->server);
804 free(remote->protocol);
805 free(remote);
810 symset(const char *nam, const char *val, int persist)
812 struct sym *sym;
814 TAILQ_FOREACH(sym, &symhead, entry) {
815 if (strcmp(nam, sym->nam) == 0)
816 break;
819 if (sym != NULL) {
820 if (sym->persist == 1)
821 return (0);
822 else {
823 free(sym->nam);
824 free(sym->val);
825 TAILQ_REMOVE(&symhead, sym, entry);
826 free(sym);
829 sym = calloc(1, sizeof(*sym));
830 if (sym == NULL)
831 return (-1);
833 sym->nam = strdup(nam);
834 if (sym->nam == NULL) {
835 free(sym);
836 return (-1);
838 sym->val = strdup(val);
839 if (sym->val == NULL) {
840 free(sym->nam);
841 free(sym);
842 return (-1);
844 sym->used = 0;
845 sym->persist = persist;
846 TAILQ_INSERT_TAIL(&symhead, sym, entry);
847 return (0);
851 cmdline_symset(char *s)
853 char *sym, *val;
854 int ret;
855 size_t len;
857 val = strrchr(s, '=');
858 if (val == NULL)
859 return (-1);
861 len = strlen(s) - strlen(val) + 1;
862 sym = malloc(len);
863 if (sym == NULL)
864 errx(1, "cmdline_symset: malloc");
866 strlcpy(sym, s, len);
868 ret = symset(sym, val + 1, 1);
869 free(sym);
871 return (ret);
874 char *
875 symget(const char *nam)
877 struct sym *sym;
879 TAILQ_FOREACH(sym, &symhead, entry) {
880 if (strcmp(nam, sym->nam) == 0) {
881 sym->used = 1;
882 return (sym->val);
885 return (NULL);
888 static int
889 atoul(char *s, u_long *ulvalp)
891 u_long ulval;
892 char *ep;
894 errno = 0;
895 ulval = strtoul(s, &ep, 0);
896 if (s[0] == '\0' || *ep != '\0')
897 return (-1);
898 if (errno == ERANGE && ulval == ULONG_MAX)
899 return (-1);
900 *ulvalp = ulval;
901 return (0);