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