3 * vim:ts=4:sw=4:expandtab
16 static Match current_match;
18 typedef struct yy_buffer_state *YY_BUFFER_STATE;
19 extern int yylex(struct context *context);
20 extern int yyparse(void);
22 YY_BUFFER_STATE yy_scan_string(const char *);
24 static struct bindings_head *current_bindings;
25 static struct context *context;
27 /* We don’t need yydebug for now, as we got decent error messages using
28 * yyerror(). Should you ever want to extend the parser, it might be handy
29 * to just comment it in again, so it stays here. */
32 void yyerror(const char *error_message) {
34 ELOG("CONFIG: %s\n", error_message);
35 ELOG("CONFIG: in file \"%s\", line %d:\n",
36 context->filename, context->line_number);
37 ELOG("CONFIG: %s\n", context->line_copy);
39 for (int c = 1; c <= context->last_column; c++)
40 if (c >= context->first_column)
52 * Goes through each line of buf (separated by \n) and checks for statements /
53 * commands which only occur in i3 v4 configuration files. If it finds any, it
54 * returns version 4, otherwise it returns version 3.
57 static int detect_version(char *buf) {
60 while (*walk != '\0') {
66 /* check for some v4-only statements */
67 if (strncasecmp(line, "bindcode", strlen("bindcode")) == 0 ||
68 strncasecmp(line, "# i3 config file (v4)", strlen("# i3 config file (v4)")) == 0 ||
69 strncasecmp(line, "workspace_layout", strlen("workspace_layout")) == 0) {
70 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
74 /* if this is a bind statement, we can check the command */
75 if (strncasecmp(line, "bind", strlen("bind")) == 0) {
76 char *bind = strchr(line, ' ');
79 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
83 if ((bind = strchr(bind, ' ')) == NULL)
85 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
89 if (strncasecmp(bind, "layout", strlen("layout")) == 0 ||
90 strncasecmp(bind, "floating", strlen("floating")) == 0 ||
91 strncasecmp(bind, "workspace", strlen("workspace")) == 0 ||
92 strncasecmp(bind, "focus left", strlen("focus left")) == 0 ||
93 strncasecmp(bind, "focus right", strlen("focus right")) == 0 ||
94 strncasecmp(bind, "focus up", strlen("focus up")) == 0 ||
95 strncasecmp(bind, "focus down", strlen("focus down")) == 0 ||
96 strncasecmp(bind, "border normal", strlen("border normal")) == 0 ||
97 strncasecmp(bind, "border 1pixel", strlen("border 1pixel")) == 0 ||
98 strncasecmp(bind, "border borderless", strlen("border borderless")) == 0) {
99 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
105 /* advance to the next line */
114 * Calls i3-migrate-config-to-v4.pl to migrate a configuration file (input
117 * Returns the converted config file or NULL if there was an error (for
118 * example the script could not be found in $PATH or the i3 executable’s
122 static char *migrate_config(char *input, off_t size) {
126 if (pipe(writepipe) != 0 ||
127 pipe(readpipe) != 0) {
128 warn("migrate_config: Could not create pipes");
134 warn("Could not fork()");
140 /* close writing end of writepipe, connect reading side to stdin */
142 dup2(writepipe[0], 0);
144 /* close reading end of readpipe, connect writing side to stdout */
146 dup2(readpipe[1], 1);
148 /* start the migration script, search PATH first */
149 char *migratepath = "i3-migrate-config-to-v4.pl";
150 execlp(migratepath, migratepath, NULL);
152 /* if the script is not in path, maybe the user installed to a strange
153 * location and runs the i3 binary with an absolute path. We use
154 * argv[0]’s dirname */
155 char *pathbuf = strdup(start_argv[0]);
156 char *dir = dirname(pathbuf);
157 asprintf(&migratepath, "%s/%s", dir, "i3-migrate-config-to-v4.pl");
158 execlp(migratepath, migratepath, NULL);
160 #if defined(__linux__)
161 /* on linux, we have one more fall-back: dirname(/proc/self/exe) */
163 if (readlink("/proc/self/exe", buffer, BUFSIZ) == -1) {
164 warn("could not read /proc/self/exe");
167 dir = dirname(buffer);
168 asprintf(&migratepath, "%s/%s", dir, "i3-migrate-config-to-v4.pl");
169 execlp(migratepath, migratepath, NULL);
172 warn("Could not start i3-migrate-config-to-v4.pl");
178 /* close reading end of the writepipe (connected to the script’s stdin) */
181 /* write the whole config file to the pipe, the script will read everything
185 while (written < size) {
186 if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
187 warn("Could not write to pipe");
194 /* close writing end of the readpipe (connected to the script’s stdout) */
197 /* read the script’s output */
198 int conv_size = 65535;
199 char *converted = malloc(conv_size);
202 if (read_bytes == conv_size) {
204 converted = realloc(converted, conv_size);
206 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
208 warn("Cannot read from pipe");
214 /* get the returncode */
217 if (!WIFEXITED(status)) {
218 fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
222 int returncode = WEXITSTATUS(status);
223 if (returncode != 0) {
224 fprintf(stderr, "Migration process exit code was != 0\n");
225 if (returncode == 2) {
226 fprintf(stderr, "could not start the migration script\n");
227 /* TODO: script was not found. tell the user to fix his system or create a v4 config */
228 } else if (returncode == 1) {
229 fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
230 fprintf(stderr, "# i3 config file (v4)\n");
231 /* TODO: nag the user with a message to include a hint for i3 in his config file */
239 void parse_file(const char *f) {
240 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
241 int fd, ret, read_bytes = 0;
245 char buffer[1026], key[512], value[512];
247 if ((fd = open(f, O_RDONLY)) == -1)
248 die("Could not open configuration file: %s\n", strerror(errno));
250 if (fstat(fd, &stbuf) == -1)
251 die("Could not fstat file: %s\n", strerror(errno));
253 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
254 while (read_bytes < stbuf.st_size) {
255 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
256 die("Could not read(): %s\n", strerror(errno));
260 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
261 die("Could not lseek: %s\n", strerror(errno));
263 if ((fstr = fdopen(fd, "r")) == NULL)
264 die("Could not fdopen: %s\n", strerror(errno));
266 while (!feof(fstr)) {
267 if (fgets(buffer, 1024, fstr) == NULL) {
270 die("Could not read configuration file\n");
273 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
274 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
275 key[0] == '#' || strlen(key) < 3)
278 if (strcasecmp(key, "set") == 0) {
280 die("Malformed variable assignment, name has to start with $\n");
282 /* get key/value for this variable */
283 char *v_key = value, *v_value;
284 if ((v_value = strstr(value, " ")) == NULL)
285 die("Malformed variable assignment, need a value\n");
289 struct Variable *new = scalloc(sizeof(struct Variable));
290 new->key = sstrdup(v_key);
291 new->value = sstrdup(v_value);
292 SLIST_INSERT_HEAD(&variables, new, variables);
293 DLOG("Got new variable %s = %s\n", v_key, v_value);
299 /* For every custom variable, see how often it occurs in the file and
300 * how much extra bytes it requires when replaced. */
301 struct Variable *current, *nearest;
303 /* We need to copy the buffer because we need to invalidate the
304 * variables (otherwise we will count them twice, which is bad when
305 * 'extra' is negative) */
306 char *bufcopy = sstrdup(buf);
307 SLIST_FOREACH(current, &variables, variables) {
308 int extra = (strlen(current->value) - strlen(current->key));
311 (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
312 next += strlen(current->key)) {
314 extra_bytes += extra;
319 /* Then, allocate a new buffer and copy the file over to the new one,
320 * but replace occurences of our variables */
321 char *walk = buf, *destwalk;
322 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
324 while (walk < (buf + stbuf.st_size)) {
325 /* Find the next variable */
326 SLIST_FOREACH(current, &variables, variables)
327 current->next_match = strcasestr(walk, current->key);
329 int distance = stbuf.st_size;
330 SLIST_FOREACH(current, &variables, variables) {
331 if (current->next_match == NULL)
333 if ((current->next_match - walk) < distance) {
334 distance = (current->next_match - walk);
338 if (nearest == NULL) {
339 /* If there are no more variables, we just copy the rest */
340 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
341 destwalk += (buf + stbuf.st_size) - walk;
345 /* Copy until the next variable, then copy its value */
346 strncpy(destwalk, walk, distance);
347 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
348 walk += distance + strlen(nearest->key);
349 destwalk += distance + strlen(nearest->value);
353 /* analyze the string to find out whether this is an old config file (3.x)
354 * or a new config file (4.x). If it’s old, we run the converter script. */
355 int version = detect_version(buf);
357 /* We need to convert this v3 configuration */
358 char *converted = migrate_config(new, stbuf.st_size);
359 if (converted != NULL) {
361 printf("****************************************************************\n");
362 printf("NOTE: Automatically converted configuration file from v3 to v4.\n");
364 printf("Please convert your config file to v4. You can use this command:\n");
365 printf(" mv %s %s.O\n", f, f);
366 printf(" i3-migrate-config-to-v4.pl %s.O > %s\n", f, f);
367 printf("****************************************************************\n");
373 printf("**********************************************************************\n");
374 printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4.pl\n");
375 printf("was not correctly installed on your system?\n");
376 printf("**********************************************************************\n");
381 /* now lex/parse it */
384 context = scalloc(sizeof(struct context));
385 context->filename = f;
387 if (yyparse() != 0) {
388 fprintf(stderr, "Could not parse configfile\n");
392 FREE(context->line_copy);
397 while (!SLIST_EMPTY(&variables)) {
398 current = SLIST_FIRST(&variables);
400 FREE(current->value);
401 SLIST_REMOVE_HEAD(&variables, variables);
409 %lex-param { struct context *context }
414 uint32_t *single_color;
415 struct Colortriple *color;
417 struct Binding *binding;
420 %token <number> NUMBER "<number>"
421 %token <string> WORD "<word>"
422 %token <string> STR "<string>"
423 %token <string> STR_NG "<string (non-greedy)>"
424 %token <string> HEX "<hex>"
425 %token <string> OUTPUT "<RandR output>"
428 %token TOKCOMMENT "<comment>"
429 %token TOKFONT "font"
430 %token TOKBINDSYM "bindsym"
431 %token <number> MODIFIER "<modifier>"
432 %token TOKCONTROL "control"
433 %token TOKSHIFT "shift"
434 %token TOKFLOATING_MODIFIER "floating_modifier"
435 %token <string> QUOTEDSTRING "<quoted string>"
436 %token TOKWORKSPACE "workspace"
437 %token TOKOUTPUT "output"
438 %token TOKASSIGN "assign"
440 %token TOKIPCSOCKET "ipc_socket"
441 %token TOKRESTARTSTATE "restart_state"
442 %token TOKEXEC "exec"
443 %token <single_color> TOKSINGLECOLOR
444 %token <color> TOKCOLOR
446 %token TOKMODE "mode"
447 %token TOK_ORIENTATION "default_orientation"
448 %token TOK_HORIZ "horizontal"
449 %token TOK_VERT "vertical"
450 %token TOK_AUTO "auto"
451 %token TOK_WORKSPACE_LAYOUT "workspace_layout"
452 %token TOKNEWWINDOW "new_window"
453 %token TOK_NORMAL "normal"
454 %token TOK_NONE "none"
455 %token TOK_1PIXEL "1pixel"
456 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
457 %token TOK_FORCE_FOCUS_WRAPPING "force_focus_wrapping"
458 %token TOKWORKSPACEBAR "workspace_bar"
459 %token TOK_DEFAULT "default"
460 %token TOK_STACKING "stacking"
461 %token TOK_TABBED "tabbed"
462 %token <number> TOKSTACKLIMIT "stack-limit"
463 %token TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
464 %token TOK_IGNORE "ignore"
465 %token TOK_LEAVE_FULLSCREEN "leave_fullscreen"
466 %token TOK_FOR_WINDOW "for_window"
468 %token TOK_MARK "mark"
469 %token TOK_CLASS "class"
471 %token TOK_CON_ID "con_id"
472 %token TOK_TITLE "title"
474 %type <binding> binding
475 %type <binding> bindcode
476 %type <binding> bindsym
477 %type <number> binding_modifiers
478 %type <number> binding_modifier
479 %type <number> direction
480 %type <number> layout_mode
481 %type <number> border_style
482 %type <number> new_window
483 %type <number> colorpixel
485 %type <number> popup_setting
486 %type <string> command
487 %type <string> word_or_number
488 %type <string> optional_workspace_name
489 %type <string> workspace_name
490 %type <string> window_class
507 | focus_follows_mouse
508 | force_focus_wrapping
520 | popup_during_fullscreen
534 TAILQ_INSERT_TAIL(bindings, $1, bindings);
539 TOKBINDCODE bindcode { $$ = $2; }
540 | TOKBINDSYM bindsym { $$ = $2; }
544 binding_modifiers NUMBER command
546 printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
547 Binding *new = scalloc(sizeof(Binding));
558 binding_modifiers word_or_number command
560 printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
561 Binding *new = scalloc(sizeof(Binding));
572 TOK_FOR_WINDOW match command
574 printf("\t should execute command %s for the criteria mentioned above\n", $3);
575 Assignment *assignment = scalloc(sizeof(Assignment));
576 assignment->type = A_COMMAND;
577 assignment->match = current_match;
578 assignment->dest.command = $3;
579 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
584 | matchstart criteria matchend
586 printf("match parsed\n");
594 match_init(¤t_match);
601 printf("match specification finished\n");
608 printf("criteria: class = %s\n", $3);
609 current_match.class = $3;
613 printf("criteria: id = %s\n", $3);
615 long parsed = strtol($3, &end, 10);
616 if (parsed == LONG_MIN ||
617 parsed == LONG_MAX ||
619 (end && *end != '\0')) {
620 ELOG("Could not parse con id \"%s\"\n", $3);
622 current_match.con_id = (Con*)parsed;
623 printf("id as int = %p\n", current_match.con_id);
628 printf("criteria: window id = %s\n", $3);
630 long parsed = strtol($3, &end, 10);
631 if (parsed == LONG_MIN ||
632 parsed == LONG_MAX ||
634 (end && *end != '\0')) {
635 ELOG("Could not parse window id \"%s\"\n", $3);
637 current_match.id = parsed;
638 printf("window id as int = %d\n", current_match.id);
643 printf("criteria: mark = %s\n", $3);
644 current_match.mark = $3;
648 printf("criteria: title = %s\n", $3);
649 current_match.title = $3;
659 asprintf(&$$, "%d", $1);
664 TOKMODE QUOTEDSTRING '{' modelines '}'
666 if (strcasecmp($2, "default") == 0) {
667 printf("You cannot use the name \"default\" for your mode\n");
670 printf("\t now in mode %s\n", $2);
671 printf("\t current bindings = %p\n", current_bindings);
673 TAILQ_FOREACH(binding, current_bindings, bindings) {
674 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
675 binding->mods, binding->keycode, binding->symbol, binding->command);
678 struct Mode *mode = scalloc(sizeof(struct Mode));
680 mode->bindings = current_bindings;
681 current_bindings = NULL;
682 SLIST_INSERT_HEAD(&modes, mode, modes);
696 if (current_bindings == NULL) {
697 current_bindings = scalloc(sizeof(struct bindings_head));
698 TAILQ_INIT(current_bindings);
701 TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
706 TOKFLOATING_MODIFIER binding_modifiers
708 DLOG("floating modifier = %d\n", $2);
709 config.floating_modifier = $2;
714 TOK_ORIENTATION direction
716 DLOG("New containers should start with split direction %d\n", $2);
717 config.default_orientation = $2;
722 TOK_HORIZ { $$ = HORIZ; }
723 | TOK_VERT { $$ = VERT; }
724 | TOK_AUTO { $$ = NO_ORIENTATION; }
728 TOK_WORKSPACE_LAYOUT layout_mode
730 DLOG("new containers will be in mode %d\n", $2);
731 config.default_layout = $2;
734 /* We also need to change the layout of the already existing
735 * workspaces here. Workspaces may exist at this point because
736 * of the other directives which are modifying workspaces
737 * (setting the preferred screen or name). While the workspace
738 * objects are already created, they have never been used.
739 * Thus, the user very likely awaits the default container mode
740 * to trigger in this case, regardless of where it is inside
741 * his configuration file. */
743 TAILQ_FOREACH(ws, workspaces, workspaces) {
744 if (ws->table == NULL)
746 switch_layout_mode(global_conn,
748 config.container_mode);
752 | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
754 DLOG("stack-limit %d with val %d\n", $3, $4);
755 config.container_stack_limit = $3;
756 config.container_stack_limit_value = $4;
759 /* See the comment above */
761 TAILQ_FOREACH(ws, workspaces, workspaces) {
762 if (ws->table == NULL)
764 Container *con = ws->table[0][0];
765 con->stack_limit = config.container_stack_limit;
766 con->stack_limit_value = config.container_stack_limit_value;
773 TOK_DEFAULT { $$ = L_DEFAULT; }
774 | TOK_STACKING { $$ = L_STACKED; }
775 | TOK_TABBED { $$ = L_TABBED; }
779 TOKNEWWINDOW border_style
781 DLOG("new windows should start with border style %d\n", $2);
782 config.default_border = $2;
787 TOK_NORMAL { $$ = BS_NORMAL; }
788 | TOK_NONE { $$ = BS_NONE; }
789 | TOK_1PIXEL { $$ = BS_1PIXEL; }
799 DLOG("checking word \"%s\"\n", $1);
800 $$ = (strcasecmp($1, "yes") == 0 ||
801 strcasecmp($1, "true") == 0 ||
802 strcasecmp($1, "on") == 0 ||
803 strcasecmp($1, "enable") == 0 ||
804 strcasecmp($1, "active") == 0);
809 TOKFOCUSFOLLOWSMOUSE bool
811 DLOG("focus follows mouse = %d\n", $2);
812 config.disable_focus_follows_mouse = !($2);
816 force_focus_wrapping:
817 TOK_FORCE_FOCUS_WRAPPING bool
819 DLOG("force focus wrapping = %d\n", $2);
820 config.force_focus_wrapping = $2;
827 DLOG("workspace bar = %d\n", $2);
828 config.disable_workspace_bar = !($2);
833 TOKWORKSPACE NUMBER TOKOUTPUT OUTPUT optional_workspace_name
837 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
839 char *ws_name = NULL;
841 asprintf(&ws_name, "%d", ws_num);
846 DLOG("Should assign workspace %s to output %s\n", ws_name, $4);
847 struct Workspace_Assignment *assignment = scalloc(sizeof(struct Workspace_Assignment));
848 assignment->name = ws_name;
849 assignment->output = $4;
850 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
853 | TOKWORKSPACE NUMBER workspace_name
857 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
859 DLOG("workspace name to: %s\n", $3);
861 if ($<string>3 != NULL) {
862 workspace_set_name(workspace_get(ws_num - 1), $<string>3);
870 optional_workspace_name:
871 /* empty */ { $$ = NULL; }
872 | workspace_name { $$ = $1; }
876 QUOTEDSTRING { $$ = $1; }
882 TOKASSIGN window_class STR
884 printf("assignment of %s to *%s*\n", $2, $3);
885 char *workspace = $3;
888 Assignment *assignment = scalloc(sizeof(Assignment));
889 Match *match = &(assignment->match);
892 char *separator = NULL;
893 if ((separator = strchr(criteria, '/')) != NULL) {
894 *(separator++) = '\0';
895 match->title = sstrdup(separator);
897 if (*criteria != '\0')
898 match->class = sstrdup(criteria);
901 printf(" class = %s\n", match->class);
902 printf(" title = %s\n", match->title);
904 /* Compatibility with older versions: If the assignment target starts
905 * with ~, we create the equivalent of:
907 * for_window [class="foo"] mode floating
909 if (*workspace == '~') {
911 if (*workspace == '\0') {
912 /* This assignment was *only* for floating */
913 assignment->type = A_COMMAND;
914 assignment->dest.command = sstrdup("floating enable");
915 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
918 /* Create a new assignment and continue afterwards */
919 Assignment *floating = scalloc(sizeof(Assignment));
920 match_copy(&(floating->match), match);
921 floating->type = A_COMMAND;
922 floating->dest.command = sstrdup("floating enable");
923 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
927 assignment->type = A_TO_WORKSPACE;
928 assignment->dest.workspace = workspace;
929 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
941 config.ipc_socket_path = $2;
948 config.restart_state_path = $2;
955 struct Autostart *new = smalloc(sizeof(struct Autostart));
957 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
964 ELOG("The terminal option is DEPRECATED and has no effect. "
965 "Please remove it from your configuration file.\n");
972 config.font = load_font($2, true);
973 printf("font %s\n", $2);
978 TOKSINGLECOLOR colorpixel
986 TOKCOLOR colorpixel colorpixel colorpixel
988 struct Colortriple *dest = $1;
991 dest->background = $3;
1000 if (asprintf(&hex, "#%s", $2) == -1)
1002 $$ = get_colorpixel(hex);
1009 /* NULL */ { $$ = 0; }
1011 | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1012 | binding_modifiers '+' { $$ = $1; }
1016 MODIFIER { $$ = $1; }
1017 | TOKCONTROL { $$ = BIND_CONTROL; }
1018 | TOKSHIFT { $$ = BIND_SHIFT; }
1021 popup_during_fullscreen:
1022 TOK_POPUP_DURING_FULLSCREEN popup_setting
1024 DLOG("popup_during_fullscreen setting: %d\n", $2);
1025 config.popup_during_fullscreen = $2;
1030 TOK_IGNORE { $$ = PDF_IGNORE; }
1031 | TOK_LEAVE_FULLSCREEN { $$ = PDF_LEAVE_FULLSCREEN; }