]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
Get rid of the remaining hardcoded height.
[i3/i3] / src / cfgparse.y
1 %{
2 /*
3  * vim:ts=8:expandtab
4  *
5  */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include "all.h"
12
13 typedef struct yy_buffer_state *YY_BUFFER_STATE;
14 extern int yylex(struct context *context);
15 extern int yyparse(void);
16 extern FILE *yyin;
17 YY_BUFFER_STATE yy_scan_string(const char *);
18
19 static struct bindings_head *current_bindings;
20 static struct context *context;
21
22 /* We don’t need yydebug for now, as we got decent error messages using
23  * yyerror(). Should you ever want to extend the parser, it might be handy
24  * to just comment it in again, so it stays here. */
25 //int yydebug = 1;
26
27 void yyerror(const char *error_message) {
28         ELOG("\n");
29         ELOG("CONFIG: %s\n", error_message);
30         ELOG("CONFIG: in file \"%s\", line %d:\n",
31                 context->filename, context->line_number);
32         ELOG("CONFIG:   %s\n", context->line_copy);
33         ELOG("CONFIG:   ");
34         for (int c = 1; c <= context->last_column; c++)
35                 if (c >= context->first_column)
36                         printf("^");
37                 else printf(" ");
38         printf("\n");
39         ELOG("\n");
40 }
41
42 int yywrap() {
43         return 1;
44 }
45
46 void parse_file(const char *f) {
47         SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
48         int fd, ret, read_bytes = 0;
49         struct stat stbuf;
50         char *buf;
51         FILE *fstr;
52         char buffer[1026], key[512], value[512];
53
54         if ((fd = open(f, O_RDONLY)) == -1)
55                 die("Could not open configuration file: %s\n", strerror(errno));
56
57         if (fstat(fd, &stbuf) == -1)
58                 die("Could not fstat file: %s\n", strerror(errno));
59
60         buf = scalloc((stbuf.st_size + 1) * sizeof(char));
61         while (read_bytes < stbuf.st_size) {
62                 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
63                         die("Could not read(): %s\n", strerror(errno));
64                 read_bytes += ret;
65         }
66
67         if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
68                 die("Could not lseek: %s\n", strerror(errno));
69
70         if ((fstr = fdopen(fd, "r")) == NULL)
71                 die("Could not fdopen: %s\n", strerror(errno));
72
73         while (!feof(fstr)) {
74                 if (fgets(buffer, 1024, fstr) == NULL) {
75                         if (feof(fstr))
76                                 break;
77                         die("Could not read configuration file\n");
78                 }
79
80                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
81                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
82                     key[0] == '#' || strlen(key) < 3)
83                         continue;
84
85                 if (strcasecmp(key, "set") == 0) {
86                         if (value[0] != '$')
87                                 die("Malformed variable assignment, name has to start with $\n");
88
89                         /* get key/value for this variable */
90                         char *v_key = value, *v_value;
91                         if ((v_value = strstr(value, " ")) == NULL)
92                                 die("Malformed variable assignment, need a value\n");
93
94                         *(v_value++) = '\0';
95
96                         struct Variable *new = scalloc(sizeof(struct Variable));
97                         new->key = sstrdup(v_key);
98                         new->value = sstrdup(v_value);
99                         SLIST_INSERT_HEAD(&variables, new, variables);
100                         DLOG("Got new variable %s = %s\n", v_key, v_value);
101                         continue;
102                 }
103         }
104
105         /* For every custom variable, see how often it occurs in the file and
106          * how much extra bytes it requires when replaced. */
107         struct Variable *current, *nearest;
108         int extra_bytes = 0;
109         SLIST_FOREACH(current, &variables, variables) {
110                 int extra = (strlen(current->value) - strlen(current->key));
111                 char *next;
112                 for (next = buf;
113                      (next = strcasestr(buf + (next - buf), current->key)) != NULL;
114                      next += strlen(current->key))
115                         extra_bytes += extra;
116         }
117
118         /* Then, allocate a new buffer and copy the file over to the new one,
119          * but replace occurences of our variables */
120         char *walk = buf, *destwalk;
121         char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
122         destwalk = new;
123         while (walk < (buf + stbuf.st_size)) {
124                 /* Find the next variable */
125                 SLIST_FOREACH(current, &variables, variables)
126                         current->next_match = strcasestr(walk, current->key);
127                 nearest = NULL;
128                 int distance = stbuf.st_size;
129                 SLIST_FOREACH(current, &variables, variables) {
130                         if (current->next_match == NULL)
131                                 continue;
132                         if ((current->next_match - walk) < distance) {
133                                 distance = (current->next_match - walk);
134                                 nearest = current;
135                         }
136                 }
137                 if (nearest == NULL) {
138                         /* If there are no more variables, we just copy the rest */
139                         strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
140                         destwalk += (buf + stbuf.st_size) - walk;
141                         *destwalk = '\0';
142                         break;
143                 } else {
144                         /* Copy until the next variable, then copy its value */
145                         strncpy(destwalk, walk, distance);
146                         strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
147                         walk += distance + strlen(nearest->key);
148                         destwalk += distance + strlen(nearest->value);
149                 }
150         }
151
152         yy_scan_string(new);
153
154         context = scalloc(sizeof(struct context));
155         context->filename = f;
156
157         if (yyparse() != 0) {
158                 fprintf(stderr, "Could not parse configfile\n");
159                 exit(1);
160         }
161
162         FREE(context->line_copy);
163         free(context);
164         free(new);
165         free(buf);
166
167         while (!SLIST_EMPTY(&variables)) {
168                 current = SLIST_FIRST(&variables);
169                 FREE(current->key);
170                 FREE(current->value);
171                 SLIST_REMOVE_HEAD(&variables, variables);
172                 FREE(current);
173         }
174 }
175
176 %}
177
178 %expect 1
179 %error-verbose
180 %lex-param { struct context *context }
181
182 %union {
183         int number;
184         char *string;
185         uint32_t *single_color;
186         struct Colortriple *color;
187         struct Assignment *assignment;
188         struct Binding *binding;
189 }
190
191 %token <number>NUMBER "<number>"
192 %token <string>WORD "<word>"
193 %token <string>STR "<string>"
194 %token <string>STR_NG "<string (non-greedy)>"
195 %token <string>HEX "<hex>"
196 %token <string>OUTPUT "<RandR output>"
197 %token TOKBIND
198 %token TOKTERMINAL
199 %token TOKCOMMENT "<comment>"
200 %token TOKFONT "font"
201 %token TOKBINDSYM "bindsym"
202 %token MODIFIER "<modifier>"
203 %token TOKCONTROL "control"
204 %token TOKSHIFT "shift"
205 %token WHITESPACE "<whitespace>"
206 %token TOKFLOATING_MODIFIER "floating_modifier"
207 %token QUOTEDSTRING "<quoted string>"
208 %token TOKWORKSPACE "workspace"
209 %token TOKOUTPUT "output"
210 %token TOKASSIGN "assign"
211 %token TOKSET
212 %token TOKIPCSOCKET "ipc_socket"
213 %token TOKEXEC "exec"
214 %token TOKSINGLECOLOR
215 %token TOKCOLOR
216 %token TOKARROW "→"
217 %token TOKMODE "mode"
218 %token TOKNEWCONTAINER "new_container"
219 %token TOKNEWWINDOW "new_window"
220 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
221 %token TOKWORKSPACEBAR "workspace_bar"
222 %token TOKCONTAINERMODE "default/stacking/tabbed"
223 %token TOKSTACKLIMIT "stack-limit"
224
225 %%
226
227 lines: /* empty */
228         | lines WHITESPACE line
229         | lines error
230         | lines line
231         ;
232
233 line:
234         bindline
235         | mode
236         | floating_modifier
237         | new_container
238         | new_window
239         | focus_follows_mouse
240         | workspace_bar
241         | workspace
242         | assign
243         | ipcsocket
244         | exec
245         | single_color
246         | color
247         | terminal
248         | font
249         | comment
250         ;
251
252 comment:
253         TOKCOMMENT
254         ;
255
256 command:
257         STR
258         ;
259
260 bindline:
261         binding
262         {
263                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
264         }
265         ;
266
267 binding:
268         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
269         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
270         ;
271
272 bind:
273         binding_modifiers NUMBER WHITESPACE command
274         {
275                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
276                 Binding *new = scalloc(sizeof(Binding));
277
278                 new->keycode = $<number>2;
279                 new->mods = $<number>1;
280                 new->command = $<string>4;
281
282                 $<binding>$ = new;
283         }
284         ;
285
286 bindsym:
287         binding_modifiers word_or_number WHITESPACE command
288         {
289                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
290                 Binding *new = scalloc(sizeof(Binding));
291
292                 new->symbol = $<string>2;
293                 new->mods = $<number>1;
294                 new->command = $<string>4;
295
296                 $<binding>$ = new;
297         }
298         ;
299
300 word_or_number:
301         WORD
302         | NUMBER
303         {
304                 asprintf(&$<string>$, "%d", $1);
305         }
306         ;
307
308 mode:
309         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
310         {
311                 if (strcasecmp($<string>3, "default") == 0) {
312                         printf("You cannot use the name \"default\" for your mode\n");
313                         exit(1);
314                 }
315                 printf("\t now in mode %s\n", $<string>3);
316                 printf("\t current bindings = %p\n", current_bindings);
317                 Binding *binding;
318                 TAILQ_FOREACH(binding, current_bindings, bindings) {
319                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
320                                         binding->mods, binding->keycode, binding->symbol, binding->command);
321                 }
322
323                 struct Mode *mode = scalloc(sizeof(struct Mode));
324                 mode->name = $<string>3;
325                 mode->bindings = current_bindings;
326                 current_bindings = NULL;
327                 SLIST_INSERT_HEAD(&modes, mode, modes);
328         }
329         ;
330
331
332 modelines:
333         /* empty */
334         | modelines modeline
335         ;
336
337 modeline:
338         WHITESPACE
339         | comment
340         | binding
341         {
342                 if (current_bindings == NULL) {
343                         current_bindings = scalloc(sizeof(struct bindings_head));
344                         TAILQ_INIT(current_bindings);
345                 }
346
347                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
348         }
349         ;
350
351 floating_modifier:
352         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
353         {
354                 DLOG("floating modifier = %d\n", $<number>3);
355                 config.floating_modifier = $<number>3;
356         }
357         ;
358
359 new_container:
360         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
361         {
362                 DLOG("new containers will be in mode %d\n", $<number>3);
363                 config.container_mode = $<number>3;
364
365 #if 0
366                 /* We also need to change the layout of the already existing
367                  * workspaces here. Workspaces may exist at this point because
368                  * of the other directives which are modifying workspaces
369                  * (setting the preferred screen or name). While the workspace
370                  * objects are already created, they have never been used.
371                  * Thus, the user very likely awaits the default container mode
372                  * to trigger in this case, regardless of where it is inside
373                  * his configuration file. */
374                 Workspace *ws;
375                 TAILQ_FOREACH(ws, workspaces, workspaces) {
376                         if (ws->table == NULL)
377                                 continue;
378                         switch_layout_mode(global_conn,
379                                            ws->table[0][0],
380                                            config.container_mode);
381                 }
382 #endif
383         }
384         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
385         {
386                 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
387                 config.container_stack_limit = $<number>5;
388                 config.container_stack_limit_value = $<number>7;
389
390 #if 0
391                 /* See the comment above */
392                 Workspace *ws;
393                 TAILQ_FOREACH(ws, workspaces, workspaces) {
394                         if (ws->table == NULL)
395                                 continue;
396                         Container *con = ws->table[0][0];
397                         con->stack_limit = config.container_stack_limit;
398                         con->stack_limit_value = config.container_stack_limit_value;
399                 }
400 #endif
401         }
402         ;
403
404 new_window:
405         TOKNEWWINDOW WHITESPACE WORD
406         {
407                 DLOG("new windows should start in mode %s\n", $<string>3);
408                 config.default_border = sstrdup($<string>3);
409         }
410         ;
411
412 bool:
413         NUMBER
414         {
415                 $<number>$ = ($<number>1 == 1);
416         }
417         | WORD
418         {
419                 DLOG("checking word \"%s\"\n", $<string>1);
420                 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
421                               strcasecmp($<string>1, "true") == 0 ||
422                               strcasecmp($<string>1, "on") == 0 ||
423                               strcasecmp($<string>1, "enable") == 0 ||
424                               strcasecmp($<string>1, "active") == 0);
425         }
426         ;
427
428 focus_follows_mouse:
429         TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
430         {
431                 DLOG("focus follows mouse = %d\n", $<number>3);
432                 config.disable_focus_follows_mouse = !($<number>3);
433         }
434         ;
435
436 workspace_bar:
437         TOKWORKSPACEBAR WHITESPACE bool
438         {
439                 DLOG("workspace bar = %d\n", $<number>3);
440                 config.disable_workspace_bar = !($<number>3);
441         }
442         ;
443
444 workspace:
445         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
446         {
447                 int ws_num = $<number>3;
448                 if (ws_num < 1) {
449                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
450                 } else {
451 #if 0
452                         Workspace *ws = workspace_get(ws_num - 1);
453                         ws->preferred_output = $<string>7;
454                         if ($<string>8 != NULL) {
455                                 workspace_set_name(ws, $<string>8);
456                                 free($<string>8);
457                         }
458 #endif
459                 }
460         }
461         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
462         {
463                 int ws_num = $<number>3;
464                 if (ws_num < 1) {
465                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
466                 } else {
467                         DLOG("workspace name to: %s\n", $<string>5);
468 #if 0
469                         if ($<string>5 != NULL) {
470                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
471                                 free($<string>5);
472                         }
473 #endif
474                 }
475         }
476         ;
477
478 optional_workspace_name:
479         /* empty */                     { $<string>$ = NULL; }
480         | WHITESPACE workspace_name     { $<string>$ = $<string>2; }
481         ;
482
483 workspace_name:
484         QUOTEDSTRING         { $<string>$ = $<string>1; }
485         | STR                { $<string>$ = $<string>1; }
486         | WORD               { $<string>$ = $<string>1; }
487         ;
488
489 assign:
490         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
491         {
492 #if 0
493                 printf("assignment of %s\n", $<string>3);
494
495                 struct Assignment *new = $<assignment>6;
496                 printf("  to %d\n", new->workspace);
497                 printf("  floating = %d\n", new->floating);
498                 new->windowclass_title = $<string>3;
499                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
500 #endif
501         }
502         ;
503
504 assign_target:
505         NUMBER
506         {
507 #if 0
508                 struct Assignment *new = scalloc(sizeof(struct Assignment));
509                 new->workspace = $<number>1;
510                 new->floating = ASSIGN_FLOATING_NO;
511                 $<assignment>$ = new;
512 #endif
513         }
514         | '~'
515         {
516 #if 0
517                 struct Assignment *new = scalloc(sizeof(struct Assignment));
518                 new->floating = ASSIGN_FLOATING_ONLY;
519                 $<assignment>$ = new;
520 #endif
521         }
522         | '~' NUMBER
523         {
524 #if 0
525                 struct Assignment *new = scalloc(sizeof(struct Assignment));
526                 new->workspace = $<number>2;
527                 new->floating = ASSIGN_FLOATING;
528                 $<assignment>$ = new;
529 #endif
530         }
531         ;
532
533 window_class:
534         QUOTEDSTRING
535         | STR_NG
536         ;
537
538 optional_arrow:
539         /* NULL */
540         | TOKARROW WHITESPACE
541         ;
542
543 ipcsocket:
544         TOKIPCSOCKET WHITESPACE STR
545         {
546                 config.ipc_socket_path = $<string>3;
547         }
548         ;
549
550 exec:
551         TOKEXEC WHITESPACE STR
552         {
553                 struct Autostart *new = smalloc(sizeof(struct Autostart));
554                 new->command = $<string>3;
555                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
556         }
557         ;
558
559 terminal:
560         TOKTERMINAL WHITESPACE STR
561         {
562                 ELOG("The terminal option is DEPRECATED and has no effect. "
563                     "Please remove it from your configuration file.\n");
564         }
565         ;
566
567 font:
568         TOKFONT WHITESPACE STR
569         {
570                 config.font = $<string>3;
571                 printf("font %s\n", config.font);
572         }
573         ;
574
575 single_color:
576         TOKSINGLECOLOR WHITESPACE colorpixel
577         {
578                 uint32_t *dest = $<single_color>1;
579                 *dest = $<number>3;
580         }
581         ;
582
583 color:
584         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
585         {
586                 struct Colortriple *dest = $<color>1;
587
588                 dest->border = $<number>3;
589                 dest->background = $<number>5;
590                 dest->text = $<number>7;
591         }
592         ;
593
594 colorpixel:
595         '#' HEX
596         {
597                 char *hex;
598                 if (asprintf(&hex, "#%s", $<string>2) == -1)
599                         die("asprintf()");
600                 $<number>$ = get_colorpixel(hex);
601                 free(hex);
602         }
603         ;
604
605
606 binding_modifiers:
607         /* NULL */                               { $<number>$ = 0; }
608         | binding_modifier
609         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
610         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
611         ;
612
613 binding_modifier:
614         MODIFIER        { $<number>$ = $<number>1; }
615         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
616         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
617         ;