]> 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 #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                  TOK_HIDE_EDGE_BORDERS       "hide_edge_borders"
739 %token                  TOK_BOTH                    "both"
740 %token                  TOKFOCUSFOLLOWSMOUSE        "focus_follows_mouse"
741 %token                  TOK_FORCE_FOCUS_WRAPPING    "force_focus_wrapping"
742 %token                  TOK_FORCE_XINERAMA          "force_xinerama"
743 %token                  TOK_FAKE_OUTPUTS            "fake_outputs"
744 %token                  TOK_WORKSPACE_AUTO_BAF      "workspace_auto_back_and_forth"
745 %token                  TOKWORKSPACEBAR             "workspace_bar"
746 %token                  TOK_DEFAULT                 "default"
747 %token                  TOK_STACKING                "stacking"
748 %token                  TOK_TABBED                  "tabbed"
749 %token  <number>        TOKSTACKLIMIT               "stack-limit"
750 %token                  TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
751 %token                  TOK_IGNORE                  "ignore"
752 %token                  TOK_LEAVE_FULLSCREEN        "leave_fullscreen"
753 %token                  TOK_FOR_WINDOW              "for_window"
754
755 %token                  TOK_BAR_OUTPUT              "output (bar)"
756 %token                  TOK_BAR_TRAY_OUTPUT         "tray_output"
757 %token                  TOK_BAR_SOCKET_PATH         "socket_path"
758 %token                  TOK_BAR_MODE                "mode (bar)"
759 %token                  TOK_BAR_HIDE                "hide"
760 %token                  TOK_BAR_DOCK                "dock"
761 %token                  TOK_BAR_MODIFIER            "modifier (bar)"
762 %token                  TOK_BAR_CONTROL             "shift (bar)"
763 %token                  TOK_BAR_SHIFT               "control (bar)"
764 %token                  TOK_BAR_MOD1                "Mod1"
765 %token                  TOK_BAR_MOD2                "Mod2"
766 %token                  TOK_BAR_MOD3                "Mod3"
767 %token                  TOK_BAR_MOD4                "Mod4"
768 %token                  TOK_BAR_MOD5                "Mod5"
769 %token                  TOK_BAR_POSITION            "position"
770 %token                  TOK_BAR_BOTTOM              "bottom"
771 %token                  TOK_BAR_TOP                 "top"
772 %token                  TOK_BAR_STATUS_COMMAND      "status_command"
773 %token                  TOK_BAR_I3BAR_COMMAND       "i3bar_command"
774 %token                  TOK_BAR_FONT                "font (bar)"
775 %token                  TOK_BAR_WORKSPACE_BUTTONS   "workspace_buttons"
776 %token                  TOK_BAR_VERBOSE             "verbose"
777 %token                  TOK_BAR_COLORS              "colors"
778 %token                  TOK_BAR_COLOR_BACKGROUND    "background"
779 %token                  TOK_BAR_COLOR_STATUSLINE    "statusline"
780 %token                  TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
781 %token                  TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
782 %token                  TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
783 %token                  TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
784 %token                  TOK_NO_STARTUP_ID           "--no-startup-id"
785
786 %token              TOK_MARK            "mark"
787 %token              TOK_CLASS           "class"
788 %token              TOK_INSTANCE        "instance"
789 %token              TOK_WINDOW_ROLE     "window_role"
790 %token              TOK_ID              "id"
791 %token              TOK_CON_ID          "con_id"
792 %token              TOK_TITLE           "title"
793 %token              TOK_URGENT          "urgent"
794
795 %type   <binding>       binding
796 %type   <binding>       bindcode
797 %type   <binding>       bindsym
798 %type   <number>        binding_modifiers
799 %type   <number>        binding_modifier
800 %type   <number>        direction
801 %type   <number>        layout_mode
802 %type   <number>        border_style
803 %type   <number>        new_window
804 %type   <number>        hide_edge_borders
805 %type   <number>        edge_hiding_mode
806 %type   <number>        new_float
807 %type   <number>        colorpixel
808 %type   <number>        bool
809 %type   <number>        popup_setting
810 %type   <number>        bar_position_position
811 %type   <number>        bar_mode_mode
812 %type   <number>        bar_modifier_modifier
813 %type   <number>        optional_no_startup_id
814 %type   <string>        command
815 %type   <string>        word_or_number
816 %type   <string>        qstring_or_number
817 %type   <string>        optional_workspace_name
818 %type   <string>        workspace_name
819 %type   <string>        window_class
820
821 %%
822
823 lines: /* empty */
824     | lines error
825     | lines line
826     ;
827
828 line:
829     bindline
830     | for_window
831     | mode
832     | bar
833     | floating_maximum_size
834     | floating_minimum_size
835     | floating_modifier
836     | orientation
837     | workspace_layout
838     | new_window
839     | new_float
840     | hide_edge_borders
841     | focus_follows_mouse
842     | force_focus_wrapping
843     | force_xinerama
844     | fake_outputs
845     | workspace_back_and_forth
846     | workspace_bar
847     | workspace
848     | assign
849     | ipcsocket
850     | restart_state
851     | exec
852     | exec_always
853     | single_color
854     | color
855     | terminal
856     | font
857     | comment
858     | popup_during_fullscreen
859     ;
860
861 comment:
862     TOKCOMMENT
863     ;
864
865 command:
866     STR
867     ;
868
869 bindline:
870     binding
871     {
872         TAILQ_INSERT_TAIL(bindings, $1, bindings);
873     }
874     ;
875
876 binding:
877     TOKBINDCODE bindcode         { $$ = $2; }
878     | TOKBINDSYM bindsym         { $$ = $2; }
879     ;
880
881 bindcode:
882     binding_modifiers NUMBER command
883     {
884         printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
885         Binding *new = scalloc(sizeof(Binding));
886
887         new->keycode = $2;
888         new->mods = $1;
889         new->command = $3;
890
891         $$ = new;
892     }
893     ;
894
895 bindsym:
896     binding_modifiers word_or_number command
897     {
898         printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
899         Binding *new = scalloc(sizeof(Binding));
900
901         new->symbol = $2;
902         new->mods = $1;
903         new->command = $3;
904
905         $$ = new;
906     }
907     ;
908
909 for_window:
910     TOK_FOR_WINDOW match command
911     {
912         if (match_is_empty(&current_match)) {
913             ELOG("Match is empty, ignoring this for_window statement\n");
914             break;
915         }
916         printf("\t should execute command %s for the criteria mentioned above\n", $3);
917         Assignment *assignment = scalloc(sizeof(Assignment));
918         assignment->type = A_COMMAND;
919         assignment->match = current_match;
920         assignment->dest.command = $3;
921         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
922     }
923     ;
924
925 match:
926     | matchstart criteria matchend
927     {
928         printf("match parsed\n");
929     }
930     ;
931
932 matchstart:
933     '['
934     {
935         printf("start\n");
936         match_init(&current_match);
937     }
938     ;
939
940 matchend:
941     ']'
942     {
943         printf("match specification finished\n");
944     }
945     ;
946
947 criteria:
948     criteria criterion
949     | criterion
950     ;
951
952 criterion:
953     TOK_CLASS '=' STR
954     {
955         printf("criteria: class = %s\n", $3);
956         current_match.class = regex_new($3);
957         free($3);
958     }
959     | TOK_INSTANCE '=' STR
960     {
961         printf("criteria: instance = %s\n", $3);
962         current_match.instance = regex_new($3);
963         free($3);
964     }
965     | TOK_WINDOW_ROLE '=' STR
966     {
967         printf("criteria: window_role = %s\n", $3);
968         current_match.role = regex_new($3);
969         free($3);
970     }
971     | TOK_CON_ID '=' STR
972     {
973         printf("criteria: id = %s\n", $3);
974         char *end;
975         long parsed = strtol($3, &end, 10);
976         if (parsed == LONG_MIN ||
977             parsed == LONG_MAX ||
978             parsed < 0 ||
979             (end && *end != '\0')) {
980             ELOG("Could not parse con id \"%s\"\n", $3);
981         } else {
982             current_match.con_id = (Con*)parsed;
983             printf("id as int = %p\n", current_match.con_id);
984         }
985     }
986     | TOK_ID '=' STR
987     {
988         printf("criteria: window id = %s\n", $3);
989         char *end;
990         long parsed = strtol($3, &end, 10);
991         if (parsed == LONG_MIN ||
992             parsed == LONG_MAX ||
993             parsed < 0 ||
994             (end && *end != '\0')) {
995             ELOG("Could not parse window id \"%s\"\n", $3);
996         } else {
997             current_match.id = parsed;
998             printf("window id as int = %d\n", current_match.id);
999         }
1000     }
1001     | TOK_MARK '=' STR
1002     {
1003         printf("criteria: mark = %s\n", $3);
1004         current_match.mark = regex_new($3);
1005         free($3);
1006     }
1007     | TOK_TITLE '=' STR
1008     {
1009         printf("criteria: title = %s\n", $3);
1010         current_match.title = regex_new($3);
1011         free($3);
1012     }
1013     | TOK_URGENT '=' STR
1014     {
1015         printf("criteria: urgent = %s\n", $3);
1016         if (strcasecmp($3, "latest") == 0 ||
1017             strcasecmp($3, "newest") == 0 ||
1018             strcasecmp($3, "recent") == 0 ||
1019             strcasecmp($3, "last") == 0) {
1020             current_match.urgent = U_LATEST;
1021         } else if (strcasecmp($3, "oldest") == 0 ||
1022                    strcasecmp($3, "first") == 0) {
1023             current_match.urgent = U_OLDEST;
1024         }
1025         free($3);
1026     }
1027     ;
1028
1029 qstring_or_number:
1030     QUOTEDSTRING
1031     | NUMBER { sasprintf(&$$, "%d", $1); }
1032     ;
1033
1034 word_or_number:
1035     WORD
1036     | NUMBER
1037     {
1038         sasprintf(&$$, "%d", $1);
1039     }
1040     ;
1041
1042 mode:
1043     TOKMODE QUOTEDSTRING '{' modelines '}'
1044     {
1045         if (strcasecmp($2, "default") == 0) {
1046             printf("You cannot use the name \"default\" for your mode\n");
1047             exit(1);
1048         }
1049         printf("\t now in mode %s\n", $2);
1050         printf("\t current bindings = %p\n", current_bindings);
1051         Binding *binding;
1052         TAILQ_FOREACH(binding, current_bindings, bindings) {
1053             printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
1054                             binding->mods, binding->keycode, binding->symbol, binding->command);
1055         }
1056
1057         struct Mode *mode = scalloc(sizeof(struct Mode));
1058         mode->name = $2;
1059         mode->bindings = current_bindings;
1060         current_bindings = NULL;
1061         SLIST_INSERT_HEAD(&modes, mode, modes);
1062     }
1063     ;
1064
1065
1066 modelines:
1067     /* empty */
1068     | modelines modeline
1069     ;
1070
1071 modeline:
1072     comment
1073     | binding
1074     {
1075         if (current_bindings == NULL) {
1076             current_bindings = scalloc(sizeof(struct bindings_head));
1077             TAILQ_INIT(current_bindings);
1078         }
1079
1080         TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
1081     }
1082     ;
1083
1084 bar:
1085     TOK_BAR '{' barlines '}'
1086     {
1087         printf("\t new bar configuration finished, saving.\n");
1088         /* Generate a unique ID for this bar */
1089         current_bar.id = sstrdup("bar-XXXXXX");
1090         /* This works similar to mktemp in that it replaces the last six X with
1091          * random letters, but without the restriction that the given buffer
1092          * has to contain a valid path name. */
1093         char *x = current_bar.id + strlen("bar-");
1094         while (*x != '\0') {
1095             *(x++) = (rand() % 26) + 'a';
1096         }
1097
1098         /* If no font was explicitly set, we use the i3 font as default */
1099         if (!current_bar.font && font_pattern)
1100             current_bar.font = sstrdup(font_pattern);
1101
1102         /* Copy the current (static) structure into a dynamically allocated
1103          * one, then cleanup our static one. */
1104         Barconfig *bar_config = scalloc(sizeof(Barconfig));
1105         memcpy(bar_config, &current_bar, sizeof(Barconfig));
1106         TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
1107
1108         memset(&current_bar, '\0', sizeof(Barconfig));
1109     }
1110     ;
1111
1112 barlines:
1113     /* empty */
1114     | barlines barline
1115     ;
1116
1117 barline:
1118     comment
1119     | bar_status_command
1120     | bar_i3bar_command
1121     | bar_output
1122     | bar_tray_output
1123     | bar_position
1124     | bar_mode
1125     | bar_modifier
1126     | bar_font
1127     | bar_workspace_buttons
1128     | bar_verbose
1129     | bar_socket_path
1130     | bar_colors
1131     | bar_color_background
1132     | bar_color_statusline
1133     | bar_color_focused_workspace
1134     | bar_color_active_workspace
1135     | bar_color_inactive_workspace
1136     | bar_color_urgent_workspace
1137     ;
1138
1139 bar_status_command:
1140     TOK_BAR_STATUS_COMMAND STR
1141     {
1142         DLOG("should add status command %s\n", $2);
1143         FREE(current_bar.status_command);
1144         current_bar.status_command = $2;
1145     }
1146     ;
1147
1148 bar_i3bar_command:
1149     TOK_BAR_I3BAR_COMMAND STR
1150     {
1151         DLOG("should add i3bar_command %s\n", $2);
1152         FREE(current_bar.i3bar_command);
1153         current_bar.i3bar_command = $2;
1154     }
1155     ;
1156
1157 bar_output:
1158     TOK_BAR_OUTPUT STR
1159     {
1160         DLOG("bar output %s\n", $2);
1161         int new_outputs = current_bar.num_outputs + 1;
1162         current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
1163         current_bar.outputs[current_bar.num_outputs] = $2;
1164         current_bar.num_outputs = new_outputs;
1165     }
1166     ;
1167
1168 bar_tray_output:
1169     TOK_BAR_TRAY_OUTPUT STR
1170     {
1171         DLOG("tray %s\n", $2);
1172         FREE(current_bar.tray_output);
1173         current_bar.tray_output = $2;
1174     }
1175     ;
1176
1177 bar_position:
1178     TOK_BAR_POSITION bar_position_position
1179     {
1180         DLOG("position %d\n", $2);
1181         current_bar.position = $2;
1182     }
1183     ;
1184
1185 bar_position_position:
1186     TOK_BAR_TOP      { $$ = P_TOP; }
1187     | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1188     ;
1189
1190 bar_mode:
1191     TOK_BAR_MODE bar_mode_mode
1192     {
1193         DLOG("mode %d\n", $2);
1194         current_bar.mode = $2;
1195     }
1196     ;
1197
1198 bar_mode_mode:
1199     TOK_BAR_HIDE   { $$ = M_HIDE; }
1200     | TOK_BAR_DOCK { $$ = M_DOCK; }
1201     ;
1202
1203 bar_modifier:
1204     TOK_BAR_MODIFIER bar_modifier_modifier
1205     {
1206         DLOG("modifier %d\n", $2);
1207         current_bar.modifier = $2;
1208     };
1209
1210 bar_modifier_modifier:
1211     TOK_BAR_CONTROL { $$ = M_CONTROL; }
1212     | TOK_BAR_SHIFT { $$ = M_SHIFT; }
1213     | TOK_BAR_MOD1  { $$ = M_MOD1; }
1214     | TOK_BAR_MOD2  { $$ = M_MOD2; }
1215     | TOK_BAR_MOD3  { $$ = M_MOD3; }
1216     | TOK_BAR_MOD4  { $$ = M_MOD4; }
1217     | TOK_BAR_MOD5  { $$ = M_MOD5; }
1218     ;
1219
1220 bar_font:
1221     TOK_BAR_FONT STR
1222     {
1223         DLOG("font %s\n", $2);
1224         FREE(current_bar.font);
1225         current_bar.font = $2;
1226     }
1227     ;
1228
1229 bar_workspace_buttons:
1230     TOK_BAR_WORKSPACE_BUTTONS bool
1231     {
1232         DLOG("workspace_buttons = %d\n", $2);
1233         /* We store this inverted to make the default setting right when
1234          * initializing the struct with zero. */
1235         current_bar.hide_workspace_buttons = !($2);
1236     }
1237     ;
1238
1239 bar_verbose:
1240     TOK_BAR_VERBOSE bool
1241     {
1242         DLOG("verbose = %d\n", $2);
1243         current_bar.verbose = $2;
1244     }
1245     ;
1246
1247 bar_socket_path:
1248     TOK_BAR_SOCKET_PATH STR
1249     {
1250         DLOG("socket_path = %s\n", $2);
1251         FREE(current_bar.socket_path);
1252         current_bar.socket_path = $2;
1253     }
1254     ;
1255
1256 bar_colors:
1257     TOK_BAR_COLORS '{' barlines '}'
1258     {
1259         /* At the moment, the TOK_BAR_COLORS token is only to make the config
1260          * friendlier for humans. We might change this in the future if it gets
1261          * more complex. */
1262     }
1263     ;
1264
1265 bar_color_background:
1266     TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1267     {
1268         DLOG("background = %s\n", $2);
1269         current_bar.colors.background = $2;
1270     }
1271     ;
1272
1273 bar_color_statusline:
1274     TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1275     {
1276         DLOG("statusline = %s\n", $2);
1277         current_bar.colors.statusline = $2;
1278     }
1279     ;
1280
1281 bar_color_focused_workspace:
1282     TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1283     {
1284         /* Old syntax: text / background */
1285         DLOG("focused_ws = %s, %s (old)\n", $2, $3);
1286         current_bar.colors.focused_workspace_bg = $3;
1287         current_bar.colors.focused_workspace_text = $2;
1288     }
1289     | TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1290     {
1291         /* New syntax: border / background / text */
1292         DLOG("focused_ws = %s, %s and %s\n", $2, $3, $4);
1293         current_bar.colors.focused_workspace_border = $2;
1294         current_bar.colors.focused_workspace_bg = $3;
1295         current_bar.colors.focused_workspace_text = $4;
1296     }
1297     ;
1298
1299 bar_color_active_workspace:
1300     TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1301     {
1302         /* Old syntax: text / background */
1303         DLOG("active_ws = %s, %s (old)\n", $2, $3);
1304         current_bar.colors.active_workspace_bg = $3;
1305         current_bar.colors.active_workspace_text = $2;
1306     }
1307     | TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1308     {
1309         /* New syntax: border / background / text */
1310         DLOG("active_ws = %s, %s and %s\n", $2, $3, $4);
1311         current_bar.colors.active_workspace_border = $2;
1312         current_bar.colors.active_workspace_bg = $3;
1313         current_bar.colors.active_workspace_text = $4;
1314     }
1315     ;
1316
1317 bar_color_inactive_workspace:
1318     TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1319     {
1320         /* Old syntax: text / background */
1321         DLOG("inactive_ws = %s, %s (old)\n", $2, $3);
1322         current_bar.colors.inactive_workspace_bg = $3;
1323         current_bar.colors.inactive_workspace_text = $2;
1324     }
1325     | TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1326     {
1327         DLOG("inactive_ws = %s, %s and %s\n", $2, $3, $4);
1328         current_bar.colors.inactive_workspace_border = $2;
1329         current_bar.colors.inactive_workspace_bg = $3;
1330         current_bar.colors.inactive_workspace_text = $4;
1331     }
1332     ;
1333
1334 bar_color_urgent_workspace:
1335     TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1336     {
1337         /* Old syntax: text / background */
1338         DLOG("urgent_ws = %s, %s (old)\n", $2, $3);
1339         current_bar.colors.urgent_workspace_bg = $3;
1340         current_bar.colors.urgent_workspace_text = $2;
1341     }
1342     | TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1343     {
1344         DLOG("urgent_ws = %s, %s and %s\n", $2, $3, $4);
1345         current_bar.colors.urgent_workspace_border = $2;
1346         current_bar.colors.urgent_workspace_bg = $3;
1347         current_bar.colors.urgent_workspace_text = $4;
1348     }
1349     ;
1350
1351 floating_maximum_size:
1352     TOKFLOATING_MAXIMUM_SIZE NUMBER WORD NUMBER
1353     {
1354         printf("floating_maximum_width = %d\n", $2);
1355         printf("floating_maximum_height = %d\n", $4);
1356         config.floating_maximum_width = $2;
1357         config.floating_maximum_height = $4;
1358     }
1359     ;
1360
1361 floating_minimum_size:
1362     TOKFLOATING_MINIMUM_SIZE NUMBER WORD NUMBER
1363     {
1364         printf("floating_minimum_width = %d\n", $2);
1365         printf("floating_minimum_height = %d\n", $4);
1366         config.floating_minimum_width = $2;
1367         config.floating_minimum_height = $4;
1368     }
1369     ;
1370
1371 floating_modifier:
1372     TOKFLOATING_MODIFIER binding_modifiers
1373     {
1374         DLOG("floating modifier = %d\n", $2);
1375         config.floating_modifier = $2;
1376     }
1377     ;
1378
1379 orientation:
1380     TOK_ORIENTATION direction
1381     {
1382         DLOG("New containers should start with split direction %d\n", $2);
1383         config.default_orientation = $2;
1384     }
1385     ;
1386
1387 direction:
1388     TOK_HORIZ       { $$ = HORIZ; }
1389     | TOK_VERT      { $$ = VERT; }
1390     | TOK_AUTO      { $$ = NO_ORIENTATION; }
1391     ;
1392
1393 workspace_layout:
1394     TOK_WORKSPACE_LAYOUT layout_mode
1395     {
1396         DLOG("new containers will be in mode %d\n", $2);
1397         config.default_layout = $2;
1398
1399 #if 0
1400         /* We also need to change the layout of the already existing
1401          * workspaces here. Workspaces may exist at this point because
1402          * of the other directives which are modifying workspaces
1403          * (setting the preferred screen or name). While the workspace
1404          * objects are already created, they have never been used.
1405          * Thus, the user very likely awaits the default container mode
1406          * to trigger in this case, regardless of where it is inside
1407          * his configuration file. */
1408         Workspace *ws;
1409         TAILQ_FOREACH(ws, workspaces, workspaces) {
1410                 if (ws->table == NULL)
1411                         continue;
1412                 switch_layout_mode(global_conn,
1413                                    ws->table[0][0],
1414                                    config.container_mode);
1415         }
1416 #endif
1417     }
1418     | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1419     {
1420         DLOG("stack-limit %d with val %d\n", $3, $4);
1421         config.container_stack_limit = $3;
1422         config.container_stack_limit_value = $4;
1423
1424 #if 0
1425         /* See the comment above */
1426         Workspace *ws;
1427         TAILQ_FOREACH(ws, workspaces, workspaces) {
1428                 if (ws->table == NULL)
1429                         continue;
1430                 Container *con = ws->table[0][0];
1431                 con->stack_limit = config.container_stack_limit;
1432                 con->stack_limit_value = config.container_stack_limit_value;
1433         }
1434 #endif
1435     }
1436     ;
1437
1438 layout_mode:
1439     TOK_DEFAULT       { $$ = L_DEFAULT; }
1440     | TOK_STACKING    { $$ = L_STACKED; }
1441     | TOK_TABBED      { $$ = L_TABBED; }
1442     ;
1443
1444 new_window:
1445     TOKNEWWINDOW border_style
1446     {
1447         DLOG("new windows should start with border style %d\n", $2);
1448         config.default_border = $2;
1449     }
1450     ;
1451
1452 new_float:
1453     TOKNEWFLOAT border_style
1454     {
1455        DLOG("new floating windows should start with border style %d\n", $2);
1456        config.default_floating_border = $2;
1457     }
1458     ;
1459
1460 border_style:
1461     TOK_NORMAL      { $$ = BS_NORMAL; }
1462     | TOK_NONE      { $$ = BS_NONE; }
1463     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
1464     ;
1465
1466 bool:
1467     NUMBER
1468     {
1469         $$ = ($1 == 1);
1470     }
1471     | WORD
1472     {
1473         DLOG("checking word \"%s\"\n", $1);
1474         $$ = (strcasecmp($1, "yes") == 0 ||
1475               strcasecmp($1, "true") == 0 ||
1476               strcasecmp($1, "on") == 0 ||
1477               strcasecmp($1, "enable") == 0 ||
1478               strcasecmp($1, "active") == 0);
1479     }
1480     ;
1481
1482 hide_edge_borders:
1483     TOK_HIDE_EDGE_BORDERS edge_hiding_mode
1484     {
1485         DLOG("hide edge borders = %d\n", $2);
1486         config.hide_edge_borders = $2;
1487     }
1488     ;
1489
1490 edge_hiding_mode:
1491     TOK_NONE        { $$ = ADJ_NONE; }
1492     | TOK_VERT      { $$ = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE; }
1493     | TOK_HORIZ     { $$ = ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE; }
1494     | TOK_BOTH      { $$ = ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE | ADJ_UPPER_SCREEN_EDGE | ADJ_LOWER_SCREEN_EDGE; }
1495     | bool          { $$ = ($1 ? ADJ_LEFT_SCREEN_EDGE | ADJ_RIGHT_SCREEN_EDGE : ADJ_NONE); }
1496     ;
1497
1498 focus_follows_mouse:
1499     TOKFOCUSFOLLOWSMOUSE bool
1500     {
1501         DLOG("focus follows mouse = %d\n", $2);
1502         config.disable_focus_follows_mouse = !($2);
1503     }
1504     ;
1505
1506 force_focus_wrapping:
1507     TOK_FORCE_FOCUS_WRAPPING bool
1508     {
1509         DLOG("force focus wrapping = %d\n", $2);
1510         config.force_focus_wrapping = $2;
1511     }
1512     ;
1513
1514 force_xinerama:
1515     TOK_FORCE_XINERAMA bool
1516     {
1517         DLOG("force xinerama = %d\n", $2);
1518         config.force_xinerama = $2;
1519     }
1520     ;
1521
1522 fake_outputs:
1523     TOK_FAKE_OUTPUTS STR
1524     {
1525         DLOG("fake outputs = %s\n", $2);
1526         config.fake_outputs = $2;
1527     }
1528     ;
1529
1530 workspace_back_and_forth:
1531     TOK_WORKSPACE_AUTO_BAF bool
1532     {
1533         DLOG("automatic workspace back-and-forth = %d\n", $2);
1534         config.workspace_auto_back_and_forth = $2;
1535     }
1536     ;
1537
1538 workspace_bar:
1539     TOKWORKSPACEBAR bool
1540     {
1541         DLOG("workspace bar = %d\n", $2);
1542         config.disable_workspace_bar = !($2);
1543     }
1544     ;
1545
1546 workspace:
1547     TOKWORKSPACE qstring_or_number TOKOUTPUT OUTPUT optional_workspace_name
1548     {
1549         char *ws_name = $2;
1550
1551         if ($5 != NULL) {
1552             ELOG("The old (v3) syntax workspace <number> output <output> <name> is deprecated.\n");
1553             ELOG("Please use the new syntax: workspace \"<workspace>\" output <output>\n");
1554             ELOG("In your case, the following should work:\n");
1555             ELOG("    workspace \"%s\" output %s\n", $5, $4);
1556             ws_name = $5;
1557             context->has_warnings = true;
1558         }
1559
1560         DLOG("Assigning workspace \"%s\" to output \"%s\"\n", ws_name, $4);
1561         /* Check for earlier assignments of the same workspace so that we
1562          * don’t have assignments of a single workspace to different
1563          * outputs */
1564         struct Workspace_Assignment *assignment;
1565         bool duplicate = false;
1566         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1567             if (strcasecmp(assignment->name, ws_name) == 0) {
1568                 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1569                      ws_name);
1570                 assignment->output = $4;
1571                 duplicate = true;
1572             }
1573         }
1574         if (!duplicate) {
1575             assignment = scalloc(sizeof(struct Workspace_Assignment));
1576             assignment->name = ws_name;
1577             assignment->output = $4;
1578             TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1579         }
1580     }
1581     | TOKWORKSPACE NUMBER workspace_name
1582     {
1583         int ws_num = $2;
1584         if (ws_num < 1) {
1585             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1586         } else {
1587             DLOG("workspace name to: %s\n", $3);
1588 #if 0
1589             if ($<string>3 != NULL) {
1590                     workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1591                     free($<string>3);
1592             }
1593 #endif
1594         }
1595     }
1596     ;
1597
1598 optional_workspace_name:
1599     /* empty */          { $$ = NULL; }
1600     | workspace_name     { $$ = $1; }
1601     ;
1602
1603 workspace_name:
1604     QUOTEDSTRING         { $$ = $1; }
1605     | STR                { $$ = $1; }
1606     | WORD               { $$ = $1; }
1607     ;
1608
1609 assign:
1610     TOKASSIGN window_class STR
1611     {
1612         /* This is the old, deprecated form of assignments. It’s provided for
1613          * compatibility in version (4.1, 4.2, 4.3) and will be removed
1614          * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1615         ELOG("You are using the old assign syntax (without criteria). "
1616              "Please see the User's Guide for the new syntax and fix "
1617              "your config file.\n");
1618         context->has_warnings = true;
1619         printf("assignment of %s to *%s*\n", $2, $3);
1620         char *workspace = $3;
1621         char *criteria = $2;
1622
1623         Assignment *assignment = scalloc(sizeof(Assignment));
1624         Match *match = &(assignment->match);
1625         match_init(match);
1626
1627         char *separator = NULL;
1628         if ((separator = strchr(criteria, '/')) != NULL) {
1629             *(separator++) = '\0';
1630             char *pattern;
1631             sasprintf(&pattern, "(?i)%s", separator);
1632             match->title = regex_new(pattern);
1633             free(pattern);
1634             printf("  title = %s\n", separator);
1635         }
1636         if (*criteria != '\0') {
1637             char *pattern;
1638             sasprintf(&pattern, "(?i)%s", criteria);
1639             match->class = regex_new(pattern);
1640             free(pattern);
1641             printf("  class = %s\n", criteria);
1642         }
1643         free(criteria);
1644
1645         /* Compatibility with older versions: If the assignment target starts
1646          * with ~, we create the equivalent of:
1647          *
1648          * for_window [class="foo"] floating enable
1649          */
1650         if (*workspace == '~') {
1651             workspace++;
1652             if (*workspace == '\0') {
1653                 /* This assignment was *only* for floating */
1654                 assignment->type = A_COMMAND;
1655                 assignment->dest.command = sstrdup("floating enable");
1656                 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1657                 break;
1658             } else {
1659                 /* Create a new assignment and continue afterwards */
1660                 Assignment *floating = scalloc(sizeof(Assignment));
1661                 match_copy(&(floating->match), match);
1662                 floating->type = A_COMMAND;
1663                 floating->dest.command = sstrdup("floating enable");
1664                 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1665             }
1666         }
1667
1668         assignment->type = A_TO_WORKSPACE;
1669         assignment->dest.workspace = workspace;
1670         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1671     }
1672     | TOKASSIGN match STR
1673     {
1674         if (match_is_empty(&current_match)) {
1675             ELOG("Match is empty, ignoring this assignment\n");
1676             break;
1677         }
1678         printf("new assignment, using above criteria, to workspace %s\n", $3);
1679         Assignment *assignment = scalloc(sizeof(Assignment));
1680         assignment->match = current_match;
1681         assignment->type = A_TO_WORKSPACE;
1682         assignment->dest.workspace = $3;
1683         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1684     }
1685     ;
1686
1687 window_class:
1688     QUOTEDSTRING
1689     | STR_NG
1690     ;
1691
1692 ipcsocket:
1693     TOKIPCSOCKET STR
1694     {
1695         config.ipc_socket_path = $2;
1696     }
1697     ;
1698
1699 restart_state:
1700     TOKRESTARTSTATE STR
1701     {
1702         config.restart_state_path = $2;
1703     }
1704     ;
1705
1706 exec:
1707     TOKEXEC optional_no_startup_id STR
1708     {
1709         struct Autostart *new = smalloc(sizeof(struct Autostart));
1710         new->command = $3;
1711         new->no_startup_id = $2;
1712         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1713     }
1714     ;
1715
1716 exec_always:
1717     TOKEXEC_ALWAYS optional_no_startup_id STR
1718     {
1719         struct Autostart *new = smalloc(sizeof(struct Autostart));
1720         new->command = $3;
1721         new->no_startup_id = $2;
1722         TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1723     }
1724     ;
1725
1726 optional_no_startup_id:
1727     /* empty */ { $$ = false; }
1728     | TOK_NO_STARTUP_ID  { $$ = true; }
1729     ;
1730
1731 terminal:
1732     TOKTERMINAL STR
1733     {
1734         ELOG("The terminal option is DEPRECATED and has no effect. "
1735             "Please remove it from your configuration file.\n");
1736     }
1737     ;
1738
1739 font:
1740     TOKFONT STR
1741     {
1742         config.font = load_font($2, true);
1743         set_font(&config.font);
1744         printf("font %s\n", $2);
1745         FREE(font_pattern);
1746         font_pattern = $2;
1747     }
1748     ;
1749
1750 single_color:
1751     TOKSINGLECOLOR colorpixel
1752     {
1753         uint32_t *dest = $1;
1754         *dest = $2;
1755     }
1756     ;
1757
1758 color:
1759     TOKCOLOR colorpixel colorpixel colorpixel
1760     {
1761         struct Colortriple *dest = $1;
1762
1763         dest->border = $2;
1764         dest->background = $3;
1765         dest->text = $4;
1766     }
1767     | TOKCOLOR colorpixel colorpixel colorpixel colorpixel
1768     {
1769         struct Colortriple *dest = $1;
1770
1771         dest->border = $2;
1772         dest->background = $3;
1773         dest->text = $4;
1774         dest->indicator = $5;
1775     }
1776     ;
1777
1778 colorpixel:
1779     HEXCOLOR
1780     {
1781         $$ = get_colorpixel($1);
1782         free($1);
1783     }
1784     ;
1785
1786
1787 binding_modifiers:
1788     /* NULL */                               { $$ = 0; }
1789     | binding_modifier
1790     | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1791     | binding_modifiers '+'                  { $$ = $1; }
1792     ;
1793
1794 binding_modifier:
1795     MODIFIER        { $$ = $1; }
1796     | TOKCONTROL    { $$ = BIND_CONTROL; }
1797     | TOKSHIFT      { $$ = BIND_SHIFT; }
1798     ;
1799
1800 popup_during_fullscreen:
1801     TOK_POPUP_DURING_FULLSCREEN popup_setting
1802     {
1803         DLOG("popup_during_fullscreen setting: %d\n", $2);
1804         config.popup_during_fullscreen = $2;
1805     }
1806     ;
1807
1808 popup_setting:
1809     TOK_IGNORE              { $$ = PDF_IGNORE; }
1810     | TOK_LEAVE_FULLSCREEN  { $$ = PDF_LEAVE_FULLSCREEN; }
1811     ;