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