]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
19bfaec10f87ae42d36d924f9d7c00536539d315
[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         struct Colortriple *color;
186         struct Assignment *assignment;
187         struct Binding *binding;
188 }
189
190 %token <number>NUMBER "<number>"
191 %token <string>WORD "<word>"
192 %token <string>STR "<string>"
193 %token <string>STR_NG "<string (non-greedy)>"
194 %token <string>HEX "<hex>"
195 %token <string>OUTPUT "<RandR output>"
196 %token TOKBIND
197 %token TOKTERMINAL
198 %token TOKCOMMENT "<comment>"
199 %token TOKFONT "font"
200 %token TOKBINDSYM "bindsym"
201 %token MODIFIER "<modifier>"
202 %token TOKCONTROL "control"
203 %token TOKSHIFT "shift"
204 %token WHITESPACE "<whitespace>"
205 %token TOKFLOATING_MODIFIER "floating_modifier"
206 %token QUOTEDSTRING "<quoted string>"
207 %token TOKWORKSPACE "workspace"
208 %token TOKOUTPUT "output"
209 %token TOKASSIGN "assign"
210 %token TOKSET
211 %token TOKIPCSOCKET "ipc_socket"
212 %token TOKEXEC "exec"
213 %token TOKCOLOR
214 %token TOKARROW "→"
215 %token TOKMODE "mode"
216 %token TOKNEWCONTAINER "new_container"
217 %token TOKNEWWINDOW "new_window"
218 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
219 %token TOKWORKSPACEBAR "workspace_bar"
220 %token TOKCONTAINERMODE "default/stacking/tabbed"
221 %token TOKSTACKLIMIT "stack-limit"
222
223 %%
224
225 lines: /* empty */
226         | lines WHITESPACE line
227         | lines error
228         | lines line
229         ;
230
231 line:
232         bindline
233         | mode
234         | floating_modifier
235         | new_container
236         | new_window
237         | focus_follows_mouse
238         | workspace_bar
239         | workspace
240         | assign
241         | ipcsocket
242         | exec
243         | color
244         | terminal
245         | font
246         | comment
247         ;
248
249 comment:
250         TOKCOMMENT
251         ;
252
253 command:
254         STR
255         ;
256
257 bindline:
258         binding
259         {
260                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
261         }
262         ;
263
264 binding:
265         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
266         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
267         ;
268
269 bind:
270         binding_modifiers NUMBER WHITESPACE command
271         {
272                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
273                 Binding *new = scalloc(sizeof(Binding));
274
275                 new->keycode = $<number>2;
276                 new->mods = $<number>1;
277                 new->command = $<string>4;
278
279                 $<binding>$ = new;
280         }
281         ;
282
283 bindsym:
284         binding_modifiers word_or_number WHITESPACE command
285         {
286                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
287                 Binding *new = scalloc(sizeof(Binding));
288
289                 new->symbol = $<string>2;
290                 new->mods = $<number>1;
291                 new->command = $<string>4;
292
293                 $<binding>$ = new;
294         }
295         ;
296
297 word_or_number:
298         WORD
299         | NUMBER
300         {
301                 asprintf(&$<string>$, "%d", $1);
302         }
303         ;
304
305 mode:
306         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
307         {
308                 if (strcasecmp($<string>3, "default") == 0) {
309                         printf("You cannot use the name \"default\" for your mode\n");
310                         exit(1);
311                 }
312                 printf("\t now in mode %s\n", $<string>3);
313                 printf("\t current bindings = %p\n", current_bindings);
314                 Binding *binding;
315                 TAILQ_FOREACH(binding, current_bindings, bindings) {
316                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
317                                         binding->mods, binding->keycode, binding->symbol, binding->command);
318                 }
319
320                 struct Mode *mode = scalloc(sizeof(struct Mode));
321                 mode->name = $<string>3;
322                 mode->bindings = current_bindings;
323                 current_bindings = NULL;
324                 SLIST_INSERT_HEAD(&modes, mode, modes);
325         }
326         ;
327
328
329 modelines:
330         /* empty */
331         | modelines modeline
332         ;
333
334 modeline:
335         WHITESPACE
336         | comment
337         | binding
338         {
339                 if (current_bindings == NULL) {
340                         current_bindings = scalloc(sizeof(struct bindings_head));
341                         TAILQ_INIT(current_bindings);
342                 }
343
344                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
345         }
346         ;
347
348 floating_modifier:
349         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
350         {
351                 DLOG("floating modifier = %d\n", $<number>3);
352                 config.floating_modifier = $<number>3;
353         }
354         ;
355
356 new_container:
357         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
358         {
359                 DLOG("new containers will be in mode %d\n", $<number>3);
360                 config.container_mode = $<number>3;
361
362 #if 0
363                 /* We also need to change the layout of the already existing
364                  * workspaces here. Workspaces may exist at this point because
365                  * of the other directives which are modifying workspaces
366                  * (setting the preferred screen or name). While the workspace
367                  * objects are already created, they have never been used.
368                  * Thus, the user very likely awaits the default container mode
369                  * to trigger in this case, regardless of where it is inside
370                  * his configuration file. */
371                 Workspace *ws;
372                 TAILQ_FOREACH(ws, workspaces, workspaces) {
373                         if (ws->table == NULL)
374                                 continue;
375                         switch_layout_mode(global_conn,
376                                            ws->table[0][0],
377                                            config.container_mode);
378                 }
379 #endif
380         }
381         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
382         {
383                 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
384                 config.container_stack_limit = $<number>5;
385                 config.container_stack_limit_value = $<number>7;
386
387 #if 0
388                 /* See the comment above */
389                 Workspace *ws;
390                 TAILQ_FOREACH(ws, workspaces, workspaces) {
391                         if (ws->table == NULL)
392                                 continue;
393                         Container *con = ws->table[0][0];
394                         con->stack_limit = config.container_stack_limit;
395                         con->stack_limit_value = config.container_stack_limit_value;
396                 }
397 #endif
398         }
399         ;
400
401 new_window:
402         TOKNEWWINDOW WHITESPACE WORD
403         {
404                 DLOG("new windows should start in mode %s\n", $<string>3);
405                 config.default_border = sstrdup($<string>3);
406         }
407         ;
408
409 bool:
410         NUMBER
411         {
412                 $<number>$ = ($<number>1 == 1);
413         }
414         | WORD
415         {
416                 DLOG("checking word \"%s\"\n", $<string>1);
417                 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
418                               strcasecmp($<string>1, "true") == 0 ||
419                               strcasecmp($<string>1, "on") == 0 ||
420                               strcasecmp($<string>1, "enable") == 0 ||
421                               strcasecmp($<string>1, "active") == 0);
422         }
423         ;
424
425 focus_follows_mouse:
426         TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
427         {
428                 DLOG("focus follows mouse = %d\n", $<number>3);
429                 config.disable_focus_follows_mouse = !($<number>3);
430         }
431         ;
432
433 workspace_bar:
434         TOKWORKSPACEBAR WHITESPACE bool
435         {
436                 DLOG("workspace bar = %d\n", $<number>3);
437                 config.disable_workspace_bar = !($<number>3);
438         }
439         ;
440
441 workspace:
442         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
443         {
444                 int ws_num = $<number>3;
445                 if (ws_num < 1) {
446                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
447                 } else {
448 #if 0
449                         Workspace *ws = workspace_get(ws_num - 1);
450                         ws->preferred_output = $<string>7;
451                         if ($<string>8 != NULL) {
452                                 workspace_set_name(ws, $<string>8);
453                                 free($<string>8);
454                         }
455 #endif
456                 }
457         }
458         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
459         {
460                 int ws_num = $<number>3;
461                 if (ws_num < 1) {
462                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
463                 } else {
464                         DLOG("workspace name to: %s\n", $<string>5);
465 #if 0
466                         if ($<string>5 != NULL) {
467                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
468                                 free($<string>5);
469                         }
470 #endif
471                 }
472         }
473         ;
474
475 optional_workspace_name:
476         /* empty */                     { $<string>$ = NULL; }
477         | WHITESPACE workspace_name     { $<string>$ = $<string>2; }
478         ;
479
480 workspace_name:
481         QUOTEDSTRING         { $<string>$ = $<string>1; }
482         | STR                { $<string>$ = $<string>1; }
483         | WORD               { $<string>$ = $<string>1; }
484         ;
485
486 assign:
487         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
488         {
489                 printf("assignment of %s\n", $<string>3);
490
491                 struct Assignment *new = $<assignment>6;
492                 printf("  to %d\n", new->workspace);
493                 printf("  floating = %d\n", new->floating);
494                 new->windowclass_title = $<string>3;
495                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
496         }
497         ;
498
499 assign_target:
500         NUMBER
501         {
502                 struct Assignment *new = scalloc(sizeof(struct Assignment));
503                 new->workspace = $<number>1;
504                 new->floating = ASSIGN_FLOATING_NO;
505                 $<assignment>$ = new;
506         }
507         | '~'
508         {
509                 struct Assignment *new = scalloc(sizeof(struct Assignment));
510                 new->floating = ASSIGN_FLOATING_ONLY;
511                 $<assignment>$ = new;
512         }
513         | '~' NUMBER
514         {
515                 struct Assignment *new = scalloc(sizeof(struct Assignment));
516                 new->workspace = $<number>2;
517                 new->floating = ASSIGN_FLOATING;
518                 $<assignment>$ = new;
519         }
520         ;
521
522 window_class:
523         QUOTEDSTRING
524         | STR_NG
525         ;
526
527 optional_arrow:
528         /* NULL */
529         | TOKARROW WHITESPACE
530         ;
531
532 ipcsocket:
533         TOKIPCSOCKET WHITESPACE STR
534         {
535                 config.ipc_socket_path = $<string>3;
536         }
537         ;
538
539 exec:
540         TOKEXEC WHITESPACE STR
541         {
542                 struct Autostart *new = smalloc(sizeof(struct Autostart));
543                 new->command = $<string>3;
544                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
545         }
546         ;
547
548 terminal:
549         TOKTERMINAL WHITESPACE STR
550         {
551                 ELOG("The terminal option is DEPRECATED and has no effect. "
552                     "Please remove it from your configuration file.\n");
553         }
554         ;
555
556 font:
557         TOKFONT WHITESPACE STR
558         {
559                 config.font = $<string>3;
560                 printf("font %s\n", config.font);
561         }
562         ;
563
564
565 color:
566         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
567         {
568                 struct Colortriple *dest = $<color>1;
569
570                 dest->border = $<number>3;
571                 dest->background = $<number>5;
572                 dest->text = $<number>7;
573         }
574         ;
575
576 colorpixel:
577         '#' HEX
578         {
579                 char *hex;
580                 if (asprintf(&hex, "#%s", $<string>2) == -1)
581                         die("asprintf()");
582                 $<number>$ = get_colorpixel(hex);
583                 free(hex);
584         }
585         ;
586
587
588 binding_modifiers:
589         /* NULL */                               { $<number>$ = 0; }
590         | binding_modifier
591         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
592         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
593         ;
594
595 binding_modifier:
596         MODIFIER        { $<number>$ = $<number>1; }
597         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
598         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
599         ;