Blame


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