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