2 257add31 2020-09-09 stsp * Copyright (c) 2020 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 **);
85 257add31 2020-09-09 stsp typedef struct {
87 1367695b 2020-09-26 naddy long long number;
88 257add31 2020-09-09 stsp char *string;
89 b8adfa55 2020-09-25 stsp struct node_branch *branch;
90 99495ddb 2021-01-10 stsp struct node_ref *ref;
97 257add31 2020-09-09 stsp %token ERROR
98 b8adfa55 2020-09-25 stsp %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
99 99495ddb 2021-01-10 stsp %token AUTHOR FETCH_ALL_BRANCHES REFERENCE
100 257add31 2020-09-09 stsp %token <v.string> STRING
101 257add31 2020-09-09 stsp %token <v.number> NUMBER
102 257add31 2020-09-09 stsp %type <v.number> boolean portplain
103 257add31 2020-09-09 stsp %type <v.string> numberstring
104 b8adfa55 2020-09-25 stsp %type <v.branch> branch xbranch branch_list
105 99495ddb 2021-01-10 stsp %type <v.ref> ref xref ref_list
109 257add31 2020-09-09 stsp grammar : /* empty */
110 257add31 2020-09-09 stsp | grammar '\n'
111 257add31 2020-09-09 stsp | grammar author '\n'
112 257add31 2020-09-09 stsp | grammar remote '\n'
114 257add31 2020-09-09 stsp boolean : STRING {
115 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
116 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
118 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
119 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
122 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
129 257add31 2020-09-09 stsp numberstring : NUMBER {
131 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
132 257add31 2020-09-09 stsp yyerror("string: asprintf");
139 257add31 2020-09-09 stsp portplain : numberstring {
140 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
147 b8adfa55 2020-09-25 stsp branch : /* empty */ { $$ = NULL; }
148 b8adfa55 2020-09-25 stsp | xbranch { $$ = $1; }
149 b8adfa55 2020-09-25 stsp | '{' optnl branch_list '}' { $$ = $3; }
151 b8adfa55 2020-09-25 stsp xbranch : STRING {
152 b8adfa55 2020-09-25 stsp $$ = calloc(1, sizeof(struct node_branch));
153 b8adfa55 2020-09-25 stsp if ($$ == NULL) {
154 b8adfa55 2020-09-25 stsp yyerror("calloc");
157 b8adfa55 2020-09-25 stsp $$->branch_name = $1;
158 b8adfa55 2020-09-25 stsp $$->tail = $$;
161 b8adfa55 2020-09-25 stsp branch_list : xbranch optnl { $$ = $1; }
162 b8adfa55 2020-09-25 stsp | branch_list comma xbranch optnl {
163 b8adfa55 2020-09-25 stsp $1->tail->next = $3;
164 b8adfa55 2020-09-25 stsp $1->tail = $3;
168 99495ddb 2021-01-10 stsp ref : /* empty */ { $$ = NULL; }
169 99495ddb 2021-01-10 stsp | xref { $$ = $1; }
170 99495ddb 2021-01-10 stsp | '{' optnl ref_list '}' { $$ = $3; }
172 99495ddb 2021-01-10 stsp xref : STRING {
173 99495ddb 2021-01-10 stsp $$ = calloc(1, sizeof(struct node_ref));
174 99495ddb 2021-01-10 stsp if ($$ == NULL) {
175 99495ddb 2021-01-10 stsp yyerror("calloc");
178 99495ddb 2021-01-10 stsp $$->ref_name = $1;
179 99495ddb 2021-01-10 stsp $$->tail = $$;
182 99495ddb 2021-01-10 stsp ref_list : xref optnl { $$ = $1; }
183 99495ddb 2021-01-10 stsp | ref_list comma xref optnl {
184 99495ddb 2021-01-10 stsp $1->tail->next = $3;
185 99495ddb 2021-01-10 stsp $1->tail = $3;
189 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
190 257add31 2020-09-09 stsp | remoteopts1 optnl
192 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
193 257add31 2020-09-09 stsp remote->repository = strdup($2);
194 257add31 2020-09-09 stsp if (remote->repository == NULL) {
196 257add31 2020-09-09 stsp yyerror("strdup");
201 257add31 2020-09-09 stsp | SERVER STRING {
202 257add31 2020-09-09 stsp remote->server = strdup($2);
203 257add31 2020-09-09 stsp if (remote->server == NULL) {
205 257add31 2020-09-09 stsp yyerror("strdup");
210 257add31 2020-09-09 stsp | PROTOCOL STRING {
211 257add31 2020-09-09 stsp remote->protocol = strdup($2);
212 257add31 2020-09-09 stsp if (remote->protocol == NULL) {
214 257add31 2020-09-09 stsp yyerror("strdup");
219 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
220 257add31 2020-09-09 stsp remote->mirror_references = $2;
222 0c8b29c5 2021-01-05 stsp | FETCH_ALL_BRANCHES boolean {
223 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = $2;
225 257add31 2020-09-09 stsp | PORT portplain {
226 257add31 2020-09-09 stsp remote->port = $2;
228 b8adfa55 2020-09-25 stsp | BRANCH branch {
229 b8adfa55 2020-09-25 stsp remote->branch = $2;
231 99495ddb 2021-01-10 stsp | REFERENCE ref {
232 99495ddb 2021-01-10 stsp remote->ref = $2;
235 257add31 2020-09-09 stsp remote : REMOTE STRING {
236 257add31 2020-09-09 stsp static const struct got_error* error;
238 257add31 2020-09-09 stsp error = new_remote(&remote);
239 257add31 2020-09-09 stsp if (error) {
241 257add31 2020-09-09 stsp yyerror("%s", error->msg);
244 257add31 2020-09-09 stsp remote->name = strdup($2);
245 257add31 2020-09-09 stsp if (remote->name == NULL) {
247 257add31 2020-09-09 stsp yyerror("strdup");
251 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
252 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
253 257add31 2020-09-09 stsp gotconfig.nremotes++;
256 257add31 2020-09-09 stsp author : AUTHOR STRING {
257 257add31 2020-09-09 stsp gotconfig.author = strdup($2);
258 257add31 2020-09-09 stsp if (gotconfig.author == NULL) {
260 257add31 2020-09-09 stsp yyerror("strdup");
266 257add31 2020-09-09 stsp optnl : '\n' optnl
267 257add31 2020-09-09 stsp | /* empty */
269 257add31 2020-09-09 stsp nl : '\n' optnl
271 b8adfa55 2020-09-25 stsp comma : ','
272 b8adfa55 2020-09-25 stsp | /* empty */
276 257add31 2020-09-09 stsp struct keywords {
277 257add31 2020-09-09 stsp const char *k_name;
282 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
284 257add31 2020-09-09 stsp va_list ap;
286 257add31 2020-09-09 stsp char *err = NULL;
288 257add31 2020-09-09 stsp va_start(ap, fmt);
289 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
290 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
293 257add31 2020-09-09 stsp va_end(ap);
294 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
295 257add31 2020-09-09 stsp msg) == -1) {
296 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
299 257add31 2020-09-09 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
305 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
307 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
311 257add31 2020-09-09 stsp lookup(char *s)
313 257add31 2020-09-09 stsp /* This has to be sorted always. */
314 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
315 257add31 2020-09-09 stsp {"author", AUTHOR},
316 b8adfa55 2020-09-25 stsp {"branch", BRANCH},
317 0c8b29c5 2021-01-05 stsp {"fetch-all-branches", FETCH_ALL_BRANCHES},
318 257add31 2020-09-09 stsp {"mirror-references", MIRROR_REFERENCES},
319 257add31 2020-09-09 stsp {"port", PORT},
320 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
321 99495ddb 2021-01-10 stsp {"reference", REFERENCE},
322 257add31 2020-09-09 stsp {"remote", REMOTE},
323 257add31 2020-09-09 stsp {"repository", REPOSITORY},
324 257add31 2020-09-09 stsp {"server", SERVER},
326 257add31 2020-09-09 stsp const struct keywords *p;
328 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
329 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
332 257add31 2020-09-09 stsp return (p->k_val);
334 257add31 2020-09-09 stsp return (STRING);
337 257add31 2020-09-09 stsp #define START_EXPAND 1
338 257add31 2020-09-09 stsp #define DONE_EXPAND 2
340 257add31 2020-09-09 stsp static int expanding;
343 257add31 2020-09-09 stsp igetc(void)
347 257add31 2020-09-09 stsp while (1) {
348 257add31 2020-09-09 stsp if (file->ungetpos > 0)
349 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
351 257add31 2020-09-09 stsp c = getc(file->stream);
353 257add31 2020-09-09 stsp if (c == START_EXPAND)
354 257add31 2020-09-09 stsp expanding = 1;
355 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
356 257add31 2020-09-09 stsp expanding = 0;
360 257add31 2020-09-09 stsp return (c);
364 257add31 2020-09-09 stsp lgetc(int quotec)
366 257add31 2020-09-09 stsp int c, next;
368 257add31 2020-09-09 stsp if (quotec) {
369 257add31 2020-09-09 stsp c = igetc();
370 257add31 2020-09-09 stsp if (c == EOF) {
371 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
372 257add31 2020-09-09 stsp "quoted string");
374 257add31 2020-09-09 stsp return (c);
377 257add31 2020-09-09 stsp c = igetc();
378 257add31 2020-09-09 stsp while (c == '\\') {
379 257add31 2020-09-09 stsp next = igetc();
380 257add31 2020-09-09 stsp if (next != '\n') {
384 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
385 257add31 2020-09-09 stsp file->lineno++;
388 257add31 2020-09-09 stsp return (c);
392 257add31 2020-09-09 stsp lungetc(int c)
394 257add31 2020-09-09 stsp if (c == EOF)
397 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
398 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
399 257add31 2020-09-09 stsp if (p == NULL)
400 257add31 2020-09-09 stsp err(1, "%s", __func__);
401 257add31 2020-09-09 stsp file->ungetbuf = p;
402 257add31 2020-09-09 stsp file->ungetsize *= 2;
404 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
408 257add31 2020-09-09 stsp findeol(void)
412 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
413 257add31 2020-09-09 stsp while (1) {
414 257add31 2020-09-09 stsp c = lgetc(0);
415 257add31 2020-09-09 stsp if (c == '\n') {
416 257add31 2020-09-09 stsp file->lineno++;
419 257add31 2020-09-09 stsp if (c == EOF)
422 257add31 2020-09-09 stsp return (ERROR);
425 257add31 2020-09-09 stsp static long long
426 257add31 2020-09-09 stsp getservice(char *n)
428 257add31 2020-09-09 stsp struct servent *s;
429 257add31 2020-09-09 stsp u_long ulval;
431 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
432 257add31 2020-09-09 stsp if (ulval > 65535) {
433 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
434 257add31 2020-09-09 stsp return (-1);
436 257add31 2020-09-09 stsp return ulval;
438 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
439 257add31 2020-09-09 stsp if (s == NULL)
440 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
441 257add31 2020-09-09 stsp if (s == NULL) {
442 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
443 257add31 2020-09-09 stsp return (-1);
445 257add31 2020-09-09 stsp return (s->s_port);
450 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
452 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
454 257add31 2020-09-09 stsp return (-1);
456 257add31 2020-09-09 stsp return (0);
461 257add31 2020-09-09 stsp yylex(void)
463 257add31 2020-09-09 stsp unsigned char buf[8096];
464 257add31 2020-09-09 stsp unsigned char *p, *val;
465 257add31 2020-09-09 stsp int quotec, next, c;
470 257add31 2020-09-09 stsp c = lgetc(0);
471 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
472 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
474 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
475 257add31 2020-09-09 stsp if (c == '#') {
476 257add31 2020-09-09 stsp c = lgetc(0);
477 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
478 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
480 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
481 257add31 2020-09-09 stsp while (1) {
482 257add31 2020-09-09 stsp c = lgetc(0);
483 257add31 2020-09-09 stsp if (c == EOF)
484 257add31 2020-09-09 stsp return (0);
486 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
487 257add31 2020-09-09 stsp yyerror("string too long");
488 257add31 2020-09-09 stsp return (findeol());
490 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
495 257add31 2020-09-09 stsp lungetc(c);
498 257add31 2020-09-09 stsp val = symget(buf);
499 257add31 2020-09-09 stsp if (val == NULL) {
500 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
501 257add31 2020-09-09 stsp return (findeol());
503 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
504 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
505 257add31 2020-09-09 stsp while (p >= val) {
506 257add31 2020-09-09 stsp lungetc(*p);
509 257add31 2020-09-09 stsp lungetc(START_EXPAND);
513 257add31 2020-09-09 stsp switch (c) {
516 257add31 2020-09-09 stsp quotec = c;
517 257add31 2020-09-09 stsp while (1) {
518 257add31 2020-09-09 stsp c = lgetc(quotec);
519 257add31 2020-09-09 stsp if (c == EOF)
520 257add31 2020-09-09 stsp return (0);
521 257add31 2020-09-09 stsp if (c == '\n') {
522 257add31 2020-09-09 stsp file->lineno++;
524 257add31 2020-09-09 stsp } else if (c == '\\') {
525 257add31 2020-09-09 stsp next = lgetc(quotec);
526 257add31 2020-09-09 stsp if (next == EOF)
527 257add31 2020-09-09 stsp return (0);
528 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
530 257add31 2020-09-09 stsp else if (next == '\n') {
531 257add31 2020-09-09 stsp file->lineno++;
534 257add31 2020-09-09 stsp lungetc(next);
535 257add31 2020-09-09 stsp } else if (c == quotec) {
538 257add31 2020-09-09 stsp } else if (c == '\0') {
539 257add31 2020-09-09 stsp yyerror("syntax error");
540 257add31 2020-09-09 stsp return (findeol());
542 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
543 257add31 2020-09-09 stsp yyerror("string too long");
544 257add31 2020-09-09 stsp return (findeol());
548 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
549 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
550 257add31 2020-09-09 stsp err(1, "%s", __func__);
551 257add31 2020-09-09 stsp return (STRING);
554 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
555 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
557 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
560 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
561 257add31 2020-09-09 stsp yyerror("string too long");
562 257add31 2020-09-09 stsp return (findeol());
564 257add31 2020-09-09 stsp c = lgetc(0);
565 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
566 257add31 2020-09-09 stsp lungetc(c);
567 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
568 257add31 2020-09-09 stsp goto nodigits;
569 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
570 257add31 2020-09-09 stsp const char *errstr = NULL;
573 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
574 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
575 257add31 2020-09-09 stsp if (errstr) {
576 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
577 257add31 2020-09-09 stsp buf, errstr);
578 257add31 2020-09-09 stsp return (findeol());
580 257add31 2020-09-09 stsp return (NUMBER);
583 257add31 2020-09-09 stsp while (p > buf + 1)
584 257add31 2020-09-09 stsp lungetc(*--p);
586 257add31 2020-09-09 stsp if (c == '-')
587 257add31 2020-09-09 stsp return (c);
591 257add31 2020-09-09 stsp #define allowed_in_string(x) \
592 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
593 257add31 2020-09-09 stsp x != '{' && x != '}' && \
594 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
597 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
600 257add31 2020-09-09 stsp if ((unsigned)(p-buf) >= sizeof(buf)) {
601 257add31 2020-09-09 stsp yyerror("string too long");
602 257add31 2020-09-09 stsp return (findeol());
604 257add31 2020-09-09 stsp c = lgetc(0);
605 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
606 257add31 2020-09-09 stsp lungetc(c);
608 257add31 2020-09-09 stsp token = lookup(buf);
609 257add31 2020-09-09 stsp if (token == STRING) {
610 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
611 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
612 257add31 2020-09-09 stsp err(1, "%s", __func__);
614 257add31 2020-09-09 stsp return (token);
616 257add31 2020-09-09 stsp if (c == '\n') {
617 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
618 257add31 2020-09-09 stsp file->lineno++;
620 257add31 2020-09-09 stsp if (c == EOF)
621 257add31 2020-09-09 stsp return (0);
622 257add31 2020-09-09 stsp return (c);
625 257add31 2020-09-09 stsp static const struct got_error*
626 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
628 257add31 2020-09-09 stsp const struct got_error* error = NULL;
630 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
631 257add31 2020-09-09 stsp if ((*nfile) == NULL)
632 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
633 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
634 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
635 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
636 257add31 2020-09-09 stsp free((*nfile));
637 257add31 2020-09-09 stsp return error;
639 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
640 257add31 2020-09-09 stsp (*nfile)->name = filename;
641 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
642 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
643 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
644 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
645 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
646 257add31 2020-09-09 stsp fclose((*nfile)->stream);
647 257add31 2020-09-09 stsp free((*nfile));
648 257add31 2020-09-09 stsp return error;
650 257add31 2020-09-09 stsp return NULL;
653 257add31 2020-09-09 stsp static const struct got_error*
654 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
656 257add31 2020-09-09 stsp const struct got_error *error = NULL;
658 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
659 257add31 2020-09-09 stsp if (*remote == NULL)
660 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
661 257add31 2020-09-09 stsp return error;
664 257add31 2020-09-09 stsp static void
665 257add31 2020-09-09 stsp closefile(struct file *file)
667 257add31 2020-09-09 stsp fclose(file->stream);
668 257add31 2020-09-09 stsp free(file->ungetbuf);
669 257add31 2020-09-09 stsp free(file);
672 257add31 2020-09-09 stsp const struct got_error *
673 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
675 257add31 2020-09-09 stsp const struct got_error *err = NULL;
676 257add31 2020-09-09 stsp struct sym *sym, *next;
678 257add31 2020-09-09 stsp *conf = NULL;
680 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
682 257add31 2020-09-09 stsp return err;
684 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
687 257add31 2020-09-09 stsp closefile(file);
689 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
690 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
691 257add31 2020-09-09 stsp if (!sym->persist) {
692 257add31 2020-09-09 stsp free(sym->nam);
693 257add31 2020-09-09 stsp free(sym->val);
694 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
699 257add31 2020-09-09 stsp if (gerror == NULL)
700 257add31 2020-09-09 stsp *conf = &gotconfig;
701 257add31 2020-09-09 stsp return gerror;
705 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
707 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
709 257add31 2020-09-09 stsp free(conf->author);
710 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
711 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
712 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
713 257add31 2020-09-09 stsp free(remote->name);
714 257add31 2020-09-09 stsp free(remote->repository);
715 257add31 2020-09-09 stsp free(remote->server);
716 257add31 2020-09-09 stsp free(remote->protocol);
717 257add31 2020-09-09 stsp free(remote);
722 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
724 257add31 2020-09-09 stsp struct sym *sym;
726 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
727 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
731 257add31 2020-09-09 stsp if (sym != NULL) {
732 257add31 2020-09-09 stsp if (sym->persist == 1)
733 257add31 2020-09-09 stsp return (0);
735 257add31 2020-09-09 stsp free(sym->nam);
736 257add31 2020-09-09 stsp free(sym->val);
737 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
741 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
742 257add31 2020-09-09 stsp if (sym == NULL)
743 257add31 2020-09-09 stsp return (-1);
745 257add31 2020-09-09 stsp sym->nam = strdup(nam);
746 257add31 2020-09-09 stsp if (sym->nam == NULL) {
748 257add31 2020-09-09 stsp return (-1);
750 257add31 2020-09-09 stsp sym->val = strdup(val);
751 257add31 2020-09-09 stsp if (sym->val == NULL) {
752 257add31 2020-09-09 stsp free(sym->nam);
754 257add31 2020-09-09 stsp return (-1);
756 257add31 2020-09-09 stsp sym->used = 0;
757 257add31 2020-09-09 stsp sym->persist = persist;
758 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
759 257add31 2020-09-09 stsp return (0);
763 257add31 2020-09-09 stsp cmdline_symset(char *s)
765 257add31 2020-09-09 stsp char *sym, *val;
767 257add31 2020-09-09 stsp size_t len;
769 257add31 2020-09-09 stsp val = strrchr(s, '=');
770 257add31 2020-09-09 stsp if (val == NULL)
771 257add31 2020-09-09 stsp return (-1);
773 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
774 257add31 2020-09-09 stsp sym = malloc(len);
775 257add31 2020-09-09 stsp if (sym == NULL)
776 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
778 257add31 2020-09-09 stsp strlcpy(sym, s, len);
780 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
783 257add31 2020-09-09 stsp return (ret);
787 257add31 2020-09-09 stsp symget(const char *nam)
789 257add31 2020-09-09 stsp struct sym *sym;
791 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
792 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
793 257add31 2020-09-09 stsp sym->used = 1;
794 257add31 2020-09-09 stsp return (sym->val);
797 257add31 2020-09-09 stsp return (NULL);
801 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
803 257add31 2020-09-09 stsp u_long ulval;
807 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
808 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
809 257add31 2020-09-09 stsp return (-1);
810 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
811 257add31 2020-09-09 stsp return (-1);
812 257add31 2020-09-09 stsp *ulvalp = ulval;
813 257add31 2020-09-09 stsp return (0);