22 #include "workspace.h"
25 extern int yylex(void);
28 static struct bindings_head *current_bindings;
32 void yyerror(const char *str) {
33 fprintf(stderr,"error: %s\n",str);
40 void parse_file(const char *f) {
41 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
42 int fd, ret, read_bytes = 0;
46 char buffer[1026], key[512], value[512];
48 if ((fd = open(f, O_RDONLY)) == -1)
49 die("Could not open configuration file: %s\n", strerror(errno));
51 if (fstat(fd, &stbuf) == -1)
52 die("Could not fstat file: %s\n", strerror(errno));
54 buf = smalloc(stbuf.st_size * sizeof(char));
55 while (read_bytes < stbuf.st_size) {
56 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
57 die("Could not read(): %s\n", strerror(errno));
61 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
62 die("Could not lseek: %s\n", strerror(errno));
64 if ((fstr = fdopen(fd, "r")) == NULL)
65 die("Could not fdopen: %s\n", strerror(errno));
68 if (fgets(buffer, 1024, fstr) == NULL) {
71 die("Could not read configuration file\n");
74 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
75 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
76 key[0] == '#' || strlen(key) < 3)
79 if (strcasecmp(key, "set") == 0) {
81 die("Malformed variable assignment, name has to start with $\n");
83 /* get key/value for this variable */
84 char *v_key = value, *v_value;
85 if ((v_value = strstr(value, " ")) == NULL)
86 die("Malformed variable assignment, need a value\n");
90 struct Variable *new = scalloc(sizeof(struct Variable));
91 new->key = sstrdup(v_key);
92 new->value = sstrdup(v_value);
93 SLIST_INSERT_HEAD(&variables, new, variables);
94 LOG("Got new variable %s = %s\n", v_key, v_value);
99 /* For every custom variable, see how often it occurs in the file and
100 * how much extra bytes it requires when replaced. */
101 struct Variable *current, *nearest;
103 SLIST_FOREACH(current, &variables, variables) {
104 int extra = (strlen(current->value) - strlen(current->key));
107 (next = strcasestr(buf + (next - buf), current->key)) != NULL;
108 next += strlen(current->key))
109 extra_bytes += extra;
112 /* Then, allocate a new buffer and copy the file over to the new one,
113 * but replace occurences of our variables */
114 char *walk = buf, *destwalk;
115 char *new = smalloc((stbuf.st_size + extra_bytes) * sizeof(char));
117 while (walk < (buf + stbuf.st_size)) {
118 /* Find the next variable */
119 SLIST_FOREACH(current, &variables, variables)
120 current->next_match = strcasestr(walk, current->key);
122 int distance = stbuf.st_size;
123 SLIST_FOREACH(current, &variables, variables) {
124 if (current->next_match == NULL)
126 if ((current->next_match - walk) < distance) {
127 distance = (current->next_match - walk);
131 if (nearest == NULL) {
132 /* If there are no more variables, we just copy the rest */
133 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
134 destwalk += (buf + stbuf.st_size) - walk;
138 /* Copy until the next variable, then copy its value */
139 strncpy(destwalk, walk, distance);
140 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
141 walk += distance + strlen(nearest->key);
142 destwalk += distance + strlen(nearest->value);
148 if (yyparse() != 0) {
149 fprintf(stderr, "Could not parse configfile\n");
162 struct Colortriple *color;
163 struct Assignment *assignment;
164 struct Binding *binding;
167 %token <number>NUMBER
170 %token <string>STR_NG
181 %token TOKFLOATING_MODIFIER
192 %token TOKNEWCONTAINER
193 %token TOKCONTAINERMODE
199 | lines WHITESPACE line
229 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
234 TOKBIND WHITESPACE bind { $<binding>$ = $<binding>3; }
235 | TOKBINDSYM WHITESPACE bindsym { $<binding>$ = $<binding>3; }
239 binding_modifiers NUMBER WHITESPACE command
241 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
242 Binding *new = scalloc(sizeof(Binding));
244 new->keycode = $<number>2;
245 new->mods = $<number>1;
246 new->command = sstrdup($<string>4);
253 binding_modifiers WORD WHITESPACE command
255 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $2, $<string>4);
256 Binding *new = scalloc(sizeof(Binding));
258 new->symbol = sstrdup($2);
259 new->mods = $<number>1;
260 new->command = sstrdup($<string>4);
267 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
269 if (strcasecmp($<string>3, "default") == 0) {
270 printf("You cannot use the name \"default\" for your mode\n");
273 printf("\t now in mode %s\n", $<string>3);
274 printf("\t current bindings = %p\n", current_bindings);
276 TAILQ_FOREACH(binding, current_bindings, bindings) {
277 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
278 binding->mods, binding->keycode, binding->symbol, binding->command);
281 struct Mode *mode = scalloc(sizeof(struct Mode));
282 mode->name = strdup($<string>3);
283 mode->bindings = current_bindings;
284 current_bindings = NULL;
285 SLIST_INSERT_HEAD(&modes, mode, modes);
291 | modelines WHITESPACE modeline
299 if (current_bindings == NULL) {
300 current_bindings = scalloc(sizeof(struct bindings_head));
301 TAILQ_INIT(current_bindings);
304 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
309 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
311 LOG("floating modifier = %d\n", $<number>3);
312 config.floating_modifier = $<number>3;
317 TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
319 LOG("new containers will be in mode %d\n", $<number>3);
320 config.container_mode = $<number>3;
322 /* We also need to change the layout of the already existing
323 * workspaces here. Workspaces may exist at this point because
324 * of the other directives which are modifying workspaces
325 * (setting the preferred screen or name). While the workspace
326 * objects are already created, they have never been used.
327 * Thus, the user very likely awaits the default container mode
328 * to trigger in this case, regardless of where it is inside
329 * his configuration file. */
330 for (int c = 0; c < num_workspaces; c++) {
331 if (workspaces[c].table == NULL)
333 switch_layout_mode(global_conn,
334 workspaces[c].table[0][0],
335 config.container_mode);
338 | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
340 LOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
341 config.container_stack_limit = $<number>5;
342 config.container_stack_limit_value = $<number>7;
344 /* See the comment above */
345 for (int c = 0; c < num_workspaces; c++) {
346 if (workspaces[c].table == NULL)
348 Container *con = workspaces[c].table[0][0];
349 con->stack_limit = config.container_stack_limit;
350 con->stack_limit_value = config.container_stack_limit_value;
356 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
358 int ws_num = $<number>3;
360 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
362 Workspace *ws = workspace_get(ws_num - 1);
363 ws->preferred_screen = sstrdup($<string>7);
364 if ($<string>8 != NULL)
365 workspace_set_name(ws, $<string>8);
368 | TOKWORKSPACE WHITESPACE NUMBER workspace_name
370 int ws_num = $<number>3;
372 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
374 if ($<string>4 != NULL)
375 workspace_set_name(workspace_get(ws_num - 1), $<string>4);
381 /* NULL */ { $<string>$ = NULL; }
382 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
383 | WHITESPACE STR { $<string>$ = $<string>2; }
387 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
388 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
389 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
390 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
394 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
396 printf("assignment of %s to %d\n", $<string>3, $<number>6);
398 struct Assignment *new = $<assignment>6;
399 new->windowclass_title = strdup($<string>3);
400 TAILQ_INSERT_TAIL(&assignments, new, assignments);
407 struct Assignment *new = scalloc(sizeof(struct Assignment));
408 new->workspace = $<number>1;
409 new->floating = ASSIGN_FLOATING_NO;
410 $<assignment>$ = new;
414 struct Assignment *new = scalloc(sizeof(struct Assignment));
415 new->floating = ASSIGN_FLOATING_ONLY;
416 $<assignment>$ = new;
420 struct Assignment *new = scalloc(sizeof(struct Assignment));
421 new->workspace = $<number>2;
422 new->floating = ASSIGN_FLOATING;
423 $<assignment>$ = new;
434 | TOKARROW WHITESPACE
438 TOKIPCSOCKET WHITESPACE STR
440 config.ipc_socket_path = sstrdup($<string>3);
445 TOKEXEC WHITESPACE STR
447 struct Autostart *new = smalloc(sizeof(struct Autostart));
448 new->command = sstrdup($<string>3);
449 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
454 TOKTERMINAL WHITESPACE STR
456 config.terminal = sstrdup($<string>3);
457 printf("terminal %s\n", config.terminal);
462 TOKFONT WHITESPACE STR
464 config.font = sstrdup($<string>3);
465 printf("font %s\n", config.font);
471 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
473 struct Colortriple *dest = $<color>1;
475 dest->border = $<number>3;
476 dest->background = $<number>5;
477 dest->text = $<number>7;
482 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
487 /* NULL */ { $<number>$ = 0; }
489 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
490 | binding_modifiers '+' { $<number>$ = $<number>1; }
494 MODIFIER { $<number>$ = $<number>1; }
495 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
496 | TOKSHIFT { $<number>$ = BIND_SHIFT; }