Blame


1 69bdd906 2021-06-15 op /*
2 69bdd906 2021-06-15 op * Much of the design is taken from doas (parse.y rev 1.29)
3 69bdd906 2021-06-15 op *
4 69bdd906 2021-06-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 69bdd906 2021-06-15 op * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
6 69bdd906 2021-06-15 op *
7 69bdd906 2021-06-15 op * Permission to use, copy, modify, and distribute this software for any
8 69bdd906 2021-06-15 op * purpose with or without fee is hereby granted, provided that the above
9 69bdd906 2021-06-15 op * copyright notice and this permission notice appear in all copies.
10 69bdd906 2021-06-15 op *
11 69bdd906 2021-06-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 69bdd906 2021-06-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 69bdd906 2021-06-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 69bdd906 2021-06-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 69bdd906 2021-06-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 69bdd906 2021-06-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 69bdd906 2021-06-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 69bdd906 2021-06-15 op *
19 69bdd906 2021-06-15 op */
20 69bdd906 2021-06-15 op
21 69bdd906 2021-06-15 op %{
22 69bdd906 2021-06-15 op
23 b7d58e0b 2021-11-05 op #include "keymap.h"
24 69bdd906 2021-06-15 op #include "telescope.h"
25 9d65b1d9 2022-01-11 op #include "utils.h"
26 984245ce 2021-06-23 op
27 74a2587f 2021-06-21 op #include <assert.h>
28 69bdd906 2021-06-15 op #include <ctype.h>
29 69bdd906 2021-06-15 op #include <limits.h>
30 74a2587f 2021-06-21 op #include <ncurses.h>
31 69bdd906 2021-06-15 op #include <stdarg.h>
32 69bdd906 2021-06-15 op #include <stdio.h>
33 69bdd906 2021-06-15 op #include <stdlib.h>
34 69bdd906 2021-06-15 op #include <string.h>
35 46102ea3 2022-12-23 op
36 46102ea3 2022-12-23 op #include "iri.h"
37 69bdd906 2021-06-15 op
38 69bdd906 2021-06-15 op typedef struct {
39 69bdd906 2021-06-15 op union {
40 69bdd906 2021-06-15 op char *str;
41 69bdd906 2021-06-15 op int num;
42 69bdd906 2021-06-15 op };
43 69bdd906 2021-06-15 op int lineno;
44 69bdd906 2021-06-15 op int colno;
45 69bdd906 2021-06-15 op } yystype;
46 69bdd906 2021-06-15 op #define YYSTYPE yystype
47 69bdd906 2021-06-15 op
48 69e1d1b6 2021-06-15 op static char *current_style;
49 d89b86d6 2021-06-21 op static int color_type;
50 74a2587f 2021-06-21 op
51 69bdd906 2021-06-15 op static const char *path;
52 69bdd906 2021-06-15 op
53 69bdd906 2021-06-15 op FILE *yyfp;
54 69bdd906 2021-06-15 op
55 69bdd906 2021-06-15 op int parse_errors = 0;
56 69bdd906 2021-06-15 op
57 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
58 69bdd906 2021-06-15 op static int yylex(void);
59 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
60 86e7d7b4 2021-06-19 op static void setvari(char *, int);
61 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
62 74a2587f 2021-06-21 op static int colorname(const char *);
63 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
64 3198f30f 2021-08-26 op static int attrname(const char *);
65 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
66 984245ce 2021-06-23 op static void add_proxy(char *, char *);
67 e3427d18 2021-06-25 op static void bindkey(const char *, const char *, const char *);
68 21404dd9 2021-07-15 op static void do_parseconfig(const char *, int);
69 69bdd906 2021-06-15 op
70 69bdd906 2021-06-15 op %}
71 69bdd906 2021-06-15 op
72 bd3e6837 2021-11-03 op %token ATTR
73 bd3e6837 2021-11-03 op %token BIND BG
74 bd3e6837 2021-11-03 op %token CONT
75 bd3e6837 2021-11-03 op %token FG
76 bd3e6837 2021-11-03 op %token PRFX PROXY
77 bd3e6837 2021-11-03 op %token SET STYLE
78 bd3e6837 2021-11-03 op %token UNBIND
79 bd3e6837 2021-11-03 op %token VIA
80 69bdd906 2021-06-15 op
81 9cc9d696 2021-11-03 op %token <str> STRING
82 9cc9d696 2021-11-03 op %token <num> NUMBER
83 69bdd906 2021-06-15 op
84 69bdd906 2021-06-15 op %%
85 69bdd906 2021-06-15 op
86 69bdd906 2021-06-15 op grammar : /* empty */
87 69bdd906 2021-06-15 op | grammar '\n'
88 69bdd906 2021-06-15 op | grammar rule '\n'
89 69bdd906 2021-06-15 op | error '\n'
90 69bdd906 2021-06-15 op ;
91 69bdd906 2021-06-15 op
92 69e1d1b6 2021-06-15 op rule : set
93 74a2587f 2021-06-21 op | style {
94 74a2587f 2021-06-21 op free(current_style);
95 74a2587f 2021-06-21 op current_style = NULL;
96 74a2587f 2021-06-21 op }
97 69e1d1b6 2021-06-15 op | bind
98 69e1d1b6 2021-06-15 op | unbind
99 984245ce 2021-06-23 op | proxy
100 69e1d1b6 2021-06-15 op ;
101 69bdd906 2021-06-15 op
102 9cc9d696 2021-11-03 op set : SET STRING '=' STRING { setvars($2, $4); }
103 9cc9d696 2021-11-03 op | SET STRING '=' NUMBER { setvari($2, $4); }
104 69bdd906 2021-06-15 op ;
105 69bdd906 2021-06-15 op
106 9cc9d696 2021-11-03 op style : STYLE STRING { current_style = $2; } stylespec ;
107 5c38fd5f 2021-08-30 op stylespec : styleopt | '{' optnl styleopts '}' ;
108 69bdd906 2021-06-15 op
109 69bdd906 2021-06-15 op styleopts : /* empty */
110 0a53787c 2021-07-09 op | styleopts styleopt optnl
111 69bdd906 2021-06-15 op ;
112 69bdd906 2021-06-15 op
113 9cc9d696 2021-11-03 op styleopt : PRFX STRING { setprfx($2, $2); }
114 9cc9d696 2021-11-03 op | PRFX STRING STRING { setprfx($2, $3); }
115 9cc9d696 2021-11-03 op | BG { color_type = BG; } colorspec
116 9cc9d696 2021-11-03 op | FG { color_type = FG; } colorspec
117 9cc9d696 2021-11-03 op | ATTR attr
118 d89b86d6 2021-06-21 op ;
119 d89b86d6 2021-06-21 op
120 9cc9d696 2021-11-03 op colorspec : STRING { setcolor($1, $1, $1); free($1); }
121 9cc9d696 2021-11-03 op | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
122 9cc9d696 2021-11-03 op | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
123 69bdd906 2021-06-15 op ;
124 69bdd906 2021-06-15 op
125 9cc9d696 2021-11-03 op attr : STRING { setattr($1, $1, $1); free($1); }
126 9cc9d696 2021-11-03 op | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
127 9cc9d696 2021-11-03 op | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
128 d0149485 2021-06-22 op ;
129 d0149485 2021-06-22 op
130 9cc9d696 2021-11-03 op bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
131 69bdd906 2021-06-15 op ;
132 69bdd906 2021-06-15 op
133 9cc9d696 2021-11-03 op unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
134 69bdd906 2021-06-15 op ;
135 69bdd906 2021-06-15 op
136 9cc9d696 2021-11-03 op proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
137 0a53787c 2021-07-09 op ;
138 0a53787c 2021-07-09 op
139 0a53787c 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
140 0a53787c 2021-07-09 op | /* empty */
141 984245ce 2021-06-23 op ;
142 984245ce 2021-06-23 op
143 69bdd906 2021-06-15 op %%
144 69bdd906 2021-06-15 op
145 69bdd906 2021-06-15 op void
146 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
147 69bdd906 2021-06-15 op {
148 69bdd906 2021-06-15 op va_list va;
149 69bdd906 2021-06-15 op
150 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
151 69bdd906 2021-06-15 op va_start(va, fmt);
152 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
153 69bdd906 2021-06-15 op va_end(va);
154 69bdd906 2021-06-15 op fprintf(stderr, "\n");
155 69bdd906 2021-06-15 op parse_errors++;
156 69bdd906 2021-06-15 op }
157 69bdd906 2021-06-15 op
158 69bdd906 2021-06-15 op static struct keyword {
159 69bdd906 2021-06-15 op const char *word;
160 69bdd906 2021-06-15 op int token;
161 69bdd906 2021-06-15 op } keywords[] = {
162 9cc9d696 2021-11-03 op { "attr", ATTR },
163 9cc9d696 2021-11-03 op { "bg", BG },
164 9cc9d696 2021-11-03 op { "bind", BIND },
165 9cc9d696 2021-11-03 op { "cont", CONT },
166 9cc9d696 2021-11-03 op { "fg", FG },
167 9cc9d696 2021-11-03 op { "prefix", PRFX },
168 9cc9d696 2021-11-03 op { "proxy", PROXY },
169 9cc9d696 2021-11-03 op { "set", SET },
170 9cc9d696 2021-11-03 op { "style", STYLE },
171 9cc9d696 2021-11-03 op { "unbind", UNBIND },
172 9cc9d696 2021-11-03 op { "via", VIA },
173 69bdd906 2021-06-15 op };
174 69bdd906 2021-06-15 op
175 69bdd906 2021-06-15 op int
176 69bdd906 2021-06-15 op yylex(void)
177 69bdd906 2021-06-15 op {
178 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
179 69bdd906 2021-06-15 op const char *errstr;
180 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
181 69bdd906 2021-06-15 op size_t i;
182 69bdd906 2021-06-15 op
183 69bdd906 2021-06-15 op p = buf;
184 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
185 69bdd906 2021-06-15 op
186 69bdd906 2021-06-15 op repeat:
187 69bdd906 2021-06-15 op /* skip whitespace first */
188 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
189 69bdd906 2021-06-15 op yylval.colno++;
190 69bdd906 2021-06-15 op
191 69bdd906 2021-06-15 op /* check for special one-character constructions */
192 69bdd906 2021-06-15 op switch (c) {
193 c86d164e 2021-06-24 op case '\r':
194 c86d164e 2021-06-24 op /* silently eat up any \r */
195 c86d164e 2021-06-24 op goto repeat;
196 69bdd906 2021-06-15 op case '\n':
197 69bdd906 2021-06-15 op yylval.colno = 0;
198 69bdd906 2021-06-15 op yylval.lineno++;
199 69bdd906 2021-06-15 op /* fallthrough */
200 69bdd906 2021-06-15 op case '{':
201 69bdd906 2021-06-15 op case '}':
202 69bdd906 2021-06-15 op case '=':
203 69bdd906 2021-06-15 op return c;
204 69bdd906 2021-06-15 op case '#':
205 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
206 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
207 69bdd906 2021-06-15 op if (c == EOF)
208 69bdd906 2021-06-15 op goto eof;
209 69bdd906 2021-06-15 op yylval.colno = 0;
210 69bdd906 2021-06-15 op yylval.lineno++;
211 69bdd906 2021-06-15 op return c;
212 69bdd906 2021-06-15 op case EOF:
213 69bdd906 2021-06-15 op goto eof;
214 69bdd906 2021-06-15 op }
215 69bdd906 2021-06-15 op
216 69bdd906 2021-06-15 op /* parsing next word */
217 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
218 69bdd906 2021-06-15 op switch (c) {
219 69bdd906 2021-06-15 op case '\0':
220 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
221 69bdd906 2021-06-15 op yylval.colno+1);
222 69bdd906 2021-06-15 op escape = 0;
223 69bdd906 2021-06-15 op continue;
224 69bdd906 2021-06-15 op case '\\':
225 69bdd906 2021-06-15 op escape = !escape;
226 69bdd906 2021-06-15 op if (escape)
227 69bdd906 2021-06-15 op continue;
228 69bdd906 2021-06-15 op break;
229 6ba9e5a8 2021-06-24 op case '\r':
230 6ba9e5a8 2021-06-24 op /* ignore \r here too */
231 6ba9e5a8 2021-06-24 op continue;
232 69bdd906 2021-06-15 op case '\n':
233 69bdd906 2021-06-15 op if (quotes)
234 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
235 69bdd906 2021-06-15 op yylval.colno+1);
236 69bdd906 2021-06-15 op if (escape) {
237 69bdd906 2021-06-15 op nonkw = 1;
238 69bdd906 2021-06-15 op escape = 0;
239 69bdd906 2021-06-15 op yylval.colno = 0;
240 69bdd906 2021-06-15 op yylval.lineno++;
241 69bdd906 2021-06-15 op }
242 69bdd906 2021-06-15 op goto eow;
243 69bdd906 2021-06-15 op case EOF:
244 69bdd906 2021-06-15 op if (escape)
245 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
246 69bdd906 2021-06-15 op yylval.colno);
247 69bdd906 2021-06-15 op if (quotes)
248 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
249 69bdd906 2021-06-15 op qpos + 1);
250 69bdd906 2021-06-15 op goto eow;
251 69bdd906 2021-06-15 op case '{':
252 69bdd906 2021-06-15 op case '}':
253 69bdd906 2021-06-15 op case '=':
254 69bdd906 2021-06-15 op case '#':
255 69bdd906 2021-06-15 op case ' ':
256 69bdd906 2021-06-15 op case '\t':
257 69bdd906 2021-06-15 op if (!escape && !quotes)
258 69bdd906 2021-06-15 op goto eow;
259 69bdd906 2021-06-15 op break;
260 69bdd906 2021-06-15 op case '"':
261 69bdd906 2021-06-15 op if (!escape) {
262 69bdd906 2021-06-15 op quotes = !quotes;
263 69bdd906 2021-06-15 op if (quotes) {
264 69bdd906 2021-06-15 op nonkw = 1;
265 69bdd906 2021-06-15 op qpos = yylval.colno;
266 69bdd906 2021-06-15 op }
267 69bdd906 2021-06-15 op continue;
268 69bdd906 2021-06-15 op }
269 69bdd906 2021-06-15 op }
270 69bdd906 2021-06-15 op *p++ = c;
271 69bdd906 2021-06-15 op if (p == ebuf) {
272 69bdd906 2021-06-15 op yyerror("line too long");
273 69bdd906 2021-06-15 op p = buf;
274 69bdd906 2021-06-15 op }
275 69bdd906 2021-06-15 op escape = 0;
276 69bdd906 2021-06-15 op }
277 69bdd906 2021-06-15 op
278 69bdd906 2021-06-15 op eow:
279 69bdd906 2021-06-15 op *p = 0;
280 69bdd906 2021-06-15 op if (c != EOF)
281 69bdd906 2021-06-15 op ungetc(c, yyfp);
282 69bdd906 2021-06-15 op if (p == buf) {
283 69bdd906 2021-06-15 op /*
284 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
285 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
286 69bdd906 2021-06-15 op * the main loop.
287 69bdd906 2021-06-15 op */
288 69bdd906 2021-06-15 op if (c == EOF)
289 69bdd906 2021-06-15 op goto eof;
290 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
291 69bdd906 2021-06-15 op goto repeat;
292 69bdd906 2021-06-15 op }
293 69bdd906 2021-06-15 op if (!nonkw) {
294 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
295 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
296 69bdd906 2021-06-15 op return keywords[i].token;
297 69bdd906 2021-06-15 op }
298 69bdd906 2021-06-15 op }
299 69bdd906 2021-06-15 op c = *buf;
300 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
301 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
302 69bdd906 2021-06-15 op if (errstr != NULL)
303 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
304 9cc9d696 2021-11-03 op return NUMBER;
305 69bdd906 2021-06-15 op }
306 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
307 69bdd906 2021-06-15 op err(1, "%s", __func__);
308 69bdd906 2021-06-15 op yylval.str = str;
309 9cc9d696 2021-11-03 op return STRING;
310 69bdd906 2021-06-15 op
311 69bdd906 2021-06-15 op eof:
312 69bdd906 2021-06-15 op if (ferror(yyfp))
313 69bdd906 2021-06-15 op yyerror("input error reading config");
314 69bdd906 2021-06-15 op return 0;
315 69bdd906 2021-06-15 op }
316 69bdd906 2021-06-15 op
317 69e1d1b6 2021-06-15 op static void
318 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
319 69e1d1b6 2021-06-15 op {
320 74a2587f 2021-06-21 op assert(current_style != NULL);
321 69e1d1b6 2021-06-15 op
322 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
323 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
324 86e7d7b4 2021-06-19 op }
325 86e7d7b4 2021-06-19 op
326 86e7d7b4 2021-06-19 op static void
327 86e7d7b4 2021-06-19 op setvari(char *var, int val)
328 86e7d7b4 2021-06-19 op {
329 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
330 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
331 86e7d7b4 2021-06-19 op var, val);
332 86e7d7b4 2021-06-19 op
333 86e7d7b4 2021-06-19 op free(var);
334 69e1d1b6 2021-06-15 op }
335 69e1d1b6 2021-06-15 op
336 86e7d7b4 2021-06-19 op static void
337 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
338 86e7d7b4 2021-06-19 op {
339 95a8c791 2021-08-26 op if (!config_setvars(var, val))
340 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
341 86e7d7b4 2021-06-19 op var, val);
342 86e7d7b4 2021-06-19 op
343 86e7d7b4 2021-06-19 op free(var);
344 74a2587f 2021-06-21 op }
345 74a2587f 2021-06-21 op
346 74a2587f 2021-06-21 op static int
347 74a2587f 2021-06-21 op colorname(const char *name)
348 74a2587f 2021-06-21 op {
349 74a2587f 2021-06-21 op struct {
350 74a2587f 2021-06-21 op const char *name;
351 74a2587f 2021-06-21 op short val;
352 74a2587f 2021-06-21 op } *i, colors[] = {
353 160abe04 2021-06-21 op { "default", -1 },
354 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
355 74a2587f 2021-06-21 op { "red", COLOR_RED },
356 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
357 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
358 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
359 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
360 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
361 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
362 74a2587f 2021-06-21 op { NULL, 0 },
363 74a2587f 2021-06-21 op };
364 27766d48 2021-06-22 op const char *errstr;
365 27766d48 2021-06-22 op int n;
366 27766d48 2021-06-22 op
367 d89eb764 2022-04-24 op if (!strncmp(name, "colo", 4)) {
368 27766d48 2021-06-22 op /* people are strange */
369 d89eb764 2022-04-24 op if (!strncmp(name, "color", 5))
370 27766d48 2021-06-22 op name += 5;
371 d89eb764 2022-04-24 op else if (!strncmp(name, "colour", 6))
372 27766d48 2021-06-22 op name += 6;
373 27766d48 2021-06-22 op else
374 27766d48 2021-06-22 op goto err;
375 74a2587f 2021-06-21 op
376 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
377 27766d48 2021-06-22 op if (errstr != NULL)
378 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
379 27766d48 2021-06-22 op return n;
380 27766d48 2021-06-22 op }
381 27766d48 2021-06-22 op
382 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
383 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
384 74a2587f 2021-06-21 op return i->val;
385 74a2587f 2021-06-21 op }
386 74a2587f 2021-06-21 op
387 27766d48 2021-06-22 op err:
388 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
389 74a2587f 2021-06-21 op return -1;
390 86e7d7b4 2021-06-19 op }
391 86e7d7b4 2021-06-19 op
392 69bdd906 2021-06-15 op void
393 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
394 74a2587f 2021-06-21 op {
395 d89b86d6 2021-06-21 op int p, l, t;
396 74a2587f 2021-06-21 op
397 74a2587f 2021-06-21 op assert(current_style != NULL);
398 74a2587f 2021-06-21 op
399 160abe04 2021-06-21 op p = colorname(prfx);
400 160abe04 2021-06-21 op l = colorname(line);
401 160abe04 2021-06-21 op t = colorname(trail);
402 74a2587f 2021-06-21 op
403 9cc9d696 2021-11-03 op if (!config_setcolor(color_type == BG, current_style, p, l, t))
404 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
405 d0149485 2021-06-22 op }
406 d0149485 2021-06-22 op
407 d0149485 2021-06-22 op static int
408 3198f30f 2021-08-26 op attrname(const char *n)
409 d0149485 2021-06-22 op {
410 d0149485 2021-06-22 op struct {
411 d0149485 2021-06-22 op const char *name;
412 d0149485 2021-06-22 op unsigned int val;
413 d0149485 2021-06-22 op } *i, attrs[] = {
414 d0149485 2021-06-22 op { "normal", A_NORMAL },
415 d0149485 2021-06-22 op { "standout", A_STANDOUT },
416 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
417 d0149485 2021-06-22 op { "reverse", A_REVERSE },
418 d0149485 2021-06-22 op { "blink", A_BLINK },
419 d0149485 2021-06-22 op { "dim", A_DIM },
420 d0149485 2021-06-22 op { "bold", A_BOLD },
421 d0149485 2021-06-22 op { NULL, 0 },
422 d0149485 2021-06-22 op };
423 d0149485 2021-06-22 op int ret, found;
424 3198f30f 2021-08-26 op char *ap, *dup, *orig;
425 3198f30f 2021-08-26 op
426 3198f30f 2021-08-26 op if ((dup = strdup(n)) == NULL)
427 3198f30f 2021-08-26 op err(1, "strdup");
428 d0149485 2021-06-22 op
429 3198f30f 2021-08-26 op orig = dup;
430 3198f30f 2021-08-26 op
431 d0149485 2021-06-22 op ret = 0;
432 3198f30f 2021-08-26 op while ((ap = strsep(&dup, ",")) != NULL) {
433 d0149485 2021-06-22 op if (*ap == '\0')
434 d0149485 2021-06-22 op continue;
435 d0149485 2021-06-22 op
436 d0149485 2021-06-22 op found = 0;
437 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
438 d0149485 2021-06-22 op if (strcmp(i->name, ap))
439 d0149485 2021-06-22 op continue;
440 d0149485 2021-06-22 op ret |= i->val;
441 d0149485 2021-06-22 op found = 1;
442 d0149485 2021-06-22 op break;
443 d0149485 2021-06-22 op }
444 d0149485 2021-06-22 op
445 d0149485 2021-06-22 op if (!found)
446 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
447 d0149485 2021-06-22 op ap, yylval.colno+1);
448 d0149485 2021-06-22 op }
449 d0149485 2021-06-22 op
450 3198f30f 2021-08-26 op free(orig);
451 d0149485 2021-06-22 op return ret;
452 d0149485 2021-06-22 op }
453 d0149485 2021-06-22 op
454 d0149485 2021-06-22 op static void
455 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
456 d0149485 2021-06-22 op {
457 d0149485 2021-06-22 op int p, l, t;
458 d0149485 2021-06-22 op
459 d0149485 2021-06-22 op assert(current_style != NULL);
460 d0149485 2021-06-22 op
461 d0149485 2021-06-22 op p = attrname(prfx);
462 d0149485 2021-06-22 op l = attrname(line);
463 d0149485 2021-06-22 op t = attrname(trail);
464 d0149485 2021-06-22 op
465 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
466 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
467 74a2587f 2021-06-21 op }
468 74a2587f 2021-06-21 op
469 984245ce 2021-06-23 op static void
470 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
471 984245ce 2021-06-23 op {
472 46102ea3 2022-12-23 op static struct iri iri;
473 984245ce 2021-06-23 op struct proxy *p;
474 984245ce 2021-06-23 op
475 46102ea3 2022-12-23 op if (iri_parse(NULL, proxy, &iri) == -1) {
476 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
477 984245ce 2021-06-23 op return;
478 984245ce 2021-06-23 op }
479 984245ce 2021-06-23 op
480 4f147cdf 2022-12-25 op if ((iri.iri_flags & (IH_QUERY|IH_FRAGMENT)) ||
481 4f147cdf 2022-12-25 op iri.iri_path[0] != '\0') {
482 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
483 984245ce 2021-06-23 op return;
484 984245ce 2021-06-23 op }
485 984245ce 2021-06-23 op
486 46102ea3 2022-12-23 op if (strcmp(iri.iri_scheme, "gemini")) {
487 46102ea3 2022-12-23 op yyerror("disallowed proxy protocol %s", iri.iri_scheme);
488 984245ce 2021-06-23 op return;
489 984245ce 2021-06-23 op }
490 984245ce 2021-06-23 op
491 984245ce 2021-06-23 op if ((p = calloc(1, sizeof(*p))) == NULL)
492 984245ce 2021-06-23 op err(1, "calloc");
493 984245ce 2021-06-23 op
494 984245ce 2021-06-23 op p->match_proto = proto;
495 8da4eaff 2021-07-25 op p->proto = PROTO_GEMINI;
496 984245ce 2021-06-23 op
497 46102ea3 2022-12-23 op if ((p->host = strdup(iri.iri_host)) == NULL)
498 984245ce 2021-06-23 op err(1, "strdup");
499 984245ce 2021-06-23 op
500 46102ea3 2022-12-23 op if ((p->port = strdup(iri.iri_portstr)) == NULL)
501 984245ce 2021-06-23 op err(1, "strdup");
502 984245ce 2021-06-23 op
503 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
504 e3427d18 2021-06-25 op }
505 e3427d18 2021-06-25 op
506 e3427d18 2021-06-25 op static interactivefn *
507 e3427d18 2021-06-25 op cmdname(const char *name)
508 e3427d18 2021-06-25 op {
509 e3427d18 2021-06-25 op struct cmd *cmd;
510 e3427d18 2021-06-25 op
511 95a8c791 2021-08-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
512 e3427d18 2021-06-25 op if (!strcmp(cmd->cmd, name))
513 e3427d18 2021-06-25 op return cmd->fn;
514 e3427d18 2021-06-25 op }
515 e3427d18 2021-06-25 op
516 e3427d18 2021-06-25 op return NULL;
517 e3427d18 2021-06-25 op }
518 e3427d18 2021-06-25 op
519 e3427d18 2021-06-25 op static void
520 e3427d18 2021-06-25 op bindkey(const char *map, const char *key, const char *cmd)
521 e3427d18 2021-06-25 op {
522 e3427d18 2021-06-25 op struct kmap *kmap;
523 e3427d18 2021-06-25 op interactivefn *fn;
524 e3427d18 2021-06-25 op
525 e3427d18 2021-06-25 op if (!strcmp(map, "global-map"))
526 e3427d18 2021-06-25 op kmap = &global_map;
527 e3427d18 2021-06-25 op else if (!strcmp(map, "minibuffer-map"))
528 e3427d18 2021-06-25 op kmap = &minibuffer_map;
529 e3427d18 2021-06-25 op else {
530 e3427d18 2021-06-25 op yyerror("unknown map: %s", map);
531 e3427d18 2021-06-25 op return;
532 e3427d18 2021-06-25 op }
533 e3427d18 2021-06-25 op
534 e3427d18 2021-06-25 op if ((fn = cmdname(cmd)) == NULL) {
535 e3427d18 2021-06-25 op yyerror("unknown cmd: %s", fn);
536 e3427d18 2021-06-25 op return;
537 e3427d18 2021-06-25 op }
538 e3427d18 2021-06-25 op
539 e3427d18 2021-06-25 op if (!kmap_define_key(kmap, key, fn))
540 e3427d18 2021-06-25 op yyerror("failed to bind %s %s %s", map, key, cmd);
541 984245ce 2021-06-23 op }
542 984245ce 2021-06-23 op
543 21404dd9 2021-07-15 op static void
544 21404dd9 2021-07-15 op do_parseconfig(const char *filename, int fonf)
545 69bdd906 2021-06-15 op {
546 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
547 69bdd906 2021-06-15 op if (fonf)
548 69bdd906 2021-06-15 op err(1, "%s", filename);
549 69bdd906 2021-06-15 op return;
550 69bdd906 2021-06-15 op }
551 69bdd906 2021-06-15 op
552 69bdd906 2021-06-15 op path = filename;
553 69bdd906 2021-06-15 op yyparse();
554 69bdd906 2021-06-15 op fclose(yyfp);
555 69bdd906 2021-06-15 op if (parse_errors)
556 69bdd906 2021-06-15 op exit(1);
557 69bdd906 2021-06-15 op }
558 21404dd9 2021-07-15 op
559 21404dd9 2021-07-15 op void
560 21404dd9 2021-07-15 op parseconfig(const char *filename, int fonf)
561 21404dd9 2021-07-15 op {
562 21404dd9 2021-07-15 op char altconf[PATH_MAX], *term;
563 21404dd9 2021-07-15 op
564 21404dd9 2021-07-15 op /* load the given config file */
565 21404dd9 2021-07-15 op do_parseconfig(filename, fonf);
566 21404dd9 2021-07-15 op
567 21404dd9 2021-07-15 op /* then try to load file-TERM */
568 21404dd9 2021-07-15 op
569 21404dd9 2021-07-15 op if ((term = getenv("TERM")) == NULL)
570 21404dd9 2021-07-15 op return;
571 21404dd9 2021-07-15 op
572 21404dd9 2021-07-15 op strlcpy(altconf, filename, sizeof(altconf));
573 21404dd9 2021-07-15 op strlcat(altconf, "-", sizeof(altconf));
574 21404dd9 2021-07-15 op strlcat(altconf, term, sizeof(altconf));
575 21404dd9 2021-07-15 op
576 21404dd9 2021-07-15 op do_parseconfig(altconf, 0);
577 21404dd9 2021-07-15 op }