Blame


1 fb863fa4 2020-06-22 tracey /*
2 c34ec417 2020-06-22 tracey * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
3 fb863fa4 2020-06-22 tracey * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
4 fb863fa4 2020-06-22 tracey * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
5 fb863fa4 2020-06-22 tracey * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
6 fb863fa4 2020-06-22 tracey * Copyright (c) 2001 Markus Friedl. All rights reserved.
7 fb863fa4 2020-06-22 tracey * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
8 fb863fa4 2020-06-22 tracey * Copyright (c) 2001 Theo de Raadt. All rights reserved.
9 fb863fa4 2020-06-22 tracey *
10 fb863fa4 2020-06-22 tracey * Permission to use, copy, modify, and distribute this software for any
11 fb863fa4 2020-06-22 tracey * purpose with or without fee is hereby granted, provided that the above
12 fb863fa4 2020-06-22 tracey * copyright notice and this permission notice appear in all copies.
13 fb863fa4 2020-06-22 tracey *
14 fb863fa4 2020-06-22 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 fb863fa4 2020-06-22 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 fb863fa4 2020-06-22 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 fb863fa4 2020-06-22 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 fb863fa4 2020-06-22 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 fb863fa4 2020-06-22 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 fb863fa4 2020-06-22 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 fb863fa4 2020-06-22 tracey */
22 fb863fa4 2020-06-22 tracey
23 fb863fa4 2020-06-22 tracey %{
24 fb863fa4 2020-06-22 tracey #include <sys/types.h>
25 fb863fa4 2020-06-22 tracey #include <sys/queue.h>
26 fb863fa4 2020-06-22 tracey #include <sys/socket.h>
27 fb863fa4 2020-06-22 tracey #include <sys/stat.h>
28 fb863fa4 2020-06-22 tracey
29 fb863fa4 2020-06-22 tracey #include <netinet/in.h>
30 fb863fa4 2020-06-22 tracey
31 fb863fa4 2020-06-22 tracey #include <arpa/inet.h>
32 fb863fa4 2020-06-22 tracey
33 fb863fa4 2020-06-22 tracey #include <ctype.h>
34 fb863fa4 2020-06-22 tracey #include <err.h>
35 fb863fa4 2020-06-22 tracey #include <errno.h>
36 fb863fa4 2020-06-22 tracey #include <event.h>
37 fb863fa4 2020-06-22 tracey #include <ifaddrs.h>
38 fb863fa4 2020-06-22 tracey #include <imsg.h>
39 fb863fa4 2020-06-22 tracey #include <limits.h>
40 fb863fa4 2020-06-22 tracey #include <stdarg.h>
41 fb863fa4 2020-06-22 tracey #include <stdio.h>
42 fb863fa4 2020-06-22 tracey #include <string.h>
43 fb863fa4 2020-06-22 tracey #include <syslog.h>
44 fb863fa4 2020-06-22 tracey #include <unistd.h>
45 fb863fa4 2020-06-22 tracey
46 fb863fa4 2020-06-22 tracey #include "got_error.h"
47 fb863fa4 2020-06-22 tracey #include "got.h"
48 fb863fa4 2020-06-22 tracey
49 fb863fa4 2020-06-22 tracey static struct file {
50 fb863fa4 2020-06-22 tracey FILE *stream;
51 fb863fa4 2020-06-22 tracey char *name;
52 fb863fa4 2020-06-22 tracey size_t ungetpos;
53 fb863fa4 2020-06-22 tracey size_t ungetsize;
54 fb863fa4 2020-06-22 tracey u_char *ungetbuf;
55 fb863fa4 2020-06-22 tracey int eof_reached;
56 fb863fa4 2020-06-22 tracey int lineno;
57 6d7260fd 2020-08-03 stsp } *file;
58 6d7260fd 2020-08-03 stsp static const struct got_error* newfile(struct file**, const char *);
59 6d7260fd 2020-08-03 stsp static void closefile(struct file *);
60 fb863fa4 2020-06-22 tracey int yyparse(void);
61 fb863fa4 2020-06-22 tracey int yylex(void);
62 fb863fa4 2020-06-22 tracey int yyerror(const char *, ...)
63 fb863fa4 2020-06-22 tracey __attribute__((__format__ (printf, 1, 2)))
64 fb863fa4 2020-06-22 tracey __attribute__((__nonnull__ (1)));
65 fb863fa4 2020-06-22 tracey int kw_cmp(const void *, const void *);
66 fb863fa4 2020-06-22 tracey int lookup(char *);
67 fb863fa4 2020-06-22 tracey int igetc(void);
68 fb863fa4 2020-06-22 tracey int lgetc(int);
69 fb863fa4 2020-06-22 tracey void lungetc(int);
70 fb863fa4 2020-06-22 tracey int findeol(void);
71 fb863fa4 2020-06-22 tracey
72 fb863fa4 2020-06-22 tracey TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
73 fb863fa4 2020-06-22 tracey struct sym {
74 fb863fa4 2020-06-22 tracey TAILQ_ENTRY(sym) entry;
75 fb863fa4 2020-06-22 tracey int used;
76 fb863fa4 2020-06-22 tracey int persist;
77 fb863fa4 2020-06-22 tracey char *nam;
78 fb863fa4 2020-06-22 tracey char *val;
79 fb863fa4 2020-06-22 tracey };
80 fb863fa4 2020-06-22 tracey
81 fb863fa4 2020-06-22 tracey int symset(const char *, const char *, int);
82 fb863fa4 2020-06-22 tracey char *symget(const char *);
83 fb863fa4 2020-06-22 tracey
84 fb863fa4 2020-06-22 tracey const struct got_error* gerror = NULL;
85 9e6e8e60 2020-08-02 stsp struct gotconfig_remote_repo *remote;
86 9e6e8e60 2020-08-02 stsp struct gotconfig gotconfig;
87 9e6e8e60 2020-08-02 stsp static const struct got_error* new_remote(struct gotconfig_remote_repo **);
88 fb863fa4 2020-06-22 tracey
89 fb863fa4 2020-06-22 tracey typedef struct {
90 fb863fa4 2020-06-22 tracey union {
91 fb863fa4 2020-06-22 tracey int64_t number;
92 fb863fa4 2020-06-22 tracey char *string;
93 fb863fa4 2020-06-22 tracey } v;
94 fb863fa4 2020-06-22 tracey int lineno;
95 fb863fa4 2020-06-22 tracey } YYSTYPE;
96 fb863fa4 2020-06-22 tracey
97 fb863fa4 2020-06-22 tracey %}
98 fb863fa4 2020-06-22 tracey
99 fb863fa4 2020-06-22 tracey %token ERROR
100 fb863fa4 2020-06-22 tracey %token REMOTE REPOSITORY SERVER PROTOCOL USER
101 fb863fa4 2020-06-22 tracey %token <v.string> STRING
102 fb863fa4 2020-06-22 tracey %token <v.number> NUMBER
103 fb863fa4 2020-06-22 tracey
104 fb863fa4 2020-06-22 tracey %%
105 fb863fa4 2020-06-22 tracey
106 fb863fa4 2020-06-22 tracey grammar : /* empty */
107 fb863fa4 2020-06-22 tracey | grammar '\n'
108 fb863fa4 2020-06-22 tracey | grammar remote '\n'
109 fb863fa4 2020-06-22 tracey ;
110 fb863fa4 2020-06-22 tracey remoteopts2 : remoteopts2 remoteopts1 nl
111 fb863fa4 2020-06-22 tracey | remoteopts1 optnl
112 fb863fa4 2020-06-22 tracey ;
113 fb863fa4 2020-06-22 tracey remoteopts1 : REPOSITORY STRING {
114 9e6e8e60 2020-08-02 stsp remote->repository = strdup($2);
115 9e6e8e60 2020-08-02 stsp if (remote->repository == NULL) {
116 fb863fa4 2020-06-22 tracey free($2);
117 fb863fa4 2020-06-22 tracey yyerror("strdup");
118 fb863fa4 2020-06-22 tracey YYERROR;
119 fb863fa4 2020-06-22 tracey }
120 fb863fa4 2020-06-22 tracey free($2);
121 fb863fa4 2020-06-22 tracey }
122 fb863fa4 2020-06-22 tracey | SERVER STRING {
123 9e6e8e60 2020-08-02 stsp remote->server = strdup($2);
124 9e6e8e60 2020-08-02 stsp if (remote->server == NULL) {
125 fb863fa4 2020-06-22 tracey free($2);
126 fb863fa4 2020-06-22 tracey yyerror("strdup");
127 fb863fa4 2020-06-22 tracey YYERROR;
128 fb863fa4 2020-06-22 tracey }
129 fb863fa4 2020-06-22 tracey free($2);
130 fb863fa4 2020-06-22 tracey }
131 fb863fa4 2020-06-22 tracey | PROTOCOL STRING {
132 9e6e8e60 2020-08-02 stsp remote->protocol = strdup($2);
133 9e6e8e60 2020-08-02 stsp if (remote->protocol == NULL) {
134 fb863fa4 2020-06-22 tracey free($2);
135 fb863fa4 2020-06-22 tracey yyerror("strdup");
136 fb863fa4 2020-06-22 tracey YYERROR;
137 fb863fa4 2020-06-22 tracey }
138 fb863fa4 2020-06-22 tracey free($2);
139 fb863fa4 2020-06-22 tracey }
140 fb863fa4 2020-06-22 tracey | USER STRING {
141 9e6e8e60 2020-08-02 stsp remote->user = strdup($2);
142 9e6e8e60 2020-08-02 stsp if (remote->user == NULL) {
143 fb863fa4 2020-06-22 tracey free($2);
144 fb863fa4 2020-06-22 tracey yyerror("strdup");
145 fb863fa4 2020-06-22 tracey YYERROR;
146 fb863fa4 2020-06-22 tracey }
147 fb863fa4 2020-06-22 tracey free($2);
148 fb863fa4 2020-06-22 tracey }
149 fb863fa4 2020-06-22 tracey ;
150 fb863fa4 2020-06-22 tracey remote : REMOTE STRING {
151 fb863fa4 2020-06-22 tracey static const struct got_error* error;
152 fb863fa4 2020-06-22 tracey
153 9e6e8e60 2020-08-02 stsp error = new_remote(&remote);
154 fb863fa4 2020-06-22 tracey if (error) {
155 fb863fa4 2020-06-22 tracey free($2);
156 fb863fa4 2020-06-22 tracey yyerror("%s", error->msg);
157 fb863fa4 2020-06-22 tracey YYERROR;
158 fb863fa4 2020-06-22 tracey }
159 9e6e8e60 2020-08-02 stsp remote->name = strdup($2);
160 9e6e8e60 2020-08-02 stsp if (remote->name == NULL) {
161 fb863fa4 2020-06-22 tracey free($2);
162 fb863fa4 2020-06-22 tracey yyerror("strdup");
163 fb863fa4 2020-06-22 tracey YYERROR;
164 fb863fa4 2020-06-22 tracey }
165 fb863fa4 2020-06-22 tracey free($2);
166 fb863fa4 2020-06-22 tracey } '{' optnl remoteopts2 '}' {
167 9e6e8e60 2020-08-02 stsp TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
168 9e6e8e60 2020-08-02 stsp gotconfig.nremotes++;
169 fb863fa4 2020-06-22 tracey }
170 fb863fa4 2020-06-22 tracey ;
171 fb863fa4 2020-06-22 tracey optnl : '\n' optnl
172 fb863fa4 2020-06-22 tracey | /* empty */
173 fb863fa4 2020-06-22 tracey ;
174 fb863fa4 2020-06-22 tracey nl : '\n' optnl
175 fb863fa4 2020-06-22 tracey ;
176 fb863fa4 2020-06-22 tracey %%
177 fb863fa4 2020-06-22 tracey
178 fb863fa4 2020-06-22 tracey struct keywords {
179 fb863fa4 2020-06-22 tracey const char *k_name;
180 fb863fa4 2020-06-22 tracey int k_val;
181 fb863fa4 2020-06-22 tracey };
182 fb863fa4 2020-06-22 tracey
183 fb863fa4 2020-06-22 tracey int
184 fb863fa4 2020-06-22 tracey yyerror(const char *fmt, ...)
185 fb863fa4 2020-06-22 tracey {
186 fb863fa4 2020-06-22 tracey va_list ap;
187 fb863fa4 2020-06-22 tracey char *msg;
188 fb863fa4 2020-06-22 tracey char *err = NULL;
189 fb863fa4 2020-06-22 tracey
190 fb863fa4 2020-06-22 tracey va_start(ap, fmt);
191 fb863fa4 2020-06-22 tracey if (vasprintf(&msg, fmt, ap) == -1) {
192 fb863fa4 2020-06-22 tracey gerror = got_error_from_errno("vasprintf");
193 fb863fa4 2020-06-22 tracey return 0;
194 fb863fa4 2020-06-22 tracey }
195 fb863fa4 2020-06-22 tracey va_end(ap);
196 fb863fa4 2020-06-22 tracey if (asprintf(&err, "%s:%d: %s", file->name, yylval.lineno, msg) == -1) {
197 fb863fa4 2020-06-22 tracey gerror = got_error_from_errno("asprintf");
198 fb863fa4 2020-06-22 tracey return(0);
199 fb863fa4 2020-06-22 tracey }
200 fb863fa4 2020-06-22 tracey gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
201 fb863fa4 2020-06-22 tracey free(msg);
202 fb863fa4 2020-06-22 tracey free(err);
203 fb863fa4 2020-06-22 tracey return(0);
204 fb863fa4 2020-06-22 tracey }
205 fb863fa4 2020-06-22 tracey
206 fb863fa4 2020-06-22 tracey int
207 fb863fa4 2020-06-22 tracey kw_cmp(const void *k, const void *e)
208 fb863fa4 2020-06-22 tracey {
209 fb863fa4 2020-06-22 tracey return (strcmp(k, ((const struct keywords *)e)->k_name));
210 fb863fa4 2020-06-22 tracey }
211 fb863fa4 2020-06-22 tracey
212 fb863fa4 2020-06-22 tracey int
213 fb863fa4 2020-06-22 tracey lookup(char *s)
214 fb863fa4 2020-06-22 tracey {
215 fb863fa4 2020-06-22 tracey /* This has to be sorted always. */
216 fb863fa4 2020-06-22 tracey static const struct keywords keywords[] = {
217 fb863fa4 2020-06-22 tracey {"protocol", PROTOCOL},
218 fb863fa4 2020-06-22 tracey {"remote", REMOTE},
219 fb863fa4 2020-06-22 tracey {"repository", REPOSITORY},
220 fb863fa4 2020-06-22 tracey {"server", SERVER},
221 fb863fa4 2020-06-22 tracey {"user", USER},
222 fb863fa4 2020-06-22 tracey };
223 fb863fa4 2020-06-22 tracey const struct keywords *p;
224 fb863fa4 2020-06-22 tracey
225 fb863fa4 2020-06-22 tracey p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
226 fb863fa4 2020-06-22 tracey sizeof(keywords[0]), kw_cmp);
227 fb863fa4 2020-06-22 tracey
228 fb863fa4 2020-06-22 tracey if (p)
229 fb863fa4 2020-06-22 tracey return (p->k_val);
230 fb863fa4 2020-06-22 tracey else
231 fb863fa4 2020-06-22 tracey return (STRING);
232 fb863fa4 2020-06-22 tracey }
233 fb863fa4 2020-06-22 tracey
234 fb863fa4 2020-06-22 tracey #define START_EXPAND 1
235 fb863fa4 2020-06-22 tracey #define DONE_EXPAND 2
236 fb863fa4 2020-06-22 tracey
237 fb863fa4 2020-06-22 tracey static int expanding;
238 fb863fa4 2020-06-22 tracey
239 fb863fa4 2020-06-22 tracey int
240 fb863fa4 2020-06-22 tracey igetc(void)
241 fb863fa4 2020-06-22 tracey {
242 fb863fa4 2020-06-22 tracey int c;
243 fb863fa4 2020-06-22 tracey
244 fb863fa4 2020-06-22 tracey while (1) {
245 fb863fa4 2020-06-22 tracey if (file->ungetpos > 0)
246 fb863fa4 2020-06-22 tracey c = file->ungetbuf[--file->ungetpos];
247 fb863fa4 2020-06-22 tracey else
248 fb863fa4 2020-06-22 tracey c = getc(file->stream);
249 fb863fa4 2020-06-22 tracey
250 fb863fa4 2020-06-22 tracey if (c == START_EXPAND)
251 fb863fa4 2020-06-22 tracey expanding = 1;
252 fb863fa4 2020-06-22 tracey else if (c == DONE_EXPAND)
253 fb863fa4 2020-06-22 tracey expanding = 0;
254 fb863fa4 2020-06-22 tracey else
255 fb863fa4 2020-06-22 tracey break;
256 fb863fa4 2020-06-22 tracey }
257 fb863fa4 2020-06-22 tracey return (c);
258 fb863fa4 2020-06-22 tracey }
259 fb863fa4 2020-06-22 tracey
260 fb863fa4 2020-06-22 tracey int
261 fb863fa4 2020-06-22 tracey lgetc(int quotec)
262 fb863fa4 2020-06-22 tracey {
263 fb863fa4 2020-06-22 tracey int c, next;
264 fb863fa4 2020-06-22 tracey
265 fb863fa4 2020-06-22 tracey if (quotec) {
266 51b930a3 2020-08-02 tracey c = igetc();
267 51b930a3 2020-08-02 tracey if (c == EOF) {
268 fb863fa4 2020-06-22 tracey yyerror("reached end of file while parsing "
269 fb863fa4 2020-06-22 tracey "quoted string");
270 fb863fa4 2020-06-22 tracey }
271 fb863fa4 2020-06-22 tracey return (c);
272 fb863fa4 2020-06-22 tracey }
273 fb863fa4 2020-06-22 tracey
274 51b930a3 2020-08-02 tracey c = igetc();
275 51b930a3 2020-08-02 tracey while (c == '\\') {
276 fb863fa4 2020-06-22 tracey next = igetc();
277 fb863fa4 2020-06-22 tracey if (next != '\n') {
278 fb863fa4 2020-06-22 tracey c = next;
279 fb863fa4 2020-06-22 tracey break;
280 fb863fa4 2020-06-22 tracey }
281 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
282 fb863fa4 2020-06-22 tracey file->lineno++;
283 fb863fa4 2020-06-22 tracey }
284 fb863fa4 2020-06-22 tracey
285 fb863fa4 2020-06-22 tracey return (c);
286 fb863fa4 2020-06-22 tracey }
287 fb863fa4 2020-06-22 tracey
288 fb863fa4 2020-06-22 tracey void
289 fb863fa4 2020-06-22 tracey lungetc(int c)
290 fb863fa4 2020-06-22 tracey {
291 fb863fa4 2020-06-22 tracey if (c == EOF)
292 fb863fa4 2020-06-22 tracey return;
293 fb863fa4 2020-06-22 tracey
294 fb863fa4 2020-06-22 tracey if (file->ungetpos >= file->ungetsize) {
295 fb863fa4 2020-06-22 tracey void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
296 fb863fa4 2020-06-22 tracey if (p == NULL)
297 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
298 fb863fa4 2020-06-22 tracey file->ungetbuf = p;
299 fb863fa4 2020-06-22 tracey file->ungetsize *= 2;
300 fb863fa4 2020-06-22 tracey }
301 fb863fa4 2020-06-22 tracey file->ungetbuf[file->ungetpos++] = c;
302 fb863fa4 2020-06-22 tracey }
303 fb863fa4 2020-06-22 tracey
304 fb863fa4 2020-06-22 tracey int
305 fb863fa4 2020-06-22 tracey findeol(void)
306 fb863fa4 2020-06-22 tracey {
307 fb863fa4 2020-06-22 tracey int c;
308 fb863fa4 2020-06-22 tracey
309 fb863fa4 2020-06-22 tracey /* Skip to either EOF or the first real EOL. */
310 fb863fa4 2020-06-22 tracey while (1) {
311 fb863fa4 2020-06-22 tracey c = lgetc(0);
312 fb863fa4 2020-06-22 tracey if (c == '\n') {
313 fb863fa4 2020-06-22 tracey file->lineno++;
314 fb863fa4 2020-06-22 tracey break;
315 fb863fa4 2020-06-22 tracey }
316 fb863fa4 2020-06-22 tracey if (c == EOF)
317 fb863fa4 2020-06-22 tracey break;
318 fb863fa4 2020-06-22 tracey }
319 fb863fa4 2020-06-22 tracey return (ERROR);
320 fb863fa4 2020-06-22 tracey }
321 fb863fa4 2020-06-22 tracey
322 fb863fa4 2020-06-22 tracey int
323 fb863fa4 2020-06-22 tracey yylex(void)
324 fb863fa4 2020-06-22 tracey {
325 fb863fa4 2020-06-22 tracey unsigned char buf[8096];
326 fb863fa4 2020-06-22 tracey unsigned char *p, *val;
327 fb863fa4 2020-06-22 tracey int quotec, next, c;
328 fb863fa4 2020-06-22 tracey int token;
329 fb863fa4 2020-06-22 tracey
330 fb863fa4 2020-06-22 tracey top:
331 fb863fa4 2020-06-22 tracey p = buf;
332 51b930a3 2020-08-02 tracey c = lgetc(0);
333 51b930a3 2020-08-02 tracey while (c == ' ' || c == '\t')
334 51b930a3 2020-08-02 tracey c = lgetc(0); /* nothing */
335 fb863fa4 2020-06-22 tracey
336 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
337 51b930a3 2020-08-02 tracey if (c == '#') {
338 51b930a3 2020-08-02 tracey c = lgetc(0);
339 51b930a3 2020-08-02 tracey while (c != '\n' && c != EOF)
340 51b930a3 2020-08-02 tracey c = lgetc(0); /* nothing */
341 51b930a3 2020-08-02 tracey }
342 fb863fa4 2020-06-22 tracey if (c == '$' && !expanding) {
343 fb863fa4 2020-06-22 tracey while (1) {
344 51b930a3 2020-08-02 tracey c = lgetc(0);
345 51b930a3 2020-08-02 tracey if (c == EOF)
346 fb863fa4 2020-06-22 tracey return (0);
347 fb863fa4 2020-06-22 tracey
348 fb863fa4 2020-06-22 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
349 fb863fa4 2020-06-22 tracey yyerror("string too long");
350 fb863fa4 2020-06-22 tracey return (findeol());
351 fb863fa4 2020-06-22 tracey }
352 fb863fa4 2020-06-22 tracey if (isalnum(c) || c == '_') {
353 fb863fa4 2020-06-22 tracey *p++ = c;
354 fb863fa4 2020-06-22 tracey continue;
355 fb863fa4 2020-06-22 tracey }
356 fb863fa4 2020-06-22 tracey *p = '\0';
357 fb863fa4 2020-06-22 tracey lungetc(c);
358 fb863fa4 2020-06-22 tracey break;
359 fb863fa4 2020-06-22 tracey }
360 fb863fa4 2020-06-22 tracey val = symget(buf);
361 fb863fa4 2020-06-22 tracey if (val == NULL) {
362 fb863fa4 2020-06-22 tracey yyerror("macro '%s' not defined", buf);
363 fb863fa4 2020-06-22 tracey return (findeol());
364 fb863fa4 2020-06-22 tracey }
365 fb863fa4 2020-06-22 tracey p = val + strlen(val) - 1;
366 fb863fa4 2020-06-22 tracey lungetc(DONE_EXPAND);
367 fb863fa4 2020-06-22 tracey while (p >= val) {
368 fb863fa4 2020-06-22 tracey lungetc(*p);
369 fb863fa4 2020-06-22 tracey p--;
370 fb863fa4 2020-06-22 tracey }
371 fb863fa4 2020-06-22 tracey lungetc(START_EXPAND);
372 fb863fa4 2020-06-22 tracey goto top;
373 fb863fa4 2020-06-22 tracey }
374 fb863fa4 2020-06-22 tracey
375 fb863fa4 2020-06-22 tracey switch (c) {
376 fb863fa4 2020-06-22 tracey case '\'':
377 fb863fa4 2020-06-22 tracey case '"':
378 fb863fa4 2020-06-22 tracey quotec = c;
379 fb863fa4 2020-06-22 tracey while (1) {
380 51b930a3 2020-08-02 tracey c = lgetc(quotec);
381 51b930a3 2020-08-02 tracey if (c == EOF)
382 fb863fa4 2020-06-22 tracey return (0);
383 fb863fa4 2020-06-22 tracey if (c == '\n') {
384 fb863fa4 2020-06-22 tracey file->lineno++;
385 fb863fa4 2020-06-22 tracey continue;
386 fb863fa4 2020-06-22 tracey } else if (c == '\\') {
387 51b930a3 2020-08-02 tracey next = lgetc(quotec);
388 51b930a3 2020-08-02 tracey if (next == EOF)
389 fb863fa4 2020-06-22 tracey return (0);
390 fb863fa4 2020-06-22 tracey if (next == quotec || c == ' ' || c == '\t')
391 fb863fa4 2020-06-22 tracey c = next;
392 fb863fa4 2020-06-22 tracey else if (next == '\n') {
393 fb863fa4 2020-06-22 tracey file->lineno++;
394 fb863fa4 2020-06-22 tracey continue;
395 fb863fa4 2020-06-22 tracey } else
396 fb863fa4 2020-06-22 tracey lungetc(next);
397 fb863fa4 2020-06-22 tracey } else if (c == quotec) {
398 fb863fa4 2020-06-22 tracey *p = '\0';
399 fb863fa4 2020-06-22 tracey break;
400 fb863fa4 2020-06-22 tracey } else if (c == '\0') {
401 fb863fa4 2020-06-22 tracey yyerror("syntax error");
402 fb863fa4 2020-06-22 tracey return (findeol());
403 fb863fa4 2020-06-22 tracey }
404 fb863fa4 2020-06-22 tracey if (p + 1 >= buf + sizeof(buf) - 1) {
405 fb863fa4 2020-06-22 tracey yyerror("string too long");
406 fb863fa4 2020-06-22 tracey return (findeol());
407 fb863fa4 2020-06-22 tracey }
408 fb863fa4 2020-06-22 tracey *p++ = c;
409 fb863fa4 2020-06-22 tracey }
410 fb863fa4 2020-06-22 tracey yylval.v.string = strdup(buf);
411 fb863fa4 2020-06-22 tracey if (yylval.v.string == NULL)
412 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
413 fb863fa4 2020-06-22 tracey return (STRING);
414 fb863fa4 2020-06-22 tracey }
415 fb863fa4 2020-06-22 tracey
416 fb863fa4 2020-06-22 tracey #define allowed_to_end_number(x) \
417 fb863fa4 2020-06-22 tracey (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
418 fb863fa4 2020-06-22 tracey
419 fb863fa4 2020-06-22 tracey if (c == '-' || isdigit(c)) {
420 fb863fa4 2020-06-22 tracey do {
421 fb863fa4 2020-06-22 tracey *p++ = c;
422 fb863fa4 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
423 fb863fa4 2020-06-22 tracey yyerror("string too long");
424 fb863fa4 2020-06-22 tracey return (findeol());
425 fb863fa4 2020-06-22 tracey }
426 51b930a3 2020-08-02 tracey c = lgetc(0);
427 51b930a3 2020-08-02 tracey } while (c != EOF && isdigit(c));
428 fb863fa4 2020-06-22 tracey lungetc(c);
429 fb863fa4 2020-06-22 tracey if (p == buf + 1 && buf[0] == '-')
430 fb863fa4 2020-06-22 tracey goto nodigits;
431 fb863fa4 2020-06-22 tracey if (c == EOF || allowed_to_end_number(c)) {
432 fb863fa4 2020-06-22 tracey const char *errstr = NULL;
433 fb863fa4 2020-06-22 tracey
434 fb863fa4 2020-06-22 tracey *p = '\0';
435 fb863fa4 2020-06-22 tracey yylval.v.number = strtonum(buf, LLONG_MIN,
436 fb863fa4 2020-06-22 tracey LLONG_MAX, &errstr);
437 fb863fa4 2020-06-22 tracey if (errstr) {
438 fb863fa4 2020-06-22 tracey yyerror("\"%s\" invalid number: %s",
439 fb863fa4 2020-06-22 tracey buf, errstr);
440 fb863fa4 2020-06-22 tracey return (findeol());
441 fb863fa4 2020-06-22 tracey }
442 fb863fa4 2020-06-22 tracey return (NUMBER);
443 fb863fa4 2020-06-22 tracey } else {
444 fb863fa4 2020-06-22 tracey nodigits:
445 fb863fa4 2020-06-22 tracey while (p > buf + 1)
446 fb863fa4 2020-06-22 tracey lungetc(*--p);
447 fb863fa4 2020-06-22 tracey c = *--p;
448 fb863fa4 2020-06-22 tracey if (c == '-')
449 fb863fa4 2020-06-22 tracey return (c);
450 fb863fa4 2020-06-22 tracey }
451 fb863fa4 2020-06-22 tracey }
452 fb863fa4 2020-06-22 tracey
453 fb863fa4 2020-06-22 tracey #define allowed_in_string(x) \
454 fb863fa4 2020-06-22 tracey (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
455 fb863fa4 2020-06-22 tracey x != '{' && x != '}' && \
456 fb863fa4 2020-06-22 tracey x != '!' && x != '=' && x != '#' && \
457 fb863fa4 2020-06-22 tracey x != ','))
458 fb863fa4 2020-06-22 tracey
459 fb863fa4 2020-06-22 tracey if (isalnum(c) || c == ':' || c == '_') {
460 fb863fa4 2020-06-22 tracey do {
461 fb863fa4 2020-06-22 tracey *p++ = c;
462 fb863fa4 2020-06-22 tracey if ((unsigned)(p-buf) >= sizeof(buf)) {
463 fb863fa4 2020-06-22 tracey yyerror("string too long");
464 fb863fa4 2020-06-22 tracey return (findeol());
465 fb863fa4 2020-06-22 tracey }
466 51b930a3 2020-08-02 tracey c = lgetc(0);
467 51b930a3 2020-08-02 tracey } while (c != EOF && (allowed_in_string(c)));
468 fb863fa4 2020-06-22 tracey lungetc(c);
469 fb863fa4 2020-06-22 tracey *p = '\0';
470 51b930a3 2020-08-02 tracey token = lookup(buf);
471 51b930a3 2020-08-02 tracey if (token == STRING) {
472 51b930a3 2020-08-02 tracey yylval.v.string = strdup(buf);
473 51b930a3 2020-08-02 tracey if (yylval.v.string == NULL)
474 fb863fa4 2020-06-22 tracey err(1, "%s", __func__);
475 51b930a3 2020-08-02 tracey }
476 fb863fa4 2020-06-22 tracey return (token);
477 fb863fa4 2020-06-22 tracey }
478 fb863fa4 2020-06-22 tracey if (c == '\n') {
479 fb863fa4 2020-06-22 tracey yylval.lineno = file->lineno;
480 fb863fa4 2020-06-22 tracey file->lineno++;
481 fb863fa4 2020-06-22 tracey }
482 fb863fa4 2020-06-22 tracey if (c == EOF)
483 fb863fa4 2020-06-22 tracey return (0);
484 fb863fa4 2020-06-22 tracey return (c);
485 fb863fa4 2020-06-22 tracey }
486 fb863fa4 2020-06-22 tracey
487 fb863fa4 2020-06-22 tracey static const struct got_error*
488 6d7260fd 2020-08-03 stsp newfile(struct file **nfile, const char *name)
489 fb863fa4 2020-06-22 tracey {
490 fb863fa4 2020-06-22 tracey const struct got_error* error = NULL;
491 fb863fa4 2020-06-22 tracey
492 51b930a3 2020-08-02 tracey (*nfile) = calloc(1, sizeof(struct file));
493 51b930a3 2020-08-02 tracey if ((*nfile) == NULL)
494 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "calloc");
495 51b930a3 2020-08-02 tracey (*nfile)->name = strdup(name);
496 51b930a3 2020-08-02 tracey if ((*nfile)->name == NULL) {
497 fb863fa4 2020-06-22 tracey free(nfile);
498 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "strdup");
499 fb863fa4 2020-06-22 tracey }
500 51b930a3 2020-08-02 tracey (*nfile)->stream = fopen((*nfile)->name, "r");
501 51b930a3 2020-08-02 tracey if ((*nfile)->stream == NULL) {
502 fb863fa4 2020-06-22 tracey char *msg = NULL;
503 fb863fa4 2020-06-22 tracey if (asprintf(&msg, "%s", (*nfile)->name) == -1)
504 fb863fa4 2020-06-22 tracey return got_error_from_errno("asprintf");
505 fb863fa4 2020-06-22 tracey error = got_error_msg(GOT_ERR_NO_CONFIG_FILE, msg);
506 fb863fa4 2020-06-22 tracey free((*nfile)->name);
507 fb863fa4 2020-06-22 tracey free((*nfile));
508 fb863fa4 2020-06-22 tracey free(msg);
509 fb863fa4 2020-06-22 tracey return error;
510 fb863fa4 2020-06-22 tracey }
511 6d7260fd 2020-08-03 stsp (*nfile)->lineno = 1;
512 fb863fa4 2020-06-22 tracey (*nfile)->ungetsize = 16;
513 fb863fa4 2020-06-22 tracey (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
514 fb863fa4 2020-06-22 tracey if ((*nfile)->ungetbuf == NULL) {
515 fb863fa4 2020-06-22 tracey fclose((*nfile)->stream);
516 fb863fa4 2020-06-22 tracey free((*nfile)->name);
517 fb863fa4 2020-06-22 tracey free((*nfile));
518 fb863fa4 2020-06-22 tracey return got_error_from_errno2(__func__, "malloc");
519 fb863fa4 2020-06-22 tracey }
520 fb863fa4 2020-06-22 tracey return error;
521 fb863fa4 2020-06-22 tracey }
522 fb863fa4 2020-06-22 tracey
523 fb863fa4 2020-06-22 tracey static const struct got_error*
524 9e6e8e60 2020-08-02 stsp new_remote(struct gotconfig_remote_repo **remote)
525 9e6e8e60 2020-08-02 stsp {
526 9e6e8e60 2020-08-02 stsp const struct got_error *error = NULL;
527 fb863fa4 2020-06-22 tracey
528 9e6e8e60 2020-08-02 stsp *remote = calloc(1, sizeof(**remote));
529 9e6e8e60 2020-08-02 stsp if (*remote == NULL)
530 fb863fa4 2020-06-22 tracey error = got_error_from_errno("calloc");
531 fb863fa4 2020-06-22 tracey return error;
532 fb863fa4 2020-06-22 tracey }
533 fb863fa4 2020-06-22 tracey
534 6d7260fd 2020-08-03 stsp static void
535 6d7260fd 2020-08-03 stsp closefile(struct file *file)
536 fb863fa4 2020-06-22 tracey {
537 fb863fa4 2020-06-22 tracey fclose(file->stream);
538 fb863fa4 2020-06-22 tracey free(file->name);
539 fb863fa4 2020-06-22 tracey free(file->ungetbuf);
540 fb863fa4 2020-06-22 tracey free(file);
541 fb863fa4 2020-06-22 tracey }
542 fb863fa4 2020-06-22 tracey
543 fb863fa4 2020-06-22 tracey const struct got_error*
544 9e6e8e60 2020-08-02 stsp gotconfig_parse(struct gotconfig **conf, const char *filename)
545 fb863fa4 2020-06-22 tracey {
546 fb863fa4 2020-06-22 tracey struct sym *sym, *next;
547 fb863fa4 2020-06-22 tracey
548 9e6e8e60 2020-08-02 stsp *conf = NULL;
549 fb863fa4 2020-06-22 tracey
550 fb863fa4 2020-06-22 tracey /*
551 fb863fa4 2020-06-22 tracey * We don't require that gotconfig exists
552 fb863fa4 2020-06-22 tracey * So, null gerror and goto done
553 fb863fa4 2020-06-22 tracey */
554 6d7260fd 2020-08-03 stsp gerror = newfile(&file, filename);
555 fb863fa4 2020-06-22 tracey if (gerror && gerror->code == GOT_ERR_NO_CONFIG_FILE) {
556 fb863fa4 2020-06-22 tracey gerror = NULL;
557 fb863fa4 2020-06-22 tracey goto done;
558 9e6e8e60 2020-08-02 stsp } else if (gerror)
559 fb863fa4 2020-06-22 tracey return gerror;
560 fb863fa4 2020-06-22 tracey
561 9e6e8e60 2020-08-02 stsp TAILQ_INIT(&gotconfig.remotes);
562 fb863fa4 2020-06-22 tracey
563 fb863fa4 2020-06-22 tracey yyparse();
564 6d7260fd 2020-08-03 stsp closefile(file);
565 fb863fa4 2020-06-22 tracey
566 fb863fa4 2020-06-22 tracey /* Free macros and check which have not been used. */
567 fb863fa4 2020-06-22 tracey TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
568 fb863fa4 2020-06-22 tracey if (!sym->persist) {
569 fb863fa4 2020-06-22 tracey free(sym->nam);
570 fb863fa4 2020-06-22 tracey free(sym->val);
571 fb863fa4 2020-06-22 tracey TAILQ_REMOVE(&symhead, sym, entry);
572 fb863fa4 2020-06-22 tracey free(sym);
573 fb863fa4 2020-06-22 tracey }
574 fb863fa4 2020-06-22 tracey }
575 fb863fa4 2020-06-22 tracey done:
576 9e6e8e60 2020-08-02 stsp *conf = &gotconfig;
577 fb863fa4 2020-06-22 tracey return gerror;
578 fb863fa4 2020-06-22 tracey }
579 fb863fa4 2020-06-22 tracey
580 9e6e8e60 2020-08-02 stsp void
581 9e6e8e60 2020-08-02 stsp gotconfig_free(struct gotconfig *conf)
582 9e6e8e60 2020-08-02 stsp {
583 9e6e8e60 2020-08-02 stsp struct gotconfig_remote_repo *remote;
584 9e6e8e60 2020-08-02 stsp
585 9e6e8e60 2020-08-02 stsp while (!TAILQ_EMPTY(&conf->remotes)) {
586 9e6e8e60 2020-08-02 stsp remote = TAILQ_FIRST(&conf->remotes);
587 9e6e8e60 2020-08-02 stsp TAILQ_REMOVE(&conf->remotes, remote, entry);
588 9e6e8e60 2020-08-02 stsp free(remote->name);
589 9e6e8e60 2020-08-02 stsp free(remote->repository);
590 9e6e8e60 2020-08-02 stsp free(remote->server);
591 9e6e8e60 2020-08-02 stsp free(remote->protocol);
592 9e6e8e60 2020-08-02 stsp free(remote->user);
593 9e6e8e60 2020-08-02 stsp free(remote);
594 9e6e8e60 2020-08-02 stsp }
595 9e6e8e60 2020-08-02 stsp }
596 9e6e8e60 2020-08-02 stsp
597 fb863fa4 2020-06-22 tracey int
598 fb863fa4 2020-06-22 tracey symset(const char *nam, const char *val, int persist)
599 fb863fa4 2020-06-22 tracey {
600 fb863fa4 2020-06-22 tracey struct sym *sym;
601 fb863fa4 2020-06-22 tracey
602 fb863fa4 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
603 fb863fa4 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0)
604 fb863fa4 2020-06-22 tracey break;
605 fb863fa4 2020-06-22 tracey }
606 fb863fa4 2020-06-22 tracey
607 fb863fa4 2020-06-22 tracey if (sym != NULL) {
608 fb863fa4 2020-06-22 tracey if (sym->persist == 1)
609 fb863fa4 2020-06-22 tracey return (0);
610 fb863fa4 2020-06-22 tracey else {
611 fb863fa4 2020-06-22 tracey free(sym->nam);
612 fb863fa4 2020-06-22 tracey free(sym->val);
613 fb863fa4 2020-06-22 tracey TAILQ_REMOVE(&symhead, sym, entry);
614 fb863fa4 2020-06-22 tracey free(sym);
615 fb863fa4 2020-06-22 tracey }
616 fb863fa4 2020-06-22 tracey }
617 51b930a3 2020-08-02 tracey sym = calloc(1, sizeof(*sym));
618 51b930a3 2020-08-02 tracey if (sym == NULL)
619 fb863fa4 2020-06-22 tracey return (-1);
620 fb863fa4 2020-06-22 tracey
621 fb863fa4 2020-06-22 tracey sym->nam = strdup(nam);
622 fb863fa4 2020-06-22 tracey if (sym->nam == NULL) {
623 fb863fa4 2020-06-22 tracey free(sym);
624 fb863fa4 2020-06-22 tracey return (-1);
625 fb863fa4 2020-06-22 tracey }
626 fb863fa4 2020-06-22 tracey sym->val = strdup(val);
627 fb863fa4 2020-06-22 tracey if (sym->val == NULL) {
628 fb863fa4 2020-06-22 tracey free(sym->nam);
629 fb863fa4 2020-06-22 tracey free(sym);
630 fb863fa4 2020-06-22 tracey return (-1);
631 fb863fa4 2020-06-22 tracey }
632 fb863fa4 2020-06-22 tracey sym->used = 0;
633 fb863fa4 2020-06-22 tracey sym->persist = persist;
634 fb863fa4 2020-06-22 tracey TAILQ_INSERT_TAIL(&symhead, sym, entry);
635 fb863fa4 2020-06-22 tracey return (0);
636 fb863fa4 2020-06-22 tracey }
637 fb863fa4 2020-06-22 tracey
638 fb863fa4 2020-06-22 tracey int
639 fb863fa4 2020-06-22 tracey cmdline_symset(char *s)
640 fb863fa4 2020-06-22 tracey {
641 fb863fa4 2020-06-22 tracey char *sym, *val;
642 fb863fa4 2020-06-22 tracey int ret;
643 fb863fa4 2020-06-22 tracey size_t len;
644 fb863fa4 2020-06-22 tracey
645 51b930a3 2020-08-02 tracey val = strrchr(s, '=');
646 51b930a3 2020-08-02 tracey if (val == NULL)
647 fb863fa4 2020-06-22 tracey return (-1);
648 fb863fa4 2020-06-22 tracey
649 fb863fa4 2020-06-22 tracey len = strlen(s) - strlen(val) + 1;
650 51b930a3 2020-08-02 tracey sym = malloc(len);
651 51b930a3 2020-08-02 tracey if (sym == NULL)
652 fb863fa4 2020-06-22 tracey errx(1, "cmdline_symset: malloc");
653 fb863fa4 2020-06-22 tracey
654 fb863fa4 2020-06-22 tracey strlcpy(sym, s, len);
655 fb863fa4 2020-06-22 tracey
656 fb863fa4 2020-06-22 tracey ret = symset(sym, val + 1, 1);
657 fb863fa4 2020-06-22 tracey free(sym);
658 fb863fa4 2020-06-22 tracey
659 fb863fa4 2020-06-22 tracey return (ret);
660 fb863fa4 2020-06-22 tracey }
661 fb863fa4 2020-06-22 tracey
662 fb863fa4 2020-06-22 tracey char *
663 fb863fa4 2020-06-22 tracey symget(const char *nam)
664 fb863fa4 2020-06-22 tracey {
665 fb863fa4 2020-06-22 tracey struct sym *sym;
666 fb863fa4 2020-06-22 tracey
667 fb863fa4 2020-06-22 tracey TAILQ_FOREACH(sym, &symhead, entry) {
668 fb863fa4 2020-06-22 tracey if (strcmp(nam, sym->nam) == 0) {
669 fb863fa4 2020-06-22 tracey sym->used = 1;
670 fb863fa4 2020-06-22 tracey return (sym->val);
671 fb863fa4 2020-06-22 tracey }
672 fb863fa4 2020-06-22 tracey }
673 fb863fa4 2020-06-22 tracey return (NULL);
674 fb863fa4 2020-06-22 tracey }