13 typedef struct yy_buffer_state *YY_BUFFER_STATE;
14 extern int yylex(struct context *context);
15 extern int yyparse(void);
17 YY_BUFFER_STATE yy_scan_string(const char *);
19 static struct bindings_head *current_bindings;
20 static struct context *context;
22 /* We don’t need yydebug for now, as we got decent error messages using
23 * yyerror(). Should you ever want to extend the parser, it might be handy
24 * to just comment it in again, so it stays here. */
27 void yyerror(const char *error_message) {
29 ELOG("CONFIG: %s\n", error_message);
30 ELOG("CONFIG: in file \"%s\", line %d:\n",
31 context->filename, context->line_number);
32 ELOG("CONFIG: %s\n", context->line_copy);
34 for (int c = 1; c <= context->last_column; c++)
35 if (c >= context->first_column)
46 void parse_file(const char *f) {
47 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
48 int fd, ret, read_bytes = 0;
52 char buffer[1026], key[512], value[512];
54 if ((fd = open(f, O_RDONLY)) == -1)
55 die("Could not open configuration file: %s\n", strerror(errno));
57 if (fstat(fd, &stbuf) == -1)
58 die("Could not fstat file: %s\n", strerror(errno));
60 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
61 while (read_bytes < stbuf.st_size) {
62 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
63 die("Could not read(): %s\n", strerror(errno));
67 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
68 die("Could not lseek: %s\n", strerror(errno));
70 if ((fstr = fdopen(fd, "r")) == NULL)
71 die("Could not fdopen: %s\n", strerror(errno));
74 if (fgets(buffer, 1024, fstr) == NULL) {
77 die("Could not read configuration file\n");
80 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
81 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
82 key[0] == '#' || strlen(key) < 3)
85 if (strcasecmp(key, "set") == 0) {
87 die("Malformed variable assignment, name has to start with $\n");
89 /* get key/value for this variable */
90 char *v_key = value, *v_value;
91 if ((v_value = strstr(value, " ")) == NULL)
92 die("Malformed variable assignment, need a value\n");
96 struct Variable *new = scalloc(sizeof(struct Variable));
97 new->key = sstrdup(v_key);
98 new->value = sstrdup(v_value);
99 SLIST_INSERT_HEAD(&variables, new, variables);
100 DLOG("Got new variable %s = %s\n", v_key, v_value);
106 /* For every custom variable, see how often it occurs in the file and
107 * how much extra bytes it requires when replaced. */
108 struct Variable *current, *nearest;
110 /* We need to copy the buffer because we need to invalidate the
111 * variables (otherwise we will count them twice, which is bad when
112 * 'extra' is negative) */
113 char *bufcopy = sstrdup(buf);
114 SLIST_FOREACH(current, &variables, variables) {
115 int extra = (strlen(current->value) - strlen(current->key));
118 (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
119 next += strlen(current->key)) {
121 extra_bytes += extra;
126 /* Then, allocate a new buffer and copy the file over to the new one,
127 * but replace occurences of our variables */
128 char *walk = buf, *destwalk;
129 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
131 while (walk < (buf + stbuf.st_size)) {
132 /* Find the next variable */
133 SLIST_FOREACH(current, &variables, variables)
134 current->next_match = strcasestr(walk, current->key);
136 int distance = stbuf.st_size;
137 SLIST_FOREACH(current, &variables, variables) {
138 if (current->next_match == NULL)
140 if ((current->next_match - walk) < distance) {
141 distance = (current->next_match - walk);
145 if (nearest == NULL) {
146 /* If there are no more variables, we just copy the rest */
147 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
148 destwalk += (buf + stbuf.st_size) - walk;
152 /* Copy until the next variable, then copy its value */
153 strncpy(destwalk, walk, distance);
154 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
155 walk += distance + strlen(nearest->key);
156 destwalk += distance + strlen(nearest->value);
162 context = scalloc(sizeof(struct context));
163 context->filename = f;
165 if (yyparse() != 0) {
166 fprintf(stderr, "Could not parse configfile\n");
170 FREE(context->line_copy);
175 while (!SLIST_EMPTY(&variables)) {
176 current = SLIST_FIRST(&variables);
178 FREE(current->value);
179 SLIST_REMOVE_HEAD(&variables, variables);
188 %lex-param { struct context *context }
193 uint32_t *single_color;
194 struct Colortriple *color;
195 struct Assignment *assignment;
196 struct Binding *binding;
199 %token <number>NUMBER "<number>"
200 %token <string>WORD "<word>"
201 %token <string>STR "<string>"
202 %token <string>STR_NG "<string (non-greedy)>"
203 %token <string>HEX "<hex>"
204 %token <string>OUTPUT "<RandR output>"
207 %token TOKCOMMENT "<comment>"
208 %token TOKFONT "font"
209 %token TOKBINDSYM "bindsym"
210 %token MODIFIER "<modifier>"
211 %token TOKCONTROL "control"
212 %token TOKSHIFT "shift"
213 %token WHITESPACE "<whitespace>"
214 %token TOKFLOATING_MODIFIER "floating_modifier"
215 %token QUOTEDSTRING "<quoted string>"
216 %token TOKWORKSPACE "workspace"
217 %token TOKOUTPUT "output"
218 %token TOKASSIGN "assign"
220 %token TOKIPCSOCKET "ipc_socket"
221 %token TOKRESTARTSTATE "restart_state"
222 %token TOKEXEC "exec"
223 %token TOKSINGLECOLOR
226 %token TOKMODE "mode"
227 %token TOKNEWCONTAINER "new_container"
228 %token TOKNEWWINDOW "new_window"
229 %token TOK_NORMAL "normal"
230 %token TOK_NONE "none"
231 %token TOK_1PIXEL "1pixel"
232 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
233 %token TOKWORKSPACEBAR "workspace_bar"
234 %token TOKCONTAINERMODE "default/stacking/tabbed"
235 %token TOKSTACKLIMIT "stack-limit"
240 | lines WHITESPACE line
251 | focus_follows_mouse
276 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
281 TOKBIND WHITESPACE bind { $<binding>$ = $<binding>3; }
282 | TOKBINDSYM WHITESPACE bindsym { $<binding>$ = $<binding>3; }
286 binding_modifiers NUMBER WHITESPACE command
288 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
289 Binding *new = scalloc(sizeof(Binding));
291 new->keycode = $<number>2;
292 new->mods = $<number>1;
293 new->command = $<string>4;
300 binding_modifiers word_or_number WHITESPACE command
302 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
303 Binding *new = scalloc(sizeof(Binding));
305 new->symbol = $<string>2;
306 new->mods = $<number>1;
307 new->command = $<string>4;
317 asprintf(&$<string>$, "%d", $1);
322 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
324 if (strcasecmp($<string>3, "default") == 0) {
325 printf("You cannot use the name \"default\" for your mode\n");
328 printf("\t now in mode %s\n", $<string>3);
329 printf("\t current bindings = %p\n", current_bindings);
331 TAILQ_FOREACH(binding, current_bindings, bindings) {
332 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
333 binding->mods, binding->keycode, binding->symbol, binding->command);
336 struct Mode *mode = scalloc(sizeof(struct Mode));
337 mode->name = $<string>3;
338 mode->bindings = current_bindings;
339 current_bindings = NULL;
340 SLIST_INSERT_HEAD(&modes, mode, modes);
355 if (current_bindings == NULL) {
356 current_bindings = scalloc(sizeof(struct bindings_head));
357 TAILQ_INIT(current_bindings);
360 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
365 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
367 DLOG("floating modifier = %d\n", $<number>3);
368 config.floating_modifier = $<number>3;
373 TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
375 DLOG("new containers will be in mode %d\n", $<number>3);
376 config.container_mode = $<number>3;
379 /* We also need to change the layout of the already existing
380 * workspaces here. Workspaces may exist at this point because
381 * of the other directives which are modifying workspaces
382 * (setting the preferred screen or name). While the workspace
383 * objects are already created, they have never been used.
384 * Thus, the user very likely awaits the default container mode
385 * to trigger in this case, regardless of where it is inside
386 * his configuration file. */
388 TAILQ_FOREACH(ws, workspaces, workspaces) {
389 if (ws->table == NULL)
391 switch_layout_mode(global_conn,
393 config.container_mode);
397 | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
399 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
400 config.container_stack_limit = $<number>5;
401 config.container_stack_limit_value = $<number>7;
404 /* See the comment above */
406 TAILQ_FOREACH(ws, workspaces, workspaces) {
407 if (ws->table == NULL)
409 Container *con = ws->table[0][0];
410 con->stack_limit = config.container_stack_limit;
411 con->stack_limit_value = config.container_stack_limit_value;
418 TOKNEWWINDOW WHITESPACE border_style
420 DLOG("new windows should start with border style %d\n", $<number>3);
421 config.default_border = $<number>3;
426 TOK_NORMAL { $<number>$ = BS_NORMAL; }
427 | TOK_NONE { $<number>$ = BS_NONE; }
428 | TOK_1PIXEL { $<number>$ = BS_1PIXEL; }
434 $<number>$ = ($<number>1 == 1);
438 DLOG("checking word \"%s\"\n", $<string>1);
439 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
440 strcasecmp($<string>1, "true") == 0 ||
441 strcasecmp($<string>1, "on") == 0 ||
442 strcasecmp($<string>1, "enable") == 0 ||
443 strcasecmp($<string>1, "active") == 0);
448 TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
450 DLOG("focus follows mouse = %d\n", $<number>3);
451 config.disable_focus_follows_mouse = !($<number>3);
456 TOKWORKSPACEBAR WHITESPACE bool
458 DLOG("workspace bar = %d\n", $<number>3);
459 config.disable_workspace_bar = !($<number>3);
464 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
466 int ws_num = $<number>3;
468 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
471 Workspace *ws = workspace_get(ws_num - 1);
472 ws->preferred_output = $<string>7;
473 if ($<string>8 != NULL) {
474 workspace_set_name(ws, $<string>8);
480 | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
482 int ws_num = $<number>3;
484 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
486 DLOG("workspace name to: %s\n", $<string>5);
488 if ($<string>5 != NULL) {
489 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
497 optional_workspace_name:
498 /* empty */ { $<string>$ = NULL; }
499 | WHITESPACE workspace_name { $<string>$ = $<string>2; }
503 QUOTEDSTRING { $<string>$ = $<string>1; }
504 | STR { $<string>$ = $<string>1; }
505 | WORD { $<string>$ = $<string>1; }
509 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
512 printf("assignment of %s\n", $<string>3);
514 struct Assignment *new = $<assignment>6;
515 printf(" to %d\n", new->workspace);
516 printf(" floating = %d\n", new->floating);
517 new->windowclass_title = $<string>3;
518 TAILQ_INSERT_TAIL(&assignments, new, assignments);
527 struct Assignment *new = scalloc(sizeof(struct Assignment));
528 new->workspace = $<number>1;
529 new->floating = ASSIGN_FLOATING_NO;
530 $<assignment>$ = new;
536 struct Assignment *new = scalloc(sizeof(struct Assignment));
537 new->floating = ASSIGN_FLOATING_ONLY;
538 $<assignment>$ = new;
544 struct Assignment *new = scalloc(sizeof(struct Assignment));
545 new->workspace = $<number>2;
546 new->floating = ASSIGN_FLOATING;
547 $<assignment>$ = new;
559 | TOKARROW WHITESPACE
563 TOKIPCSOCKET WHITESPACE STR
565 config.ipc_socket_path = $<string>3;
570 TOKRESTARTSTATE WHITESPACE STR
572 config.restart_state_path = $<string>3;
577 TOKEXEC WHITESPACE STR
579 struct Autostart *new = smalloc(sizeof(struct Autostart));
580 new->command = $<string>3;
581 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
586 TOKTERMINAL WHITESPACE STR
588 ELOG("The terminal option is DEPRECATED and has no effect. "
589 "Please remove it from your configuration file.\n");
594 TOKFONT WHITESPACE STR
596 config.font = $<string>3;
597 printf("font %s\n", config.font);
602 TOKSINGLECOLOR WHITESPACE colorpixel
604 uint32_t *dest = $<single_color>1;
610 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
612 struct Colortriple *dest = $<color>1;
614 dest->border = $<number>3;
615 dest->background = $<number>5;
616 dest->text = $<number>7;
624 if (asprintf(&hex, "#%s", $<string>2) == -1)
626 $<number>$ = get_colorpixel(hex);
633 /* NULL */ { $<number>$ = 0; }
635 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
636 | binding_modifiers '+' { $<number>$ = $<number>1; }
640 MODIFIER { $<number>$ = $<number>1; }
641 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
642 | TOKSHIFT { $<number>$ = BIND_SHIFT; }