]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
migration from exec i3bar to bar config: i3 substitutes exec with a bar block, shows...
[i3/i3] / src / cfgparse.y
1 %{
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11
12 #include "all.h"
13
14 static pid_t configerror_pid = -1;
15
16 static Match current_match;
17 static Barconfig current_bar;
18
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int yylex(struct context *context);
21 extern int yyparse(void);
22 extern int yylex_destroy(void);
23 extern FILE *yyin;
24 YY_BUFFER_STATE yy_scan_string(const char *);
25
26 static struct bindings_head *current_bindings;
27 static struct context *context;
28
29 /* We don’t need yydebug for now, as we got decent error messages using
30  * yyerror(). Should you ever want to extend the parser, it might be handy
31  * to just comment it in again, so it stays here. */
32 //int yydebug = 1;
33
34 void yyerror(const char *error_message) {
35     context->has_errors = true;
36
37     ELOG("\n");
38     ELOG("CONFIG: %s\n", error_message);
39     ELOG("CONFIG: in file \"%s\", line %d:\n",
40         context->filename, context->line_number);
41     ELOG("CONFIG:   %s\n", context->line_copy);
42     char buffer[context->last_column+1];
43     buffer[context->last_column] = '\0';
44     for (int c = 1; c <= context->last_column; c++)
45         buffer[c-1] = (c >= context->first_column ? '^' : ' ');
46     ELOG("CONFIG:   %s\n", buffer);
47     ELOG("\n");
48 }
49
50 int yywrap() {
51     return 1;
52 }
53
54 /*
55  * Goes through each line of buf (separated by \n) and checks for statements /
56  * commands which only occur in i3 v4 configuration files. If it finds any, it
57  * returns version 4, otherwise it returns version 3.
58  *
59  */
60 static int detect_version(char *buf) {
61     char *walk = buf;
62     char *line = buf;
63     while (*walk != '\0') {
64         if (*walk != '\n') {
65             walk++;
66             continue;
67         }
68
69         /* check for some v4-only statements */
70         if (strncasecmp(line, "bindcode", strlen("bindcode")) == 0 ||
71             strncasecmp(line, "force_focus_wrapping", strlen("force_focus_wrapping")) == 0 ||
72             strncasecmp(line, "# i3 config file (v4)", strlen("# i3 config file (v4)")) == 0 ||
73             strncasecmp(line, "workspace_layout", strlen("workspace_layout")) == 0) {
74             printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
75             return 4;
76         }
77
78         /* if this is a bind statement, we can check the command */
79         if (strncasecmp(line, "bind", strlen("bind")) == 0) {
80             char *bind = strchr(line, ' ');
81             if (bind == NULL)
82                 goto next;
83             while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
84                 bind++;
85             if (*bind == '\0')
86                 goto next;
87             if ((bind = strchr(bind, ' ')) == NULL)
88                 goto next;
89             while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
90                 bind++;
91             if (*bind == '\0')
92                 goto next;
93             if (strncasecmp(bind, "layout", strlen("layout")) == 0 ||
94                 strncasecmp(bind, "floating", strlen("floating")) == 0 ||
95                 strncasecmp(bind, "workspace", strlen("workspace")) == 0 ||
96                 strncasecmp(bind, "focus left", strlen("focus left")) == 0 ||
97                 strncasecmp(bind, "focus right", strlen("focus right")) == 0 ||
98                 strncasecmp(bind, "focus up", strlen("focus up")) == 0 ||
99                 strncasecmp(bind, "focus down", strlen("focus down")) == 0 ||
100                 strncasecmp(bind, "border normal", strlen("border normal")) == 0 ||
101                 strncasecmp(bind, "border 1pixel", strlen("border 1pixel")) == 0 ||
102                 strncasecmp(bind, "border borderless", strlen("border borderless")) == 0) {
103                 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
104                 return 4;
105             }
106         }
107
108 next:
109         /* advance to the next line */
110         walk++;
111         line = walk;
112     }
113
114     return 3;
115 }
116
117 /*
118  * Calls i3-migrate-config-to-v4 to migrate a configuration file (input
119  * buffer).
120  *
121  * Returns the converted config file or NULL if there was an error (for
122  * example the script could not be found in $PATH or the i3 executable’s
123  * directory).
124  *
125  */
126 static char *migrate_config(char *input, off_t size) {
127     int writepipe[2];
128     int readpipe[2];
129
130     if (pipe(writepipe) != 0 ||
131         pipe(readpipe) != 0) {
132         warn("migrate_config: Could not create pipes");
133         return NULL;
134     }
135
136     pid_t pid = fork();
137     if (pid == -1) {
138         warn("Could not fork()");
139         return NULL;
140     }
141
142     /* child */
143     if (pid == 0) {
144         /* close writing end of writepipe, connect reading side to stdin */
145         close(writepipe[1]);
146         dup2(writepipe[0], 0);
147
148         /* close reading end of readpipe, connect writing side to stdout */
149         close(readpipe[0]);
150         dup2(readpipe[1], 1);
151
152         static char *argv[] = {
153             NULL, /* will be replaced by the executable path */
154             NULL
155         };
156         exec_i3_utility("i3-migrate-config-to-v4", argv);
157     }
158
159     /* parent */
160
161     /* close reading end of the writepipe (connected to the script’s stdin) */
162     close(writepipe[0]);
163
164     /* write the whole config file to the pipe, the script will read everything
165      * immediately */
166     int written = 0;
167     int ret;
168     while (written < size) {
169         if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
170             warn("Could not write to pipe");
171             return NULL;
172         }
173         written += ret;
174     }
175     close(writepipe[1]);
176
177     /* close writing end of the readpipe (connected to the script’s stdout) */
178     close(readpipe[1]);
179
180     /* read the script’s output */
181     int conv_size = 65535;
182     char *converted = malloc(conv_size);
183     int read_bytes = 0;
184     do {
185         if (read_bytes == conv_size) {
186             conv_size += 65535;
187             converted = realloc(converted, conv_size);
188         }
189         ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
190         if (ret == -1) {
191             warn("Cannot read from pipe");
192             FREE(converted);
193             return NULL;
194         }
195         read_bytes += ret;
196     } while (ret > 0);
197
198     /* get the returncode */
199     int status;
200     wait(&status);
201     if (!WIFEXITED(status)) {
202         fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
203         return NULL;
204     }
205
206     int returncode = WEXITSTATUS(status);
207     if (returncode != 0) {
208         fprintf(stderr, "Migration process exit code was != 0\n");
209         if (returncode == 2) {
210             fprintf(stderr, "could not start the migration script\n");
211             /* TODO: script was not found. tell the user to fix his system or create a v4 config */
212         } else if (returncode == 1) {
213             fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
214             fprintf(stderr, "# i3 config file (v4)\n");
215             /* TODO: nag the user with a message to include a hint for i3 in his config file */
216         }
217         return NULL;
218     }
219
220     return converted;
221 }
222
223 /*
224  * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
225  * it exited (or could not be started, depending on the exit code).
226  *
227  */
228 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
229     ev_child_stop(EV_A_ watcher);
230     if (!WIFEXITED(watcher->rstatus)) {
231         fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
232         return;
233     }
234
235     int exitcode = WEXITSTATUS(watcher->rstatus);
236     printf("i3-nagbar process exited with status %d\n", exitcode);
237     if (exitcode == 2) {
238         fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
239     }
240
241     configerror_pid = -1;
242 }
243
244 /* We need ev >= 4 for the following code. Since it is not *that* important (it
245  * only makes sure that there are no i3-nagbar instances left behind) we still
246  * support old systems with libev 3. */
247 #if EV_VERSION_MAJOR >= 4
248 /*
249  * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
250  * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
251  *
252  */
253 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
254     if (configerror_pid != -1) {
255         LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", configerror_pid);
256         kill(configerror_pid, SIGKILL);
257     }
258 }
259 #endif
260
261 /*
262  * Starts an i3-nagbar process which alerts the user that his configuration
263  * file contains one or more errors. Also offers two buttons: One to launch an
264  * $EDITOR on the config file and another one to launch a $PAGER on the error
265  * logfile.
266  *
267  */
268 static void start_configerror_nagbar(const char *config_path) {
269     if (only_check_config)
270         return;
271
272     fprintf(stderr, "Starting i3-nagbar due to configuration errors\n");
273     configerror_pid = fork();
274     if (configerror_pid == -1) {
275         warn("Could not fork()");
276         return;
277     }
278
279     /* child */
280     if (configerror_pid == 0) {
281         char *editaction,
282              *pageraction;
283         if (asprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path) == -1)
284             exit(1);
285         if (asprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename) == -1)
286             exit(1);
287         char *argv[] = {
288             NULL, /* will be replaced by the executable path */
289             "-t",
290             (context->has_errors ? "error" : "warning"),
291             "-m",
292             (context->has_errors ?
293              "You have an error in your i3 config file!" :
294              "Your config is outdated. Please fix the warnings to make sure everything works."),
295             "-b",
296             "edit config",
297             editaction,
298             (errorfilename ? "-b" : NULL),
299             (context->has_errors ? "show errors" : "show warnings"),
300             pageraction,
301             NULL
302         };
303         exec_i3_utility("i3-nagbar", argv);
304     }
305
306     /* parent */
307     /* install a child watcher */
308     ev_child *child = smalloc(sizeof(ev_child));
309     ev_child_init(child, &nagbar_exited, configerror_pid, 0);
310     ev_child_start(main_loop, child);
311
312 /* We need ev >= 4 for the following code. Since it is not *that* important (it
313  * only makes sure that there are no i3-nagbar instances left behind) we still
314  * support old systems with libev 3. */
315 #if EV_VERSION_MAJOR >= 4
316     /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
317      * still running) */
318     ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
319     ev_cleanup_init(cleanup, nagbar_cleanup);
320     ev_cleanup_start(main_loop, cleanup);
321 #endif
322 }
323
324 /*
325  * Kills the configerror i3-nagbar process, if any.
326  *
327  * Called when reloading/restarting.
328  *
329  * If wait_for_it is set (restarting), this function will waitpid(), otherwise,
330  * ev is assumed to handle it (reloading).
331  *
332  */
333 void kill_configerror_nagbar(bool wait_for_it) {
334     if (configerror_pid == -1)
335         return;
336
337     if (kill(configerror_pid, SIGTERM) == -1)
338         warn("kill(configerror_nagbar) failed");
339
340     if (!wait_for_it)
341         return;
342
343     /* When restarting, we don’t enter the ev main loop anymore and after the
344      * exec(), our old pid is no longer watched. So, ev won’t handle SIGCHLD
345      * for us and we would end up with a <defunct> process. Therefore we
346      * waitpid() here. */
347     waitpid(configerror_pid, NULL, 0);
348 }
349
350 /*
351  * Checks for duplicate key bindings (the same keycode or keysym is configured
352  * more than once). If a duplicate binding is found, a message is printed to
353  * stderr and the has_errors variable is set to true, which will start
354  * i3-nagbar.
355  *
356  */
357 static void check_for_duplicate_bindings(struct context *context) {
358     Binding *bind, *current;
359     TAILQ_FOREACH(current, bindings, bindings) {
360         TAILQ_FOREACH(bind, bindings, bindings) {
361             /* Abort when we reach the current keybinding, only check the
362              * bindings before */
363             if (bind == current)
364                 break;
365
366             /* Check if one is using keysym while the other is using bindsym.
367              * If so, skip. */
368             /* XXX: It should be checked at a later place (when translating the
369              * keysym to keycodes) if there are any duplicates */
370             if ((bind->symbol == NULL && current->symbol != NULL) ||
371                 (bind->symbol != NULL && current->symbol == NULL))
372                 continue;
373
374             /* If bind is NULL, current has to be NULL, too (see above).
375              * If the keycodes differ, it can't be a duplicate. */
376             if (bind->symbol != NULL &&
377                 strcasecmp(bind->symbol, current->symbol) != 0)
378                 continue;
379
380             /* Check if the keycodes or modifiers are different. If so, they
381              * can't be duplicate */
382             if (bind->keycode != current->keycode ||
383                 bind->mods != current->mods)
384                 continue;
385             context->has_errors = true;
386             if (current->keycode != 0) {
387                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keycode %d, command \"%s\"\n",
388                      current->mods, current->keycode, current->command);
389             } else {
390                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keysym %s, command \"%s\"\n",
391                      current->mods, current->symbol, current->command);
392             }
393         }
394     }
395 }
396
397 static void migrate_i3bar_exec(struct Autostart *exec) {
398     ELOG("**********************************************************************\n");
399     ELOG("IGNORING exec command: %s\n", exec->command);
400     ELOG("It contains \"i3bar\". Since i3 v4.1, i3bar will be automatically started\n");
401     ELOG("for each 'bar' configuration block in your i3 config. Please remove the exec\n");
402     ELOG("line and add the following to your i3 config:\n");
403     ELOG("\n");
404     ELOG("    bar {\n");
405     ELOG("        status_command i3status\n");
406     ELOG("    }\n");
407     ELOG("**********************************************************************\n");
408
409     /* Generate a dummy bar configuration */
410     Barconfig *bar_config = scalloc(sizeof(Barconfig));
411     /* The hard-coded ID is not a problem. It does not conflict with the
412      * auto-generated bar IDs and having multiple hard-coded IDs is irrelevant
413      * – they all just contain status_command = i3status */
414     bar_config->id = sstrdup("migrate-bar");
415     bar_config->status_command = sstrdup("i3status");
416     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
417
418     /* Trigger an i3-nagbar */
419     context->has_warnings = true;
420 }
421
422 void parse_file(const char *f) {
423     SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
424     int fd, ret, read_bytes = 0;
425     struct stat stbuf;
426     char *buf;
427     FILE *fstr;
428     char buffer[1026], key[512], value[512];
429
430     if ((fd = open(f, O_RDONLY)) == -1)
431         die("Could not open configuration file: %s\n", strerror(errno));
432
433     if (fstat(fd, &stbuf) == -1)
434         die("Could not fstat file: %s\n", strerror(errno));
435
436     buf = scalloc((stbuf.st_size + 1) * sizeof(char));
437     while (read_bytes < stbuf.st_size) {
438         if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
439             die("Could not read(): %s\n", strerror(errno));
440         read_bytes += ret;
441     }
442
443     if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
444         die("Could not lseek: %s\n", strerror(errno));
445
446     if ((fstr = fdopen(fd, "r")) == NULL)
447         die("Could not fdopen: %s\n", strerror(errno));
448
449     while (!feof(fstr)) {
450         if (fgets(buffer, 1024, fstr) == NULL) {
451             if (feof(fstr))
452                 break;
453             die("Could not read configuration file\n");
454         }
455
456         /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
457         if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
458             key[0] == '#' || strlen(key) < 3)
459             continue;
460
461         if (strcasecmp(key, "set") == 0) {
462             if (value[0] != '$') {
463                 ELOG("Malformed variable assignment, name has to start with $\n");
464                 continue;
465             }
466
467             /* get key/value for this variable */
468             char *v_key = value, *v_value;
469             if (strstr(value, " ") == NULL && strstr(value, "\t") == NULL) {
470                 ELOG("Malformed variable assignment, need a value\n");
471                 continue;
472             }
473
474             if (!(v_value = strstr(value, " ")))
475                 v_value = strstr(value, "\t");
476
477             *(v_value++) = '\0';
478
479             struct Variable *new = scalloc(sizeof(struct Variable));
480             new->key = sstrdup(v_key);
481             new->value = sstrdup(v_value);
482             SLIST_INSERT_HEAD(&variables, new, variables);
483             DLOG("Got new variable %s = %s\n", v_key, v_value);
484             continue;
485         }
486     }
487     fclose(fstr);
488
489     /* For every custom variable, see how often it occurs in the file and
490      * how much extra bytes it requires when replaced. */
491     struct Variable *current, *nearest;
492     int extra_bytes = 0;
493     /* We need to copy the buffer because we need to invalidate the
494      * variables (otherwise we will count them twice, which is bad when
495      * 'extra' is negative) */
496     char *bufcopy = sstrdup(buf);
497     SLIST_FOREACH(current, &variables, variables) {
498         int extra = (strlen(current->value) - strlen(current->key));
499         char *next;
500         for (next = bufcopy;
501              next < (bufcopy + stbuf.st_size) &&
502              (next = strcasestr(next, current->key)) != NULL;
503              next += strlen(current->key)) {
504             *next = '_';
505             extra_bytes += extra;
506         }
507     }
508     FREE(bufcopy);
509
510     /* Then, allocate a new buffer and copy the file over to the new one,
511      * but replace occurences of our variables */
512     char *walk = buf, *destwalk;
513     char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
514     destwalk = new;
515     while (walk < (buf + stbuf.st_size)) {
516         /* Find the next variable */
517         SLIST_FOREACH(current, &variables, variables)
518             current->next_match = strcasestr(walk, current->key);
519         nearest = NULL;
520         int distance = stbuf.st_size;
521         SLIST_FOREACH(current, &variables, variables) {
522             if (current->next_match == NULL)
523                 continue;
524             if ((current->next_match - walk) < distance) {
525                 distance = (current->next_match - walk);
526                 nearest = current;
527             }
528         }
529         if (nearest == NULL) {
530             /* If there are no more variables, we just copy the rest */
531             strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
532             destwalk += (buf + stbuf.st_size) - walk;
533             *destwalk = '\0';
534             break;
535         } else {
536             /* Copy until the next variable, then copy its value */
537             strncpy(destwalk, walk, distance);
538             strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
539             walk += distance + strlen(nearest->key);
540             destwalk += distance + strlen(nearest->value);
541         }
542     }
543
544     /* analyze the string to find out whether this is an old config file (3.x)
545      * or a new config file (4.x). If it’s old, we run the converter script. */
546     int version = detect_version(buf);
547     if (version == 3) {
548         /* We need to convert this v3 configuration */
549         char *converted = migrate_config(new, stbuf.st_size);
550         if (converted != NULL) {
551             printf("\n");
552             printf("****************************************************************\n");
553             printf("NOTE: Automatically converted configuration file from v3 to v4.\n");
554             printf("\n");
555             printf("Please convert your config file to v4. You can use this command:\n");
556             printf("    mv %s %s.O\n", f, f);
557             printf("    i3-migrate-config-to-v4 %s.O > %s\n", f, f);
558             printf("****************************************************************\n");
559             printf("\n");
560             free(new);
561             new = converted;
562         } else {
563             printf("\n");
564             printf("**********************************************************************\n");
565             printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
566             printf("was not correctly installed on your system?\n");
567             printf("**********************************************************************\n");
568             printf("\n");
569         }
570     }
571
572     /* now lex/parse it */
573     yy_scan_string(new);
574
575     context = scalloc(sizeof(struct context));
576     context->filename = f;
577
578     if (yyparse() != 0) {
579         fprintf(stderr, "Could not parse configfile\n");
580         exit(1);
581     }
582
583     check_for_duplicate_bindings(context);
584
585     /* XXX: The following code will be removed in i3 v4.3 (three releases from
586      * now, as of 2011-10-22) */
587     /* Check for any exec or exec_always lines starting i3bar. We remove these
588      * and add a bar block instead. Additionally, a i3-nagbar warning (not an
589      * error) will be displayed so that users update their config file. */
590     struct Autostart *exec, *next;
591     for (exec = TAILQ_FIRST(&autostarts); exec; ) {
592         next = TAILQ_NEXT(exec, autostarts);
593         if (strstr(exec->command, "i3bar") != NULL) {
594             migrate_i3bar_exec(exec);
595             TAILQ_REMOVE(&autostarts, exec, autostarts);
596         }
597         exec = next;
598     }
599
600     for (exec = TAILQ_FIRST(&autostarts_always); exec; ) {
601         next = TAILQ_NEXT(exec, autostarts_always);
602         if (strstr(exec->command, "i3bar") != NULL) {
603             migrate_i3bar_exec(exec);
604             TAILQ_REMOVE(&autostarts_always, exec, autostarts_always);
605         }
606         exec = next;
607     }
608
609     if (context->has_errors || context->has_warnings) {
610         start_configerror_nagbar(f);
611     }
612
613     yylex_destroy();
614     FREE(context->line_copy);
615     free(context);
616     free(new);
617     free(buf);
618
619     while (!SLIST_EMPTY(&variables)) {
620         current = SLIST_FIRST(&variables);
621         FREE(current->key);
622         FREE(current->value);
623         SLIST_REMOVE_HEAD(&variables, variables);
624         FREE(current);
625     }
626 }
627
628 %}
629
630 %error-verbose
631 %lex-param { struct context *context }
632
633 %union {
634     int number;
635     char *string;
636     uint32_t *single_color;
637     struct Colortriple *color;
638     Match *match;
639     struct Binding *binding;
640 }
641
642 %token  <number>        NUMBER                      "<number>"
643 %token  <string>        WORD                        "<word>"
644 %token  <string>        STR                         "<string>"
645 %token  <string>        STR_NG                      "<string (non-greedy)>"
646 %token  <string>        HEXCOLOR                    "#<hex>"
647 %token  <string>        OUTPUT                      "<RandR output>"
648 %token                  TOKBINDCODE
649 %token                  TOKTERMINAL
650 %token                  TOKCOMMENT                  "<comment>"
651 %token                  TOKFONT                     "font"
652 %token                  TOKBINDSYM                  "bindsym"
653 %token  <number>        MODIFIER                    "<modifier>"
654 %token                  TOKCONTROL                  "control"
655 %token                  TOKSHIFT                    "shift"
656 %token                  TOKFLOATING_MODIFIER        "floating_modifier"
657 %token  <string>        QUOTEDSTRING                "<quoted string>"
658 %token                  TOKWORKSPACE                "workspace"
659 %token                  TOKOUTPUT                   "output"
660 %token                  TOKASSIGN                   "assign"
661 %token                  TOKSET
662 %token                  TOKIPCSOCKET                "ipc_socket"
663 %token                  TOKRESTARTSTATE             "restart_state"
664 %token                  TOKEXEC                     "exec"
665 %token                  TOKEXEC_ALWAYS              "exec_always"
666 %token  <single_color>  TOKSINGLECOLOR
667 %token  <color>         TOKCOLOR
668 %token                  TOKARROW                    "→"
669 %token                  TOKMODE                     "mode"
670 %token                  TOK_BAR                     "bar"
671 %token                  TOK_ORIENTATION             "default_orientation"
672 %token                  TOK_HORIZ                   "horizontal"
673 %token                  TOK_VERT                    "vertical"
674 %token                  TOK_AUTO                    "auto"
675 %token                  TOK_WORKSPACE_LAYOUT        "workspace_layout"
676 %token                  TOKNEWWINDOW                "new_window"
677 %token                  TOKNEWFLOAT                 "new_float"
678 %token                  TOK_NORMAL                  "normal"
679 %token                  TOK_NONE                    "none"
680 %token                  TOK_1PIXEL                  "1pixel"
681 %token                  TOKFOCUSFOLLOWSMOUSE        "focus_follows_mouse"
682 %token                  TOK_FORCE_FOCUS_WRAPPING    "force_focus_wrapping"
683 %token                  TOK_FORCE_XINERAMA          "force_xinerama"
684 %token                  TOK_WORKSPACE_AUTO_BAF      "workspace_auto_back_and_forth"
685 %token                  TOKWORKSPACEBAR             "workspace_bar"
686 %token                  TOK_DEFAULT                 "default"
687 %token                  TOK_STACKING                "stacking"
688 %token                  TOK_TABBED                  "tabbed"
689 %token  <number>        TOKSTACKLIMIT               "stack-limit"
690 %token                  TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
691 %token                  TOK_IGNORE                  "ignore"
692 %token                  TOK_LEAVE_FULLSCREEN        "leave_fullscreen"
693 %token                  TOK_FOR_WINDOW              "for_window"
694
695 %token                  TOK_BAR_OUTPUT              "output (bar)"
696 %token                  TOK_BAR_TRAY_OUTPUT         "tray_output"
697 %token                  TOK_BAR_SOCKET_PATH         "socket_path"
698 %token                  TOK_BAR_MODE                "mode"
699 %token                  TOK_BAR_HIDE                "hide"
700 %token                  TOK_BAR_DOCK                "dock"
701 %token                  TOK_BAR_POSITION            "position"
702 %token                  TOK_BAR_BOTTOM              "bottom"
703 %token                  TOK_BAR_TOP                 "top"
704 %token                  TOK_BAR_STATUS_COMMAND      "status_command"
705 %token                  TOK_BAR_FONT                "font"
706 %token                  TOK_BAR_WORKSPACE_BUTTONS   "workspace_buttons"
707 %token                  TOK_BAR_VERBOSE             "verbose"
708 %token                  TOK_BAR_COLORS              "colors"
709 %token                  TOK_BAR_COLOR_BACKGROUND    "background"
710 %token                  TOK_BAR_COLOR_STATUSLINE    "statusline"
711 %token                  TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
712 %token                  TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
713 %token                  TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
714 %token                  TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
715
716 %token              TOK_MARK            "mark"
717 %token              TOK_CLASS           "class"
718 %token              TOK_INSTANCE        "instance"
719 %token              TOK_WINDOW_ROLE     "window_role"
720 %token              TOK_ID              "id"
721 %token              TOK_CON_ID          "con_id"
722 %token              TOK_TITLE           "title"
723
724 %type   <binding>       binding
725 %type   <binding>       bindcode
726 %type   <binding>       bindsym
727 %type   <number>        binding_modifiers
728 %type   <number>        binding_modifier
729 %type   <number>        direction
730 %type   <number>        layout_mode
731 %type   <number>        border_style
732 %type   <number>        new_window
733 %type   <number>        new_float
734 %type   <number>        colorpixel
735 %type   <number>        bool
736 %type   <number>        popup_setting
737 %type   <number>        bar_position_position
738 %type   <number>        bar_mode_mode
739 %type   <string>        command
740 %type   <string>        word_or_number
741 %type   <string>        optional_workspace_name
742 %type   <string>        workspace_name
743 %type   <string>        window_class
744
745 %%
746
747 lines: /* empty */
748     | lines error
749     | lines line
750     ;
751
752 line:
753     bindline
754     | for_window
755     | mode
756     | bar
757     | floating_modifier
758     | orientation
759     | workspace_layout
760     | new_window
761     | new_float
762     | focus_follows_mouse
763     | force_focus_wrapping
764     | force_xinerama
765     | workspace_back_and_forth
766     | workspace_bar
767     | workspace
768     | assign
769     | ipcsocket
770     | restart_state
771     | exec
772     | exec_always
773     | single_color
774     | color
775     | terminal
776     | font
777     | comment
778     | popup_during_fullscreen
779     ;
780
781 comment:
782     TOKCOMMENT
783     ;
784
785 command:
786     STR
787     ;
788
789 bindline:
790     binding
791     {
792         TAILQ_INSERT_TAIL(bindings, $1, bindings);
793     }
794     ;
795
796 binding:
797     TOKBINDCODE bindcode         { $$ = $2; }
798     | TOKBINDSYM bindsym         { $$ = $2; }
799     ;
800
801 bindcode:
802     binding_modifiers NUMBER command
803     {
804         printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
805         Binding *new = scalloc(sizeof(Binding));
806
807         new->keycode = $2;
808         new->mods = $1;
809         new->command = $3;
810
811         $$ = new;
812     }
813     ;
814
815 bindsym:
816     binding_modifiers word_or_number command
817     {
818         printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
819         Binding *new = scalloc(sizeof(Binding));
820
821         new->symbol = $2;
822         new->mods = $1;
823         new->command = $3;
824
825         $$ = new;
826     }
827     ;
828
829 for_window:
830     TOK_FOR_WINDOW match command
831     {
832         if (match_is_empty(&current_match)) {
833             ELOG("Match is empty, ignoring this for_window statement\n");
834             break;
835         }
836         printf("\t should execute command %s for the criteria mentioned above\n", $3);
837         Assignment *assignment = scalloc(sizeof(Assignment));
838         assignment->type = A_COMMAND;
839         assignment->match = current_match;
840         assignment->dest.command = $3;
841         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
842     }
843     ;
844
845 match:
846     | matchstart criteria matchend
847     {
848         printf("match parsed\n");
849     }
850     ;
851
852 matchstart:
853     '['
854     {
855         printf("start\n");
856         match_init(&current_match);
857     }
858     ;
859
860 matchend:
861     ']'
862     {
863         printf("match specification finished\n");
864     }
865     ;
866
867 criteria:
868     criteria criterion
869     | criterion
870     ;
871
872 criterion:
873     TOK_CLASS '=' STR
874     {
875         printf("criteria: class = %s\n", $3);
876         current_match.class = regex_new($3);
877         free($3);
878     }
879     | TOK_INSTANCE '=' STR
880     {
881         printf("criteria: instance = %s\n", $3);
882         current_match.instance = regex_new($3);
883         free($3);
884     }
885     | TOK_WINDOW_ROLE '=' STR
886     {
887         printf("criteria: window_role = %s\n", $3);
888         current_match.role = regex_new($3);
889         free($3);
890     }
891     | TOK_CON_ID '=' STR
892     {
893         printf("criteria: id = %s\n", $3);
894         char *end;
895         long parsed = strtol($3, &end, 10);
896         if (parsed == LONG_MIN ||
897             parsed == LONG_MAX ||
898             parsed < 0 ||
899             (end && *end != '\0')) {
900             ELOG("Could not parse con id \"%s\"\n", $3);
901         } else {
902             current_match.con_id = (Con*)parsed;
903             printf("id as int = %p\n", current_match.con_id);
904         }
905     }
906     | TOK_ID '=' STR
907     {
908         printf("criteria: window id = %s\n", $3);
909         char *end;
910         long parsed = strtol($3, &end, 10);
911         if (parsed == LONG_MIN ||
912             parsed == LONG_MAX ||
913             parsed < 0 ||
914             (end && *end != '\0')) {
915             ELOG("Could not parse window id \"%s\"\n", $3);
916         } else {
917             current_match.id = parsed;
918             printf("window id as int = %d\n", current_match.id);
919         }
920     }
921     | TOK_MARK '=' STR
922     {
923         printf("criteria: mark = %s\n", $3);
924         current_match.mark = regex_new($3);
925         free($3);
926     }
927     | TOK_TITLE '=' STR
928     {
929         printf("criteria: title = %s\n", $3);
930         current_match.title = regex_new($3);
931         free($3);
932     }
933     ;
934
935
936
937 word_or_number:
938     WORD
939     | NUMBER
940     {
941         asprintf(&$$, "%d", $1);
942     }
943     ;
944
945 mode:
946     TOKMODE QUOTEDSTRING '{' modelines '}'
947     {
948         if (strcasecmp($2, "default") == 0) {
949             printf("You cannot use the name \"default\" for your mode\n");
950             exit(1);
951         }
952         printf("\t now in mode %s\n", $2);
953         printf("\t current bindings = %p\n", current_bindings);
954         Binding *binding;
955         TAILQ_FOREACH(binding, current_bindings, bindings) {
956             printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
957                             binding->mods, binding->keycode, binding->symbol, binding->command);
958         }
959
960         struct Mode *mode = scalloc(sizeof(struct Mode));
961         mode->name = $2;
962         mode->bindings = current_bindings;
963         current_bindings = NULL;
964         SLIST_INSERT_HEAD(&modes, mode, modes);
965     }
966     ;
967
968
969 modelines:
970     /* empty */
971     | modelines modeline
972     ;
973
974 modeline:
975     comment
976     | binding
977     {
978         if (current_bindings == NULL) {
979             current_bindings = scalloc(sizeof(struct bindings_head));
980             TAILQ_INIT(current_bindings);
981         }
982
983         TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
984     }
985     ;
986
987 bar:
988     TOK_BAR '{' barlines '}'
989     {
990         printf("\t new bar configuration finished, saving.\n");
991         /* Generate a unique ID for this bar */
992         current_bar.id = sstrdup("bar-XXXXXX");
993         /* This works similar to mktemp in that it replaces the last six X with
994          * random letters, but without the restriction that the given buffer
995          * has to contain a valid path name. */
996         char *x = current_bar.id + strlen("bar-");
997         while (*x != '\0') {
998             *(x++) = (rand() % 26) + 'a';
999         }
1000
1001         /* Copy the current (static) structure into a dynamically allocated
1002          * one, then cleanup our static one. */
1003         Barconfig *bar_config = scalloc(sizeof(Barconfig));
1004         memcpy(bar_config, &current_bar, sizeof(Barconfig));
1005         TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
1006
1007         memset(&current_bar, '\0', sizeof(Barconfig));
1008     }
1009     ;
1010
1011 barlines:
1012     /* empty */
1013     | barlines barline
1014     ;
1015
1016 barline:
1017     comment
1018     | bar_status_command
1019     | bar_output
1020     | bar_tray_output
1021     | bar_position
1022     | bar_mode
1023     | bar_font
1024     | bar_workspace_buttons
1025     | bar_verbose
1026     | bar_socket_path
1027     | bar_colors
1028     | bar_color_background
1029     | bar_color_statusline
1030     | bar_color_focused_workspace
1031     | bar_color_active_workspace
1032     | bar_color_inactive_workspace
1033     | bar_color_urgent_workspace
1034     ;
1035
1036 bar_status_command:
1037     TOK_BAR_STATUS_COMMAND STR
1038     {
1039         DLOG("should add status command %s\n", $2);
1040         FREE(current_bar.status_command);
1041         current_bar.status_command = $2;
1042     }
1043     ;
1044
1045 bar_output:
1046     TOK_BAR_OUTPUT STR
1047     {
1048         DLOG("bar output %s\n", $2);
1049         int new_outputs = current_bar.num_outputs + 1;
1050         current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
1051         current_bar.outputs[current_bar.num_outputs] = $2;
1052         current_bar.num_outputs = new_outputs;
1053     }
1054     ;
1055
1056 bar_tray_output:
1057     TOK_BAR_TRAY_OUTPUT STR
1058     {
1059         DLOG("tray %s\n", $2);
1060         FREE(current_bar.tray_output);
1061         current_bar.tray_output = $2;
1062     }
1063     ;
1064
1065 bar_position:
1066     TOK_BAR_POSITION bar_position_position
1067     {
1068         DLOG("position %d\n", $2);
1069         current_bar.position = $2;
1070     }
1071     ;
1072
1073 bar_position_position:
1074     TOK_BAR_TOP      { $$ = P_TOP; }
1075     | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1076     ;
1077
1078 bar_mode:
1079     TOK_BAR_MODE bar_mode_mode
1080     {
1081         DLOG("mode %d\n", $2);
1082         current_bar.mode = $2;
1083     }
1084     ;
1085
1086 bar_mode_mode:
1087     TOK_BAR_HIDE   { $$ = M_HIDE; }
1088     | TOK_BAR_DOCK { $$ = M_DOCK; }
1089     ;
1090
1091 bar_font:
1092     TOK_BAR_FONT STR
1093     {
1094         DLOG("font %s\n", $2);
1095         FREE(current_bar.font);
1096         current_bar.font = $2;
1097     }
1098     ;
1099
1100 bar_workspace_buttons:
1101     TOK_BAR_WORKSPACE_BUTTONS bool
1102     {
1103         DLOG("workspace_buttons = %d\n", $2);
1104         /* We store this inverted to make the default setting right when
1105          * initializing the struct with zero. */
1106         current_bar.hide_workspace_buttons = !($2);
1107     }
1108     ;
1109
1110 bar_verbose:
1111     TOK_BAR_VERBOSE bool
1112     {
1113         DLOG("verbose = %d\n", $2);
1114         current_bar.verbose = $2;
1115     }
1116     ;
1117
1118 bar_socket_path:
1119     TOK_BAR_SOCKET_PATH STR
1120     {
1121         DLOG("socket_path = %s\n", $2);
1122         FREE(current_bar.socket_path);
1123         current_bar.socket_path = $2;
1124     }
1125     ;
1126
1127 bar_colors:
1128     TOK_BAR_COLORS '{' barlines '}'
1129     {
1130         /* At the moment, the TOK_BAR_COLORS token is only to make the config
1131          * friendlier for humans. We might change this in the future if it gets
1132          * more complex. */
1133     }
1134     ;
1135
1136 bar_color_background:
1137     TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1138     {
1139         DLOG("background = %s\n", $2);
1140         current_bar.colors.background = $2;
1141     }
1142     ;
1143
1144 bar_color_statusline:
1145     TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1146     {
1147         DLOG("statusline = %s\n", $2);
1148         current_bar.colors.statusline = $2;
1149     }
1150     ;
1151
1152 bar_color_focused_workspace:
1153     TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1154     {
1155         DLOG("focused_ws = %s and %s\n", $2, $3);
1156         current_bar.colors.focused_workspace_text = $2;
1157         current_bar.colors.focused_workspace_bg = $3;
1158     }
1159     ;
1160
1161 bar_color_active_workspace:
1162     TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1163     {
1164         DLOG("active_ws = %s and %s\n", $2, $3);
1165         current_bar.colors.active_workspace_text = $2;
1166         current_bar.colors.active_workspace_bg = $3;
1167     }
1168     ;
1169
1170 bar_color_inactive_workspace:
1171     TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1172     {
1173         DLOG("inactive_ws = %s and %s\n", $2, $3);
1174         current_bar.colors.inactive_workspace_text = $2;
1175         current_bar.colors.inactive_workspace_bg = $3;
1176     }
1177     ;
1178
1179 bar_color_urgent_workspace:
1180     TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1181     {
1182         DLOG("urgent_ws = %s and %s\n", $2, $3);
1183         current_bar.colors.urgent_workspace_text = $2;
1184         current_bar.colors.urgent_workspace_bg = $3;
1185     }
1186     ;
1187
1188 floating_modifier:
1189     TOKFLOATING_MODIFIER binding_modifiers
1190     {
1191         DLOG("floating modifier = %d\n", $2);
1192         config.floating_modifier = $2;
1193     }
1194     ;
1195
1196 orientation:
1197     TOK_ORIENTATION direction
1198     {
1199         DLOG("New containers should start with split direction %d\n", $2);
1200         config.default_orientation = $2;
1201     }
1202     ;
1203
1204 direction:
1205     TOK_HORIZ       { $$ = HORIZ; }
1206     | TOK_VERT      { $$ = VERT; }
1207     | TOK_AUTO      { $$ = NO_ORIENTATION; }
1208     ;
1209
1210 workspace_layout:
1211     TOK_WORKSPACE_LAYOUT layout_mode
1212     {
1213         DLOG("new containers will be in mode %d\n", $2);
1214         config.default_layout = $2;
1215
1216 #if 0
1217         /* We also need to change the layout of the already existing
1218          * workspaces here. Workspaces may exist at this point because
1219          * of the other directives which are modifying workspaces
1220          * (setting the preferred screen or name). While the workspace
1221          * objects are already created, they have never been used.
1222          * Thus, the user very likely awaits the default container mode
1223          * to trigger in this case, regardless of where it is inside
1224          * his configuration file. */
1225         Workspace *ws;
1226         TAILQ_FOREACH(ws, workspaces, workspaces) {
1227                 if (ws->table == NULL)
1228                         continue;
1229                 switch_layout_mode(global_conn,
1230                                    ws->table[0][0],
1231                                    config.container_mode);
1232         }
1233 #endif
1234     }
1235     | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1236     {
1237         DLOG("stack-limit %d with val %d\n", $3, $4);
1238         config.container_stack_limit = $3;
1239         config.container_stack_limit_value = $4;
1240
1241 #if 0
1242         /* See the comment above */
1243         Workspace *ws;
1244         TAILQ_FOREACH(ws, workspaces, workspaces) {
1245                 if (ws->table == NULL)
1246                         continue;
1247                 Container *con = ws->table[0][0];
1248                 con->stack_limit = config.container_stack_limit;
1249                 con->stack_limit_value = config.container_stack_limit_value;
1250         }
1251 #endif
1252     }
1253     ;
1254
1255 layout_mode:
1256     TOK_DEFAULT       { $$ = L_DEFAULT; }
1257     | TOK_STACKING    { $$ = L_STACKED; }
1258     | TOK_TABBED      { $$ = L_TABBED; }
1259     ;
1260
1261 new_window:
1262     TOKNEWWINDOW border_style
1263     {
1264         DLOG("new windows should start with border style %d\n", $2);
1265         config.default_border = $2;
1266     }
1267     ;
1268
1269 new_float:
1270     TOKNEWFLOAT border_style
1271     {
1272        DLOG("new floating windows should start with border style %d\n", $2);
1273        config.default_floating_border = $2;
1274     }
1275     ;
1276
1277 border_style:
1278     TOK_NORMAL      { $$ = BS_NORMAL; }
1279     | TOK_NONE      { $$ = BS_NONE; }
1280     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
1281     ;
1282
1283 bool:
1284     NUMBER
1285     {
1286         $$ = ($1 == 1);
1287     }
1288     | WORD
1289     {
1290         DLOG("checking word \"%s\"\n", $1);
1291         $$ = (strcasecmp($1, "yes") == 0 ||
1292               strcasecmp($1, "true") == 0 ||
1293               strcasecmp($1, "on") == 0 ||
1294               strcasecmp($1, "enable") == 0 ||
1295               strcasecmp($1, "active") == 0);
1296     }
1297     ;
1298
1299 focus_follows_mouse:
1300     TOKFOCUSFOLLOWSMOUSE bool
1301     {
1302         DLOG("focus follows mouse = %d\n", $2);
1303         config.disable_focus_follows_mouse = !($2);
1304     }
1305     ;
1306
1307 force_focus_wrapping:
1308     TOK_FORCE_FOCUS_WRAPPING bool
1309     {
1310         DLOG("force focus wrapping = %d\n", $2);
1311         config.force_focus_wrapping = $2;
1312     }
1313     ;
1314
1315 force_xinerama:
1316     TOK_FORCE_XINERAMA bool
1317     {
1318         DLOG("force xinerama = %d\n", $2);
1319         config.force_xinerama = $2;
1320     }
1321     ;
1322
1323 workspace_back_and_forth:
1324     TOK_WORKSPACE_AUTO_BAF bool
1325     {
1326         DLOG("automatic workspace back-and-forth = %d\n", $2);
1327         config.workspace_auto_back_and_forth = $2;
1328     }
1329     ;
1330
1331 workspace_bar:
1332     TOKWORKSPACEBAR bool
1333     {
1334         DLOG("workspace bar = %d\n", $2);
1335         config.disable_workspace_bar = !($2);
1336     }
1337     ;
1338
1339 workspace:
1340     TOKWORKSPACE NUMBER TOKOUTPUT OUTPUT optional_workspace_name
1341     {
1342         int ws_num = $2;
1343         if (ws_num < 1) {
1344             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1345         } else {
1346             char *ws_name = NULL;
1347             if ($5 == NULL) {
1348                 asprintf(&ws_name, "%d", ws_num);
1349             } else {
1350                 ws_name = $5;
1351             }
1352
1353             DLOG("Should assign workspace %s to output %s\n", ws_name, $4);
1354             /* Check for earlier assignments of the same workspace so that we
1355              * don’t have assignments of a single workspace to different
1356              * outputs */
1357             struct Workspace_Assignment *assignment;
1358             bool duplicate = false;
1359             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1360                 if (strcasecmp(assignment->name, ws_name) == 0) {
1361                     ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1362                          ws_name);
1363                     assignment->output = $4;
1364                     duplicate = true;
1365                 }
1366             }
1367             if (!duplicate) {
1368                 assignment = scalloc(sizeof(struct Workspace_Assignment));
1369                 assignment->name = ws_name;
1370                 assignment->output = $4;
1371                 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1372             }
1373         }
1374     }
1375     | TOKWORKSPACE NUMBER workspace_name
1376     {
1377         int ws_num = $2;
1378         if (ws_num < 1) {
1379             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1380         } else {
1381             DLOG("workspace name to: %s\n", $3);
1382 #if 0
1383             if ($<string>3 != NULL) {
1384                     workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1385                     free($<string>3);
1386             }
1387 #endif
1388         }
1389     }
1390     ;
1391
1392 optional_workspace_name:
1393     /* empty */          { $$ = NULL; }
1394     | workspace_name     { $$ = $1; }
1395     ;
1396
1397 workspace_name:
1398     QUOTEDSTRING         { $$ = $1; }
1399     | STR                { $$ = $1; }
1400     | WORD               { $$ = $1; }
1401     ;
1402
1403 assign:
1404     TOKASSIGN window_class STR
1405     {
1406         /* This is the old, deprecated form of assignments. It’s provided for
1407          * compatibility in version (4.1, 4.2, 4.3) and will be removed
1408          * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1409         ELOG("You are using the old assign syntax (without criteria). "
1410              "Please see the User's Guide for the new syntax and fix "
1411              "your config file.\n");
1412         context->has_warnings = true;
1413         printf("assignment of %s to *%s*\n", $2, $3);
1414         char *workspace = $3;
1415         char *criteria = $2;
1416
1417         Assignment *assignment = scalloc(sizeof(Assignment));
1418         Match *match = &(assignment->match);
1419         match_init(match);
1420
1421         char *separator = NULL;
1422         if ((separator = strchr(criteria, '/')) != NULL) {
1423             *(separator++) = '\0';
1424             char *pattern;
1425             if (asprintf(&pattern, "(?i)%s", separator) == -1) {
1426                 ELOG("asprintf failed\n");
1427                 break;
1428             }
1429             match->title = regex_new(pattern);
1430             free(pattern);
1431             printf("  title = %s\n", separator);
1432         }
1433         if (*criteria != '\0') {
1434             char *pattern;
1435             if (asprintf(&pattern, "(?i)%s", criteria) == -1) {
1436                 ELOG("asprintf failed\n");
1437                 break;
1438             }
1439             match->class = regex_new(pattern);
1440             free(pattern);
1441             printf("  class = %s\n", criteria);
1442         }
1443         free(criteria);
1444
1445         /* Compatibility with older versions: If the assignment target starts
1446          * with ~, we create the equivalent of:
1447          *
1448          * for_window [class="foo"] floating enable
1449          */
1450         if (*workspace == '~') {
1451             workspace++;
1452             if (*workspace == '\0') {
1453                 /* This assignment was *only* for floating */
1454                 assignment->type = A_COMMAND;
1455                 assignment->dest.command = sstrdup("floating enable");
1456                 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1457                 break;
1458             } else {
1459                 /* Create a new assignment and continue afterwards */
1460                 Assignment *floating = scalloc(sizeof(Assignment));
1461                 match_copy(&(floating->match), match);
1462                 floating->type = A_COMMAND;
1463                 floating->dest.command = sstrdup("floating enable");
1464                 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1465             }
1466         }
1467
1468         assignment->type = A_TO_WORKSPACE;
1469         assignment->dest.workspace = workspace;
1470         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1471     }
1472     | TOKASSIGN match STR
1473     {
1474         if (match_is_empty(&current_match)) {
1475             ELOG("Match is empty, ignoring this assignment\n");
1476             break;
1477         }
1478         printf("new assignment, using above criteria, to workspace %s\n", $3);
1479         Assignment *assignment = scalloc(sizeof(Assignment));
1480         assignment->match = current_match;
1481         assignment->type = A_TO_WORKSPACE;
1482         assignment->dest.workspace = $3;
1483         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1484     }
1485     ;
1486
1487 window_class:
1488     QUOTEDSTRING
1489     | STR_NG
1490     ;
1491
1492 ipcsocket:
1493     TOKIPCSOCKET STR
1494     {
1495         config.ipc_socket_path = $2;
1496     }
1497     ;
1498
1499 restart_state:
1500     TOKRESTARTSTATE STR
1501     {
1502         config.restart_state_path = $2;
1503     }
1504     ;
1505
1506 exec:
1507     TOKEXEC STR
1508     {
1509         struct Autostart *new = smalloc(sizeof(struct Autostart));
1510         new->command = $2;
1511         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1512     }
1513     ;
1514
1515 exec_always:
1516     TOKEXEC_ALWAYS STR
1517     {
1518         struct Autostart *new = smalloc(sizeof(struct Autostart));
1519         new->command = $2;
1520         TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1521     }
1522     ;
1523
1524 terminal:
1525     TOKTERMINAL STR
1526     {
1527         ELOG("The terminal option is DEPRECATED and has no effect. "
1528             "Please remove it from your configuration file.\n");
1529     }
1530     ;
1531
1532 font:
1533     TOKFONT STR
1534     {
1535         config.font = load_font($2, true);
1536         printf("font %s\n", $2);
1537         free($2);
1538     }
1539     ;
1540
1541 single_color:
1542     TOKSINGLECOLOR colorpixel
1543     {
1544         uint32_t *dest = $1;
1545         *dest = $2;
1546     }
1547     ;
1548
1549 color:
1550     TOKCOLOR colorpixel colorpixel colorpixel
1551     {
1552         struct Colortriple *dest = $1;
1553
1554         dest->border = $2;
1555         dest->background = $3;
1556         dest->text = $4;
1557     }
1558     ;
1559
1560 colorpixel:
1561     HEXCOLOR
1562     {
1563         $$ = get_colorpixel($1);
1564         free($1);
1565     }
1566     ;
1567
1568
1569 binding_modifiers:
1570     /* NULL */                               { $$ = 0; }
1571     | binding_modifier
1572     | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1573     | binding_modifiers '+'                  { $$ = $1; }
1574     ;
1575
1576 binding_modifier:
1577     MODIFIER        { $$ = $1; }
1578     | TOKCONTROL    { $$ = BIND_CONTROL; }
1579     | TOKSHIFT      { $$ = BIND_SHIFT; }
1580     ;
1581
1582 popup_during_fullscreen:
1583     TOK_POPUP_DURING_FULLSCREEN popup_setting
1584     {
1585         DLOG("popup_during_fullscreen setting: %d\n", $2);
1586         config.popup_during_fullscreen = $2;
1587     }
1588     ;
1589
1590 popup_setting:
1591     TOK_IGNORE              { $$ = PDF_IGNORE; }
1592     | TOK_LEAVE_FULLSCREEN  { $$ = PDF_LEAVE_FULLSCREEN; }
1593     ;