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 69bdd906 2021-06-15 op #include <ctype.h>
26 69bdd906 2021-06-15 op #include <limits.h>
27 69bdd906 2021-06-15 op #include <stdarg.h>
28 69bdd906 2021-06-15 op #include <stdio.h>
29 69bdd906 2021-06-15 op #include <stdlib.h>
30 69bdd906 2021-06-15 op #include <string.h>
32 69bdd906 2021-06-15 op typedef struct {
40 69bdd906 2021-06-15 op #define YYSTYPE yystype
42 69bdd906 2021-06-15 op static const char *path;
46 69bdd906 2021-06-15 op int parse_errors = 0;
48 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
49 69bdd906 2021-06-15 op static int yylex(void);
54 69bdd906 2021-06-15 op %token TSTYLE TPRFX TCONT TBG TFG TATTR TBOLD TUNDERLINE
55 69bdd906 2021-06-15 op %token TBIND TUNBIND
57 69bdd906 2021-06-15 op %token <str> TSTRING
58 69bdd906 2021-06-15 op %token <num> TNUMBER
62 69bdd906 2021-06-15 op grammar : /* empty */
63 69bdd906 2021-06-15 op | grammar '\n'
64 69bdd906 2021-06-15 op | grammar rule '\n'
68 69bdd906 2021-06-15 op rule : set | style | bind | unbind ;
70 69bdd906 2021-06-15 op set : TSET TSTRING '=' TSTRING { printf("set %s = \"%s\"\n", $2, $4); }
71 69bdd906 2021-06-15 op | TSET TSTRING '=' TNUMBER { printf("set %s = %d\n", $2, $4); }
74 69bdd906 2021-06-15 op style : TSTYLE TSTRING { printf("(%s) ", $2); } styleopt
75 69bdd906 2021-06-15 op | TSTYLE TSTRING { printf("[%s]\n", $2); } '{' styleopts '}'
78 69bdd906 2021-06-15 op styleopts : /* empty */
79 69bdd906 2021-06-15 op | styleopts '\n'
80 69bdd906 2021-06-15 op | styleopts styleopt '\n'
83 69bdd906 2021-06-15 op styleopt : TPRFX TSTRING { printf("style prefix setted to \"%s\"\n", $2); }
84 69bdd906 2021-06-15 op | TCONT TSTRING { printf("style cont setted to \"%s\"\n", $2); }
85 69bdd906 2021-06-15 op | TBG TSTRING { printf("style background setted to \"%s\"\n", $2); }
86 69bdd906 2021-06-15 op | TFG TSTRING { printf("style foreground setted to \"%s\"\n", $2); }
87 69bdd906 2021-06-15 op | TATTR TBOLD { printf("style attr setted to bold\n"); }
88 69bdd906 2021-06-15 op | TATTR TUNDERLINE { printf("style attr setted to underline\n"); }
91 69bdd906 2021-06-15 op bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
94 69bdd906 2021-06-15 op unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
100 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
104 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
105 69bdd906 2021-06-15 op va_start(va, fmt);
106 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
108 69bdd906 2021-06-15 op fprintf(stderr, "\n");
109 69bdd906 2021-06-15 op parse_errors++;
112 69bdd906 2021-06-15 op static struct keyword {
113 69bdd906 2021-06-15 op const char *word;
115 69bdd906 2021-06-15 op } keywords[] = {
116 69bdd906 2021-06-15 op { "set", TSET },
117 69bdd906 2021-06-15 op { "style", TSTYLE },
118 69bdd906 2021-06-15 op { "prefix", TPRFX },
119 69bdd906 2021-06-15 op { "cont", TCONT },
120 69bdd906 2021-06-15 op { "background", TBG },
121 69bdd906 2021-06-15 op { "foreground", TFG },
122 69bdd906 2021-06-15 op { "attr", TATTR },
123 69bdd906 2021-06-15 op { "bold", TBOLD },
124 69bdd906 2021-06-15 op { "underline", TUNDERLINE },
125 69bdd906 2021-06-15 op { "bind", TBIND },
126 69bdd906 2021-06-15 op { "unbind", TUNBIND },
132 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
133 69bdd906 2021-06-15 op const char *errstr;
134 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
138 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
141 69bdd906 2021-06-15 op /* skip whitespace first */
142 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
143 69bdd906 2021-06-15 op yylval.colno++;
145 69bdd906 2021-06-15 op /* check for special one-character constructions */
148 69bdd906 2021-06-15 op yylval.colno = 0;
149 69bdd906 2021-06-15 op yylval.lineno++;
150 69bdd906 2021-06-15 op /* fallthrough */
156 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
157 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
158 69bdd906 2021-06-15 op if (c == EOF)
160 69bdd906 2021-06-15 op yylval.colno = 0;
161 69bdd906 2021-06-15 op yylval.lineno++;
167 69bdd906 2021-06-15 op /* parsing next word */
168 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
171 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
172 69bdd906 2021-06-15 op yylval.colno+1);
176 69bdd906 2021-06-15 op escape = !escape;
182 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
183 69bdd906 2021-06-15 op yylval.colno+1);
184 69bdd906 2021-06-15 op if (escape) {
187 69bdd906 2021-06-15 op yylval.colno = 0;
188 69bdd906 2021-06-15 op yylval.lineno++;
193 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
194 69bdd906 2021-06-15 op yylval.colno);
196 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
205 69bdd906 2021-06-15 op if (!escape && !quotes)
209 69bdd906 2021-06-15 op if (!escape) {
210 69bdd906 2021-06-15 op quotes = !quotes;
211 69bdd906 2021-06-15 op if (quotes) {
213 69bdd906 2021-06-15 op qpos = yylval.colno;
219 69bdd906 2021-06-15 op if (p == ebuf) {
220 69bdd906 2021-06-15 op yyerror("line too long");
228 69bdd906 2021-06-15 op if (c != EOF)
229 69bdd906 2021-06-15 op ungetc(c, yyfp);
230 69bdd906 2021-06-15 op if (p == buf) {
232 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
233 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
234 69bdd906 2021-06-15 op * the main loop.
236 69bdd906 2021-06-15 op if (c == EOF)
238 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
241 69bdd906 2021-06-15 op if (!nonkw) {
242 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
243 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
244 69bdd906 2021-06-15 op return keywords[i].token;
248 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
249 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
250 69bdd906 2021-06-15 op if (errstr != NULL)
251 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
252 69bdd906 2021-06-15 op return TNUMBER;
254 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
255 69bdd906 2021-06-15 op err(1, "%s", __func__);
256 69bdd906 2021-06-15 op yylval.str = str;
257 69bdd906 2021-06-15 op return TSTRING;
260 69bdd906 2021-06-15 op if (ferror(yyfp))
261 69bdd906 2021-06-15 op yyerror("input error reading config");
266 69bdd906 2021-06-15 op parseconfig(const char *filename, int fonf)
268 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
270 69bdd906 2021-06-15 op err(1, "%s", filename);
274 69bdd906 2021-06-15 op path = filename;
276 69bdd906 2021-06-15 op fclose(yyfp);
277 69bdd906 2021-06-15 op if (parse_errors)