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 int64_t number;
88 char *string;
89 } v;
90 int lineno;
91 } YYSTYPE;
93 %}
95 %token ERROR
96 %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES AUTHOR
97 %token <v.string> STRING
98 %token <v.number> NUMBER
99 %type <v.number> boolean portplain
100 %type <v.string> numberstring
102 %%
104 grammar : /* empty */
105 | grammar '\n'
106 | grammar author '\n'
107 | grammar remote '\n'
109 boolean : STRING {
110 if (strcasecmp($1, "true") == 0 ||
111 strcasecmp($1, "yes") == 0)
112 $$ = 1;
113 else if (strcasecmp($1, "false") == 0 ||
114 strcasecmp($1, "no") == 0)
115 $$ = 0;
116 else {
117 yyerror("invalid boolean value '%s'", $1);
118 free($1);
119 YYERROR;
121 free($1);
124 numberstring : NUMBER {
125 char *s;
126 if (asprintf(&s, "%lld", $1) == -1) {
127 yyerror("string: asprintf");
128 YYERROR;
130 $$ = s;
132 | STRING
134 portplain : numberstring {
135 if (parseport($1, &$$) == -1) {
136 free($1);
137 YYERROR;
139 free($1);
142 remoteopts2 : remoteopts2 remoteopts1 nl
143 | remoteopts1 optnl
145 remoteopts1 : REPOSITORY STRING {
146 remote->repository = strdup($2);
147 if (remote->repository == NULL) {
148 free($2);
149 yyerror("strdup");
150 YYERROR;
152 free($2);
154 | SERVER STRING {
155 remote->server = strdup($2);
156 if (remote->server == NULL) {
157 free($2);
158 yyerror("strdup");
159 YYERROR;
161 free($2);
163 | PROTOCOL STRING {
164 remote->protocol = strdup($2);
165 if (remote->protocol == NULL) {
166 free($2);
167 yyerror("strdup");
168 YYERROR;
170 free($2);
172 | MIRROR_REFERENCES boolean {
173 remote->mirror_references = $2;
175 | PORT portplain {
176 remote->port = $2;
179 remote : REMOTE STRING {
180 static const struct got_error* error;
182 error = new_remote(&remote);
183 if (error) {
184 free($2);
185 yyerror("%s", error->msg);
186 YYERROR;
188 remote->name = strdup($2);
189 if (remote->name == NULL) {
190 free($2);
191 yyerror("strdup");
192 YYERROR;
194 free($2);
195 } '{' optnl remoteopts2 '}' {
196 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
197 gotconfig.nremotes++;
200 author : AUTHOR STRING {
201 gotconfig.author = strdup($2);
202 if (gotconfig.author == NULL) {
203 free($2);
204 yyerror("strdup");
205 YYERROR;
207 free($2);
210 optnl : '\n' optnl
211 | /* empty */
213 nl : '\n' optnl
215 %%
217 struct keywords {
218 const char *k_name;
219 int k_val;
220 };
222 int
223 yyerror(const char *fmt, ...)
225 va_list ap;
226 char *msg;
227 char *err = NULL;
229 va_start(ap, fmt);
230 if (vasprintf(&msg, fmt, ap) == -1) {
231 gerror = got_error_from_errno("vasprintf");
232 return 0;
234 va_end(ap);
235 if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
236 msg) == -1) {
237 gerror = got_error_from_errno("asprintf");
238 return(0);
240 gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
241 free(msg);
242 free(err);
243 return(0);
245 int
246 kw_cmp(const void *k, const void *e)
248 return (strcmp(k, ((const struct keywords *)e)->k_name));
251 int
252 lookup(char *s)
254 /* This has to be sorted always. */
255 static const struct keywords keywords[] = {
256 {"author", AUTHOR},
257 {"mirror-references", MIRROR_REFERENCES},
258 {"port", PORT},
259 {"protocol", PROTOCOL},
260 {"remote", REMOTE},
261 {"repository", REPOSITORY},
262 {"server", SERVER},
263 };
264 const struct keywords *p;
266 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
267 sizeof(keywords[0]), kw_cmp);
269 if (p)
270 return (p->k_val);
271 else
272 return (STRING);
275 #define START_EXPAND 1
276 #define DONE_EXPAND 2
278 static int expanding;
280 int
281 igetc(void)
283 int c;
285 while (1) {
286 if (file->ungetpos > 0)
287 c = file->ungetbuf[--file->ungetpos];
288 else
289 c = getc(file->stream);
291 if (c == START_EXPAND)
292 expanding = 1;
293 else if (c == DONE_EXPAND)
294 expanding = 0;
295 else
296 break;
298 return (c);
301 int
302 lgetc(int quotec)
304 int c, next;
306 if (quotec) {
307 c = igetc();
308 if (c == EOF) {
309 yyerror("reached end of file while parsing "
310 "quoted string");
312 return (c);
315 c = igetc();
316 while (c == '\\') {
317 next = igetc();
318 if (next != '\n') {
319 c = next;
320 break;
322 yylval.lineno = file->lineno;
323 file->lineno++;
326 return (c);
329 void
330 lungetc(int c)
332 if (c == EOF)
333 return;
335 if (file->ungetpos >= file->ungetsize) {
336 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
337 if (p == NULL)
338 err(1, "%s", __func__);
339 file->ungetbuf = p;
340 file->ungetsize *= 2;
342 file->ungetbuf[file->ungetpos++] = c;
345 int
346 findeol(void)
348 int c;
350 /* Skip to either EOF or the first real EOL. */
351 while (1) {
352 c = lgetc(0);
353 if (c == '\n') {
354 file->lineno++;
355 break;
357 if (c == EOF)
358 break;
360 return (ERROR);
363 static long long
364 getservice(char *n)
366 struct servent *s;
367 u_long ulval;
369 if (atoul(n, &ulval) == 0) {
370 if (ulval > 65535) {
371 yyerror("illegal port value %lu", ulval);
372 return (-1);
374 return ulval;
375 } else {
376 s = getservbyname(n, "tcp");
377 if (s == NULL)
378 s = getservbyname(n, "udp");
379 if (s == NULL) {
380 yyerror("unknown port %s", n);
381 return (-1);
383 return (s->s_port);
387 static int
388 parseport(char *port, long long *pn)
390 if ((*pn = getservice(port)) == -1) {
391 *pn = 0LL;
392 return (-1);
394 return (0);
398 int
399 yylex(void)
401 unsigned char buf[8096];
402 unsigned char *p, *val;
403 int quotec, next, c;
404 int token;
406 top:
407 p = buf;
408 c = lgetc(0);
409 while (c == ' ' || c == '\t')
410 c = lgetc(0); /* nothing */
412 yylval.lineno = file->lineno;
413 if (c == '#') {
414 c = lgetc(0);
415 while (c != '\n' && c != EOF)
416 c = lgetc(0); /* nothing */
418 if (c == '$' && !expanding) {
419 while (1) {
420 c = lgetc(0);
421 if (c == EOF)
422 return (0);
424 if (p + 1 >= buf + sizeof(buf) - 1) {
425 yyerror("string too long");
426 return (findeol());
428 if (isalnum(c) || c == '_') {
429 *p++ = c;
430 continue;
432 *p = '\0';
433 lungetc(c);
434 break;
436 val = symget(buf);
437 if (val == NULL) {
438 yyerror("macro '%s' not defined", buf);
439 return (findeol());
441 p = val + strlen(val) - 1;
442 lungetc(DONE_EXPAND);
443 while (p >= val) {
444 lungetc(*p);
445 p--;
447 lungetc(START_EXPAND);
448 goto top;
451 switch (c) {
452 case '\'':
453 case '"':
454 quotec = c;
455 while (1) {
456 c = lgetc(quotec);
457 if (c == EOF)
458 return (0);
459 if (c == '\n') {
460 file->lineno++;
461 continue;
462 } else if (c == '\\') {
463 next = lgetc(quotec);
464 if (next == EOF)
465 return (0);
466 if (next == quotec || c == ' ' || c == '\t')
467 c = next;
468 else if (next == '\n') {
469 file->lineno++;
470 continue;
471 } else
472 lungetc(next);
473 } else if (c == quotec) {
474 *p = '\0';
475 break;
476 } else if (c == '\0') {
477 yyerror("syntax error");
478 return (findeol());
480 if (p + 1 >= buf + sizeof(buf) - 1) {
481 yyerror("string too long");
482 return (findeol());
484 *p++ = c;
486 yylval.v.string = strdup(buf);
487 if (yylval.v.string == NULL)
488 err(1, "%s", __func__);
489 return (STRING);
492 #define allowed_to_end_number(x) \
493 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
495 if (c == '-' || isdigit(c)) {
496 do {
497 *p++ = c;
498 if ((unsigned)(p-buf) >= sizeof(buf)) {
499 yyerror("string too long");
500 return (findeol());
502 c = lgetc(0);
503 } while (c != EOF && isdigit(c));
504 lungetc(c);
505 if (p == buf + 1 && buf[0] == '-')
506 goto nodigits;
507 if (c == EOF || allowed_to_end_number(c)) {
508 const char *errstr = NULL;
510 *p = '\0';
511 yylval.v.number = strtonum(buf, LLONG_MIN,
512 LLONG_MAX, &errstr);
513 if (errstr) {
514 yyerror("\"%s\" invalid number: %s",
515 buf, errstr);
516 return (findeol());
518 return (NUMBER);
519 } else {
520 nodigits:
521 while (p > buf + 1)
522 lungetc(*--p);
523 c = *--p;
524 if (c == '-')
525 return (c);
529 #define allowed_in_string(x) \
530 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
531 x != '{' && x != '}' && \
532 x != '!' && x != '=' && x != '#' && \
533 x != ','))
535 if (isalnum(c) || c == ':' || c == '_') {
536 do {
537 *p++ = c;
538 if ((unsigned)(p-buf) >= sizeof(buf)) {
539 yyerror("string too long");
540 return (findeol());
542 c = lgetc(0);
543 } while (c != EOF && (allowed_in_string(c)));
544 lungetc(c);
545 *p = '\0';
546 token = lookup(buf);
547 if (token == STRING) {
548 yylval.v.string = strdup(buf);
549 if (yylval.v.string == NULL)
550 err(1, "%s", __func__);
552 return (token);
554 if (c == '\n') {
555 yylval.lineno = file->lineno;
556 file->lineno++;
558 if (c == EOF)
559 return (0);
560 return (c);
563 static const struct got_error*
564 newfile(struct file **nfile, const char *filename, int *fd)
566 const struct got_error* error = NULL;
568 (*nfile) = calloc(1, sizeof(struct file));
569 if ((*nfile) == NULL)
570 return got_error_from_errno("calloc");
571 (*nfile)->stream = fdopen(*fd, "r");
572 if ((*nfile)->stream == NULL) {
573 error = got_error_from_errno("fdopen");
574 free((*nfile));
575 return error;
577 *fd = -1; /* Stream owns the file descriptor now. */
578 (*nfile)->name = filename;
579 (*nfile)->lineno = 1;
580 (*nfile)->ungetsize = 16;
581 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
582 if ((*nfile)->ungetbuf == NULL) {
583 error = got_error_from_errno("malloc");
584 fclose((*nfile)->stream);
585 free((*nfile));
586 return error;
588 return NULL;
591 static const struct got_error*
592 new_remote(struct gotconfig_remote_repo **remote)
594 const struct got_error *error = NULL;
596 *remote = calloc(1, sizeof(**remote));
597 if (*remote == NULL)
598 error = got_error_from_errno("calloc");
599 return error;
602 static void
603 closefile(struct file *file)
605 fclose(file->stream);
606 free(file->ungetbuf);
607 free(file);
610 const struct got_error *
611 gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
613 const struct got_error *err = NULL;
614 struct sym *sym, *next;
616 *conf = NULL;
618 err = newfile(&file, filename, fd);
619 if (err)
620 return err;
622 TAILQ_INIT(&gotconfig.remotes);
624 yyparse();
625 closefile(file);
627 /* Free macros and check which have not been used. */
628 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
629 if (!sym->persist) {
630 free(sym->nam);
631 free(sym->val);
632 TAILQ_REMOVE(&symhead, sym, entry);
633 free(sym);
637 if (gerror == NULL)
638 *conf = &gotconfig;
639 return gerror;
642 void
643 gotconfig_free(struct gotconfig *conf)
645 struct gotconfig_remote_repo *remote;
647 free(conf->author);
648 while (!TAILQ_EMPTY(&conf->remotes)) {
649 remote = TAILQ_FIRST(&conf->remotes);
650 TAILQ_REMOVE(&conf->remotes, remote, entry);
651 free(remote->name);
652 free(remote->repository);
653 free(remote->server);
654 free(remote->protocol);
655 free(remote);
659 int
660 symset(const char *nam, const char *val, int persist)
662 struct sym *sym;
664 TAILQ_FOREACH(sym, &symhead, entry) {
665 if (strcmp(nam, sym->nam) == 0)
666 break;
669 if (sym != NULL) {
670 if (sym->persist == 1)
671 return (0);
672 else {
673 free(sym->nam);
674 free(sym->val);
675 TAILQ_REMOVE(&symhead, sym, entry);
676 free(sym);
679 sym = calloc(1, sizeof(*sym));
680 if (sym == NULL)
681 return (-1);
683 sym->nam = strdup(nam);
684 if (sym->nam == NULL) {
685 free(sym);
686 return (-1);
688 sym->val = strdup(val);
689 if (sym->val == NULL) {
690 free(sym->nam);
691 free(sym);
692 return (-1);
694 sym->used = 0;
695 sym->persist = persist;
696 TAILQ_INSERT_TAIL(&symhead, sym, entry);
697 return (0);
700 int
701 cmdline_symset(char *s)
703 char *sym, *val;
704 int ret;
705 size_t len;
707 val = strrchr(s, '=');
708 if (val == NULL)
709 return (-1);
711 len = strlen(s) - strlen(val) + 1;
712 sym = malloc(len);
713 if (sym == NULL)
714 errx(1, "cmdline_symset: malloc");
716 strlcpy(sym, s, len);
718 ret = symset(sym, val + 1, 1);
719 free(sym);
721 return (ret);
724 char *
725 symget(const char *nam)
727 struct sym *sym;
729 TAILQ_FOREACH(sym, &symhead, entry) {
730 if (strcmp(nam, sym->nam) == 0) {
731 sym->used = 1;
732 return (sym->val);
735 return (NULL);
738 static int
739 atoul(char *s, u_long *ulvalp)
741 u_long ulval;
742 char *ep;
744 errno = 0;
745 ulval = strtoul(s, &ep, 0);
746 if (s[0] == '\0' || *ep != '\0')
747 return (-1);
748 if (errno == ERANGE && ulval == ULONG_MAX)
749 return (-1);
750 *ulvalp = ulval;
751 return (0);