]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
Bugfix: Don’t leak file descriptors (Thanks InfraRed)
[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 = scalloc((stbuf.st_size + 1) * 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         while (!SLIST_EMPTY(&variables)) {
181                 current = SLIST_FIRST(&variables);
182                 FREE(current->key);
183                 FREE(current->value);
184                 SLIST_REMOVE_HEAD(&variables, variables);
185                 FREE(current);
186         }
187         fclose(fstr);
188         close(fd);
189 }
190
191 %}
192
193 %expect 1
194 %error-verbose
195 %lex-param { struct context *context }
196
197 %union {
198         int number;
199         char *string;
200         struct Colortriple *color;
201         struct Assignment *assignment;
202         struct Binding *binding;
203 }
204
205 %token <number>NUMBER "<number>"
206 %token <string>WORD "<word>"
207 %token <string>STR "<string>"
208 %token <string>STR_NG "<string (non-greedy)>"
209 %token <string>HEX "<hex>"
210 %token <string>OUTPUT "<RandR output>"
211 %token TOKBIND
212 %token TOKTERMINAL
213 %token TOKCOMMENT "<comment>"
214 %token TOKFONT "font"
215 %token TOKBINDSYM "bindsym"
216 %token MODIFIER "<modifier>"
217 %token TOKCONTROL "control"
218 %token TOKSHIFT "shift"
219 %token WHITESPACE "<whitespace>"
220 %token TOKFLOATING_MODIFIER "floating_modifier"
221 %token QUOTEDSTRING "<quoted string>"
222 %token TOKWORKSPACE "workspace"
223 %token TOKOUTPUT "output"
224 %token TOKASSIGN "assign"
225 %token TOKSET
226 %token TOKIPCSOCKET "ipc_socket"
227 %token TOKEXEC "exec"
228 %token TOKCOLOR
229 %token TOKARROW "→"
230 %token TOKMODE "mode"
231 %token TOKNEWCONTAINER "new_container"
232 %token TOKNEWWINDOW "new_window"
233 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
234 %token TOKWORKSPACEBAR "workspace_bar"
235 %token TOKCONTAINERMODE "default/stacking/tabbed"
236 %token TOKSTACKLIMIT "stack-limit"
237
238 %%
239
240 lines: /* empty */
241         | lines WHITESPACE line
242         | lines error
243         | lines line
244         ;
245
246 line:
247         bindline
248         | mode
249         | floating_modifier
250         | new_container
251         | new_window
252         | focus_follows_mouse
253         | workspace_bar
254         | workspace
255         | assign
256         | ipcsocket
257         | exec
258         | color
259         | terminal
260         | font
261         | comment
262         ;
263
264 comment:
265         TOKCOMMENT
266         ;
267
268 command:
269         STR
270         ;
271
272 bindline:
273         binding
274         {
275                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
276         }
277         ;
278
279 binding:
280         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
281         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
282         ;
283
284 bind:
285         binding_modifiers NUMBER WHITESPACE command
286         {
287                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
288                 Binding *new = scalloc(sizeof(Binding));
289
290                 new->keycode = $<number>2;
291                 new->mods = $<number>1;
292                 new->command = $<string>4;
293
294                 $<binding>$ = new;
295         }
296         ;
297
298 bindsym:
299         binding_modifiers word_or_number WHITESPACE command
300         {
301                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
302                 Binding *new = scalloc(sizeof(Binding));
303
304                 new->symbol = $<string>2;
305                 new->mods = $<number>1;
306                 new->command = $<string>4;
307
308                 $<binding>$ = new;
309         }
310         ;
311
312 word_or_number:
313         WORD
314         | NUMBER
315         {
316                 asprintf(&$<string>$, "%d", $1);
317         }
318         ;
319
320 mode:
321         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
322         {
323                 if (strcasecmp($<string>3, "default") == 0) {
324                         printf("You cannot use the name \"default\" for your mode\n");
325                         exit(1);
326                 }
327                 printf("\t now in mode %s\n", $<string>3);
328                 printf("\t current bindings = %p\n", current_bindings);
329                 Binding *binding;
330                 TAILQ_FOREACH(binding, current_bindings, bindings) {
331                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
332                                         binding->mods, binding->keycode, binding->symbol, binding->command);
333                 }
334
335                 struct Mode *mode = scalloc(sizeof(struct Mode));
336                 mode->name = $<string>3;
337                 mode->bindings = current_bindings;
338                 current_bindings = NULL;
339                 SLIST_INSERT_HEAD(&modes, mode, modes);
340         }
341         ;
342
343
344 modelines:
345         /* empty */
346         | modelines modeline
347         ;
348
349 modeline:
350         WHITESPACE
351         | comment
352         | binding
353         {
354                 if (current_bindings == NULL) {
355                         current_bindings = scalloc(sizeof(struct bindings_head));
356                         TAILQ_INIT(current_bindings);
357                 }
358
359                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
360         }
361         ;
362
363 floating_modifier:
364         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
365         {
366                 DLOG("floating modifier = %d\n", $<number>3);
367                 config.floating_modifier = $<number>3;
368         }
369         ;
370
371 new_container:
372         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
373         {
374                 DLOG("new containers will be in mode %d\n", $<number>3);
375                 config.container_mode = $<number>3;
376
377                 /* We also need to change the layout of the already existing
378                  * workspaces here. Workspaces may exist at this point because
379                  * of the other directives which are modifying workspaces
380                  * (setting the preferred screen or name). While the workspace
381                  * objects are already created, they have never been used.
382                  * Thus, the user very likely awaits the default container mode
383                  * to trigger in this case, regardless of where it is inside
384                  * his configuration file. */
385                 Workspace *ws;
386                 TAILQ_FOREACH(ws, workspaces, workspaces) {
387                         if (ws->table == NULL)
388                                 continue;
389                         switch_layout_mode(global_conn,
390                                            ws->table[0][0],
391                                            config.container_mode);
392                 }
393         }
394         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
395         {
396                 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
397                 config.container_stack_limit = $<number>5;
398                 config.container_stack_limit_value = $<number>7;
399
400                 /* See the comment above */
401                 Workspace *ws;
402                 TAILQ_FOREACH(ws, workspaces, workspaces) {
403                         if (ws->table == NULL)
404                                 continue;
405                         Container *con = ws->table[0][0];
406                         con->stack_limit = config.container_stack_limit;
407                         con->stack_limit_value = config.container_stack_limit_value;
408                 }
409         }
410         ;
411
412 new_window:
413         TOKNEWWINDOW WHITESPACE WORD
414         {
415                 DLOG("new windows should start in mode %s\n", $<string>3);
416                 config.default_border = sstrdup($<string>3);
417         }
418         ;
419
420 bool:
421         NUMBER
422         {
423                 $<number>$ = ($<number>1 == 1);
424         }
425         | WORD
426         {
427                 DLOG("checking word \"%s\"\n", $<string>1);
428                 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
429                               strcasecmp($<string>1, "true") == 0 ||
430                               strcasecmp($<string>1, "on") == 0 ||
431                               strcasecmp($<string>1, "enable") == 0 ||
432                               strcasecmp($<string>1, "active") == 0);
433         }
434         ;
435
436 focus_follows_mouse:
437         TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
438         {
439                 DLOG("focus follows mouse = %d\n", $<number>3);
440                 config.disable_focus_follows_mouse = !($<number>3);
441         }
442         ;
443
444 workspace_bar:
445         TOKWORKSPACEBAR WHITESPACE bool
446         {
447                 DLOG("workspace bar = %d\n", $<number>3);
448                 config.disable_workspace_bar = !($<number>3);
449         }
450         ;
451
452 workspace:
453         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
454         {
455                 int ws_num = $<number>3;
456                 if (ws_num < 1) {
457                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
458                 } else {
459                         Workspace *ws = workspace_get(ws_num - 1);
460                         ws->preferred_output = $<string>7;
461                         if ($<string>8 != NULL) {
462                                 workspace_set_name(ws, $<string>8);
463                                 free($<string>8);
464                         }
465                 }
466         }
467         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
468         {
469                 int ws_num = $<number>3;
470                 if (ws_num < 1) {
471                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
472                 } else {
473                         DLOG("workspace name to: %s\n", $<string>5);
474                         if ($<string>5 != NULL) {
475                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
476                                 free($<string>5);
477                         }
478                 }
479         }
480         ;
481
482 optional_workspace_name:
483         /* empty */                     { $<string>$ = NULL; }
484         | WHITESPACE workspace_name     { $<string>$ = $<string>2; }
485         ;
486
487 workspace_name:
488         QUOTEDSTRING         { $<string>$ = $<string>1; }
489         | STR                { $<string>$ = $<string>1; }
490         | WORD               { $<string>$ = $<string>1; }
491         ;
492
493 assign:
494         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
495         {
496                 printf("assignment of %s\n", $<string>3);
497
498                 struct Assignment *new = $<assignment>6;
499                 printf("  to %d\n", new->workspace);
500                 printf("  floating = %d\n", new->floating);
501                 new->windowclass_title = $<string>3;
502                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
503         }
504         ;
505
506 assign_target:
507         NUMBER
508         {
509                 struct Assignment *new = scalloc(sizeof(struct Assignment));
510                 new->workspace = $<number>1;
511                 new->floating = ASSIGN_FLOATING_NO;
512                 $<assignment>$ = new;
513         }
514         | '~'
515         {
516                 struct Assignment *new = scalloc(sizeof(struct Assignment));
517                 new->floating = ASSIGN_FLOATING_ONLY;
518                 $<assignment>$ = new;
519         }
520         | '~' NUMBER
521         {
522                 struct Assignment *new = scalloc(sizeof(struct Assignment));
523                 new->workspace = $<number>2;
524                 new->floating = ASSIGN_FLOATING;
525                 $<assignment>$ = new;
526         }
527         ;
528
529 window_class:
530         QUOTEDSTRING
531         | STR_NG
532         ;
533
534 optional_arrow:
535         /* NULL */
536         | TOKARROW WHITESPACE
537         ;
538
539 ipcsocket:
540         TOKIPCSOCKET WHITESPACE STR
541         {
542                 config.ipc_socket_path = $<string>3;
543         }
544         ;
545
546 exec:
547         TOKEXEC WHITESPACE STR
548         {
549                 struct Autostart *new = smalloc(sizeof(struct Autostart));
550                 new->command = $<string>3;
551                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
552         }
553         ;
554
555 terminal:
556         TOKTERMINAL WHITESPACE STR
557         {
558                 ELOG("The terminal option is DEPRECATED and has no effect. "
559                     "Please remove it from your configuration file.\n");
560         }
561         ;
562
563 font:
564         TOKFONT WHITESPACE STR
565         {
566                 config.font = $<string>3;
567                 printf("font %s\n", config.font);
568         }
569         ;
570
571
572 color:
573         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
574         {
575                 struct Colortriple *dest = $<color>1;
576
577                 dest->border = $<number>3;
578                 dest->background = $<number>5;
579                 dest->text = $<number>7;
580         }
581         ;
582
583 colorpixel:
584         '#' HEX
585         {
586                 char *hex;
587                 if (asprintf(&hex, "#%s", $<string>2) == -1)
588                         die("asprintf()");
589                 $<number>$ = get_colorpixel(global_conn, hex);
590                 free(hex);
591         }
592         ;
593
594
595 binding_modifiers:
596         /* NULL */                               { $<number>$ = 0; }
597         | binding_modifier
598         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
599         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
600         ;
601
602 binding_modifier:
603         MODIFIER        { $<number>$ = $<number>1; }
604         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
605         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
606         ;