2 69bdd906 2021-06-15 op * Much of the design is taken from doas (parse.y rev 1.29)
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>
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.
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.
23 69bdd906 2021-06-15 op #include "telescope.h"
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>
34 69bdd906 2021-06-15 op typedef struct {
42 69bdd906 2021-06-15 op #define YYSTYPE yystype
44 69e1d1b6 2021-06-15 op static char *current_style;
45 74a2587f 2021-06-21 op static int current_bg, current_fg;
47 69bdd906 2021-06-15 op static const char *path;
51 69bdd906 2021-06-15 op int parse_errors = 0;
53 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
54 69bdd906 2021-06-15 op static int yylex(void);
55 2ca05f73 2021-06-18 op static void setprfx(int, const char *);
56 86e7d7b4 2021-06-19 op static void setvari(char *, int);
57 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
58 74a2587f 2021-06-21 op static int colorname(const char *);
59 74a2587f 2021-06-21 op static void setcolor(int, int, const char *);
64 69bdd906 2021-06-15 op %token TSTYLE TPRFX TCONT TBG TFG TATTR TBOLD TUNDERLINE
65 69bdd906 2021-06-15 op %token TBIND TUNBIND
67 69bdd906 2021-06-15 op %token <str> TSTRING
68 69bdd906 2021-06-15 op %token <num> TNUMBER
72 69bdd906 2021-06-15 op grammar : /* empty */
73 69bdd906 2021-06-15 op | grammar '\n'
74 69bdd906 2021-06-15 op | grammar rule '\n'
80 74a2587f 2021-06-21 op free(current_style);
81 74a2587f 2021-06-21 op current_style = NULL;
87 86e7d7b4 2021-06-19 op set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
88 86e7d7b4 2021-06-19 op | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
91 74a2587f 2021-06-21 op style : TSTYLE TSTRING { current_style = $2; } stylespec ;
92 74a2587f 2021-06-21 op stylespec : styleopt | '{' styleopts '}' ;
94 69bdd906 2021-06-15 op styleopts : /* empty */
95 69bdd906 2021-06-15 op | styleopts '\n'
96 69bdd906 2021-06-15 op | styleopts styleopt '\n'
99 74a2587f 2021-06-21 op styleopt : TPRFX TSTRING { setprfx(0, $2); }
100 74a2587f 2021-06-21 op | TCONT TSTRING { setprfx(1, $2); }
101 74a2587f 2021-06-21 op | TBG TSTRING {
102 74a2587f 2021-06-21 op setcolor(0, 1, $2);
103 74a2587f 2021-06-21 op setcolor(1, 1, $2);
106 74a2587f 2021-06-21 op | TFG TSTRING {
107 74a2587f 2021-06-21 op setcolor(0, 0, $2);
108 74a2587f 2021-06-21 op setcolor(1, 0, $2);
111 69bdd906 2021-06-15 op | TATTR TBOLD { printf("style attr setted to bold\n"); }
112 69bdd906 2021-06-15 op | TATTR TUNDERLINE { printf("style attr setted to underline\n"); }
115 69bdd906 2021-06-15 op bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
118 69bdd906 2021-06-15 op unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
124 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
128 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
129 69bdd906 2021-06-15 op va_start(va, fmt);
130 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
132 69bdd906 2021-06-15 op fprintf(stderr, "\n");
133 69bdd906 2021-06-15 op parse_errors++;
136 69bdd906 2021-06-15 op static struct keyword {
137 69bdd906 2021-06-15 op const char *word;
139 69bdd906 2021-06-15 op } keywords[] = {
140 69bdd906 2021-06-15 op { "set", TSET },
141 69bdd906 2021-06-15 op { "style", TSTYLE },
142 69bdd906 2021-06-15 op { "prefix", TPRFX },
143 69bdd906 2021-06-15 op { "cont", TCONT },
144 74a2587f 2021-06-21 op { "bg", TBG },
145 74a2587f 2021-06-21 op { "fg", TFG },
146 69bdd906 2021-06-15 op { "attr", TATTR },
147 69bdd906 2021-06-15 op { "bold", TBOLD },
148 69bdd906 2021-06-15 op { "underline", TUNDERLINE },
149 69bdd906 2021-06-15 op { "bind", TBIND },
150 69bdd906 2021-06-15 op { "unbind", TUNBIND },
156 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
157 69bdd906 2021-06-15 op const char *errstr;
158 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
162 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
165 69bdd906 2021-06-15 op /* skip whitespace first */
166 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
167 69bdd906 2021-06-15 op yylval.colno++;
169 69bdd906 2021-06-15 op /* check for special one-character constructions */
172 69bdd906 2021-06-15 op yylval.colno = 0;
173 69bdd906 2021-06-15 op yylval.lineno++;
174 69bdd906 2021-06-15 op /* fallthrough */
180 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
181 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
182 69bdd906 2021-06-15 op if (c == EOF)
184 69bdd906 2021-06-15 op yylval.colno = 0;
185 69bdd906 2021-06-15 op yylval.lineno++;
191 69bdd906 2021-06-15 op /* parsing next word */
192 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
195 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
196 69bdd906 2021-06-15 op yylval.colno+1);
200 69bdd906 2021-06-15 op escape = !escape;
206 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
207 69bdd906 2021-06-15 op yylval.colno+1);
208 69bdd906 2021-06-15 op if (escape) {
211 69bdd906 2021-06-15 op yylval.colno = 0;
212 69bdd906 2021-06-15 op yylval.lineno++;
217 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
218 69bdd906 2021-06-15 op yylval.colno);
220 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
229 69bdd906 2021-06-15 op if (!escape && !quotes)
233 69bdd906 2021-06-15 op if (!escape) {
234 69bdd906 2021-06-15 op quotes = !quotes;
235 69bdd906 2021-06-15 op if (quotes) {
237 69bdd906 2021-06-15 op qpos = yylval.colno;
243 69bdd906 2021-06-15 op if (p == ebuf) {
244 69bdd906 2021-06-15 op yyerror("line too long");
252 69bdd906 2021-06-15 op if (c != EOF)
253 69bdd906 2021-06-15 op ungetc(c, yyfp);
254 69bdd906 2021-06-15 op if (p == buf) {
256 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
257 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
258 69bdd906 2021-06-15 op * the main loop.
260 69bdd906 2021-06-15 op if (c == EOF)
262 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
265 69bdd906 2021-06-15 op if (!nonkw) {
266 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
267 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
268 69bdd906 2021-06-15 op return keywords[i].token;
272 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
273 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
274 69bdd906 2021-06-15 op if (errstr != NULL)
275 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
276 69bdd906 2021-06-15 op return TNUMBER;
278 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
279 69bdd906 2021-06-15 op err(1, "%s", __func__);
280 69bdd906 2021-06-15 op yylval.str = str;
281 69bdd906 2021-06-15 op return TSTRING;
284 69bdd906 2021-06-15 op if (ferror(yyfp))
285 69bdd906 2021-06-15 op yyerror("input error reading config");
290 69e1d1b6 2021-06-15 op setprfx(int cont, const char *name)
292 74a2587f 2021-06-21 op assert(current_style != NULL);
294 69e1d1b6 2021-06-15 op if (!config_setprfx(current_style, cont, name))
295 69e1d1b6 2021-06-15 op yyerror("invalid style %s", name);
299 86e7d7b4 2021-06-19 op setvari(char *var, int val)
301 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
302 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
309 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
311 86e7d7b4 2021-06-19 op if (!config_setvars(var, val))
312 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
319 74a2587f 2021-06-21 op colorname(const char *name)
322 74a2587f 2021-06-21 op const char *name;
324 74a2587f 2021-06-21 op } *i, colors[] = {
325 74a2587f 2021-06-21 op { "default", 0 },
326 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
327 74a2587f 2021-06-21 op { "red", COLOR_RED },
328 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
329 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
330 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
331 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
332 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
333 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
337 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
338 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
339 74a2587f 2021-06-21 op return i->val;
342 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
347 74a2587f 2021-06-21 op setcolor(int prfx, int bg, const char *color)
351 74a2587f 2021-06-21 op assert(current_style != NULL);
353 74a2587f 2021-06-21 op if ((c = colorname(color)) == -1)
356 74a2587f 2021-06-21 op if (!config_setcolor(current_style, prfx, bg, c))
357 74a2587f 2021-06-21 op yyerror("can't set color \"%s\" for %s", color,
358 74a2587f 2021-06-21 op current_style);
362 69bdd906 2021-06-15 op parseconfig(const char *filename, int fonf)
366 74a2587f 2021-06-21 op printf("proceed?\n");
367 74a2587f 2021-06-21 op read(0, &c, 1);
370 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
372 69bdd906 2021-06-15 op err(1, "%s", filename);
376 69bdd906 2021-06-15 op path = filename;
378 69bdd906 2021-06-15 op fclose(yyfp);
379 69bdd906 2021-06-15 op if (parse_errors)