Blob


1 /*
2 * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
3 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 *
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
23 %{
24 #include <sys/types.h>
25 #include <sys/queue.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
29 #include <netinet/in.h>
31 #include <arpa/inet.h>
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <event.h>
37 #include <ifaddrs.h>
38 #include <imsg.h>
39 #include <limits.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <syslog.h>
44 #include <unistd.h>
46 #include "got_error.h"
47 #include "got.h"
49 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
50 static struct file {
51 TAILQ_ENTRY(file) entry;
52 FILE *stream;
53 char *name;
54 size_t ungetpos;
55 size_t ungetsize;
56 u_char *ungetbuf;
57 int eof_reached;
58 int lineno;
59 } *file, *topfile;
60 static const struct got_error* pushfile(struct file**, const char *);
61 int popfile(void);
62 int yyparse(void);
63 int yylex(void);
64 int yyerror(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
68 int lookup(char *);
69 int igetc(void);
70 int lgetc(int);
71 void lungetc(int);
72 int findeol(void);
74 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
75 struct sym {
76 TAILQ_ENTRY(sym) entry;
77 int used;
78 int persist;
79 char *nam;
80 char *val;
81 };
83 int symset(const char *, const char *, int);
84 char *symget(const char *);
86 const struct got_error* gerror = NULL;
87 struct gotconfig_remote_repo *remote;
88 struct gotconfig gotconfig;
89 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
91 typedef struct {
92 union {
93 int64_t number;
94 char *string;
95 } v;
96 int lineno;
97 } YYSTYPE;
99 %}
101 %token ERROR
102 %token REMOTE REPOSITORY SERVER PROTOCOL USER
103 %token <v.string> STRING
104 %token <v.number> NUMBER
106 %%
108 grammar : /* empty */
109 | grammar '\n'
110 | grammar remote '\n'
112 remoteopts2 : remoteopts2 remoteopts1 nl
113 | remoteopts1 optnl
115 remoteopts1 : REPOSITORY STRING {
116 remote->repository = strdup($2);
117 if (remote->repository == NULL) {
118 free($2);
119 yyerror("strdup");
120 YYERROR;
122 free($2);
124 | SERVER STRING {
125 remote->server = strdup($2);
126 if (remote->server == NULL) {
127 free($2);
128 yyerror("strdup");
129 YYERROR;
131 free($2);
133 | PROTOCOL STRING {
134 remote->protocol = strdup($2);
135 if (remote->protocol == NULL) {
136 free($2);
137 yyerror("strdup");
138 YYERROR;
140 free($2);
142 | USER STRING {
143 remote->user = strdup($2);
144 if (remote->user == NULL) {
145 free($2);
146 yyerror("strdup");
147 YYERROR;
149 free($2);
152 remote : REMOTE STRING {
153 static const struct got_error* error;
155 error = new_remote(&remote);
156 if (error) {
157 free($2);
158 yyerror("%s", error->msg);
159 YYERROR;
161 remote->name = strdup($2);
162 if (remote->name == NULL) {
163 free($2);
164 yyerror("strdup");
165 YYERROR;
167 free($2);
168 } '{' optnl remoteopts2 '}' {
169 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
170 gotconfig.nremotes++;
173 optnl : '\n' optnl
174 | /* empty */
176 nl : '\n' optnl
178 %%
180 struct keywords {
181 const char *k_name;
182 int k_val;
183 };
185 int
186 yyerror(const char *fmt, ...)
188 va_list ap;
189 char *msg;
190 char *err = NULL;
192 va_start(ap, fmt);
193 if (vasprintf(&msg, fmt, ap) == -1) {
194 gerror = got_error_from_errno("vasprintf");
195 return 0;
197 va_end(ap);
198 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
199 gerror = got_error_from_errno("asprintf");
200 return(0);
202 gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
203 free(msg);
204 free(err);
205 return(0);
208 int
209 kw_cmp(const void *k, const void *e)
211 return (strcmp(k, ((const struct keywords *)e)->k_name));
214 int
215 lookup(char *s)
217 /* This has to be sorted always. */
218 static const struct keywords keywords[] = {
219 {"protocol", PROTOCOL},
220 {"remote", REMOTE},
221 {"repository", REPOSITORY},
222 {"server", SERVER},
223 {"user", USER},
224 };
225 const struct keywords *p;
227 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
228 sizeof(keywords[0]), kw_cmp);
230 if (p)
231 return (p->k_val);
232 else
233 return (STRING);
236 #define START_EXPAND 1
237 #define DONE_EXPAND 2
239 static int expanding;
241 int
242 igetc(void)
244 int c;
246 while (1) {
247 if (file->ungetpos > 0)
248 c = file->ungetbuf[--file->ungetpos];
249 else
250 c = getc(file->stream);
252 if (c == START_EXPAND)
253 expanding = 1;
254 else if (c == DONE_EXPAND)
255 expanding = 0;
256 else
257 break;
259 return (c);
262 int
263 lgetc(int quotec)
265 int c, next;
267 if (quotec) {
268 if ((c = igetc()) == EOF) {
269 yyerror("reached end of file while parsing "
270 "quoted string");
271 if (file == topfile || popfile() == EOF)
272 return (EOF);
273 return (quotec);
275 return (c);
278 while ((c = igetc()) == '\\') {
279 next = igetc();
280 if (next != '\n') {
281 c = next;
282 break;
284 yylval.lineno = file->lineno;
285 file->lineno++;
288 if (c == EOF) {
289 /*
290 * Fake EOL when hit EOF for the first time. This gets line
291 * count right if last line in included file is syntactically
292 * invalid and has no newline.
293 */
294 if (file->eof_reached == 0) {
295 file->eof_reached = 1;
296 return ('\n');
298 while (c == EOF) {
299 if (file == topfile || popfile() == EOF)
300 return (EOF);
301 c = igetc();
304 return (c);
307 void
308 lungetc(int c)
310 if (c == EOF)
311 return;
313 if (file->ungetpos >= file->ungetsize) {
314 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
315 if (p == NULL)
316 err(1, "%s", __func__);
317 file->ungetbuf = p;
318 file->ungetsize *= 2;
320 file->ungetbuf[file->ungetpos++] = c;
323 int
324 findeol(void)
326 int c;
328 /* Skip to either EOF or the first real EOL. */
329 while (1) {
330 c = lgetc(0);
331 if (c == '\n') {
332 file->lineno++;
333 break;
335 if (c == EOF)
336 break;
338 return (ERROR);
341 int
342 yylex(void)
344 unsigned char buf[8096];
345 unsigned char *p, *val;
346 int quotec, next, c;
347 int token;
349 top:
350 p = buf;
351 while ((c = lgetc(0)) == ' ' || c == '\t')
352 ; /* nothing */
354 yylval.lineno = file->lineno;
355 if (c == '#')
356 while ((c = lgetc(0)) != '\n' && c != EOF)
357 ; /* nothing */
358 if (c == '$' && !expanding) {
359 while (1) {
360 if ((c = lgetc(0)) == EOF)
361 return (0);
363 if (p + 1 >= buf + sizeof(buf) - 1) {
364 yyerror("string too long");
365 return (findeol());
367 if (isalnum(c) || c == '_') {
368 *p++ = c;
369 continue;
371 *p = '\0';
372 lungetc(c);
373 break;
375 val = symget(buf);
376 if (val == NULL) {
377 yyerror("macro '%s' not defined", buf);
378 return (findeol());
380 p = val + strlen(val) - 1;
381 lungetc(DONE_EXPAND);
382 while (p >= val) {
383 lungetc(*p);
384 p--;
386 lungetc(START_EXPAND);
387 goto top;
390 switch (c) {
391 case '\'':
392 case '"':
393 quotec = c;
394 while (1) {
395 if ((c = lgetc(quotec)) == EOF)
396 return (0);
397 if (c == '\n') {
398 file->lineno++;
399 continue;
400 } else if (c == '\\') {
401 if ((next = lgetc(quotec)) == EOF)
402 return (0);
403 if (next == quotec || c == ' ' || c == '\t')
404 c = next;
405 else if (next == '\n') {
406 file->lineno++;
407 continue;
408 } else
409 lungetc(next);
410 } else if (c == quotec) {
411 *p = '\0';
412 break;
413 } else if (c == '\0') {
414 yyerror("syntax error");
415 return (findeol());
417 if (p + 1 >= buf + sizeof(buf) - 1) {
418 yyerror("string too long");
419 return (findeol());
421 *p++ = c;
423 yylval.v.string = strdup(buf);
424 if (yylval.v.string == NULL)
425 err(1, "%s", __func__);
426 return (STRING);
429 #define allowed_to_end_number(x) \
430 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
432 if (c == '-' || isdigit(c)) {
433 do {
434 *p++ = c;
435 if ((unsigned)(p-buf) >= sizeof(buf)) {
436 yyerror("string too long");
437 return (findeol());
439 } while ((c = lgetc(0)) != EOF && isdigit(c));
440 lungetc(c);
441 if (p == buf + 1 && buf[0] == '-')
442 goto nodigits;
443 if (c == EOF || allowed_to_end_number(c)) {
444 const char *errstr = NULL;
446 *p = '\0';
447 yylval.v.number = strtonum(buf, LLONG_MIN,
448 LLONG_MAX, &errstr);
449 if (errstr) {
450 yyerror("\"%s\" invalid number: %s",
451 buf, errstr);
452 return (findeol());
454 return (NUMBER);
455 } else {
456 nodigits:
457 while (p > buf + 1)
458 lungetc(*--p);
459 c = *--p;
460 if (c == '-')
461 return (c);
465 #define allowed_in_string(x) \
466 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
467 x != '{' && x != '}' && \
468 x != '!' && x != '=' && x != '#' && \
469 x != ','))
471 if (isalnum(c) || c == ':' || c == '_') {
472 do {
473 *p++ = c;
474 if ((unsigned)(p-buf) >= sizeof(buf)) {
475 yyerror("string too long");
476 return (findeol());
478 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
479 lungetc(c);
480 *p = '\0';
481 if ((token = lookup(buf)) == STRING)
482 if ((yylval.v.string = strdup(buf)) == NULL)
483 err(1, "%s", __func__);
484 return (token);
486 if (c == '\n') {
487 yylval.lineno = file->lineno;
488 file->lineno++;
490 if (c == EOF)
491 return (0);
492 return (c);
495 static const struct got_error*
496 pushfile(struct file **nfile, const char *name)
498 const struct got_error* error = NULL;
500 if (((*nfile) = calloc(1, sizeof(struct file))) == NULL)
501 return got_error_from_errno2(__func__, "calloc");
502 if (((*nfile)->name = strdup(name)) == NULL) {
503 free(nfile);
504 return got_error_from_errno2(__func__, "strdup");
506 if (((*nfile)->stream = fopen((*nfile)->name, "r")) == NULL) {
507 char *msg = NULL;
508 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
509 return got_error_from_errno("asprintf");
510 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
511 free((*nfile)->name);
512 free((*nfile));
513 free(msg);
514 return error;
516 (*nfile)->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
517 (*nfile)->ungetsize = 16;
518 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
519 if ((*nfile)->ungetbuf == NULL) {
520 fclose((*nfile)->stream);
521 free((*nfile)->name);
522 free((*nfile));
523 return got_error_from_errno2(__func__, "malloc");
525 TAILQ_INSERT_TAIL(&files, (*nfile), entry);
526 return error;
529 static const struct got_error*
530 new_remote(struct gotconfig_remote_repo **remote)
532 const struct got_error *error = NULL;
534 *remote = calloc(1, sizeof(**remote));
535 if (*remote == NULL)
536 error = got_error_from_errno("calloc");
537 return error;
540 int
541 popfile(void)
543 struct file *prev = NULL;
545 TAILQ_REMOVE(&files, file, entry);
546 fclose(file->stream);
547 free(file->name);
548 free(file->ungetbuf);
549 free(file);
550 file = prev;
551 return (file ? 0 : EOF);
554 const struct got_error*
555 gotconfig_parse(struct gotconfig **conf, const char *filename)
557 struct sym *sym, *next;
559 *conf = NULL;
561 /*
562 * We don't require that gotconfig exists
563 * So, null gerror and goto done
564 */
565 gerror = pushfile(&file, filename);
566 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
567 gerror = NULL;
568 goto done;
569 } else if (gerror)
570 return gerror;
572 TAILQ_INIT(&gotconfig.remotes);
573 topfile = file;
575 yyparse();
576 popfile();
578 /* Free macros and check which have not been used. */
579 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
580 if (!sym->persist) {
581 free(sym->nam);
582 free(sym->val);
583 TAILQ_REMOVE(&symhead, sym, entry);
584 free(sym);
587 done:
588 *conf = &gotconfig;
589 return gerror;
592 void
593 gotconfig_free(struct gotconfig *conf)
595 struct gotconfig_remote_repo *remote;
597 while (!TAILQ_EMPTY(&conf->remotes)) {
598 remote = TAILQ_FIRST(&conf->remotes);
599 TAILQ_REMOVE(&conf->remotes, remote, entry);
600 free(remote->name);
601 free(remote->repository);
602 free(remote->server);
603 free(remote->protocol);
604 free(remote->user);
605 free(remote);
609 int
610 symset(const char *nam, const char *val, int persist)
612 struct sym *sym;
614 TAILQ_FOREACH(sym, &symhead, entry) {
615 if (strcmp(nam, sym->nam) == 0)
616 break;
619 if (sym != NULL) {
620 if (sym->persist == 1)
621 return (0);
622 else {
623 free(sym->nam);
624 free(sym->val);
625 TAILQ_REMOVE(&symhead, sym, entry);
626 free(sym);
629 if ((sym = calloc(1, sizeof(*sym))) == NULL)
630 return (-1);
632 sym->nam = strdup(nam);
633 if (sym->nam == NULL) {
634 free(sym);
635 return (-1);
637 sym->val = strdup(val);
638 if (sym->val == NULL) {
639 free(sym->nam);
640 free(sym);
641 return (-1);
643 sym->used = 0;
644 sym->persist = persist;
645 TAILQ_INSERT_TAIL(&symhead, sym, entry);
646 return (0);
649 int
650 cmdline_symset(char *s)
652 char *sym, *val;
653 int ret;
654 size_t len;
656 if ((val = strrchr(s, '=')) == NULL)
657 return (-1);
659 len = strlen(s) - strlen(val) + 1;
660 if ((sym = malloc(len)) == NULL)
661 errx(1, "cmdline_symset: malloc");
663 strlcpy(sym, s, len);
665 ret = symset(sym, val + 1, 1);
666 free(sym);
668 return (ret);
671 char *
672 symget(const char *nam)
674 struct sym *sym;
676 TAILQ_FOREACH(sym, &symhead, entry) {
677 if (strcmp(nam, sym->nam) == 0) {
678 sym->used = 1;
679 return (sym->val);
682 return (NULL);