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