22 #include "workspace.h"
26 typedef struct yy_buffer_state *YY_BUFFER_STATE;
27 extern int yylex(void);
28 extern int yyparse(void);
30 YY_BUFFER_STATE yy_scan_string(const char *);
32 static struct bindings_head *current_bindings;
36 void yyerror(const char *str) {
37 fprintf(stderr,"error: %s\n",str);
44 void parse_file(const char *f) {
45 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
46 int fd, ret, read_bytes = 0;
50 char buffer[1026], key[512], value[512];
52 if ((fd = open(f, O_RDONLY)) == -1)
53 die("Could not open configuration file: %s\n", strerror(errno));
55 if (fstat(fd, &stbuf) == -1)
56 die("Could not fstat file: %s\n", strerror(errno));
58 buf = smalloc(stbuf.st_size * sizeof(char));
59 while (read_bytes < stbuf.st_size) {
60 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
61 die("Could not read(): %s\n", strerror(errno));
65 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
66 die("Could not lseek: %s\n", strerror(errno));
68 if ((fstr = fdopen(fd, "r")) == NULL)
69 die("Could not fdopen: %s\n", strerror(errno));
72 if (fgets(buffer, 1024, fstr) == NULL) {
75 die("Could not read configuration file\n");
78 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
79 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
80 key[0] == '#' || strlen(key) < 3)
83 if (strcasecmp(key, "set") == 0) {
85 die("Malformed variable assignment, name has to start with $\n");
87 /* get key/value for this variable */
88 char *v_key = value, *v_value;
89 if ((v_value = strstr(value, " ")) == NULL)
90 die("Malformed variable assignment, need a value\n");
94 struct Variable *new = scalloc(sizeof(struct Variable));
95 new->key = sstrdup(v_key);
96 new->value = sstrdup(v_value);
97 SLIST_INSERT_HEAD(&variables, new, variables);
98 DLOG("Got new variable %s = %s\n", v_key, v_value);
103 /* For every custom variable, see how often it occurs in the file and
104 * how much extra bytes it requires when replaced. */
105 struct Variable *current, *nearest;
107 SLIST_FOREACH(current, &variables, variables) {
108 int extra = (strlen(current->value) - strlen(current->key));
111 (next = strcasestr(buf + (next - buf), current->key)) != NULL;
112 next += strlen(current->key))
113 extra_bytes += extra;
116 /* Then, allocate a new buffer and copy the file over to the new one,
117 * but replace occurences of our variables */
118 char *walk = buf, *destwalk;
119 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
121 while (walk < (buf + stbuf.st_size)) {
122 /* Find the next variable */
123 SLIST_FOREACH(current, &variables, variables)
124 current->next_match = strcasestr(walk, current->key);
126 int distance = stbuf.st_size;
127 SLIST_FOREACH(current, &variables, variables) {
128 if (current->next_match == NULL)
130 if ((current->next_match - walk) < distance) {
131 distance = (current->next_match - walk);
135 if (nearest == NULL) {
136 /* If there are no more variables, we just copy the rest */
137 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
138 destwalk += (buf + stbuf.st_size) - walk;
142 /* Copy until the next variable, then copy its value */
143 strncpy(destwalk, walk, distance);
144 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
145 walk += distance + strlen(nearest->key);
146 destwalk += distance + strlen(nearest->value);
152 if (yyparse() != 0) {
153 fprintf(stderr, "Could not parse configfile\n");
168 struct Colortriple *color;
169 struct Assignment *assignment;
170 struct Binding *binding;
173 %token <number>NUMBER
176 %token <string>STR_NG
187 %token TOKFLOATING_MODIFIER
198 %token TOKNEWCONTAINER
200 %token TOKFOCUSFOLLOWSMOUSE
201 %token TOKCONTAINERMODE
207 | lines WHITESPACE line
217 | focus_follows_mouse
239 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
244 TOKBIND WHITESPACE bind { $<binding>$ = $<binding>3; }
245 | TOKBINDSYM WHITESPACE bindsym { $<binding>$ = $<binding>3; }
249 binding_modifiers NUMBER WHITESPACE command
251 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
252 Binding *new = scalloc(sizeof(Binding));
254 new->keycode = $<number>2;
255 new->mods = $<number>1;
256 new->command = sstrdup($<string>4);
263 binding_modifiers word_or_number WHITESPACE command
265 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
266 Binding *new = scalloc(sizeof(Binding));
268 new->symbol = sstrdup($<string>2);
269 new->mods = $<number>1;
270 new->command = sstrdup($<string>4);
280 asprintf(&$<string>$, "%d", $1);
285 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
287 if (strcasecmp($<string>3, "default") == 0) {
288 printf("You cannot use the name \"default\" for your mode\n");
291 printf("\t now in mode %s\n", $<string>3);
292 printf("\t current bindings = %p\n", current_bindings);
294 TAILQ_FOREACH(binding, current_bindings, bindings) {
295 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
296 binding->mods, binding->keycode, binding->symbol, binding->command);
299 struct Mode *mode = scalloc(sizeof(struct Mode));
300 mode->name = strdup($<string>3);
301 mode->bindings = current_bindings;
302 current_bindings = NULL;
303 SLIST_INSERT_HEAD(&modes, mode, modes);
318 if (current_bindings == NULL) {
319 current_bindings = scalloc(sizeof(struct bindings_head));
320 TAILQ_INIT(current_bindings);
323 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
328 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
330 DLOG("floating modifier = %d\n", $<number>3);
331 config.floating_modifier = $<number>3;
336 TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
338 DLOG("new containers will be in mode %d\n", $<number>3);
339 config.container_mode = $<number>3;
341 /* We also need to change the layout of the already existing
342 * workspaces here. Workspaces may exist at this point because
343 * of the other directives which are modifying workspaces
344 * (setting the preferred screen or name). While the workspace
345 * objects are already created, they have never been used.
346 * Thus, the user very likely awaits the default container mode
347 * to trigger in this case, regardless of where it is inside
348 * his configuration file. */
350 TAILQ_FOREACH(ws, workspaces, workspaces) {
351 if (ws->table == NULL)
353 switch_layout_mode(global_conn,
355 config.container_mode);
358 | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
360 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
361 config.container_stack_limit = $<number>5;
362 config.container_stack_limit_value = $<number>7;
364 /* See the comment above */
366 TAILQ_FOREACH(ws, workspaces, workspaces) {
367 if (ws->table == NULL)
369 Container *con = ws->table[0][0];
370 con->stack_limit = config.container_stack_limit;
371 con->stack_limit_value = config.container_stack_limit_value;
377 TOKNEWWINDOW WHITESPACE WORD
379 DLOG("new windows should start in mode %s\n", $<string>3);
380 config.default_border = strdup($<string>3);
385 TOKFOCUSFOLLOWSMOUSE WHITESPACE NUMBER
387 config.focus_follows_mouse = ($<number>3 == 0 ? 0 : 1);
392 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen optional_workspace_name
394 int ws_num = $<number>3;
396 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
398 Workspace *ws = workspace_get(ws_num - 1);
399 ws->preferred_screen = sstrdup($<string>7);
400 if ($<string>8 != NULL)
401 workspace_set_name(ws, $<string>8);
404 | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
406 int ws_num = $<number>3;
408 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
410 if ($<string>5 != NULL)
411 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
416 optional_workspace_name:
417 /* empty */ { $<string>$ = NULL; }
418 | WHITESPACE workspace_name { $<string>$ = $<string>2; }
422 QUOTEDSTRING { $<string>$ = $<string>1; }
423 | STR { $<string>$ = $<string>1; }
424 | WORD { $<string>$ = $<string>1; }
428 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
429 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
430 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
431 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
435 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
437 printf("assignment of %s\n", $<string>3);
439 struct Assignment *new = $<assignment>6;
440 printf(" to %d\n", new->workspace);
441 printf(" floating = %d\n", new->floating);
442 new->windowclass_title = strdup($<string>3);
443 TAILQ_INSERT_TAIL(&assignments, new, assignments);
450 struct Assignment *new = scalloc(sizeof(struct Assignment));
451 new->workspace = $<number>1;
452 new->floating = ASSIGN_FLOATING_NO;
453 $<assignment>$ = new;
457 struct Assignment *new = scalloc(sizeof(struct Assignment));
458 new->floating = ASSIGN_FLOATING_ONLY;
459 $<assignment>$ = new;
463 struct Assignment *new = scalloc(sizeof(struct Assignment));
464 new->workspace = $<number>2;
465 new->floating = ASSIGN_FLOATING;
466 $<assignment>$ = new;
477 | TOKARROW WHITESPACE
481 TOKIPCSOCKET WHITESPACE STR
483 config.ipc_socket_path = sstrdup($<string>3);
488 TOKEXEC WHITESPACE STR
490 struct Autostart *new = smalloc(sizeof(struct Autostart));
491 new->command = sstrdup($<string>3);
492 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
497 TOKTERMINAL WHITESPACE STR
499 DLOG("The terminal option is DEPRECATED and has no effect. "
500 "Please remove it from your configuration file.");
505 TOKFONT WHITESPACE STR
507 config.font = sstrdup($<string>3);
508 printf("font %s\n", config.font);
514 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
516 struct Colortriple *dest = $<color>1;
518 dest->border = $<number>3;
519 dest->background = $<number>5;
520 dest->text = $<number>7;
528 if (asprintf(&hex, "#%s", $<string>2) == -1)
530 $<number>$ = get_colorpixel(global_conn, hex);
537 /* NULL */ { $<number>$ = 0; }
539 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
540 | binding_modifiers '+' { $<number>$ = $<number>1; }
544 MODIFIER { $<number>$ = $<number>1; }
545 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
546 | TOKSHIFT { $<number>$ = BIND_SHIFT; }