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