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 printf("\t now in mode %s\n", $<string>3);
266 printf("\t current bindings = %p\n", current_bindings);
272 | modelines WHITESPACE modeline
280 if (current_bindings == NULL) {
281 current_bindings = scalloc(sizeof(struct bindings_head));
282 TAILQ_INIT(current_bindings);
285 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
290 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
292 LOG("floating modifier = %d\n", $<number>3);
293 config.floating_modifier = $<number>3;
298 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
300 int ws_num = $<number>3;
301 if (ws_num < 1 || ws_num > 10) {
302 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
304 workspaces[ws_num - 1].preferred_screen = sstrdup($<string>7);
305 if ($<string>8 != NULL)
306 workspace_set_name(&(workspaces[ws_num - 1]), $<string>8);
309 | TOKWORKSPACE WHITESPACE NUMBER workspace_name
311 int ws_num = $<number>3;
312 if (ws_num < 1 || ws_num > 10) {
313 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
315 if ($<string>4 != NULL)
316 workspace_set_name(&(workspaces[ws_num - 1]), $<string>4);
322 /* NULL */ { $<string>$ = NULL; }
323 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
324 | WHITESPACE STR { $<string>$ = $<string>2; }
328 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
329 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
330 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
331 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
335 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
337 printf("assignment of %s to %d\n", $<string>3, $<number>6);
339 struct Assignment *new = $<assignment>6;
340 new->windowclass_title = strdup($<string>3);
341 TAILQ_INSERT_TAIL(&assignments, new, assignments);
348 struct Assignment *new = scalloc(sizeof(struct Assignment));
349 new->workspace = $<number>1;
350 new->floating = ASSIGN_FLOATING_NO;
351 $<assignment>$ = new;
355 struct Assignment *new = scalloc(sizeof(struct Assignment));
356 new->floating = ASSIGN_FLOATING_ONLY;
357 $<assignment>$ = new;
361 struct Assignment *new = scalloc(sizeof(struct Assignment));
362 new->workspace = $<number>2;
363 new->floating = ASSIGN_FLOATING;
364 $<assignment>$ = new;
375 | TOKARROW WHITESPACE
379 TOKIPCSOCKET WHITESPACE STR
381 config.ipc_socket_path = sstrdup($<string>3);
386 TOKEXEC WHITESPACE STR
388 struct Autostart *new = smalloc(sizeof(struct Autostart));
389 new->command = sstrdup($<string>3);
390 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
395 TOKTERMINAL WHITESPACE STR
397 config.terminal = sstrdup($<string>3);
398 printf("terminal %s\n", config.terminal);
403 TOKFONT WHITESPACE STR
405 config.font = sstrdup($<string>3);
406 printf("font %s\n", config.font);
412 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
414 struct Colortriple *dest = $<color>1;
416 dest->border = $<number>3;
417 dest->background = $<number>5;
418 dest->text = $<number>7;
423 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
428 /* NULL */ { $<number>$ = 0; }
430 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
431 | binding_modifiers '+' { $<number>$ = $<number>1; }
435 MODIFIER { $<number>$ = $<number>1; }
436 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
437 | TOKSHIFT { $<number>$ = BIND_SHIFT; }