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 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
27 257add31 2020-09-09 stsp #include <sys/types.h>
28 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
30 257add31 2020-09-09 stsp #include <netdb.h>
32 257add31 2020-09-09 stsp #include <ctype.h>
33 257add31 2020-09-09 stsp #include <err.h>
34 257add31 2020-09-09 stsp #include <errno.h>
35 257add31 2020-09-09 stsp #include <limits.h>
36 257add31 2020-09-09 stsp #include <stdarg.h>
37 257add31 2020-09-09 stsp #include <stdio.h>
38 8de9818a 2020-09-14 naddy #include <stdlib.h>
39 257add31 2020-09-09 stsp #include <string.h>
41 257add31 2020-09-09 stsp #include "got_error.h"
42 257add31 2020-09-09 stsp #include "gotconfig.h"
44 257add31 2020-09-09 stsp static struct file {
45 257add31 2020-09-09 stsp FILE *stream;
46 257add31 2020-09-09 stsp const char *name;
47 257add31 2020-09-09 stsp size_t ungetpos;
48 257add31 2020-09-09 stsp size_t ungetsize;
49 257add31 2020-09-09 stsp u_char *ungetbuf;
50 257add31 2020-09-09 stsp int eof_reached;
53 257add31 2020-09-09 stsp static const struct got_error* newfile(struct file**, const char *, int *);
54 257add31 2020-09-09 stsp static void closefile(struct file *);
55 257add31 2020-09-09 stsp int yyparse(void);
56 257add31 2020-09-09 stsp int yylex(void);
57 257add31 2020-09-09 stsp int yyerror(const char *, ...)
58 257add31 2020-09-09 stsp __attribute__((__format__ (printf, 1, 2)))
59 257add31 2020-09-09 stsp __attribute__((__nonnull__ (1)));
60 257add31 2020-09-09 stsp int kw_cmp(const void *, const void *);
61 257add31 2020-09-09 stsp int lookup(char *);
62 257add31 2020-09-09 stsp int igetc(void);
63 257add31 2020-09-09 stsp int lgetc(int);
64 257add31 2020-09-09 stsp void lungetc(int);
65 257add31 2020-09-09 stsp int findeol(void);
66 257add31 2020-09-09 stsp static int parseport(char *, long long *);
68 257add31 2020-09-09 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
69 257add31 2020-09-09 stsp struct sym {
70 257add31 2020-09-09 stsp TAILQ_ENTRY(sym) entry;
72 257add31 2020-09-09 stsp int persist;
77 257add31 2020-09-09 stsp int symset(const char *, const char *, int);
78 ef20f542 2022-06-26 thomas int cmdline_symset(char *);
79 257add31 2020-09-09 stsp char *symget(const char *);
81 257add31 2020-09-09 stsp static int atoul(char *, u_long *);
83 257add31 2020-09-09 stsp static const struct got_error* gerror;
84 257add31 2020-09-09 stsp static struct gotconfig_remote_repo *remote;
85 257add31 2020-09-09 stsp static struct gotconfig gotconfig;
86 257add31 2020-09-09 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
87 aaf30ee7 2021-08-29 stsp static const struct got_error* new_fetch_config(struct fetch_config **);
88 aaf30ee7 2021-08-29 stsp static const struct got_error* new_send_config(struct send_config **);
90 257add31 2020-09-09 stsp typedef struct {
92 1367695b 2020-09-26 naddy long long number;
93 257add31 2020-09-09 stsp char *string;
94 b8adfa55 2020-09-25 stsp struct node_branch *branch;
95 99495ddb 2021-01-10 stsp struct node_ref *ref;
100 81e077a6 2022-03-08 thomas #if defined(__APPLE__) && !defined(YYSTYPE)
101 81e077a6 2022-03-08 thomas #warning "Setting YYSTYPE - is GNU Bison installed?"
102 81e077a6 2022-03-08 thomas #define YYSTYPE YYSTYPE
106 257add31 2020-09-09 stsp %token ERROR
107 b8adfa55 2020-09-25 stsp %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
108 ff5e1f09 2022-07-06 thomas %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS SIGNER_ID FETCH_ALL_BRANCHES
109 ff5e1f09 2022-07-06 thomas %token REFERENCE FETCH SEND
110 257add31 2020-09-09 stsp %token <v.string> STRING
111 257add31 2020-09-09 stsp %token <v.number> NUMBER
112 257add31 2020-09-09 stsp %type <v.number> boolean portplain
113 257add31 2020-09-09 stsp %type <v.string> numberstring
114 b8adfa55 2020-09-25 stsp %type <v.branch> branch xbranch branch_list
115 99495ddb 2021-01-10 stsp %type <v.ref> ref xref ref_list
119 257add31 2020-09-09 stsp grammar : /* empty */
120 257add31 2020-09-09 stsp | grammar '\n'
121 257add31 2020-09-09 stsp | grammar author '\n'
122 257add31 2020-09-09 stsp | grammar remote '\n'
123 871bd038 2022-07-03 thomas | grammar allowed_signers '\n'
124 aef4f7ae 2022-07-04 thomas | grammar revoked_signers '\n'
125 ff5e1f09 2022-07-06 thomas | grammar signer_id '\n'
127 257add31 2020-09-09 stsp boolean : STRING {
128 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
129 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
131 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
132 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
135 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
142 257add31 2020-09-09 stsp numberstring : NUMBER {
144 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
145 257add31 2020-09-09 stsp yyerror("string: asprintf");
152 257add31 2020-09-09 stsp portplain : numberstring {
153 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
160 b8adfa55 2020-09-25 stsp branch : /* empty */ { $$ = NULL; }
161 b8adfa55 2020-09-25 stsp | xbranch { $$ = $1; }
162 b8adfa55 2020-09-25 stsp | '{' optnl branch_list '}' { $$ = $3; }
164 b8adfa55 2020-09-25 stsp xbranch : STRING {
165 b8adfa55 2020-09-25 stsp $$ = calloc(1, sizeof(struct node_branch));
166 b8adfa55 2020-09-25 stsp if ($$ == NULL) {
167 b8adfa55 2020-09-25 stsp yyerror("calloc");
170 b8adfa55 2020-09-25 stsp $$->branch_name = $1;
171 b8adfa55 2020-09-25 stsp $$->tail = $$;
174 b8adfa55 2020-09-25 stsp branch_list : xbranch optnl { $$ = $1; }
175 b8adfa55 2020-09-25 stsp | branch_list comma xbranch optnl {
176 b8adfa55 2020-09-25 stsp $1->tail->next = $3;
177 b8adfa55 2020-09-25 stsp $1->tail = $3;
181 99495ddb 2021-01-10 stsp ref : /* empty */ { $$ = NULL; }
182 99495ddb 2021-01-10 stsp | xref { $$ = $1; }
183 99495ddb 2021-01-10 stsp | '{' optnl ref_list '}' { $$ = $3; }
185 99495ddb 2021-01-10 stsp xref : STRING {
186 99495ddb 2021-01-10 stsp $$ = calloc(1, sizeof(struct node_ref));
187 99495ddb 2021-01-10 stsp if ($$ == NULL) {
188 99495ddb 2021-01-10 stsp yyerror("calloc");
191 99495ddb 2021-01-10 stsp $$->ref_name = $1;
192 99495ddb 2021-01-10 stsp $$->tail = $$;
195 99495ddb 2021-01-10 stsp ref_list : xref optnl { $$ = $1; }
196 99495ddb 2021-01-10 stsp | ref_list comma xref optnl {
197 99495ddb 2021-01-10 stsp $1->tail->next = $3;
198 99495ddb 2021-01-10 stsp $1->tail = $3;
202 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
203 abc59930 2021-09-05 naddy | remoteopts1 optnl
205 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
206 abc59930 2021-09-05 naddy remote->repository = $2;
208 abc59930 2021-09-05 naddy | SERVER STRING {
209 abc59930 2021-09-05 naddy remote->server = $2;
211 257add31 2020-09-09 stsp | PROTOCOL STRING {
212 abc59930 2021-09-05 naddy remote->protocol = $2;
214 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
215 257add31 2020-09-09 stsp remote->mirror_references = $2;
217 0c8b29c5 2021-01-05 stsp | FETCH_ALL_BRANCHES boolean {
218 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = $2;
220 257add31 2020-09-09 stsp | PORT portplain {
221 257add31 2020-09-09 stsp remote->port = $2;
223 b8adfa55 2020-09-25 stsp | BRANCH branch {
224 b8adfa55 2020-09-25 stsp remote->branch = $2;
226 99495ddb 2021-01-10 stsp | REFERENCE ref {
227 6480c871 2021-08-30 stsp remote->fetch_ref = $2;
229 0ff2bf46 2021-08-27 tracey | FETCH {
230 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
232 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL) {
233 0ff2bf46 2021-08-27 tracey yyerror("fetch block already exists");
236 aaf30ee7 2021-08-29 stsp error = new_fetch_config(&remote->fetch_config);
237 0ff2bf46 2021-08-27 tracey if (error) {
238 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
241 f08eaca0 2021-08-30 tracey } '{' optnl fetchempty '}'
243 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
245 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL) {
246 0ff2bf46 2021-08-27 tracey yyerror("send block already exists");
249 aaf30ee7 2021-08-29 stsp error = new_send_config(&remote->send_config);
250 0ff2bf46 2021-08-27 tracey if (error) {
251 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
254 f08eaca0 2021-08-30 tracey } '{' optnl sendempty '}'
256 f08eaca0 2021-08-30 tracey fetchempty : /* empty */
257 f08eaca0 2021-08-30 tracey | fetchopts2
259 cfd92333 2021-08-27 tracey fetchopts2 : fetchopts2 fetchopts1 nl
260 abc59930 2021-09-05 naddy | fetchopts1 optnl
262 92952c0e 2021-08-30 stsp fetchopts1 : REPOSITORY STRING {
263 abc59930 2021-09-05 naddy remote->fetch_config->repository = $2;
265 abc59930 2021-09-05 naddy | SERVER STRING {
266 abc59930 2021-09-05 naddy remote->fetch_config->server = $2;
268 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
269 abc59930 2021-09-05 naddy remote->fetch_config->protocol = $2;
271 cfd92333 2021-08-27 tracey | PORT portplain {
272 aaf30ee7 2021-08-29 stsp remote->fetch_config->port = $2;
274 cfd92333 2021-08-27 tracey | BRANCH branch {
275 aaf30ee7 2021-08-29 stsp remote->fetch_config->branch = $2;
278 f08eaca0 2021-08-30 tracey sendempty : /* empty */
279 f08eaca0 2021-08-30 tracey | sendopts2
281 cfd92333 2021-08-27 tracey sendopts2 : sendopts2 sendopts1 nl
282 abc59930 2021-09-05 naddy | sendopts1 optnl
284 92952c0e 2021-08-30 stsp sendopts1 : REPOSITORY STRING {
285 abc59930 2021-09-05 naddy remote->send_config->repository = $2;
287 abc59930 2021-09-05 naddy | SERVER STRING {
288 abc59930 2021-09-05 naddy remote->send_config->server = $2;
290 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
291 abc59930 2021-09-05 naddy remote->send_config->protocol = $2;
293 cfd92333 2021-08-27 tracey | PORT portplain {
294 aaf30ee7 2021-08-29 stsp remote->send_config->port = $2;
296 cfd92333 2021-08-27 tracey | BRANCH branch {
297 aaf30ee7 2021-08-29 stsp remote->send_config->branch = $2;
300 257add31 2020-09-09 stsp remote : REMOTE STRING {
301 257add31 2020-09-09 stsp static const struct got_error* error;
303 257add31 2020-09-09 stsp error = new_remote(&remote);
304 257add31 2020-09-09 stsp if (error) {
306 257add31 2020-09-09 stsp yyerror("%s", error->msg);
309 c2d7bc3f 2021-08-31 stsp remote->name = $2;
310 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
311 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
312 257add31 2020-09-09 stsp gotconfig.nremotes++;
315 257add31 2020-09-09 stsp author : AUTHOR STRING {
316 abc59930 2021-09-05 naddy gotconfig.author = $2;
319 871bd038 2022-07-03 thomas allowed_signers : ALLOWED_SIGNERS STRING {
320 871bd038 2022-07-03 thomas gotconfig.allowed_signers_file = $2;
323 871bd038 2022-07-03 thomas revoked_signers : REVOKED_SIGNERS STRING {
324 871bd038 2022-07-03 thomas gotconfig.revoked_signers_file = $2;
327 ff5e1f09 2022-07-06 thomas signer_id : SIGNER_ID STRING {
328 ff5e1f09 2022-07-06 thomas gotconfig.signer_id = $2;
331 257add31 2020-09-09 stsp optnl : '\n' optnl
332 257add31 2020-09-09 stsp | /* empty */
334 257add31 2020-09-09 stsp nl : '\n' optnl
336 b8adfa55 2020-09-25 stsp comma : ','
337 b8adfa55 2020-09-25 stsp | /* empty */
341 257add31 2020-09-09 stsp struct keywords {
342 257add31 2020-09-09 stsp const char *k_name;
347 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
349 257add31 2020-09-09 stsp va_list ap;
351 257add31 2020-09-09 stsp char *err = NULL;
353 257add31 2020-09-09 stsp va_start(ap, fmt);
354 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
355 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
358 257add31 2020-09-09 stsp va_end(ap);
359 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
360 257add31 2020-09-09 stsp msg) == -1) {
361 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
364 c2d7bc3f 2021-08-31 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
369 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
371 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
375 257add31 2020-09-09 stsp lookup(char *s)
377 257add31 2020-09-09 stsp /* This has to be sorted always. */
378 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
379 871bd038 2022-07-03 thomas {"allowed_signers", ALLOWED_SIGNERS},
380 257add31 2020-09-09 stsp {"author", AUTHOR},
381 b8adfa55 2020-09-25 stsp {"branch", BRANCH},
382 cfd92333 2021-08-27 tracey {"fetch", FETCH},
383 25eb5847 2022-07-03 thomas {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
384 25eb5847 2022-07-03 thomas {"fetch_all_branches", FETCH_ALL_BRANCHES},
385 459c9b5d 2022-07-03 thomas {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
386 459c9b5d 2022-07-03 thomas {"mirror_references", MIRROR_REFERENCES},
387 257add31 2020-09-09 stsp {"port", PORT},
388 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
389 99495ddb 2021-01-10 stsp {"reference", REFERENCE},
390 257add31 2020-09-09 stsp {"remote", REMOTE},
391 257add31 2020-09-09 stsp {"repository", REPOSITORY},
392 871bd038 2022-07-03 thomas {"revoked_signers", REVOKED_SIGNERS},
393 cfd92333 2021-08-27 tracey {"send", SEND},
394 257add31 2020-09-09 stsp {"server", SERVER},
395 ff5e1f09 2022-07-06 thomas {"signer_id", SIGNER_ID},
397 257add31 2020-09-09 stsp const struct keywords *p;
399 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
400 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
403 257add31 2020-09-09 stsp return (p->k_val);
405 257add31 2020-09-09 stsp return (STRING);
408 257add31 2020-09-09 stsp #define START_EXPAND 1
409 257add31 2020-09-09 stsp #define DONE_EXPAND 2
411 257add31 2020-09-09 stsp static int expanding;
414 257add31 2020-09-09 stsp igetc(void)
418 257add31 2020-09-09 stsp while (1) {
419 257add31 2020-09-09 stsp if (file->ungetpos > 0)
420 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
422 257add31 2020-09-09 stsp c = getc(file->stream);
424 257add31 2020-09-09 stsp if (c == START_EXPAND)
425 257add31 2020-09-09 stsp expanding = 1;
426 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
427 257add31 2020-09-09 stsp expanding = 0;
431 257add31 2020-09-09 stsp return (c);
435 257add31 2020-09-09 stsp lgetc(int quotec)
437 257add31 2020-09-09 stsp int c, next;
439 257add31 2020-09-09 stsp if (quotec) {
440 257add31 2020-09-09 stsp c = igetc();
441 257add31 2020-09-09 stsp if (c == EOF) {
442 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
443 257add31 2020-09-09 stsp "quoted string");
445 257add31 2020-09-09 stsp return (c);
448 257add31 2020-09-09 stsp c = igetc();
449 257add31 2020-09-09 stsp while (c == '\\') {
450 257add31 2020-09-09 stsp next = igetc();
451 257add31 2020-09-09 stsp if (next != '\n') {
455 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
456 257add31 2020-09-09 stsp file->lineno++;
459 257add31 2020-09-09 stsp return (c);
463 257add31 2020-09-09 stsp lungetc(int c)
465 257add31 2020-09-09 stsp if (c == EOF)
468 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
469 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
470 257add31 2020-09-09 stsp if (p == NULL)
471 257add31 2020-09-09 stsp err(1, "%s", __func__);
472 257add31 2020-09-09 stsp file->ungetbuf = p;
473 257add31 2020-09-09 stsp file->ungetsize *= 2;
475 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
479 257add31 2020-09-09 stsp findeol(void)
483 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
484 257add31 2020-09-09 stsp while (1) {
485 257add31 2020-09-09 stsp c = lgetc(0);
486 257add31 2020-09-09 stsp if (c == '\n') {
487 257add31 2020-09-09 stsp file->lineno++;
490 257add31 2020-09-09 stsp if (c == EOF)
493 257add31 2020-09-09 stsp return (ERROR);
496 257add31 2020-09-09 stsp static long long
497 257add31 2020-09-09 stsp getservice(char *n)
499 257add31 2020-09-09 stsp struct servent *s;
500 257add31 2020-09-09 stsp u_long ulval;
502 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
503 7c84ef07 2021-08-29 stsp if (ulval == 0 || ulval > 65535) {
504 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
505 257add31 2020-09-09 stsp return (-1);
507 257add31 2020-09-09 stsp return ulval;
509 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
510 257add31 2020-09-09 stsp if (s == NULL)
511 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
512 257add31 2020-09-09 stsp if (s == NULL) {
513 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
514 257add31 2020-09-09 stsp return (-1);
516 257add31 2020-09-09 stsp return (s->s_port);
521 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
523 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
525 257add31 2020-09-09 stsp return (-1);
527 257add31 2020-09-09 stsp return (0);
532 257add31 2020-09-09 stsp yylex(void)
534 1a670123 2021-09-28 thomas char buf[8096];
535 1a670123 2021-09-28 thomas char *p, *val;
536 1a670123 2021-09-28 thomas int quotec, next, c;
537 1a670123 2021-09-28 thomas int token;
541 257add31 2020-09-09 stsp c = lgetc(0);
542 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
543 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
545 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
546 257add31 2020-09-09 stsp if (c == '#') {
547 257add31 2020-09-09 stsp c = lgetc(0);
548 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
549 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
551 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
552 257add31 2020-09-09 stsp while (1) {
553 257add31 2020-09-09 stsp c = lgetc(0);
554 257add31 2020-09-09 stsp if (c == EOF)
555 257add31 2020-09-09 stsp return (0);
557 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
558 257add31 2020-09-09 stsp yyerror("string too long");
559 257add31 2020-09-09 stsp return (findeol());
561 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
566 257add31 2020-09-09 stsp lungetc(c);
569 257add31 2020-09-09 stsp val = symget(buf);
570 257add31 2020-09-09 stsp if (val == NULL) {
571 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
572 257add31 2020-09-09 stsp return (findeol());
574 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
575 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
576 257add31 2020-09-09 stsp while (p >= val) {
577 bb27d0d1 2021-10-15 thomas lungetc((unsigned char)*p);
580 257add31 2020-09-09 stsp lungetc(START_EXPAND);
584 257add31 2020-09-09 stsp switch (c) {
587 257add31 2020-09-09 stsp quotec = c;
588 257add31 2020-09-09 stsp while (1) {
589 257add31 2020-09-09 stsp c = lgetc(quotec);
590 257add31 2020-09-09 stsp if (c == EOF)
591 257add31 2020-09-09 stsp return (0);
592 257add31 2020-09-09 stsp if (c == '\n') {
593 257add31 2020-09-09 stsp file->lineno++;
595 257add31 2020-09-09 stsp } else if (c == '\\') {
596 257add31 2020-09-09 stsp next = lgetc(quotec);
597 257add31 2020-09-09 stsp if (next == EOF)
598 257add31 2020-09-09 stsp return (0);
599 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
601 257add31 2020-09-09 stsp else if (next == '\n') {
602 257add31 2020-09-09 stsp file->lineno++;
605 257add31 2020-09-09 stsp lungetc(next);
606 257add31 2020-09-09 stsp } else if (c == quotec) {
609 257add31 2020-09-09 stsp } else if (c == '\0') {
610 257add31 2020-09-09 stsp yyerror("syntax error");
611 257add31 2020-09-09 stsp return (findeol());
613 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
614 257add31 2020-09-09 stsp yyerror("string too long");
615 257add31 2020-09-09 stsp return (findeol());
619 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
620 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
621 257add31 2020-09-09 stsp err(1, "%s", __func__);
622 257add31 2020-09-09 stsp return (STRING);
625 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
626 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
628 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
631 ccd081e7 2021-09-30 thomas if ((size_t)(p-buf) >= sizeof(buf)) {
632 257add31 2020-09-09 stsp yyerror("string too long");
633 257add31 2020-09-09 stsp return (findeol());
635 257add31 2020-09-09 stsp c = lgetc(0);
636 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
637 257add31 2020-09-09 stsp lungetc(c);
638 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
639 257add31 2020-09-09 stsp goto nodigits;
640 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
641 257add31 2020-09-09 stsp const char *errstr = NULL;
644 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
645 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
646 257add31 2020-09-09 stsp if (errstr) {
647 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
648 257add31 2020-09-09 stsp buf, errstr);
649 257add31 2020-09-09 stsp return (findeol());
651 257add31 2020-09-09 stsp return (NUMBER);
654 257add31 2020-09-09 stsp while (p > buf + 1)
655 bb27d0d1 2021-10-15 thomas lungetc((unsigned char)*--p);
656 bb27d0d1 2021-10-15 thomas c = (unsigned char)*--p;
657 257add31 2020-09-09 stsp if (c == '-')
658 257add31 2020-09-09 stsp return (c);
662 257add31 2020-09-09 stsp #define allowed_in_string(x) \
663 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
664 257add31 2020-09-09 stsp x != '{' && x != '}' && \
665 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
668 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
671 ccd081e7 2021-09-30 thomas if ((size_t)(p-buf) >= sizeof(buf)) {
672 257add31 2020-09-09 stsp yyerror("string too long");
673 257add31 2020-09-09 stsp return (findeol());
675 257add31 2020-09-09 stsp c = lgetc(0);
676 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
677 257add31 2020-09-09 stsp lungetc(c);
679 257add31 2020-09-09 stsp token = lookup(buf);
680 257add31 2020-09-09 stsp if (token == STRING) {
681 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
682 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
683 257add31 2020-09-09 stsp err(1, "%s", __func__);
685 257add31 2020-09-09 stsp return (token);
687 257add31 2020-09-09 stsp if (c == '\n') {
688 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
689 257add31 2020-09-09 stsp file->lineno++;
691 257add31 2020-09-09 stsp if (c == EOF)
692 257add31 2020-09-09 stsp return (0);
693 257add31 2020-09-09 stsp return (c);
696 257add31 2020-09-09 stsp static const struct got_error*
697 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
699 257add31 2020-09-09 stsp const struct got_error* error = NULL;
701 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
702 257add31 2020-09-09 stsp if ((*nfile) == NULL)
703 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
704 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
705 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
706 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
707 257add31 2020-09-09 stsp free((*nfile));
708 257add31 2020-09-09 stsp return error;
710 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
711 257add31 2020-09-09 stsp (*nfile)->name = filename;
712 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
713 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
714 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
715 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
716 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
717 257add31 2020-09-09 stsp fclose((*nfile)->stream);
718 257add31 2020-09-09 stsp free((*nfile));
719 257add31 2020-09-09 stsp return error;
721 257add31 2020-09-09 stsp return NULL;
724 257add31 2020-09-09 stsp static const struct got_error*
725 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
727 257add31 2020-09-09 stsp const struct got_error *error = NULL;
729 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
730 257add31 2020-09-09 stsp if (*remote == NULL)
731 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
732 cfd92333 2021-08-27 tracey return error;
735 cfd92333 2021-08-27 tracey static const struct got_error*
736 aaf30ee7 2021-08-29 stsp new_fetch_config(struct fetch_config **fetch_config)
738 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
740 aaf30ee7 2021-08-29 stsp *fetch_config = calloc(1, sizeof(**fetch_config));
741 aaf30ee7 2021-08-29 stsp if (*fetch_config == NULL)
742 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
743 cfd92333 2021-08-27 tracey return error;
746 cfd92333 2021-08-27 tracey static const struct got_error*
747 aaf30ee7 2021-08-29 stsp new_send_config(struct send_config **send_config)
749 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
751 aaf30ee7 2021-08-29 stsp *send_config = calloc(1, sizeof(**send_config));
752 aaf30ee7 2021-08-29 stsp if (*send_config == NULL)
753 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
754 257add31 2020-09-09 stsp return error;
757 257add31 2020-09-09 stsp static void
758 257add31 2020-09-09 stsp closefile(struct file *file)
760 257add31 2020-09-09 stsp fclose(file->stream);
761 257add31 2020-09-09 stsp free(file->ungetbuf);
762 257add31 2020-09-09 stsp free(file);
765 257add31 2020-09-09 stsp const struct got_error *
766 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
768 257add31 2020-09-09 stsp const struct got_error *err = NULL;
769 257add31 2020-09-09 stsp struct sym *sym, *next;
771 257add31 2020-09-09 stsp *conf = NULL;
773 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
775 257add31 2020-09-09 stsp return err;
777 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
780 257add31 2020-09-09 stsp closefile(file);
782 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
783 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
784 257add31 2020-09-09 stsp if (!sym->persist) {
785 257add31 2020-09-09 stsp free(sym->nam);
786 257add31 2020-09-09 stsp free(sym->val);
787 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
792 257add31 2020-09-09 stsp if (gerror == NULL)
793 257add31 2020-09-09 stsp *conf = &gotconfig;
794 257add31 2020-09-09 stsp return gerror;
797 aaf30ee7 2021-08-29 stsp static void
798 aaf30ee7 2021-08-29 stsp free_fetch_config(struct fetch_config *fetch_config)
800 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->repository);
801 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->server);
802 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->protocol);
803 aaf30ee7 2021-08-29 stsp free(remote->fetch_config);
806 aaf30ee7 2021-08-29 stsp static void
807 aaf30ee7 2021-08-29 stsp free_send_config(struct send_config *send_config)
809 aaf30ee7 2021-08-29 stsp free(remote->send_config->repository);
810 aaf30ee7 2021-08-29 stsp free(remote->send_config->server);
811 aaf30ee7 2021-08-29 stsp free(remote->send_config->protocol);
812 aaf30ee7 2021-08-29 stsp free(remote->send_config);
816 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
818 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
820 257add31 2020-09-09 stsp free(conf->author);
821 871bd038 2022-07-03 thomas free(conf->allowed_signers_file);
822 871bd038 2022-07-03 thomas free(conf->revoked_signers_file);
823 ff5e1f09 2022-07-06 thomas free(conf->signer_id);
824 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
825 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
826 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
827 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL)
828 aaf30ee7 2021-08-29 stsp free_fetch_config(remote->fetch_config);
829 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL)
830 aaf30ee7 2021-08-29 stsp free_send_config(remote->send_config);
831 257add31 2020-09-09 stsp free(remote->name);
832 257add31 2020-09-09 stsp free(remote->repository);
833 257add31 2020-09-09 stsp free(remote->server);
834 257add31 2020-09-09 stsp free(remote->protocol);
835 257add31 2020-09-09 stsp free(remote);
840 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
842 257add31 2020-09-09 stsp struct sym *sym;
844 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
845 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
849 257add31 2020-09-09 stsp if (sym != NULL) {
850 257add31 2020-09-09 stsp if (sym->persist == 1)
851 257add31 2020-09-09 stsp return (0);
853 257add31 2020-09-09 stsp free(sym->nam);
854 257add31 2020-09-09 stsp free(sym->val);
855 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
859 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
860 257add31 2020-09-09 stsp if (sym == NULL)
861 257add31 2020-09-09 stsp return (-1);
863 257add31 2020-09-09 stsp sym->nam = strdup(nam);
864 257add31 2020-09-09 stsp if (sym->nam == NULL) {
866 257add31 2020-09-09 stsp return (-1);
868 257add31 2020-09-09 stsp sym->val = strdup(val);
869 257add31 2020-09-09 stsp if (sym->val == NULL) {
870 257add31 2020-09-09 stsp free(sym->nam);
872 257add31 2020-09-09 stsp return (-1);
874 257add31 2020-09-09 stsp sym->used = 0;
875 257add31 2020-09-09 stsp sym->persist = persist;
876 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
877 257add31 2020-09-09 stsp return (0);
881 257add31 2020-09-09 stsp cmdline_symset(char *s)
883 257add31 2020-09-09 stsp char *sym, *val;
885 257add31 2020-09-09 stsp size_t len;
887 257add31 2020-09-09 stsp val = strrchr(s, '=');
888 257add31 2020-09-09 stsp if (val == NULL)
889 257add31 2020-09-09 stsp return (-1);
891 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
892 257add31 2020-09-09 stsp sym = malloc(len);
893 257add31 2020-09-09 stsp if (sym == NULL)
894 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
896 257add31 2020-09-09 stsp strlcpy(sym, s, len);
898 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
901 257add31 2020-09-09 stsp return (ret);
905 257add31 2020-09-09 stsp symget(const char *nam)
907 257add31 2020-09-09 stsp struct sym *sym;
909 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
910 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
911 257add31 2020-09-09 stsp sym->used = 1;
912 257add31 2020-09-09 stsp return (sym->val);
915 257add31 2020-09-09 stsp return (NULL);
919 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
921 257add31 2020-09-09 stsp u_long ulval;
925 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
926 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
927 257add31 2020-09-09 stsp return (-1);
928 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
929 257add31 2020-09-09 stsp return (-1);
930 257add31 2020-09-09 stsp *ulvalp = ulval;
931 257add31 2020-09-09 stsp return (0);