2 cfd92333 2021-08-27 tracey * Copyright (c) 2020, 2021 Tracey Emery <tracey@openbsd.org>
3 257add31 2020-09-09 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 257add31 2020-09-09 stsp * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
5 257add31 2020-09-09 stsp * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
6 257add31 2020-09-09 stsp * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
7 257add31 2020-09-09 stsp * Copyright (c) 2001 Markus Friedl. All rights reserved.
8 257add31 2020-09-09 stsp * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
9 257add31 2020-09-09 stsp * Copyright (c) 2001 Theo de Raadt. All rights reserved.
11 257add31 2020-09-09 stsp * Permission to use, copy, modify, and distribute this software for any
12 257add31 2020-09-09 stsp * purpose with or without fee is hereby granted, provided that the above
13 257add31 2020-09-09 stsp * copyright notice and this permission notice appear in all copies.
15 257add31 2020-09-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
16 257add31 2020-09-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 257add31 2020-09-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
18 257add31 2020-09-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 257add31 2020-09-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 257add31 2020-09-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 257add31 2020-09-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 257add31 2020-09-09 stsp #include <sys/types.h>
27 257add31 2020-09-09 stsp #include <netdb.h>
29 257add31 2020-09-09 stsp #include <ctype.h>
30 257add31 2020-09-09 stsp #include <err.h>
31 257add31 2020-09-09 stsp #include <errno.h>
32 257add31 2020-09-09 stsp #include <limits.h>
33 257add31 2020-09-09 stsp #include <stdarg.h>
34 257add31 2020-09-09 stsp #include <stdio.h>
35 8de9818a 2020-09-14 naddy #include <stdlib.h>
36 257add31 2020-09-09 stsp #include <string.h>
38 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
40 257add31 2020-09-09 stsp #include "got_error.h"
41 257add31 2020-09-09 stsp #include "gotconfig.h"
43 257add31 2020-09-09 stsp static struct file {
44 257add31 2020-09-09 stsp FILE *stream;
45 257add31 2020-09-09 stsp const char *name;
46 257add31 2020-09-09 stsp size_t ungetpos;
47 257add31 2020-09-09 stsp size_t ungetsize;
48 257add31 2020-09-09 stsp u_char *ungetbuf;
49 257add31 2020-09-09 stsp int eof_reached;
52 257add31 2020-09-09 stsp static const struct got_error* newfile(struct file**, const char *, int *);
53 257add31 2020-09-09 stsp static void closefile(struct file *);
54 257add31 2020-09-09 stsp int yyparse(void);
55 257add31 2020-09-09 stsp int yylex(void);
56 257add31 2020-09-09 stsp int yyerror(const char *, ...)
57 257add31 2020-09-09 stsp __attribute__((__format__ (printf, 1, 2)))
58 257add31 2020-09-09 stsp __attribute__((__nonnull__ (1)));
59 257add31 2020-09-09 stsp int kw_cmp(const void *, const void *);
60 257add31 2020-09-09 stsp int lookup(char *);
61 257add31 2020-09-09 stsp int igetc(void);
62 257add31 2020-09-09 stsp int lgetc(int);
63 257add31 2020-09-09 stsp void lungetc(int);
64 257add31 2020-09-09 stsp int findeol(void);
65 257add31 2020-09-09 stsp static int parseport(char *, long long *);
67 257add31 2020-09-09 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 257add31 2020-09-09 stsp struct sym {
69 257add31 2020-09-09 stsp TAILQ_ENTRY(sym) entry;
71 257add31 2020-09-09 stsp int persist;
76 257add31 2020-09-09 stsp int symset(const char *, const char *, int);
77 257add31 2020-09-09 stsp char *symget(const char *);
79 257add31 2020-09-09 stsp static int atoul(char *, u_long *);
81 257add31 2020-09-09 stsp static const struct got_error* gerror;
82 257add31 2020-09-09 stsp static struct gotconfig_remote_repo *remote;
83 257add31 2020-09-09 stsp static struct gotconfig gotconfig;
84 257add31 2020-09-09 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
85 aaf30ee7 2021-08-29 stsp static const struct got_error* new_fetch_config(struct fetch_config **);
86 aaf30ee7 2021-08-29 stsp static const struct got_error* new_send_config(struct send_config **);
88 257add31 2020-09-09 stsp typedef struct {
90 1367695b 2020-09-26 naddy long long number;
91 257add31 2020-09-09 stsp char *string;
92 b8adfa55 2020-09-25 stsp struct node_branch *branch;
93 99495ddb 2021-01-10 stsp struct node_ref *ref;
100 257add31 2020-09-09 stsp %token ERROR
101 b8adfa55 2020-09-25 stsp %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
102 cfd92333 2021-08-27 tracey %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
103 257add31 2020-09-09 stsp %token <v.string> STRING
104 257add31 2020-09-09 stsp %token <v.number> NUMBER
105 257add31 2020-09-09 stsp %type <v.number> boolean portplain
106 257add31 2020-09-09 stsp %type <v.string> numberstring
107 b8adfa55 2020-09-25 stsp %type <v.branch> branch xbranch branch_list
108 99495ddb 2021-01-10 stsp %type <v.ref> ref xref ref_list
112 257add31 2020-09-09 stsp grammar : /* empty */
113 257add31 2020-09-09 stsp | grammar '\n'
114 257add31 2020-09-09 stsp | grammar author '\n'
115 257add31 2020-09-09 stsp | grammar remote '\n'
117 257add31 2020-09-09 stsp boolean : STRING {
118 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
119 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
121 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
122 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
125 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
132 257add31 2020-09-09 stsp numberstring : NUMBER {
134 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
135 257add31 2020-09-09 stsp yyerror("string: asprintf");
142 257add31 2020-09-09 stsp portplain : numberstring {
143 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
150 b8adfa55 2020-09-25 stsp branch : /* empty */ { $$ = NULL; }
151 b8adfa55 2020-09-25 stsp | xbranch { $$ = $1; }
152 b8adfa55 2020-09-25 stsp | '{' optnl branch_list '}' { $$ = $3; }
154 b8adfa55 2020-09-25 stsp xbranch : STRING {
155 b8adfa55 2020-09-25 stsp $$ = calloc(1, sizeof(struct node_branch));
156 b8adfa55 2020-09-25 stsp if ($$ == NULL) {
157 b8adfa55 2020-09-25 stsp yyerror("calloc");
160 b8adfa55 2020-09-25 stsp $$->branch_name = $1;
161 b8adfa55 2020-09-25 stsp $$->tail = $$;
164 b8adfa55 2020-09-25 stsp branch_list : xbranch optnl { $$ = $1; }
165 b8adfa55 2020-09-25 stsp | branch_list comma xbranch optnl {
166 b8adfa55 2020-09-25 stsp $1->tail->next = $3;
167 b8adfa55 2020-09-25 stsp $1->tail = $3;
171 99495ddb 2021-01-10 stsp ref : /* empty */ { $$ = NULL; }
172 99495ddb 2021-01-10 stsp | xref { $$ = $1; }
173 99495ddb 2021-01-10 stsp | '{' optnl ref_list '}' { $$ = $3; }
175 99495ddb 2021-01-10 stsp xref : STRING {
176 99495ddb 2021-01-10 stsp $$ = calloc(1, sizeof(struct node_ref));
177 99495ddb 2021-01-10 stsp if ($$ == NULL) {
178 99495ddb 2021-01-10 stsp yyerror("calloc");
181 99495ddb 2021-01-10 stsp $$->ref_name = $1;
182 99495ddb 2021-01-10 stsp $$->tail = $$;
185 99495ddb 2021-01-10 stsp ref_list : xref optnl { $$ = $1; }
186 99495ddb 2021-01-10 stsp | ref_list comma xref optnl {
187 99495ddb 2021-01-10 stsp $1->tail->next = $3;
188 99495ddb 2021-01-10 stsp $1->tail = $3;
192 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
193 abc59930 2021-09-05 naddy | remoteopts1 optnl
195 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
196 abc59930 2021-09-05 naddy remote->repository = $2;
198 abc59930 2021-09-05 naddy | SERVER STRING {
199 abc59930 2021-09-05 naddy remote->server = $2;
201 257add31 2020-09-09 stsp | PROTOCOL STRING {
202 abc59930 2021-09-05 naddy remote->protocol = $2;
204 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
205 257add31 2020-09-09 stsp remote->mirror_references = $2;
207 0c8b29c5 2021-01-05 stsp | FETCH_ALL_BRANCHES boolean {
208 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = $2;
210 257add31 2020-09-09 stsp | PORT portplain {
211 257add31 2020-09-09 stsp remote->port = $2;
213 b8adfa55 2020-09-25 stsp | BRANCH branch {
214 b8adfa55 2020-09-25 stsp remote->branch = $2;
216 99495ddb 2021-01-10 stsp | REFERENCE ref {
217 6480c871 2021-08-30 stsp remote->fetch_ref = $2;
219 0ff2bf46 2021-08-27 tracey | FETCH {
220 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
222 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL) {
223 0ff2bf46 2021-08-27 tracey yyerror("fetch block already exists");
226 aaf30ee7 2021-08-29 stsp error = new_fetch_config(&remote->fetch_config);
227 0ff2bf46 2021-08-27 tracey if (error) {
228 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
231 f08eaca0 2021-08-30 tracey } '{' optnl fetchempty '}'
233 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
235 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL) {
236 0ff2bf46 2021-08-27 tracey yyerror("send block already exists");
239 aaf30ee7 2021-08-29 stsp error = new_send_config(&remote->send_config);
240 0ff2bf46 2021-08-27 tracey if (error) {
241 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
244 f08eaca0 2021-08-30 tracey } '{' optnl sendempty '}'
246 f08eaca0 2021-08-30 tracey fetchempty : /* empty */
247 f08eaca0 2021-08-30 tracey | fetchopts2
249 cfd92333 2021-08-27 tracey fetchopts2 : fetchopts2 fetchopts1 nl
250 abc59930 2021-09-05 naddy | fetchopts1 optnl
252 92952c0e 2021-08-30 stsp fetchopts1 : REPOSITORY STRING {
253 abc59930 2021-09-05 naddy remote->fetch_config->repository = $2;
255 abc59930 2021-09-05 naddy | SERVER STRING {
256 abc59930 2021-09-05 naddy remote->fetch_config->server = $2;
258 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
259 abc59930 2021-09-05 naddy remote->fetch_config->protocol = $2;
261 cfd92333 2021-08-27 tracey | PORT portplain {
262 aaf30ee7 2021-08-29 stsp remote->fetch_config->port = $2;
264 cfd92333 2021-08-27 tracey | BRANCH branch {
265 aaf30ee7 2021-08-29 stsp remote->fetch_config->branch = $2;
268 f08eaca0 2021-08-30 tracey sendempty : /* empty */
269 f08eaca0 2021-08-30 tracey | sendopts2
271 cfd92333 2021-08-27 tracey sendopts2 : sendopts2 sendopts1 nl
272 abc59930 2021-09-05 naddy | sendopts1 optnl
274 92952c0e 2021-08-30 stsp sendopts1 : REPOSITORY STRING {
275 abc59930 2021-09-05 naddy remote->send_config->repository = $2;
277 abc59930 2021-09-05 naddy | SERVER STRING {
278 abc59930 2021-09-05 naddy remote->send_config->server = $2;
280 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
281 abc59930 2021-09-05 naddy remote->send_config->protocol = $2;
283 cfd92333 2021-08-27 tracey | PORT portplain {
284 aaf30ee7 2021-08-29 stsp remote->send_config->port = $2;
286 cfd92333 2021-08-27 tracey | BRANCH branch {
287 aaf30ee7 2021-08-29 stsp remote->send_config->branch = $2;
290 257add31 2020-09-09 stsp remote : REMOTE STRING {
291 257add31 2020-09-09 stsp static const struct got_error* error;
293 257add31 2020-09-09 stsp error = new_remote(&remote);
294 257add31 2020-09-09 stsp if (error) {
296 257add31 2020-09-09 stsp yyerror("%s", error->msg);
299 c2d7bc3f 2021-08-31 stsp remote->name = $2;
300 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
301 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
302 257add31 2020-09-09 stsp gotconfig.nremotes++;
305 257add31 2020-09-09 stsp author : AUTHOR STRING {
306 abc59930 2021-09-05 naddy gotconfig.author = $2;
309 257add31 2020-09-09 stsp optnl : '\n' optnl
310 257add31 2020-09-09 stsp | /* empty */
312 257add31 2020-09-09 stsp nl : '\n' optnl
314 b8adfa55 2020-09-25 stsp comma : ','
315 b8adfa55 2020-09-25 stsp | /* empty */
319 257add31 2020-09-09 stsp struct keywords {
320 257add31 2020-09-09 stsp const char *k_name;
325 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
327 257add31 2020-09-09 stsp va_list ap;
329 257add31 2020-09-09 stsp char *err = NULL;
331 257add31 2020-09-09 stsp va_start(ap, fmt);
332 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
333 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
336 257add31 2020-09-09 stsp va_end(ap);
337 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
338 257add31 2020-09-09 stsp msg) == -1) {
339 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
342 c2d7bc3f 2021-08-31 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
347 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
349 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
353 257add31 2020-09-09 stsp lookup(char *s)
355 257add31 2020-09-09 stsp /* This has to be sorted always. */
356 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
357 257add31 2020-09-09 stsp {"author", AUTHOR},
358 b8adfa55 2020-09-25 stsp {"branch", BRANCH},
359 cfd92333 2021-08-27 tracey {"fetch", FETCH},
360 0c8b29c5 2021-01-05 stsp {"fetch-all-branches", FETCH_ALL_BRANCHES},
361 257add31 2020-09-09 stsp {"mirror-references", MIRROR_REFERENCES},
362 257add31 2020-09-09 stsp {"port", PORT},
363 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
364 99495ddb 2021-01-10 stsp {"reference", REFERENCE},
365 257add31 2020-09-09 stsp {"remote", REMOTE},
366 257add31 2020-09-09 stsp {"repository", REPOSITORY},
367 cfd92333 2021-08-27 tracey {"send", SEND},
368 257add31 2020-09-09 stsp {"server", SERVER},
370 257add31 2020-09-09 stsp const struct keywords *p;
372 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
373 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
376 257add31 2020-09-09 stsp return (p->k_val);
378 257add31 2020-09-09 stsp return (STRING);
381 257add31 2020-09-09 stsp #define START_EXPAND 1
382 257add31 2020-09-09 stsp #define DONE_EXPAND 2
384 257add31 2020-09-09 stsp static int expanding;
387 257add31 2020-09-09 stsp igetc(void)
391 257add31 2020-09-09 stsp while (1) {
392 257add31 2020-09-09 stsp if (file->ungetpos > 0)
393 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
395 257add31 2020-09-09 stsp c = getc(file->stream);
397 257add31 2020-09-09 stsp if (c == START_EXPAND)
398 257add31 2020-09-09 stsp expanding = 1;
399 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
400 257add31 2020-09-09 stsp expanding = 0;
404 257add31 2020-09-09 stsp return (c);
408 257add31 2020-09-09 stsp lgetc(int quotec)
410 257add31 2020-09-09 stsp int c, next;
412 257add31 2020-09-09 stsp if (quotec) {
413 257add31 2020-09-09 stsp c = igetc();
414 257add31 2020-09-09 stsp if (c == EOF) {
415 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
416 257add31 2020-09-09 stsp "quoted string");
418 257add31 2020-09-09 stsp return (c);
421 257add31 2020-09-09 stsp c = igetc();
422 257add31 2020-09-09 stsp while (c == '\\') {
423 257add31 2020-09-09 stsp next = igetc();
424 257add31 2020-09-09 stsp if (next != '\n') {
428 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
429 257add31 2020-09-09 stsp file->lineno++;
432 257add31 2020-09-09 stsp return (c);
436 257add31 2020-09-09 stsp lungetc(int c)
438 257add31 2020-09-09 stsp if (c == EOF)
441 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
442 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
443 257add31 2020-09-09 stsp if (p == NULL)
444 257add31 2020-09-09 stsp err(1, "%s", __func__);
445 257add31 2020-09-09 stsp file->ungetbuf = p;
446 257add31 2020-09-09 stsp file->ungetsize *= 2;
448 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
452 257add31 2020-09-09 stsp findeol(void)
456 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
457 257add31 2020-09-09 stsp while (1) {
458 257add31 2020-09-09 stsp c = lgetc(0);
459 257add31 2020-09-09 stsp if (c == '\n') {
460 257add31 2020-09-09 stsp file->lineno++;
463 257add31 2020-09-09 stsp if (c == EOF)
466 257add31 2020-09-09 stsp return (ERROR);
469 257add31 2020-09-09 stsp static long long
470 257add31 2020-09-09 stsp getservice(char *n)
472 257add31 2020-09-09 stsp struct servent *s;
473 257add31 2020-09-09 stsp u_long ulval;
475 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
476 7c84ef07 2021-08-29 stsp if (ulval == 0 || ulval > 65535) {
477 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
478 257add31 2020-09-09 stsp return (-1);
480 257add31 2020-09-09 stsp return ulval;
482 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
483 257add31 2020-09-09 stsp if (s == NULL)
484 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
485 257add31 2020-09-09 stsp if (s == NULL) {
486 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
487 257add31 2020-09-09 stsp return (-1);
489 257add31 2020-09-09 stsp return (s->s_port);
494 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
496 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
498 257add31 2020-09-09 stsp return (-1);
500 257add31 2020-09-09 stsp return (0);
505 257add31 2020-09-09 stsp yylex(void)
507 1a670123 2021-09-28 thomas char buf[8096];
508 1a670123 2021-09-28 thomas char *p, *val;
509 1a670123 2021-09-28 thomas int quotec, next, c;
510 1a670123 2021-09-28 thomas int token;
514 257add31 2020-09-09 stsp c = lgetc(0);
515 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
516 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
518 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
519 257add31 2020-09-09 stsp if (c == '#') {
520 257add31 2020-09-09 stsp c = lgetc(0);
521 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
522 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
524 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
525 257add31 2020-09-09 stsp while (1) {
526 257add31 2020-09-09 stsp c = lgetc(0);
527 257add31 2020-09-09 stsp if (c == EOF)
528 257add31 2020-09-09 stsp return (0);
530 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
531 257add31 2020-09-09 stsp yyerror("string too long");
532 257add31 2020-09-09 stsp return (findeol());
534 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
539 257add31 2020-09-09 stsp lungetc(c);
542 257add31 2020-09-09 stsp val = symget(buf);
543 257add31 2020-09-09 stsp if (val == NULL) {
544 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
545 257add31 2020-09-09 stsp return (findeol());
547 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
548 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
549 257add31 2020-09-09 stsp while (p >= val) {
550 bb27d0d1 2021-10-15 thomas lungetc((unsigned char)*p);
553 257add31 2020-09-09 stsp lungetc(START_EXPAND);
557 257add31 2020-09-09 stsp switch (c) {
560 257add31 2020-09-09 stsp quotec = c;
561 257add31 2020-09-09 stsp while (1) {
562 257add31 2020-09-09 stsp c = lgetc(quotec);
563 257add31 2020-09-09 stsp if (c == EOF)
564 257add31 2020-09-09 stsp return (0);
565 257add31 2020-09-09 stsp if (c == '\n') {
566 257add31 2020-09-09 stsp file->lineno++;
568 257add31 2020-09-09 stsp } else if (c == '\\') {
569 257add31 2020-09-09 stsp next = lgetc(quotec);
570 257add31 2020-09-09 stsp if (next == EOF)
571 257add31 2020-09-09 stsp return (0);
572 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
574 257add31 2020-09-09 stsp else if (next == '\n') {
575 257add31 2020-09-09 stsp file->lineno++;
578 257add31 2020-09-09 stsp lungetc(next);
579 257add31 2020-09-09 stsp } else if (c == quotec) {
582 257add31 2020-09-09 stsp } else if (c == '\0') {
583 257add31 2020-09-09 stsp yyerror("syntax error");
584 257add31 2020-09-09 stsp return (findeol());
586 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
587 257add31 2020-09-09 stsp yyerror("string too long");
588 257add31 2020-09-09 stsp return (findeol());
592 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
593 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
594 257add31 2020-09-09 stsp err(1, "%s", __func__);
595 257add31 2020-09-09 stsp return (STRING);
598 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
599 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
601 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
604 ccd081e7 2021-09-30 thomas if ((size_t)(p-buf) >= sizeof(buf)) {
605 257add31 2020-09-09 stsp yyerror("string too long");
606 257add31 2020-09-09 stsp return (findeol());
608 257add31 2020-09-09 stsp c = lgetc(0);
609 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
610 257add31 2020-09-09 stsp lungetc(c);
611 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
612 257add31 2020-09-09 stsp goto nodigits;
613 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
614 257add31 2020-09-09 stsp const char *errstr = NULL;
617 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
618 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
619 257add31 2020-09-09 stsp if (errstr) {
620 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
621 257add31 2020-09-09 stsp buf, errstr);
622 257add31 2020-09-09 stsp return (findeol());
624 257add31 2020-09-09 stsp return (NUMBER);
627 257add31 2020-09-09 stsp while (p > buf + 1)
628 bb27d0d1 2021-10-15 thomas lungetc((unsigned char)*--p);
629 bb27d0d1 2021-10-15 thomas c = (unsigned char)*--p;
630 257add31 2020-09-09 stsp if (c == '-')
631 257add31 2020-09-09 stsp return (c);
635 257add31 2020-09-09 stsp #define allowed_in_string(x) \
636 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
637 257add31 2020-09-09 stsp x != '{' && x != '}' && \
638 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
641 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
644 ccd081e7 2021-09-30 thomas if ((size_t)(p-buf) >= sizeof(buf)) {
645 257add31 2020-09-09 stsp yyerror("string too long");
646 257add31 2020-09-09 stsp return (findeol());
648 257add31 2020-09-09 stsp c = lgetc(0);
649 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
650 257add31 2020-09-09 stsp lungetc(c);
652 257add31 2020-09-09 stsp token = lookup(buf);
653 257add31 2020-09-09 stsp if (token == STRING) {
654 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
655 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
656 257add31 2020-09-09 stsp err(1, "%s", __func__);
658 257add31 2020-09-09 stsp return (token);
660 257add31 2020-09-09 stsp if (c == '\n') {
661 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
662 257add31 2020-09-09 stsp file->lineno++;
664 257add31 2020-09-09 stsp if (c == EOF)
665 257add31 2020-09-09 stsp return (0);
666 257add31 2020-09-09 stsp return (c);
669 257add31 2020-09-09 stsp static const struct got_error*
670 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
672 257add31 2020-09-09 stsp const struct got_error* error = NULL;
674 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
675 257add31 2020-09-09 stsp if ((*nfile) == NULL)
676 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
677 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
678 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
679 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
680 257add31 2020-09-09 stsp free((*nfile));
681 257add31 2020-09-09 stsp return error;
683 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
684 257add31 2020-09-09 stsp (*nfile)->name = filename;
685 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
686 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
687 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
688 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
689 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
690 257add31 2020-09-09 stsp fclose((*nfile)->stream);
691 257add31 2020-09-09 stsp free((*nfile));
692 257add31 2020-09-09 stsp return error;
694 257add31 2020-09-09 stsp return NULL;
697 257add31 2020-09-09 stsp static const struct got_error*
698 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
700 257add31 2020-09-09 stsp const struct got_error *error = NULL;
702 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
703 257add31 2020-09-09 stsp if (*remote == NULL)
704 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
705 cfd92333 2021-08-27 tracey return error;
708 cfd92333 2021-08-27 tracey static const struct got_error*
709 aaf30ee7 2021-08-29 stsp new_fetch_config(struct fetch_config **fetch_config)
711 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
713 aaf30ee7 2021-08-29 stsp *fetch_config = calloc(1, sizeof(**fetch_config));
714 aaf30ee7 2021-08-29 stsp if (*fetch_config == NULL)
715 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
716 cfd92333 2021-08-27 tracey return error;
719 cfd92333 2021-08-27 tracey static const struct got_error*
720 aaf30ee7 2021-08-29 stsp new_send_config(struct send_config **send_config)
722 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
724 aaf30ee7 2021-08-29 stsp *send_config = calloc(1, sizeof(**send_config));
725 aaf30ee7 2021-08-29 stsp if (*send_config == NULL)
726 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
727 257add31 2020-09-09 stsp return error;
730 257add31 2020-09-09 stsp static void
731 257add31 2020-09-09 stsp closefile(struct file *file)
733 257add31 2020-09-09 stsp fclose(file->stream);
734 257add31 2020-09-09 stsp free(file->ungetbuf);
735 257add31 2020-09-09 stsp free(file);
738 257add31 2020-09-09 stsp const struct got_error *
739 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
741 257add31 2020-09-09 stsp const struct got_error *err = NULL;
742 257add31 2020-09-09 stsp struct sym *sym, *next;
744 257add31 2020-09-09 stsp *conf = NULL;
746 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
748 257add31 2020-09-09 stsp return err;
750 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
753 257add31 2020-09-09 stsp closefile(file);
755 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
756 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
757 257add31 2020-09-09 stsp if (!sym->persist) {
758 257add31 2020-09-09 stsp free(sym->nam);
759 257add31 2020-09-09 stsp free(sym->val);
760 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
765 257add31 2020-09-09 stsp if (gerror == NULL)
766 257add31 2020-09-09 stsp *conf = &gotconfig;
767 257add31 2020-09-09 stsp return gerror;
770 aaf30ee7 2021-08-29 stsp static void
771 aaf30ee7 2021-08-29 stsp free_fetch_config(struct fetch_config *fetch_config)
773 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->repository);
774 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->server);
775 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->protocol);
776 aaf30ee7 2021-08-29 stsp free(remote->fetch_config);
779 aaf30ee7 2021-08-29 stsp static void
780 aaf30ee7 2021-08-29 stsp free_send_config(struct send_config *send_config)
782 aaf30ee7 2021-08-29 stsp free(remote->send_config->repository);
783 aaf30ee7 2021-08-29 stsp free(remote->send_config->server);
784 aaf30ee7 2021-08-29 stsp free(remote->send_config->protocol);
785 aaf30ee7 2021-08-29 stsp free(remote->send_config);
789 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
791 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
793 257add31 2020-09-09 stsp free(conf->author);
794 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
795 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
796 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
797 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL)
798 aaf30ee7 2021-08-29 stsp free_fetch_config(remote->fetch_config);
799 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL)
800 aaf30ee7 2021-08-29 stsp free_send_config(remote->send_config);
801 257add31 2020-09-09 stsp free(remote->name);
802 257add31 2020-09-09 stsp free(remote->repository);
803 257add31 2020-09-09 stsp free(remote->server);
804 257add31 2020-09-09 stsp free(remote->protocol);
805 257add31 2020-09-09 stsp free(remote);
810 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
812 257add31 2020-09-09 stsp struct sym *sym;
814 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
815 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
819 257add31 2020-09-09 stsp if (sym != NULL) {
820 257add31 2020-09-09 stsp if (sym->persist == 1)
821 257add31 2020-09-09 stsp return (0);
823 257add31 2020-09-09 stsp free(sym->nam);
824 257add31 2020-09-09 stsp free(sym->val);
825 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
829 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
830 257add31 2020-09-09 stsp if (sym == NULL)
831 257add31 2020-09-09 stsp return (-1);
833 257add31 2020-09-09 stsp sym->nam = strdup(nam);
834 257add31 2020-09-09 stsp if (sym->nam == NULL) {
836 257add31 2020-09-09 stsp return (-1);
838 257add31 2020-09-09 stsp sym->val = strdup(val);
839 257add31 2020-09-09 stsp if (sym->val == NULL) {
840 257add31 2020-09-09 stsp free(sym->nam);
842 257add31 2020-09-09 stsp return (-1);
844 257add31 2020-09-09 stsp sym->used = 0;
845 257add31 2020-09-09 stsp sym->persist = persist;
846 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
847 257add31 2020-09-09 stsp return (0);
851 257add31 2020-09-09 stsp cmdline_symset(char *s)
853 257add31 2020-09-09 stsp char *sym, *val;
855 257add31 2020-09-09 stsp size_t len;
857 257add31 2020-09-09 stsp val = strrchr(s, '=');
858 257add31 2020-09-09 stsp if (val == NULL)
859 257add31 2020-09-09 stsp return (-1);
861 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
862 257add31 2020-09-09 stsp sym = malloc(len);
863 257add31 2020-09-09 stsp if (sym == NULL)
864 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
866 257add31 2020-09-09 stsp strlcpy(sym, s, len);
868 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
871 257add31 2020-09-09 stsp return (ret);
875 257add31 2020-09-09 stsp symget(const char *nam)
877 257add31 2020-09-09 stsp struct sym *sym;
879 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
880 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
881 257add31 2020-09-09 stsp sym->used = 1;
882 257add31 2020-09-09 stsp return (sym->val);
885 257add31 2020-09-09 stsp return (NULL);
889 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
891 257add31 2020-09-09 stsp u_long ulval;
895 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
896 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
897 257add31 2020-09-09 stsp return (-1);
898 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
899 257add31 2020-09-09 stsp return (-1);
900 257add31 2020-09-09 stsp *ulvalp = ulval;
901 257add31 2020-09-09 stsp return (0);