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