22 #include "workspace.h"
25 extern int yylex(void);
30 void yyerror(const char *str) {
31 fprintf(stderr,"error: %s\n",str);
38 void parse_file(const char *f) {
39 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
40 int fd, ret, read_bytes = 0;
44 char buffer[1026], key[512], value[512];
46 if ((fd = open(f, O_RDONLY)) == -1)
47 die("Could not open configuration file: %s\n", strerror(errno));
49 if (fstat(fd, &stbuf) == -1)
50 die("Could not fstat file: %s\n", strerror(errno));
52 buf = smalloc(stbuf.st_size * sizeof(char));
53 while (read_bytes < stbuf.st_size) {
54 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
55 die("Could not read(): %s\n", strerror(errno));
59 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
60 die("Could not lseek: %s\n", strerror(errno));
62 if ((fstr = fdopen(fd, "r")) == NULL)
63 die("Could not fdopen: %s\n", strerror(errno));
66 if (fgets(buffer, 1024, fstr) == NULL) {
69 die("Could not read configuration file\n");
72 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
73 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
74 key[0] == '#' || strlen(key) < 3)
77 if (strcasecmp(key, "set") == 0) {
79 die("Malformed variable assignment, name has to start with $\n");
81 /* get key/value for this variable */
82 char *v_key = value, *v_value;
83 if ((v_value = strstr(value, " ")) == NULL)
84 die("Malformed variable assignment, need a value\n");
88 struct Variable *new = scalloc(sizeof(struct Variable));
89 new->key = sstrdup(v_key);
90 new->value = sstrdup(v_value);
91 SLIST_INSERT_HEAD(&variables, new, variables);
92 LOG("Got new variable %s = %s\n", v_key, v_value);
97 /* For every custom variable, see how often it occurs in the file and
98 * how much extra bytes it requires when replaced. */
99 struct Variable *current, *nearest;
101 SLIST_FOREACH(current, &variables, variables) {
102 int extra = (strlen(current->value) - strlen(current->key));
105 (next = strcasestr(buf + (next - buf), current->key)) != NULL;
106 next += strlen(current->key))
107 extra_bytes += extra;
110 /* Then, allocate a new buffer and copy the file over to the new one,
111 * but replace occurences of our variables */
112 char *walk = buf, *destwalk;
113 char *new = smalloc((stbuf.st_size + extra_bytes) * sizeof(char));
115 while (walk < (buf + stbuf.st_size)) {
116 /* Find the next variable */
117 SLIST_FOREACH(current, &variables, variables)
118 current->next_match = strcasestr(walk, current->key);
120 int distance = stbuf.st_size;
121 SLIST_FOREACH(current, &variables, variables) {
122 if (current->next_match == NULL)
124 if ((current->next_match - walk) < distance) {
125 distance = (current->next_match - walk);
129 if (nearest == NULL) {
130 /* If there are no more variables, we just copy the rest */
131 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
132 destwalk += (buf + stbuf.st_size) - walk;
136 /* Copy until the next variable, then copy its value */
137 strncpy(destwalk, walk, distance);
138 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
139 walk += distance + strlen(nearest->key);
140 destwalk += distance + strlen(nearest->value);
146 if (yyparse() != 0) {
147 fprintf(stderr, "Could not parse configfile\n");
160 struct Colortriple *color;
161 struct Assignment *assignment;
164 %token <number>NUMBER
167 %token <string>STR_NG
178 %token TOKFLOATING_MODIFIER
192 | lines WHITESPACE line
219 TOKBIND WHITESPACE binding_modifiers NUMBER WHITESPACE command
221 printf("\tFound binding mod%d with key %d and command %s\n", $<number>3, $4, $<string>6);
222 Binding *new = scalloc(sizeof(Binding));
224 new->keycode = $<number>4;
225 new->mods = $<number>3;
226 new->command = sstrdup($<string>6);
228 TAILQ_INSERT_TAIL(&bindings, new, bindings);
233 TOKBINDSYM WHITESPACE binding_modifiers WORD WHITESPACE command
235 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>3, $4, $<string>6);
236 Binding *new = scalloc(sizeof(Binding));
238 new->symbol = sstrdup($4);
239 new->mods = $<number>3;
240 new->command = sstrdup($<string>6);
242 TAILQ_INSERT_TAIL(&bindings, new, bindings);
247 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
249 LOG("floating modifier = %d\n", $<number>3);
250 config.floating_modifier = $<number>3;
255 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
257 int ws_num = $<number>3;
258 if (ws_num < 1 || ws_num > 10) {
259 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
261 workspaces[ws_num - 1].preferred_screen = sstrdup($<string>7);
262 if ($<string>8 != NULL)
263 workspace_set_name(&(workspaces[ws_num - 1]), $<string>8);
266 | TOKWORKSPACE WHITESPACE NUMBER workspace_name
268 int ws_num = $<number>3;
269 if (ws_num < 1 || ws_num > 10) {
270 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
272 if ($<string>4 != NULL)
273 workspace_set_name(&(workspaces[ws_num - 1]), $<string>4);
279 /* NULL */ { $<string>$ = NULL; }
280 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
281 | WHITESPACE STR { $<string>$ = $<string>2; }
285 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
286 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
287 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
288 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
292 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
294 printf("assignment of %s to %d\n", $<string>3, $<number>6);
296 struct Assignment *new = $<assignment>6;
297 new->windowclass_title = strdup($<string>3);
298 TAILQ_INSERT_TAIL(&assignments, new, assignments);
305 struct Assignment *new = scalloc(sizeof(struct Assignment));
306 new->workspace = $<number>1;
307 new->floating = ASSIGN_FLOATING_NO;
308 $<assignment>$ = new;
312 struct Assignment *new = scalloc(sizeof(struct Assignment));
313 new->floating = ASSIGN_FLOATING_ONLY;
314 $<assignment>$ = new;
318 struct Assignment *new = scalloc(sizeof(struct Assignment));
319 new->workspace = $<number>2;
320 new->floating = ASSIGN_FLOATING;
321 $<assignment>$ = new;
332 | TOKARROW WHITESPACE
336 TOKIPCSOCKET WHITESPACE STR
338 config.ipc_socket_path = sstrdup($<string>3);
343 TOKEXEC WHITESPACE STR
345 struct Autostart *new = smalloc(sizeof(struct Autostart));
346 new->command = sstrdup($<string>3);
347 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
352 TOKTERMINAL WHITESPACE STR
354 config.terminal = sstrdup($<string>3);
355 printf("terminal %s\n", config.terminal);
360 TOKFONT WHITESPACE STR
362 config.font = sstrdup($<string>3);
363 printf("font %s\n", config.font);
369 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
371 struct Colortriple *dest = $<color>1;
373 dest->border = $<number>3;
374 dest->background = $<number>5;
375 dest->text = $<number>7;
380 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
385 /* NULL */ { $<number>$ = 0; }
387 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
388 | binding_modifiers '+' { $<number>$ = $<number>1; }
392 MODIFIER { $<number>$ = $<number>1; }
393 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
394 | TOKSHIFT { $<number>$ = BIND_SHIFT; }