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 /* We need to copy the buffer because we need to invalidate the
123 * variables (otherwise we will count them twice, which is bad when
124 * 'extra' is negative) */
125 char *bufcopy = sstrdup(buf);
126 SLIST_FOREACH(current, &variables, variables) {
127 int extra = (strlen(current->value) - strlen(current->key));
130 (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
131 next += strlen(current->key)) {
133 extra_bytes += extra;
138 /* Then, allocate a new buffer and copy the file over to the new one,
139 * but replace occurences of our variables */
140 char *walk = buf, *destwalk;
141 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
143 while (walk < (buf + stbuf.st_size)) {
144 /* Find the next variable */
145 SLIST_FOREACH(current, &variables, variables)
146 current->next_match = strcasestr(walk, current->key);
148 int distance = stbuf.st_size;
149 SLIST_FOREACH(current, &variables, variables) {
150 if (current->next_match == NULL)
152 if ((current->next_match - walk) < distance) {
153 distance = (current->next_match - walk);
157 if (nearest == NULL) {
158 /* If there are no more variables, we just copy the rest */
159 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
160 destwalk += (buf + stbuf.st_size) - walk;
164 /* Copy until the next variable, then copy its value */
165 strncpy(destwalk, walk, distance);
166 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
167 walk += distance + strlen(nearest->key);
168 destwalk += distance + strlen(nearest->value);
174 context = scalloc(sizeof(struct context));
175 context->filename = f;
177 if (yyparse() != 0) {
178 fprintf(stderr, "Could not parse configfile\n");
182 FREE(context->line_copy);
187 while (!SLIST_EMPTY(&variables)) {
188 current = SLIST_FIRST(&variables);
190 FREE(current->value);
191 SLIST_REMOVE_HEAD(&variables, variables);
202 %lex-param { struct context *context }
207 uint32_t *single_color;
208 struct Colortriple *color;
209 struct Assignment *assignment;
210 struct Binding *binding;
213 %token <number>NUMBER "<number>"
214 %token <string>WORD "<word>"
215 %token <string>STR "<string>"
216 %token <string>STR_NG "<string (non-greedy)>"
217 %token <string>HEX "<hex>"
218 %token <string>OUTPUT "<RandR output>"
221 %token TOKCOMMENT "<comment>"
222 %token TOKFONT "font"
223 %token TOKBINDSYM "bindsym"
224 %token MODIFIER "<modifier>"
225 %token TOKCONTROL "control"
226 %token TOKSHIFT "shift"
227 %token WHITESPACE "<whitespace>"
228 %token TOKFLOATING_MODIFIER "floating_modifier"
229 %token QUOTEDSTRING "<quoted string>"
230 %token TOKWORKSPACE "workspace"
231 %token TOKOUTPUT "output"
232 %token TOKASSIGN "assign"
234 %token TOKIPCSOCKET "ipc_socket"
235 %token TOKEXEC "exec"
236 %token TOKSINGLECOLOR
238 %token TOKARROW "→"
239 %token TOKMODE "mode"
240 %token TOKNEWCONTAINER "new_container"
241 %token TOKNEWWINDOW "new_window"
242 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
243 %token TOKWORKSPACEBAR "workspace_bar"
244 %token TOKCONTAINERMODE "default/stacking/tabbed"
245 %token TOKSTACKLIMIT "stack-limit"
250 | lines WHITESPACE line
261 | focus_follows_mouse
285 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
290 TOKBIND WHITESPACE bind { $<binding>$ = $<binding>3; }
291 | TOKBINDSYM WHITESPACE bindsym { $<binding>$ = $<binding>3; }
295 binding_modifiers NUMBER WHITESPACE command
297 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
298 Binding *new = scalloc(sizeof(Binding));
300 new->keycode = $<number>2;
301 new->mods = $<number>1;
302 new->command = $<string>4;
309 binding_modifiers word_or_number WHITESPACE command
311 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
312 Binding *new = scalloc(sizeof(Binding));
314 new->symbol = $<string>2;
315 new->mods = $<number>1;
316 new->command = $<string>4;
326 asprintf(&$<string>$, "%d", $1);
331 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
333 if (strcasecmp($<string>3, "default") == 0) {
334 printf("You cannot use the name \"default\" for your mode\n");
337 printf("\t now in mode %s\n", $<string>3);
338 printf("\t current bindings = %p\n", current_bindings);
340 TAILQ_FOREACH(binding, current_bindings, bindings) {
341 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
342 binding->mods, binding->keycode, binding->symbol, binding->command);
345 struct Mode *mode = scalloc(sizeof(struct Mode));
346 mode->name = $<string>3;
347 mode->bindings = current_bindings;
348 current_bindings = NULL;
349 SLIST_INSERT_HEAD(&modes, mode, modes);
364 if (current_bindings == NULL) {
365 current_bindings = scalloc(sizeof(struct bindings_head));
366 TAILQ_INIT(current_bindings);
369 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
374 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
376 DLOG("floating modifier = %d\n", $<number>3);
377 config.floating_modifier = $<number>3;
382 TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
384 DLOG("new containers will be in mode %d\n", $<number>3);
385 config.container_mode = $<number>3;
387 /* We also need to change the layout of the already existing
388 * workspaces here. Workspaces may exist at this point because
389 * of the other directives which are modifying workspaces
390 * (setting the preferred screen or name). While the workspace
391 * objects are already created, they have never been used.
392 * Thus, the user very likely awaits the default container mode
393 * to trigger in this case, regardless of where it is inside
394 * his configuration file. */
396 TAILQ_FOREACH(ws, workspaces, workspaces) {
397 if (ws->table == NULL)
399 switch_layout_mode(global_conn,
401 config.container_mode);
404 | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
406 if ($<number>7 <= 0) {
407 ELOG("Invalid stack-limit, need a limit which is > 0\n");
409 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
410 config.container_stack_limit = $<number>5;
411 config.container_stack_limit_value = $<number>7;
413 /* See the comment above */
415 TAILQ_FOREACH(ws, workspaces, workspaces) {
416 if (ws->table == NULL)
418 Container *con = ws->table[0][0];
419 con->stack_limit = config.container_stack_limit;
420 con->stack_limit_value = config.container_stack_limit_value;
427 TOKNEWWINDOW WHITESPACE WORD
429 DLOG("new windows should start in mode %s\n", $<string>3);
430 config.default_border = sstrdup($<string>3);
437 $<number>$ = ($<number>1 == 1);
441 DLOG("checking word \"%s\"\n", $<string>1);
442 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
443 strcasecmp($<string>1, "true") == 0 ||
444 strcasecmp($<string>1, "on") == 0 ||
445 strcasecmp($<string>1, "enable") == 0 ||
446 strcasecmp($<string>1, "active") == 0);
451 TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
453 DLOG("focus follows mouse = %d\n", $<number>3);
454 config.disable_focus_follows_mouse = !($<number>3);
459 TOKWORKSPACEBAR WHITESPACE bool
461 DLOG("workspace bar = %d\n", $<number>3);
462 config.disable_workspace_bar = !($<number>3);
467 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
469 int ws_num = $<number>3;
471 ELOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
473 Workspace *ws = workspace_get(ws_num - 1);
474 ws->preferred_output = $<string>7;
475 if ($<string>8 != NULL) {
476 workspace_set_name(ws, $<string>8);
481 | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
483 int ws_num = $<number>3;
485 ELOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
487 DLOG("workspace name to: %s\n", $<string>5);
488 if ($<string>5 != NULL) {
489 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
496 optional_workspace_name:
497 /* empty */ { $<string>$ = NULL; }
498 | WHITESPACE workspace_name { $<string>$ = $<string>2; }
502 QUOTEDSTRING { $<string>$ = $<string>1; }
503 | STR { $<string>$ = $<string>1; }
504 | WORD { $<string>$ = $<string>1; }
508 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
510 DLOG("assignment of %s\n", $<string>3);
512 struct Assignment *new = $<assignment>6;
513 if (new->floating != ASSIGN_FLOATING_ONLY && new->workspace < 1) {
514 ELOG("Invalid client assignment, workspace number %d out of range\n", new->workspace);
517 DLOG(" to %d\n", new->workspace);
518 DLOG(" floating = %d\n", new->floating);
519 new->windowclass_title = $<string>3;
520 TAILQ_INSERT_TAIL(&assignments, new, assignments);
528 struct Assignment *new = scalloc(sizeof(struct Assignment));
529 new->workspace = $<number>1;
530 new->floating = ASSIGN_FLOATING_NO;
531 $<assignment>$ = new;
535 struct Assignment *new = scalloc(sizeof(struct Assignment));
536 new->floating = ASSIGN_FLOATING_ONLY;
537 $<assignment>$ = new;
541 struct Assignment *new = scalloc(sizeof(struct Assignment));
542 new->workspace = $<number>2;
543 new->floating = ASSIGN_FLOATING;
544 $<assignment>$ = new;
555 | TOKARROW WHITESPACE
559 TOKIPCSOCKET WHITESPACE STR
561 config.ipc_socket_path = $<string>3;
566 TOKEXEC WHITESPACE STR
568 struct Autostart *new = smalloc(sizeof(struct Autostart));
569 new->command = $<string>3;
570 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
575 TOKTERMINAL WHITESPACE STR
577 ELOG("The terminal option is DEPRECATED and has no effect. "
578 "Please remove it from your configuration file.\n");
583 TOKFONT WHITESPACE STR
585 config.font = $<string>3;
586 printf("font %s\n", config.font);
591 TOKSINGLECOLOR WHITESPACE colorpixel
593 uint32_t *dest = $<single_color>1;
599 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
601 struct Colortriple *dest = $<color>1;
603 dest->border = $<number>3;
604 dest->background = $<number>5;
605 dest->text = $<number>7;
613 if (asprintf(&hex, "#%s", $<string>2) == -1)
615 $<number>$ = get_colorpixel(global_conn, hex);
622 /* NULL */ { $<number>$ = 0; }
624 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
625 | binding_modifiers '+' { $<number>$ = $<number>1; }
629 MODIFIER { $<number>$ = $<number>1; }
630 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
631 | TOKSHIFT { $<number>$ = BIND_SHIFT; }