3 * vim:ts=4:sw=4:expandtab
14 static Match current_match;
16 typedef struct yy_buffer_state *YY_BUFFER_STATE;
17 extern int yylex(struct context *context);
18 extern int yyparse(void);
20 YY_BUFFER_STATE yy_scan_string(const char *);
22 static struct bindings_head *current_bindings;
23 static struct context *context;
25 /* We don’t need yydebug for now, as we got decent error messages using
26 * yyerror(). Should you ever want to extend the parser, it might be handy
27 * to just comment it in again, so it stays here. */
30 void yyerror(const char *error_message) {
32 ELOG("CONFIG: %s\n", error_message);
33 ELOG("CONFIG: in file \"%s\", line %d:\n",
34 context->filename, context->line_number);
35 ELOG("CONFIG: %s\n", context->line_copy);
37 for (int c = 1; c <= context->last_column; c++)
38 if (c >= context->first_column)
49 void parse_file(const char *f) {
50 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
51 int fd, ret, read_bytes = 0;
55 char buffer[1026], key[512], value[512];
57 if ((fd = open(f, O_RDONLY)) == -1)
58 die("Could not open configuration file: %s\n", strerror(errno));
60 if (fstat(fd, &stbuf) == -1)
61 die("Could not fstat file: %s\n", strerror(errno));
63 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
64 while (read_bytes < stbuf.st_size) {
65 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
66 die("Could not read(): %s\n", strerror(errno));
70 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
71 die("Could not lseek: %s\n", strerror(errno));
73 if ((fstr = fdopen(fd, "r")) == NULL)
74 die("Could not fdopen: %s\n", strerror(errno));
77 if (fgets(buffer, 1024, fstr) == NULL) {
80 die("Could not read configuration file\n");
83 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
84 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
85 key[0] == '#' || strlen(key) < 3)
88 if (strcasecmp(key, "set") == 0) {
90 die("Malformed variable assignment, name has to start with $\n");
92 /* get key/value for this variable */
93 char *v_key = value, *v_value;
94 if ((v_value = strstr(value, " ")) == NULL)
95 die("Malformed variable assignment, need a value\n");
99 struct Variable *new = scalloc(sizeof(struct Variable));
100 new->key = sstrdup(v_key);
101 new->value = sstrdup(v_value);
102 SLIST_INSERT_HEAD(&variables, new, variables);
103 DLOG("Got new variable %s = %s\n", v_key, v_value);
109 /* For every custom variable, see how often it occurs in the file and
110 * how much extra bytes it requires when replaced. */
111 struct Variable *current, *nearest;
113 /* We need to copy the buffer because we need to invalidate the
114 * variables (otherwise we will count them twice, which is bad when
115 * 'extra' is negative) */
116 char *bufcopy = sstrdup(buf);
117 SLIST_FOREACH(current, &variables, variables) {
118 int extra = (strlen(current->value) - strlen(current->key));
121 (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
122 next += strlen(current->key)) {
124 extra_bytes += extra;
129 /* Then, allocate a new buffer and copy the file over to the new one,
130 * but replace occurences of our variables */
131 char *walk = buf, *destwalk;
132 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
134 while (walk < (buf + stbuf.st_size)) {
135 /* Find the next variable */
136 SLIST_FOREACH(current, &variables, variables)
137 current->next_match = strcasestr(walk, current->key);
139 int distance = stbuf.st_size;
140 SLIST_FOREACH(current, &variables, variables) {
141 if (current->next_match == NULL)
143 if ((current->next_match - walk) < distance) {
144 distance = (current->next_match - walk);
148 if (nearest == NULL) {
149 /* If there are no more variables, we just copy the rest */
150 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
151 destwalk += (buf + stbuf.st_size) - walk;
155 /* Copy until the next variable, then copy its value */
156 strncpy(destwalk, walk, distance);
157 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
158 walk += distance + strlen(nearest->key);
159 destwalk += distance + strlen(nearest->value);
165 context = scalloc(sizeof(struct context));
166 context->filename = f;
168 if (yyparse() != 0) {
169 fprintf(stderr, "Could not parse configfile\n");
173 FREE(context->line_copy);
178 while (!SLIST_EMPTY(&variables)) {
179 current = SLIST_FIRST(&variables);
181 FREE(current->value);
182 SLIST_REMOVE_HEAD(&variables, variables);
190 %lex-param { struct context *context }
195 uint32_t *single_color;
196 struct Colortriple *color;
198 struct Binding *binding;
201 %token <number> NUMBER "<number>"
202 %token <string> WORD "<word>"
203 %token <string> STR "<string>"
204 %token <string> STR_NG "<string (non-greedy)>"
205 %token <string> HEX "<hex>"
206 %token <string> OUTPUT "<RandR output>"
209 %token TOKCOMMENT "<comment>"
210 %token TOKFONT "font"
211 %token TOKBINDSYM "bindsym"
212 %token <number> MODIFIER "<modifier>"
213 %token TOKCONTROL "control"
214 %token TOKSHIFT "shift"
215 %token TOKFLOATING_MODIFIER "floating_modifier"
216 %token <string> QUOTEDSTRING "<quoted string>"
217 %token TOKWORKSPACE "workspace"
218 %token TOKOUTPUT "output"
219 %token TOKASSIGN "assign"
221 %token TOKIPCSOCKET "ipc_socket"
222 %token TOKRESTARTSTATE "restart_state"
223 %token TOKEXEC "exec"
224 %token <single_color> TOKSINGLECOLOR
225 %token <color> TOKCOLOR
227 %token TOKMODE "mode"
228 %token TOK_ORIENTATION "default_orientation"
229 %token TOK_HORIZ "horizontal"
230 %token TOK_VERT "vertical"
231 %token TOK_AUTO "auto"
232 %token TOK_WORKSPACE_LAYOUT "workspace_layout"
233 %token TOKNEWWINDOW "new_window"
234 %token TOK_NORMAL "normal"
235 %token TOK_NONE "none"
236 %token TOK_1PIXEL "1pixel"
237 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
238 %token TOK_FORCE_FOCUS_WRAPPING "force_focus_wrapping"
239 %token TOKWORKSPACEBAR "workspace_bar"
240 %token TOK_DEFAULT "default"
241 %token TOK_STACKING "stacking"
242 %token TOK_TABBED "tabbed"
243 %token <number> TOKSTACKLIMIT "stack-limit"
244 %token TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
245 %token TOK_IGNORE "ignore"
246 %token TOK_LEAVE_FULLSCREEN "leave_fullscreen"
247 %token TOK_FOR_WINDOW "for_window"
249 %token TOK_MARK "mark"
250 %token TOK_CLASS "class"
252 %token TOK_CON_ID "con_id"
253 %token TOK_TITLE "title"
255 %type <binding> binding
256 %type <binding> bindcode
257 %type <binding> bindsym
258 %type <number> binding_modifiers
259 %type <number> binding_modifier
260 %type <number> direction
261 %type <number> layout_mode
262 %type <number> border_style
263 %type <number> new_window
264 %type <number> colorpixel
266 %type <number> popup_setting
267 %type <string> command
268 %type <string> word_or_number
269 %type <string> optional_workspace_name
270 %type <string> workspace_name
271 %type <string> window_class
288 | focus_follows_mouse
289 | force_focus_wrapping
301 | popup_during_fullscreen
315 TAILQ_INSERT_TAIL(bindings, $1, bindings);
320 TOKBINDCODE bindcode { $$ = $2; }
321 | TOKBINDSYM bindsym { $$ = $2; }
325 binding_modifiers NUMBER command
327 printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
328 Binding *new = scalloc(sizeof(Binding));
339 binding_modifiers word_or_number command
341 printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
342 Binding *new = scalloc(sizeof(Binding));
353 TOK_FOR_WINDOW match command
355 printf("\t should execute command %s for the criteria mentioned above\n", $3);
356 Assignment *assignment = scalloc(sizeof(Assignment));
357 assignment->type = A_COMMAND;
358 assignment->match = current_match;
359 assignment->dest.command = $3;
360 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
365 | matchstart criteria matchend
367 printf("match parsed\n");
375 match_init(¤t_match);
382 printf("match specification finished\n");
389 printf("criteria: class = %s\n", $3);
390 current_match.class = $3;
394 printf("criteria: id = %s\n", $3);
396 long parsed = strtol($3, &end, 10);
397 if (parsed == LONG_MIN ||
398 parsed == LONG_MAX ||
400 (end && *end != '\0')) {
401 ELOG("Could not parse con id \"%s\"\n", $3);
403 current_match.con_id = (Con*)parsed;
404 printf("id as int = %p\n", current_match.con_id);
409 printf("criteria: window id = %s\n", $3);
411 long parsed = strtol($3, &end, 10);
412 if (parsed == LONG_MIN ||
413 parsed == LONG_MAX ||
415 (end && *end != '\0')) {
416 ELOG("Could not parse window id \"%s\"\n", $3);
418 current_match.id = parsed;
419 printf("window id as int = %d\n", current_match.id);
424 printf("criteria: mark = %s\n", $3);
425 current_match.mark = $3;
429 printf("criteria: title = %s\n", $3);
430 current_match.title = $3;
440 asprintf(&$$, "%d", $1);
445 TOKMODE QUOTEDSTRING '{' modelines '}'
447 if (strcasecmp($2, "default") == 0) {
448 printf("You cannot use the name \"default\" for your mode\n");
451 printf("\t now in mode %s\n", $2);
452 printf("\t current bindings = %p\n", current_bindings);
454 TAILQ_FOREACH(binding, current_bindings, bindings) {
455 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
456 binding->mods, binding->keycode, binding->symbol, binding->command);
459 struct Mode *mode = scalloc(sizeof(struct Mode));
461 mode->bindings = current_bindings;
462 current_bindings = NULL;
463 SLIST_INSERT_HEAD(&modes, mode, modes);
477 if (current_bindings == NULL) {
478 current_bindings = scalloc(sizeof(struct bindings_head));
479 TAILQ_INIT(current_bindings);
482 TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
487 TOKFLOATING_MODIFIER binding_modifiers
489 DLOG("floating modifier = %d\n", $2);
490 config.floating_modifier = $2;
495 TOK_ORIENTATION direction
497 DLOG("New containers should start with split direction %d\n", $2);
498 config.default_orientation = $2;
503 TOK_HORIZ { $$ = HORIZ; }
504 | TOK_VERT { $$ = VERT; }
505 | TOK_AUTO { $$ = NO_ORIENTATION; }
509 TOK_WORKSPACE_LAYOUT layout_mode
511 DLOG("new containers will be in mode %d\n", $2);
512 config.default_layout = $2;
515 /* We also need to change the layout of the already existing
516 * workspaces here. Workspaces may exist at this point because
517 * of the other directives which are modifying workspaces
518 * (setting the preferred screen or name). While the workspace
519 * objects are already created, they have never been used.
520 * Thus, the user very likely awaits the default container mode
521 * to trigger in this case, regardless of where it is inside
522 * his configuration file. */
524 TAILQ_FOREACH(ws, workspaces, workspaces) {
525 if (ws->table == NULL)
527 switch_layout_mode(global_conn,
529 config.container_mode);
533 | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
535 DLOG("stack-limit %d with val %d\n", $3, $4);
536 config.container_stack_limit = $3;
537 config.container_stack_limit_value = $4;
540 /* See the comment above */
542 TAILQ_FOREACH(ws, workspaces, workspaces) {
543 if (ws->table == NULL)
545 Container *con = ws->table[0][0];
546 con->stack_limit = config.container_stack_limit;
547 con->stack_limit_value = config.container_stack_limit_value;
554 TOK_DEFAULT { $$ = L_DEFAULT; }
555 | TOK_STACKING { $$ = L_STACKED; }
556 | TOK_TABBED { $$ = L_TABBED; }
560 TOKNEWWINDOW border_style
562 DLOG("new windows should start with border style %d\n", $2);
563 config.default_border = $2;
568 TOK_NORMAL { $$ = BS_NORMAL; }
569 | TOK_NONE { $$ = BS_NONE; }
570 | TOK_1PIXEL { $$ = BS_1PIXEL; }
580 DLOG("checking word \"%s\"\n", $1);
581 $$ = (strcasecmp($1, "yes") == 0 ||
582 strcasecmp($1, "true") == 0 ||
583 strcasecmp($1, "on") == 0 ||
584 strcasecmp($1, "enable") == 0 ||
585 strcasecmp($1, "active") == 0);
590 TOKFOCUSFOLLOWSMOUSE bool
592 DLOG("focus follows mouse = %d\n", $2);
593 config.disable_focus_follows_mouse = !($2);
597 force_focus_wrapping:
598 TOK_FORCE_FOCUS_WRAPPING bool
600 DLOG("force focus wrapping = %d\n", $2);
601 config.force_focus_wrapping = $2;
608 DLOG("workspace bar = %d\n", $2);
609 config.disable_workspace_bar = !($2);
614 TOKWORKSPACE NUMBER TOKOUTPUT OUTPUT optional_workspace_name
618 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
620 char *ws_name = NULL;
622 asprintf(&ws_name, "%d", ws_num);
627 DLOG("Should assign workspace %s to output %s\n", ws_name, $4);
628 struct Workspace_Assignment *assignment = scalloc(sizeof(struct Workspace_Assignment));
629 assignment->name = ws_name;
630 assignment->output = $4;
631 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
634 | TOKWORKSPACE NUMBER workspace_name
638 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
640 DLOG("workspace name to: %s\n", $3);
642 if ($<string>3 != NULL) {
643 workspace_set_name(workspace_get(ws_num - 1), $<string>3);
651 optional_workspace_name:
652 /* empty */ { $$ = NULL; }
653 | workspace_name { $$ = $1; }
657 QUOTEDSTRING { $$ = $1; }
663 TOKASSIGN window_class STR
665 printf("assignment of %s to *%s*\n", $2, $3);
666 char *workspace = $3;
669 Assignment *assignment = scalloc(sizeof(Assignment));
670 Match *match = &(assignment->match);
673 char *separator = NULL;
674 if ((separator = strchr(criteria, '/')) != NULL) {
675 *(separator++) = '\0';
676 match->title = sstrdup(separator);
678 if (*criteria != '\0')
679 match->class = sstrdup(criteria);
682 printf(" class = %s\n", match->class);
683 printf(" title = %s\n", match->title);
685 /* Compatibility with older versions: If the assignment target starts
686 * with ~, we create the equivalent of:
688 * for_window [class="foo"] mode floating
690 if (*workspace == '~') {
692 if (*workspace == '\0') {
693 /* This assignment was *only* for floating */
694 assignment->type = A_COMMAND;
695 assignment->dest.command = sstrdup("mode floating");
696 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
699 /* Create a new assignment and continue afterwards */
700 Assignment *floating = scalloc(sizeof(Assignment));
701 match_copy(&(floating->match), match);
702 floating->type = A_COMMAND;
703 floating->dest.command = sstrdup("mode floating");
704 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
708 assignment->type = A_TO_WORKSPACE;
709 assignment->dest.workspace = workspace;
710 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
722 config.ipc_socket_path = $2;
729 config.restart_state_path = $2;
736 struct Autostart *new = smalloc(sizeof(struct Autostart));
738 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
745 ELOG("The terminal option is DEPRECATED and has no effect. "
746 "Please remove it from your configuration file.\n");
753 config.font = load_font($2, true);
754 printf("font %s\n", $2);
759 TOKSINGLECOLOR colorpixel
767 TOKCOLOR colorpixel colorpixel colorpixel
769 struct Colortriple *dest = $1;
772 dest->background = $3;
781 if (asprintf(&hex, "#%s", $2) == -1)
783 $$ = get_colorpixel(hex);
790 /* NULL */ { $$ = 0; }
792 | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
793 | binding_modifiers '+' { $$ = $1; }
797 MODIFIER { $$ = $1; }
798 | TOKCONTROL { $$ = BIND_CONTROL; }
799 | TOKSHIFT { $$ = BIND_SHIFT; }
802 popup_during_fullscreen:
803 TOK_POPUP_DURING_FULLSCREEN popup_setting
805 DLOG("popup_during_fullscreen setting: %d\n", $2);
806 config.popup_during_fullscreen = $2;
811 TOK_IGNORE { $$ = PDF_IGNORE; }
812 | TOK_LEAVE_FULLSCREEN { $$ = PDF_LEAVE_FULLSCREEN; }