22 #include "workspace.h"
26 typedef struct yy_buffer_state *YY_BUFFER_STATE;
27 extern int yylex(struct context *context);
28 extern int yyparse(void);
30 YY_BUFFER_STATE yy_scan_string(const char *);
32 static struct bindings_head *current_bindings;
33 static struct context *context;
35 /* We don’t need yydebug for now, as we got decent error messages using
36 * yyerror(). Should you ever want to extend the parser, it might be handy
37 * to just comment it in again, so it stays here. */
40 void yyerror(const char *error_message) {
42 ELOG("CONFIG: %s\n", error_message);
43 ELOG("CONFIG: in file \"%s\", line %d:\n",
44 context->filename, context->line_number);
45 ELOG("CONFIG: %s\n", context->line_copy);
47 for (int c = 1; c <= context->last_column; c++)
48 if (c >= context->first_column)
59 void parse_file(const char *f) {
60 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
61 int fd, ret, read_bytes = 0;
65 char buffer[1026], key[512], value[512];
67 if ((fd = open(f, O_RDONLY)) == -1)
68 die("Could not open configuration file: %s\n", strerror(errno));
70 if (fstat(fd, &stbuf) == -1)
71 die("Could not fstat file: %s\n", strerror(errno));
73 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
74 while (read_bytes < stbuf.st_size) {
75 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
76 die("Could not read(): %s\n", strerror(errno));
80 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
81 die("Could not lseek: %s\n", strerror(errno));
83 if ((fstr = fdopen(fd, "r")) == NULL)
84 die("Could not fdopen: %s\n", strerror(errno));
87 if (fgets(buffer, 1024, fstr) == NULL) {
90 die("Could not read configuration file\n");
93 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
94 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
95 key[0] == '#' || strlen(key) < 3)
98 if (strcasecmp(key, "set") == 0) {
100 die("Malformed variable assignment, name has to start with $\n");
102 /* get key/value for this variable */
103 char *v_key = value, *v_value;
104 if ((v_value = strstr(value, " ")) == NULL)
105 die("Malformed variable assignment, need a value\n");
109 struct Variable *new = scalloc(sizeof(struct Variable));
110 new->key = sstrdup(v_key);
111 new->value = sstrdup(v_value);
112 SLIST_INSERT_HEAD(&variables, new, variables);
113 DLOG("Got new variable %s = %s\n", v_key, v_value);
118 /* For every custom variable, see how often it occurs in the file and
119 * how much extra bytes it requires when replaced. */
120 struct Variable *current, *nearest;
122 SLIST_FOREACH(current, &variables, variables) {
123 int extra = (strlen(current->value) - strlen(current->key));
126 (next = strcasestr(buf + (next - buf), current->key)) != NULL;
127 next += strlen(current->key))
128 extra_bytes += extra;
131 /* Then, allocate a new buffer and copy the file over to the new one,
132 * but replace occurences of our variables */
133 char *walk = buf, *destwalk;
134 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
136 while (walk < (buf + stbuf.st_size)) {
137 /* Find the next variable */
138 SLIST_FOREACH(current, &variables, variables)
139 current->next_match = strcasestr(walk, current->key);
141 int distance = stbuf.st_size;
142 SLIST_FOREACH(current, &variables, variables) {
143 if (current->next_match == NULL)
145 if ((current->next_match - walk) < distance) {
146 distance = (current->next_match - walk);
150 if (nearest == NULL) {
151 /* If there are no more variables, we just copy the rest */
152 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
153 destwalk += (buf + stbuf.st_size) - walk;
157 /* Copy until the next variable, then copy its value */
158 strncpy(destwalk, walk, distance);
159 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
160 walk += distance + strlen(nearest->key);
161 destwalk += distance + strlen(nearest->value);
167 context = scalloc(sizeof(struct context));
168 context->filename = f;
170 if (yyparse() != 0) {
171 fprintf(stderr, "Could not parse configfile\n");
175 FREE(context->line_copy);
180 while (!SLIST_EMPTY(&variables)) {
181 current = SLIST_FIRST(&variables);
183 FREE(current->value);
184 SLIST_REMOVE_HEAD(&variables, variables);
195 %lex-param { struct context *context }
200 uint32_t *single_color;
201 struct Colortriple *color;
202 struct Assignment *assignment;
203 struct Binding *binding;
206 %token <number>NUMBER "<number>"
207 %token <string>WORD "<word>"
208 %token <string>STR "<string>"
209 %token <string>STR_NG "<string (non-greedy)>"
210 %token <string>HEX "<hex>"
211 %token <string>OUTPUT "<RandR output>"
214 %token TOKCOMMENT "<comment>"
215 %token TOKFONT "font"
216 %token TOKBINDSYM "bindsym"
217 %token MODIFIER "<modifier>"
218 %token TOKCONTROL "control"
219 %token TOKSHIFT "shift"
220 %token WHITESPACE "<whitespace>"
221 %token TOKFLOATING_MODIFIER "floating_modifier"
222 %token QUOTEDSTRING "<quoted string>"
223 %token TOKWORKSPACE "workspace"
224 %token TOKOUTPUT "output"
225 %token TOKASSIGN "assign"
227 %token TOKIPCSOCKET "ipc_socket"
228 %token TOKEXEC "exec"
229 %token TOKSINGLECOLOR
232 %token TOKMODE "mode"
233 %token TOKNEWCONTAINER "new_container"
234 %token TOKNEWWINDOW "new_window"
235 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
236 %token TOKWORKSPACEBAR "workspace_bar"
237 %token TOKCONTAINERMODE "default/stacking/tabbed"
238 %token TOKSTACKLIMIT "stack-limit"
243 | lines WHITESPACE line
254 | focus_follows_mouse
278 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
283 TOKBIND WHITESPACE bind { $<binding>$ = $<binding>3; }
284 | TOKBINDSYM WHITESPACE bindsym { $<binding>$ = $<binding>3; }
288 binding_modifiers NUMBER WHITESPACE command
290 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
291 Binding *new = scalloc(sizeof(Binding));
293 new->keycode = $<number>2;
294 new->mods = $<number>1;
295 new->command = $<string>4;
302 binding_modifiers word_or_number WHITESPACE command
304 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
305 Binding *new = scalloc(sizeof(Binding));
307 new->symbol = $<string>2;
308 new->mods = $<number>1;
309 new->command = $<string>4;
319 asprintf(&$<string>$, "%d", $1);
324 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
326 if (strcasecmp($<string>3, "default") == 0) {
327 printf("You cannot use the name \"default\" for your mode\n");
330 printf("\t now in mode %s\n", $<string>3);
331 printf("\t current bindings = %p\n", current_bindings);
333 TAILQ_FOREACH(binding, current_bindings, bindings) {
334 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
335 binding->mods, binding->keycode, binding->symbol, binding->command);
338 struct Mode *mode = scalloc(sizeof(struct Mode));
339 mode->name = $<string>3;
340 mode->bindings = current_bindings;
341 current_bindings = NULL;
342 SLIST_INSERT_HEAD(&modes, mode, modes);
357 if (current_bindings == NULL) {
358 current_bindings = scalloc(sizeof(struct bindings_head));
359 TAILQ_INIT(current_bindings);
362 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
367 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
369 DLOG("floating modifier = %d\n", $<number>3);
370 config.floating_modifier = $<number>3;
375 TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
377 DLOG("new containers will be in mode %d\n", $<number>3);
378 config.container_mode = $<number>3;
380 /* We also need to change the layout of the already existing
381 * workspaces here. Workspaces may exist at this point because
382 * of the other directives which are modifying workspaces
383 * (setting the preferred screen or name). While the workspace
384 * objects are already created, they have never been used.
385 * Thus, the user very likely awaits the default container mode
386 * to trigger in this case, regardless of where it is inside
387 * his configuration file. */
389 TAILQ_FOREACH(ws, workspaces, workspaces) {
390 if (ws->table == NULL)
392 switch_layout_mode(global_conn,
394 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;
403 /* See the comment above */
405 TAILQ_FOREACH(ws, workspaces, workspaces) {
406 if (ws->table == NULL)
408 Container *con = ws->table[0][0];
409 con->stack_limit = config.container_stack_limit;
410 con->stack_limit_value = config.container_stack_limit_value;
416 TOKNEWWINDOW WHITESPACE WORD
418 DLOG("new windows should start in mode %s\n", $<string>3);
419 config.default_border = sstrdup($<string>3);
426 $<number>$ = ($<number>1 == 1);
430 DLOG("checking word \"%s\"\n", $<string>1);
431 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
432 strcasecmp($<string>1, "true") == 0 ||
433 strcasecmp($<string>1, "on") == 0 ||
434 strcasecmp($<string>1, "enable") == 0 ||
435 strcasecmp($<string>1, "active") == 0);
440 TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
442 DLOG("focus follows mouse = %d\n", $<number>3);
443 config.disable_focus_follows_mouse = !($<number>3);
448 TOKWORKSPACEBAR WHITESPACE bool
450 DLOG("workspace bar = %d\n", $<number>3);
451 config.disable_workspace_bar = !($<number>3);
456 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
458 int ws_num = $<number>3;
460 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
462 Workspace *ws = workspace_get(ws_num - 1);
463 ws->preferred_output = $<string>7;
464 if ($<string>8 != NULL) {
465 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);
477 if ($<string>5 != NULL) {
478 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
485 optional_workspace_name:
486 /* empty */ { $<string>$ = NULL; }
487 | WHITESPACE workspace_name { $<string>$ = $<string>2; }
491 QUOTEDSTRING { $<string>$ = $<string>1; }
492 | STR { $<string>$ = $<string>1; }
493 | WORD { $<string>$ = $<string>1; }
497 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
499 DLOG("assignment of %s\n", $<string>3);
501 struct Assignment *new = $<assignment>6;
502 if (new->floating != ASSIGN_FLOATING_ONLY && new->workspace < 1) {
503 DLOG("Invalid client assignment, workspace number %d out of range\n", new->workspace);
506 DLOG(" to %d\n", new->workspace);
507 DLOG(" floating = %d\n", new->floating);
508 new->windowclass_title = $<string>3;
509 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;
524 struct Assignment *new = scalloc(sizeof(struct Assignment));
525 new->floating = ASSIGN_FLOATING_ONLY;
526 $<assignment>$ = new;
530 struct Assignment *new = scalloc(sizeof(struct Assignment));
531 new->workspace = $<number>2;
532 new->floating = ASSIGN_FLOATING;
533 $<assignment>$ = new;
544 | TOKARROW WHITESPACE
548 TOKIPCSOCKET WHITESPACE STR
550 config.ipc_socket_path = $<string>3;
555 TOKEXEC WHITESPACE STR
557 struct Autostart *new = smalloc(sizeof(struct Autostart));
558 new->command = $<string>3;
559 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
564 TOKTERMINAL WHITESPACE STR
566 ELOG("The terminal option is DEPRECATED and has no effect. "
567 "Please remove it from your configuration file.\n");
572 TOKFONT WHITESPACE STR
574 config.font = $<string>3;
575 printf("font %s\n", config.font);
580 TOKSINGLECOLOR WHITESPACE colorpixel
582 uint32_t *dest = $<single_color>1;
588 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
590 struct Colortriple *dest = $<color>1;
592 dest->border = $<number>3;
593 dest->background = $<number>5;
594 dest->text = $<number>7;
602 if (asprintf(&hex, "#%s", $<string>2) == -1)
604 $<number>$ = get_colorpixel(global_conn, hex);
611 /* NULL */ { $<number>$ = 0; }
613 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
614 | binding_modifiers '+' { $<number>$ = $<number>1; }
618 MODIFIER { $<number>$ = $<number>1; }
619 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
620 | TOKSHIFT { $<number>$ = BIND_SHIFT; }