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