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