Blame


1 257add31 2020-09-09 stsp /*
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.
10 257add31 2020-09-09 stsp *
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.
14 257add31 2020-09-09 stsp *
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.
22 257add31 2020-09-09 stsp */
23 257add31 2020-09-09 stsp
24 257add31 2020-09-09 stsp %{
25 257add31 2020-09-09 stsp #include <sys/types.h>
26 257add31 2020-09-09 stsp
27 257add31 2020-09-09 stsp #include <netdb.h>
28 257add31 2020-09-09 stsp
29 257add31 2020-09-09 stsp #include <ctype.h>
30 257add31 2020-09-09 stsp #include <err.h>
31 257add31 2020-09-09 stsp #include <errno.h>
32 257add31 2020-09-09 stsp #include <limits.h>
33 257add31 2020-09-09 stsp #include <stdarg.h>
34 257add31 2020-09-09 stsp #include <stdio.h>
35 8de9818a 2020-09-14 naddy #include <stdlib.h>
36 257add31 2020-09-09 stsp #include <string.h>
37 257add31 2020-09-09 stsp
38 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
39 dd038bc6 2021-09-21 thomas.ad
40 257add31 2020-09-09 stsp #include "got_error.h"
41 257add31 2020-09-09 stsp #include "gotconfig.h"
42 257add31 2020-09-09 stsp
43 257add31 2020-09-09 stsp static struct file {
44 257add31 2020-09-09 stsp FILE *stream;
45 257add31 2020-09-09 stsp const char *name;
46 257add31 2020-09-09 stsp size_t ungetpos;
47 257add31 2020-09-09 stsp size_t ungetsize;
48 257add31 2020-09-09 stsp u_char *ungetbuf;
49 257add31 2020-09-09 stsp int eof_reached;
50 257add31 2020-09-09 stsp int lineno;
51 257add31 2020-09-09 stsp } *file;
52 257add31 2020-09-09 stsp static const struct got_error* newfile(struct file**, const char *, int *);
53 257add31 2020-09-09 stsp static void closefile(struct file *);
54 257add31 2020-09-09 stsp int yyparse(void);
55 257add31 2020-09-09 stsp int yylex(void);
56 257add31 2020-09-09 stsp int yyerror(const char *, ...)
57 257add31 2020-09-09 stsp __attribute__((__format__ (printf, 1, 2)))
58 257add31 2020-09-09 stsp __attribute__((__nonnull__ (1)));
59 257add31 2020-09-09 stsp int kw_cmp(const void *, const void *);
60 257add31 2020-09-09 stsp int lookup(char *);
61 257add31 2020-09-09 stsp int igetc(void);
62 257add31 2020-09-09 stsp int lgetc(int);
63 257add31 2020-09-09 stsp void lungetc(int);
64 257add31 2020-09-09 stsp int findeol(void);
65 257add31 2020-09-09 stsp static int parseport(char *, long long *);
66 257add31 2020-09-09 stsp
67 257add31 2020-09-09 stsp TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
68 257add31 2020-09-09 stsp struct sym {
69 257add31 2020-09-09 stsp TAILQ_ENTRY(sym) entry;
70 257add31 2020-09-09 stsp int used;
71 257add31 2020-09-09 stsp int persist;
72 257add31 2020-09-09 stsp char *nam;
73 257add31 2020-09-09 stsp char *val;
74 257add31 2020-09-09 stsp };
75 257add31 2020-09-09 stsp
76 257add31 2020-09-09 stsp int symset(const char *, const char *, int);
77 ef20f542 2022-06-26 thomas int cmdline_symset(char *);
78 257add31 2020-09-09 stsp char *symget(const char *);
79 257add31 2020-09-09 stsp
80 257add31 2020-09-09 stsp static int atoul(char *, u_long *);
81 257add31 2020-09-09 stsp
82 257add31 2020-09-09 stsp static const struct got_error* gerror;
83 257add31 2020-09-09 stsp static struct gotconfig_remote_repo *remote;
84 257add31 2020-09-09 stsp static struct gotconfig gotconfig;
85 257add31 2020-09-09 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
86 aaf30ee7 2021-08-29 stsp static const struct got_error* new_fetch_config(struct fetch_config **);
87 aaf30ee7 2021-08-29 stsp static const struct got_error* new_send_config(struct send_config **);
88 257add31 2020-09-09 stsp
89 257add31 2020-09-09 stsp typedef struct {
90 257add31 2020-09-09 stsp union {
91 1367695b 2020-09-26 naddy long long number;
92 257add31 2020-09-09 stsp char *string;
93 b8adfa55 2020-09-25 stsp struct node_branch *branch;
94 99495ddb 2021-01-10 stsp struct node_ref *ref;
95 257add31 2020-09-09 stsp } v;
96 257add31 2020-09-09 stsp int lineno;
97 257add31 2020-09-09 stsp } YYSTYPE;
98 257add31 2020-09-09 stsp
99 81e077a6 2022-03-08 thomas #if defined(__APPLE__) && !defined(YYSTYPE)
100 81e077a6 2022-03-08 thomas #warning "Setting YYSTYPE - is GNU Bison installed?"
101 81e077a6 2022-03-08 thomas #define YYSTYPE YYSTYPE
102 81e077a6 2022-03-08 thomas #endif
103 257add31 2020-09-09 stsp %}
104 257add31 2020-09-09 stsp
105 257add31 2020-09-09 stsp %token ERROR
106 b8adfa55 2020-09-25 stsp %token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES BRANCH
107 ff5e1f09 2022-07-06 thomas %token AUTHOR ALLOWED_SIGNERS REVOKED_SIGNERS SIGNER_ID FETCH_ALL_BRANCHES
108 ff5e1f09 2022-07-06 thomas %token REFERENCE FETCH SEND
109 257add31 2020-09-09 stsp %token <v.string> STRING
110 257add31 2020-09-09 stsp %token <v.number> NUMBER
111 257add31 2020-09-09 stsp %type <v.number> boolean portplain
112 257add31 2020-09-09 stsp %type <v.string> numberstring
113 b8adfa55 2020-09-25 stsp %type <v.branch> branch xbranch branch_list
114 99495ddb 2021-01-10 stsp %type <v.ref> ref xref ref_list
115 257add31 2020-09-09 stsp
116 257add31 2020-09-09 stsp %%
117 257add31 2020-09-09 stsp
118 257add31 2020-09-09 stsp grammar : /* empty */
119 257add31 2020-09-09 stsp | grammar '\n'
120 257add31 2020-09-09 stsp | grammar author '\n'
121 257add31 2020-09-09 stsp | grammar remote '\n'
122 871bd038 2022-07-03 thomas | grammar allowed_signers '\n'
123 aef4f7ae 2022-07-04 thomas | grammar revoked_signers '\n'
124 ff5e1f09 2022-07-06 thomas | grammar signer_id '\n'
125 257add31 2020-09-09 stsp ;
126 257add31 2020-09-09 stsp boolean : STRING {
127 257add31 2020-09-09 stsp if (strcasecmp($1, "true") == 0 ||
128 257add31 2020-09-09 stsp strcasecmp($1, "yes") == 0)
129 257add31 2020-09-09 stsp $$ = 1;
130 257add31 2020-09-09 stsp else if (strcasecmp($1, "false") == 0 ||
131 257add31 2020-09-09 stsp strcasecmp($1, "no") == 0)
132 257add31 2020-09-09 stsp $$ = 0;
133 257add31 2020-09-09 stsp else {
134 257add31 2020-09-09 stsp yyerror("invalid boolean value '%s'", $1);
135 257add31 2020-09-09 stsp free($1);
136 257add31 2020-09-09 stsp YYERROR;
137 257add31 2020-09-09 stsp }
138 257add31 2020-09-09 stsp free($1);
139 257add31 2020-09-09 stsp }
140 257add31 2020-09-09 stsp ;
141 257add31 2020-09-09 stsp numberstring : NUMBER {
142 257add31 2020-09-09 stsp char *s;
143 257add31 2020-09-09 stsp if (asprintf(&s, "%lld", $1) == -1) {
144 257add31 2020-09-09 stsp yyerror("string: asprintf");
145 257add31 2020-09-09 stsp YYERROR;
146 257add31 2020-09-09 stsp }
147 257add31 2020-09-09 stsp $$ = s;
148 257add31 2020-09-09 stsp }
149 257add31 2020-09-09 stsp | STRING
150 257add31 2020-09-09 stsp ;
151 257add31 2020-09-09 stsp portplain : numberstring {
152 257add31 2020-09-09 stsp if (parseport($1, &$$) == -1) {
153 257add31 2020-09-09 stsp free($1);
154 257add31 2020-09-09 stsp YYERROR;
155 257add31 2020-09-09 stsp }
156 257add31 2020-09-09 stsp free($1);
157 b8adfa55 2020-09-25 stsp }
158 b8adfa55 2020-09-25 stsp ;
159 b8adfa55 2020-09-25 stsp branch : /* empty */ { $$ = NULL; }
160 b8adfa55 2020-09-25 stsp | xbranch { $$ = $1; }
161 b8adfa55 2020-09-25 stsp | '{' optnl branch_list '}' { $$ = $3; }
162 b8adfa55 2020-09-25 stsp ;
163 b8adfa55 2020-09-25 stsp xbranch : STRING {
164 b8adfa55 2020-09-25 stsp $$ = calloc(1, sizeof(struct node_branch));
165 b8adfa55 2020-09-25 stsp if ($$ == NULL) {
166 b8adfa55 2020-09-25 stsp yyerror("calloc");
167 b8adfa55 2020-09-25 stsp YYERROR;
168 b8adfa55 2020-09-25 stsp }
169 b8adfa55 2020-09-25 stsp $$->branch_name = $1;
170 b8adfa55 2020-09-25 stsp $$->tail = $$;
171 b8adfa55 2020-09-25 stsp }
172 b8adfa55 2020-09-25 stsp ;
173 b8adfa55 2020-09-25 stsp branch_list : xbranch optnl { $$ = $1; }
174 b8adfa55 2020-09-25 stsp | branch_list comma xbranch optnl {
175 b8adfa55 2020-09-25 stsp $1->tail->next = $3;
176 b8adfa55 2020-09-25 stsp $1->tail = $3;
177 b8adfa55 2020-09-25 stsp $$ = $1;
178 257add31 2020-09-09 stsp }
179 257add31 2020-09-09 stsp ;
180 99495ddb 2021-01-10 stsp ref : /* empty */ { $$ = NULL; }
181 99495ddb 2021-01-10 stsp | xref { $$ = $1; }
182 99495ddb 2021-01-10 stsp | '{' optnl ref_list '}' { $$ = $3; }
183 99495ddb 2021-01-10 stsp ;
184 99495ddb 2021-01-10 stsp xref : STRING {
185 99495ddb 2021-01-10 stsp $$ = calloc(1, sizeof(struct node_ref));
186 99495ddb 2021-01-10 stsp if ($$ == NULL) {
187 99495ddb 2021-01-10 stsp yyerror("calloc");
188 99495ddb 2021-01-10 stsp YYERROR;
189 99495ddb 2021-01-10 stsp }
190 99495ddb 2021-01-10 stsp $$->ref_name = $1;
191 99495ddb 2021-01-10 stsp $$->tail = $$;
192 99495ddb 2021-01-10 stsp }
193 99495ddb 2021-01-10 stsp ;
194 99495ddb 2021-01-10 stsp ref_list : xref optnl { $$ = $1; }
195 99495ddb 2021-01-10 stsp | ref_list comma xref optnl {
196 99495ddb 2021-01-10 stsp $1->tail->next = $3;
197 99495ddb 2021-01-10 stsp $1->tail = $3;
198 99495ddb 2021-01-10 stsp $$ = $1;
199 99495ddb 2021-01-10 stsp }
200 99495ddb 2021-01-10 stsp ;
201 257add31 2020-09-09 stsp remoteopts2 : remoteopts2 remoteopts1 nl
202 abc59930 2021-09-05 naddy | remoteopts1 optnl
203 257add31 2020-09-09 stsp ;
204 257add31 2020-09-09 stsp remoteopts1 : REPOSITORY STRING {
205 abc59930 2021-09-05 naddy remote->repository = $2;
206 257add31 2020-09-09 stsp }
207 abc59930 2021-09-05 naddy | SERVER STRING {
208 abc59930 2021-09-05 naddy remote->server = $2;
209 abc59930 2021-09-05 naddy }
210 257add31 2020-09-09 stsp | PROTOCOL STRING {
211 abc59930 2021-09-05 naddy remote->protocol = $2;
212 257add31 2020-09-09 stsp }
213 257add31 2020-09-09 stsp | MIRROR_REFERENCES boolean {
214 257add31 2020-09-09 stsp remote->mirror_references = $2;
215 0c8b29c5 2021-01-05 stsp }
216 0c8b29c5 2021-01-05 stsp | FETCH_ALL_BRANCHES boolean {
217 0c8b29c5 2021-01-05 stsp remote->fetch_all_branches = $2;
218 257add31 2020-09-09 stsp }
219 257add31 2020-09-09 stsp | PORT portplain {
220 257add31 2020-09-09 stsp remote->port = $2;
221 b8adfa55 2020-09-25 stsp }
222 b8adfa55 2020-09-25 stsp | BRANCH branch {
223 b8adfa55 2020-09-25 stsp remote->branch = $2;
224 99495ddb 2021-01-10 stsp }
225 99495ddb 2021-01-10 stsp | REFERENCE ref {
226 6480c871 2021-08-30 stsp remote->fetch_ref = $2;
227 cfd92333 2021-08-27 tracey }
228 0ff2bf46 2021-08-27 tracey | FETCH {
229 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
230 0ff2bf46 2021-08-27 tracey
231 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL) {
232 0ff2bf46 2021-08-27 tracey yyerror("fetch block already exists");
233 0ff2bf46 2021-08-27 tracey YYERROR;
234 0ff2bf46 2021-08-27 tracey }
235 aaf30ee7 2021-08-29 stsp error = new_fetch_config(&remote->fetch_config);
236 0ff2bf46 2021-08-27 tracey if (error) {
237 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
238 0ff2bf46 2021-08-27 tracey YYERROR;
239 0ff2bf46 2021-08-27 tracey }
240 f08eaca0 2021-08-30 tracey } '{' optnl fetchempty '}'
241 0ff2bf46 2021-08-27 tracey | SEND {
242 0ff2bf46 2021-08-27 tracey static const struct got_error* error;
243 0ff2bf46 2021-08-27 tracey
244 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL) {
245 0ff2bf46 2021-08-27 tracey yyerror("send block already exists");
246 0ff2bf46 2021-08-27 tracey YYERROR;
247 0ff2bf46 2021-08-27 tracey }
248 aaf30ee7 2021-08-29 stsp error = new_send_config(&remote->send_config);
249 0ff2bf46 2021-08-27 tracey if (error) {
250 0ff2bf46 2021-08-27 tracey yyerror("%s", error->msg);
251 0ff2bf46 2021-08-27 tracey YYERROR;
252 0ff2bf46 2021-08-27 tracey }
253 f08eaca0 2021-08-30 tracey } '{' optnl sendempty '}'
254 abc59930 2021-09-05 naddy ;
255 f08eaca0 2021-08-30 tracey fetchempty : /* empty */
256 f08eaca0 2021-08-30 tracey | fetchopts2
257 f08eaca0 2021-08-30 tracey ;
258 cfd92333 2021-08-27 tracey fetchopts2 : fetchopts2 fetchopts1 nl
259 abc59930 2021-09-05 naddy | fetchopts1 optnl
260 cfd92333 2021-08-27 tracey ;
261 92952c0e 2021-08-30 stsp fetchopts1 : REPOSITORY STRING {
262 abc59930 2021-09-05 naddy remote->fetch_config->repository = $2;
263 abc59930 2021-09-05 naddy }
264 abc59930 2021-09-05 naddy | SERVER STRING {
265 abc59930 2021-09-05 naddy remote->fetch_config->server = $2;
266 cfd92333 2021-08-27 tracey }
267 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
268 abc59930 2021-09-05 naddy remote->fetch_config->protocol = $2;
269 cfd92333 2021-08-27 tracey }
270 cfd92333 2021-08-27 tracey | PORT portplain {
271 aaf30ee7 2021-08-29 stsp remote->fetch_config->port = $2;
272 cfd92333 2021-08-27 tracey }
273 cfd92333 2021-08-27 tracey | BRANCH branch {
274 aaf30ee7 2021-08-29 stsp remote->fetch_config->branch = $2;
275 cfd92333 2021-08-27 tracey }
276 abc59930 2021-09-05 naddy ;
277 f08eaca0 2021-08-30 tracey sendempty : /* empty */
278 f08eaca0 2021-08-30 tracey | sendopts2
279 f08eaca0 2021-08-30 tracey ;
280 cfd92333 2021-08-27 tracey sendopts2 : sendopts2 sendopts1 nl
281 abc59930 2021-09-05 naddy | sendopts1 optnl
282 cfd92333 2021-08-27 tracey ;
283 92952c0e 2021-08-30 stsp sendopts1 : REPOSITORY STRING {
284 abc59930 2021-09-05 naddy remote->send_config->repository = $2;
285 257add31 2020-09-09 stsp }
286 abc59930 2021-09-05 naddy | SERVER STRING {
287 abc59930 2021-09-05 naddy remote->send_config->server = $2;
288 abc59930 2021-09-05 naddy }
289 cfd92333 2021-08-27 tracey | PROTOCOL STRING {
290 abc59930 2021-09-05 naddy remote->send_config->protocol = $2;
291 cfd92333 2021-08-27 tracey }
292 cfd92333 2021-08-27 tracey | PORT portplain {
293 aaf30ee7 2021-08-29 stsp remote->send_config->port = $2;
294 cfd92333 2021-08-27 tracey }
295 cfd92333 2021-08-27 tracey | BRANCH branch {
296 aaf30ee7 2021-08-29 stsp remote->send_config->branch = $2;
297 cfd92333 2021-08-27 tracey }
298 abc59930 2021-09-05 naddy ;
299 257add31 2020-09-09 stsp remote : REMOTE STRING {
300 257add31 2020-09-09 stsp static const struct got_error* error;
301 257add31 2020-09-09 stsp
302 257add31 2020-09-09 stsp error = new_remote(&remote);
303 257add31 2020-09-09 stsp if (error) {
304 257add31 2020-09-09 stsp free($2);
305 257add31 2020-09-09 stsp yyerror("%s", error->msg);
306 257add31 2020-09-09 stsp YYERROR;
307 257add31 2020-09-09 stsp }
308 c2d7bc3f 2021-08-31 stsp remote->name = $2;
309 257add31 2020-09-09 stsp } '{' optnl remoteopts2 '}' {
310 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
311 257add31 2020-09-09 stsp gotconfig.nremotes++;
312 257add31 2020-09-09 stsp }
313 257add31 2020-09-09 stsp ;
314 257add31 2020-09-09 stsp author : AUTHOR STRING {
315 abc59930 2021-09-05 naddy gotconfig.author = $2;
316 871bd038 2022-07-03 thomas }
317 871bd038 2022-07-03 thomas ;
318 871bd038 2022-07-03 thomas allowed_signers : ALLOWED_SIGNERS STRING {
319 871bd038 2022-07-03 thomas gotconfig.allowed_signers_file = $2;
320 871bd038 2022-07-03 thomas }
321 871bd038 2022-07-03 thomas ;
322 871bd038 2022-07-03 thomas revoked_signers : REVOKED_SIGNERS STRING {
323 871bd038 2022-07-03 thomas gotconfig.revoked_signers_file = $2;
324 257add31 2020-09-09 stsp }
325 257add31 2020-09-09 stsp ;
326 ff5e1f09 2022-07-06 thomas signer_id : SIGNER_ID STRING {
327 ff5e1f09 2022-07-06 thomas gotconfig.signer_id = $2;
328 ff5e1f09 2022-07-06 thomas }
329 ff5e1f09 2022-07-06 thomas ;
330 257add31 2020-09-09 stsp optnl : '\n' optnl
331 257add31 2020-09-09 stsp | /* empty */
332 257add31 2020-09-09 stsp ;
333 257add31 2020-09-09 stsp nl : '\n' optnl
334 b8adfa55 2020-09-25 stsp ;
335 b8adfa55 2020-09-25 stsp comma : ','
336 b8adfa55 2020-09-25 stsp | /* empty */
337 257add31 2020-09-09 stsp ;
338 257add31 2020-09-09 stsp %%
339 257add31 2020-09-09 stsp
340 257add31 2020-09-09 stsp struct keywords {
341 257add31 2020-09-09 stsp const char *k_name;
342 257add31 2020-09-09 stsp int k_val;
343 257add31 2020-09-09 stsp };
344 257add31 2020-09-09 stsp
345 257add31 2020-09-09 stsp int
346 257add31 2020-09-09 stsp yyerror(const char *fmt, ...)
347 257add31 2020-09-09 stsp {
348 257add31 2020-09-09 stsp va_list ap;
349 257add31 2020-09-09 stsp char *msg;
350 257add31 2020-09-09 stsp char *err = NULL;
351 257add31 2020-09-09 stsp
352 257add31 2020-09-09 stsp va_start(ap, fmt);
353 257add31 2020-09-09 stsp if (vasprintf(&msg, fmt, ap) == -1) {
354 257add31 2020-09-09 stsp gerror = got_error_from_errno("vasprintf");
355 257add31 2020-09-09 stsp return 0;
356 257add31 2020-09-09 stsp }
357 257add31 2020-09-09 stsp va_end(ap);
358 257add31 2020-09-09 stsp if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
359 257add31 2020-09-09 stsp msg) == -1) {
360 257add31 2020-09-09 stsp gerror = got_error_from_errno("asprintf");
361 257add31 2020-09-09 stsp return(0);
362 257add31 2020-09-09 stsp }
363 c2d7bc3f 2021-08-31 stsp gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, err);
364 257add31 2020-09-09 stsp free(msg);
365 257add31 2020-09-09 stsp return(0);
366 257add31 2020-09-09 stsp }
367 257add31 2020-09-09 stsp int
368 257add31 2020-09-09 stsp kw_cmp(const void *k, const void *e)
369 257add31 2020-09-09 stsp {
370 257add31 2020-09-09 stsp return (strcmp(k, ((const struct keywords *)e)->k_name));
371 257add31 2020-09-09 stsp }
372 257add31 2020-09-09 stsp
373 257add31 2020-09-09 stsp int
374 257add31 2020-09-09 stsp lookup(char *s)
375 257add31 2020-09-09 stsp {
376 257add31 2020-09-09 stsp /* This has to be sorted always. */
377 257add31 2020-09-09 stsp static const struct keywords keywords[] = {
378 871bd038 2022-07-03 thomas {"allowed_signers", ALLOWED_SIGNERS},
379 257add31 2020-09-09 stsp {"author", AUTHOR},
380 b8adfa55 2020-09-25 stsp {"branch", BRANCH},
381 cfd92333 2021-08-27 tracey {"fetch", FETCH},
382 25eb5847 2022-07-03 thomas {"fetch-all-branches", FETCH_ALL_BRANCHES}, /* deprecated */
383 25eb5847 2022-07-03 thomas {"fetch_all_branches", FETCH_ALL_BRANCHES},
384 459c9b5d 2022-07-03 thomas {"mirror-references", MIRROR_REFERENCES}, /* deprecated */
385 459c9b5d 2022-07-03 thomas {"mirror_references", MIRROR_REFERENCES},
386 257add31 2020-09-09 stsp {"port", PORT},
387 257add31 2020-09-09 stsp {"protocol", PROTOCOL},
388 99495ddb 2021-01-10 stsp {"reference", REFERENCE},
389 257add31 2020-09-09 stsp {"remote", REMOTE},
390 257add31 2020-09-09 stsp {"repository", REPOSITORY},
391 871bd038 2022-07-03 thomas {"revoked_signers", REVOKED_SIGNERS},
392 cfd92333 2021-08-27 tracey {"send", SEND},
393 257add31 2020-09-09 stsp {"server", SERVER},
394 ff5e1f09 2022-07-06 thomas {"signer_id", SIGNER_ID},
395 257add31 2020-09-09 stsp };
396 257add31 2020-09-09 stsp const struct keywords *p;
397 257add31 2020-09-09 stsp
398 257add31 2020-09-09 stsp p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
399 257add31 2020-09-09 stsp sizeof(keywords[0]), kw_cmp);
400 257add31 2020-09-09 stsp
401 257add31 2020-09-09 stsp if (p)
402 257add31 2020-09-09 stsp return (p->k_val);
403 257add31 2020-09-09 stsp else
404 257add31 2020-09-09 stsp return (STRING);
405 257add31 2020-09-09 stsp }
406 257add31 2020-09-09 stsp
407 257add31 2020-09-09 stsp #define START_EXPAND 1
408 257add31 2020-09-09 stsp #define DONE_EXPAND 2
409 257add31 2020-09-09 stsp
410 257add31 2020-09-09 stsp static int expanding;
411 257add31 2020-09-09 stsp
412 257add31 2020-09-09 stsp int
413 257add31 2020-09-09 stsp igetc(void)
414 257add31 2020-09-09 stsp {
415 257add31 2020-09-09 stsp int c;
416 257add31 2020-09-09 stsp
417 257add31 2020-09-09 stsp while (1) {
418 257add31 2020-09-09 stsp if (file->ungetpos > 0)
419 257add31 2020-09-09 stsp c = file->ungetbuf[--file->ungetpos];
420 257add31 2020-09-09 stsp else
421 257add31 2020-09-09 stsp c = getc(file->stream);
422 257add31 2020-09-09 stsp
423 257add31 2020-09-09 stsp if (c == START_EXPAND)
424 257add31 2020-09-09 stsp expanding = 1;
425 257add31 2020-09-09 stsp else if (c == DONE_EXPAND)
426 257add31 2020-09-09 stsp expanding = 0;
427 257add31 2020-09-09 stsp else
428 257add31 2020-09-09 stsp break;
429 257add31 2020-09-09 stsp }
430 257add31 2020-09-09 stsp return (c);
431 257add31 2020-09-09 stsp }
432 257add31 2020-09-09 stsp
433 257add31 2020-09-09 stsp int
434 257add31 2020-09-09 stsp lgetc(int quotec)
435 257add31 2020-09-09 stsp {
436 257add31 2020-09-09 stsp int c, next;
437 257add31 2020-09-09 stsp
438 257add31 2020-09-09 stsp if (quotec) {
439 257add31 2020-09-09 stsp c = igetc();
440 257add31 2020-09-09 stsp if (c == EOF) {
441 257add31 2020-09-09 stsp yyerror("reached end of file while parsing "
442 257add31 2020-09-09 stsp "quoted string");
443 257add31 2020-09-09 stsp }
444 257add31 2020-09-09 stsp return (c);
445 257add31 2020-09-09 stsp }
446 257add31 2020-09-09 stsp
447 257add31 2020-09-09 stsp c = igetc();
448 257add31 2020-09-09 stsp while (c == '\\') {
449 257add31 2020-09-09 stsp next = igetc();
450 257add31 2020-09-09 stsp if (next != '\n') {
451 257add31 2020-09-09 stsp c = next;
452 257add31 2020-09-09 stsp break;
453 257add31 2020-09-09 stsp }
454 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
455 257add31 2020-09-09 stsp file->lineno++;
456 257add31 2020-09-09 stsp }
457 257add31 2020-09-09 stsp
458 257add31 2020-09-09 stsp return (c);
459 257add31 2020-09-09 stsp }
460 257add31 2020-09-09 stsp
461 257add31 2020-09-09 stsp void
462 257add31 2020-09-09 stsp lungetc(int c)
463 257add31 2020-09-09 stsp {
464 257add31 2020-09-09 stsp if (c == EOF)
465 257add31 2020-09-09 stsp return;
466 257add31 2020-09-09 stsp
467 257add31 2020-09-09 stsp if (file->ungetpos >= file->ungetsize) {
468 257add31 2020-09-09 stsp void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
469 257add31 2020-09-09 stsp if (p == NULL)
470 257add31 2020-09-09 stsp err(1, "%s", __func__);
471 257add31 2020-09-09 stsp file->ungetbuf = p;
472 257add31 2020-09-09 stsp file->ungetsize *= 2;
473 257add31 2020-09-09 stsp }
474 257add31 2020-09-09 stsp file->ungetbuf[file->ungetpos++] = c;
475 257add31 2020-09-09 stsp }
476 257add31 2020-09-09 stsp
477 257add31 2020-09-09 stsp int
478 257add31 2020-09-09 stsp findeol(void)
479 257add31 2020-09-09 stsp {
480 257add31 2020-09-09 stsp int c;
481 257add31 2020-09-09 stsp
482 257add31 2020-09-09 stsp /* Skip to either EOF or the first real EOL. */
483 257add31 2020-09-09 stsp while (1) {
484 257add31 2020-09-09 stsp c = lgetc(0);
485 257add31 2020-09-09 stsp if (c == '\n') {
486 257add31 2020-09-09 stsp file->lineno++;
487 257add31 2020-09-09 stsp break;
488 257add31 2020-09-09 stsp }
489 257add31 2020-09-09 stsp if (c == EOF)
490 257add31 2020-09-09 stsp break;
491 257add31 2020-09-09 stsp }
492 257add31 2020-09-09 stsp return (ERROR);
493 257add31 2020-09-09 stsp }
494 257add31 2020-09-09 stsp
495 257add31 2020-09-09 stsp static long long
496 257add31 2020-09-09 stsp getservice(char *n)
497 257add31 2020-09-09 stsp {
498 257add31 2020-09-09 stsp struct servent *s;
499 257add31 2020-09-09 stsp u_long ulval;
500 257add31 2020-09-09 stsp
501 257add31 2020-09-09 stsp if (atoul(n, &ulval) == 0) {
502 7c84ef07 2021-08-29 stsp if (ulval == 0 || ulval > 65535) {
503 257add31 2020-09-09 stsp yyerror("illegal port value %lu", ulval);
504 257add31 2020-09-09 stsp return (-1);
505 257add31 2020-09-09 stsp }
506 257add31 2020-09-09 stsp return ulval;
507 257add31 2020-09-09 stsp } else {
508 257add31 2020-09-09 stsp s = getservbyname(n, "tcp");
509 257add31 2020-09-09 stsp if (s == NULL)
510 257add31 2020-09-09 stsp s = getservbyname(n, "udp");
511 257add31 2020-09-09 stsp if (s == NULL) {
512 257add31 2020-09-09 stsp yyerror("unknown port %s", n);
513 257add31 2020-09-09 stsp return (-1);
514 257add31 2020-09-09 stsp }
515 257add31 2020-09-09 stsp return (s->s_port);
516 257add31 2020-09-09 stsp }
517 257add31 2020-09-09 stsp }
518 257add31 2020-09-09 stsp
519 257add31 2020-09-09 stsp static int
520 257add31 2020-09-09 stsp parseport(char *port, long long *pn)
521 257add31 2020-09-09 stsp {
522 257add31 2020-09-09 stsp if ((*pn = getservice(port)) == -1) {
523 257add31 2020-09-09 stsp *pn = 0LL;
524 257add31 2020-09-09 stsp return (-1);
525 257add31 2020-09-09 stsp }
526 257add31 2020-09-09 stsp return (0);
527 257add31 2020-09-09 stsp }
528 257add31 2020-09-09 stsp
529 257add31 2020-09-09 stsp
530 257add31 2020-09-09 stsp int
531 257add31 2020-09-09 stsp yylex(void)
532 257add31 2020-09-09 stsp {
533 1a670123 2021-09-28 thomas char buf[8096];
534 1a670123 2021-09-28 thomas char *p, *val;
535 1a670123 2021-09-28 thomas int quotec, next, c;
536 1a670123 2021-09-28 thomas int token;
537 257add31 2020-09-09 stsp
538 257add31 2020-09-09 stsp top:
539 257add31 2020-09-09 stsp p = buf;
540 257add31 2020-09-09 stsp c = lgetc(0);
541 257add31 2020-09-09 stsp while (c == ' ' || c == '\t')
542 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
543 257add31 2020-09-09 stsp
544 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
545 257add31 2020-09-09 stsp if (c == '#') {
546 257add31 2020-09-09 stsp c = lgetc(0);
547 257add31 2020-09-09 stsp while (c != '\n' && c != EOF)
548 257add31 2020-09-09 stsp c = lgetc(0); /* nothing */
549 257add31 2020-09-09 stsp }
550 257add31 2020-09-09 stsp if (c == '$' && !expanding) {
551 257add31 2020-09-09 stsp while (1) {
552 257add31 2020-09-09 stsp c = lgetc(0);
553 257add31 2020-09-09 stsp if (c == EOF)
554 257add31 2020-09-09 stsp return (0);
555 257add31 2020-09-09 stsp
556 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
557 257add31 2020-09-09 stsp yyerror("string too long");
558 257add31 2020-09-09 stsp return (findeol());
559 257add31 2020-09-09 stsp }
560 257add31 2020-09-09 stsp if (isalnum(c) || c == '_') {
561 257add31 2020-09-09 stsp *p++ = c;
562 257add31 2020-09-09 stsp continue;
563 257add31 2020-09-09 stsp }
564 257add31 2020-09-09 stsp *p = '\0';
565 257add31 2020-09-09 stsp lungetc(c);
566 257add31 2020-09-09 stsp break;
567 257add31 2020-09-09 stsp }
568 257add31 2020-09-09 stsp val = symget(buf);
569 257add31 2020-09-09 stsp if (val == NULL) {
570 257add31 2020-09-09 stsp yyerror("macro '%s' not defined", buf);
571 257add31 2020-09-09 stsp return (findeol());
572 257add31 2020-09-09 stsp }
573 257add31 2020-09-09 stsp p = val + strlen(val) - 1;
574 257add31 2020-09-09 stsp lungetc(DONE_EXPAND);
575 257add31 2020-09-09 stsp while (p >= val) {
576 bb27d0d1 2021-10-15 thomas lungetc((unsigned char)*p);
577 257add31 2020-09-09 stsp p--;
578 257add31 2020-09-09 stsp }
579 257add31 2020-09-09 stsp lungetc(START_EXPAND);
580 257add31 2020-09-09 stsp goto top;
581 257add31 2020-09-09 stsp }
582 257add31 2020-09-09 stsp
583 257add31 2020-09-09 stsp switch (c) {
584 257add31 2020-09-09 stsp case '\'':
585 257add31 2020-09-09 stsp case '"':
586 257add31 2020-09-09 stsp quotec = c;
587 257add31 2020-09-09 stsp while (1) {
588 257add31 2020-09-09 stsp c = lgetc(quotec);
589 257add31 2020-09-09 stsp if (c == EOF)
590 257add31 2020-09-09 stsp return (0);
591 257add31 2020-09-09 stsp if (c == '\n') {
592 257add31 2020-09-09 stsp file->lineno++;
593 257add31 2020-09-09 stsp continue;
594 257add31 2020-09-09 stsp } else if (c == '\\') {
595 257add31 2020-09-09 stsp next = lgetc(quotec);
596 257add31 2020-09-09 stsp if (next == EOF)
597 257add31 2020-09-09 stsp return (0);
598 257add31 2020-09-09 stsp if (next == quotec || c == ' ' || c == '\t')
599 257add31 2020-09-09 stsp c = next;
600 257add31 2020-09-09 stsp else if (next == '\n') {
601 257add31 2020-09-09 stsp file->lineno++;
602 257add31 2020-09-09 stsp continue;
603 257add31 2020-09-09 stsp } else
604 257add31 2020-09-09 stsp lungetc(next);
605 257add31 2020-09-09 stsp } else if (c == quotec) {
606 257add31 2020-09-09 stsp *p = '\0';
607 257add31 2020-09-09 stsp break;
608 257add31 2020-09-09 stsp } else if (c == '\0') {
609 257add31 2020-09-09 stsp yyerror("syntax error");
610 257add31 2020-09-09 stsp return (findeol());
611 257add31 2020-09-09 stsp }
612 257add31 2020-09-09 stsp if (p + 1 >= buf + sizeof(buf) - 1) {
613 257add31 2020-09-09 stsp yyerror("string too long");
614 257add31 2020-09-09 stsp return (findeol());
615 257add31 2020-09-09 stsp }
616 257add31 2020-09-09 stsp *p++ = c;
617 257add31 2020-09-09 stsp }
618 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
619 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
620 257add31 2020-09-09 stsp err(1, "%s", __func__);
621 257add31 2020-09-09 stsp return (STRING);
622 257add31 2020-09-09 stsp }
623 257add31 2020-09-09 stsp
624 257add31 2020-09-09 stsp #define allowed_to_end_number(x) \
625 257add31 2020-09-09 stsp (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
626 257add31 2020-09-09 stsp
627 257add31 2020-09-09 stsp if (c == '-' || isdigit(c)) {
628 257add31 2020-09-09 stsp do {
629 257add31 2020-09-09 stsp *p++ = c;
630 ccd081e7 2021-09-30 thomas if ((size_t)(p-buf) >= sizeof(buf)) {
631 257add31 2020-09-09 stsp yyerror("string too long");
632 257add31 2020-09-09 stsp return (findeol());
633 257add31 2020-09-09 stsp }
634 257add31 2020-09-09 stsp c = lgetc(0);
635 257add31 2020-09-09 stsp } while (c != EOF && isdigit(c));
636 257add31 2020-09-09 stsp lungetc(c);
637 257add31 2020-09-09 stsp if (p == buf + 1 && buf[0] == '-')
638 257add31 2020-09-09 stsp goto nodigits;
639 257add31 2020-09-09 stsp if (c == EOF || allowed_to_end_number(c)) {
640 257add31 2020-09-09 stsp const char *errstr = NULL;
641 257add31 2020-09-09 stsp
642 257add31 2020-09-09 stsp *p = '\0';
643 257add31 2020-09-09 stsp yylval.v.number = strtonum(buf, LLONG_MIN,
644 257add31 2020-09-09 stsp LLONG_MAX, &errstr);
645 257add31 2020-09-09 stsp if (errstr) {
646 257add31 2020-09-09 stsp yyerror("\"%s\" invalid number: %s",
647 257add31 2020-09-09 stsp buf, errstr);
648 257add31 2020-09-09 stsp return (findeol());
649 257add31 2020-09-09 stsp }
650 257add31 2020-09-09 stsp return (NUMBER);
651 257add31 2020-09-09 stsp } else {
652 257add31 2020-09-09 stsp nodigits:
653 257add31 2020-09-09 stsp while (p > buf + 1)
654 bb27d0d1 2021-10-15 thomas lungetc((unsigned char)*--p);
655 bb27d0d1 2021-10-15 thomas c = (unsigned char)*--p;
656 257add31 2020-09-09 stsp if (c == '-')
657 257add31 2020-09-09 stsp return (c);
658 257add31 2020-09-09 stsp }
659 257add31 2020-09-09 stsp }
660 257add31 2020-09-09 stsp
661 257add31 2020-09-09 stsp #define allowed_in_string(x) \
662 257add31 2020-09-09 stsp (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
663 257add31 2020-09-09 stsp x != '{' && x != '}' && \
664 257add31 2020-09-09 stsp x != '!' && x != '=' && x != '#' && \
665 257add31 2020-09-09 stsp x != ','))
666 257add31 2020-09-09 stsp
667 257add31 2020-09-09 stsp if (isalnum(c) || c == ':' || c == '_') {
668 257add31 2020-09-09 stsp do {
669 257add31 2020-09-09 stsp *p++ = c;
670 ccd081e7 2021-09-30 thomas if ((size_t)(p-buf) >= sizeof(buf)) {
671 257add31 2020-09-09 stsp yyerror("string too long");
672 257add31 2020-09-09 stsp return (findeol());
673 257add31 2020-09-09 stsp }
674 257add31 2020-09-09 stsp c = lgetc(0);
675 257add31 2020-09-09 stsp } while (c != EOF && (allowed_in_string(c)));
676 257add31 2020-09-09 stsp lungetc(c);
677 257add31 2020-09-09 stsp *p = '\0';
678 257add31 2020-09-09 stsp token = lookup(buf);
679 257add31 2020-09-09 stsp if (token == STRING) {
680 257add31 2020-09-09 stsp yylval.v.string = strdup(buf);
681 257add31 2020-09-09 stsp if (yylval.v.string == NULL)
682 257add31 2020-09-09 stsp err(1, "%s", __func__);
683 257add31 2020-09-09 stsp }
684 257add31 2020-09-09 stsp return (token);
685 257add31 2020-09-09 stsp }
686 257add31 2020-09-09 stsp if (c == '\n') {
687 257add31 2020-09-09 stsp yylval.lineno = file->lineno;
688 257add31 2020-09-09 stsp file->lineno++;
689 257add31 2020-09-09 stsp }
690 257add31 2020-09-09 stsp if (c == EOF)
691 257add31 2020-09-09 stsp return (0);
692 257add31 2020-09-09 stsp return (c);
693 257add31 2020-09-09 stsp }
694 257add31 2020-09-09 stsp
695 257add31 2020-09-09 stsp static const struct got_error*
696 257add31 2020-09-09 stsp newfile(struct file **nfile, const char *filename, int *fd)
697 257add31 2020-09-09 stsp {
698 257add31 2020-09-09 stsp const struct got_error* error = NULL;
699 257add31 2020-09-09 stsp
700 257add31 2020-09-09 stsp (*nfile) = calloc(1, sizeof(struct file));
701 257add31 2020-09-09 stsp if ((*nfile) == NULL)
702 257add31 2020-09-09 stsp return got_error_from_errno("calloc");
703 257add31 2020-09-09 stsp (*nfile)->stream = fdopen(*fd, "r");
704 257add31 2020-09-09 stsp if ((*nfile)->stream == NULL) {
705 257add31 2020-09-09 stsp error = got_error_from_errno("fdopen");
706 257add31 2020-09-09 stsp free((*nfile));
707 257add31 2020-09-09 stsp return error;
708 257add31 2020-09-09 stsp }
709 257add31 2020-09-09 stsp *fd = -1; /* Stream owns the file descriptor now. */
710 257add31 2020-09-09 stsp (*nfile)->name = filename;
711 257add31 2020-09-09 stsp (*nfile)->lineno = 1;
712 257add31 2020-09-09 stsp (*nfile)->ungetsize = 16;
713 257add31 2020-09-09 stsp (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
714 257add31 2020-09-09 stsp if ((*nfile)->ungetbuf == NULL) {
715 257add31 2020-09-09 stsp error = got_error_from_errno("malloc");
716 257add31 2020-09-09 stsp fclose((*nfile)->stream);
717 257add31 2020-09-09 stsp free((*nfile));
718 257add31 2020-09-09 stsp return error;
719 257add31 2020-09-09 stsp }
720 257add31 2020-09-09 stsp return NULL;
721 257add31 2020-09-09 stsp }
722 257add31 2020-09-09 stsp
723 257add31 2020-09-09 stsp static const struct got_error*
724 257add31 2020-09-09 stsp new_remote(struct gotconfig_remote_repo **remote)
725 257add31 2020-09-09 stsp {
726 257add31 2020-09-09 stsp const struct got_error *error = NULL;
727 257add31 2020-09-09 stsp
728 257add31 2020-09-09 stsp *remote = calloc(1, sizeof(**remote));
729 257add31 2020-09-09 stsp if (*remote == NULL)
730 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
731 cfd92333 2021-08-27 tracey return error;
732 cfd92333 2021-08-27 tracey }
733 cfd92333 2021-08-27 tracey
734 cfd92333 2021-08-27 tracey static const struct got_error*
735 aaf30ee7 2021-08-29 stsp new_fetch_config(struct fetch_config **fetch_config)
736 cfd92333 2021-08-27 tracey {
737 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
738 cfd92333 2021-08-27 tracey
739 aaf30ee7 2021-08-29 stsp *fetch_config = calloc(1, sizeof(**fetch_config));
740 aaf30ee7 2021-08-29 stsp if (*fetch_config == NULL)
741 cfd92333 2021-08-27 tracey error = got_error_from_errno("calloc");
742 cfd92333 2021-08-27 tracey return error;
743 cfd92333 2021-08-27 tracey }
744 cfd92333 2021-08-27 tracey
745 cfd92333 2021-08-27 tracey static const struct got_error*
746 aaf30ee7 2021-08-29 stsp new_send_config(struct send_config **send_config)
747 cfd92333 2021-08-27 tracey {
748 cfd92333 2021-08-27 tracey const struct got_error *error = NULL;
749 cfd92333 2021-08-27 tracey
750 aaf30ee7 2021-08-29 stsp *send_config = calloc(1, sizeof(**send_config));
751 aaf30ee7 2021-08-29 stsp if (*send_config == NULL)
752 257add31 2020-09-09 stsp error = got_error_from_errno("calloc");
753 257add31 2020-09-09 stsp return error;
754 257add31 2020-09-09 stsp }
755 257add31 2020-09-09 stsp
756 257add31 2020-09-09 stsp static void
757 257add31 2020-09-09 stsp closefile(struct file *file)
758 257add31 2020-09-09 stsp {
759 257add31 2020-09-09 stsp fclose(file->stream);
760 257add31 2020-09-09 stsp free(file->ungetbuf);
761 257add31 2020-09-09 stsp free(file);
762 257add31 2020-09-09 stsp }
763 257add31 2020-09-09 stsp
764 257add31 2020-09-09 stsp const struct got_error *
765 257add31 2020-09-09 stsp gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
766 257add31 2020-09-09 stsp {
767 257add31 2020-09-09 stsp const struct got_error *err = NULL;
768 257add31 2020-09-09 stsp struct sym *sym, *next;
769 257add31 2020-09-09 stsp
770 257add31 2020-09-09 stsp *conf = NULL;
771 257add31 2020-09-09 stsp
772 257add31 2020-09-09 stsp err = newfile(&file, filename, fd);
773 257add31 2020-09-09 stsp if (err)
774 257add31 2020-09-09 stsp return err;
775 257add31 2020-09-09 stsp
776 257add31 2020-09-09 stsp TAILQ_INIT(&gotconfig.remotes);
777 257add31 2020-09-09 stsp
778 257add31 2020-09-09 stsp yyparse();
779 257add31 2020-09-09 stsp closefile(file);
780 257add31 2020-09-09 stsp
781 257add31 2020-09-09 stsp /* Free macros and check which have not been used. */
782 257add31 2020-09-09 stsp TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
783 257add31 2020-09-09 stsp if (!sym->persist) {
784 257add31 2020-09-09 stsp free(sym->nam);
785 257add31 2020-09-09 stsp free(sym->val);
786 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
787 257add31 2020-09-09 stsp free(sym);
788 257add31 2020-09-09 stsp }
789 257add31 2020-09-09 stsp }
790 257add31 2020-09-09 stsp
791 257add31 2020-09-09 stsp if (gerror == NULL)
792 257add31 2020-09-09 stsp *conf = &gotconfig;
793 257add31 2020-09-09 stsp return gerror;
794 257add31 2020-09-09 stsp }
795 257add31 2020-09-09 stsp
796 aaf30ee7 2021-08-29 stsp static void
797 aaf30ee7 2021-08-29 stsp free_fetch_config(struct fetch_config *fetch_config)
798 aaf30ee7 2021-08-29 stsp {
799 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->repository);
800 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->server);
801 aaf30ee7 2021-08-29 stsp free(remote->fetch_config->protocol);
802 aaf30ee7 2021-08-29 stsp free(remote->fetch_config);
803 aaf30ee7 2021-08-29 stsp }
804 aaf30ee7 2021-08-29 stsp
805 aaf30ee7 2021-08-29 stsp static void
806 aaf30ee7 2021-08-29 stsp free_send_config(struct send_config *send_config)
807 aaf30ee7 2021-08-29 stsp {
808 aaf30ee7 2021-08-29 stsp free(remote->send_config->repository);
809 aaf30ee7 2021-08-29 stsp free(remote->send_config->server);
810 aaf30ee7 2021-08-29 stsp free(remote->send_config->protocol);
811 aaf30ee7 2021-08-29 stsp free(remote->send_config);
812 aaf30ee7 2021-08-29 stsp }
813 aaf30ee7 2021-08-29 stsp
814 257add31 2020-09-09 stsp void
815 257add31 2020-09-09 stsp gotconfig_free(struct gotconfig *conf)
816 257add31 2020-09-09 stsp {
817 257add31 2020-09-09 stsp struct gotconfig_remote_repo *remote;
818 257add31 2020-09-09 stsp
819 257add31 2020-09-09 stsp free(conf->author);
820 871bd038 2022-07-03 thomas free(conf->allowed_signers_file);
821 871bd038 2022-07-03 thomas free(conf->revoked_signers_file);
822 ff5e1f09 2022-07-06 thomas free(conf->signer_id);
823 257add31 2020-09-09 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
824 257add31 2020-09-09 stsp remote = TAILQ_FIRST(&conf->remotes);
825 257add31 2020-09-09 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
826 aaf30ee7 2021-08-29 stsp if (remote->fetch_config != NULL)
827 aaf30ee7 2021-08-29 stsp free_fetch_config(remote->fetch_config);
828 aaf30ee7 2021-08-29 stsp if (remote->send_config != NULL)
829 aaf30ee7 2021-08-29 stsp free_send_config(remote->send_config);
830 257add31 2020-09-09 stsp free(remote->name);
831 257add31 2020-09-09 stsp free(remote->repository);
832 257add31 2020-09-09 stsp free(remote->server);
833 257add31 2020-09-09 stsp free(remote->protocol);
834 257add31 2020-09-09 stsp free(remote);
835 257add31 2020-09-09 stsp }
836 257add31 2020-09-09 stsp }
837 257add31 2020-09-09 stsp
838 257add31 2020-09-09 stsp int
839 257add31 2020-09-09 stsp symset(const char *nam, const char *val, int persist)
840 257add31 2020-09-09 stsp {
841 257add31 2020-09-09 stsp struct sym *sym;
842 257add31 2020-09-09 stsp
843 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
844 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0)
845 257add31 2020-09-09 stsp break;
846 257add31 2020-09-09 stsp }
847 257add31 2020-09-09 stsp
848 257add31 2020-09-09 stsp if (sym != NULL) {
849 257add31 2020-09-09 stsp if (sym->persist == 1)
850 257add31 2020-09-09 stsp return (0);
851 257add31 2020-09-09 stsp else {
852 257add31 2020-09-09 stsp free(sym->nam);
853 257add31 2020-09-09 stsp free(sym->val);
854 257add31 2020-09-09 stsp TAILQ_REMOVE(&symhead, sym, entry);
855 257add31 2020-09-09 stsp free(sym);
856 257add31 2020-09-09 stsp }
857 257add31 2020-09-09 stsp }
858 257add31 2020-09-09 stsp sym = calloc(1, sizeof(*sym));
859 257add31 2020-09-09 stsp if (sym == NULL)
860 257add31 2020-09-09 stsp return (-1);
861 257add31 2020-09-09 stsp
862 257add31 2020-09-09 stsp sym->nam = strdup(nam);
863 257add31 2020-09-09 stsp if (sym->nam == NULL) {
864 257add31 2020-09-09 stsp free(sym);
865 257add31 2020-09-09 stsp return (-1);
866 257add31 2020-09-09 stsp }
867 257add31 2020-09-09 stsp sym->val = strdup(val);
868 257add31 2020-09-09 stsp if (sym->val == NULL) {
869 257add31 2020-09-09 stsp free(sym->nam);
870 257add31 2020-09-09 stsp free(sym);
871 257add31 2020-09-09 stsp return (-1);
872 257add31 2020-09-09 stsp }
873 257add31 2020-09-09 stsp sym->used = 0;
874 257add31 2020-09-09 stsp sym->persist = persist;
875 257add31 2020-09-09 stsp TAILQ_INSERT_TAIL(&symhead, sym, entry);
876 257add31 2020-09-09 stsp return (0);
877 257add31 2020-09-09 stsp }
878 257add31 2020-09-09 stsp
879 257add31 2020-09-09 stsp int
880 257add31 2020-09-09 stsp cmdline_symset(char *s)
881 257add31 2020-09-09 stsp {
882 257add31 2020-09-09 stsp char *sym, *val;
883 257add31 2020-09-09 stsp int ret;
884 257add31 2020-09-09 stsp size_t len;
885 257add31 2020-09-09 stsp
886 257add31 2020-09-09 stsp val = strrchr(s, '=');
887 257add31 2020-09-09 stsp if (val == NULL)
888 257add31 2020-09-09 stsp return (-1);
889 257add31 2020-09-09 stsp
890 257add31 2020-09-09 stsp len = strlen(s) - strlen(val) + 1;
891 257add31 2020-09-09 stsp sym = malloc(len);
892 257add31 2020-09-09 stsp if (sym == NULL)
893 257add31 2020-09-09 stsp errx(1, "cmdline_symset: malloc");
894 257add31 2020-09-09 stsp
895 257add31 2020-09-09 stsp strlcpy(sym, s, len);
896 257add31 2020-09-09 stsp
897 257add31 2020-09-09 stsp ret = symset(sym, val + 1, 1);
898 257add31 2020-09-09 stsp free(sym);
899 257add31 2020-09-09 stsp
900 257add31 2020-09-09 stsp return (ret);
901 257add31 2020-09-09 stsp }
902 257add31 2020-09-09 stsp
903 257add31 2020-09-09 stsp char *
904 257add31 2020-09-09 stsp symget(const char *nam)
905 257add31 2020-09-09 stsp {
906 257add31 2020-09-09 stsp struct sym *sym;
907 257add31 2020-09-09 stsp
908 257add31 2020-09-09 stsp TAILQ_FOREACH(sym, &symhead, entry) {
909 257add31 2020-09-09 stsp if (strcmp(nam, sym->nam) == 0) {
910 257add31 2020-09-09 stsp sym->used = 1;
911 257add31 2020-09-09 stsp return (sym->val);
912 257add31 2020-09-09 stsp }
913 257add31 2020-09-09 stsp }
914 257add31 2020-09-09 stsp return (NULL);
915 257add31 2020-09-09 stsp }
916 257add31 2020-09-09 stsp
917 257add31 2020-09-09 stsp static int
918 257add31 2020-09-09 stsp atoul(char *s, u_long *ulvalp)
919 257add31 2020-09-09 stsp {
920 257add31 2020-09-09 stsp u_long ulval;
921 257add31 2020-09-09 stsp char *ep;
922 257add31 2020-09-09 stsp
923 257add31 2020-09-09 stsp errno = 0;
924 257add31 2020-09-09 stsp ulval = strtoul(s, &ep, 0);
925 257add31 2020-09-09 stsp if (s[0] == '\0' || *ep != '\0')
926 257add31 2020-09-09 stsp return (-1);
927 257add31 2020-09-09 stsp if (errno == ERANGE && ulval == ULONG_MAX)
928 257add31 2020-09-09 stsp return (-1);
929 257add31 2020-09-09 stsp *ulvalp = ulval;
930 257add31 2020-09-09 stsp return (0);
931 257add31 2020-09-09 stsp }