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;
157 %token <number>NUMBER
160 %token <string>STR_NG
171 %token TOKFLOATING_MODIFIER
185 | lines WHITESPACE line
212 TOKBIND WHITESPACE binding_modifiers NUMBER WHITESPACE command
214 printf("\tFound binding mod%d with key %d and command %s\n", $<number>3, $4, $<string>6);
215 Binding *new = scalloc(sizeof(Binding));
217 new->keycode = $<number>4;
218 new->mods = $<number>3;
219 new->command = sstrdup($<string>6);
221 TAILQ_INSERT_TAIL(&bindings, new, bindings);
226 TOKBINDSYM WHITESPACE binding_modifiers WORD WHITESPACE command
228 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>3, $4, $<string>6);
229 Binding *new = scalloc(sizeof(Binding));
231 new->symbol = sstrdup($4);
232 new->mods = $<number>3;
233 new->command = sstrdup($<string>6);
235 TAILQ_INSERT_TAIL(&bindings, new, bindings);
240 TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
242 LOG("floating modifier = %d\n", $<number>3);
243 config.floating_modifier = $<number>3;
248 TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
250 int ws_num = $<number>3;
251 if (ws_num < 1 || ws_num > 10) {
252 LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
254 workspaces[ws_num - 1].preferred_screen = sstrdup($<string>7);
255 if ($<string>8 != NULL)
256 workspace_set_name(&(workspaces[ws_num - 1]), $<string>8);
262 /* NULL */ { $<string>$ = NULL; }
263 | WHITESPACE QUOTEDSTRING { $<string>$ = $<string>2; }
264 | WHITESPACE STR { $<string>$ = $<string>2; }
268 NUMBER { asprintf(&$<string>$, "%d", $<number>1); }
269 | NUMBER 'x' { asprintf(&$<string>$, "%d", $<number>1); }
270 | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
271 | 'x' NUMBER { asprintf(&$<string>$, "x%d", $<number>2); }
275 TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow NUMBER
278 printf("assignment of %s to %d\n", $<string>3, $<number>6);
289 | TOKARROW WHITESPACE
293 TOKIPCSOCKET WHITESPACE STR
295 config.ipc_socket_path = sstrdup($<string>3);
300 TOKEXEC WHITESPACE STR
302 struct Autostart *new = smalloc(sizeof(struct Autostart));
303 new->command = sstrdup($<string>3);
304 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
309 TOKTERMINAL WHITESPACE STR
311 config.terminal = sstrdup($<string>3);
312 printf("terminal %s\n", config.terminal);
317 TOKFONT WHITESPACE STR
319 config.font = sstrdup($<string>3);
320 printf("font %s\n", config.font);
326 TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
328 struct Colortriple *dest = $<color>1;
330 dest->border = $<number>3;
331 dest->background = $<number>5;
332 dest->text = $<number>7;
337 '#' HEX { $<number>$ = get_colorpixel(global_conn, $<string>2); }
342 /* NULL */ { $<number>$ = 0; }
344 | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
345 | binding_modifiers '+' { $<number>$ = $<number>1; }
349 MODIFIER { $<number>$ = $<number>1; }
350 | TOKCONTROL { $<number>$ = BIND_CONTROL; }
351 | TOKSHIFT { $<number>$ = BIND_SHIFT; }