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