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