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>
26 257add31 2020-09-09 stsp #include <sys/queue.h>
28 257add31 2020-09-09 stsp #include <netdb.h>
30 257add31 2020-09-09 stsp #include <ctype.h>
31 257add31 2020-09-09 stsp #include <err.h>
32 257add31 2020-09-09 stsp #include <errno.h>
33 257add31 2020-09-09 stsp #include <limits.h>
34 257add31 2020-09-09 stsp #include <stdarg.h>
35 257add31 2020-09-09 stsp #include <stdio.h>
36 8de9818a 2020-09-14 naddy #include <stdlib.h>
37 257add31 2020-09-09 stsp #include <string.h>
39 257add31 2020-09-09 stsp #include "got_error.h"
40 257add31 2020-09-09 stsp #include "gotconfig.h"
42 257add31 2020-09-09 stsp static struct file {
43 257add31 2020-09-09 stsp FILE *stream;
44 257add31 2020-09-09 stsp const char *name;
45 257add31 2020-09-09 stsp size_t ungetpos;
46 257add31 2020-09-09 stsp size_t ungetsize;
47 257add31 2020-09-09 stsp u_char *ungetbuf;
48 257add31 2020-09-09 stsp int eof_reached;
51 257add31 2020-09-09 stsp static const struct got_error* newfile(struct file**, const char *, int *);
52 257add31 2020-09-09 stsp static void closefile(struct file *);
53 257add31 2020-09-09 stsp int yyparse(void);
54 257add31 2020-09-09 stsp int yylex(void);
55 257add31 2020-09-09 stsp int yyerror(const char *, ...)
56 257add31 2020-09-09 stsp __attribute__((__format__ (printf, 1, 2)))
57 257add31 2020-09-09 stsp __attribute__((__nonnull__ (1)));
58 257add31 2020-09-09 stsp int kw_cmp(const void *, const void *);
59 257add31 2020-09-09 stsp int lookup(char *);
60 257add31 2020-09-09 stsp int igetc(void);
61 257add31 2020-09-09 stsp int lgetc(int);
62 257add31 2020-09-09 stsp void lungetc(int);
63 257add31 2020-09-09 stsp int findeol(void);
64 257add31 2020-09-09 stsp static int parseport(char *, long long *);
66 257add31 2020-09-09 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
67 257add31 2020-09-09 stsp struct sym {
68 257add31 2020-09-09 stsp TAILQ_ENTRY(sym) entry;
70 257add31 2020-09-09 stsp int persist;
75 257add31 2020-09-09 stsp int symset(const char *, const char *, int);
76 336075a4 2022-06-25 op int cmdline_symset(char *);
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 d68f2c0e 2022-07-05 jrick %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS SIGNER_ID FETCH_ALL_BRANCHES
103 d68f2c0e 2022-07-05 jrick %token REFERENCE FETCH SEND
104 257add31 2020-09-09 stsp %token <v.string> STRING
105 257add31 2020-09-09 stsp %token <v.number> NUMBER
106 257add31 2020-09-09 stsp %type <v.number> boolean portplain
107 257add31 2020-09-09 stsp %type <v.string> numberstring
108 b8adfa55 2020-09-25 stsp %type <v.branch> branch xbranch branch_list
109 99495ddb 2021-01-10 stsp %type <v.ref> ref xref ref_list
113 257add31 2020-09-09 stsp grammar : /* empty */
114 257add31 2020-09-09 stsp | grammar '\n'
115 257add31 2020-09-09 stsp | grammar author '\n'
116 257add31 2020-09-09 stsp | grammar remote '\n'
117 4d5ee956 2022-07-02 jrick | grammar allowed_signers '\n'
118 ea2c9c36 2022-07-04 jrick | grammar revoked_signers '\n'
119 d68f2c0e 2022-07-05 jrick | grammar signer_id '\n'
121 257add31 2020-09-09 stsp boolean : STRING {
122 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
123 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
125 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
126 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
129 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
136 257add31 2020-09-09 stsp numberstring : NUMBER {
138 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
139 257add31 2020-09-09 stsp yyerror("string: asprintf");
146 257add31 2020-09-09 stsp portplain : numberstring {
147 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
154 b8adfa55 2020-09-25 stsp branch : /* empty */ { $$ = NULL; }
155 b8adfa55 2020-09-25 stsp | xbranch { $$ = $1; }
156 b8adfa55 2020-09-25 stsp | '{' optnl branch_list '}' { $$ = $3; }
158 b8adfa55 2020-09-25 stsp xbranch : STRING {
159 b8adfa55 2020-09-25 stsp $$ = calloc(1, sizeof(struct node_branch));
160 b8adfa55 2020-09-25 stsp if ($$ == NULL) {
161 b8adfa55 2020-09-25 stsp yyerror("calloc");
164 b8adfa55 2020-09-25 stsp $$->branch_name = $1;
165 b8adfa55 2020-09-25 stsp $$->tail = $$;
168 b8adfa55 2020-09-25 stsp branch_list : xbranch optnl { $$ = $1; }
169 b8adfa55 2020-09-25 stsp | branch_list comma xbranch optnl {
170 b8adfa55 2020-09-25 stsp $1->tail->next = $3;
171 b8adfa55 2020-09-25 stsp $1->tail = $3;
175 99495ddb 2021-01-10 stsp ref : /* empty */ { $$ = NULL; }
176 99495ddb 2021-01-10 stsp | xref { $$ = $1; }
177 99495ddb 2021-01-10 stsp | '{' optnl ref_list '}' { $$ = $3; }
179 99495ddb 2021-01-10 stsp xref : STRING {
180 99495ddb 2021-01-10 stsp $$ = calloc(1, sizeof(struct node_ref));
181 99495ddb 2021-01-10 stsp if ($$ == NULL) {
182 99495ddb 2021-01-10 stsp yyerror("calloc");
185 99495ddb 2021-01-10 stsp $$->ref_name = $1;
186 99495ddb 2021-01-10 stsp $$->tail = $$;
189 99495ddb 2021-01-10 stsp ref_list : xref optnl { $$ = $1; }
190 99495ddb 2021-01-10 stsp | ref_list comma xref optnl {
191 99495ddb 2021-01-10 stsp $1->tail->next = $3;
192 99495ddb 2021-01-10 stsp $1->tail = $3;
196 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
197 abc59930 2021-09-05 naddy | remoteopts1 optnl
199 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
200 abc59930 2021-09-05 naddy remote->repository = $2;
202 abc59930 2021-09-05 naddy | SERVER STRING {
203 abc59930 2021-09-05 naddy remote->server = $2;
205 257add31 2020-09-09 stsp | PROTOCOL STRING {
206 abc59930 2021-09-05 naddy remote->protocol = $2;
208 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
209 257add31 2020-09-09 stsp remote->mirror_references = $2;
211 0c8b29c5 2021-01-05 stsp | FETCH_ALL_BRANCHES boolean {
212 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = $2;
214 257add31 2020-09-09 stsp | PORT portplain {
215 257add31 2020-09-09 stsp remote->port = $2;
217 b8adfa55 2020-09-25 stsp | BRANCH branch {
218 b8adfa55 2020-09-25 stsp remote->branch = $2;
220 99495ddb 2021-01-10 stsp | REFERENCE ref {
221 6480c871 2021-08-30 stsp remote->fetch_ref = $2;
223 0ff2bf46 2021-08-27 tracey | FETCH {
224 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
226 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL) {
227 0ff2bf46 2021-08-27 tracey yyerror("fetch block already exists");
230 aaf30ee7 2021-08-29 stsp error = new_fetch_config(&remote->fetch_config);
231 0ff2bf46 2021-08-27 tracey if (error) {
232 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
235 f08eaca0 2021-08-30 tracey } '{' optnl fetchempty '}'
237 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
239 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL) {
240 0ff2bf46 2021-08-27 tracey yyerror("send block already exists");
243 aaf30ee7 2021-08-29 stsp error = new_send_config(&remote->send_config);
244 0ff2bf46 2021-08-27 tracey if (error) {
245 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
248 f08eaca0 2021-08-30 tracey } '{' optnl sendempty '}'
250 f08eaca0 2021-08-30 tracey fetchempty : /* empty */
251 f08eaca0 2021-08-30 tracey | fetchopts2
253 cfd92333 2021-08-27 tracey fetchopts2 : fetchopts2 fetchopts1 nl
254 abc59930 2021-09-05 naddy | fetchopts1 optnl
256 92952c0e 2021-08-30 stsp fetchopts1 : REPOSITORY STRING {
257 abc59930 2021-09-05 naddy remote->fetch_config->repository = $2;
259 abc59930 2021-09-05 naddy | SERVER STRING {
260 abc59930 2021-09-05 naddy remote->fetch_config->server = $2;
262 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
263 abc59930 2021-09-05 naddy remote->fetch_config->protocol = $2;
265 cfd92333 2021-08-27 tracey | PORT portplain {
266 aaf30ee7 2021-08-29 stsp remote->fetch_config->port = $2;
268 cfd92333 2021-08-27 tracey | BRANCH branch {
269 aaf30ee7 2021-08-29 stsp remote->fetch_config->branch = $2;
272 f08eaca0 2021-08-30 tracey sendempty : /* empty */
273 f08eaca0 2021-08-30 tracey | sendopts2
275 cfd92333 2021-08-27 tracey sendopts2 : sendopts2 sendopts1 nl
276 abc59930 2021-09-05 naddy | sendopts1 optnl
278 92952c0e 2021-08-30 stsp sendopts1 : REPOSITORY STRING {
279 abc59930 2021-09-05 naddy remote->send_config->repository = $2;
281 abc59930 2021-09-05 naddy | SERVER STRING {
282 abc59930 2021-09-05 naddy remote->send_config->server = $2;
284 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
285 abc59930 2021-09-05 naddy remote->send_config->protocol = $2;
287 cfd92333 2021-08-27 tracey | PORT portplain {
288 aaf30ee7 2021-08-29 stsp remote->send_config->port = $2;
290 cfd92333 2021-08-27 tracey | BRANCH branch {
291 aaf30ee7 2021-08-29 stsp remote->send_config->branch = $2;
294 257add31 2020-09-09 stsp remote : REMOTE STRING {
295 257add31 2020-09-09 stsp static const struct got_error* error;
297 257add31 2020-09-09 stsp error = new_remote(&remote);
298 257add31 2020-09-09 stsp if (error) {
300 257add31 2020-09-09 stsp yyerror("%s", error->msg);
303 c2d7bc3f 2021-08-31 stsp remote->name = $2;
304 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
305 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
306 257add31 2020-09-09 stsp gotconfig.nremotes++;
309 257add31 2020-09-09 stsp author : AUTHOR STRING {
310 abc59930 2021-09-05 naddy gotconfig.author = $2;
313 4d5ee956 2022-07-02 jrick allowed_signers : ALLOWED_SIGNERS STRING {
314 4d5ee956 2022-07-02 jrick gotconfig.allowed_signers_file = $2;
317 4d5ee956 2022-07-02 jrick revoked_signers : REVOKED_SIGNERS STRING {
318 4d5ee956 2022-07-02 jrick gotconfig.revoked_signers_file = $2;
321 d68f2c0e 2022-07-05 jrick signer_id : SIGNER_ID STRING {
322 d68f2c0e 2022-07-05 jrick gotconfig.signer_id = $2;
325 257add31 2020-09-09 stsp optnl : '\n' optnl
326 257add31 2020-09-09 stsp | /* empty */
328 257add31 2020-09-09 stsp nl : '\n' optnl
330 b8adfa55 2020-09-25 stsp comma : ','
331 b8adfa55 2020-09-25 stsp | /* empty */
335 257add31 2020-09-09 stsp struct keywords {
336 257add31 2020-09-09 stsp const char *k_name;
341 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
343 257add31 2020-09-09 stsp va_list ap;
345 257add31 2020-09-09 stsp char *err = NULL;
347 257add31 2020-09-09 stsp va_start(ap, fmt);
348 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
349 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
352 257add31 2020-09-09 stsp va_end(ap);
353 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
354 257add31 2020-09-09 stsp msg) == -1) {
355 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
358 c2d7bc3f 2021-08-31 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
363 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
365 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
369 257add31 2020-09-09 stsp lookup(char *s)
371 257add31 2020-09-09 stsp /* This has to be sorted always. */
372 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
373 4d5ee956 2022-07-02 jrick {"allowed_signers", ALLOWED_SIGNERS},
374 257add31 2020-09-09 stsp {"author", AUTHOR},
375 b8adfa55 2020-09-25 stsp {"branch", BRANCH},
376 cfd92333 2021-08-27 tracey {"fetch", FETCH},
377 f1bf60d1 2022-07-03 stsp {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
378 f1bf60d1 2022-07-03 stsp {"fetch_all_branches", FETCH_ALL_BRANCHES},
379 26e6f38e 2022-07-03 stsp {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
380 26e6f38e 2022-07-03 stsp {"mirror_references", MIRROR_REFERENCES},
381 257add31 2020-09-09 stsp {"port", PORT},
382 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
383 99495ddb 2021-01-10 stsp {"reference", REFERENCE},
384 257add31 2020-09-09 stsp {"remote", REMOTE},
385 257add31 2020-09-09 stsp {"repository", REPOSITORY},
386 4d5ee956 2022-07-02 jrick {"revoked_signers", REVOKED_SIGNERS},
387 cfd92333 2021-08-27 tracey {"send", SEND},
388 257add31 2020-09-09 stsp {"server", SERVER},
389 d68f2c0e 2022-07-05 jrick {"signer_id", SIGNER_ID},
391 257add31 2020-09-09 stsp const struct keywords *p;
393 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
394 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
397 257add31 2020-09-09 stsp return (p->k_val);
399 257add31 2020-09-09 stsp return (STRING);
402 257add31 2020-09-09 stsp #define START_EXPAND 1
403 257add31 2020-09-09 stsp #define DONE_EXPAND 2
405 257add31 2020-09-09 stsp static int expanding;
408 257add31 2020-09-09 stsp igetc(void)
412 257add31 2020-09-09 stsp while (1) {
413 257add31 2020-09-09 stsp if (file->ungetpos > 0)
414 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
416 257add31 2020-09-09 stsp c = getc(file->stream);
418 257add31 2020-09-09 stsp if (c == START_EXPAND)
419 257add31 2020-09-09 stsp expanding = 1;
420 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
421 257add31 2020-09-09 stsp expanding = 0;
425 257add31 2020-09-09 stsp return (c);
429 257add31 2020-09-09 stsp lgetc(int quotec)
431 257add31 2020-09-09 stsp int c, next;
433 257add31 2020-09-09 stsp if (quotec) {
434 257add31 2020-09-09 stsp c = igetc();
435 257add31 2020-09-09 stsp if (c == EOF) {
436 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
437 257add31 2020-09-09 stsp "quoted string");
439 257add31 2020-09-09 stsp return (c);
442 257add31 2020-09-09 stsp c = igetc();
443 257add31 2020-09-09 stsp while (c == '\\') {
444 257add31 2020-09-09 stsp next = igetc();
445 257add31 2020-09-09 stsp if (next != '\n') {
449 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
450 257add31 2020-09-09 stsp file->lineno++;
453 257add31 2020-09-09 stsp return (c);
457 257add31 2020-09-09 stsp lungetc(int c)
459 257add31 2020-09-09 stsp if (c == EOF)
462 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
463 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
464 257add31 2020-09-09 stsp if (p == NULL)
465 257add31 2020-09-09 stsp err(1, "%s", __func__);
466 257add31 2020-09-09 stsp file->ungetbuf = p;
467 257add31 2020-09-09 stsp file->ungetsize *= 2;
469 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
473 257add31 2020-09-09 stsp findeol(void)
477 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
478 257add31 2020-09-09 stsp while (1) {
479 257add31 2020-09-09 stsp c = lgetc(0);
480 257add31 2020-09-09 stsp if (c == '\n') {
481 257add31 2020-09-09 stsp file->lineno++;
484 257add31 2020-09-09 stsp if (c == EOF)
487 257add31 2020-09-09 stsp return (ERROR);
490 257add31 2020-09-09 stsp static long long
491 257add31 2020-09-09 stsp getservice(char *n)
493 257add31 2020-09-09 stsp struct servent *s;
494 257add31 2020-09-09 stsp u_long ulval;
496 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
497 7c84ef07 2021-08-29 stsp if (ulval == 0 || ulval > 65535) {
498 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
499 257add31 2020-09-09 stsp return (-1);
501 257add31 2020-09-09 stsp return ulval;
503 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
504 257add31 2020-09-09 stsp if (s == NULL)
505 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
506 257add31 2020-09-09 stsp if (s == NULL) {
507 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
508 257add31 2020-09-09 stsp return (-1);
510 257add31 2020-09-09 stsp return (s->s_port);
515 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
517 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
519 257add31 2020-09-09 stsp return (-1);
521 257add31 2020-09-09 stsp return (0);
526 257add31 2020-09-09 stsp yylex(void)
528 14af9299 2021-09-28 naddy char buf[8096];
529 14af9299 2021-09-28 naddy char *p, *val;
530 14af9299 2021-09-28 naddy int quotec, next, c;
531 14af9299 2021-09-28 naddy int token;
535 257add31 2020-09-09 stsp c = lgetc(0);
536 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
537 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
539 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
540 257add31 2020-09-09 stsp if (c == '#') {
541 257add31 2020-09-09 stsp c = lgetc(0);
542 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
543 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
545 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
546 257add31 2020-09-09 stsp while (1) {
547 257add31 2020-09-09 stsp c = lgetc(0);
548 257add31 2020-09-09 stsp if (c == EOF)
549 257add31 2020-09-09 stsp return (0);
551 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
552 257add31 2020-09-09 stsp yyerror("string too long");
553 257add31 2020-09-09 stsp return (findeol());
555 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
560 257add31 2020-09-09 stsp lungetc(c);
563 257add31 2020-09-09 stsp val = symget(buf);
564 257add31 2020-09-09 stsp if (val == NULL) {
565 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
566 257add31 2020-09-09 stsp return (findeol());
568 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
569 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
570 257add31 2020-09-09 stsp while (p >= val) {
571 0c82d267 2021-10-15 naddy lungetc((unsigned char)*p);
574 257add31 2020-09-09 stsp lungetc(START_EXPAND);
578 257add31 2020-09-09 stsp switch (c) {
581 257add31 2020-09-09 stsp quotec = c;
582 257add31 2020-09-09 stsp while (1) {
583 257add31 2020-09-09 stsp c = lgetc(quotec);
584 257add31 2020-09-09 stsp if (c == EOF)
585 257add31 2020-09-09 stsp return (0);
586 257add31 2020-09-09 stsp if (c == '\n') {
587 257add31 2020-09-09 stsp file->lineno++;
589 257add31 2020-09-09 stsp } else if (c == '\\') {
590 257add31 2020-09-09 stsp next = lgetc(quotec);
591 257add31 2020-09-09 stsp if (next == EOF)
592 257add31 2020-09-09 stsp return (0);
593 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
595 257add31 2020-09-09 stsp else if (next == '\n') {
596 257add31 2020-09-09 stsp file->lineno++;
599 257add31 2020-09-09 stsp lungetc(next);
600 257add31 2020-09-09 stsp } else if (c == quotec) {
603 257add31 2020-09-09 stsp } else if (c == '\0') {
604 257add31 2020-09-09 stsp yyerror("syntax error");
605 257add31 2020-09-09 stsp return (findeol());
607 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
608 257add31 2020-09-09 stsp yyerror("string too long");
609 257add31 2020-09-09 stsp return (findeol());
613 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
614 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
615 257add31 2020-09-09 stsp err(1, "%s", __func__);
616 257add31 2020-09-09 stsp return (STRING);
619 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
620 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
622 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
625 5d8cbca3 2021-09-29 naddy if ((size_t)(p-buf) >= sizeof(buf)) {
626 257add31 2020-09-09 stsp yyerror("string too long");
627 257add31 2020-09-09 stsp return (findeol());
629 257add31 2020-09-09 stsp c = lgetc(0);
630 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
631 257add31 2020-09-09 stsp lungetc(c);
632 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
633 257add31 2020-09-09 stsp goto nodigits;
634 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
635 257add31 2020-09-09 stsp const char *errstr = NULL;
638 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
639 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
640 257add31 2020-09-09 stsp if (errstr) {
641 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
642 257add31 2020-09-09 stsp buf, errstr);
643 257add31 2020-09-09 stsp return (findeol());
645 257add31 2020-09-09 stsp return (NUMBER);
648 257add31 2020-09-09 stsp while (p > buf + 1)
649 0c82d267 2021-10-15 naddy lungetc((unsigned char)*--p);
650 0c82d267 2021-10-15 naddy c = (unsigned char)*--p;
651 257add31 2020-09-09 stsp if (c == '-')
652 257add31 2020-09-09 stsp return (c);
656 257add31 2020-09-09 stsp #define allowed_in_string(x) \
657 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
658 257add31 2020-09-09 stsp x != '{' && x != '}' && \
659 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
662 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
665 5d8cbca3 2021-09-29 naddy if ((size_t)(p-buf) >= sizeof(buf)) {
666 257add31 2020-09-09 stsp yyerror("string too long");
667 257add31 2020-09-09 stsp return (findeol());
669 257add31 2020-09-09 stsp c = lgetc(0);
670 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
671 257add31 2020-09-09 stsp lungetc(c);
673 257add31 2020-09-09 stsp token = lookup(buf);
674 257add31 2020-09-09 stsp if (token == STRING) {
675 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
676 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
677 257add31 2020-09-09 stsp err(1, "%s", __func__);
679 257add31 2020-09-09 stsp return (token);
681 257add31 2020-09-09 stsp if (c == '\n') {
682 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
683 257add31 2020-09-09 stsp file->lineno++;
685 257add31 2020-09-09 stsp if (c == EOF)
686 257add31 2020-09-09 stsp return (0);
687 257add31 2020-09-09 stsp return (c);
690 257add31 2020-09-09 stsp static const struct got_error*
691 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
693 257add31 2020-09-09 stsp const struct got_error* error = NULL;
695 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
696 257add31 2020-09-09 stsp if ((*nfile) == NULL)
697 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
698 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
699 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
700 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
701 257add31 2020-09-09 stsp free((*nfile));
702 257add31 2020-09-09 stsp return error;
704 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
705 257add31 2020-09-09 stsp (*nfile)->name = filename;
706 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
707 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
708 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
709 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
710 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
711 257add31 2020-09-09 stsp fclose((*nfile)->stream);
712 257add31 2020-09-09 stsp free((*nfile));
713 257add31 2020-09-09 stsp return error;
715 257add31 2020-09-09 stsp return NULL;
718 257add31 2020-09-09 stsp static const struct got_error*
719 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
721 257add31 2020-09-09 stsp const struct got_error *error = NULL;
723 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
724 257add31 2020-09-09 stsp if (*remote == NULL)
725 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
726 cfd92333 2021-08-27 tracey return error;
729 cfd92333 2021-08-27 tracey static const struct got_error*
730 aaf30ee7 2021-08-29 stsp new_fetch_config(struct fetch_config **fetch_config)
732 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
734 aaf30ee7 2021-08-29 stsp *fetch_config = calloc(1, sizeof(**fetch_config));
735 aaf30ee7 2021-08-29 stsp if (*fetch_config == NULL)
736 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
737 cfd92333 2021-08-27 tracey return error;
740 cfd92333 2021-08-27 tracey static const struct got_error*
741 aaf30ee7 2021-08-29 stsp new_send_config(struct send_config **send_config)
743 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
745 aaf30ee7 2021-08-29 stsp *send_config = calloc(1, sizeof(**send_config));
746 aaf30ee7 2021-08-29 stsp if (*send_config == NULL)
747 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
748 257add31 2020-09-09 stsp return error;
751 257add31 2020-09-09 stsp static void
752 257add31 2020-09-09 stsp closefile(struct file *file)
754 257add31 2020-09-09 stsp fclose(file->stream);
755 257add31 2020-09-09 stsp free(file->ungetbuf);
756 257add31 2020-09-09 stsp free(file);
759 257add31 2020-09-09 stsp const struct got_error *
760 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
762 257add31 2020-09-09 stsp const struct got_error *err = NULL;
763 257add31 2020-09-09 stsp struct sym *sym, *next;
765 257add31 2020-09-09 stsp *conf = NULL;
767 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
769 257add31 2020-09-09 stsp return err;
771 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
774 257add31 2020-09-09 stsp closefile(file);
776 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
777 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
778 257add31 2020-09-09 stsp if (!sym->persist) {
779 257add31 2020-09-09 stsp free(sym->nam);
780 257add31 2020-09-09 stsp free(sym->val);
781 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
786 257add31 2020-09-09 stsp if (gerror == NULL)
787 257add31 2020-09-09 stsp *conf = &gotconfig;
788 257add31 2020-09-09 stsp return gerror;
791 aaf30ee7 2021-08-29 stsp static void
792 aaf30ee7 2021-08-29 stsp free_fetch_config(struct fetch_config *fetch_config)
794 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->repository);
795 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->server);
796 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->protocol);
797 aaf30ee7 2021-08-29 stsp free(remote->fetch_config);
800 aaf30ee7 2021-08-29 stsp static void
801 aaf30ee7 2021-08-29 stsp free_send_config(struct send_config *send_config)
803 aaf30ee7 2021-08-29 stsp free(remote->send_config->repository);
804 aaf30ee7 2021-08-29 stsp free(remote->send_config->server);
805 aaf30ee7 2021-08-29 stsp free(remote->send_config->protocol);
806 aaf30ee7 2021-08-29 stsp free(remote->send_config);
810 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
812 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
814 257add31 2020-09-09 stsp free(conf->author);
815 4d5ee956 2022-07-02 jrick free(conf->allowed_signers_file);
816 4d5ee956 2022-07-02 jrick free(conf->revoked_signers_file);
817 d68f2c0e 2022-07-05 jrick free(conf->signer_id);
818 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
819 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
820 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
821 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL)
822 aaf30ee7 2021-08-29 stsp free_fetch_config(remote->fetch_config);
823 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL)
824 aaf30ee7 2021-08-29 stsp free_send_config(remote->send_config);
825 257add31 2020-09-09 stsp free(remote->name);
826 257add31 2020-09-09 stsp free(remote->repository);
827 257add31 2020-09-09 stsp free(remote->server);
828 257add31 2020-09-09 stsp free(remote->protocol);
829 257add31 2020-09-09 stsp free(remote);
834 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
836 257add31 2020-09-09 stsp struct sym *sym;
838 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
839 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
843 257add31 2020-09-09 stsp if (sym != NULL) {
844 257add31 2020-09-09 stsp if (sym->persist == 1)
845 257add31 2020-09-09 stsp return (0);
847 257add31 2020-09-09 stsp free(sym->nam);
848 257add31 2020-09-09 stsp free(sym->val);
849 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
853 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
854 257add31 2020-09-09 stsp if (sym == NULL)
855 257add31 2020-09-09 stsp return (-1);
857 257add31 2020-09-09 stsp sym->nam = strdup(nam);
858 257add31 2020-09-09 stsp if (sym->nam == NULL) {
860 257add31 2020-09-09 stsp return (-1);
862 257add31 2020-09-09 stsp sym->val = strdup(val);
863 257add31 2020-09-09 stsp if (sym->val == NULL) {
864 257add31 2020-09-09 stsp free(sym->nam);
866 257add31 2020-09-09 stsp return (-1);
868 257add31 2020-09-09 stsp sym->used = 0;
869 257add31 2020-09-09 stsp sym->persist = persist;
870 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
871 257add31 2020-09-09 stsp return (0);
875 257add31 2020-09-09 stsp cmdline_symset(char *s)
877 257add31 2020-09-09 stsp char *sym, *val;
879 257add31 2020-09-09 stsp size_t len;
881 257add31 2020-09-09 stsp val = strrchr(s, '=');
882 257add31 2020-09-09 stsp if (val == NULL)
883 257add31 2020-09-09 stsp return (-1);
885 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
886 257add31 2020-09-09 stsp sym = malloc(len);
887 257add31 2020-09-09 stsp if (sym == NULL)
888 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
890 257add31 2020-09-09 stsp strlcpy(sym, s, len);
892 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
895 257add31 2020-09-09 stsp return (ret);
899 257add31 2020-09-09 stsp symget(const char *nam)
901 257add31 2020-09-09 stsp struct sym *sym;
903 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
904 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
905 257add31 2020-09-09 stsp sym->used = 1;
906 257add31 2020-09-09 stsp return (sym->val);
909 257add31 2020-09-09 stsp return (NULL);
913 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
915 257add31 2020-09-09 stsp u_long ulval;
919 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
920 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
921 257add31 2020-09-09 stsp return (-1);
922 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
923 257add31 2020-09-09 stsp return (-1);
924 257add31 2020-09-09 stsp *ulvalp = ulval;
925 257add31 2020-09-09 stsp return (0);