18 #include "workspace.h"
21 extern int yylex(void);
26 void yyerror(const char *str) {
27 fprintf(stderr,"error: %s\n",str);
34 void parse_file(const char *f) {
35 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
36 int fd, ret, read_bytes = 0;
40 char buffer[1026], key[512], value[512];
42 if ((fd = open(f, O_RDONLY)) == -1)
43 die("Could not open configuration file: %s\n", strerror(errno));
45 if (fstat(fd, &stbuf) == -1)
46 die("Could not fstat file: %s\n", strerror(errno));
48 buf = smalloc(stbuf.st_size * sizeof(char));
49 while (read_bytes < stbuf.st_size) {
50 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
51 die("Could not read(): %s\n", strerror(errno));
55 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
56 die("Could not lseek: %s\n", strerror(errno));
58 if ((fstr = fdopen(fd, "r")) == NULL)
59 die("Could not fdopen: %s\n", strerror(errno));
62 if (fgets(buffer, 1024, fstr) == NULL) {
65 die("Could not read configuration file\n");
68 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
69 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
70 key[0] == '#' || strlen(key) < 3)
73 if (strcasecmp(key, "set") == 0) {
75 die("Malformed variable assignment, name has to start with $\n");
77 /* get key/value for this variable */
78 char *v_key = value, *v_value;
79 if ((v_value = strstr(value, " ")) == NULL)
80 die("Malformed variable assignment, need a value\n");
84 struct Variable *new = scalloc(sizeof(struct Variable));
85 new->key = sstrdup(v_key);
86 new->value = sstrdup(v_value);
87 SLIST_INSERT_HEAD(&variables, new, variables);
88 LOG("Got new variable %s = %s\n", v_key, v_value);
93 /* For every custom variable, see how often it occurs in the file and
94 * how much extra bytes it requires when replaced. */
95 struct Variable *current, *nearest;
97 SLIST_FOREACH(current, &variables, variables) {
98 int extra = (strlen(current->value) - strlen(current->key));
101 (next = strcasestr(buf + (next - buf), current->key)) != NULL;
102 next += strlen(current->key))
103 extra_bytes += extra;
106 /* Then, allocate a new buffer and copy the file over to the new one,
107 * but replace occurences of our variables */
108 char *walk = buf, *destwalk;
109 char *new = smalloc((stbuf.st_size + extra_bytes) * sizeof(char));
111 while (walk < (buf + stbuf.st_size)) {
112 /* Find the next variable */
113 SLIST_FOREACH(current, &variables, variables)
114 current->next_match = strcasestr(walk, current->key);
116 int distance = stbuf.st_size;
117 SLIST_FOREACH(current, &variables, variables) {
118 if (current->next_match == NULL)
120 if ((current->next_match - walk) < distance) {
121 distance = (current->next_match - walk);
125 if (nearest == NULL) {
126 /* If there are no more variables, we just copy the rest */
127 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
128 destwalk += (buf + stbuf.st_size) - walk;
132 /* Copy until the next variable, then copy its value */
133 strncpy(destwalk, walk, distance);
134 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
135 walk += distance + strlen(nearest->key);
136 destwalk += distance + strlen(nearest->value);
142 if (yyparse() != 0) {
143 fprintf(stderr, "Could not parse configfile\n");
156 struct Colortriple *color;
157 struct Assignment *assignment;
160 %token <number>NUMBER
163 %token <string>STR_NG
174 %token TOKFLOATING_MODIFIER
188 | lines WHITESPACE line
215 TOKBIND WHITESPACE binding_modifiers NUMBER WHITESPACE command
217 printf("\tFound binding mod%d with key %d and command %s\n", $<number>3, $4, $<string>6);
218 Binding *new = scalloc(sizeof(Binding));
220 new->keycode = $<number>4;
221 new->mods = $<number>3;
222 new->command = sstrdup($<string>6);
224 TAILQ_INSERT_TAIL(&bindings, new, bindings);
229 TOKBINDSYM WHITESPACE binding_modifiers WORD WHITESPACE command
231 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>3, $4, $<string>6);
232 Binding *new = scalloc(sizeof(Binding));
234 new->symbol = sstrdup($4);
235 new->mods = $<number>3;
236 new->command = sstrdup($<string>6);
238 TAILQ_INSERT_TAIL(&bindings, new, bindings);
243 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
245 LOG("floating modifier = %d\n", $<number>3);
246 config.floating_modifier = $<number>3;
251 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
253 int ws_num = $<number>3;
254 if (ws_num < 1 || ws_num > 10) {
255 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
257 workspaces[ws_num - 1].preferred_screen = sstrdup($<string>7);
258 if ($<string>8 != NULL)
259 workspace_set_name(&(workspaces[ws_num - 1]), $<string>8);
262 | TOKWORKSPACE WHITESPACE NUMBER workspace_name
264 int ws_num = $<number>3;
265 if (ws_num < 1 || ws_num > 10) {
266 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
268 if ($<string>4 != NULL)
269 workspace_set_name(&(workspaces[ws_num - 1]), $<string>4);
275 /* NULL */ { $<string>$ = NULL; }
276 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
277 | WHITESPACE STR { $<string>$ = $<string>2; }
281 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
282 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
283 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
284 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
288 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
290 printf("assignment of %s to %d\n", $<string>3, $<number>6);
292 struct Assignment *new = $<assignment>6;
293 new->windowclass_title = strdup($<string>3);
294 TAILQ_INSERT_TAIL(&assignments, new, assignments);
301 struct Assignment *new = scalloc(sizeof(struct Assignment));
302 new->workspace = $<number>1;
303 new->floating = ASSIGN_FLOATING_NO;
304 $<assignment>$ = new;
308 struct Assignment *new = scalloc(sizeof(struct Assignment));
309 new->floating = ASSIGN_FLOATING_ONLY;
310 $<assignment>$ = new;
314 struct Assignment *new = scalloc(sizeof(struct Assignment));
315 new->workspace = $<number>2;
316 new->floating = ASSIGN_FLOATING;
317 $<assignment>$ = new;
328 | TOKARROW WHITESPACE
332 TOKIPCSOCKET WHITESPACE STR
334 config.ipc_socket_path = sstrdup($<string>3);
339 TOKEXEC WHITESPACE STR
341 struct Autostart *new = smalloc(sizeof(struct Autostart));
342 new->command = sstrdup($<string>3);
343 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
348 TOKTERMINAL WHITESPACE STR
350 config.terminal = sstrdup($<string>3);
351 printf("terminal %s\n", config.terminal);
356 TOKFONT WHITESPACE STR
358 config.font = sstrdup($<string>3);
359 printf("font %s\n", config.font);
365 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
367 struct Colortriple *dest = $<color>1;
369 dest->border = $<number>3;
370 dest->background = $<number>5;
371 dest->text = $<number>7;
376 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
381 /* NULL */ { $<number>$ = 0; }
383 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
384 | binding_modifiers '+' { $<number>$ = $<number>1; }
388 MODIFIER { $<number>$ = $<number>1; }
389 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
390 | TOKSHIFT { $<number>$ = BIND_SHIFT; }