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 + 1) * 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_or_number WHITESPACE command
255 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
256 Binding *new = scalloc(sizeof(Binding));
258 new->symbol = sstrdup($<string>2);
259 new->mods = $<number>1;
260 new->command = sstrdup($<string>4);
270 asprintf(&$<string>$, "%d", $1);
275 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
277 if (strcasecmp($<string>3, "default") == 0) {
278 printf("You cannot use the name \"default\" for your mode\n");
281 printf("\t now in mode %s\n", $<string>3);
282 printf("\t current bindings = %p\n", current_bindings);
284 TAILQ_FOREACH(binding, current_bindings, bindings) {
285 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
286 binding->mods, binding->keycode, binding->symbol, binding->command);
289 struct Mode *mode = scalloc(sizeof(struct Mode));
290 mode->name = strdup($<string>3);
291 mode->bindings = current_bindings;
292 current_bindings = NULL;
293 SLIST_INSERT_HEAD(&modes, mode, modes);
299 | modelines WHITESPACE modeline
307 if (current_bindings == NULL) {
308 current_bindings = scalloc(sizeof(struct bindings_head));
309 TAILQ_INIT(current_bindings);
312 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
317 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
319 LOG("floating modifier = %d\n", $<number>3);
320 config.floating_modifier = $<number>3;
325 TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
327 LOG("new containers will be in mode %d\n", $<number>3);
328 config.container_mode = $<number>3;
330 /* We also need to change the layout of the already existing
331 * workspaces here. Workspaces may exist at this point because
332 * of the other directives which are modifying workspaces
333 * (setting the preferred screen or name). While the workspace
334 * objects are already created, they have never been used.
335 * Thus, the user very likely awaits the default container mode
336 * to trigger in this case, regardless of where it is inside
337 * his configuration file. */
339 TAILQ_FOREACH(ws, workspaces, workspaces) {
340 if (ws->table == NULL)
342 switch_layout_mode(global_conn,
344 config.container_mode);
347 | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
349 LOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
350 config.container_stack_limit = $<number>5;
351 config.container_stack_limit_value = $<number>7;
353 /* See the comment above */
355 TAILQ_FOREACH(ws, workspaces, workspaces) {
356 if (ws->table == NULL)
358 Container *con = ws->table[0][0];
359 con->stack_limit = config.container_stack_limit;
360 con->stack_limit_value = config.container_stack_limit_value;
366 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
368 int ws_num = $<number>3;
370 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
372 Workspace *ws = workspace_get(ws_num - 1);
373 ws->preferred_screen = sstrdup($<string>7);
374 if ($<string>8 != NULL)
375 workspace_set_name(ws, $<string>8);
378 | TOKWORKSPACE WHITESPACE NUMBER workspace_name
380 int ws_num = $<number>3;
382 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
384 if ($<string>4 != NULL)
385 workspace_set_name(workspace_get(ws_num - 1), $<string>4);
391 /* NULL */ { $<string>$ = NULL; }
392 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
393 | WHITESPACE STR { $<string>$ = $<string>2; }
397 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
398 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
399 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
400 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
404 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
406 printf("assignment of %s to %d\n", $<string>3, $<number>6);
408 struct Assignment *new = $<assignment>6;
409 new->windowclass_title = strdup($<string>3);
410 TAILQ_INSERT_TAIL(&assignments, new, assignments);
417 struct Assignment *new = scalloc(sizeof(struct Assignment));
418 new->workspace = $<number>1;
419 new->floating = ASSIGN_FLOATING_NO;
420 $<assignment>$ = new;
424 struct Assignment *new = scalloc(sizeof(struct Assignment));
425 new->floating = ASSIGN_FLOATING_ONLY;
426 $<assignment>$ = new;
430 struct Assignment *new = scalloc(sizeof(struct Assignment));
431 new->workspace = $<number>2;
432 new->floating = ASSIGN_FLOATING;
433 $<assignment>$ = new;
444 | TOKARROW WHITESPACE
448 TOKIPCSOCKET WHITESPACE STR
450 config.ipc_socket_path = sstrdup($<string>3);
455 TOKEXEC WHITESPACE STR
457 struct Autostart *new = smalloc(sizeof(struct Autostart));
458 new->command = sstrdup($<string>3);
459 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
464 TOKTERMINAL WHITESPACE STR
466 config.terminal = sstrdup($<string>3);
467 printf("terminal %s\n", config.terminal);
472 TOKFONT WHITESPACE STR
474 config.font = sstrdup($<string>3);
475 printf("font %s\n", config.font);
481 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
483 struct Colortriple *dest = $<color>1;
485 dest->border = $<number>3;
486 dest->background = $<number>5;
487 dest->text = $<number>7;
492 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
497 /* NULL */ { $<number>$ = 0; }
499 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
500 | binding_modifiers '+' { $<number>$ = $<number>1; }
504 MODIFIER { $<number>$ = $<number>1; }
505 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
506 | TOKSHIFT { $<number>$ = BIND_SHIFT; }