]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
refactor some places to use output_get_content()
[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         /* We need to copy the buffer because we need to invalidate the
110          * variables (otherwise we will count them twice, which is bad when
111          * 'extra' is negative) */
112         char *bufcopy = sstrdup(buf);
113         SLIST_FOREACH(current, &variables, variables) {
114                 int extra = (strlen(current->value) - strlen(current->key));
115                 char *next;
116                 for (next = bufcopy;
117                      (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
118                      next += strlen(current->key)) {
119                         *next = '_';
120                         extra_bytes += extra;
121                 }
122         }
123         FREE(bufcopy);
124
125         /* Then, allocate a new buffer and copy the file over to the new one,
126          * but replace occurences of our variables */
127         char *walk = buf, *destwalk;
128         char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
129         destwalk = new;
130         while (walk < (buf + stbuf.st_size)) {
131                 /* Find the next variable */
132                 SLIST_FOREACH(current, &variables, variables)
133                         current->next_match = strcasestr(walk, current->key);
134                 nearest = NULL;
135                 int distance = stbuf.st_size;
136                 SLIST_FOREACH(current, &variables, variables) {
137                         if (current->next_match == NULL)
138                                 continue;
139                         if ((current->next_match - walk) < distance) {
140                                 distance = (current->next_match - walk);
141                                 nearest = current;
142                         }
143                 }
144                 if (nearest == NULL) {
145                         /* If there are no more variables, we just copy the rest */
146                         strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
147                         destwalk += (buf + stbuf.st_size) - walk;
148                         *destwalk = '\0';
149                         break;
150                 } else {
151                         /* Copy until the next variable, then copy its value */
152                         strncpy(destwalk, walk, distance);
153                         strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
154                         walk += distance + strlen(nearest->key);
155                         destwalk += distance + strlen(nearest->value);
156                 }
157         }
158
159         yy_scan_string(new);
160
161         context = scalloc(sizeof(struct context));
162         context->filename = f;
163
164         if (yyparse() != 0) {
165                 fprintf(stderr, "Could not parse configfile\n");
166                 exit(1);
167         }
168
169         FREE(context->line_copy);
170         free(context);
171         free(new);
172         free(buf);
173
174         while (!SLIST_EMPTY(&variables)) {
175                 current = SLIST_FIRST(&variables);
176                 FREE(current->key);
177                 FREE(current->value);
178                 SLIST_REMOVE_HEAD(&variables, variables);
179                 FREE(current);
180         }
181 }
182
183 %}
184
185 %expect 1
186 %error-verbose
187 %lex-param { struct context *context }
188
189 %union {
190         int number;
191         char *string;
192         uint32_t *single_color;
193         struct Colortriple *color;
194         struct Assignment *assignment;
195         struct Binding *binding;
196 }
197
198 %token <number>NUMBER "<number>"
199 %token <string>WORD "<word>"
200 %token <string>STR "<string>"
201 %token <string>STR_NG "<string (non-greedy)>"
202 %token <string>HEX "<hex>"
203 %token <string>OUTPUT "<RandR output>"
204 %token TOKBIND
205 %token TOKTERMINAL
206 %token TOKCOMMENT "<comment>"
207 %token TOKFONT "font"
208 %token TOKBINDSYM "bindsym"
209 %token MODIFIER "<modifier>"
210 %token TOKCONTROL "control"
211 %token TOKSHIFT "shift"
212 %token WHITESPACE "<whitespace>"
213 %token TOKFLOATING_MODIFIER "floating_modifier"
214 %token QUOTEDSTRING "<quoted string>"
215 %token TOKWORKSPACE "workspace"
216 %token TOKOUTPUT "output"
217 %token TOKASSIGN "assign"
218 %token TOKSET
219 %token TOKIPCSOCKET "ipc_socket"
220 %token TOKRESTARTSTATE "restart_state"
221 %token TOKEXEC "exec"
222 %token TOKSINGLECOLOR
223 %token TOKCOLOR
224 %token TOKARROW "→"
225 %token TOKMODE "mode"
226 %token TOKNEWCONTAINER "new_container"
227 %token TOKNEWWINDOW "new_window"
228 %token TOK_NORMAL "normal"
229 %token TOK_NONE "none"
230 %token TOK_1PIXEL "1pixel"
231 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
232 %token TOKWORKSPACEBAR "workspace_bar"
233 %token TOKCONTAINERMODE "default/stacking/tabbed"
234 %token TOKSTACKLIMIT "stack-limit"
235
236 %%
237
238 lines: /* empty */
239         | lines WHITESPACE line
240         | lines error
241         | lines line
242         ;
243
244 line:
245         bindline
246         | mode
247         | floating_modifier
248         | new_container
249         | new_window
250         | focus_follows_mouse
251         | workspace_bar
252         | workspace
253         | assign
254         | ipcsocket
255         | restart_state
256         | exec
257         | single_color
258         | color
259         | terminal
260         | font
261         | comment
262         ;
263
264 comment:
265         TOKCOMMENT
266         ;
267
268 command:
269         STR
270         ;
271
272 bindline:
273         binding
274         {
275                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
276         }
277         ;
278
279 binding:
280         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
281         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
282         ;
283
284 bind:
285         binding_modifiers NUMBER WHITESPACE command
286         {
287                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
288                 Binding *new = scalloc(sizeof(Binding));
289
290                 new->keycode = $<number>2;
291                 new->mods = $<number>1;
292                 new->command = $<string>4;
293
294                 $<binding>$ = new;
295         }
296         ;
297
298 bindsym:
299         binding_modifiers word_or_number WHITESPACE command
300         {
301                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
302                 Binding *new = scalloc(sizeof(Binding));
303
304                 new->symbol = $<string>2;
305                 new->mods = $<number>1;
306                 new->command = $<string>4;
307
308                 $<binding>$ = new;
309         }
310         ;
311
312 word_or_number:
313         WORD
314         | NUMBER
315         {
316                 asprintf(&$<string>$, "%d", $1);
317         }
318         ;
319
320 mode:
321         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
322         {
323                 if (strcasecmp($<string>3, "default") == 0) {
324                         printf("You cannot use the name \"default\" for your mode\n");
325                         exit(1);
326                 }
327                 printf("\t now in mode %s\n", $<string>3);
328                 printf("\t current bindings = %p\n", current_bindings);
329                 Binding *binding;
330                 TAILQ_FOREACH(binding, current_bindings, bindings) {
331                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
332                                         binding->mods, binding->keycode, binding->symbol, binding->command);
333                 }
334
335                 struct Mode *mode = scalloc(sizeof(struct Mode));
336                 mode->name = $<string>3;
337                 mode->bindings = current_bindings;
338                 current_bindings = NULL;
339                 SLIST_INSERT_HEAD(&modes, mode, modes);
340         }
341         ;
342
343
344 modelines:
345         /* empty */
346         | modelines modeline
347         ;
348
349 modeline:
350         WHITESPACE
351         | comment
352         | binding
353         {
354                 if (current_bindings == NULL) {
355                         current_bindings = scalloc(sizeof(struct bindings_head));
356                         TAILQ_INIT(current_bindings);
357                 }
358
359                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
360         }
361         ;
362
363 floating_modifier:
364         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
365         {
366                 DLOG("floating modifier = %d\n", $<number>3);
367                 config.floating_modifier = $<number>3;
368         }
369         ;
370
371 new_container:
372         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
373         {
374                 DLOG("new containers will be in mode %d\n", $<number>3);
375                 config.container_mode = $<number>3;
376
377 #if 0
378                 /* We also need to change the layout of the already existing
379                  * workspaces here. Workspaces may exist at this point because
380                  * of the other directives which are modifying workspaces
381                  * (setting the preferred screen or name). While the workspace
382                  * objects are already created, they have never been used.
383                  * Thus, the user very likely awaits the default container mode
384                  * to trigger in this case, regardless of where it is inside
385                  * his configuration file. */
386                 Workspace *ws;
387                 TAILQ_FOREACH(ws, workspaces, workspaces) {
388                         if (ws->table == NULL)
389                                 continue;
390                         switch_layout_mode(global_conn,
391                                            ws->table[0][0],
392                                            config.container_mode);
393                 }
394 #endif
395         }
396         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
397         {
398                 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
399                 config.container_stack_limit = $<number>5;
400                 config.container_stack_limit_value = $<number>7;
401
402 #if 0
403                 /* See the comment above */
404                 Workspace *ws;
405                 TAILQ_FOREACH(ws, workspaces, workspaces) {
406                         if (ws->table == NULL)
407                                 continue;
408                         Container *con = ws->table[0][0];
409                         con->stack_limit = config.container_stack_limit;
410                         con->stack_limit_value = config.container_stack_limit_value;
411                 }
412 #endif
413         }
414         ;
415
416 new_window:
417         TOKNEWWINDOW WHITESPACE border_style
418         {
419                 DLOG("new windows should start with border style %d\n", $<number>3);
420                 config.default_border = $<number>3;
421         }
422         ;
423
424 border_style:
425         TOK_NORMAL      { $<number>$ = BS_NORMAL; }
426         | TOK_NONE      { $<number>$ = BS_NONE; }
427         | TOK_1PIXEL    { $<number>$ = BS_1PIXEL; }
428         ;
429
430 bool:
431         NUMBER
432         {
433                 $<number>$ = ($<number>1 == 1);
434         }
435         | WORD
436         {
437                 DLOG("checking word \"%s\"\n", $<string>1);
438                 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
439                               strcasecmp($<string>1, "true") == 0 ||
440                               strcasecmp($<string>1, "on") == 0 ||
441                               strcasecmp($<string>1, "enable") == 0 ||
442                               strcasecmp($<string>1, "active") == 0);
443         }
444         ;
445
446 focus_follows_mouse:
447         TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
448         {
449                 DLOG("focus follows mouse = %d\n", $<number>3);
450                 config.disable_focus_follows_mouse = !($<number>3);
451         }
452         ;
453
454 workspace_bar:
455         TOKWORKSPACEBAR WHITESPACE bool
456         {
457                 DLOG("workspace bar = %d\n", $<number>3);
458                 config.disable_workspace_bar = !($<number>3);
459         }
460         ;
461
462 workspace:
463         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
464         {
465                 int ws_num = $<number>3;
466                 if (ws_num < 1) {
467                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
468                 } else {
469 #if 0
470                         Workspace *ws = workspace_get(ws_num - 1);
471                         ws->preferred_output = $<string>7;
472                         if ($<string>8 != NULL) {
473                                 workspace_set_name(ws, $<string>8);
474                                 free($<string>8);
475                         }
476 #endif
477                 }
478         }
479         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
480         {
481                 int ws_num = $<number>3;
482                 if (ws_num < 1) {
483                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
484                 } else {
485                         DLOG("workspace name to: %s\n", $<string>5);
486 #if 0
487                         if ($<string>5 != NULL) {
488                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
489                                 free($<string>5);
490                         }
491 #endif
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 #if 0
511                 printf("assignment of %s\n", $<string>3);
512
513                 struct Assignment *new = $<assignment>6;
514                 printf("  to %d\n", new->workspace);
515                 printf("  floating = %d\n", new->floating);
516                 new->windowclass_title = $<string>3;
517                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
518 #endif
519         }
520         ;
521
522 assign_target:
523         NUMBER
524         {
525 #if 0
526                 struct Assignment *new = scalloc(sizeof(struct Assignment));
527                 new->workspace = $<number>1;
528                 new->floating = ASSIGN_FLOATING_NO;
529                 $<assignment>$ = new;
530 #endif
531         }
532         | '~'
533         {
534 #if 0
535                 struct Assignment *new = scalloc(sizeof(struct Assignment));
536                 new->floating = ASSIGN_FLOATING_ONLY;
537                 $<assignment>$ = new;
538 #endif
539         }
540         | '~' NUMBER
541         {
542 #if 0
543                 struct Assignment *new = scalloc(sizeof(struct Assignment));
544                 new->workspace = $<number>2;
545                 new->floating = ASSIGN_FLOATING;
546                 $<assignment>$ = new;
547 #endif
548         }
549         ;
550
551 window_class:
552         QUOTEDSTRING
553         | STR_NG
554         ;
555
556 optional_arrow:
557         /* NULL */
558         | TOKARROW WHITESPACE
559         ;
560
561 ipcsocket:
562         TOKIPCSOCKET WHITESPACE STR
563         {
564                 config.ipc_socket_path = $<string>3;
565         }
566         ;
567
568 restart_state:
569         TOKRESTARTSTATE WHITESPACE STR
570         {
571                 config.restart_state_path = $<string>3;
572         }
573         ;
574
575 exec:
576         TOKEXEC WHITESPACE STR
577         {
578                 struct Autostart *new = smalloc(sizeof(struct Autostart));
579                 new->command = $<string>3;
580                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
581         }
582         ;
583
584 terminal:
585         TOKTERMINAL WHITESPACE STR
586         {
587                 ELOG("The terminal option is DEPRECATED and has no effect. "
588                     "Please remove it from your configuration file.\n");
589         }
590         ;
591
592 font:
593         TOKFONT WHITESPACE STR
594         {
595                 config.font = $<string>3;
596                 printf("font %s\n", config.font);
597         }
598         ;
599
600 single_color:
601         TOKSINGLECOLOR WHITESPACE colorpixel
602         {
603                 uint32_t *dest = $<single_color>1;
604                 *dest = $<number>3;
605         }
606         ;
607
608 color:
609         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
610         {
611                 struct Colortriple *dest = $<color>1;
612
613                 dest->border = $<number>3;
614                 dest->background = $<number>5;
615                 dest->text = $<number>7;
616         }
617         ;
618
619 colorpixel:
620         '#' HEX
621         {
622                 char *hex;
623                 if (asprintf(&hex, "#%s", $<string>2) == -1)
624                         die("asprintf()");
625                 $<number>$ = get_colorpixel(hex);
626                 free(hex);
627         }
628         ;
629
630
631 binding_modifiers:
632         /* NULL */                               { $<number>$ = 0; }
633         | binding_modifier
634         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
635         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
636         ;
637
638 binding_modifier:
639         MODIFIER        { $<number>$ = $<number>1; }
640         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
641         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
642         ;