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 static struct file {
50 FILE *stream;
51 char *name;
52 size_t ungetpos;
53 size_t ungetsize;
54 u_char *ungetbuf;
55 int eof_reached;
56 int lineno;
57 } *file;
58 static const struct got_error* newfile(struct file**, const char *);
59 static void closefile(struct file *);
60 int yyparse(void);
61 int yylex(void);
62 int yyerror(const char *, ...)
63 __attribute__((__format__ (printf, 1, 2)))
64 __attribute__((__nonnull__ (1)));
65 int kw_cmp(const void *, const void *);
66 int lookup(char *);
67 int igetc(void);
68 int lgetc(int);
69 void lungetc(int);
70 int findeol(void);
72 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
73 struct sym {
74 TAILQ_ENTRY(sym) entry;
75 int used;
76 int persist;
77 char *nam;
78 char *val;
79 };
81 int symset(const char *, const char *, int);
82 char *symget(const char *);
84 const struct got_error* gerror = NULL;
85 struct gotconfig_remote_repo *remote;
86 struct gotconfig gotconfig;
87 static const struct got_error* new_remote(struct gotconfig_remote_repo **);
89 typedef struct {
90 union {
91 int64_t number;
92 char *string;
93 } v;
94 int lineno;
95 } YYSTYPE;
97 %}
99 %token ERROR
100 %token REMOTE REPOSITORY SERVER PROTOCOL USER
101 %token <v.string> STRING
102 %token <v.number> NUMBER
104 %%
106 grammar : /* empty */
107 | grammar '\n'
108 | grammar remote '\n'
110 remoteopts2 : remoteopts2 remoteopts1 nl
111 | remoteopts1 optnl
113 remoteopts1 : REPOSITORY STRING {
114 remote->repository = strdup($2);
115 if (remote->repository == NULL) {
116 free($2);
117 yyerror("strdup");
118 YYERROR;
120 free($2);
122 | SERVER STRING {
123 remote->server = strdup($2);
124 if (remote->server == NULL) {
125 free($2);
126 yyerror("strdup");
127 YYERROR;
129 free($2);
131 | PROTOCOL STRING {
132 remote->protocol = strdup($2);
133 if (remote->protocol == NULL) {
134 free($2);
135 yyerror("strdup");
136 YYERROR;
138 free($2);
140 | USER STRING {
141 remote->user = strdup($2);
142 if (remote->user == NULL) {
143 free($2);
144 yyerror("strdup");
145 YYERROR;
147 free($2);
150 remote : REMOTE STRING {
151 static const struct got_error* error;
153 error = new_remote(&remote);
154 if (error) {
155 free($2);
156 yyerror("%s", error->msg);
157 YYERROR;
159 remote->name = strdup($2);
160 if (remote->name == NULL) {
161 free($2);
162 yyerror("strdup");
163 YYERROR;
165 free($2);
166 } '{' optnl remoteopts2 '}' {
167 TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
168 gotconfig.nremotes++;
171 optnl : '\n' optnl
172 | /* empty */
174 nl : '\n' optnl
176 %%
178 struct keywords {
179 const char *k_name;
180 int k_val;
181 };
183 int
184 yyerror(const char *fmt, ...)
186 va_list ap;
187 char *msg;
188 char *err = NULL;
190 va_start(ap, fmt);
191 if (vasprintf(&msg, fmt, ap) == -1) {
192 gerror = got_error_from_errno("vasprintf");
193 return 0;
195 va_end(ap);
196 if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
197 gerror = got_error_from_errno("asprintf");
198 return(0);
200 gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
201 free(msg);
202 free(err);
203 return(0);
206 int
207 kw_cmp(const void *k, const void *e)
209 return (strcmp(k, ((const struct keywords *)e)->k_name));
212 int
213 lookup(char *s)
215 /* This has to be sorted always. */
216 static const struct keywords keywords[] = {
217 {"protocol", PROTOCOL},
218 {"remote", REMOTE},
219 {"repository", REPOSITORY},
220 {"server", SERVER},
221 {"user", USER},
222 };
223 const struct keywords *p;
225 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
226 sizeof(keywords[0]), kw_cmp);
228 if (p)
229 return (p->k_val);
230 else
231 return (STRING);
234 #define START_EXPAND 1
235 #define DONE_EXPAND 2
237 static int expanding;
239 int
240 igetc(void)
242 int c;
244 while (1) {
245 if (file->ungetpos > 0)
246 c = file->ungetbuf[--file->ungetpos];
247 else
248 c = getc(file->stream);
250 if (c == START_EXPAND)
251 expanding = 1;
252 else if (c == DONE_EXPAND)
253 expanding = 0;
254 else
255 break;
257 return (c);
260 int
261 lgetc(int quotec)
263 int c, next;
265 if (quotec) {
266 c = igetc();
267 if (c == EOF) {
268 yyerror("reached end of file while parsing "
269 "quoted string");
271 return (c);
274 c = igetc();
275 while (c == '\\') {
276 next = igetc();
277 if (next != '\n') {
278 c = next;
279 break;
281 yylval.lineno = file->lineno;
282 file->lineno++;
285 return (c);
288 void
289 lungetc(int c)
291 if (c == EOF)
292 return;
294 if (file->ungetpos >= file->ungetsize) {
295 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
296 if (p == NULL)
297 err(1, "%s", __func__);
298 file->ungetbuf = p;
299 file->ungetsize *= 2;
301 file->ungetbuf[file->ungetpos++] = c;
304 int
305 findeol(void)
307 int c;
309 /* Skip to either EOF or the first real EOL. */
310 while (1) {
311 c = lgetc(0);
312 if (c == '\n') {
313 file->lineno++;
314 break;
316 if (c == EOF)
317 break;
319 return (ERROR);
322 int
323 yylex(void)
325 unsigned char buf[8096];
326 unsigned char *p, *val;
327 int quotec, next, c;
328 int token;
330 top:
331 p = buf;
332 c = lgetc(0);
333 while (c == ' ' || c == '\t')
334 c = lgetc(0); /* nothing */
336 yylval.lineno = file->lineno;
337 if (c == '#') {
338 c = lgetc(0);
339 while (c != '\n' && c != EOF)
340 c = lgetc(0); /* nothing */
342 if (c == '$' && !expanding) {
343 while (1) {
344 c = lgetc(0);
345 if (c == EOF)
346 return (0);
348 if (p + 1 >= buf + sizeof(buf) - 1) {
349 yyerror("string too long");
350 return (findeol());
352 if (isalnum(c) || c == '_') {
353 *p++ = c;
354 continue;
356 *p = '\0';
357 lungetc(c);
358 break;
360 val = symget(buf);
361 if (val == NULL) {
362 yyerror("macro '%s' not defined", buf);
363 return (findeol());
365 p = val + strlen(val) - 1;
366 lungetc(DONE_EXPAND);
367 while (p >= val) {
368 lungetc(*p);
369 p--;
371 lungetc(START_EXPAND);
372 goto top;
375 switch (c) {
376 case '\'':
377 case '"':
378 quotec = c;
379 while (1) {
380 c = lgetc(quotec);
381 if (c == EOF)
382 return (0);
383 if (c == '\n') {
384 file->lineno++;
385 continue;
386 } else if (c == '\\') {
387 next = lgetc(quotec);
388 if (next == EOF)
389 return (0);
390 if (next == quotec || c == ' ' || c == '\t')
391 c = next;
392 else if (next == '\n') {
393 file->lineno++;
394 continue;
395 } else
396 lungetc(next);
397 } else if (c == quotec) {
398 *p = '\0';
399 break;
400 } else if (c == '\0') {
401 yyerror("syntax error");
402 return (findeol());
404 if (p + 1 >= buf + sizeof(buf) - 1) {
405 yyerror("string too long");
406 return (findeol());
408 *p++ = c;
410 yylval.v.string = strdup(buf);
411 if (yylval.v.string == NULL)
412 err(1, "%s", __func__);
413 return (STRING);
416 #define allowed_to_end_number(x) \
417 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
419 if (c == '-' || isdigit(c)) {
420 do {
421 *p++ = c;
422 if ((unsigned)(p-buf) >= sizeof(buf)) {
423 yyerror("string too long");
424 return (findeol());
426 c = lgetc(0);
427 } while (c != EOF && isdigit(c));
428 lungetc(c);
429 if (p == buf + 1 && buf[0] == '-')
430 goto nodigits;
431 if (c == EOF || allowed_to_end_number(c)) {
432 const char *errstr = NULL;
434 *p = '\0';
435 yylval.v.number = strtonum(buf, LLONG_MIN,
436 LLONG_MAX, &errstr);
437 if (errstr) {
438 yyerror("\"%s\" invalid number: %s",
439 buf, errstr);
440 return (findeol());
442 return (NUMBER);
443 } else {
444 nodigits:
445 while (p > buf + 1)
446 lungetc(*--p);
447 c = *--p;
448 if (c == '-')
449 return (c);
453 #define allowed_in_string(x) \
454 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
455 x != '{' && x != '}' && \
456 x != '!' && x != '=' && x != '#' && \
457 x != ','))
459 if (isalnum(c) || c == ':' || c == '_') {
460 do {
461 *p++ = c;
462 if ((unsigned)(p-buf) >= sizeof(buf)) {
463 yyerror("string too long");
464 return (findeol());
466 c = lgetc(0);
467 } while (c != EOF && (allowed_in_string(c)));
468 lungetc(c);
469 *p = '\0';
470 token = lookup(buf);
471 if (token == STRING) {
472 yylval.v.string = strdup(buf);
473 if (yylval.v.string == NULL)
474 err(1, "%s", __func__);
476 return (token);
478 if (c == '\n') {
479 yylval.lineno = file->lineno;
480 file->lineno++;
482 if (c == EOF)
483 return (0);
484 return (c);
487 static const struct got_error*
488 newfile(struct file **nfile, const char *name)
490 const struct got_error* error = NULL;
492 (*nfile) = calloc(1, sizeof(struct file));
493 if ((*nfile) == NULL)
494 return got_error_from_errno2(__func__, "calloc");
495 (*nfile)->name = strdup(name);
496 if ((*nfile)->name == NULL) {
497 free(nfile);
498 return got_error_from_errno2(__func__, "strdup");
500 (*nfile)->stream = fopen((*nfile)->name, "r");
501 if ((*nfile)->stream == NULL) {
502 char *msg = NULL;
503 if (asprintf(&msg, "%s", (*nfile)->name) == -1)
504 return got_error_from_errno("asprintf");
505 error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
506 free((*nfile)->name);
507 free((*nfile));
508 free(msg);
509 return error;
511 (*nfile)->lineno = 1;
512 (*nfile)->ungetsize = 16;
513 (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
514 if ((*nfile)->ungetbuf == NULL) {
515 fclose((*nfile)->stream);
516 free((*nfile)->name);
517 free((*nfile));
518 return got_error_from_errno2(__func__, "malloc");
520 return error;
523 static const struct got_error*
524 new_remote(struct gotconfig_remote_repo **remote)
526 const struct got_error *error = NULL;
528 *remote = calloc(1, sizeof(**remote));
529 if (*remote == NULL)
530 error = got_error_from_errno("calloc");
531 return error;
534 static void
535 closefile(struct file *file)
537 fclose(file->stream);
538 free(file->name);
539 free(file->ungetbuf);
540 free(file);
543 const struct got_error*
544 gotconfig_parse(struct gotconfig **conf, const char *filename)
546 struct sym *sym, *next;
548 *conf = NULL;
550 /*
551 * We don't require that gotconfig exists
552 * So, null gerror and goto done
553 */
554 gerror = newfile(&file, filename);
555 if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
556 gerror = NULL;
557 goto done;
558 } else if (gerror)
559 return gerror;
561 TAILQ_INIT(&gotconfig.remotes);
563 yyparse();
564 closefile(file);
566 /* Free macros and check which have not been used. */
567 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
568 if (!sym->persist) {
569 free(sym->nam);
570 free(sym->val);
571 TAILQ_REMOVE(&symhead, sym, entry);
572 free(sym);
575 done:
576 *conf = &gotconfig;
577 return gerror;
580 void
581 gotconfig_free(struct gotconfig *conf)
583 struct gotconfig_remote_repo *remote;
585 while (!TAILQ_EMPTY(&conf->remotes)) {
586 remote = TAILQ_FIRST(&conf->remotes);
587 TAILQ_REMOVE(&conf->remotes, remote, entry);
588 free(remote->name);
589 free(remote->repository);
590 free(remote->server);
591 free(remote->protocol);
592 free(remote->user);
593 free(remote);
597 int
598 symset(const char *nam, const char *val, int persist)
600 struct sym *sym;
602 TAILQ_FOREACH(sym, &symhead, entry) {
603 if (strcmp(nam, sym->nam) == 0)
604 break;
607 if (sym != NULL) {
608 if (sym->persist == 1)
609 return (0);
610 else {
611 free(sym->nam);
612 free(sym->val);
613 TAILQ_REMOVE(&symhead, sym, entry);
614 free(sym);
617 sym = calloc(1, sizeof(*sym));
618 if (sym == NULL)
619 return (-1);
621 sym->nam = strdup(nam);
622 if (sym->nam == NULL) {
623 free(sym);
624 return (-1);
626 sym->val = strdup(val);
627 if (sym->val == NULL) {
628 free(sym->nam);
629 free(sym);
630 return (-1);
632 sym->used = 0;
633 sym->persist = persist;
634 TAILQ_INSERT_TAIL(&symhead, sym, entry);
635 return (0);
638 int
639 cmdline_symset(char *s)
641 char *sym, *val;
642 int ret;
643 size_t len;
645 val = strrchr(s, '=');
646 if (val == NULL)
647 return (-1);
649 len = strlen(s) - strlen(val) + 1;
650 sym = malloc(len);
651 if (sym == NULL)
652 errx(1, "cmdline_symset: malloc");
654 strlcpy(sym, s, len);
656 ret = symset(sym, val + 1, 1);
657 free(sym);
659 return (ret);
662 char *
663 symget(const char *nam)
665 struct sym *sym;
667 TAILQ_FOREACH(sym, &symhead, entry) {
668 if (strcmp(nam, sym->nam) == 0) {
669 sym->used = 1;
670 return (sym->val);
673 return (NULL);