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