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
196 | lines WHITESPACE line
225 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
230 TOKBIND WHITESPACE bind { $<binding>$ = $<binding>3; }
231 | TOKBINDSYM WHITESPACE bindsym { $<binding>$ = $<binding>3; }
235 binding_modifiers NUMBER WHITESPACE command
237 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
238 Binding *new = scalloc(sizeof(Binding));
240 new->keycode = $<number>2;
241 new->mods = $<number>1;
242 new->command = sstrdup($<string>4);
249 binding_modifiers WORD WHITESPACE command
251 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $2, $<string>4);
252 Binding *new = scalloc(sizeof(Binding));
254 new->symbol = sstrdup($2);
255 new->mods = $<number>1;
256 new->command = sstrdup($<string>4);
263 TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
265 if (strcasecmp($<string>3, "default") == 0) {
266 printf("You cannot use the name \"default\" for your mode\n");
269 printf("\t now in mode %s\n", $<string>3);
270 printf("\t current bindings = %p\n", current_bindings);
272 TAILQ_FOREACH(binding, current_bindings, bindings) {
273 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
274 binding->mods, binding->keycode, binding->symbol, binding->command);
277 struct Mode *mode = scalloc(sizeof(struct Mode));
278 mode->name = strdup($<string>3);
279 mode->bindings = current_bindings;
280 current_bindings = NULL;
281 SLIST_INSERT_HEAD(&modes, mode, modes);
287 | modelines WHITESPACE modeline
295 if (current_bindings == NULL) {
296 current_bindings = scalloc(sizeof(struct bindings_head));
297 TAILQ_INIT(current_bindings);
300 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
305 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
307 LOG("floating modifier = %d\n", $<number>3);
308 config.floating_modifier = $<number>3;
313 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
315 int ws_num = $<number>3;
317 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
319 Workspace *ws = workspace_get(ws_num - 1);
320 ws->preferred_screen = sstrdup($<string>7);
321 if ($<string>8 != NULL)
322 workspace_set_name(ws, $<string>8);
325 | TOKWORKSPACE WHITESPACE NUMBER workspace_name
327 int ws_num = $<number>3;
329 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
331 if ($<string>4 != NULL)
332 workspace_set_name(workspace_get(ws_num - 1), $<string>4);
338 /* NULL */ { $<string>$ = NULL; }
339 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
340 | WHITESPACE STR { $<string>$ = $<string>2; }
344 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
345 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
346 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
347 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
351 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
353 printf("assignment of %s to %d\n", $<string>3, $<number>6);
355 struct Assignment *new = $<assignment>6;
356 new->windowclass_title = strdup($<string>3);
357 TAILQ_INSERT_TAIL(&assignments, new, assignments);
364 struct Assignment *new = scalloc(sizeof(struct Assignment));
365 new->workspace = $<number>1;
366 new->floating = ASSIGN_FLOATING_NO;
367 $<assignment>$ = new;
371 struct Assignment *new = scalloc(sizeof(struct Assignment));
372 new->floating = ASSIGN_FLOATING_ONLY;
373 $<assignment>$ = new;
377 struct Assignment *new = scalloc(sizeof(struct Assignment));
378 new->workspace = $<number>2;
379 new->floating = ASSIGN_FLOATING;
380 $<assignment>$ = new;
391 | TOKARROW WHITESPACE
395 TOKIPCSOCKET WHITESPACE STR
397 config.ipc_socket_path = sstrdup($<string>3);
402 TOKEXEC WHITESPACE STR
404 struct Autostart *new = smalloc(sizeof(struct Autostart));
405 new->command = sstrdup($<string>3);
406 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
411 TOKTERMINAL WHITESPACE STR
413 config.terminal = sstrdup($<string>3);
414 printf("terminal %s\n", config.terminal);
419 TOKFONT WHITESPACE STR
421 config.font = sstrdup($<string>3);
422 printf("font %s\n", config.font);
428 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
430 struct Colortriple *dest = $<color>1;
432 dest->border = $<number>3;
433 dest->background = $<number>5;
434 dest->text = $<number>7;
439 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
444 /* NULL */ { $<number>$ = 0; }
446 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
447 | binding_modifiers '+' { $<number>$ = $<number>1; }
451 MODIFIER { $<number>$ = $<number>1; }
452 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
453 | TOKSHIFT { $<number>$ = BIND_SHIFT; }