Blob


1 /*
2 * Copyright (c) 2020 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 **);
85 typedef struct {
86 union {
87 long long number;
88 char *string;
89 struct node_branch *branch;
90 struct node_ref *ref;
91 } v;
92 int lineno;
93 } YYSTYPE;
95 %}
97 %token ERROR
98 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
99 %token AUTHOR FETCH_ALL_BRANCHES REFERENCE
100 %token <v.string> STRING
101 %token <v.number> NUMBER
102 %type <v.number> boolean portplain
103 %type <v.string> numberstring
104 %type <v.branch> branch xbranch branch_list
105 %type <v.ref> ref xref ref_list
107 %%
109 grammar : /* empty */
110 | grammar '\n'
111 | grammar author '\n'
112 | grammar remote '\n'
114 boolean : STRING {
115 if (strcasecmp($1, "true") == 0 ||
116 strcasecmp($1, "yes") == 0)
117 $$ = 1;
118 else if (strcasecmp($1, "false") == 0 ||
119 strcasecmp($1, "no") == 0)
120 $$ = 0;
121 else {
122 yyerror("invalid boolean value '%s'", $1);
123 free($1);
124 YYERROR;
126 free($1);
129 numberstring : NUMBER {
130 char *s;
131 if (asprintf(&s, "%lld", $1) == -1) {
132 yyerror("string: asprintf");
133 YYERROR;
135 $$ = s;
137 | STRING
139 portplain : numberstring {
140 if (parseport($1, &$$) == -1) {
141 free($1);
142 YYERROR;
144 free($1);
147 branch : /* empty */ { $$ = NULL; }
148 | xbranch { $$ = $1; }
149 | '{' optnl branch_list '}' { $$ = $3; }
151 xbranch : STRING {
152 $$ = calloc(1, sizeof(struct node_branch));
153 if ($$ == NULL) {
154 yyerror("calloc");
155 YYERROR;
157 $$->branch_name = $1;
158 $$->tail = $$;
161 branch_list : xbranch optnl { $$ = $1; }
162 | branch_list comma xbranch optnl {
163 $1->tail->next = $3;
164 $1->tail = $3;
165 $$ = $1;
168 ref : /* empty */ { $$ = NULL; }
169 | xref { $$ = $1; }
170 | '{' optnl ref_list '}' { $$ = $3; }
172 xref : STRING {
173 $$ = calloc(1, sizeof(struct node_ref));
174 if ($$ == NULL) {
175 yyerror("calloc");
176 YYERROR;
178 $$->ref_name = $1;
179 $$->tail = $$;
182 ref_list : xref optnl { $$ = $1; }
183 | ref_list comma xref optnl {
184 $1->tail->next = $3;
185 $1->tail = $3;
186 $$ = $1;
189 remoteopts2 : remoteopts2 remoteopts1 nl
190 | remoteopts1 optnl
192 remoteopts1 : REPOSITORY STRING {
193 remote->repository = strdup($2);
194 if (remote->repository == NULL) {
195 free($2);
196 yyerror("strdup");
197 YYERROR;
199 free($2);
201 | SERVER STRING {
202 remote->server = strdup($2);
203 if (remote->server == NULL) {
204 free($2);
205 yyerror("strdup");
206 YYERROR;
208 free($2);
210 | PROTOCOL STRING {
211 remote->protocol = strdup($2);
212 if (remote->protocol == NULL) {
213 free($2);
214 yyerror("strdup");
215 YYERROR;
217 free($2);
219 | MIRROR_REFERENCES boolean {
220 remote->mirror_references = $2;
222 | FETCH_ALL_BRANCHES boolean {
223 remote->fetch_all_branches = $2;
225 | PORT portplain {
226 remote->port = $2;
228 | BRANCH branch {
229 remote->branch = $2;
231 | REFERENCE ref {
232 remote->ref = $2;
235 remote : REMOTE STRING {
236 static const struct got_error* error;
238 error = new_remote(&remote);
239 if (error) {
240 free($2);
241 yyerror("%s", error->msg);
242 YYERROR;
244 remote->name = strdup($2);
245 if (remote->name == NULL) {
246 free($2);
247 yyerror("strdup");
248 YYERROR;
250 free($2);
251 } '{' optnl remoteopts2 '}' {
252 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
253 gotconfig.nremotes++;
256 author : AUTHOR STRING {
257 gotconfig.author = strdup($2);
258 if (gotconfig.author == NULL) {
259 free($2);
260 yyerror("strdup");
261 YYERROR;
263 free($2);
266 optnl : '\n' optnl
267 | /* empty */
269 nl : '\n' optnl
271 comma : ','
272 | /* empty */
274 %%
276 struct keywords {
277 const char *k_name;
278 int k_val;
279 };
281 int
282 yyerror(const char *fmt, ...)
284 va_list ap;
285 char *msg;
286 char *err = NULL;
288 va_start(ap, fmt);
289 if (vasprintf(&msg, fmt, ap) == -1) {
290 gerror = got_error_from_errno("vasprintf");
291 return 0;
293 va_end(ap);
294 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
295 msg) == -1) {
296 gerror = got_error_from_errno("asprintf");
297 return(0);
299 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
300 free(msg);
301 free(err);
302 return(0);
304 int
305 kw_cmp(const void *k, const void *e)
307 return (strcmp(k, ((const struct keywords *)e)->k_name));
310 int
311 lookup(char *s)
313 /* This has to be sorted always. */
314 static const struct keywords keywords[] = {
315 {"author", AUTHOR},
316 {"branch", BRANCH},
317 {"fetch-all-branches", FETCH_ALL_BRANCHES},
318 {"mirror-references", MIRROR_REFERENCES},
319 {"port", PORT},
320 {"protocol", PROTOCOL},
321 {"reference", REFERENCE},
322 {"remote", REMOTE},
323 {"repository", REPOSITORY},
324 {"server", SERVER},
325 };
326 const struct keywords *p;
328 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
329 sizeof(keywords[0]), kw_cmp);
331 if (p)
332 return (p->k_val);
333 else
334 return (STRING);
337 #define START_EXPAND 1
338 #define DONE_EXPAND 2
340 static int expanding;
342 int
343 igetc(void)
345 int c;
347 while (1) {
348 if (file->ungetpos > 0)
349 c = file->ungetbuf[--file->ungetpos];
350 else
351 c = getc(file->stream);
353 if (c == START_EXPAND)
354 expanding = 1;
355 else if (c == DONE_EXPAND)
356 expanding = 0;
357 else
358 break;
360 return (c);
363 int
364 lgetc(int quotec)
366 int c, next;
368 if (quotec) {
369 c = igetc();
370 if (c == EOF) {
371 yyerror("reached end of file while parsing "
372 "quoted string");
374 return (c);
377 c = igetc();
378 while (c == '\\') {
379 next = igetc();
380 if (next != '\n') {
381 c = next;
382 break;
384 yylval.lineno = file->lineno;
385 file->lineno++;
388 return (c);
391 void
392 lungetc(int c)
394 if (c == EOF)
395 return;
397 if (file->ungetpos >= file->ungetsize) {
398 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
399 if (p == NULL)
400 err(1, "%s", __func__);
401 file->ungetbuf = p;
402 file->ungetsize *= 2;
404 file->ungetbuf[file->ungetpos++] = c;
407 int
408 findeol(void)
410 int c;
412 /* Skip to either EOF or the first real EOL. */
413 while (1) {
414 c = lgetc(0);
415 if (c == '\n') {
416 file->lineno++;
417 break;
419 if (c == EOF)
420 break;
422 return (ERROR);
425 static long long
426 getservice(char *n)
428 struct servent *s;
429 u_long ulval;
431 if (atoul(n, &ulval) == 0) {
432 if (ulval > 65535) {
433 yyerror("illegal port value %lu", ulval);
434 return (-1);
436 return ulval;
437 } else {
438 s = getservbyname(n, "tcp");
439 if (s == NULL)
440 s = getservbyname(n, "udp");
441 if (s == NULL) {
442 yyerror("unknown port %s", n);
443 return (-1);
445 return (s->s_port);
449 static int
450 parseport(char *port, long long *pn)
452 if ((*pn = getservice(port)) == -1) {
453 *pn = 0LL;
454 return (-1);
456 return (0);
460 int
461 yylex(void)
463 unsigned char buf[8096];
464 unsigned char *p, *val;
465 int quotec, next, c;
466 int token;
468 top:
469 p = buf;
470 c = lgetc(0);
471 while (c == ' ' || c == '\t')
472 c = lgetc(0); /* nothing */
474 yylval.lineno = file->lineno;
475 if (c == '#') {
476 c = lgetc(0);
477 while (c != '\n' && c != EOF)
478 c = lgetc(0); /* nothing */
480 if (c == '$' && !expanding) {
481 while (1) {
482 c = lgetc(0);
483 if (c == EOF)
484 return (0);
486 if (p + 1 >= buf + sizeof(buf) - 1) {
487 yyerror("string too long");
488 return (findeol());
490 if (isalnum(c) || c == '_') {
491 *p++ = c;
492 continue;
494 *p = '\0';
495 lungetc(c);
496 break;
498 val = symget(buf);
499 if (val == NULL) {
500 yyerror("macro '%s' not defined", buf);
501 return (findeol());
503 p = val + strlen(val) - 1;
504 lungetc(DONE_EXPAND);
505 while (p >= val) {
506 lungetc(*p);
507 p--;
509 lungetc(START_EXPAND);
510 goto top;
513 switch (c) {
514 case '\'':
515 case '"':
516 quotec = c;
517 while (1) {
518 c = lgetc(quotec);
519 if (c == EOF)
520 return (0);
521 if (c == '\n') {
522 file->lineno++;
523 continue;
524 } else if (c == '\\') {
525 next = lgetc(quotec);
526 if (next == EOF)
527 return (0);
528 if (next == quotec || c == ' ' || c == '\t')
529 c = next;
530 else if (next == '\n') {
531 file->lineno++;
532 continue;
533 } else
534 lungetc(next);
535 } else if (c == quotec) {
536 *p = '\0';
537 break;
538 } else if (c == '\0') {
539 yyerror("syntax error");
540 return (findeol());
542 if (p + 1 >= buf + sizeof(buf) - 1) {
543 yyerror("string too long");
544 return (findeol());
546 *p++ = c;
548 yylval.v.string = strdup(buf);
549 if (yylval.v.string == NULL)
550 err(1, "%s", __func__);
551 return (STRING);
554 #define allowed_to_end_number(x) \
555 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
557 if (c == '-' || isdigit(c)) {
558 do {
559 *p++ = c;
560 if ((unsigned)(p-buf) >= sizeof(buf)) {
561 yyerror("string too long");
562 return (findeol());
564 c = lgetc(0);
565 } while (c != EOF && isdigit(c));
566 lungetc(c);
567 if (p == buf + 1 && buf[0] == '-')
568 goto nodigits;
569 if (c == EOF || allowed_to_end_number(c)) {
570 const char *errstr = NULL;
572 *p = '\0';
573 yylval.v.number = strtonum(buf, LLONG_MIN,
574 LLONG_MAX, &errstr);
575 if (errstr) {
576 yyerror("\"%s\" invalid number: %s",
577 buf, errstr);
578 return (findeol());
580 return (NUMBER);
581 } else {
582 nodigits:
583 while (p > buf + 1)
584 lungetc(*--p);
585 c = *--p;
586 if (c == '-')
587 return (c);
591 #define allowed_in_string(x) \
592 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
593 x != '{' && x != '}' && \
594 x != '!' && x != '=' && x != '#' && \
595 x != ','))
597 if (isalnum(c) || c == ':' || c == '_') {
598 do {
599 *p++ = c;
600 if ((unsigned)(p-buf) >= sizeof(buf)) {
601 yyerror("string too long");
602 return (findeol());
604 c = lgetc(0);
605 } while (c != EOF && (allowed_in_string(c)));
606 lungetc(c);
607 *p = '\0';
608 token = lookup(buf);
609 if (token == STRING) {
610 yylval.v.string = strdup(buf);
611 if (yylval.v.string == NULL)
612 err(1, "%s", __func__);
614 return (token);
616 if (c == '\n') {
617 yylval.lineno = file->lineno;
618 file->lineno++;
620 if (c == EOF)
621 return (0);
622 return (c);
625 static const struct got_error*
626 newfile(struct file **nfile, const char *filename, int *fd)
628 const struct got_error* error = NULL;
630 (*nfile) = calloc(1, sizeof(struct file));
631 if ((*nfile) == NULL)
632 return got_error_from_errno("calloc");
633 (*nfile)->stream = fdopen(*fd, "r");
634 if ((*nfile)->stream == NULL) {
635 error = got_error_from_errno("fdopen");
636 free((*nfile));
637 return error;
639 *fd = -1; /* Stream owns the file descriptor now. */
640 (*nfile)->name = filename;
641 (*nfile)->lineno = 1;
642 (*nfile)->ungetsize = 16;
643 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
644 if ((*nfile)->ungetbuf == NULL) {
645 error = got_error_from_errno("malloc");
646 fclose((*nfile)->stream);
647 free((*nfile));
648 return error;
650 return NULL;
653 static const struct got_error*
654 new_remote(struct gotconfig_remote_repo **remote)
656 const struct got_error *error = NULL;
658 *remote = calloc(1, sizeof(**remote));
659 if (*remote == NULL)
660 error = got_error_from_errno("calloc");
661 return error;
664 static void
665 closefile(struct file *file)
667 fclose(file->stream);
668 free(file->ungetbuf);
669 free(file);
672 const struct got_error *
673 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
675 const struct got_error *err = NULL;
676 struct sym *sym, *next;
678 *conf = NULL;
680 err = newfile(&file, filename, fd);
681 if (err)
682 return err;
684 TAILQ_INIT(&gotconfig.remotes);
686 yyparse();
687 closefile(file);
689 /* Free macros and check which have not been used. */
690 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
691 if (!sym->persist) {
692 free(sym->nam);
693 free(sym->val);
694 TAILQ_REMOVE(&symhead, sym, entry);
695 free(sym);
699 if (gerror == NULL)
700 *conf = &gotconfig;
701 return gerror;
704 void
705 gotconfig_free(struct gotconfig *conf)
707 struct gotconfig_remote_repo *remote;
709 free(conf->author);
710 while (!TAILQ_EMPTY(&conf->remotes)) {
711 remote = TAILQ_FIRST(&conf->remotes);
712 TAILQ_REMOVE(&conf->remotes, remote, entry);
713 free(remote->name);
714 free(remote->repository);
715 free(remote->server);
716 free(remote->protocol);
717 free(remote);
721 int
722 symset(const char *nam, const char *val, int persist)
724 struct sym *sym;
726 TAILQ_FOREACH(sym, &symhead, entry) {
727 if (strcmp(nam, sym->nam) == 0)
728 break;
731 if (sym != NULL) {
732 if (sym->persist == 1)
733 return (0);
734 else {
735 free(sym->nam);
736 free(sym->val);
737 TAILQ_REMOVE(&symhead, sym, entry);
738 free(sym);
741 sym = calloc(1, sizeof(*sym));
742 if (sym == NULL)
743 return (-1);
745 sym->nam = strdup(nam);
746 if (sym->nam == NULL) {
747 free(sym);
748 return (-1);
750 sym->val = strdup(val);
751 if (sym->val == NULL) {
752 free(sym->nam);
753 free(sym);
754 return (-1);
756 sym->used = 0;
757 sym->persist = persist;
758 TAILQ_INSERT_TAIL(&symhead, sym, entry);
759 return (0);
762 int
763 cmdline_symset(char *s)
765 char *sym, *val;
766 int ret;
767 size_t len;
769 val = strrchr(s, '=');
770 if (val == NULL)
771 return (-1);
773 len = strlen(s) - strlen(val) + 1;
774 sym = malloc(len);
775 if (sym == NULL)
776 errx(1, "cmdline_symset: malloc");
778 strlcpy(sym, s, len);
780 ret = symset(sym, val + 1, 1);
781 free(sym);
783 return (ret);
786 char *
787 symget(const char *nam)
789 struct sym *sym;
791 TAILQ_FOREACH(sym, &symhead, entry) {
792 if (strcmp(nam, sym->nam) == 0) {
793 sym->used = 1;
794 return (sym->val);
797 return (NULL);
800 static int
801 atoul(char *s, u_long *ulvalp)
803 u_long ulval;
804 char *ep;
806 errno = 0;
807 ulval = strtoul(s, &ep, 0);
808 if (s[0] == '\0' || *ep != '\0')
809 return (-1);
810 if (errno == ERANGE && ulval == ULONG_MAX)
811 return (-1);
812 *ulvalp = ulval;
813 return (0);