]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
Make the warning about $terminal being deprecated an error (Thanks fallen)
[i3/i3] / src / cfgparse.y
1 %{
2 /*
3  * vim:ts=8:expandtab
4  *
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include <xcb/xcb.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <errno.h>
15
16 #include "data.h"
17 #include "config.h"
18 #include "i3.h"
19 #include "util.h"
20 #include "queue.h"
21 #include "table.h"
22 #include "workspace.h"
23 #include "xcb.h"
24 #include "log.h"
25
26 typedef struct yy_buffer_state *YY_BUFFER_STATE;
27 extern int yylex(struct context *context);
28 extern int yyparse(void);
29 extern FILE *yyin;
30 YY_BUFFER_STATE yy_scan_string(const char *);
31
32 static struct bindings_head *current_bindings;
33 static struct context *context;
34
35 /* We don’t need yydebug for now, as we got decent error messages using
36  * yyerror(). Should you ever want to extend the parser, it might be handy
37  * to just comment it in again, so it stays here. */
38 //int yydebug = 1;
39
40 void yyerror(const char *error_message) {
41         ELOG("\n");
42         ELOG("CONFIG: %s\n", error_message);
43         ELOG("CONFIG: in file \"%s\", line %d:\n",
44                 context->filename, context->line_number);
45         ELOG("CONFIG:   %s\n", context->line_copy);
46         ELOG("CONFIG:   ");
47         for (int c = 1; c <= context->last_column; c++)
48                 if (c >= context->first_column)
49                         printf("^");
50                 else printf(" ");
51         printf("\n");
52         ELOG("\n");
53 }
54
55 int yywrap() {
56         return 1;
57 }
58
59 void parse_file(const char *f) {
60         SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
61         int fd, ret, read_bytes = 0;
62         struct stat stbuf;
63         char *buf;
64         FILE *fstr;
65         char buffer[1026], key[512], value[512];
66
67         if ((fd = open(f, O_RDONLY)) == -1)
68                 die("Could not open configuration file: %s\n", strerror(errno));
69
70         if (fstat(fd, &stbuf) == -1)
71                 die("Could not fstat file: %s\n", strerror(errno));
72
73         buf = smalloc(stbuf.st_size * sizeof(char));
74         while (read_bytes < stbuf.st_size) {
75                 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
76                         die("Could not read(): %s\n", strerror(errno));
77                 read_bytes += ret;
78         }
79
80         if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
81                 die("Could not lseek: %s\n", strerror(errno));
82
83         if ((fstr = fdopen(fd, "r")) == NULL)
84                 die("Could not fdopen: %s\n", strerror(errno));
85
86         while (!feof(fstr)) {
87                 if (fgets(buffer, 1024, fstr) == NULL) {
88                         if (feof(fstr))
89                                 break;
90                         die("Could not read configuration file\n");
91                 }
92
93                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
94                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
95                     key[0] == '#' || strlen(key) < 3)
96                         continue;
97
98                 if (strcasecmp(key, "set") == 0) {
99                         if (value[0] != '$')
100                                 die("Malformed variable assignment, name has to start with $\n");
101
102                         /* get key/value for this variable */
103                         char *v_key = value, *v_value;
104                         if ((v_value = strstr(value, " ")) == NULL)
105                                 die("Malformed variable assignment, need a value\n");
106
107                         *(v_value++) = '\0';
108
109                         struct Variable *new = scalloc(sizeof(struct Variable));
110                         new->key = sstrdup(v_key);
111                         new->value = sstrdup(v_value);
112                         SLIST_INSERT_HEAD(&variables, new, variables);
113                         DLOG("Got new variable %s = %s\n", v_key, v_value);
114                         continue;
115                 }
116         }
117
118         /* For every custom variable, see how often it occurs in the file and
119          * how much extra bytes it requires when replaced. */
120         struct Variable *current, *nearest;
121         int extra_bytes = 0;
122         SLIST_FOREACH(current, &variables, variables) {
123                 int extra = (strlen(current->value) - strlen(current->key));
124                 char *next;
125                 for (next = buf;
126                      (next = strcasestr(buf + (next - buf), current->key)) != NULL;
127                      next += strlen(current->key))
128                         extra_bytes += extra;
129         }
130
131         /* Then, allocate a new buffer and copy the file over to the new one,
132          * but replace occurences of our variables */
133         char *walk = buf, *destwalk;
134         char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
135         destwalk = new;
136         while (walk < (buf + stbuf.st_size)) {
137                 /* Find the next variable */
138                 SLIST_FOREACH(current, &variables, variables)
139                         current->next_match = strcasestr(walk, current->key);
140                 nearest = NULL;
141                 int distance = stbuf.st_size;
142                 SLIST_FOREACH(current, &variables, variables) {
143                         if (current->next_match == NULL)
144                                 continue;
145                         if ((current->next_match - walk) < distance) {
146                                 distance = (current->next_match - walk);
147                                 nearest = current;
148                         }
149                 }
150                 if (nearest == NULL) {
151                         /* If there are no more variables, we just copy the rest */
152                         strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
153                         destwalk += (buf + stbuf.st_size) - walk;
154                         *destwalk = '\0';
155                         break;
156                 } else {
157                         /* Copy until the next variable, then copy its value */
158                         strncpy(destwalk, walk, distance);
159                         strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
160                         walk += distance + strlen(nearest->key);
161                         destwalk += distance + strlen(nearest->value);
162                 }
163         }
164
165         yy_scan_string(new);
166
167         context = scalloc(sizeof(struct context));
168         context->filename = f;
169
170         if (yyparse() != 0) {
171                 fprintf(stderr, "Could not parse configfile\n");
172                 exit(1);
173         }
174
175         FREE(context->line_copy);
176         free(context);
177         free(new);
178         free(buf);
179 }
180
181 %}
182
183 %expect 1
184 %error-verbose
185 %lex-param { struct context *context }
186
187 %union {
188         int number;
189         char *string;
190         struct Colortriple *color;
191         struct Assignment *assignment;
192         struct Binding *binding;
193 }
194
195 %token <number>NUMBER "<number>"
196 %token <string>WORD "<word>"
197 %token <string>STR "<string>"
198 %token <string>STR_NG "<string (non-greedy)>"
199 %token <string>HEX "<hex>"
200 %token TOKBIND
201 %token TOKTERMINAL
202 %token TOKCOMMENT "<comment>"
203 %token TOKFONT "font"
204 %token TOKBINDSYM "bindsym"
205 %token MODIFIER "<modifier>"
206 %token TOKCONTROL "control"
207 %token TOKSHIFT "shift"
208 %token WHITESPACE "<whitespace>"
209 %token TOKFLOATING_MODIFIER "floating_modifier"
210 %token QUOTEDSTRING "<quoted string>"
211 %token TOKWORKSPACE "workspace"
212 %token TOKSCREEN "screen"
213 %token TOKASSIGN "assign"
214 %token TOKSET
215 %token TOKIPCSOCKET "ipc_socket"
216 %token TOKEXEC "exec"
217 %token TOKCOLOR
218 %token TOKARROW "→"
219 %token TOKMODE "mode"
220 %token TOKNEWCONTAINER "new_container"
221 %token TOKNEWWINDOW "new_window"
222 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
223 %token TOKCONTAINERMODE "default/stacking/tabbed"
224 %token TOKSTACKLIMIT "stack-limit"
225
226 %%
227
228 lines: /* empty */
229         | lines WHITESPACE line
230         | lines error
231         | lines line
232         ;
233
234 line:
235         bindline
236         | mode
237         | floating_modifier
238         | new_container
239         | new_window
240         | focus_follows_mouse
241         | workspace
242         | assign
243         | ipcsocket
244         | exec
245         | color
246         | terminal
247         | font
248         | comment
249         ;
250
251 comment:
252         TOKCOMMENT
253         ;
254
255 command:
256         STR
257         ;
258
259 bindline:
260         binding
261         {
262                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
263         }
264         ;
265
266 binding:
267         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
268         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
269         ;
270
271 bind:
272         binding_modifiers NUMBER WHITESPACE command
273         {
274                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
275                 Binding *new = scalloc(sizeof(Binding));
276
277                 new->keycode = $<number>2;
278                 new->mods = $<number>1;
279                 new->command = sstrdup($<string>4);
280
281                 $<binding>$ = new;
282         }
283         ;
284
285 bindsym:
286         binding_modifiers word_or_number WHITESPACE command
287         {
288                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
289                 Binding *new = scalloc(sizeof(Binding));
290
291                 new->symbol = sstrdup($<string>2);
292                 new->mods = $<number>1;
293                 new->command = sstrdup($<string>4);
294
295                 $<binding>$ = new;
296         }
297         ;
298
299 word_or_number:
300         WORD
301         | NUMBER
302         {
303                 asprintf(&$<string>$, "%d", $1);
304         }
305         ;
306
307 mode:
308         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
309         {
310                 if (strcasecmp($<string>3, "default") == 0) {
311                         printf("You cannot use the name \"default\" for your mode\n");
312                         exit(1);
313                 }
314                 printf("\t now in mode %s\n", $<string>3);
315                 printf("\t current bindings = %p\n", current_bindings);
316                 Binding *binding;
317                 TAILQ_FOREACH(binding, current_bindings, bindings) {
318                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
319                                         binding->mods, binding->keycode, binding->symbol, binding->command);
320                 }
321
322                 struct Mode *mode = scalloc(sizeof(struct Mode));
323                 mode->name = strdup($<string>3);
324                 mode->bindings = current_bindings;
325                 current_bindings = NULL;
326                 SLIST_INSERT_HEAD(&modes, mode, modes);
327         }
328         ;
329
330
331 modelines:
332         /* empty */
333         | modelines modeline
334         ;
335
336 modeline:
337         WHITESPACE
338         | comment
339         | binding
340         {
341                 if (current_bindings == NULL) {
342                         current_bindings = scalloc(sizeof(struct bindings_head));
343                         TAILQ_INIT(current_bindings);
344                 }
345
346                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
347         }
348         ;
349
350 floating_modifier:
351         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
352         {
353                 DLOG("floating modifier = %d\n", $<number>3);
354                 config.floating_modifier = $<number>3;
355         }
356         ;
357
358 new_container:
359         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
360         {
361                 DLOG("new containers will be in mode %d\n", $<number>3);
362                 config.container_mode = $<number>3;
363
364                 /* We also need to change the layout of the already existing
365                  * workspaces here. Workspaces may exist at this point because
366                  * of the other directives which are modifying workspaces
367                  * (setting the preferred screen or name). While the workspace
368                  * objects are already created, they have never been used.
369                  * Thus, the user very likely awaits the default container mode
370                  * to trigger in this case, regardless of where it is inside
371                  * his configuration file. */
372                 Workspace *ws;
373                 TAILQ_FOREACH(ws, workspaces, workspaces) {
374                         if (ws->table == NULL)
375                                 continue;
376                         switch_layout_mode(global_conn,
377                                            ws->table[0][0],
378                                            config.container_mode);
379                 }
380         }
381         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
382         {
383                 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
384                 config.container_stack_limit = $<number>5;
385                 config.container_stack_limit_value = $<number>7;
386
387                 /* See the comment above */
388                 Workspace *ws;
389                 TAILQ_FOREACH(ws, workspaces, workspaces) {
390                         if (ws->table == NULL)
391                                 continue;
392                         Container *con = ws->table[0][0];
393                         con->stack_limit = config.container_stack_limit;
394                         con->stack_limit_value = config.container_stack_limit_value;
395                 }
396         }
397         ;
398
399 new_window:
400         TOKNEWWINDOW WHITESPACE WORD
401         {
402                 DLOG("new windows should start in mode %s\n", $<string>3);
403                 config.default_border = strdup($<string>3);
404         }
405         ;
406
407 bool:
408         NUMBER
409         {
410                 $<number>$ = ($<number>1 == 1);
411         }
412         | WORD
413         {
414                 DLOG("checking word \"%s\"\n", $<string>1);
415                 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
416                               strcasecmp($<string>1, "true") == 0 ||
417                               strcasecmp($<string>1, "on") == 0 ||
418                               strcasecmp($<string>1, "enable") == 0 ||
419                               strcasecmp($<string>1, "active") == 0);
420         }
421         ;
422
423 focus_follows_mouse:
424         TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
425         {
426                 DLOG("focus follows mouse = %d\n", $<number>3);
427                 config.disable_focus_follows_mouse = !($<number>3);
428         }
429         ;
430
431 workspace:
432         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen optional_workspace_name
433         {
434                 int ws_num = $<number>3;
435                 if (ws_num < 1) {
436                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
437                 } else {
438                         Workspace *ws = workspace_get(ws_num - 1);
439                         ws->preferred_screen = sstrdup($<string>7);
440                         if ($<string>8 != NULL)
441                                 workspace_set_name(ws, $<string>8);
442                 }
443         }
444         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
445         {
446                 int ws_num = $<number>3;
447                 if (ws_num < 1) {
448                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
449                 } else {
450                         if ($<string>5 != NULL)
451                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
452                 }
453         }
454         ;
455
456 optional_workspace_name:
457         /* empty */                     { $<string>$ = NULL; }
458         | WHITESPACE workspace_name     { $<string>$ = $<string>2; }
459         ;
460
461 workspace_name:
462         QUOTEDSTRING         { $<string>$ = $<string>1; }
463         | STR                { $<string>$ = $<string>1; }
464         | WORD               { $<string>$ = $<string>1; }
465         ;
466
467 screen:
468         NUMBER              { asprintf(&$<string>$, "%d", $<number>1); }
469         | NUMBER 'x'        { asprintf(&$<string>$, "%d", $<number>1); }
470         | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
471         | 'x' NUMBER        { asprintf(&$<string>$, "x%d", $<number>2); }
472         ;
473
474 assign:
475         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
476         {
477                 printf("assignment of %s\n", $<string>3);
478
479                 struct Assignment *new = $<assignment>6;
480                 printf("  to %d\n", new->workspace);
481                 printf("  floating = %d\n", new->floating);
482                 new->windowclass_title = strdup($<string>3);
483                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
484         }
485         ;
486
487 assign_target:
488         NUMBER
489         {
490                 struct Assignment *new = scalloc(sizeof(struct Assignment));
491                 new->workspace = $<number>1;
492                 new->floating = ASSIGN_FLOATING_NO;
493                 $<assignment>$ = new;
494         }
495         | '~'
496         {
497                 struct Assignment *new = scalloc(sizeof(struct Assignment));
498                 new->floating = ASSIGN_FLOATING_ONLY;
499                 $<assignment>$ = new;
500         }
501         | '~' NUMBER
502         {
503                 struct Assignment *new = scalloc(sizeof(struct Assignment));
504                 new->workspace = $<number>2;
505                 new->floating = ASSIGN_FLOATING;
506                 $<assignment>$ = new;
507         }
508         ;
509
510 window_class:
511         QUOTEDSTRING
512         | STR_NG
513         ;
514
515 optional_arrow:
516         /* NULL */
517         | TOKARROW WHITESPACE
518         ;
519
520 ipcsocket:
521         TOKIPCSOCKET WHITESPACE STR
522         {
523                 config.ipc_socket_path = sstrdup($<string>3);
524         }
525         ;
526
527 exec:
528         TOKEXEC WHITESPACE STR
529         {
530                 struct Autostart *new = smalloc(sizeof(struct Autostart));
531                 new->command = sstrdup($<string>3);
532                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
533         }
534         ;
535
536 terminal:
537         TOKTERMINAL WHITESPACE STR
538         {
539                 ELOG("The terminal option is DEPRECATED and has no effect. "
540                     "Please remove it from your configuration file.");
541         }
542         ;
543
544 font:
545         TOKFONT WHITESPACE STR
546         {
547                 config.font = sstrdup($<string>3);
548                 printf("font %s\n", config.font);
549         }
550         ;
551
552
553 color:
554         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
555         {
556                 struct Colortriple *dest = $<color>1;
557
558                 dest->border = $<number>3;
559                 dest->background = $<number>5;
560                 dest->text = $<number>7;
561         }
562         ;
563
564 colorpixel:
565         '#' HEX
566         {
567                 char *hex;
568                 if (asprintf(&hex, "#%s", $<string>2) == -1)
569                         die("asprintf()");
570                 $<number>$ = get_colorpixel(global_conn, hex);
571                 free(hex);
572         }
573         ;
574
575
576 binding_modifiers:
577         /* NULL */                               { $<number>$ = 0; }
578         | binding_modifier
579         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
580         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
581         ;
582
583 binding_modifier:
584         MODIFIER        { $<number>$ = $<number>1; }
585         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
586         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
587         ;