]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
0738d0c90eca134a4925e20c6222833148b60ffa
[i3/i3] / src / cmdparse.y
1 %{
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  * i3 - an improved dynamic tiling window manager
6  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7  *
8  * cmdparse.y: the parser for commands you send to i3 (or bind on keys)
9  *
10
11  */
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <limits.h>
17
18 #include "all.h"
19
20 /** When the command did not include match criteria (!), we use the currently
21  * focused command. Do not confuse this case with a command which included
22  * criteria but which did not match any windows. This macro has to be called in
23  * every command.
24  */
25 #define HANDLE_EMPTY_MATCH do { \
26     if (match_is_empty(&current_match)) { \
27         owindow *ow = smalloc(sizeof(owindow)); \
28         ow->con = focused; \
29         TAILQ_INIT(&owindows); \
30         TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
31     } \
32 } while (0)
33
34 typedef struct yy_buffer_state *YY_BUFFER_STATE;
35 extern int cmdyylex(struct context *context);
36 extern int cmdyyparse(void);
37 extern FILE *cmdyyin;
38 YY_BUFFER_STATE cmdyy_scan_string(const char *);
39
40 static struct context *context;
41 static Match current_match;
42
43 /*
44  * Helper data structure for an operation window (window on which the operation
45  * will be performed). Used to build the TAILQ owindows.
46  *
47  */
48 typedef struct owindow {
49     Con *con;
50     TAILQ_ENTRY(owindow) owindows;
51 } owindow;
52 static TAILQ_HEAD(owindows_head, owindow) owindows;
53
54 /* Holds the JSON which will be returned via IPC or NULL for the default return
55  * message */
56 static char *json_output;
57
58 /* We don’t need yydebug for now, as we got decent error messages using
59  * yyerror(). Should you ever want to extend the parser, it might be handy
60  * to just comment it in again, so it stays here. */
61 //int cmdyydebug = 1;
62
63 void cmdyyerror(const char *error_message) {
64     ELOG("\n");
65     ELOG("CMD: %s\n", error_message);
66     ELOG("CMD: in command:\n");
67     ELOG("CMD:   %s\n", context->line_copy);
68     ELOG("CMD:   ");
69     for (int c = 1; c <= context->last_column; c++)
70         if (c >= context->first_column)
71                 printf("^");
72         else printf(" ");
73     printf("\n");
74     ELOG("\n");
75     context->compact_error = sstrdup(error_message);
76 }
77
78 int cmdyywrap() {
79     return 1;
80 }
81
82 char *parse_cmd(const char *new) {
83     LOG("COMMAND: *%s*\n", new);
84     cmdyy_scan_string(new);
85
86     match_init(&current_match);
87     context = scalloc(sizeof(struct context));
88     context->filename = "cmd";
89     FREE(json_output);
90     if (cmdyyparse() != 0) {
91         fprintf(stderr, "Could not parse command\n");
92         asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
93                  context->compact_error, context->first_column);
94         FREE(context->line_copy);
95         FREE(context->compact_error);
96         free(context);
97         return json_output;
98     }
99     printf("done, json output = %s\n", json_output);
100
101     FREE(context->line_copy);
102     FREE(context->compact_error);
103     free(context);
104     return json_output;
105 }
106
107 %}
108
109 %error-verbose
110 %lex-param { struct context *context }
111
112 %union {
113     char *string;
114     char chr;
115     int number;
116 }
117
118 %token              TOK_EXEC            "exec"
119 %token              TOK_EXIT            "exit"
120 %token              TOK_RELOAD          "reload"
121 %token              TOK_RESTART         "restart"
122 %token              TOK_KILL            "kill"
123 %token              TOK_WINDOW          "window"
124 %token              TOK_CLIENT          "client"
125 %token              TOK_FULLSCREEN      "fullscreen"
126 %token              TOK_GLOBAL          "global"
127 %token              TOK_LAYOUT          "layout"
128 %token              TOK_DEFAULT         "default"
129 %token              TOK_STACKED         "stacked"
130 %token              TOK_TABBED          "tabbed"
131 %token              TOK_BORDER          "border"
132 %token              TOK_NORMAL          "normal"
133 %token              TOK_NONE            "none"
134 %token              TOK_1PIXEL          "1pixel"
135 %token              TOK_MODE            "mode"
136 %token              TOK_TILING          "tiling"
137 %token              TOK_FLOATING        "floating"
138 %token              TOK_ENABLE          "enable"
139 %token              TOK_DISABLE         "disable"
140 %token              TOK_WORKSPACE       "workspace"
141 %token              TOK_TOGGLE          "toggle"
142 %token              TOK_FOCUS           "focus"
143 %token              TOK_MOVE            "move"
144 %token              TOK_OPEN            "open"
145 %token              TOK_PREV            "prev"
146 %token              TOK_SPLIT           "split"
147 %token              TOK_HORIZONTAL      "horizontal"
148 %token              TOK_VERTICAL        "vertical"
149 %token              TOK_UP              "up"
150 %token              TOK_DOWN            "down"
151 %token              TOK_LEFT            "left"
152 %token              TOK_RIGHT           "right"
153 %token              TOK_PARENT          "parent"
154 %token              TOK_CHILD           "child"
155 %token              TOK_RESTORE         "restore"
156 %token              TOK_MARK            "mark"
157 %token              TOK_RESIZE          "resize"
158 %token              TOK_GROW            "grow"
159 %token              TOK_SHRINK          "shrink"
160 %token              TOK_PX              "px"
161 %token              TOK_OR              "or"
162 %token              TOK_PPT             "ppt"
163 %token              TOK_NOP             "nop"
164
165 %token              TOK_CLASS           "class"
166 %token              TOK_ID              "id"
167 %token              TOK_CON_ID          "con_id"
168 %token              TOK_TITLE           "title"
169
170 %token  <string>    STR                 "<string>"
171 %token  <number>    NUMBER              "<number>"
172
173 %type   <number>    direction
174 %type   <number>    level
175 %type   <number>    boolean
176 %type   <number>    border_style
177 %type   <number>    layout_mode
178 %type   <number>    resize_px
179 %type   <number>    resize_way
180 %type   <number>    resize_tiling
181 %type   <number>    optional_kill_mode
182
183 %%
184
185 commands:
186     commands ';' command
187     | command
188     {
189         owindow *current;
190
191         printf("single command completely parsed, dropping state...\n");
192         while (!TAILQ_EMPTY(&owindows)) {
193             current = TAILQ_FIRST(&owindows);
194             TAILQ_REMOVE(&owindows, current, owindows);
195             free(current);
196         }
197         match_init(&current_match);
198     }
199     ;
200
201 command:
202     match operations
203     ;
204
205 match:
206     | matchstart criteria matchend
207     {
208         printf("match parsed\n");
209     }
210     ;
211
212 matchstart:
213     '['
214     {
215         printf("start\n");
216         match_init(&current_match);
217         TAILQ_INIT(&owindows);
218         /* copy all_cons */
219         Con *con;
220         TAILQ_FOREACH(con, &all_cons, all_cons) {
221             owindow *ow = smalloc(sizeof(owindow));
222             ow->con = con;
223             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
224         }
225     }
226     ;
227
228 matchend:
229     ']'
230     {
231         owindow *next, *current;
232
233         printf("match specification finished, matching...\n");
234         /* copy the old list head to iterate through it and start with a fresh
235          * list which will contain only matching windows */
236         struct owindows_head old = owindows;
237         TAILQ_INIT(&owindows);
238         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
239             /* make a copy of the next pointer and advance the pointer to the
240              * next element as we are going to invalidate the element’s
241              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
242             current = next;
243             next = TAILQ_NEXT(next, owindows);
244
245             printf("checking if con %p / %s matches\n", current->con, current->con->name);
246             if (current_match.con_id != NULL) {
247                 if (current_match.con_id == current->con) {
248                     printf("matches container!\n");
249                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
250
251                 }
252             } else if (current_match.mark != NULL && current->con->mark != NULL &&
253                     strcasecmp(current_match.mark, current->con->mark) == 0) {
254                 printf("match by mark\n");
255                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
256
257             } else {
258                 if (current->con->window == NULL)
259                     continue;
260                 if (match_matches_window(&current_match, current->con->window)) {
261                     printf("matches window!\n");
262                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
263                 } else {
264                     printf("doesnt match\n");
265                     free(current);
266                 }
267             }
268         }
269
270         TAILQ_FOREACH(current, &owindows, owindows) {
271             printf("matching: %p / %s\n", current->con, current->con->name);
272         }
273
274     }
275     ;
276
277 criteria:
278     criteria criterion
279     | criterion
280     ;
281
282 criterion:
283     TOK_CLASS '=' STR
284     {
285         printf("criteria: class = %s\n", $3);
286         current_match.class = $3;
287     }
288     | TOK_CON_ID '=' STR
289     {
290         printf("criteria: id = %s\n", $3);
291         char *end;
292         long parsed = strtol($3, &end, 10);
293         if (parsed == LONG_MIN ||
294             parsed == LONG_MAX ||
295             parsed < 0 ||
296             (end && *end != '\0')) {
297             ELOG("Could not parse con id \"%s\"\n", $3);
298         } else {
299             current_match.con_id = (Con*)parsed;
300             printf("id as int = %p\n", current_match.con_id);
301         }
302     }
303     | TOK_ID '=' STR
304     {
305         printf("criteria: window id = %s\n", $3);
306         char *end;
307         long parsed = strtol($3, &end, 10);
308         if (parsed == LONG_MIN ||
309             parsed == LONG_MAX ||
310             parsed < 0 ||
311             (end && *end != '\0')) {
312             ELOG("Could not parse window id \"%s\"\n", $3);
313         } else {
314             current_match.id = parsed;
315             printf("window id as int = %d\n", current_match.id);
316         }
317     }
318     | TOK_MARK '=' STR
319     {
320         printf("criteria: mark = %s\n", $3);
321         current_match.mark = $3;
322     }
323     | TOK_TITLE '=' STR
324     {
325         printf("criteria: title = %s\n", $3);
326         current_match.title = $3;
327     }
328     ;
329
330 operations:
331     operation
332     | operations ',' operation
333     ;
334
335 operation:
336     exec
337     | exit
338     | restart
339     | reload
340     | border
341     | layout
342     | restore
343     | move
344     | workspace
345     | focus
346     | kill
347     | open
348     | fullscreen
349     | split
350     | floating
351     | mark
352     | resize
353     | nop
354     ;
355
356 exec:
357     TOK_EXEC STR
358     {
359         printf("should execute %s\n", $2);
360         start_application($2);
361         free($2);
362     }
363     ;
364
365 exit:
366     TOK_EXIT
367     {
368         printf("exit, bye bye\n");
369         exit(0);
370     }
371     ;
372
373 reload:
374     TOK_RELOAD
375     {
376         printf("reloading\n");
377         load_configuration(conn, NULL, true);
378         x_set_i3_atoms();
379         /* Send an IPC event just in case the ws names have changed */
380         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
381     }
382     ;
383
384 restart:
385     TOK_RESTART
386     {
387         printf("restarting i3\n");
388         i3_restart(false);
389     }
390     ;
391
392 focus:
393     TOK_FOCUS
394     {
395         owindow *current;
396
397         if (match_is_empty(&current_match)) {
398             ELOG("You have to specify which window/container should be focused.\n");
399             ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
400
401             asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
402                      "specify which window/container should be focused\"}");
403             break;
404         }
405
406         int count = 0;
407         TAILQ_FOREACH(current, &owindows, owindows) {
408             LOG("focusing %p / %s\n", current->con, current->con->name);
409             con_focus(current->con);
410             count++;
411         }
412
413         if (count > 1)
414             LOG("WARNING: Your criteria for the focus command matches %d containers, "
415                 "while only exactly one container can be focused at a time.\n", count);
416
417         tree_render();
418     }
419     | TOK_FOCUS direction
420     {
421         int direction = $2;
422         switch (direction) {
423             case TOK_LEFT:
424                 LOG("Focusing left\n");
425                 tree_next('p', HORIZ);
426                 break;
427             case TOK_RIGHT:
428                 LOG("Focusing right\n");
429                 tree_next('n', HORIZ);
430                 break;
431             case TOK_UP:
432                 LOG("Focusing up\n");
433                 tree_next('p', VERT);
434                 break;
435             case TOK_DOWN:
436                 LOG("Focusing down\n");
437                 tree_next('n', VERT);
438                 break;
439             default:
440                 ELOG("Invalid focus direction (%d)\n", direction);
441                 break;
442         }
443
444         tree_render();
445     }
446     | TOK_FOCUS level
447     {
448         if ($2 == TOK_PARENT)
449             level_up();
450         else level_down();
451
452         tree_render();
453     }
454     ;
455
456 level:
457     TOK_PARENT  { $$ = TOK_PARENT; }
458     | TOK_CHILD { $$ = TOK_CHILD;  }
459     ;
460
461 kill:
462     TOK_KILL optional_kill_mode
463     {
464         owindow *current;
465
466         printf("killing!\n");
467         /* check if the match is empty, not if the result is empty */
468         if (match_is_empty(&current_match))
469             tree_close_con($2);
470         else {
471             TAILQ_FOREACH(current, &owindows, owindows) {
472                 printf("matching: %p / %s\n", current->con, current->con->name);
473                 tree_close(current->con, $2, false);
474             }
475         }
476
477         tree_render();
478     }
479     ;
480
481 optional_kill_mode:
482     /* empty */             { $$ = KILL_WINDOW; }
483     | TOK_WINDOW { $$ = KILL_WINDOW; }
484     | TOK_CLIENT { $$ = KILL_CLIENT; }
485     ;
486
487 workspace:
488     TOK_WORKSPACE STR
489     {
490         printf("should switch to workspace %s\n", $2);
491         workspace_show($2);
492         free($2);
493
494         tree_render();
495     }
496     ;
497
498 open:
499     TOK_OPEN
500     {
501         printf("opening new container\n");
502         Con *con = tree_open_con(NULL, NULL);
503         con_focus(con);
504         asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
505
506         tree_render();
507     }
508     ;
509
510 fullscreen:
511     TOK_FULLSCREEN
512     {
513         printf("toggling fullscreen\n");
514         owindow *current;
515
516
517         HANDLE_EMPTY_MATCH;
518
519         TAILQ_FOREACH(current, &owindows, owindows) {
520             printf("matching: %p / %s\n", current->con, current->con->name);
521             con_toggle_fullscreen(current->con);
522         }
523
524         tree_render();
525     }
526     ;
527
528 split:
529     TOK_SPLIT direction
530     {
531         /* TODO: use matches */
532         printf("splitting in direction %c\n", $2);
533         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
534
535         tree_render();
536     }
537     ;
538
539 direction:
540     TOK_HORIZONTAL  { $$ = 'h'; }
541     | 'h'           { $$ = 'h'; }
542     | TOK_VERTICAL  { $$ = 'v'; }
543     | 'v'           { $$ = 'v'; }
544     ;
545
546 floating:
547     TOK_FLOATING boolean
548     {
549         HANDLE_EMPTY_MATCH;
550
551         owindow *current;
552         TAILQ_FOREACH(current, &owindows, owindows) {
553             printf("matching: %p / %s\n", current->con, current->con->name);
554             if ($2 == TOK_TOGGLE) {
555                 printf("should toggle mode\n");
556                 toggle_floating_mode(current->con, false);
557             } else {
558                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
559                 if ($2 == TOK_ENABLE) {
560                     floating_enable(current->con, false);
561                 } else {
562                     floating_disable(current->con, false);
563                 }
564             }
565         }
566
567         tree_render();
568     }
569     ;
570
571 boolean:
572     TOK_ENABLE    { $$ = TOK_ENABLE; }
573     | TOK_DISABLE { $$ = TOK_DISABLE; }
574     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
575     ;
576
577 border:
578     TOK_BORDER border_style
579     {
580         printf("border style should be changed to %d\n", $2);
581         owindow *current;
582
583         HANDLE_EMPTY_MATCH;
584
585         TAILQ_FOREACH(current, &owindows, owindows) {
586             printf("matching: %p / %s\n", current->con, current->con->name);
587             current->con->border_style = $2;
588         }
589
590         tree_render();
591     }
592     ;
593
594 border_style:
595     TOK_NORMAL      { $$ = BS_NORMAL; }
596     | TOK_NONE      { $$ = BS_NONE; }
597     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
598     ;
599
600 move:
601     TOK_MOVE direction
602     {
603         printf("moving in direction %d\n", $2);
604         tree_move($2);
605
606         tree_render();
607     }
608     | TOK_MOVE TOK_WORKSPACE STR
609     {
610         owindow *current;
611
612         printf("should move window to workspace %s\n", $3);
613         /* get the workspace */
614         Con *ws = workspace_get($3, NULL);
615         free($3);
616
617         HANDLE_EMPTY_MATCH;
618
619         TAILQ_FOREACH(current, &owindows, owindows) {
620             printf("matching: %p / %s\n", current->con, current->con->name);
621             con_move_to_workspace(current->con, ws);
622         }
623
624         tree_render();
625     }
626     ;
627
628 restore:
629     TOK_RESTORE STR
630     {
631         printf("restoring \"%s\"\n", $2);
632         tree_append_json($2);
633         free($2);
634         tree_render();
635     }
636     ;
637
638 layout:
639     TOK_LAYOUT layout_mode
640     {
641         printf("changing layout to %d\n", $2);
642         owindow *current;
643
644         /* check if the match is empty, not if the result is empty */
645         if (match_is_empty(&current_match))
646             con_set_layout(focused->parent, $2);
647         else {
648             TAILQ_FOREACH(current, &owindows, owindows) {
649                 printf("matching: %p / %s\n", current->con, current->con->name);
650                 con_set_layout(current->con, $2);
651             }
652         }
653
654         tree_render();
655     }
656     ;
657
658 layout_mode:
659     TOK_DEFAULT   { $$ = L_DEFAULT; }
660     | TOK_STACKED { $$ = L_STACKED; }
661     | TOK_TABBED  { $$ = L_TABBED; }
662     ;
663
664 mark:
665     TOK_MARK STR
666     {
667         printf("marking window with str %s\n", $2);
668         owindow *current;
669
670         HANDLE_EMPTY_MATCH;
671
672         TAILQ_FOREACH(current, &owindows, owindows) {
673             printf("matching: %p / %s\n", current->con, current->con->name);
674             current->con->mark = sstrdup($2);
675         }
676
677         free($<string>2);
678
679         tree_render();
680     }
681     ;
682
683 nop:
684     TOK_NOP STR
685     {
686         printf("-------------------------------------------------\n");
687         printf("  NOP: %s\n", $2);
688         printf("-------------------------------------------------\n");
689         free($2);
690     }
691     ;
692
693 resize:
694     TOK_RESIZE resize_way direction resize_px resize_tiling
695     {
696         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
697         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
698         int direction = $3;
699         int px = $4;
700         int ppt = $5;
701         if ($2 == TOK_SHRINK) {
702             px *= -1;
703             ppt *= -1;
704         }
705
706         if (con_is_floating(focused)) {
707             printf("floating resize\n");
708             if (direction == TOK_UP) {
709                 focused->parent->rect.y -= px;
710                 focused->parent->rect.height += px;
711             } else if (direction == TOK_DOWN) {
712                 focused->rect.height += px;
713             } else if (direction == TOK_LEFT) {
714                 focused->rect.x -= px;
715                 focused->rect.width += px;
716             } else {
717                 focused->rect.width += px;
718             }
719         } else {
720             LOG("tiling resize\n");
721             /* get the default percentage */
722             int children = con_num_children(focused->parent);
723             Con *other;
724             LOG("ins. %d children\n", children);
725             double percentage = 1.0 / children;
726             LOG("default percentage = %f\n", percentage);
727
728             if (direction == TOK_UP || direction == TOK_LEFT) {
729                 other = TAILQ_PREV(focused, nodes_head, nodes);
730             } else {
731                 other = TAILQ_NEXT(focused, nodes);
732             }
733             if (other == TAILQ_END(workspaces)) {
734                 LOG("No other container in this direction found, cannot resize.\n");
735                 return 0;
736             }
737             LOG("other->percent = %f\n", other->percent);
738             LOG("focused->percent before = %f\n", focused->percent);
739             if (focused->percent == 0.0)
740                 focused->percent = percentage;
741             if (other->percent == 0.0)
742                 other->percent = percentage;
743             focused->percent += ((double)ppt / 100.0);
744             other->percent -= ((double)ppt / 100.0);
745             LOG("focused->percent after = %f\n", focused->percent);
746             LOG("other->percent after = %f\n", other->percent);
747         }
748
749         tree_render();
750     }
751     ;
752
753 resize_px:
754     /* empty */
755     {
756         $$ = 10;
757     }
758     | NUMBER TOK_PX
759     {
760         $$ = $1;
761     }
762     ;
763
764 resize_tiling:
765     /* empty */
766     {
767         $$ = 10;
768     }
769     | TOK_OR NUMBER TOK_PPT
770     {
771         $$ = $2;
772     }
773     ;
774
775 resize_way:
776     TOK_GROW        { $$ = TOK_GROW; }
777     | TOK_SHRINK    { $$ = TOK_SHRINK; }
778     ;
779
780 direction:
781     TOK_UP          { $$ = TOK_UP; }
782     | TOK_DOWN      { $$ = TOK_DOWN; }
783     | TOK_LEFT      { $$ = TOK_LEFT; }
784     | TOK_RIGHT     { $$ = TOK_RIGHT; }
785     ;