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