19 extern int yylex(void);
24 void yyerror(const char *str) {
25 fprintf(stderr,"error: %s\n",str);
32 void parse_file(const char *f) {
33 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
34 int fd, ret, read_bytes = 0;
38 char buffer[1026], key[512], value[512];
40 if ((fd = open(f, O_RDONLY)) == -1)
41 die("Could not open configuration file: %s\n", strerror(errno));
43 if (fstat(fd, &stbuf) == -1)
44 die("Could not fstat file: %s\n", strerror(errno));
46 buf = smalloc(stbuf.st_size * sizeof(char));
47 while (read_bytes < stbuf.st_size) {
48 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
49 die("Could not read(): %s\n", strerror(errno));
53 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
54 die("Could not lseek: %s\n", strerror(errno));
56 if ((fstr = fdopen(fd, "r")) == NULL)
57 die("Could not fdopen: %s\n", strerror(errno));
60 if (fgets(buffer, 1024, fstr) == NULL) {
63 die("Could not read configuration file\n");
66 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
67 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
68 key[0] == '#' || strlen(key) < 3)
71 if (strcasecmp(key, "set") == 0) {
73 die("Malformed variable assignment, name has to start with $\n");
75 /* get key/value for this variable */
76 char *v_key = value, *v_value;
77 if ((v_value = strstr(value, " ")) == NULL)
78 die("Malformed variable assignment, need a value\n");
82 struct Variable *new = scalloc(sizeof(struct Variable));
83 new->key = sstrdup(v_key);
84 new->value = sstrdup(v_value);
85 SLIST_INSERT_HEAD(&variables, new, variables);
86 LOG("Got new variable %s = %s\n", v_key, v_value);
91 /* For every custom variable, see how often it occurs in the file and
92 * how much extra bytes it requires when replaced. */
93 struct Variable *current, *nearest;
95 SLIST_FOREACH(current, &variables, variables) {
96 int extra = (strlen(current->value) - strlen(current->key));
99 (next = strcasestr(buf + (next - buf), current->key)) != NULL;
100 next += strlen(current->key))
101 extra_bytes += extra;
104 /* Then, allocate a new buffer and copy the file over to the new one,
105 * but replace occurences of our variables */
106 char *walk = buf, *destwalk;
107 char *new = smalloc((stbuf.st_size + extra_bytes) * sizeof(char));
109 while (walk < (buf + stbuf.st_size)) {
110 /* Find the next variable */
111 SLIST_FOREACH(current, &variables, variables)
112 current->next_match = strcasestr(walk, current->key);
114 int distance = stbuf.st_size;
115 SLIST_FOREACH(current, &variables, variables) {
116 if (current->next_match == NULL)
118 if ((current->next_match - walk) < distance) {
119 distance = (current->next_match - walk);
123 if (nearest == NULL) {
124 /* If there are no more variables, we just copy the rest */
125 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
126 destwalk += (buf + stbuf.st_size) - walk;
130 /* Copy until the next variable, then copy its value */
131 strncpy(destwalk, walk, distance);
132 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
133 walk += distance + strlen(nearest->key);
134 destwalk += distance + strlen(nearest->value);
140 if (yyparse() != 0) {
141 fprintf(stderr, "Could not parse configfile\n");
154 struct Colortriple *color;
155 struct Assignment *assignment;
158 %token <number>NUMBER
161 %token <string>STR_NG
172 %token TOKFLOATING_MODIFIER
186 | lines WHITESPACE line
213 TOKBIND WHITESPACE binding_modifiers NUMBER WHITESPACE command
215 printf("\tFound binding mod%d with key %d and command %s\n", $<number>3, $4, $<string>6);
216 Binding *new = scalloc(sizeof(Binding));
218 new->keycode = $<number>4;
219 new->mods = $<number>3;
220 new->command = sstrdup($<string>6);
222 TAILQ_INSERT_TAIL(&bindings, new, bindings);
227 TOKBINDSYM WHITESPACE binding_modifiers WORD WHITESPACE command
229 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>3, $4, $<string>6);
230 Binding *new = scalloc(sizeof(Binding));
232 new->symbol = sstrdup($4);
233 new->mods = $<number>3;
234 new->command = sstrdup($<string>6);
236 TAILQ_INSERT_TAIL(&bindings, new, bindings);
241 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
243 LOG("floating modifier = %d\n", $<number>3);
244 config.floating_modifier = $<number>3;
249 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
251 int ws_num = $<number>3;
252 if (ws_num < 1 || ws_num > 10) {
253 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
255 workspaces[ws_num - 1].preferred_screen = sstrdup($<string>7);
256 if ($<string>8 != NULL)
257 workspace_set_name(&(workspaces[ws_num - 1]), $<string>8);
263 /* NULL */ { $<string>$ = NULL; }
264 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
265 | WHITESPACE STR { $<string>$ = $<string>2; }
269 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
270 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
271 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
272 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
276 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
278 printf("assignment of %s to %d\n", $<string>3, $<number>6);
280 struct Assignment *new = $<assignment>6;
281 new->windowclass_title = strdup($<string>3);
282 TAILQ_INSERT_TAIL(&assignments, new, assignments);
289 struct Assignment *new = scalloc(sizeof(struct Assignment));
290 new->workspace = $<number>1;
291 new->floating = ASSIGN_FLOATING_NO;
292 $<assignment>$ = new;
296 struct Assignment *new = scalloc(sizeof(struct Assignment));
297 new->floating = ASSIGN_FLOATING_ONLY;
298 $<assignment>$ = new;
302 struct Assignment *new = scalloc(sizeof(struct Assignment));
303 new->workspace = $<number>2;
304 new->floating = ASSIGN_FLOATING;
305 $<assignment>$ = new;
316 | TOKARROW WHITESPACE
320 TOKIPCSOCKET WHITESPACE STR
322 config.ipc_socket_path = sstrdup($<string>3);
327 TOKEXEC WHITESPACE STR
329 struct Autostart *new = smalloc(sizeof(struct Autostart));
330 new->command = sstrdup($<string>3);
331 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
336 TOKTERMINAL WHITESPACE STR
338 config.terminal = sstrdup($<string>3);
339 printf("terminal %s\n", config.terminal);
344 TOKFONT WHITESPACE STR
346 config.font = sstrdup($<string>3);
347 printf("font %s\n", config.font);
353 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
355 struct Colortriple *dest = $<color>1;
357 dest->border = $<number>3;
358 dest->background = $<number>5;
359 dest->text = $<number>7;
364 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
369 /* NULL */ { $<number>$ = 0; }
371 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
372 | binding_modifiers '+' { $<number>$ = $<number>1; }
376 MODIFIER { $<number>$ = $<number>1; }
377 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
378 | TOKSHIFT { $<number>$ = BIND_SHIFT; }