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 257add31 2020-09-09 stsp char *symget(const char *);
78 257add31 2020-09-09 stsp static int atoul(char *, u_long *);
80 257add31 2020-09-09 stsp static const struct got_error* gerror;
81 257add31 2020-09-09 stsp static struct gotconfig_remote_repo *remote;
82 257add31 2020-09-09 stsp static struct gotconfig gotconfig;
83 257add31 2020-09-09 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
84 cfd92333 2021-08-27 tracey static const struct got_error* new_fetch(struct fetch_repo **);
85 cfd92333 2021-08-27 tracey static const struct got_error* new_send(struct send_repo **);
87 257add31 2020-09-09 stsp typedef struct {
89 1367695b 2020-09-26 naddy long long number;
90 257add31 2020-09-09 stsp char *string;
91 b8adfa55 2020-09-25 stsp struct node_branch *branch;
92 99495ddb 2021-01-10 stsp struct node_ref *ref;
99 257add31 2020-09-09 stsp %token ERROR
100 b8adfa55 2020-09-25 stsp %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
101 cfd92333 2021-08-27 tracey %token AUTHOR FETCH_ALL_BRANCHES REFERENCE FETCH SEND
102 257add31 2020-09-09 stsp %token <v.string> STRING
103 257add31 2020-09-09 stsp %token <v.number> NUMBER
104 257add31 2020-09-09 stsp %type <v.number> boolean portplain
105 257add31 2020-09-09 stsp %type <v.string> numberstring
106 b8adfa55 2020-09-25 stsp %type <v.branch> branch xbranch branch_list
107 99495ddb 2021-01-10 stsp %type <v.ref> ref xref ref_list
111 257add31 2020-09-09 stsp grammar : /* empty */
112 257add31 2020-09-09 stsp | grammar '\n'
113 257add31 2020-09-09 stsp | grammar author '\n'
114 257add31 2020-09-09 stsp | grammar remote '\n'
116 257add31 2020-09-09 stsp boolean : STRING {
117 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
118 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
120 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
121 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
124 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
131 257add31 2020-09-09 stsp numberstring : NUMBER {
133 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
134 257add31 2020-09-09 stsp yyerror("string: asprintf");
141 257add31 2020-09-09 stsp portplain : numberstring {
142 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
149 b8adfa55 2020-09-25 stsp branch : /* empty */ { $$ = NULL; }
150 b8adfa55 2020-09-25 stsp | xbranch { $$ = $1; }
151 b8adfa55 2020-09-25 stsp | '{' optnl branch_list '}' { $$ = $3; }
153 b8adfa55 2020-09-25 stsp xbranch : STRING {
154 b8adfa55 2020-09-25 stsp $$ = calloc(1, sizeof(struct node_branch));
155 b8adfa55 2020-09-25 stsp if ($$ == NULL) {
156 b8adfa55 2020-09-25 stsp yyerror("calloc");
159 b8adfa55 2020-09-25 stsp $$->branch_name = $1;
160 b8adfa55 2020-09-25 stsp $$->tail = $$;
163 b8adfa55 2020-09-25 stsp branch_list : xbranch optnl { $$ = $1; }
164 b8adfa55 2020-09-25 stsp | branch_list comma xbranch optnl {
165 b8adfa55 2020-09-25 stsp $1->tail->next = $3;
166 b8adfa55 2020-09-25 stsp $1->tail = $3;
170 99495ddb 2021-01-10 stsp ref : /* empty */ { $$ = NULL; }
171 99495ddb 2021-01-10 stsp | xref { $$ = $1; }
172 99495ddb 2021-01-10 stsp | '{' optnl ref_list '}' { $$ = $3; }
174 99495ddb 2021-01-10 stsp xref : STRING {
175 99495ddb 2021-01-10 stsp $$ = calloc(1, sizeof(struct node_ref));
176 99495ddb 2021-01-10 stsp if ($$ == NULL) {
177 99495ddb 2021-01-10 stsp yyerror("calloc");
180 99495ddb 2021-01-10 stsp $$->ref_name = $1;
181 99495ddb 2021-01-10 stsp $$->tail = $$;
184 99495ddb 2021-01-10 stsp ref_list : xref optnl { $$ = $1; }
185 99495ddb 2021-01-10 stsp | ref_list comma xref optnl {
186 99495ddb 2021-01-10 stsp $1->tail->next = $3;
187 99495ddb 2021-01-10 stsp $1->tail = $3;
191 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
192 257add31 2020-09-09 stsp | remoteopts1 optnl
194 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
195 257add31 2020-09-09 stsp remote->repository = strdup($2);
196 257add31 2020-09-09 stsp if (remote->repository == NULL) {
198 257add31 2020-09-09 stsp yyerror("strdup");
203 257add31 2020-09-09 stsp | SERVER STRING {
204 257add31 2020-09-09 stsp remote->server = strdup($2);
205 257add31 2020-09-09 stsp if (remote->server == NULL) {
207 257add31 2020-09-09 stsp yyerror("strdup");
212 257add31 2020-09-09 stsp | PROTOCOL STRING {
213 257add31 2020-09-09 stsp remote->protocol = strdup($2);
214 257add31 2020-09-09 stsp if (remote->protocol == NULL) {
216 257add31 2020-09-09 stsp yyerror("strdup");
221 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
222 257add31 2020-09-09 stsp remote->mirror_references = $2;
224 0c8b29c5 2021-01-05 stsp | FETCH_ALL_BRANCHES boolean {
225 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = $2;
227 257add31 2020-09-09 stsp | PORT portplain {
228 257add31 2020-09-09 stsp remote->port = $2;
230 b8adfa55 2020-09-25 stsp | BRANCH branch {
231 b8adfa55 2020-09-25 stsp remote->branch = $2;
233 99495ddb 2021-01-10 stsp | REFERENCE ref {
234 99495ddb 2021-01-10 stsp remote->ref = $2;
236 0ff2bf46 2021-08-27 tracey | FETCH {
237 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
239 0ff2bf46 2021-08-27 tracey if (remote->fetch_repo != NULL) {
240 0ff2bf46 2021-08-27 tracey yyerror("fetch block already exists");
243 0ff2bf46 2021-08-27 tracey error = new_fetch(&remote->fetch_repo);
244 0ff2bf46 2021-08-27 tracey if (error) {
245 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
248 0ff2bf46 2021-08-27 tracey } '{' optnl fetchopts2 '}'
250 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
252 0ff2bf46 2021-08-27 tracey if (remote->send_repo != NULL) {
253 0ff2bf46 2021-08-27 tracey yyerror("send block already exists");
256 0ff2bf46 2021-08-27 tracey error = new_send(&remote->send_repo);
257 0ff2bf46 2021-08-27 tracey if (error) {
258 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
261 0ff2bf46 2021-08-27 tracey } '{' optnl sendopts2 '}'
263 cfd92333 2021-08-27 tracey fetchopts2 : fetchopts2 fetchopts1 nl
264 cfd92333 2021-08-27 tracey | fetchopts1 optnl
266 16c4be8c 2021-08-27 tracey fetchopts1 : /* empty */
267 16c4be8c 2021-08-27 tracey | REPOSITORY STRING {
268 cfd92333 2021-08-27 tracey remote->fetch_repo->fetch_repository = strdup($2);
269 cfd92333 2021-08-27 tracey if (remote->fetch_repo->fetch_repository == NULL) {
270 cfd92333 2021-08-27 tracey free($2);
271 cfd92333 2021-08-27 tracey yyerror("strdup");
274 cfd92333 2021-08-27 tracey free($2);
276 cfd92333 2021-08-27 tracey | SERVER STRING {
277 cfd92333 2021-08-27 tracey remote->fetch_repo->fetch_server = strdup($2);
278 cfd92333 2021-08-27 tracey if (remote->fetch_repo->fetch_server == NULL) {
279 cfd92333 2021-08-27 tracey free($2);
280 cfd92333 2021-08-27 tracey yyerror("strdup");
283 cfd92333 2021-08-27 tracey free($2);
285 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
286 cfd92333 2021-08-27 tracey remote->fetch_repo->fetch_protocol = strdup($2);
287 cfd92333 2021-08-27 tracey if (remote->fetch_repo->fetch_protocol == NULL) {
288 cfd92333 2021-08-27 tracey free($2);
289 cfd92333 2021-08-27 tracey yyerror("strdup");
292 cfd92333 2021-08-27 tracey free($2);
294 cfd92333 2021-08-27 tracey | PORT portplain {
295 cfd92333 2021-08-27 tracey remote->fetch_repo->fetch_port = $2;
297 cfd92333 2021-08-27 tracey | BRANCH branch {
298 cfd92333 2021-08-27 tracey remote->fetch_repo->fetch_branch = $2;
301 cfd92333 2021-08-27 tracey sendopts2 : sendopts2 sendopts1 nl
302 cfd92333 2021-08-27 tracey | sendopts1 optnl
304 16c4be8c 2021-08-27 tracey sendopts1 : /* empty */
305 16c4be8c 2021-08-27 tracey | REPOSITORY STRING {
306 cfd92333 2021-08-27 tracey remote->send_repo->send_repository = strdup($2);
307 cfd92333 2021-08-27 tracey if (remote->send_repo->send_repository == NULL) {
308 cfd92333 2021-08-27 tracey free($2);
309 cfd92333 2021-08-27 tracey yyerror("strdup");
312 cfd92333 2021-08-27 tracey free($2);
314 cfd92333 2021-08-27 tracey | SERVER STRING {
315 cfd92333 2021-08-27 tracey remote->send_repo->send_server = strdup($2);
316 cfd92333 2021-08-27 tracey if (remote->send_repo->send_server == NULL) {
317 cfd92333 2021-08-27 tracey free($2);
318 cfd92333 2021-08-27 tracey yyerror("strdup");
321 cfd92333 2021-08-27 tracey free($2);
323 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
324 cfd92333 2021-08-27 tracey remote->send_repo->send_protocol = strdup($2);
325 cfd92333 2021-08-27 tracey if (remote->send_repo->send_protocol == NULL) {
326 cfd92333 2021-08-27 tracey free($2);
327 cfd92333 2021-08-27 tracey yyerror("strdup");
330 cfd92333 2021-08-27 tracey free($2);
332 cfd92333 2021-08-27 tracey | PORT portplain {
333 cfd92333 2021-08-27 tracey remote->send_repo->send_port = $2;
335 cfd92333 2021-08-27 tracey | BRANCH branch {
336 cfd92333 2021-08-27 tracey remote->send_repo->send_branch = $2;
339 257add31 2020-09-09 stsp remote : REMOTE STRING {
340 257add31 2020-09-09 stsp static const struct got_error* error;
342 257add31 2020-09-09 stsp error = new_remote(&remote);
343 257add31 2020-09-09 stsp if (error) {
345 257add31 2020-09-09 stsp yyerror("%s", error->msg);
348 257add31 2020-09-09 stsp remote->name = strdup($2);
349 257add31 2020-09-09 stsp if (remote->name == NULL) {
351 257add31 2020-09-09 stsp yyerror("strdup");
355 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
356 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
357 257add31 2020-09-09 stsp gotconfig.nremotes++;
360 257add31 2020-09-09 stsp author : AUTHOR STRING {
361 257add31 2020-09-09 stsp gotconfig.author = strdup($2);
362 257add31 2020-09-09 stsp if (gotconfig.author == NULL) {
364 257add31 2020-09-09 stsp yyerror("strdup");
370 257add31 2020-09-09 stsp optnl : '\n' optnl
371 257add31 2020-09-09 stsp | /* empty */
373 257add31 2020-09-09 stsp nl : '\n' optnl
375 b8adfa55 2020-09-25 stsp comma : ','
376 b8adfa55 2020-09-25 stsp | /* empty */
380 257add31 2020-09-09 stsp struct keywords {
381 257add31 2020-09-09 stsp const char *k_name;
386 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
388 257add31 2020-09-09 stsp va_list ap;
390 257add31 2020-09-09 stsp char *err = NULL;
392 257add31 2020-09-09 stsp va_start(ap, fmt);
393 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
394 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
397 257add31 2020-09-09 stsp va_end(ap);
398 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
399 257add31 2020-09-09 stsp msg) == -1) {
400 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
403 257add31 2020-09-09 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
409 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
411 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
415 257add31 2020-09-09 stsp lookup(char *s)
417 257add31 2020-09-09 stsp /* This has to be sorted always. */
418 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
419 257add31 2020-09-09 stsp {"author", AUTHOR},
420 b8adfa55 2020-09-25 stsp {"branch", BRANCH},
421 cfd92333 2021-08-27 tracey {"fetch", FETCH},
422 0c8b29c5 2021-01-05 stsp {"fetch-all-branches", FETCH_ALL_BRANCHES},
423 257add31 2020-09-09 stsp {"mirror-references", MIRROR_REFERENCES},
424 257add31 2020-09-09 stsp {"port", PORT},
425 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
426 99495ddb 2021-01-10 stsp {"reference", REFERENCE},
427 257add31 2020-09-09 stsp {"remote", REMOTE},
428 257add31 2020-09-09 stsp {"repository", REPOSITORY},
429 cfd92333 2021-08-27 tracey {"send", SEND},
430 257add31 2020-09-09 stsp {"server", SERVER},
432 257add31 2020-09-09 stsp const struct keywords *p;
434 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
435 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
438 257add31 2020-09-09 stsp return (p->k_val);
440 257add31 2020-09-09 stsp return (STRING);
443 257add31 2020-09-09 stsp #define START_EXPAND 1
444 257add31 2020-09-09 stsp #define DONE_EXPAND 2
446 257add31 2020-09-09 stsp static int expanding;
449 257add31 2020-09-09 stsp igetc(void)
453 257add31 2020-09-09 stsp while (1) {
454 257add31 2020-09-09 stsp if (file->ungetpos > 0)
455 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
457 257add31 2020-09-09 stsp c = getc(file->stream);
459 257add31 2020-09-09 stsp if (c == START_EXPAND)
460 257add31 2020-09-09 stsp expanding = 1;
461 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
462 257add31 2020-09-09 stsp expanding = 0;
466 257add31 2020-09-09 stsp return (c);
470 257add31 2020-09-09 stsp lgetc(int quotec)
472 257add31 2020-09-09 stsp int c, next;
474 257add31 2020-09-09 stsp if (quotec) {
475 257add31 2020-09-09 stsp c = igetc();
476 257add31 2020-09-09 stsp if (c == EOF) {
477 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
478 257add31 2020-09-09 stsp "quoted string");
480 257add31 2020-09-09 stsp return (c);
483 257add31 2020-09-09 stsp c = igetc();
484 257add31 2020-09-09 stsp while (c == '\\') {
485 257add31 2020-09-09 stsp next = igetc();
486 257add31 2020-09-09 stsp if (next != '\n') {
490 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
491 257add31 2020-09-09 stsp file->lineno++;
494 257add31 2020-09-09 stsp return (c);
498 257add31 2020-09-09 stsp lungetc(int c)
500 257add31 2020-09-09 stsp if (c == EOF)
503 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
504 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
505 257add31 2020-09-09 stsp if (p == NULL)
506 257add31 2020-09-09 stsp err(1, "%s", __func__);
507 257add31 2020-09-09 stsp file->ungetbuf = p;
508 257add31 2020-09-09 stsp file->ungetsize *= 2;
510 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
514 257add31 2020-09-09 stsp findeol(void)
518 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
519 257add31 2020-09-09 stsp while (1) {
520 257add31 2020-09-09 stsp c = lgetc(0);
521 257add31 2020-09-09 stsp if (c == '\n') {
522 257add31 2020-09-09 stsp file->lineno++;
525 257add31 2020-09-09 stsp if (c == EOF)
528 257add31 2020-09-09 stsp return (ERROR);
531 257add31 2020-09-09 stsp static long long
532 257add31 2020-09-09 stsp getservice(char *n)
534 257add31 2020-09-09 stsp struct servent *s;
535 257add31 2020-09-09 stsp u_long ulval;
537 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
538 257add31 2020-09-09 stsp if (ulval > 65535) {
539 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
540 257add31 2020-09-09 stsp return (-1);
542 257add31 2020-09-09 stsp return ulval;
544 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
545 257add31 2020-09-09 stsp if (s == NULL)
546 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
547 257add31 2020-09-09 stsp if (s == NULL) {
548 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
549 257add31 2020-09-09 stsp return (-1);
551 257add31 2020-09-09 stsp return (s->s_port);
556 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
558 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
560 257add31 2020-09-09 stsp return (-1);
562 257add31 2020-09-09 stsp return (0);
567 257add31 2020-09-09 stsp yylex(void)
569 257add31 2020-09-09 stsp unsigned char buf[8096];
570 257add31 2020-09-09 stsp unsigned char *p, *val;
571 257add31 2020-09-09 stsp int quotec, next, c;
576 257add31 2020-09-09 stsp c = lgetc(0);
577 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
578 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
580 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
581 257add31 2020-09-09 stsp if (c == '#') {
582 257add31 2020-09-09 stsp c = lgetc(0);
583 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
584 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
586 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
587 257add31 2020-09-09 stsp while (1) {
588 257add31 2020-09-09 stsp c = lgetc(0);
589 257add31 2020-09-09 stsp if (c == EOF)
590 257add31 2020-09-09 stsp return (0);
592 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
593 257add31 2020-09-09 stsp yyerror("string too long");
594 257add31 2020-09-09 stsp return (findeol());
596 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
601 257add31 2020-09-09 stsp lungetc(c);
604 257add31 2020-09-09 stsp val = symget(buf);
605 257add31 2020-09-09 stsp if (val == NULL) {
606 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
607 257add31 2020-09-09 stsp return (findeol());
609 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
610 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
611 257add31 2020-09-09 stsp while (p >= val) {
612 257add31 2020-09-09 stsp lungetc(*p);
615 257add31 2020-09-09 stsp lungetc(START_EXPAND);
619 257add31 2020-09-09 stsp switch (c) {
622 257add31 2020-09-09 stsp quotec = c;
623 257add31 2020-09-09 stsp while (1) {
624 257add31 2020-09-09 stsp c = lgetc(quotec);
625 257add31 2020-09-09 stsp if (c == EOF)
626 257add31 2020-09-09 stsp return (0);
627 257add31 2020-09-09 stsp if (c == '\n') {
628 257add31 2020-09-09 stsp file->lineno++;
630 257add31 2020-09-09 stsp } else if (c == '\\') {
631 257add31 2020-09-09 stsp next = lgetc(quotec);
632 257add31 2020-09-09 stsp if (next == EOF)
633 257add31 2020-09-09 stsp return (0);
634 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
636 257add31 2020-09-09 stsp else if (next == '\n') {
637 257add31 2020-09-09 stsp file->lineno++;
640 257add31 2020-09-09 stsp lungetc(next);
641 257add31 2020-09-09 stsp } else if (c == quotec) {
644 257add31 2020-09-09 stsp } else if (c == '\0') {
645 257add31 2020-09-09 stsp yyerror("syntax error");
646 257add31 2020-09-09 stsp return (findeol());
648 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
649 257add31 2020-09-09 stsp yyerror("string too long");
650 257add31 2020-09-09 stsp return (findeol());
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__);
657 257add31 2020-09-09 stsp return (STRING);
660 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
661 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
663 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
666 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
667 257add31 2020-09-09 stsp yyerror("string too long");
668 257add31 2020-09-09 stsp return (findeol());
670 257add31 2020-09-09 stsp c = lgetc(0);
671 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
672 257add31 2020-09-09 stsp lungetc(c);
673 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
674 257add31 2020-09-09 stsp goto nodigits;
675 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
676 257add31 2020-09-09 stsp const char *errstr = NULL;
679 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
680 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
681 257add31 2020-09-09 stsp if (errstr) {
682 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
683 257add31 2020-09-09 stsp buf, errstr);
684 257add31 2020-09-09 stsp return (findeol());
686 257add31 2020-09-09 stsp return (NUMBER);
689 257add31 2020-09-09 stsp while (p > buf + 1)
690 257add31 2020-09-09 stsp lungetc(*--p);
692 257add31 2020-09-09 stsp if (c == '-')
693 257add31 2020-09-09 stsp return (c);
697 257add31 2020-09-09 stsp #define allowed_in_string(x) \
698 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
699 257add31 2020-09-09 stsp x != '{' && x != '}' && \
700 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
703 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
706 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
707 257add31 2020-09-09 stsp yyerror("string too long");
708 257add31 2020-09-09 stsp return (findeol());
710 257add31 2020-09-09 stsp c = lgetc(0);
711 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
712 257add31 2020-09-09 stsp lungetc(c);
714 257add31 2020-09-09 stsp token = lookup(buf);
715 257add31 2020-09-09 stsp if (token == STRING) {
716 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
717 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
718 257add31 2020-09-09 stsp err(1, "%s", __func__);
720 257add31 2020-09-09 stsp return (token);
722 257add31 2020-09-09 stsp if (c == '\n') {
723 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
724 257add31 2020-09-09 stsp file->lineno++;
726 257add31 2020-09-09 stsp if (c == EOF)
727 257add31 2020-09-09 stsp return (0);
728 257add31 2020-09-09 stsp return (c);
731 257add31 2020-09-09 stsp static const struct got_error*
732 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
734 257add31 2020-09-09 stsp const struct got_error* error = NULL;
736 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
737 257add31 2020-09-09 stsp if ((*nfile) == NULL)
738 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
739 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
740 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
741 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
742 257add31 2020-09-09 stsp free((*nfile));
743 257add31 2020-09-09 stsp return error;
745 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
746 257add31 2020-09-09 stsp (*nfile)->name = filename;
747 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
748 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
749 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
750 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
751 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
752 257add31 2020-09-09 stsp fclose((*nfile)->stream);
753 257add31 2020-09-09 stsp free((*nfile));
754 257add31 2020-09-09 stsp return error;
756 257add31 2020-09-09 stsp return NULL;
759 257add31 2020-09-09 stsp static const struct got_error*
760 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
762 257add31 2020-09-09 stsp const struct got_error *error = NULL;
764 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
765 257add31 2020-09-09 stsp if (*remote == NULL)
766 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
767 cfd92333 2021-08-27 tracey return error;
770 cfd92333 2021-08-27 tracey static const struct got_error*
771 cfd92333 2021-08-27 tracey new_fetch(struct fetch_repo **fetch_repo)
773 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
775 cfd92333 2021-08-27 tracey *fetch_repo = calloc(1, sizeof(**fetch_repo));
776 cfd92333 2021-08-27 tracey if (*fetch_repo == NULL)
777 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
778 cfd92333 2021-08-27 tracey return error;
781 cfd92333 2021-08-27 tracey static const struct got_error*
782 cfd92333 2021-08-27 tracey new_send(struct send_repo **send_repo)
784 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
786 cfd92333 2021-08-27 tracey *send_repo = calloc(1, sizeof(**send_repo));
787 cfd92333 2021-08-27 tracey if (*send_repo == NULL)
788 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
789 257add31 2020-09-09 stsp return error;
792 257add31 2020-09-09 stsp static void
793 257add31 2020-09-09 stsp closefile(struct file *file)
795 257add31 2020-09-09 stsp fclose(file->stream);
796 257add31 2020-09-09 stsp free(file->ungetbuf);
797 257add31 2020-09-09 stsp free(file);
800 257add31 2020-09-09 stsp const struct got_error *
801 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
803 257add31 2020-09-09 stsp const struct got_error *err = NULL;
804 257add31 2020-09-09 stsp struct sym *sym, *next;
806 257add31 2020-09-09 stsp *conf = NULL;
808 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
810 257add31 2020-09-09 stsp return err;
812 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
815 257add31 2020-09-09 stsp closefile(file);
817 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
818 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
819 257add31 2020-09-09 stsp if (!sym->persist) {
820 257add31 2020-09-09 stsp free(sym->nam);
821 257add31 2020-09-09 stsp free(sym->val);
822 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
827 257add31 2020-09-09 stsp if (gerror == NULL)
828 257add31 2020-09-09 stsp *conf = &gotconfig;
829 257add31 2020-09-09 stsp return gerror;
833 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
835 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
837 257add31 2020-09-09 stsp free(conf->author);
838 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
839 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
840 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
841 cfd92333 2021-08-27 tracey if (remote->fetch_repo != NULL) {
842 cfd92333 2021-08-27 tracey free(remote->fetch_repo->fetch_repository);
843 cfd92333 2021-08-27 tracey free(remote->fetch_repo->fetch_server);
844 cfd92333 2021-08-27 tracey free(remote->fetch_repo->fetch_protocol);
846 cfd92333 2021-08-27 tracey if (remote->send_repo != NULL) {
847 cfd92333 2021-08-27 tracey free(remote->send_repo->send_repository);
848 cfd92333 2021-08-27 tracey free(remote->send_repo->send_server);
849 cfd92333 2021-08-27 tracey free(remote->send_repo->send_protocol);
851 257add31 2020-09-09 stsp free(remote->name);
852 257add31 2020-09-09 stsp free(remote->repository);
853 257add31 2020-09-09 stsp free(remote->server);
854 257add31 2020-09-09 stsp free(remote->protocol);
855 257add31 2020-09-09 stsp free(remote);
860 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
862 257add31 2020-09-09 stsp struct sym *sym;
864 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
865 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
869 257add31 2020-09-09 stsp if (sym != NULL) {
870 257add31 2020-09-09 stsp if (sym->persist == 1)
871 257add31 2020-09-09 stsp return (0);
873 257add31 2020-09-09 stsp free(sym->nam);
874 257add31 2020-09-09 stsp free(sym->val);
875 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
879 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
880 257add31 2020-09-09 stsp if (sym == NULL)
881 257add31 2020-09-09 stsp return (-1);
883 257add31 2020-09-09 stsp sym->nam = strdup(nam);
884 257add31 2020-09-09 stsp if (sym->nam == NULL) {
886 257add31 2020-09-09 stsp return (-1);
888 257add31 2020-09-09 stsp sym->val = strdup(val);
889 257add31 2020-09-09 stsp if (sym->val == NULL) {
890 257add31 2020-09-09 stsp free(sym->nam);
892 257add31 2020-09-09 stsp return (-1);
894 257add31 2020-09-09 stsp sym->used = 0;
895 257add31 2020-09-09 stsp sym->persist = persist;
896 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
897 257add31 2020-09-09 stsp return (0);
901 257add31 2020-09-09 stsp cmdline_symset(char *s)
903 257add31 2020-09-09 stsp char *sym, *val;
905 257add31 2020-09-09 stsp size_t len;
907 257add31 2020-09-09 stsp val = strrchr(s, '=');
908 257add31 2020-09-09 stsp if (val == NULL)
909 257add31 2020-09-09 stsp return (-1);
911 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
912 257add31 2020-09-09 stsp sym = malloc(len);
913 257add31 2020-09-09 stsp if (sym == NULL)
914 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
916 257add31 2020-09-09 stsp strlcpy(sym, s, len);
918 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
921 257add31 2020-09-09 stsp return (ret);
925 257add31 2020-09-09 stsp symget(const char *nam)
927 257add31 2020-09-09 stsp struct sym *sym;
929 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
930 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
931 257add31 2020-09-09 stsp sym->used = 1;
932 257add31 2020-09-09 stsp return (sym->val);
935 257add31 2020-09-09 stsp return (NULL);
939 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
941 257add31 2020-09-09 stsp u_long ulval;
945 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
946 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
947 257add31 2020-09-09 stsp return (-1);
948 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
949 257add31 2020-09-09 stsp return (-1);
950 257add31 2020-09-09 stsp *ulvalp = ulval;
951 257add31 2020-09-09 stsp return (0);