]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
Merge branch 'fix-split-indicator'
[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                 bind->release != current->release)
436                 continue;
437
438             context->has_errors = true;
439             if (current->keycode != 0) {
440                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keycode %d, command \"%s\"\n",
441                      current->mods, current->keycode, current->command);
442             } else {
443                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keysym %s, command \"%s\"\n",
444                      current->mods, current->symbol, current->command);
445             }
446         }
447     }
448 }
449
450 static void migrate_i3bar_exec(struct Autostart *exec) {
451     ELOG("**********************************************************************\n");
452     ELOG("IGNORING exec command: %s\n", exec->command);
453     ELOG("It contains \"i3bar\". Since i3 v4.1, i3bar will be automatically started\n");
454     ELOG("for each 'bar' configuration block in your i3 config. Please remove the exec\n");
455     ELOG("line and add the following to your i3 config:\n");
456     ELOG("\n");
457     ELOG("    bar {\n");
458     ELOG("        status_command i3status\n");
459     ELOG("    }\n");
460     ELOG("**********************************************************************\n");
461
462     /* Generate a dummy bar configuration */
463     Barconfig *bar_config = scalloc(sizeof(Barconfig));
464     /* The hard-coded ID is not a problem. It does not conflict with the
465      * auto-generated bar IDs and having multiple hard-coded IDs is irrelevant
466      * – they all just contain status_command = i3status */
467     bar_config->id = sstrdup("migrate-bar");
468     bar_config->status_command = sstrdup("i3status");
469     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
470
471     /* Trigger an i3-nagbar */
472     context->has_warnings = true;
473 }
474
475 void parse_file(const char *f) {
476     SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
477     int fd, ret, read_bytes = 0;
478     struct stat stbuf;
479     char *buf;
480     FILE *fstr;
481     char buffer[1026], key[512], value[512];
482
483     if ((fd = open(f, O_RDONLY)) == -1)
484         die("Could not open configuration file: %s\n", strerror(errno));
485
486     if (fstat(fd, &stbuf) == -1)
487         die("Could not fstat file: %s\n", strerror(errno));
488
489     buf = scalloc((stbuf.st_size + 1) * sizeof(char));
490     while (read_bytes < stbuf.st_size) {
491         if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
492             die("Could not read(): %s\n", strerror(errno));
493         read_bytes += ret;
494     }
495
496     if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
497         die("Could not lseek: %s\n", strerror(errno));
498
499     if ((fstr = fdopen(fd, "r")) == NULL)
500         die("Could not fdopen: %s\n", strerror(errno));
501
502     while (!feof(fstr)) {
503         if (fgets(buffer, 1024, fstr) == NULL) {
504             if (feof(fstr))
505                 break;
506             die("Could not read configuration file\n");
507         }
508
509         /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
510         if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
511             key[0] == '#' || strlen(key) < 3)
512             continue;
513
514         if (strcasecmp(key, "set") == 0) {
515             if (value[0] != '$') {
516                 ELOG("Malformed variable assignment, name has to start with $\n");
517                 continue;
518             }
519
520             /* get key/value for this variable */
521             char *v_key = value, *v_value;
522             if (strstr(value, " ") == NULL && strstr(value, "\t") == NULL) {
523                 ELOG("Malformed variable assignment, need a value\n");
524                 continue;
525             }
526
527             if (!(v_value = strstr(value, " ")))
528                 v_value = strstr(value, "\t");
529
530             *(v_value++) = '\0';
531             while (*v_value == '\t' || *v_value == ' ')
532                 v_value++;
533
534             struct Variable *new = scalloc(sizeof(struct Variable));
535             new->key = sstrdup(v_key);
536             new->value = sstrdup(v_value);
537             SLIST_INSERT_HEAD(&variables, new, variables);
538             DLOG("Got new variable %s = %s\n", v_key, v_value);
539             continue;
540         }
541     }
542     fclose(fstr);
543
544     /* For every custom variable, see how often it occurs in the file and
545      * how much extra bytes it requires when replaced. */
546     struct Variable *current, *nearest;
547     int extra_bytes = 0;
548     /* We need to copy the buffer because we need to invalidate the
549      * variables (otherwise we will count them twice, which is bad when
550      * 'extra' is negative) */
551     char *bufcopy = sstrdup(buf);
552     SLIST_FOREACH(current, &variables, variables) {
553         int extra = (strlen(current->value) - strlen(current->key));
554         char *next;
555         for (next = bufcopy;
556              next < (bufcopy + stbuf.st_size) &&
557              (next = strcasestr(next, current->key)) != NULL;
558              next += strlen(current->key)) {
559             *next = '_';
560             extra_bytes += extra;
561         }
562     }
563     FREE(bufcopy);
564
565     /* Then, allocate a new buffer and copy the file over to the new one,
566      * but replace occurences of our variables */
567     char *walk = buf, *destwalk;
568     char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
569     destwalk = new;
570     while (walk < (buf + stbuf.st_size)) {
571         /* Find the next variable */
572         SLIST_FOREACH(current, &variables, variables)
573             current->next_match = strcasestr(walk, current->key);
574         nearest = NULL;
575         int distance = stbuf.st_size;
576         SLIST_FOREACH(current, &variables, variables) {
577             if (current->next_match == NULL)
578                 continue;
579             if ((current->next_match - walk) < distance) {
580                 distance = (current->next_match - walk);
581                 nearest = current;
582             }
583         }
584         if (nearest == NULL) {
585             /* If there are no more variables, we just copy the rest */
586             strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
587             destwalk += (buf + stbuf.st_size) - walk;
588             *destwalk = '\0';
589             break;
590         } else {
591             /* Copy until the next variable, then copy its value */
592             strncpy(destwalk, walk, distance);
593             strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
594             walk += distance + strlen(nearest->key);
595             destwalk += distance + strlen(nearest->value);
596         }
597     }
598
599     /* analyze the string to find out whether this is an old config file (3.x)
600      * or a new config file (4.x). If it’s old, we run the converter script. */
601     int version = detect_version(buf);
602     if (version == 3) {
603         /* We need to convert this v3 configuration */
604         char *converted = migrate_config(new, stbuf.st_size);
605         if (converted != NULL) {
606             ELOG("\n");
607             ELOG("****************************************************************\n");
608             ELOG("NOTE: Automatically converted configuration file from v3 to v4.\n");
609             ELOG("\n");
610             ELOG("Please convert your config file to v4. You can use this command:\n");
611             ELOG("    mv %s %s.O\n", f, f);
612             ELOG("    i3-migrate-config-to-v4 %s.O > %s\n", f, f);
613             ELOG("****************************************************************\n");
614             ELOG("\n");
615             free(new);
616             new = converted;
617         } else {
618             printf("\n");
619             printf("**********************************************************************\n");
620             printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
621             printf("was not correctly installed on your system?\n");
622             printf("**********************************************************************\n");
623             printf("\n");
624         }
625     }
626
627     /* now lex/parse it */
628     yy_scan_string(new);
629
630     context = scalloc(sizeof(struct context));
631     context->filename = f;
632
633     if (yyparse() != 0) {
634         fprintf(stderr, "Could not parse configfile\n");
635         exit(1);
636     }
637
638     check_for_duplicate_bindings(context);
639
640     /* XXX: The following code will be removed in i3 v4.3 (three releases from
641      * now, as of 2011-10-22) */
642     /* Check for any exec or exec_always lines starting i3bar. We remove these
643      * and add a bar block instead. Additionally, a i3-nagbar warning (not an
644      * error) will be displayed so that users update their config file. */
645     struct Autostart *exec, *next;
646     for (exec = TAILQ_FIRST(&autostarts); exec; ) {
647         next = TAILQ_NEXT(exec, autostarts);
648         if (strstr(exec->command, "i3bar") != NULL) {
649             migrate_i3bar_exec(exec);
650             TAILQ_REMOVE(&autostarts, exec, autostarts);
651         }
652         exec = next;
653     }
654
655     for (exec = TAILQ_FIRST(&autostarts_always); exec; ) {
656         next = TAILQ_NEXT(exec, autostarts_always);
657         if (strstr(exec->command, "i3bar") != NULL) {
658             migrate_i3bar_exec(exec);
659             TAILQ_REMOVE(&autostarts_always, exec, autostarts_always);
660         }
661         exec = next;
662     }
663
664     if (context->has_errors || context->has_warnings) {
665         ELOG("FYI: You are using i3 version " I3_VERSION "\n");
666         if (version == 3)
667             ELOG("Please convert your configfile first, then fix any remaining errors (see above).\n");
668         start_configerror_nagbar(f);
669     }
670
671     yylex_destroy();
672     FREE(context->line_copy);
673     free(context);
674     FREE(font_pattern);
675     free(new);
676     free(buf);
677
678     while (!SLIST_EMPTY(&variables)) {
679         current = SLIST_FIRST(&variables);
680         FREE(current->key);
681         FREE(current->value);
682         SLIST_REMOVE_HEAD(&variables, variables);
683         FREE(current);
684     }
685 }
686
687 %}
688
689 %error-verbose
690 %lex-param { struct context *context }
691
692 %union {
693     int number;
694     char *string;
695     uint32_t *single_color;
696     struct Colortriple *color;
697     Match *match;
698     struct Binding *binding;
699 }
700
701 %token  <number>        NUMBER                      "<number>"
702 %token  <string>        WORD                        "<word>"
703 %token  <string>        STR                         "<string>"
704 %token  <string>        STR_NG                      "<string (non-greedy)>"
705 %token  <string>        HEXCOLOR                    "#<hex>"
706 %token  <string>        OUTPUT                      "<RandR output>"
707 %token                  TOKBINDCODE
708 %token                  TOKTERMINAL
709 %token                  TOKCOMMENT                  "<comment>"
710 %token                  TOKFONT                     "font"
711 %token                  TOKBINDSYM                  "bindsym"
712 %token  <number>        MODIFIER                    "<modifier>"
713 %token                  TOKCONTROL                  "control"
714 %token                  TOKSHIFT                    "shift"
715 %token                  TOKFLOATING_MODIFIER        "floating_modifier"
716 %token                  TOKFLOATING_MAXIMUM_SIZE    "floating_maximum_size"
717 %token                  TOKFLOATING_MINIMUM_SIZE    "floating_minimum_size"
718 %token  <string>        QUOTEDSTRING                "<quoted string>"
719 %token                  TOKWORKSPACE                "workspace"
720 %token                  TOKOUTPUT                   "output"
721 %token                  TOKASSIGN                   "assign"
722 %token                  TOKSET
723 %token                  TOKIPCSOCKET                "ipc_socket"
724 %token                  TOKRESTARTSTATE             "restart_state"
725 %token                  TOKEXEC                     "exec"
726 %token                  TOKEXEC_ALWAYS              "exec_always"
727 %token  <single_color>  TOKSINGLECOLOR
728 %token  <color>         TOKCOLOR
729 %token                  TOKARROW                    "→"
730 %token                  TOKMODE                     "mode"
731 %token                  TOK_BAR                     "bar"
732 %token                  TOK_ORIENTATION             "default_orientation"
733 %token                  TOK_HORIZ                   "horizontal"
734 %token                  TOK_VERT                    "vertical"
735 %token                  TOK_AUTO                    "auto"
736 %token                  TOK_WORKSPACE_LAYOUT        "workspace_layout"
737 %token                  TOKNEWWINDOW                "new_window"
738 %token                  TOKNEWFLOAT                 "new_float"
739 %token                  TOK_NORMAL                  "normal"
740 %token                  TOK_NONE                    "none"
741 %token                  TOK_1PIXEL                  "1pixel"
742 %token                  TOK_HIDE_EDGE_BORDERS       "hide_edge_borders"
743 %token                  TOK_BOTH                    "both"
744 %token                  TOKFOCUSFOLLOWSMOUSE        "focus_follows_mouse"
745 %token                  TOK_FORCE_FOCUS_WRAPPING    "force_focus_wrapping"
746 %token                  TOK_FORCE_XINERAMA          "force_xinerama"
747 %token                  TOK_FAKE_OUTPUTS            "fake_outputs"
748 %token                  TOK_WORKSPACE_AUTO_BAF      "workspace_auto_back_and_forth"
749 %token                  TOKWORKSPACEBAR             "workspace_bar"
750 %token                  TOK_DEFAULT                 "default"
751 %token                  TOK_STACKING                "stacking"
752 %token                  TOK_TABBED                  "tabbed"
753 %token  <number>        TOKSTACKLIMIT               "stack-limit"
754 %token                  TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
755 %token                  TOK_IGNORE                  "ignore"
756 %token                  TOK_LEAVE_FULLSCREEN        "leave_fullscreen"
757 %token                  TOK_FOR_WINDOW              "for_window"
758
759 %token                  TOK_BAR_OUTPUT              "output (bar)"
760 %token                  TOK_BAR_TRAY_OUTPUT         "tray_output"
761 %token                  TOK_BAR_SOCKET_PATH         "socket_path"
762 %token                  TOK_BAR_MODE                "mode (bar)"
763 %token                  TOK_BAR_HIDE                "hide"
764 %token                  TOK_BAR_DOCK                "dock"
765 %token                  TOK_BAR_MODIFIER            "modifier (bar)"
766 %token                  TOK_BAR_CONTROL             "shift (bar)"
767 %token                  TOK_BAR_SHIFT               "control (bar)"
768 %token                  TOK_BAR_MOD1                "Mod1"
769 %token                  TOK_BAR_MOD2                "Mod2"
770 %token                  TOK_BAR_MOD3                "Mod3"
771 %token                  TOK_BAR_MOD4                "Mod4"
772 %token                  TOK_BAR_MOD5                "Mod5"
773 %token                  TOK_BAR_POSITION            "position"
774 %token                  TOK_BAR_BOTTOM              "bottom"
775 %token                  TOK_BAR_TOP                 "top"
776 %token                  TOK_BAR_STATUS_COMMAND      "status_command"
777 %token                  TOK_BAR_I3BAR_COMMAND       "i3bar_command"
778 %token                  TOK_BAR_FONT                "font (bar)"
779 %token                  TOK_BAR_WORKSPACE_BUTTONS   "workspace_buttons"
780 %token                  TOK_BAR_VERBOSE             "verbose"
781 %token                  TOK_BAR_COLORS              "colors"
782 %token                  TOK_BAR_COLOR_BACKGROUND    "background"
783 %token                  TOK_BAR_COLOR_STATUSLINE    "statusline"
784 %token                  TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
785 %token                  TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
786 %token                  TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
787 %token                  TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
788 %token                  TOK_NO_STARTUP_ID           "--no-startup-id"
789 %token                  TOK_RELEASE                 "--release"
790
791 %token              TOK_MARK            "mark"
792 %token              TOK_CLASS           "class"
793 %token              TOK_INSTANCE        "instance"
794 %token              TOK_WINDOW_ROLE     "window_role"
795 %token              TOK_ID              "id"
796 %token              TOK_CON_ID          "con_id"
797 %token              TOK_TITLE           "title"
798 %token              TOK_URGENT          "urgent"
799
800 %type   <binding>       binding
801 %type   <binding>       bindcode
802 %type   <binding>       bindsym
803 %type   <number>        binding_modifiers
804 %type   <number>        binding_modifier
805 %type   <number>        direction
806 %type   <number>        layout_mode
807 %type   <number>        border_style
808 %type   <number>        new_window
809 %type   <number>        hide_edge_borders
810 %type   <number>        edge_hiding_mode
811 %type   <number>        new_float
812 %type   <number>        colorpixel
813 %type   <number>        bool
814 %type   <number>        popup_setting
815 %type   <number>        bar_position_position
816 %type   <number>        bar_mode_mode
817 %type   <number>        bar_modifier_modifier
818 %type   <number>        optional_no_startup_id
819 %type   <number>        optional_release
820 %type   <string>        command
821 %type   <string>        word_or_number
822 %type   <string>        qstring_or_number
823 %type   <string>        optional_workspace_name
824 %type   <string>        workspace_name
825 %type   <string>        window_class
826
827 %%
828
829 lines: /* empty */
830     | lines error
831     | lines line
832     ;
833
834 line:
835     bindline
836     | for_window
837     | mode
838     | bar
839     | floating_maximum_size
840     | floating_minimum_size
841     | floating_modifier
842     | orientation
843     | workspace_layout
844     | new_window
845     | new_float
846     | hide_edge_borders
847     | focus_follows_mouse
848     | force_focus_wrapping
849     | force_xinerama
850     | fake_outputs
851     | workspace_back_and_forth
852     | workspace_bar
853     | workspace
854     | assign
855     | ipcsocket
856     | restart_state
857     | exec
858     | exec_always
859     | single_color
860     | color
861     | terminal
862     | font
863     | comment
864     | popup_during_fullscreen
865     ;
866
867 comment:
868     TOKCOMMENT
869     ;
870
871 command:
872     STR
873     ;
874
875 bindline:
876     binding
877     {
878         TAILQ_INSERT_TAIL(bindings, $1, bindings);
879     }
880     ;
881
882 binding:
883     TOKBINDCODE bindcode         { $$ = $2; }
884     | TOKBINDSYM bindsym         { $$ = $2; }
885     ;
886
887 bindcode:
888     optional_release binding_modifiers NUMBER command
889     {
890         DLOG("bindcode: release = %d, mod = %d, key = %d, command = %s\n", $1, $2, $3, $4);
891         Binding *new = scalloc(sizeof(Binding));
892
893         new->release = $1;
894         new->keycode = $3;
895         new->mods = $2;
896         new->command = $4;
897
898         $$ = new;
899     }
900     ;
901
902 bindsym:
903     optional_release binding_modifiers word_or_number command
904     {
905         DLOG("bindsym: release = %d, mod = %d, key = %s, command = %s\n", $1, $2, $3, $4);
906         Binding *new = scalloc(sizeof(Binding));
907
908         new->release = $1;
909         new->symbol = $3;
910         new->mods = $2;
911         new->command = $4;
912
913         $$ = new;
914     }
915     ;
916
917 optional_release:
918     /* empty */ { $$ = B_UPON_KEYPRESS; }
919     | TOK_RELEASE  { $$ = B_UPON_KEYRELEASE; }
920     ;
921
922 for_window:
923     TOK_FOR_WINDOW match command
924     {
925         if (match_is_empty(&current_match)) {
926             ELOG("Match is empty, ignoring this for_window statement\n");
927             break;
928         }
929         printf("\t should execute command %s for the criteria mentioned above\n", $3);
930         Assignment *assignment = scalloc(sizeof(Assignment));
931         assignment->type = A_COMMAND;
932         assignment->match = current_match;
933         assignment->dest.command = $3;
934         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
935     }
936     ;
937
938 match:
939     | matchstart criteria matchend
940     {
941         printf("match parsed\n");
942     }
943     ;
944
945 matchstart:
946     '['
947     {
948         printf("start\n");
949         match_init(&current_match);
950     }
951     ;
952
953 matchend:
954     ']'
955     {
956         printf("match specification finished\n");
957     }
958     ;
959
960 criteria:
961     criteria criterion
962     | criterion
963     ;
964
965 criterion:
966     TOK_CLASS '=' STR
967     {
968         printf("criteria: class = %s\n", $3);
969         current_match.class = regex_new($3);
970         free($3);
971     }
972     | TOK_INSTANCE '=' STR
973     {
974         printf("criteria: instance = %s\n", $3);
975         current_match.instance = regex_new($3);
976         free($3);
977     }
978     | TOK_WINDOW_ROLE '=' STR
979     {
980         printf("criteria: window_role = %s\n", $3);
981         current_match.role = regex_new($3);
982         free($3);
983     }
984     | TOK_CON_ID '=' STR
985     {
986         printf("criteria: id = %s\n", $3);
987         char *end;
988         long parsed = strtol($3, &end, 10);
989         if (parsed == LONG_MIN ||
990             parsed == LONG_MAX ||
991             parsed < 0 ||
992             (end && *end != '\0')) {
993             ELOG("Could not parse con id \"%s\"\n", $3);
994         } else {
995             current_match.con_id = (Con*)parsed;
996             printf("id as int = %p\n", current_match.con_id);
997         }
998     }
999     | TOK_ID '=' STR
1000     {
1001         printf("criteria: window id = %s\n", $3);
1002         char *end;
1003         long parsed = strtol($3, &end, 10);
1004         if (parsed == LONG_MIN ||
1005             parsed == LONG_MAX ||
1006             parsed < 0 ||
1007             (end && *end != '\0')) {
1008             ELOG("Could not parse window id \"%s\"\n", $3);
1009         } else {
1010             current_match.id = parsed;
1011             printf("window id as int = %d\n", current_match.id);
1012         }
1013     }
1014     | TOK_MARK '=' STR
1015     {
1016         printf("criteria: mark = %s\n", $3);
1017         current_match.mark = regex_new($3);
1018         free($3);
1019     }
1020     | TOK_TITLE '=' STR
1021     {
1022         printf("criteria: title = %s\n", $3);
1023         current_match.title = regex_new($3);
1024         free($3);
1025     }
1026     | TOK_URGENT '=' STR
1027     {
1028         printf("criteria: urgent = %s\n", $3);
1029         if (strcasecmp($3, "latest") == 0 ||
1030             strcasecmp($3, "newest") == 0 ||
1031             strcasecmp($3, "recent") == 0 ||
1032             strcasecmp($3, "last") == 0) {
1033             current_match.urgent = U_LATEST;
1034         } else if (strcasecmp($3, "oldest") == 0 ||
1035                    strcasecmp($3, "first") == 0) {
1036             current_match.urgent = U_OLDEST;
1037         }
1038         free($3);
1039     }
1040     ;
1041
1042 qstring_or_number:
1043     QUOTEDSTRING
1044     | NUMBER { sasprintf(&$$, "%d", $1); }
1045     ;
1046
1047 word_or_number:
1048     WORD
1049     | NUMBER
1050     {
1051         sasprintf(&$$, "%d", $1);
1052     }
1053     ;
1054
1055 mode:
1056     TOKMODE QUOTEDSTRING '{' modelines '}'
1057     {
1058         if (strcasecmp($2, "default") == 0) {
1059             printf("You cannot use the name \"default\" for your mode\n");
1060             exit(1);
1061         }
1062         printf("\t now in mode %s\n", $2);
1063         printf("\t current bindings = %p\n", current_bindings);
1064         Binding *binding;
1065         TAILQ_FOREACH(binding, current_bindings, bindings) {
1066             printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
1067                             binding->mods, binding->keycode, binding->symbol, binding->command);
1068         }
1069
1070         struct Mode *mode = scalloc(sizeof(struct Mode));
1071         mode->name = $2;
1072         mode->bindings = current_bindings;
1073         current_bindings = NULL;
1074         SLIST_INSERT_HEAD(&modes, mode, modes);
1075     }
1076     ;
1077
1078
1079 modelines:
1080     /* empty */
1081     | modelines modeline
1082     ;
1083
1084 modeline:
1085     comment
1086     | binding
1087     {
1088         if (current_bindings == NULL) {
1089             current_bindings = scalloc(sizeof(struct bindings_head));
1090             TAILQ_INIT(current_bindings);
1091         }
1092
1093         TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
1094     }
1095     ;
1096
1097 bar:
1098     TOK_BAR '{' barlines '}'
1099     {
1100         printf("\t new bar configuration finished, saving.\n");
1101         /* Generate a unique ID for this bar */
1102         current_bar.id = sstrdup("bar-XXXXXX");
1103         /* This works similar to mktemp in that it replaces the last six X with
1104          * random letters, but without the restriction that the given buffer
1105          * has to contain a valid path name. */
1106         char *x = current_bar.id + strlen("bar-");
1107         while (*x != '\0') {
1108             *(x++) = (rand() % 26) + 'a';
1109         }
1110
1111         /* If no font was explicitly set, we use the i3 font as default */
1112         if (!current_bar.font && font_pattern)
1113             current_bar.font = sstrdup(font_pattern);
1114
1115         /* Copy the current (static) structure into a dynamically allocated
1116          * one, then cleanup our static one. */
1117         Barconfig *bar_config = scalloc(sizeof(Barconfig));
1118         memcpy(bar_config, &current_bar, sizeof(Barconfig));
1119         TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
1120
1121         memset(&current_bar, '\0', sizeof(Barconfig));
1122     }
1123     ;
1124
1125 barlines:
1126     /* empty */
1127     | barlines barline
1128     ;
1129
1130 barline:
1131     comment
1132     | bar_status_command
1133     | bar_i3bar_command
1134     | bar_output
1135     | bar_tray_output
1136     | bar_position
1137     | bar_mode
1138     | bar_modifier
1139     | bar_font
1140     | bar_workspace_buttons
1141     | bar_verbose
1142     | bar_socket_path
1143     | bar_colors
1144     | bar_color_background
1145     | bar_color_statusline
1146     | bar_color_focused_workspace
1147     | bar_color_active_workspace
1148     | bar_color_inactive_workspace
1149     | bar_color_urgent_workspace
1150     ;
1151
1152 bar_status_command:
1153     TOK_BAR_STATUS_COMMAND STR
1154     {
1155         DLOG("should add status command %s\n", $2);
1156         FREE(current_bar.status_command);
1157         current_bar.status_command = $2;
1158     }
1159     ;
1160
1161 bar_i3bar_command:
1162     TOK_BAR_I3BAR_COMMAND STR
1163     {
1164         DLOG("should add i3bar_command %s\n", $2);
1165         FREE(current_bar.i3bar_command);
1166         current_bar.i3bar_command = $2;
1167     }
1168     ;
1169
1170 bar_output:
1171     TOK_BAR_OUTPUT STR
1172     {
1173         DLOG("bar output %s\n", $2);
1174         int new_outputs = current_bar.num_outputs + 1;
1175         current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
1176         current_bar.outputs[current_bar.num_outputs] = $2;
1177         current_bar.num_outputs = new_outputs;
1178     }
1179     ;
1180
1181 bar_tray_output:
1182     TOK_BAR_TRAY_OUTPUT STR
1183     {
1184         DLOG("tray %s\n", $2);
1185         FREE(current_bar.tray_output);
1186         current_bar.tray_output = $2;
1187     }
1188     ;
1189
1190 bar_position:
1191     TOK_BAR_POSITION bar_position_position
1192     {
1193         DLOG("position %d\n", $2);
1194         current_bar.position = $2;
1195     }
1196     ;
1197
1198 bar_position_position:
1199     TOK_BAR_TOP      { $$ = P_TOP; }
1200     | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1201     ;
1202
1203 bar_mode:
1204     TOK_BAR_MODE bar_mode_mode
1205     {
1206         DLOG("mode %d\n", $2);
1207         current_bar.mode = $2;
1208     }
1209     ;
1210
1211 bar_mode_mode:
1212     TOK_BAR_HIDE   { $$ = M_HIDE; }
1213     | TOK_BAR_DOCK { $$ = M_DOCK; }
1214     ;
1215
1216 bar_modifier:
1217     TOK_BAR_MODIFIER bar_modifier_modifier
1218     {
1219         DLOG("modifier %d\n", $2);
1220         current_bar.modifier = $2;
1221     };
1222
1223 bar_modifier_modifier:
1224     TOK_BAR_CONTROL { $$ = M_CONTROL; }
1225     | TOK_BAR_SHIFT { $$ = M_SHIFT; }
1226     | TOK_BAR_MOD1  { $$ = M_MOD1; }
1227     | TOK_BAR_MOD2  { $$ = M_MOD2; }
1228     | TOK_BAR_MOD3  { $$ = M_MOD3; }
1229     | TOK_BAR_MOD4  { $$ = M_MOD4; }
1230     | TOK_BAR_MOD5  { $$ = M_MOD5; }
1231     ;
1232
1233 bar_font:
1234     TOK_BAR_FONT STR
1235     {
1236         DLOG("font %s\n", $2);
1237         FREE(current_bar.font);
1238         current_bar.font = $2;
1239     }
1240     ;
1241
1242 bar_workspace_buttons:
1243     TOK_BAR_WORKSPACE_BUTTONS bool
1244     {
1245         DLOG("workspace_buttons = %d\n", $2);
1246         /* We store this inverted to make the default setting right when
1247          * initializing the struct with zero. */
1248         current_bar.hide_workspace_buttons = !($2);
1249     }
1250     ;
1251
1252 bar_verbose:
1253     TOK_BAR_VERBOSE bool
1254     {
1255         DLOG("verbose = %d\n", $2);
1256         current_bar.verbose = $2;
1257     }
1258     ;
1259
1260 bar_socket_path:
1261     TOK_BAR_SOCKET_PATH STR
1262     {
1263         DLOG("socket_path = %s\n", $2);
1264         FREE(current_bar.socket_path);
1265         current_bar.socket_path = $2;
1266     }
1267     ;
1268
1269 bar_colors:
1270     TOK_BAR_COLORS '{' barlines '}'
1271     {
1272         /* At the moment, the TOK_BAR_COLORS token is only to make the config
1273          * friendlier for humans. We might change this in the future if it gets
1274          * more complex. */
1275     }
1276     ;
1277
1278 bar_color_background:
1279     TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1280     {
1281         DLOG("background = %s\n", $2);
1282         current_bar.colors.background = $2;
1283     }
1284     ;
1285
1286 bar_color_statusline:
1287     TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1288     {
1289         DLOG("statusline = %s\n", $2);
1290         current_bar.colors.statusline = $2;
1291     }
1292     ;
1293
1294 bar_color_focused_workspace:
1295     TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1296     {
1297         /* Old syntax: text / background */
1298         DLOG("focused_ws = %s, %s (old)\n", $2, $3);
1299         current_bar.colors.focused_workspace_bg = $3;
1300         current_bar.colors.focused_workspace_text = $2;
1301     }
1302     | TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1303     {
1304         /* New syntax: border / background / text */
1305         DLOG("focused_ws = %s, %s and %s\n", $2, $3, $4);
1306         current_bar.colors.focused_workspace_border = $2;
1307         current_bar.colors.focused_workspace_bg = $3;
1308         current_bar.colors.focused_workspace_text = $4;
1309     }
1310     ;
1311
1312 bar_color_active_workspace:
1313     TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1314     {
1315         /* Old syntax: text / background */
1316         DLOG("active_ws = %s, %s (old)\n", $2, $3);
1317         current_bar.colors.active_workspace_bg = $3;
1318         current_bar.colors.active_workspace_text = $2;
1319     }
1320     | TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1321     {
1322         /* New syntax: border / background / text */
1323         DLOG("active_ws = %s, %s and %s\n", $2, $3, $4);
1324         current_bar.colors.active_workspace_border = $2;
1325         current_bar.colors.active_workspace_bg = $3;
1326         current_bar.colors.active_workspace_text = $4;
1327     }
1328     ;
1329
1330 bar_color_inactive_workspace:
1331     TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1332     {
1333         /* Old syntax: text / background */
1334         DLOG("inactive_ws = %s, %s (old)\n", $2, $3);
1335         current_bar.colors.inactive_workspace_bg = $3;
1336         current_bar.colors.inactive_workspace_text = $2;
1337     }
1338     | TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1339     {
1340         DLOG("inactive_ws = %s, %s and %s\n", $2, $3, $4);
1341         current_bar.colors.inactive_workspace_border = $2;
1342         current_bar.colors.inactive_workspace_bg = $3;
1343         current_bar.colors.inactive_workspace_text = $4;
1344     }
1345     ;
1346
1347 bar_color_urgent_workspace:
1348     TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1349     {
1350         /* Old syntax: text / background */
1351         DLOG("urgent_ws = %s, %s (old)\n", $2, $3);
1352         current_bar.colors.urgent_workspace_bg = $3;
1353         current_bar.colors.urgent_workspace_text = $2;
1354     }
1355     | TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1356     {
1357         DLOG("urgent_ws = %s, %s and %s\n", $2, $3, $4);
1358         current_bar.colors.urgent_workspace_border = $2;
1359         current_bar.colors.urgent_workspace_bg = $3;
1360         current_bar.colors.urgent_workspace_text = $4;
1361     }
1362     ;
1363
1364 floating_maximum_size:
1365     TOKFLOATING_MAXIMUM_SIZE NUMBER WORD NUMBER
1366     {
1367         printf("floating_maximum_width = %d\n", $2);
1368         printf("floating_maximum_height = %d\n", $4);
1369         config.floating_maximum_width = $2;
1370         config.floating_maximum_height = $4;
1371     }
1372     ;
1373
1374 floating_minimum_size:
1375     TOKFLOATING_MINIMUM_SIZE NUMBER WORD NUMBER
1376     {
1377         printf("floating_minimum_width = %d\n", $2);
1378         printf("floating_minimum_height = %d\n", $4);
1379         config.floating_minimum_width = $2;
1380         config.floating_minimum_height = $4;
1381     }
1382     ;
1383
1384 floating_modifier:
1385     TOKFLOATING_MODIFIER binding_modifiers
1386     {
1387         DLOG("floating modifier = %d\n", $2);
1388         config.floating_modifier = $2;
1389     }
1390     ;
1391
1392 orientation:
1393     TOK_ORIENTATION direction
1394     {
1395         DLOG("New containers should start with split direction %d\n", $2);
1396         config.default_orientation = $2;
1397     }
1398     ;
1399
1400 direction:
1401     TOK_HORIZ       { $$ = HORIZ; }
1402     | TOK_VERT      { $$ = VERT; }
1403     | TOK_AUTO      { $$ = NO_ORIENTATION; }
1404     ;
1405
1406 workspace_layout:
1407     TOK_WORKSPACE_LAYOUT layout_mode
1408     {
1409         DLOG("new containers will be in mode %d\n", $2);
1410         config.default_layout = $2;
1411
1412 #if 0
1413         /* We also need to change the layout of the already existing
1414          * workspaces here. Workspaces may exist at this point because
1415          * of the other directives which are modifying workspaces
1416          * (setting the preferred screen or name). While the workspace
1417          * objects are already created, they have never been used.
1418          * Thus, the user very likely awaits the default container mode
1419          * to trigger in this case, regardless of where it is inside
1420          * his configuration file. */
1421         Workspace *ws;
1422         TAILQ_FOREACH(ws, workspaces, workspaces) {
1423                 if (ws->table == NULL)
1424                         continue;
1425                 switch_layout_mode(global_conn,
1426                                    ws->table[0][0],
1427                                    config.container_mode);
1428         }
1429 #endif
1430     }
1431     | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1432     {
1433         DLOG("stack-limit %d with val %d\n", $3, $4);
1434         config.container_stack_limit = $3;
1435         config.container_stack_limit_value = $4;
1436
1437 #if 0
1438         /* See the comment above */
1439         Workspace *ws;
1440         TAILQ_FOREACH(ws, workspaces, workspaces) {
1441                 if (ws->table == NULL)
1442                         continue;
1443                 Container *con = ws->table[0][0];
1444                 con->stack_limit = config.container_stack_limit;
1445                 con->stack_limit_value = config.container_stack_limit_value;
1446         }
1447 #endif
1448     }
1449     ;
1450
1451 layout_mode:
1452     TOK_DEFAULT       { $$ = L_DEFAULT; }
1453     | TOK_STACKING    { $$ = L_STACKED; }
1454     | TOK_TABBED      { $$ = L_TABBED; }
1455     ;
1456
1457 new_window:
1458     TOKNEWWINDOW border_style
1459     {
1460         DLOG("new windows should start with border style %d\n", $2);
1461         config.default_border = $2;
1462     }
1463     ;
1464
1465 new_float:
1466     TOKNEWFLOAT border_style
1467     {
1468        DLOG("new floating windows should start with border style %d\n", $2);
1469        config.default_floating_border = $2;
1470     }
1471     ;
1472
1473 border_style:
1474     TOK_NORMAL      { $$ = BS_NORMAL; }
1475     | TOK_NONE      { $$ = BS_NONE; }
1476     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
1477     ;
1478
1479 bool:
1480     NUMBER
1481     {
1482         $$ = ($1 == 1);
1483     }
1484     | WORD
1485     {
1486         DLOG("checking word \"%s\"\n", $1);
1487         $$ = (strcasecmp($1, "yes") == 0 ||
1488               strcasecmp($1, "true") == 0 ||
1489               strcasecmp($1, "on") == 0 ||
1490               strcasecmp($1, "enable") == 0 ||
1491               strcasecmp($1, "active") == 0);
1492     }
1493     ;
1494
1495 hide_edge_borders:
1496     TOK_HIDE_EDGE_BORDERS edge_hiding_mode
1497     {
1498         DLOG("hide edge borders = %d\n", $2);
1499         config.hide_edge_borders = $2;
1500     }
1501     ;
1502
1503 edge_hiding_mode:
1504     TOK_NONE        { $$ = ADJ_NONE; }
1505     | TOK_VERT      { $$ = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE; }
1506     | TOK_HORIZ     { $$ = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE; }
1507     | TOK_BOTH      { $$ = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE; }
1508     | bool          { $$ = ($1 ? ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE : ADJ_NONE); }
1509     ;
1510
1511 focus_follows_mouse:
1512     TOKFOCUSFOLLOWSMOUSE bool
1513     {
1514         DLOG("focus follows mouse = %d\n", $2);
1515         config.disable_focus_follows_mouse = !($2);
1516     }
1517     ;
1518
1519 force_focus_wrapping:
1520     TOK_FORCE_FOCUS_WRAPPING bool
1521     {
1522         DLOG("force focus wrapping = %d\n", $2);
1523         config.force_focus_wrapping = $2;
1524     }
1525     ;
1526
1527 force_xinerama:
1528     TOK_FORCE_XINERAMA bool
1529     {
1530         DLOG("force xinerama = %d\n", $2);
1531         config.force_xinerama = $2;
1532     }
1533     ;
1534
1535 fake_outputs:
1536     TOK_FAKE_OUTPUTS STR
1537     {
1538         DLOG("fake outputs = %s\n", $2);
1539         config.fake_outputs = $2;
1540     }
1541     ;
1542
1543 workspace_back_and_forth:
1544     TOK_WORKSPACE_AUTO_BAF bool
1545     {
1546         DLOG("automatic workspace back-and-forth = %d\n", $2);
1547         config.workspace_auto_back_and_forth = $2;
1548     }
1549     ;
1550
1551 workspace_bar:
1552     TOKWORKSPACEBAR bool
1553     {
1554         DLOG("workspace bar = %d\n", $2);
1555         config.disable_workspace_bar = !($2);
1556     }
1557     ;
1558
1559 workspace:
1560     TOKWORKSPACE qstring_or_number TOKOUTPUT OUTPUT optional_workspace_name
1561     {
1562         char *ws_name = $2;
1563
1564         if ($5 != NULL) {
1565             ELOG("The old (v3) syntax workspace <number> output <output> <name> is deprecated.\n");
1566             ELOG("Please use the new syntax: workspace \"<workspace>\" output <output>\n");
1567             ELOG("In your case, the following should work:\n");
1568             ELOG("    workspace \"%s\" output %s\n", $5, $4);
1569             ws_name = $5;
1570             context->has_warnings = true;
1571         }
1572
1573         DLOG("Assigning workspace \"%s\" to output \"%s\"\n", ws_name, $4);
1574         /* Check for earlier assignments of the same workspace so that we
1575          * don’t have assignments of a single workspace to different
1576          * outputs */
1577         struct Workspace_Assignment *assignment;
1578         bool duplicate = false;
1579         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1580             if (strcasecmp(assignment->name, ws_name) == 0) {
1581                 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1582                      ws_name);
1583                 assignment->output = $4;
1584                 duplicate = true;
1585             }
1586         }
1587         if (!duplicate) {
1588             assignment = scalloc(sizeof(struct Workspace_Assignment));
1589             assignment->name = ws_name;
1590             assignment->output = $4;
1591             TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1592         }
1593     }
1594     | TOKWORKSPACE NUMBER workspace_name
1595     {
1596         int ws_num = $2;
1597         if (ws_num < 1) {
1598             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1599         } else {
1600             DLOG("workspace name to: %s\n", $3);
1601 #if 0
1602             if ($<string>3 != NULL) {
1603                     workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1604                     free($<string>3);
1605             }
1606 #endif
1607         }
1608     }
1609     ;
1610
1611 optional_workspace_name:
1612     /* empty */          { $$ = NULL; }
1613     | workspace_name     { $$ = $1; }
1614     ;
1615
1616 workspace_name:
1617     QUOTEDSTRING         { $$ = $1; }
1618     | STR                { $$ = $1; }
1619     | WORD               { $$ = $1; }
1620     ;
1621
1622 assign:
1623     TOKASSIGN window_class STR
1624     {
1625         /* This is the old, deprecated form of assignments. It’s provided for
1626          * compatibility in version (4.1, 4.2, 4.3) and will be removed
1627          * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1628         ELOG("You are using the old assign syntax (without criteria). "
1629              "Please see the User's Guide for the new syntax and fix "
1630              "your config file.\n");
1631         context->has_warnings = true;
1632         printf("assignment of %s to *%s*\n", $2, $3);
1633         char *workspace = $3;
1634         char *criteria = $2;
1635
1636         Assignment *assignment = scalloc(sizeof(Assignment));
1637         Match *match = &(assignment->match);
1638         match_init(match);
1639
1640         char *separator = NULL;
1641         if ((separator = strchr(criteria, '/')) != NULL) {
1642             *(separator++) = '\0';
1643             char *pattern;
1644             sasprintf(&pattern, "(?i)%s", separator);
1645             match->title = regex_new(pattern);
1646             free(pattern);
1647             printf("  title = %s\n", separator);
1648         }
1649         if (*criteria != '\0') {
1650             char *pattern;
1651             sasprintf(&pattern, "(?i)%s", criteria);
1652             match->class = regex_new(pattern);
1653             free(pattern);
1654             printf("  class = %s\n", criteria);
1655         }
1656         free(criteria);
1657
1658         /* Compatibility with older versions: If the assignment target starts
1659          * with ~, we create the equivalent of:
1660          *
1661          * for_window [class="foo"] floating enable
1662          */
1663         if (*workspace == '~') {
1664             workspace++;
1665             if (*workspace == '\0') {
1666                 /* This assignment was *only* for floating */
1667                 assignment->type = A_COMMAND;
1668                 assignment->dest.command = sstrdup("floating enable");
1669                 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1670                 break;
1671             } else {
1672                 /* Create a new assignment and continue afterwards */
1673                 Assignment *floating = scalloc(sizeof(Assignment));
1674                 match_copy(&(floating->match), match);
1675                 floating->type = A_COMMAND;
1676                 floating->dest.command = sstrdup("floating enable");
1677                 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1678             }
1679         }
1680
1681         assignment->type = A_TO_WORKSPACE;
1682         assignment->dest.workspace = workspace;
1683         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1684     }
1685     | TOKASSIGN match STR
1686     {
1687         if (match_is_empty(&current_match)) {
1688             ELOG("Match is empty, ignoring this assignment\n");
1689             break;
1690         }
1691         printf("new assignment, using above criteria, to workspace %s\n", $3);
1692         Assignment *assignment = scalloc(sizeof(Assignment));
1693         assignment->match = current_match;
1694         assignment->type = A_TO_WORKSPACE;
1695         assignment->dest.workspace = $3;
1696         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1697     }
1698     ;
1699
1700 window_class:
1701     QUOTEDSTRING
1702     | STR_NG
1703     ;
1704
1705 ipcsocket:
1706     TOKIPCSOCKET STR
1707     {
1708         config.ipc_socket_path = $2;
1709     }
1710     ;
1711
1712 restart_state:
1713     TOKRESTARTSTATE STR
1714     {
1715         config.restart_state_path = $2;
1716     }
1717     ;
1718
1719 exec:
1720     TOKEXEC optional_no_startup_id STR
1721     {
1722         struct Autostart *new = smalloc(sizeof(struct Autostart));
1723         new->command = $3;
1724         new->no_startup_id = $2;
1725         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1726     }
1727     ;
1728
1729 exec_always:
1730     TOKEXEC_ALWAYS optional_no_startup_id STR
1731     {
1732         struct Autostart *new = smalloc(sizeof(struct Autostart));
1733         new->command = $3;
1734         new->no_startup_id = $2;
1735         TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1736     }
1737     ;
1738
1739 optional_no_startup_id:
1740     /* empty */ { $$ = false; }
1741     | TOK_NO_STARTUP_ID  { $$ = true; }
1742     ;
1743
1744 terminal:
1745     TOKTERMINAL STR
1746     {
1747         ELOG("The terminal option is DEPRECATED and has no effect. "
1748             "Please remove it from your configuration file.\n");
1749     }
1750     ;
1751
1752 font:
1753     TOKFONT STR
1754     {
1755         config.font = load_font($2, true);
1756         set_font(&config.font);
1757         printf("font %s\n", $2);
1758         FREE(font_pattern);
1759         font_pattern = $2;
1760     }
1761     ;
1762
1763 single_color:
1764     TOKSINGLECOLOR colorpixel
1765     {
1766         uint32_t *dest = $1;
1767         *dest = $2;
1768     }
1769     ;
1770
1771 color:
1772     TOKCOLOR colorpixel colorpixel colorpixel
1773     {
1774         struct Colortriple *dest = $1;
1775
1776         dest->border = $2;
1777         dest->background = $3;
1778         dest->text = $4;
1779     }
1780     | TOKCOLOR colorpixel colorpixel colorpixel colorpixel
1781     {
1782         struct Colortriple *dest = $1;
1783
1784         dest->border = $2;
1785         dest->background = $3;
1786         dest->text = $4;
1787         dest->indicator = $5;
1788     }
1789     ;
1790
1791 colorpixel:
1792     HEXCOLOR
1793     {
1794         $$ = get_colorpixel($1);
1795         free($1);
1796     }
1797     ;
1798
1799
1800 binding_modifiers:
1801     /* NULL */                               { $$ = 0; }
1802     | binding_modifier
1803     | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1804     | binding_modifiers '+'                  { $$ = $1; }
1805     ;
1806
1807 binding_modifier:
1808     MODIFIER        { $$ = $1; }
1809     | TOKCONTROL    { $$ = BIND_CONTROL; }
1810     | TOKSHIFT      { $$ = BIND_SHIFT; }
1811     ;
1812
1813 popup_during_fullscreen:
1814     TOK_POPUP_DURING_FULLSCREEN popup_setting
1815     {
1816         DLOG("popup_during_fullscreen setting: %d\n", $2);
1817         config.popup_during_fullscreen = $2;
1818     }
1819     ;
1820
1821 popup_setting:
1822     TOK_IGNORE              { $$ = PDF_IGNORE; }
1823     | TOK_LEAVE_FULLSCREEN  { $$ = PDF_LEAVE_FULLSCREEN; }
1824     ;