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);
105 /* For every custom variable, see how often it occurs in the file and
106 * how much extra bytes it requires when replaced. */
107 struct Variable *current, *nearest;
109 /* We need to copy the buffer because we need to invalidate the
110 * variables (otherwise we will count them twice, which is bad when
111 * 'extra' is negative) */
112 char *bufcopy = sstrdup(buf);
113 SLIST_FOREACH(current, &variables, variables) {
114 int extra = (strlen(current->value) - strlen(current->key));
117 (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
118 next += strlen(current->key)) {
120 extra_bytes += extra;
125 /* Then, allocate a new buffer and copy the file over to the new one,
126 * but replace occurences of our variables */
127 char *walk = buf, *destwalk;
128 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
130 while (walk < (buf + stbuf.st_size)) {
131 /* Find the next variable */
132 SLIST_FOREACH(current, &variables, variables)
133 current->next_match = strcasestr(walk, current->key);
135 int distance = stbuf.st_size;
136 SLIST_FOREACH(current, &variables, variables) {
137 if (current->next_match == NULL)
139 if ((current->next_match - walk) < distance) {
140 distance = (current->next_match - walk);
144 if (nearest == NULL) {
145 /* If there are no more variables, we just copy the rest */
146 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
147 destwalk += (buf + stbuf.st_size) - walk;
151 /* Copy until the next variable, then copy its value */
152 strncpy(destwalk, walk, distance);
153 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
154 walk += distance + strlen(nearest->key);
155 destwalk += distance + strlen(nearest->value);
161 context = scalloc(sizeof(struct context));
162 context->filename = f;
164 if (yyparse() != 0) {
165 fprintf(stderr, "Could not parse configfile\n");
169 FREE(context->line_copy);
174 while (!SLIST_EMPTY(&variables)) {
175 current = SLIST_FIRST(&variables);
177 FREE(current->value);
178 SLIST_REMOVE_HEAD(&variables, variables);
187 %lex-param { struct context *context }
192 uint32_t *single_color;
193 struct Colortriple *color;
194 struct Assignment *assignment;
195 struct Binding *binding;
198 %token <number>NUMBER "<number>"
199 %token <string>WORD "<word>"
200 %token <string>STR "<string>"
201 %token <string>STR_NG "<string (non-greedy)>"
202 %token <string>HEX "<hex>"
203 %token <string>OUTPUT "<RandR output>"
206 %token TOKCOMMENT "<comment>"
207 %token TOKFONT "font"
208 %token TOKBINDSYM "bindsym"
209 %token MODIFIER "<modifier>"
210 %token TOKCONTROL "control"
211 %token TOKSHIFT "shift"
212 %token WHITESPACE "<whitespace>"
213 %token TOKFLOATING_MODIFIER "floating_modifier"
214 %token QUOTEDSTRING "<quoted string>"
215 %token TOKWORKSPACE "workspace"
216 %token TOKOUTPUT "output"
217 %token TOKASSIGN "assign"
219 %token TOKIPCSOCKET "ipc_socket"
220 %token TOKRESTARTSTATE "restart_state"
221 %token TOKEXEC "exec"
222 %token TOKSINGLECOLOR
224 %token TOKARROW "→"
225 %token TOKMODE "mode"
226 %token TOKNEWCONTAINER "new_container"
227 %token TOKNEWWINDOW "new_window"
228 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
229 %token TOKWORKSPACEBAR "workspace_bar"
230 %token TOKCONTAINERMODE "default/stacking/tabbed"
231 %token TOKSTACKLIMIT "stack-limit"
236 | lines WHITESPACE line
247 | focus_follows_mouse
272 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
277 TOKBIND WHITESPACE bind { $<binding>$ = $<binding>3; }
278 | TOKBINDSYM WHITESPACE bindsym { $<binding>$ = $<binding>3; }
282 binding_modifiers NUMBER WHITESPACE command
284 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
285 Binding *new = scalloc(sizeof(Binding));
287 new->keycode = $<number>2;
288 new->mods = $<number>1;
289 new->command = $<string>4;
296 binding_modifiers word_or_number WHITESPACE command
298 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
299 Binding *new = scalloc(sizeof(Binding));
301 new->symbol = $<string>2;
302 new->mods = $<number>1;
303 new->command = $<string>4;
313 asprintf(&$<string>$, "%d", $1);
318 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
320 if (strcasecmp($<string>3, "default") == 0) {
321 printf("You cannot use the name \"default\" for your mode\n");
324 printf("\t now in mode %s\n", $<string>3);
325 printf("\t current bindings = %p\n", current_bindings);
327 TAILQ_FOREACH(binding, current_bindings, bindings) {
328 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
329 binding->mods, binding->keycode, binding->symbol, binding->command);
332 struct Mode *mode = scalloc(sizeof(struct Mode));
333 mode->name = $<string>3;
334 mode->bindings = current_bindings;
335 current_bindings = NULL;
336 SLIST_INSERT_HEAD(&modes, mode, modes);
351 if (current_bindings == NULL) {
352 current_bindings = scalloc(sizeof(struct bindings_head));
353 TAILQ_INIT(current_bindings);
356 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
361 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
363 DLOG("floating modifier = %d\n", $<number>3);
364 config.floating_modifier = $<number>3;
369 TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
371 DLOG("new containers will be in mode %d\n", $<number>3);
372 config.container_mode = $<number>3;
375 /* We also need to change the layout of the already existing
376 * workspaces here. Workspaces may exist at this point because
377 * of the other directives which are modifying workspaces
378 * (setting the preferred screen or name). While the workspace
379 * objects are already created, they have never been used.
380 * Thus, the user very likely awaits the default container mode
381 * to trigger in this case, regardless of where it is inside
382 * his configuration file. */
384 TAILQ_FOREACH(ws, workspaces, workspaces) {
385 if (ws->table == NULL)
387 switch_layout_mode(global_conn,
389 config.container_mode);
393 | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
395 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
396 config.container_stack_limit = $<number>5;
397 config.container_stack_limit_value = $<number>7;
400 /* See the comment above */
402 TAILQ_FOREACH(ws, workspaces, workspaces) {
403 if (ws->table == NULL)
405 Container *con = ws->table[0][0];
406 con->stack_limit = config.container_stack_limit;
407 con->stack_limit_value = config.container_stack_limit_value;
414 TOKNEWWINDOW WHITESPACE WORD
416 DLOG("new windows should start in mode %s\n", $<string>3);
417 config.default_border = sstrdup($<string>3);
424 $<number>$ = ($<number>1 == 1);
428 DLOG("checking word \"%s\"\n", $<string>1);
429 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
430 strcasecmp($<string>1, "true") == 0 ||
431 strcasecmp($<string>1, "on") == 0 ||
432 strcasecmp($<string>1, "enable") == 0 ||
433 strcasecmp($<string>1, "active") == 0);
438 TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
440 DLOG("focus follows mouse = %d\n", $<number>3);
441 config.disable_focus_follows_mouse = !($<number>3);
446 TOKWORKSPACEBAR WHITESPACE bool
448 DLOG("workspace bar = %d\n", $<number>3);
449 config.disable_workspace_bar = !($<number>3);
454 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
456 int ws_num = $<number>3;
458 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
461 Workspace *ws = workspace_get(ws_num - 1);
462 ws->preferred_output = $<string>7;
463 if ($<string>8 != NULL) {
464 workspace_set_name(ws, $<string>8);
470 | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
472 int ws_num = $<number>3;
474 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
476 DLOG("workspace name to: %s\n", $<string>5);
478 if ($<string>5 != NULL) {
479 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
487 optional_workspace_name:
488 /* empty */ { $<string>$ = NULL; }
489 | WHITESPACE workspace_name { $<string>$ = $<string>2; }
493 QUOTEDSTRING { $<string>$ = $<string>1; }
494 | STR { $<string>$ = $<string>1; }
495 | WORD { $<string>$ = $<string>1; }
499 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
502 printf("assignment of %s\n", $<string>3);
504 struct Assignment *new = $<assignment>6;
505 printf(" to %d\n", new->workspace);
506 printf(" floating = %d\n", new->floating);
507 new->windowclass_title = $<string>3;
508 TAILQ_INSERT_TAIL(&assignments, new, assignments);
517 struct Assignment *new = scalloc(sizeof(struct Assignment));
518 new->workspace = $<number>1;
519 new->floating = ASSIGN_FLOATING_NO;
520 $<assignment>$ = new;
526 struct Assignment *new = scalloc(sizeof(struct Assignment));
527 new->floating = ASSIGN_FLOATING_ONLY;
528 $<assignment>$ = new;
534 struct Assignment *new = scalloc(sizeof(struct Assignment));
535 new->workspace = $<number>2;
536 new->floating = ASSIGN_FLOATING;
537 $<assignment>$ = new;
549 | TOKARROW WHITESPACE
553 TOKIPCSOCKET WHITESPACE STR
555 config.ipc_socket_path = $<string>3;
560 TOKRESTARTSTATE WHITESPACE STR
562 config.restart_state_path = $<string>3;
567 TOKEXEC WHITESPACE STR
569 struct Autostart *new = smalloc(sizeof(struct Autostart));
570 new->command = $<string>3;
571 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
576 TOKTERMINAL WHITESPACE STR
578 ELOG("The terminal option is DEPRECATED and has no effect. "
579 "Please remove it from your configuration file.\n");
584 TOKFONT WHITESPACE STR
586 config.font = $<string>3;
587 printf("font %s\n", config.font);
592 TOKSINGLECOLOR WHITESPACE colorpixel
594 uint32_t *dest = $<single_color>1;
600 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
602 struct Colortriple *dest = $<color>1;
604 dest->border = $<number>3;
605 dest->background = $<number>5;
606 dest->text = $<number>7;
614 if (asprintf(&hex, "#%s", $<string>2) == -1)
616 $<number>$ = get_colorpixel(hex);
623 /* NULL */ { $<number>$ = 0; }
625 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
626 | binding_modifiers '+' { $<number>$ = $<number>1; }
630 MODIFIER { $<number>$ = $<number>1; }
631 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
632 | TOKSHIFT { $<number>$ = BIND_SHIFT; }