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 } v;
91 int lineno;
92 } YYSTYPE;
94 %}
96 %token ERROR
97 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
98 %token AUTHOR
99 %token <v.string> STRING
100 %token <v.number> NUMBER
101 %type <v.number> boolean portplain
102 %type <v.string> numberstring
103 %type <v.branch> branch xbranch branch_list
105 %%
107 grammar : /* empty */
108 | grammar '\n'
109 | grammar author '\n'
110 | grammar remote '\n'
112 boolean : STRING {
113 if (strcasecmp($1, "true") == 0 ||
114 strcasecmp($1, "yes") == 0)
115 $$ = 1;
116 else if (strcasecmp($1, "false") == 0 ||
117 strcasecmp($1, "no") == 0)
118 $$ = 0;
119 else {
120 yyerror("invalid boolean value '%s'", $1);
121 free($1);
122 YYERROR;
124 free($1);
127 numberstring : NUMBER {
128 char *s;
129 if (asprintf(&s, "%lld", $1) == -1) {
130 yyerror("string: asprintf");
131 YYERROR;
133 $$ = s;
135 | STRING
137 portplain : numberstring {
138 if (parseport($1, &$$) == -1) {
139 free($1);
140 YYERROR;
142 free($1);
145 branch : /* empty */ { $$ = NULL; }
146 | xbranch { $$ = $1; }
147 | '{' optnl branch_list '}' { $$ = $3; }
149 xbranch : STRING {
150 $$ = calloc(1, sizeof(struct node_branch));
151 if ($$ == NULL) {
152 yyerror("calloc");
153 YYERROR;
155 $$->branch_name = $1;
156 $$->tail = $$;
159 branch_list : xbranch optnl { $$ = $1; }
160 | branch_list comma xbranch optnl {
161 $1->tail->next = $3;
162 $1->tail = $3;
163 $$ = $1;
167 remoteopts2 : remoteopts2 remoteopts1 nl
168 | remoteopts1 optnl
170 remoteopts1 : REPOSITORY STRING {
171 remote->repository = strdup($2);
172 if (remote->repository == NULL) {
173 free($2);
174 yyerror("strdup");
175 YYERROR;
177 free($2);
179 | SERVER STRING {
180 remote->server = strdup($2);
181 if (remote->server == NULL) {
182 free($2);
183 yyerror("strdup");
184 YYERROR;
186 free($2);
188 | PROTOCOL STRING {
189 remote->protocol = strdup($2);
190 if (remote->protocol == NULL) {
191 free($2);
192 yyerror("strdup");
193 YYERROR;
195 free($2);
197 | MIRROR_REFERENCES boolean {
198 remote->mirror_references = $2;
200 | PORT portplain {
201 remote->port = $2;
203 | BRANCH branch {
204 remote->branch = $2;
207 remote : REMOTE STRING {
208 static const struct got_error* error;
210 error = new_remote(&remote);
211 if (error) {
212 free($2);
213 yyerror("%s", error->msg);
214 YYERROR;
216 remote->name = strdup($2);
217 if (remote->name == NULL) {
218 free($2);
219 yyerror("strdup");
220 YYERROR;
222 free($2);
223 } '{' optnl remoteopts2 '}' {
224 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
225 gotconfig.nremotes++;
228 author : AUTHOR STRING {
229 gotconfig.author = strdup($2);
230 if (gotconfig.author == NULL) {
231 free($2);
232 yyerror("strdup");
233 YYERROR;
235 free($2);
238 optnl : '\n' optnl
239 | /* empty */
241 nl : '\n' optnl
243 comma : ','
244 | /* empty */
246 %%
248 struct keywords {
249 const char *k_name;
250 int k_val;
251 };
253 int
254 yyerror(const char *fmt, ...)
256 va_list ap;
257 char *msg;
258 char *err = NULL;
260 va_start(ap, fmt);
261 if (vasprintf(&msg, fmt, ap) == -1) {
262 gerror = got_error_from_errno("vasprintf");
263 return 0;
265 va_end(ap);
266 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
267 msg) == -1) {
268 gerror = got_error_from_errno("asprintf");
269 return(0);
271 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
272 free(msg);
273 free(err);
274 return(0);
276 int
277 kw_cmp(const void *k, const void *e)
279 return (strcmp(k, ((const struct keywords *)e)->k_name));
282 int
283 lookup(char *s)
285 /* This has to be sorted always. */
286 static const struct keywords keywords[] = {
287 {"author", AUTHOR},
288 {"branch", BRANCH},
289 {"mirror-references", MIRROR_REFERENCES},
290 {"port", PORT},
291 {"protocol", PROTOCOL},
292 {"remote", REMOTE},
293 {"repository", REPOSITORY},
294 {"server", SERVER},
295 };
296 const struct keywords *p;
298 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
299 sizeof(keywords[0]), kw_cmp);
301 if (p)
302 return (p->k_val);
303 else
304 return (STRING);
307 #define START_EXPAND 1
308 #define DONE_EXPAND 2
310 static int expanding;
312 int
313 igetc(void)
315 int c;
317 while (1) {
318 if (file->ungetpos > 0)
319 c = file->ungetbuf[--file->ungetpos];
320 else
321 c = getc(file->stream);
323 if (c == START_EXPAND)
324 expanding = 1;
325 else if (c == DONE_EXPAND)
326 expanding = 0;
327 else
328 break;
330 return (c);
333 int
334 lgetc(int quotec)
336 int c, next;
338 if (quotec) {
339 c = igetc();
340 if (c == EOF) {
341 yyerror("reached end of file while parsing "
342 "quoted string");
344 return (c);
347 c = igetc();
348 while (c == '\\') {
349 next = igetc();
350 if (next != '\n') {
351 c = next;
352 break;
354 yylval.lineno = file->lineno;
355 file->lineno++;
358 return (c);
361 void
362 lungetc(int c)
364 if (c == EOF)
365 return;
367 if (file->ungetpos >= file->ungetsize) {
368 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
369 if (p == NULL)
370 err(1, "%s", __func__);
371 file->ungetbuf = p;
372 file->ungetsize *= 2;
374 file->ungetbuf[file->ungetpos++] = c;
377 int
378 findeol(void)
380 int c;
382 /* Skip to either EOF or the first real EOL. */
383 while (1) {
384 c = lgetc(0);
385 if (c == '\n') {
386 file->lineno++;
387 break;
389 if (c == EOF)
390 break;
392 return (ERROR);
395 static long long
396 getservice(char *n)
398 struct servent *s;
399 u_long ulval;
401 if (atoul(n, &ulval) == 0) {
402 if (ulval > 65535) {
403 yyerror("illegal port value %lu", ulval);
404 return (-1);
406 return ulval;
407 } else {
408 s = getservbyname(n, "tcp");
409 if (s == NULL)
410 s = getservbyname(n, "udp");
411 if (s == NULL) {
412 yyerror("unknown port %s", n);
413 return (-1);
415 return (s->s_port);
419 static int
420 parseport(char *port, long long *pn)
422 if ((*pn = getservice(port)) == -1) {
423 *pn = 0LL;
424 return (-1);
426 return (0);
430 int
431 yylex(void)
433 unsigned char buf[8096];
434 unsigned char *p, *val;
435 int quotec, next, c;
436 int token;
438 top:
439 p = buf;
440 c = lgetc(0);
441 while (c == ' ' || c == '\t')
442 c = lgetc(0); /* nothing */
444 yylval.lineno = file->lineno;
445 if (c == '#') {
446 c = lgetc(0);
447 while (c != '\n' && c != EOF)
448 c = lgetc(0); /* nothing */
450 if (c == '$' && !expanding) {
451 while (1) {
452 c = lgetc(0);
453 if (c == EOF)
454 return (0);
456 if (p + 1 >= buf + sizeof(buf) - 1) {
457 yyerror("string too long");
458 return (findeol());
460 if (isalnum(c) || c == '_') {
461 *p++ = c;
462 continue;
464 *p = '\0';
465 lungetc(c);
466 break;
468 val = symget(buf);
469 if (val == NULL) {
470 yyerror("macro '%s' not defined", buf);
471 return (findeol());
473 p = val + strlen(val) - 1;
474 lungetc(DONE_EXPAND);
475 while (p >= val) {
476 lungetc(*p);
477 p--;
479 lungetc(START_EXPAND);
480 goto top;
483 switch (c) {
484 case '\'':
485 case '"':
486 quotec = c;
487 while (1) {
488 c = lgetc(quotec);
489 if (c == EOF)
490 return (0);
491 if (c == '\n') {
492 file->lineno++;
493 continue;
494 } else if (c == '\\') {
495 next = lgetc(quotec);
496 if (next == EOF)
497 return (0);
498 if (next == quotec || c == ' ' || c == '\t')
499 c = next;
500 else if (next == '\n') {
501 file->lineno++;
502 continue;
503 } else
504 lungetc(next);
505 } else if (c == quotec) {
506 *p = '\0';
507 break;
508 } else if (c == '\0') {
509 yyerror("syntax error");
510 return (findeol());
512 if (p + 1 >= buf + sizeof(buf) - 1) {
513 yyerror("string too long");
514 return (findeol());
516 *p++ = c;
518 yylval.v.string = strdup(buf);
519 if (yylval.v.string == NULL)
520 err(1, "%s", __func__);
521 return (STRING);
524 #define allowed_to_end_number(x) \
525 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
527 if (c == '-' || isdigit(c)) {
528 do {
529 *p++ = c;
530 if ((unsigned)(p-buf) >= sizeof(buf)) {
531 yyerror("string too long");
532 return (findeol());
534 c = lgetc(0);
535 } while (c != EOF && isdigit(c));
536 lungetc(c);
537 if (p == buf + 1 && buf[0] == '-')
538 goto nodigits;
539 if (c == EOF || allowed_to_end_number(c)) {
540 const char *errstr = NULL;
542 *p = '\0';
543 yylval.v.number = strtonum(buf, LLONG_MIN,
544 LLONG_MAX, &errstr);
545 if (errstr) {
546 yyerror("\"%s\" invalid number: %s",
547 buf, errstr);
548 return (findeol());
550 return (NUMBER);
551 } else {
552 nodigits:
553 while (p > buf + 1)
554 lungetc(*--p);
555 c = *--p;
556 if (c == '-')
557 return (c);
561 #define allowed_in_string(x) \
562 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
563 x != '{' && x != '}' && \
564 x != '!' && x != '=' && x != '#' && \
565 x != ','))
567 if (isalnum(c) || c == ':' || c == '_') {
568 do {
569 *p++ = c;
570 if ((unsigned)(p-buf) >= sizeof(buf)) {
571 yyerror("string too long");
572 return (findeol());
574 c = lgetc(0);
575 } while (c != EOF && (allowed_in_string(c)));
576 lungetc(c);
577 *p = '\0';
578 token = lookup(buf);
579 if (token == STRING) {
580 yylval.v.string = strdup(buf);
581 if (yylval.v.string == NULL)
582 err(1, "%s", __func__);
584 return (token);
586 if (c == '\n') {
587 yylval.lineno = file->lineno;
588 file->lineno++;
590 if (c == EOF)
591 return (0);
592 return (c);
595 static const struct got_error*
596 newfile(struct file **nfile, const char *filename, int *fd)
598 const struct got_error* error = NULL;
600 (*nfile) = calloc(1, sizeof(struct file));
601 if ((*nfile) == NULL)
602 return got_error_from_errno("calloc");
603 (*nfile)->stream = fdopen(*fd, "r");
604 if ((*nfile)->stream == NULL) {
605 error = got_error_from_errno("fdopen");
606 free((*nfile));
607 return error;
609 *fd = -1; /* Stream owns the file descriptor now. */
610 (*nfile)->name = filename;
611 (*nfile)->lineno = 1;
612 (*nfile)->ungetsize = 16;
613 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
614 if ((*nfile)->ungetbuf == NULL) {
615 error = got_error_from_errno("malloc");
616 fclose((*nfile)->stream);
617 free((*nfile));
618 return error;
620 return NULL;
623 static const struct got_error*
624 new_remote(struct gotconfig_remote_repo **remote)
626 const struct got_error *error = NULL;
628 *remote = calloc(1, sizeof(**remote));
629 if (*remote == NULL)
630 error = got_error_from_errno("calloc");
631 return error;
634 static void
635 closefile(struct file *file)
637 fclose(file->stream);
638 free(file->ungetbuf);
639 free(file);
642 const struct got_error *
643 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
645 const struct got_error *err = NULL;
646 struct sym *sym, *next;
648 *conf = NULL;
650 err = newfile(&file, filename, fd);
651 if (err)
652 return err;
654 TAILQ_INIT(&gotconfig.remotes);
656 yyparse();
657 closefile(file);
659 /* Free macros and check which have not been used. */
660 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
661 if (!sym->persist) {
662 free(sym->nam);
663 free(sym->val);
664 TAILQ_REMOVE(&symhead, sym, entry);
665 free(sym);
669 if (gerror == NULL)
670 *conf = &gotconfig;
671 return gerror;
674 void
675 gotconfig_free(struct gotconfig *conf)
677 struct gotconfig_remote_repo *remote;
679 free(conf->author);
680 while (!TAILQ_EMPTY(&conf->remotes)) {
681 remote = TAILQ_FIRST(&conf->remotes);
682 TAILQ_REMOVE(&conf->remotes, remote, entry);
683 free(remote->name);
684 free(remote->repository);
685 free(remote->server);
686 free(remote->protocol);
687 free(remote);
691 int
692 symset(const char *nam, const char *val, int persist)
694 struct sym *sym;
696 TAILQ_FOREACH(sym, &symhead, entry) {
697 if (strcmp(nam, sym->nam) == 0)
698 break;
701 if (sym != NULL) {
702 if (sym->persist == 1)
703 return (0);
704 else {
705 free(sym->nam);
706 free(sym->val);
707 TAILQ_REMOVE(&symhead, sym, entry);
708 free(sym);
711 sym = calloc(1, sizeof(*sym));
712 if (sym == NULL)
713 return (-1);
715 sym->nam = strdup(nam);
716 if (sym->nam == NULL) {
717 free(sym);
718 return (-1);
720 sym->val = strdup(val);
721 if (sym->val == NULL) {
722 free(sym->nam);
723 free(sym);
724 return (-1);
726 sym->used = 0;
727 sym->persist = persist;
728 TAILQ_INSERT_TAIL(&symhead, sym, entry);
729 return (0);
732 int
733 cmdline_symset(char *s)
735 char *sym, *val;
736 int ret;
737 size_t len;
739 val = strrchr(s, '=');
740 if (val == NULL)
741 return (-1);
743 len = strlen(s) - strlen(val) + 1;
744 sym = malloc(len);
745 if (sym == NULL)
746 errx(1, "cmdline_symset: malloc");
748 strlcpy(sym, s, len);
750 ret = symset(sym, val + 1, 1);
751 free(sym);
753 return (ret);
756 char *
757 symget(const char *nam)
759 struct sym *sym;
761 TAILQ_FOREACH(sym, &symhead, entry) {
762 if (strcmp(nam, sym->nam) == 0) {
763 sym->used = 1;
764 return (sym->val);
767 return (NULL);
770 static int
771 atoul(char *s, u_long *ulvalp)
773 u_long ulval;
774 char *ep;
776 errno = 0;
777 ulval = strtoul(s, &ep, 0);
778 if (s[0] == '\0' || *ep != '\0')
779 return (-1);
780 if (errno == ERANGE && ulval == ULONG_MAX)
781 return (-1);
782 *ulvalp = ulval;
783 return (0);