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 69bdd906 2021-06-15 op
41 69bdd906 2021-06-15 op typedef struct {
42 69bdd906 2021-06-15 op union {
43 69bdd906 2021-06-15 op char *str;
44 69bdd906 2021-06-15 op int num;
45 69bdd906 2021-06-15 op };
46 69bdd906 2021-06-15 op int lineno;
47 69bdd906 2021-06-15 op int colno;
48 69bdd906 2021-06-15 op } yystype;
49 69bdd906 2021-06-15 op #define YYSTYPE yystype
50 69bdd906 2021-06-15 op
51 69e1d1b6 2021-06-15 op static char *current_style;
52 d89b86d6 2021-06-21 op static int color_type;
53 74a2587f 2021-06-21 op
54 69bdd906 2021-06-15 op static const char *path;
55 69bdd906 2021-06-15 op
56 69bdd906 2021-06-15 op FILE *yyfp;
57 69bdd906 2021-06-15 op
58 69bdd906 2021-06-15 op int parse_errors = 0;
59 69bdd906 2021-06-15 op
60 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
61 69bdd906 2021-06-15 op static int yylex(void);
62 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
63 86e7d7b4 2021-06-19 op static void setvari(char *, int);
64 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
65 2513365f 2024-01-16 op static void setvarb(char *, int);
66 74a2587f 2021-06-21 op static int colorname(const char *);
67 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
68 3198f30f 2021-08-26 op static int attrname(const char *);
69 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
70 984245ce 2021-06-23 op static void add_proxy(char *, char *);
71 e3427d18 2021-06-25 op static void bindkey(const char *, const char *, const char *);
72 21404dd9 2021-07-15 op static void do_parseconfig(const char *, int);
73 69bdd906 2021-06-15 op
74 69bdd906 2021-06-15 op %}
75 69bdd906 2021-06-15 op
76 bd3e6837 2021-11-03 op %token ATTR
77 bd3e6837 2021-11-03 op %token BIND BG
78 bd3e6837 2021-11-03 op %token CONT
79 bd3e6837 2021-11-03 op %token FG
80 bd3e6837 2021-11-03 op %token PRFX PROXY
81 bd3e6837 2021-11-03 op %token SET STYLE
82 bd3e6837 2021-11-03 op %token UNBIND
83 bd3e6837 2021-11-03 op %token VIA
84 69bdd906 2021-06-15 op
85 2513365f 2024-01-16 op /* Sigh... they conflict with ncurses TRUE and FALSE */
86 2513365f 2024-01-16 op %token TOK_TRUE TOK_FALSE
87 2513365f 2024-01-16 op
88 9cc9d696 2021-11-03 op %token <str> STRING
89 9cc9d696 2021-11-03 op %token <num> NUMBER
90 2513365f 2024-01-16 op %type <num> bool
91 69bdd906 2021-06-15 op
92 69bdd906 2021-06-15 op %%
93 69bdd906 2021-06-15 op
94 69bdd906 2021-06-15 op grammar : /* empty */
95 69bdd906 2021-06-15 op | grammar '\n'
96 69bdd906 2021-06-15 op | grammar rule '\n'
97 69bdd906 2021-06-15 op | error '\n'
98 2513365f 2024-01-16 op ;
99 2513365f 2024-01-16 op
100 2513365f 2024-01-16 op bool : TOK_TRUE { $$ = 1; }
101 2513365f 2024-01-16 op | TOK_FALSE { $$ = 0; }
102 69bdd906 2021-06-15 op ;
103 69bdd906 2021-06-15 op
104 69e1d1b6 2021-06-15 op rule : set
105 74a2587f 2021-06-21 op | style {
106 74a2587f 2021-06-21 op free(current_style);
107 74a2587f 2021-06-21 op current_style = NULL;
108 74a2587f 2021-06-21 op }
109 69e1d1b6 2021-06-15 op | bind
110 69e1d1b6 2021-06-15 op | unbind
111 984245ce 2021-06-23 op | proxy
112 69e1d1b6 2021-06-15 op ;
113 69bdd906 2021-06-15 op
114 9cc9d696 2021-11-03 op set : SET STRING '=' STRING { setvars($2, $4); }
115 9cc9d696 2021-11-03 op | SET STRING '=' NUMBER { setvari($2, $4); }
116 2513365f 2024-01-16 op | SET STRING '=' bool { setvarb($2, $4); }
117 69bdd906 2021-06-15 op ;
118 69bdd906 2021-06-15 op
119 9cc9d696 2021-11-03 op style : STYLE STRING { current_style = $2; } stylespec ;
120 5c38fd5f 2021-08-30 op stylespec : styleopt | '{' optnl styleopts '}' ;
121 69bdd906 2021-06-15 op
122 69bdd906 2021-06-15 op styleopts : /* empty */
123 0a53787c 2021-07-09 op | styleopts styleopt optnl
124 69bdd906 2021-06-15 op ;
125 69bdd906 2021-06-15 op
126 9cc9d696 2021-11-03 op styleopt : PRFX STRING { setprfx($2, $2); }
127 9cc9d696 2021-11-03 op | PRFX STRING STRING { setprfx($2, $3); }
128 9cc9d696 2021-11-03 op | BG { color_type = BG; } colorspec
129 9cc9d696 2021-11-03 op | FG { color_type = FG; } colorspec
130 9cc9d696 2021-11-03 op | ATTR attr
131 d89b86d6 2021-06-21 op ;
132 d89b86d6 2021-06-21 op
133 9cc9d696 2021-11-03 op colorspec : STRING { setcolor($1, $1, $1); free($1); }
134 9cc9d696 2021-11-03 op | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
135 9cc9d696 2021-11-03 op | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
136 69bdd906 2021-06-15 op ;
137 69bdd906 2021-06-15 op
138 9cc9d696 2021-11-03 op attr : STRING { setattr($1, $1, $1); free($1); }
139 9cc9d696 2021-11-03 op | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
140 9cc9d696 2021-11-03 op | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
141 d0149485 2021-06-22 op ;
142 d0149485 2021-06-22 op
143 9cc9d696 2021-11-03 op bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
144 69bdd906 2021-06-15 op ;
145 69bdd906 2021-06-15 op
146 9cc9d696 2021-11-03 op unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
147 69bdd906 2021-06-15 op ;
148 69bdd906 2021-06-15 op
149 9cc9d696 2021-11-03 op proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
150 0a53787c 2021-07-09 op ;
151 0a53787c 2021-07-09 op
152 0a53787c 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
153 0a53787c 2021-07-09 op | /* empty */
154 984245ce 2021-06-23 op ;
155 984245ce 2021-06-23 op
156 69bdd906 2021-06-15 op %%
157 69bdd906 2021-06-15 op
158 69bdd906 2021-06-15 op void
159 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
160 69bdd906 2021-06-15 op {
161 69bdd906 2021-06-15 op va_list va;
162 69bdd906 2021-06-15 op
163 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
164 69bdd906 2021-06-15 op va_start(va, fmt);
165 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
166 69bdd906 2021-06-15 op va_end(va);
167 69bdd906 2021-06-15 op fprintf(stderr, "\n");
168 69bdd906 2021-06-15 op parse_errors++;
169 69bdd906 2021-06-15 op }
170 69bdd906 2021-06-15 op
171 69bdd906 2021-06-15 op static struct keyword {
172 69bdd906 2021-06-15 op const char *word;
173 69bdd906 2021-06-15 op int token;
174 69bdd906 2021-06-15 op } keywords[] = {
175 9cc9d696 2021-11-03 op { "attr", ATTR },
176 9cc9d696 2021-11-03 op { "bg", BG },
177 9cc9d696 2021-11-03 op { "bind", BIND },
178 9cc9d696 2021-11-03 op { "cont", CONT },
179 2513365f 2024-01-16 op { "false", TOK_FALSE },
180 9cc9d696 2021-11-03 op { "fg", FG },
181 9cc9d696 2021-11-03 op { "prefix", PRFX },
182 9cc9d696 2021-11-03 op { "proxy", PROXY },
183 9cc9d696 2021-11-03 op { "set", SET },
184 9cc9d696 2021-11-03 op { "style", STYLE },
185 2513365f 2024-01-16 op { "true", TOK_TRUE },
186 9cc9d696 2021-11-03 op { "unbind", UNBIND },
187 9cc9d696 2021-11-03 op { "via", VIA },
188 69bdd906 2021-06-15 op };
189 69bdd906 2021-06-15 op
190 69bdd906 2021-06-15 op int
191 69bdd906 2021-06-15 op yylex(void)
192 69bdd906 2021-06-15 op {
193 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
194 69bdd906 2021-06-15 op const char *errstr;
195 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
196 69bdd906 2021-06-15 op size_t i;
197 69bdd906 2021-06-15 op
198 69bdd906 2021-06-15 op p = buf;
199 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
200 69bdd906 2021-06-15 op
201 69bdd906 2021-06-15 op repeat:
202 69bdd906 2021-06-15 op /* skip whitespace first */
203 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
204 69bdd906 2021-06-15 op yylval.colno++;
205 69bdd906 2021-06-15 op
206 69bdd906 2021-06-15 op /* check for special one-character constructions */
207 69bdd906 2021-06-15 op switch (c) {
208 c86d164e 2021-06-24 op case '\r':
209 c86d164e 2021-06-24 op /* silently eat up any \r */
210 c86d164e 2021-06-24 op goto repeat;
211 69bdd906 2021-06-15 op case '\n':
212 69bdd906 2021-06-15 op yylval.colno = 0;
213 69bdd906 2021-06-15 op yylval.lineno++;
214 69bdd906 2021-06-15 op /* fallthrough */
215 69bdd906 2021-06-15 op case '{':
216 69bdd906 2021-06-15 op case '}':
217 69bdd906 2021-06-15 op case '=':
218 69bdd906 2021-06-15 op return c;
219 69bdd906 2021-06-15 op case '#':
220 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
221 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
222 69bdd906 2021-06-15 op if (c == EOF)
223 69bdd906 2021-06-15 op goto eof;
224 69bdd906 2021-06-15 op yylval.colno = 0;
225 69bdd906 2021-06-15 op yylval.lineno++;
226 69bdd906 2021-06-15 op return c;
227 69bdd906 2021-06-15 op case EOF:
228 69bdd906 2021-06-15 op goto eof;
229 69bdd906 2021-06-15 op }
230 69bdd906 2021-06-15 op
231 69bdd906 2021-06-15 op /* parsing next word */
232 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
233 69bdd906 2021-06-15 op switch (c) {
234 69bdd906 2021-06-15 op case '\0':
235 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
236 69bdd906 2021-06-15 op yylval.colno+1);
237 69bdd906 2021-06-15 op escape = 0;
238 69bdd906 2021-06-15 op continue;
239 69bdd906 2021-06-15 op case '\\':
240 69bdd906 2021-06-15 op escape = !escape;
241 69bdd906 2021-06-15 op if (escape)
242 69bdd906 2021-06-15 op continue;
243 69bdd906 2021-06-15 op break;
244 6ba9e5a8 2021-06-24 op case '\r':
245 6ba9e5a8 2021-06-24 op /* ignore \r here too */
246 6ba9e5a8 2021-06-24 op continue;
247 69bdd906 2021-06-15 op case '\n':
248 69bdd906 2021-06-15 op if (quotes)
249 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
250 69bdd906 2021-06-15 op yylval.colno+1);
251 69bdd906 2021-06-15 op if (escape) {
252 69bdd906 2021-06-15 op nonkw = 1;
253 69bdd906 2021-06-15 op escape = 0;
254 69bdd906 2021-06-15 op yylval.colno = 0;
255 69bdd906 2021-06-15 op yylval.lineno++;
256 69bdd906 2021-06-15 op }
257 69bdd906 2021-06-15 op goto eow;
258 69bdd906 2021-06-15 op case EOF:
259 69bdd906 2021-06-15 op if (escape)
260 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
261 69bdd906 2021-06-15 op yylval.colno);
262 69bdd906 2021-06-15 op if (quotes)
263 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
264 69bdd906 2021-06-15 op qpos + 1);
265 69bdd906 2021-06-15 op goto eow;
266 69bdd906 2021-06-15 op case '{':
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 '\t':
272 69bdd906 2021-06-15 op if (!escape && !quotes)
273 69bdd906 2021-06-15 op goto eow;
274 69bdd906 2021-06-15 op break;
275 69bdd906 2021-06-15 op case '"':
276 69bdd906 2021-06-15 op if (!escape) {
277 69bdd906 2021-06-15 op quotes = !quotes;
278 69bdd906 2021-06-15 op if (quotes) {
279 69bdd906 2021-06-15 op nonkw = 1;
280 69bdd906 2021-06-15 op qpos = yylval.colno;
281 69bdd906 2021-06-15 op }
282 69bdd906 2021-06-15 op continue;
283 69bdd906 2021-06-15 op }
284 69bdd906 2021-06-15 op }
285 69bdd906 2021-06-15 op *p++ = c;
286 69bdd906 2021-06-15 op if (p == ebuf) {
287 69bdd906 2021-06-15 op yyerror("line too long");
288 69bdd906 2021-06-15 op p = buf;
289 69bdd906 2021-06-15 op }
290 69bdd906 2021-06-15 op escape = 0;
291 69bdd906 2021-06-15 op }
292 69bdd906 2021-06-15 op
293 69bdd906 2021-06-15 op eow:
294 69bdd906 2021-06-15 op *p = 0;
295 69bdd906 2021-06-15 op if (c != EOF)
296 69bdd906 2021-06-15 op ungetc(c, yyfp);
297 69bdd906 2021-06-15 op if (p == buf) {
298 69bdd906 2021-06-15 op /*
299 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
300 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
301 69bdd906 2021-06-15 op * the main loop.
302 69bdd906 2021-06-15 op */
303 69bdd906 2021-06-15 op if (c == EOF)
304 69bdd906 2021-06-15 op goto eof;
305 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
306 69bdd906 2021-06-15 op goto repeat;
307 69bdd906 2021-06-15 op }
308 69bdd906 2021-06-15 op if (!nonkw) {
309 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
310 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
311 69bdd906 2021-06-15 op return keywords[i].token;
312 69bdd906 2021-06-15 op }
313 69bdd906 2021-06-15 op }
314 69bdd906 2021-06-15 op c = *buf;
315 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
316 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
317 69bdd906 2021-06-15 op if (errstr != NULL)
318 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
319 9cc9d696 2021-11-03 op return NUMBER;
320 69bdd906 2021-06-15 op }
321 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
322 69bdd906 2021-06-15 op err(1, "%s", __func__);
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 3198f30f 2021-08-26 op if ((dup = strdup(n)) == NULL)
457 3198f30f 2021-08-26 op err(1, "strdup");
458 d0149485 2021-06-22 op
459 3198f30f 2021-08-26 op orig = dup;
460 3198f30f 2021-08-26 op
461 d0149485 2021-06-22 op ret = 0;
462 3198f30f 2021-08-26 op while ((ap = strsep(&dup, ",")) != NULL) {
463 d0149485 2021-06-22 op if (*ap == '\0')
464 d0149485 2021-06-22 op continue;
465 d0149485 2021-06-22 op
466 d0149485 2021-06-22 op found = 0;
467 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
468 d0149485 2021-06-22 op if (strcmp(i->name, ap))
469 d0149485 2021-06-22 op continue;
470 d0149485 2021-06-22 op ret |= i->val;
471 d0149485 2021-06-22 op found = 1;
472 d0149485 2021-06-22 op break;
473 d0149485 2021-06-22 op }
474 d0149485 2021-06-22 op
475 d0149485 2021-06-22 op if (!found)
476 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
477 d0149485 2021-06-22 op ap, yylval.colno+1);
478 d0149485 2021-06-22 op }
479 d0149485 2021-06-22 op
480 3198f30f 2021-08-26 op free(orig);
481 d0149485 2021-06-22 op return ret;
482 d0149485 2021-06-22 op }
483 d0149485 2021-06-22 op
484 d0149485 2021-06-22 op static void
485 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
486 d0149485 2021-06-22 op {
487 d0149485 2021-06-22 op int p, l, t;
488 d0149485 2021-06-22 op
489 d0149485 2021-06-22 op assert(current_style != NULL);
490 d0149485 2021-06-22 op
491 d0149485 2021-06-22 op p = attrname(prfx);
492 d0149485 2021-06-22 op l = attrname(line);
493 d0149485 2021-06-22 op t = attrname(trail);
494 d0149485 2021-06-22 op
495 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
496 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
497 74a2587f 2021-06-21 op }
498 74a2587f 2021-06-21 op
499 984245ce 2021-06-23 op static void
500 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
501 984245ce 2021-06-23 op {
502 46102ea3 2022-12-23 op static struct iri iri;
503 984245ce 2021-06-23 op struct proxy *p;
504 984245ce 2021-06-23 op
505 46102ea3 2022-12-23 op if (iri_parse(NULL, proxy, &iri) == -1) {
506 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
507 984245ce 2021-06-23 op return;
508 984245ce 2021-06-23 op }
509 984245ce 2021-06-23 op
510 4f147cdf 2022-12-25 op if ((iri.iri_flags & (IH_QUERY|IH_FRAGMENT)) ||
511 4f147cdf 2022-12-25 op iri.iri_path[0] != '\0') {
512 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
513 984245ce 2021-06-23 op return;
514 984245ce 2021-06-23 op }
515 984245ce 2021-06-23 op
516 46102ea3 2022-12-23 op if (strcmp(iri.iri_scheme, "gemini")) {
517 46102ea3 2022-12-23 op yyerror("disallowed proxy protocol %s", iri.iri_scheme);
518 984245ce 2021-06-23 op return;
519 984245ce 2021-06-23 op }
520 984245ce 2021-06-23 op
521 984245ce 2021-06-23 op if ((p = calloc(1, sizeof(*p))) == NULL)
522 984245ce 2021-06-23 op err(1, "calloc");
523 984245ce 2021-06-23 op
524 984245ce 2021-06-23 op p->match_proto = proto;
525 8da4eaff 2021-07-25 op p->proto = PROTO_GEMINI;
526 984245ce 2021-06-23 op
527 46102ea3 2022-12-23 op if ((p->host = strdup(iri.iri_host)) == NULL)
528 984245ce 2021-06-23 op err(1, "strdup");
529 984245ce 2021-06-23 op
530 46102ea3 2022-12-23 op if ((p->port = strdup(iri.iri_portstr)) == NULL)
531 984245ce 2021-06-23 op err(1, "strdup");
532 984245ce 2021-06-23 op
533 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
534 e3427d18 2021-06-25 op }
535 e3427d18 2021-06-25 op
536 e3427d18 2021-06-25 op static interactivefn *
537 e3427d18 2021-06-25 op cmdname(const char *name)
538 e3427d18 2021-06-25 op {
539 e3427d18 2021-06-25 op struct cmd *cmd;
540 e3427d18 2021-06-25 op
541 95a8c791 2021-08-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
542 e3427d18 2021-06-25 op if (!strcmp(cmd->cmd, name))
543 e3427d18 2021-06-25 op return cmd->fn;
544 e3427d18 2021-06-25 op }
545 e3427d18 2021-06-25 op
546 e3427d18 2021-06-25 op return NULL;
547 e3427d18 2021-06-25 op }
548 e3427d18 2021-06-25 op
549 e3427d18 2021-06-25 op static void
550 e3427d18 2021-06-25 op bindkey(const char *map, const char *key, const char *cmd)
551 e3427d18 2021-06-25 op {
552 e3427d18 2021-06-25 op struct kmap *kmap;
553 e3427d18 2021-06-25 op interactivefn *fn;
554 e3427d18 2021-06-25 op
555 e3427d18 2021-06-25 op if (!strcmp(map, "global-map"))
556 e3427d18 2021-06-25 op kmap = &global_map;
557 e3427d18 2021-06-25 op else if (!strcmp(map, "minibuffer-map"))
558 e3427d18 2021-06-25 op kmap = &minibuffer_map;
559 e3427d18 2021-06-25 op else {
560 e3427d18 2021-06-25 op yyerror("unknown map: %s", map);
561 e3427d18 2021-06-25 op return;
562 e3427d18 2021-06-25 op }
563 e3427d18 2021-06-25 op
564 e3427d18 2021-06-25 op if ((fn = cmdname(cmd)) == NULL) {
565 e3427d18 2021-06-25 op yyerror("unknown cmd: %s", fn);
566 e3427d18 2021-06-25 op return;
567 e3427d18 2021-06-25 op }
568 e3427d18 2021-06-25 op
569 e3427d18 2021-06-25 op if (!kmap_define_key(kmap, key, fn))
570 e3427d18 2021-06-25 op yyerror("failed to bind %s %s %s", map, key, cmd);
571 984245ce 2021-06-23 op }
572 984245ce 2021-06-23 op
573 21404dd9 2021-07-15 op static void
574 21404dd9 2021-07-15 op do_parseconfig(const char *filename, int fonf)
575 69bdd906 2021-06-15 op {
576 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
577 69bdd906 2021-06-15 op if (fonf)
578 69bdd906 2021-06-15 op err(1, "%s", filename);
579 69bdd906 2021-06-15 op return;
580 69bdd906 2021-06-15 op }
581 69bdd906 2021-06-15 op
582 69bdd906 2021-06-15 op path = filename;
583 69bdd906 2021-06-15 op yyparse();
584 69bdd906 2021-06-15 op fclose(yyfp);
585 69bdd906 2021-06-15 op if (parse_errors)
586 69bdd906 2021-06-15 op exit(1);
587 69bdd906 2021-06-15 op }
588 21404dd9 2021-07-15 op
589 21404dd9 2021-07-15 op void
590 21404dd9 2021-07-15 op parseconfig(const char *filename, int fonf)
591 21404dd9 2021-07-15 op {
592 21404dd9 2021-07-15 op char altconf[PATH_MAX], *term;
593 21404dd9 2021-07-15 op
594 21404dd9 2021-07-15 op /* load the given config file */
595 21404dd9 2021-07-15 op do_parseconfig(filename, fonf);
596 21404dd9 2021-07-15 op
597 21404dd9 2021-07-15 op /* then try to load file-TERM */
598 21404dd9 2021-07-15 op
599 21404dd9 2021-07-15 op if ((term = getenv("TERM")) == NULL)
600 21404dd9 2021-07-15 op return;
601 21404dd9 2021-07-15 op
602 21404dd9 2021-07-15 op strlcpy(altconf, filename, sizeof(altconf));
603 21404dd9 2021-07-15 op strlcat(altconf, "-", sizeof(altconf));
604 21404dd9 2021-07-15 op strlcat(altconf, term, sizeof(altconf));
605 21404dd9 2021-07-15 op
606 21404dd9 2021-07-15 op do_parseconfig(altconf, 0);
607 21404dd9 2021-07-15 op }