Blame


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