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