]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
Bugfix: Ensure that the percentage is > 0.05 when using the 'resize' cmd (Thanks...
[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 #include <limits.h>
12
13 #include "all.h"
14
15 static pid_t configerror_pid = -1;
16
17 static Match current_match;
18
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int yylex(struct context *context);
21 extern int yyparse(void);
22 extern int yylex_destroy(void);
23 extern FILE *yyin;
24 YY_BUFFER_STATE yy_scan_string(const char *);
25
26 static struct bindings_head *current_bindings;
27 static struct context *context;
28
29 /* We don’t need yydebug for now, as we got decent error messages using
30  * yyerror(). Should you ever want to extend the parser, it might be handy
31  * to just comment it in again, so it stays here. */
32 //int yydebug = 1;
33
34 void yyerror(const char *error_message) {
35     context->has_errors = true;
36
37     ELOG("\n");
38     ELOG("CONFIG: %s\n", error_message);
39     ELOG("CONFIG: in file \"%s\", line %d:\n",
40         context->filename, context->line_number);
41     ELOG("CONFIG:   %s\n", context->line_copy);
42     char buffer[context->last_column+1];
43     buffer[context->last_column] = '\0';
44     for (int c = 1; c <= context->last_column; c++)
45         buffer[c-1] = (c >= context->first_column ? '^' : ' ');
46     ELOG("CONFIG:   %s\n", buffer);
47     ELOG("\n");
48 }
49
50 int yywrap() {
51     return 1;
52 }
53
54 /*
55  * Goes through each line of buf (separated by \n) and checks for statements /
56  * commands which only occur in i3 v4 configuration files. If it finds any, it
57  * returns version 4, otherwise it returns version 3.
58  *
59  */
60 static int detect_version(char *buf) {
61     char *walk = buf;
62     char *line = buf;
63     while (*walk != '\0') {
64         if (*walk != '\n') {
65             walk++;
66             continue;
67         }
68
69         /* check for some v4-only statements */
70         if (strncasecmp(line, "bindcode", strlen("bindcode")) == 0 ||
71             strncasecmp(line, "force_focus_wrapping", strlen("force_focus_wrapping")) == 0 ||
72             strncasecmp(line, "# i3 config file (v4)", strlen("# i3 config file (v4)")) == 0 ||
73             strncasecmp(line, "workspace_layout", strlen("workspace_layout")) == 0) {
74             printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
75             return 4;
76         }
77
78         /* if this is a bind statement, we can check the command */
79         if (strncasecmp(line, "bind", strlen("bind")) == 0) {
80             char *bind = strchr(line, ' ');
81             if (bind == NULL)
82                 goto next;
83             while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
84                 bind++;
85             if (*bind == '\0')
86                 goto next;
87             if ((bind = strchr(bind, ' ')) == NULL)
88                 goto next;
89             while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
90                 bind++;
91             if (*bind == '\0')
92                 goto next;
93             if (strncasecmp(bind, "layout", strlen("layout")) == 0 ||
94                 strncasecmp(bind, "floating", strlen("floating")) == 0 ||
95                 strncasecmp(bind, "workspace", strlen("workspace")) == 0 ||
96                 strncasecmp(bind, "focus left", strlen("focus left")) == 0 ||
97                 strncasecmp(bind, "focus right", strlen("focus right")) == 0 ||
98                 strncasecmp(bind, "focus up", strlen("focus up")) == 0 ||
99                 strncasecmp(bind, "focus down", strlen("focus down")) == 0 ||
100                 strncasecmp(bind, "border normal", strlen("border normal")) == 0 ||
101                 strncasecmp(bind, "border 1pixel", strlen("border 1pixel")) == 0 ||
102                 strncasecmp(bind, "border borderless", strlen("border borderless")) == 0) {
103                 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
104                 return 4;
105             }
106         }
107
108 next:
109         /* advance to the next line */
110         walk++;
111         line = walk;
112     }
113
114     return 3;
115 }
116
117 /*
118  * Calls i3-migrate-config-to-v4 to migrate a configuration file (input
119  * buffer).
120  *
121  * Returns the converted config file or NULL if there was an error (for
122  * example the script could not be found in $PATH or the i3 executable’s
123  * directory).
124  *
125  */
126 static char *migrate_config(char *input, off_t size) {
127     int writepipe[2];
128     int readpipe[2];
129
130     if (pipe(writepipe) != 0 ||
131         pipe(readpipe) != 0) {
132         warn("migrate_config: Could not create pipes");
133         return NULL;
134     }
135
136     pid_t pid = fork();
137     if (pid == -1) {
138         warn("Could not fork()");
139         return NULL;
140     }
141
142     /* child */
143     if (pid == 0) {
144         /* close writing end of writepipe, connect reading side to stdin */
145         close(writepipe[1]);
146         dup2(writepipe[0], 0);
147
148         /* close reading end of readpipe, connect writing side to stdout */
149         close(readpipe[0]);
150         dup2(readpipe[1], 1);
151
152         static char *argv[] = {
153             NULL, /* will be replaced by the executable path */
154             NULL
155         };
156         exec_i3_utility("i3-migrate-config-to-v4", argv);
157     }
158
159     /* parent */
160
161     /* close reading end of the writepipe (connected to the script’s stdin) */
162     close(writepipe[0]);
163
164     /* write the whole config file to the pipe, the script will read everything
165      * immediately */
166     int written = 0;
167     int ret;
168     while (written < size) {
169         if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
170             warn("Could not write to pipe");
171             return NULL;
172         }
173         written += ret;
174     }
175     close(writepipe[1]);
176
177     /* close writing end of the readpipe (connected to the script’s stdout) */
178     close(readpipe[1]);
179
180     /* read the script’s output */
181     int conv_size = 65535;
182     char *converted = malloc(conv_size);
183     int read_bytes = 0;
184     do {
185         if (read_bytes == conv_size) {
186             conv_size += 65535;
187             converted = realloc(converted, conv_size);
188         }
189         ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
190         if (ret == -1) {
191             warn("Cannot read from pipe");
192             return NULL;
193         }
194         read_bytes += ret;
195     } while (ret > 0);
196
197     /* get the returncode */
198     int status;
199     wait(&status);
200     if (!WIFEXITED(status)) {
201         fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
202         return NULL;
203     }
204
205     int returncode = WEXITSTATUS(status);
206     if (returncode != 0) {
207         fprintf(stderr, "Migration process exit code was != 0\n");
208         if (returncode == 2) {
209             fprintf(stderr, "could not start the migration script\n");
210             /* TODO: script was not found. tell the user to fix his system or create a v4 config */
211         } else if (returncode == 1) {
212             fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
213             fprintf(stderr, "# i3 config file (v4)\n");
214             /* TODO: nag the user with a message to include a hint for i3 in his config file */
215         }
216         return NULL;
217     }
218
219     return converted;
220 }
221
222 /*
223  * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
224  * it exited (or could not be started, depending on the exit code).
225  *
226  */
227 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
228     ev_child_stop(EV_A_ watcher);
229     if (!WIFEXITED(watcher->rstatus)) {
230         fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
231         return;
232     }
233
234     int exitcode = WEXITSTATUS(watcher->rstatus);
235     printf("i3-nagbar process exited with status %d\n", exitcode);
236     if (exitcode == 2) {
237         fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
238     }
239
240     configerror_pid = -1;
241 }
242
243 /*
244  * Starts an i3-nagbar process which alerts the user that his configuration
245  * file contains one or more errors. Also offers two buttons: One to launch an
246  * $EDITOR on the config file and another one to launch a $PAGER on the error
247  * logfile.
248  *
249  */
250 static void start_configerror_nagbar(const char *config_path) {
251     fprintf(stderr, "Would start i3-nagscreen now\n");
252     configerror_pid = fork();
253     if (configerror_pid == -1) {
254         warn("Could not fork()");
255         return;
256     }
257
258     /* child */
259     if (configerror_pid == 0) {
260         char *editaction,
261              *pageraction;
262         if (asprintf(&editaction, TERM_EMU " -e sh -c \"${EDITOR:-vi} \"%s\" && i3-msg reload\"", config_path) == -1)
263             exit(1);
264         if (asprintf(&pageraction, TERM_EMU " -e sh -c \"${PAGER:-less} \"%s\"\"", errorfilename) == -1)
265             exit(1);
266         char *argv[] = {
267             NULL, /* will be replaced by the executable path */
268             "-m",
269             "You have an error in your i3 config file!",
270             "-b",
271             "edit config",
272             editaction,
273             (errorfilename ? "-b" : NULL),
274             "show errors",
275             pageraction,
276             NULL
277         };
278         exec_i3_utility("i3-nagbar", argv);
279     }
280
281     /* parent */
282     /* install a child watcher */
283     ev_child *child = smalloc(sizeof(ev_child));
284     ev_child_init(child, &nagbar_exited, configerror_pid, 0);
285     ev_child_start(main_loop, child);
286 }
287
288 /*
289  * Kills the configerror i3-nagbar process, if any.
290  *
291  * Called when reloading/restarting.
292  *
293  * If wait_for_it is set (restarting), this function will waitpid(), otherwise,
294  * ev is assumed to handle it (reloading).
295  *
296  */
297 void kill_configerror_nagbar(bool wait_for_it) {
298     if (configerror_pid == -1)
299         return;
300
301     if (kill(configerror_pid, SIGTERM) == -1)
302         warn("kill(configerror_nagbar) failed");
303
304     if (!wait_for_it)
305         return;
306
307     /* When restarting, we don’t enter the ev main loop anymore and after the
308      * exec(), our old pid is no longer watched. So, ev won’t handle SIGCHLD
309      * for us and we would end up with a <defunct> process. Therefore we
310      * waitpid() here. */
311     waitpid(configerror_pid, NULL, 0);
312 }
313
314 void parse_file(const char *f) {
315     SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
316     int fd, ret, read_bytes = 0;
317     struct stat stbuf;
318     char *buf;
319     FILE *fstr;
320     char buffer[1026], key[512], value[512];
321
322     if ((fd = open(f, O_RDONLY)) == -1)
323         die("Could not open configuration file: %s\n", strerror(errno));
324
325     if (fstat(fd, &stbuf) == -1)
326         die("Could not fstat file: %s\n", strerror(errno));
327
328     buf = scalloc((stbuf.st_size + 1) * sizeof(char));
329     while (read_bytes < stbuf.st_size) {
330         if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
331             die("Could not read(): %s\n", strerror(errno));
332         read_bytes += ret;
333     }
334
335     if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
336         die("Could not lseek: %s\n", strerror(errno));
337
338     if ((fstr = fdopen(fd, "r")) == NULL)
339         die("Could not fdopen: %s\n", strerror(errno));
340
341     while (!feof(fstr)) {
342         if (fgets(buffer, 1024, fstr) == NULL) {
343             if (feof(fstr))
344                 break;
345             die("Could not read configuration file\n");
346         }
347
348         /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
349         if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
350             key[0] == '#' || strlen(key) < 3)
351             continue;
352
353         if (strcasecmp(key, "set") == 0) {
354             if (value[0] != '$')
355                 die("Malformed variable assignment, name has to start with $\n");
356
357             /* get key/value for this variable */
358             char *v_key = value, *v_value;
359             if ((v_value = strstr(value, " ")) == NULL)
360                 die("Malformed variable assignment, need a value\n");
361
362             *(v_value++) = '\0';
363
364             struct Variable *new = scalloc(sizeof(struct Variable));
365             new->key = sstrdup(v_key);
366             new->value = sstrdup(v_value);
367             SLIST_INSERT_HEAD(&variables, new, variables);
368             DLOG("Got new variable %s = %s\n", v_key, v_value);
369             continue;
370         }
371     }
372     fclose(fstr);
373
374     /* For every custom variable, see how often it occurs in the file and
375      * how much extra bytes it requires when replaced. */
376     struct Variable *current, *nearest;
377     int extra_bytes = 0;
378     /* We need to copy the buffer because we need to invalidate the
379      * variables (otherwise we will count them twice, which is bad when
380      * 'extra' is negative) */
381     char *bufcopy = sstrdup(buf);
382     SLIST_FOREACH(current, &variables, variables) {
383         int extra = (strlen(current->value) - strlen(current->key));
384         char *next;
385         for (next = bufcopy;
386              (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
387              next += strlen(current->key)) {
388             *next = '_';
389             extra_bytes += extra;
390         }
391     }
392     FREE(bufcopy);
393
394     /* Then, allocate a new buffer and copy the file over to the new one,
395      * but replace occurences of our variables */
396     char *walk = buf, *destwalk;
397     char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
398     destwalk = new;
399     while (walk < (buf + stbuf.st_size)) {
400         /* Find the next variable */
401         SLIST_FOREACH(current, &variables, variables)
402             current->next_match = strcasestr(walk, current->key);
403         nearest = NULL;
404         int distance = stbuf.st_size;
405         SLIST_FOREACH(current, &variables, variables) {
406             if (current->next_match == NULL)
407                 continue;
408             if ((current->next_match - walk) < distance) {
409                 distance = (current->next_match - walk);
410                 nearest = current;
411             }
412         }
413         if (nearest == NULL) {
414             /* If there are no more variables, we just copy the rest */
415             strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
416             destwalk += (buf + stbuf.st_size) - walk;
417             *destwalk = '\0';
418             break;
419         } else {
420             /* Copy until the next variable, then copy its value */
421             strncpy(destwalk, walk, distance);
422             strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
423             walk += distance + strlen(nearest->key);
424             destwalk += distance + strlen(nearest->value);
425         }
426     }
427
428     /* analyze the string to find out whether this is an old config file (3.x)
429      * or a new config file (4.x). If it’s old, we run the converter script. */
430     int version = detect_version(buf);
431     if (version == 3) {
432         /* We need to convert this v3 configuration */
433         char *converted = migrate_config(new, stbuf.st_size);
434         if (converted != NULL) {
435             printf("\n");
436             printf("****************************************************************\n");
437             printf("NOTE: Automatically converted configuration file from v3 to v4.\n");
438             printf("\n");
439             printf("Please convert your config file to v4. You can use this command:\n");
440             printf("    mv %s %s.O\n", f, f);
441             printf("    i3-migrate-config-to-v4 %s.O > %s\n", f, f);
442             printf("****************************************************************\n");
443             printf("\n");
444             free(new);
445             new = converted;
446         } else {
447             printf("\n");
448             printf("**********************************************************************\n");
449             printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
450             printf("was not correctly installed on your system?\n");
451             printf("**********************************************************************\n");
452             printf("\n");
453         }
454     }
455
456     /* now lex/parse it */
457     yy_scan_string(new);
458
459     context = scalloc(sizeof(struct context));
460     context->filename = f;
461
462     if (yyparse() != 0) {
463         fprintf(stderr, "Could not parse configfile\n");
464         exit(1);
465     }
466
467     if (context->has_errors) {
468         start_configerror_nagbar(f);
469     }
470
471     yylex_destroy();
472     FREE(context->line_copy);
473     free(context);
474     free(new);
475     free(buf);
476
477     while (!SLIST_EMPTY(&variables)) {
478         current = SLIST_FIRST(&variables);
479         FREE(current->key);
480         FREE(current->value);
481         SLIST_REMOVE_HEAD(&variables, variables);
482         FREE(current);
483     }
484 }
485
486 %}
487
488 %error-verbose
489 %lex-param { struct context *context }
490
491 %union {
492     int number;
493     char *string;
494     uint32_t *single_color;
495     struct Colortriple *color;
496     Match *match;
497     struct Binding *binding;
498 }
499
500 %token  <number>        NUMBER                      "<number>"
501 %token  <string>        WORD                        "<word>"
502 %token  <string>        STR                         "<string>"
503 %token  <string>        STR_NG                      "<string (non-greedy)>"
504 %token  <string>        HEX                         "<hex>"
505 %token  <string>        OUTPUT                      "<RandR output>"
506 %token                  TOKBINDCODE
507 %token                  TOKTERMINAL
508 %token                  TOKCOMMENT                  "<comment>"
509 %token                  TOKFONT                     "font"
510 %token                  TOKBINDSYM                  "bindsym"
511 %token  <number>        MODIFIER                    "<modifier>"
512 %token                  TOKCONTROL                  "control"
513 %token                  TOKSHIFT                    "shift"
514 %token                  TOKFLOATING_MODIFIER        "floating_modifier"
515 %token  <string>        QUOTEDSTRING                "<quoted string>"
516 %token                  TOKWORKSPACE                "workspace"
517 %token                  TOKOUTPUT                   "output"
518 %token                  TOKASSIGN                   "assign"
519 %token                  TOKSET
520 %token                  TOKIPCSOCKET                "ipc_socket"
521 %token                  TOKRESTARTSTATE             "restart_state"
522 %token                  TOKEXEC                     "exec"
523 %token                  TOKEXEC_ALWAYS              "exec_always"
524 %token  <single_color>  TOKSINGLECOLOR
525 %token  <color>         TOKCOLOR
526 %token                  TOKARROW                    "→"
527 %token                  TOKMODE                     "mode"
528 %token                  TOK_ORIENTATION             "default_orientation"
529 %token                  TOK_HORIZ                   "horizontal"
530 %token                  TOK_VERT                    "vertical"
531 %token                  TOK_AUTO                    "auto"
532 %token                  TOK_WORKSPACE_LAYOUT        "workspace_layout"
533 %token                  TOKNEWWINDOW                "new_window"
534 %token                  TOK_NORMAL                  "normal"
535 %token                  TOK_NONE                    "none"
536 %token                  TOK_1PIXEL                  "1pixel"
537 %token                  TOKFOCUSFOLLOWSMOUSE        "focus_follows_mouse"
538 %token                  TOK_FORCE_FOCUS_WRAPPING    "force_focus_wrapping"
539 %token                  TOKWORKSPACEBAR             "workspace_bar"
540 %token                  TOK_DEFAULT                 "default"
541 %token                  TOK_STACKING                "stacking"
542 %token                  TOK_TABBED                  "tabbed"
543 %token  <number>        TOKSTACKLIMIT               "stack-limit"
544 %token                  TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
545 %token                  TOK_IGNORE                  "ignore"
546 %token                  TOK_LEAVE_FULLSCREEN        "leave_fullscreen"
547 %token                  TOK_FOR_WINDOW              "for_window"
548
549 %token              TOK_MARK            "mark"
550 %token              TOK_CLASS           "class"
551 %token              TOK_ID              "id"
552 %token              TOK_CON_ID          "con_id"
553 %token              TOK_TITLE           "title"
554
555 %type   <binding>       binding
556 %type   <binding>       bindcode
557 %type   <binding>       bindsym
558 %type   <number>        binding_modifiers
559 %type   <number>        binding_modifier
560 %type   <number>        direction
561 %type   <number>        layout_mode
562 %type   <number>        border_style
563 %type   <number>        new_window
564 %type   <number>        colorpixel
565 %type   <number>        bool
566 %type   <number>        popup_setting
567 %type   <string>        command
568 %type   <string>        word_or_number
569 %type   <string>        optional_workspace_name
570 %type   <string>        workspace_name
571 %type   <string>        window_class
572
573 %%
574
575 lines: /* empty */
576     | lines error
577     | lines line
578     ;
579
580 line:
581     bindline
582     | for_window
583     | mode
584     | floating_modifier
585     | orientation
586     | workspace_layout
587     | new_window
588     | focus_follows_mouse
589     | force_focus_wrapping
590     | workspace_bar
591     | workspace
592     | assign
593     | ipcsocket
594     | restart_state
595     | exec
596     | exec_always
597     | single_color
598     | color
599     | terminal
600     | font
601     | comment
602     | popup_during_fullscreen
603     ;
604
605 comment:
606     TOKCOMMENT
607     ;
608
609 command:
610     STR
611     ;
612
613 bindline:
614     binding
615     {
616         TAILQ_INSERT_TAIL(bindings, $1, bindings);
617     }
618     ;
619
620 binding:
621     TOKBINDCODE bindcode         { $$ = $2; }
622     | TOKBINDSYM bindsym         { $$ = $2; }
623     ;
624
625 bindcode:
626     binding_modifiers NUMBER command
627     {
628         printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
629         Binding *new = scalloc(sizeof(Binding));
630
631         new->keycode = $2;
632         new->mods = $1;
633         new->command = $3;
634
635         $$ = new;
636     }
637     ;
638
639 bindsym:
640     binding_modifiers word_or_number command
641     {
642         printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
643         Binding *new = scalloc(sizeof(Binding));
644
645         new->symbol = $2;
646         new->mods = $1;
647         new->command = $3;
648
649         $$ = new;
650     }
651     ;
652
653 for_window:
654     TOK_FOR_WINDOW match command
655     {
656         printf("\t should execute command %s for the criteria mentioned above\n", $3);
657         Assignment *assignment = scalloc(sizeof(Assignment));
658         assignment->type = A_COMMAND;
659         assignment->match = current_match;
660         assignment->dest.command = $3;
661         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
662     }
663     ;
664
665 match:
666     | matchstart criteria matchend
667     {
668         printf("match parsed\n");
669     }
670     ;
671
672 matchstart:
673     '['
674     {
675         printf("start\n");
676         match_init(&current_match);
677     }
678     ;
679
680 matchend:
681     ']'
682     {
683         printf("match specification finished\n");
684     }
685     ;
686
687 criteria:
688     criteria criterion
689     | criterion
690     ;
691
692 criterion:
693     TOK_CLASS '=' STR
694     {
695         printf("criteria: class = %s\n", $3);
696         current_match.class = $3;
697     }
698     | TOK_CON_ID '=' STR
699     {
700         printf("criteria: id = %s\n", $3);
701         char *end;
702         long parsed = strtol($3, &end, 10);
703         if (parsed == LONG_MIN ||
704             parsed == LONG_MAX ||
705             parsed < 0 ||
706             (end && *end != '\0')) {
707             ELOG("Could not parse con id \"%s\"\n", $3);
708         } else {
709             current_match.con_id = (Con*)parsed;
710             printf("id as int = %p\n", current_match.con_id);
711         }
712     }
713     | TOK_ID '=' STR
714     {
715         printf("criteria: window id = %s\n", $3);
716         char *end;
717         long parsed = strtol($3, &end, 10);
718         if (parsed == LONG_MIN ||
719             parsed == LONG_MAX ||
720             parsed < 0 ||
721             (end && *end != '\0')) {
722             ELOG("Could not parse window id \"%s\"\n", $3);
723         } else {
724             current_match.id = parsed;
725             printf("window id as int = %d\n", current_match.id);
726         }
727     }
728     | TOK_MARK '=' STR
729     {
730         printf("criteria: mark = %s\n", $3);
731         current_match.mark = $3;
732     }
733     | TOK_TITLE '=' STR
734     {
735         printf("criteria: title = %s\n", $3);
736         current_match.title = $3;
737     }
738     ;
739
740
741
742 word_or_number:
743     WORD
744     | NUMBER
745     {
746         asprintf(&$$, "%d", $1);
747     }
748     ;
749
750 mode:
751     TOKMODE QUOTEDSTRING '{' modelines '}'
752     {
753         if (strcasecmp($2, "default") == 0) {
754             printf("You cannot use the name \"default\" for your mode\n");
755             exit(1);
756         }
757         printf("\t now in mode %s\n", $2);
758         printf("\t current bindings = %p\n", current_bindings);
759         Binding *binding;
760         TAILQ_FOREACH(binding, current_bindings, bindings) {
761             printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
762                             binding->mods, binding->keycode, binding->symbol, binding->command);
763         }
764
765         struct Mode *mode = scalloc(sizeof(struct Mode));
766         mode->name = $2;
767         mode->bindings = current_bindings;
768         current_bindings = NULL;
769         SLIST_INSERT_HEAD(&modes, mode, modes);
770     }
771     ;
772
773
774 modelines:
775     /* empty */
776     | modelines modeline
777     ;
778
779 modeline:
780     comment
781     | binding
782     {
783         if (current_bindings == NULL) {
784             current_bindings = scalloc(sizeof(struct bindings_head));
785             TAILQ_INIT(current_bindings);
786         }
787
788         TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
789     }
790     ;
791
792 floating_modifier:
793     TOKFLOATING_MODIFIER binding_modifiers
794     {
795         DLOG("floating modifier = %d\n", $2);
796         config.floating_modifier = $2;
797     }
798     ;
799
800 orientation:
801     TOK_ORIENTATION direction
802     {
803         DLOG("New containers should start with split direction %d\n", $2);
804         config.default_orientation = $2;
805     }
806     ;
807
808 direction:
809     TOK_HORIZ       { $$ = HORIZ; }
810     | TOK_VERT      { $$ = VERT; }
811     | TOK_AUTO      { $$ = NO_ORIENTATION; }
812     ;
813
814 workspace_layout:
815     TOK_WORKSPACE_LAYOUT layout_mode
816     {
817         DLOG("new containers will be in mode %d\n", $2);
818         config.default_layout = $2;
819
820 #if 0
821         /* We also need to change the layout of the already existing
822          * workspaces here. Workspaces may exist at this point because
823          * of the other directives which are modifying workspaces
824          * (setting the preferred screen or name). While the workspace
825          * objects are already created, they have never been used.
826          * Thus, the user very likely awaits the default container mode
827          * to trigger in this case, regardless of where it is inside
828          * his configuration file. */
829         Workspace *ws;
830         TAILQ_FOREACH(ws, workspaces, workspaces) {
831                 if (ws->table == NULL)
832                         continue;
833                 switch_layout_mode(global_conn,
834                                    ws->table[0][0],
835                                    config.container_mode);
836         }
837 #endif
838     }
839     | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
840     {
841         DLOG("stack-limit %d with val %d\n", $3, $4);
842         config.container_stack_limit = $3;
843         config.container_stack_limit_value = $4;
844
845 #if 0
846         /* See the comment above */
847         Workspace *ws;
848         TAILQ_FOREACH(ws, workspaces, workspaces) {
849                 if (ws->table == NULL)
850                         continue;
851                 Container *con = ws->table[0][0];
852                 con->stack_limit = config.container_stack_limit;
853                 con->stack_limit_value = config.container_stack_limit_value;
854         }
855 #endif
856     }
857     ;
858
859 layout_mode:
860     TOK_DEFAULT       { $$ = L_DEFAULT; }
861     | TOK_STACKING    { $$ = L_STACKED; }
862     | TOK_TABBED      { $$ = L_TABBED; }
863     ;
864
865 new_window:
866     TOKNEWWINDOW border_style
867     {
868         DLOG("new windows should start with border style %d\n", $2);
869         config.default_border = $2;
870     }
871     ;
872
873 border_style:
874     TOK_NORMAL      { $$ = BS_NORMAL; }
875     | TOK_NONE      { $$ = BS_NONE; }
876     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
877     ;
878
879 bool:
880     NUMBER
881     {
882         $$ = ($1 == 1);
883     }
884     | WORD
885     {
886         DLOG("checking word \"%s\"\n", $1);
887         $$ = (strcasecmp($1, "yes") == 0 ||
888               strcasecmp($1, "true") == 0 ||
889               strcasecmp($1, "on") == 0 ||
890               strcasecmp($1, "enable") == 0 ||
891               strcasecmp($1, "active") == 0);
892     }
893     ;
894
895 focus_follows_mouse:
896     TOKFOCUSFOLLOWSMOUSE bool
897     {
898         DLOG("focus follows mouse = %d\n", $2);
899         config.disable_focus_follows_mouse = !($2);
900     }
901     ;
902
903 force_focus_wrapping:
904     TOK_FORCE_FOCUS_WRAPPING bool
905     {
906         DLOG("force focus wrapping = %d\n", $2);
907         config.force_focus_wrapping = $2;
908     }
909     ;
910
911 workspace_bar:
912     TOKWORKSPACEBAR bool
913     {
914         DLOG("workspace bar = %d\n", $2);
915         config.disable_workspace_bar = !($2);
916     }
917     ;
918
919 workspace:
920     TOKWORKSPACE NUMBER TOKOUTPUT OUTPUT optional_workspace_name
921     {
922         int ws_num = $2;
923         if (ws_num < 1) {
924             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
925         } else {
926             char *ws_name = NULL;
927             if ($5 == NULL) {
928                 asprintf(&ws_name, "%d", ws_num);
929             } else {
930                 ws_name = $5;
931             }
932
933             DLOG("Should assign workspace %s to output %s\n", ws_name, $4);
934             struct Workspace_Assignment *assignment = scalloc(sizeof(struct Workspace_Assignment));
935             assignment->name = ws_name;
936             assignment->output = $4;
937             TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
938         }
939     }
940     | TOKWORKSPACE NUMBER workspace_name
941     {
942         int ws_num = $2;
943         if (ws_num < 1) {
944             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
945         } else {
946             DLOG("workspace name to: %s\n", $3);
947 #if 0
948             if ($<string>3 != NULL) {
949                     workspace_set_name(workspace_get(ws_num - 1), $<string>3);
950                     free($<string>3);
951             }
952 #endif
953         }
954     }
955     ;
956
957 optional_workspace_name:
958     /* empty */          { $$ = NULL; }
959     | workspace_name     { $$ = $1; }
960     ;
961
962 workspace_name:
963     QUOTEDSTRING         { $$ = $1; }
964     | STR                { $$ = $1; }
965     | WORD               { $$ = $1; }
966     ;
967
968 assign:
969     TOKASSIGN window_class STR
970     {
971         printf("assignment of %s to *%s*\n", $2, $3);
972         char *workspace = $3;
973         char *criteria = $2;
974
975         Assignment *assignment = scalloc(sizeof(Assignment));
976         Match *match = &(assignment->match);
977         match_init(match);
978
979         char *separator = NULL;
980         if ((separator = strchr(criteria, '/')) != NULL) {
981             *(separator++) = '\0';
982             match->title = sstrdup(separator);
983         }
984         if (*criteria != '\0')
985             match->class = sstrdup(criteria);
986         free(criteria);
987
988         printf("  class = %s\n", match->class);
989         printf("  title = %s\n", match->title);
990
991         /* Compatibility with older versions: If the assignment target starts
992          * with ~, we create the equivalent of:
993          *
994          * for_window [class="foo"] mode floating
995          */
996         if (*workspace == '~') {
997             workspace++;
998             if (*workspace == '\0') {
999                 /* This assignment was *only* for floating */
1000                 assignment->type = A_COMMAND;
1001                 assignment->dest.command = sstrdup("floating enable");
1002                 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1003                 break;
1004             } else {
1005                 /* Create a new assignment and continue afterwards */
1006                 Assignment *floating = scalloc(sizeof(Assignment));
1007                 match_copy(&(floating->match), match);
1008                 floating->type = A_COMMAND;
1009                 floating->dest.command = sstrdup("floating enable");
1010                 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1011             }
1012         }
1013
1014         assignment->type = A_TO_WORKSPACE;
1015         assignment->dest.workspace = workspace;
1016         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1017     }
1018     ;
1019
1020 window_class:
1021     QUOTEDSTRING
1022     | STR_NG
1023     ;
1024
1025 ipcsocket:
1026     TOKIPCSOCKET STR
1027     {
1028         config.ipc_socket_path = $2;
1029     }
1030     ;
1031
1032 restart_state:
1033     TOKRESTARTSTATE STR
1034     {
1035         config.restart_state_path = $2;
1036     }
1037     ;
1038
1039 exec:
1040     TOKEXEC STR
1041     {
1042         struct Autostart *new = smalloc(sizeof(struct Autostart));
1043         new->command = $2;
1044         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1045     }
1046     ;
1047
1048 exec_always:
1049     TOKEXEC_ALWAYS STR
1050     {
1051         struct Autostart *new = smalloc(sizeof(struct Autostart));
1052         new->command = $2;
1053         TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1054     }
1055     ;
1056
1057 terminal:
1058     TOKTERMINAL STR
1059     {
1060         ELOG("The terminal option is DEPRECATED and has no effect. "
1061             "Please remove it from your configuration file.\n");
1062     }
1063     ;
1064
1065 font:
1066     TOKFONT STR
1067     {
1068         config.font = load_font($2, true);
1069         printf("font %s\n", $2);
1070         free($2);
1071     }
1072     ;
1073
1074 single_color:
1075     TOKSINGLECOLOR colorpixel
1076     {
1077         uint32_t *dest = $1;
1078         *dest = $2;
1079     }
1080     ;
1081
1082 color:
1083     TOKCOLOR colorpixel colorpixel colorpixel
1084     {
1085         struct Colortriple *dest = $1;
1086
1087         dest->border = $2;
1088         dest->background = $3;
1089         dest->text = $4;
1090     }
1091     ;
1092
1093 colorpixel:
1094     '#' HEX
1095     {
1096         char *hex;
1097         if (asprintf(&hex, "#%s", $2) == -1)
1098             die("asprintf()");
1099         $$ = get_colorpixel(hex);
1100         free(hex);
1101     }
1102     ;
1103
1104
1105 binding_modifiers:
1106     /* NULL */                               { $$ = 0; }
1107     | binding_modifier
1108     | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1109     | binding_modifiers '+'                  { $$ = $1; }
1110     ;
1111
1112 binding_modifier:
1113     MODIFIER        { $$ = $1; }
1114     | TOKCONTROL    { $$ = BIND_CONTROL; }
1115     | TOKSHIFT      { $$ = BIND_SHIFT; }
1116     ;
1117
1118 popup_during_fullscreen:
1119     TOK_POPUP_DURING_FULLSCREEN popup_setting
1120     {
1121         DLOG("popup_during_fullscreen setting: %d\n", $2);
1122         config.popup_during_fullscreen = $2;
1123     }
1124     ;
1125
1126 popup_setting:
1127     TOK_IGNORE              { $$ = PDF_IGNORE; }
1128     | TOK_LEAVE_FULLSCREEN  { $$ = PDF_LEAVE_FULLSCREEN; }
1129     ;