]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
1c65fbc544bfdb2ed6991a03807770b166d2f1ff
[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_NEXT            "next"
146 %token              TOK_PREV            "prev"
147 %token              TOK_SPLIT           "split"
148 %token              TOK_HORIZONTAL      "horizontal"
149 %token              TOK_VERTICAL        "vertical"
150 %token              TOK_UP              "up"
151 %token              TOK_DOWN            "down"
152 %token              TOK_LEFT            "left"
153 %token              TOK_RIGHT           "right"
154 %token              TOK_PARENT          "parent"
155 %token              TOK_CHILD           "child"
156 %token              TOK_APPEND_LAYOUT   "append_layout"
157 %token              TOK_MARK            "mark"
158 %token              TOK_RESIZE          "resize"
159 %token              TOK_GROW            "grow"
160 %token              TOK_SHRINK          "shrink"
161 %token              TOK_PX              "px"
162 %token              TOK_OR              "or"
163 %token              TOK_PPT             "ppt"
164 %token              TOK_NOP             "nop"
165
166 %token              TOK_CLASS           "class"
167 %token              TOK_ID              "id"
168 %token              TOK_CON_ID          "con_id"
169 %token              TOK_TITLE           "title"
170
171 %token  <string>    STR                 "<string>"
172 %token  <number>    NUMBER              "<number>"
173
174 %type   <number>    direction
175 %type   <number>    split_direction
176 %type   <number>    level
177 %type   <number>    boolean
178 %type   <number>    border_style
179 %type   <number>    layout_mode
180 %type   <number>    resize_px
181 %type   <number>    resize_way
182 %type   <number>    resize_tiling
183 %type   <number>    optional_kill_mode
184
185 %%
186
187 commands:
188     commands ';' command
189     | command
190     {
191         owindow *current;
192
193         printf("single command completely parsed, dropping state...\n");
194         while (!TAILQ_EMPTY(&owindows)) {
195             current = TAILQ_FIRST(&owindows);
196             TAILQ_REMOVE(&owindows, current, owindows);
197             free(current);
198         }
199         match_init(&current_match);
200     }
201     ;
202
203 command:
204     match operations
205     ;
206
207 match:
208     | matchstart criteria matchend
209     {
210         printf("match parsed\n");
211     }
212     ;
213
214 matchstart:
215     '['
216     {
217         printf("start\n");
218         match_init(&current_match);
219         TAILQ_INIT(&owindows);
220         /* copy all_cons */
221         Con *con;
222         TAILQ_FOREACH(con, &all_cons, all_cons) {
223             owindow *ow = smalloc(sizeof(owindow));
224             ow->con = con;
225             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
226         }
227     }
228     ;
229
230 matchend:
231     ']'
232     {
233         owindow *next, *current;
234
235         printf("match specification finished, matching...\n");
236         /* copy the old list head to iterate through it and start with a fresh
237          * list which will contain only matching windows */
238         struct owindows_head old = owindows;
239         TAILQ_INIT(&owindows);
240         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
241             /* make a copy of the next pointer and advance the pointer to the
242              * next element as we are going to invalidate the element’s
243              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
244             current = next;
245             next = TAILQ_NEXT(next, owindows);
246
247             printf("checking if con %p / %s matches\n", current->con, current->con->name);
248             if (current_match.con_id != NULL) {
249                 if (current_match.con_id == current->con) {
250                     printf("matches container!\n");
251                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
252
253                 }
254             } else if (current_match.mark != NULL && current->con->mark != NULL &&
255                     strcasecmp(current_match.mark, current->con->mark) == 0) {
256                 printf("match by mark\n");
257                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
258
259             } else {
260                 if (current->con->window == NULL)
261                     continue;
262                 if (match_matches_window(&current_match, current->con->window)) {
263                     printf("matches window!\n");
264                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
265                 } else {
266                     printf("doesnt match\n");
267                     free(current);
268                 }
269             }
270         }
271
272         TAILQ_FOREACH(current, &owindows, owindows) {
273             printf("matching: %p / %s\n", current->con, current->con->name);
274         }
275
276     }
277     ;
278
279 criteria:
280     criteria criterion
281     | criterion
282     ;
283
284 criterion:
285     TOK_CLASS '=' STR
286     {
287         printf("criteria: class = %s\n", $3);
288         current_match.class = $3;
289     }
290     | TOK_CON_ID '=' STR
291     {
292         printf("criteria: id = %s\n", $3);
293         char *end;
294         long parsed = strtol($3, &end, 10);
295         if (parsed == LONG_MIN ||
296             parsed == LONG_MAX ||
297             parsed < 0 ||
298             (end && *end != '\0')) {
299             ELOG("Could not parse con id \"%s\"\n", $3);
300         } else {
301             current_match.con_id = (Con*)parsed;
302             printf("id as int = %p\n", current_match.con_id);
303         }
304     }
305     | TOK_ID '=' STR
306     {
307         printf("criteria: window id = %s\n", $3);
308         char *end;
309         long parsed = strtol($3, &end, 10);
310         if (parsed == LONG_MIN ||
311             parsed == LONG_MAX ||
312             parsed < 0 ||
313             (end && *end != '\0')) {
314             ELOG("Could not parse window id \"%s\"\n", $3);
315         } else {
316             current_match.id = parsed;
317             printf("window id as int = %d\n", current_match.id);
318         }
319     }
320     | TOK_MARK '=' STR
321     {
322         printf("criteria: mark = %s\n", $3);
323         current_match.mark = $3;
324     }
325     | TOK_TITLE '=' STR
326     {
327         printf("criteria: title = %s\n", $3);
328         current_match.title = $3;
329     }
330     ;
331
332 operations:
333     operation
334     | operations ',' operation
335     ;
336
337 operation:
338     exec
339     | exit
340     | restart
341     | reload
342     | border
343     | layout
344     | append_layout
345     | move
346     | workspace
347     | focus
348     | kill
349     | open
350     | fullscreen
351     | split
352     | floating
353     | mark
354     | resize
355     | nop
356     | mode
357     ;
358
359 exec:
360     TOK_EXEC STR
361     {
362         printf("should execute %s\n", $2);
363         start_application($2);
364         free($2);
365     }
366     ;
367
368 exit:
369     TOK_EXIT
370     {
371         printf("exit, bye bye\n");
372         exit(0);
373     }
374     ;
375
376 reload:
377     TOK_RELOAD
378     {
379         printf("reloading\n");
380         load_configuration(conn, NULL, true);
381         x_set_i3_atoms();
382         /* Send an IPC event just in case the ws names have changed */
383         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
384     }
385     ;
386
387 restart:
388     TOK_RESTART
389     {
390         printf("restarting i3\n");
391         i3_restart(false);
392     }
393     ;
394
395 focus:
396     TOK_FOCUS
397     {
398         owindow *current;
399
400         if (match_is_empty(&current_match)) {
401             ELOG("You have to specify which window/container should be focused.\n");
402             ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
403
404             asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
405                      "specify which window/container should be focused\"}");
406             break;
407         }
408
409         int count = 0;
410         TAILQ_FOREACH(current, &owindows, owindows) {
411             LOG("focusing %p / %s\n", current->con, current->con->name);
412             con_focus(current->con);
413             count++;
414         }
415
416         if (count > 1)
417             LOG("WARNING: Your criteria for the focus command matches %d containers, "
418                 "while only exactly one container can be focused at a time.\n", count);
419
420         tree_render();
421     }
422     | TOK_FOCUS direction
423     {
424         int direction = $2;
425         switch (direction) {
426             case TOK_LEFT:
427                 LOG("Focusing left\n");
428                 tree_next('p', HORIZ);
429                 break;
430             case TOK_RIGHT:
431                 LOG("Focusing right\n");
432                 tree_next('n', HORIZ);
433                 break;
434             case TOK_UP:
435                 LOG("Focusing up\n");
436                 tree_next('p', VERT);
437                 break;
438             case TOK_DOWN:
439                 LOG("Focusing down\n");
440                 tree_next('n', VERT);
441                 break;
442             default:
443                 ELOG("Invalid focus direction (%d)\n", direction);
444                 break;
445         }
446
447         tree_render();
448     }
449     | TOK_FOCUS level
450     {
451         if ($2 == TOK_PARENT)
452             level_up();
453         else level_down();
454
455         tree_render();
456     }
457     ;
458
459 level:
460     TOK_PARENT  { $$ = TOK_PARENT; }
461     | TOK_CHILD { $$ = TOK_CHILD;  }
462     ;
463
464 kill:
465     TOK_KILL optional_kill_mode
466     {
467         owindow *current;
468
469         printf("killing!\n");
470         /* check if the match is empty, not if the result is empty */
471         if (match_is_empty(&current_match))
472             tree_close_con($2);
473         else {
474             TAILQ_FOREACH(current, &owindows, owindows) {
475                 printf("matching: %p / %s\n", current->con, current->con->name);
476                 tree_close(current->con, $2, false);
477             }
478         }
479
480         tree_render();
481     }
482     ;
483
484 optional_kill_mode:
485     /* empty */             { $$ = KILL_WINDOW; }
486     | TOK_WINDOW { $$ = KILL_WINDOW; }
487     | TOK_CLIENT { $$ = KILL_CLIENT; }
488     ;
489
490 workspace:
491     TOK_WORKSPACE TOK_NEXT
492     {
493         workspace_next();
494         tree_render();
495     }
496     | TOK_WORKSPACE TOK_PREV
497     {
498         workspace_prev();
499         tree_render();
500     }
501     | TOK_WORKSPACE STR
502     {
503         printf("should switch to workspace %s\n", $2);
504         workspace_show($2);
505         free($2);
506
507         tree_render();
508     }
509     ;
510
511 open:
512     TOK_OPEN
513     {
514         printf("opening new container\n");
515         Con *con = tree_open_con(NULL, NULL);
516         con_focus(con);
517         asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
518
519         tree_render();
520     }
521     ;
522
523 fullscreen:
524     TOK_FULLSCREEN
525     {
526         printf("toggling fullscreen\n");
527         owindow *current;
528
529
530         HANDLE_EMPTY_MATCH;
531
532         TAILQ_FOREACH(current, &owindows, owindows) {
533             printf("matching: %p / %s\n", current->con, current->con->name);
534             con_toggle_fullscreen(current->con);
535         }
536
537         tree_render();
538     }
539     ;
540
541 split:
542     TOK_SPLIT split_direction
543     {
544         /* TODO: use matches */
545         printf("splitting in direction %c\n", $2);
546         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
547
548         tree_render();
549     }
550     ;
551
552 split_direction:
553     TOK_HORIZONTAL  { $$ = 'h'; }
554     | 'h'           { $$ = 'h'; }
555     | TOK_VERTICAL  { $$ = 'v'; }
556     | 'v'           { $$ = 'v'; }
557     ;
558
559 floating:
560     TOK_FLOATING boolean
561     {
562         HANDLE_EMPTY_MATCH;
563
564         owindow *current;
565         TAILQ_FOREACH(current, &owindows, owindows) {
566             printf("matching: %p / %s\n", current->con, current->con->name);
567             if ($2 == TOK_TOGGLE) {
568                 printf("should toggle mode\n");
569                 toggle_floating_mode(current->con, false);
570             } else {
571                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
572                 if ($2 == TOK_ENABLE) {
573                     floating_enable(current->con, false);
574                 } else {
575                     floating_disable(current->con, false);
576                 }
577             }
578         }
579
580         tree_render();
581     }
582     ;
583
584 boolean:
585     TOK_ENABLE    { $$ = TOK_ENABLE; }
586     | TOK_DISABLE { $$ = TOK_DISABLE; }
587     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
588     ;
589
590 border:
591     TOK_BORDER border_style
592     {
593         printf("border style should be changed to %d\n", $2);
594         owindow *current;
595
596         HANDLE_EMPTY_MATCH;
597
598         TAILQ_FOREACH(current, &owindows, owindows) {
599             printf("matching: %p / %s\n", current->con, current->con->name);
600             if ($2 == TOK_TOGGLE) {
601                 current->con->border_style++;
602                 current->con->border_style %= 3;
603             } else current->con->border_style = $2;
604         }
605
606         tree_render();
607     }
608     ;
609
610 border_style:
611     TOK_NORMAL      { $$ = BS_NORMAL; }
612     | TOK_NONE      { $$ = BS_NONE; }
613     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
614     | TOK_TOGGLE    { $$ = TOK_TOGGLE; }
615     ;
616
617 move:
618     TOK_MOVE direction
619     {
620         printf("moving in direction %d\n", $2);
621         tree_move($2);
622
623         tree_render();
624     }
625     | TOK_MOVE TOK_WORKSPACE STR
626     {
627         owindow *current;
628
629         printf("should move window to workspace %s\n", $3);
630         /* get the workspace */
631         Con *ws = workspace_get($3, NULL);
632         free($3);
633
634         HANDLE_EMPTY_MATCH;
635
636         TAILQ_FOREACH(current, &owindows, owindows) {
637             printf("matching: %p / %s\n", current->con, current->con->name);
638             con_move_to_workspace(current->con, ws);
639         }
640
641         tree_render();
642     }
643     ;
644
645 append_layout:
646     TOK_APPEND_LAYOUT STR
647     {
648         printf("restoring \"%s\"\n", $2);
649         tree_append_json($2);
650         free($2);
651         tree_render();
652     }
653     ;
654
655 layout:
656     TOK_LAYOUT layout_mode
657     {
658         printf("changing layout to %d\n", $2);
659         owindow *current;
660
661         /* check if the match is empty, not if the result is empty */
662         if (match_is_empty(&current_match))
663             con_set_layout(focused->parent, $2);
664         else {
665             TAILQ_FOREACH(current, &owindows, owindows) {
666                 printf("matching: %p / %s\n", current->con, current->con->name);
667                 con_set_layout(current->con, $2);
668             }
669         }
670
671         tree_render();
672     }
673     ;
674
675 layout_mode:
676     TOK_DEFAULT   { $$ = L_DEFAULT; }
677     | TOK_STACKED { $$ = L_STACKED; }
678     | TOK_TABBED  { $$ = L_TABBED; }
679     ;
680
681 mark:
682     TOK_MARK STR
683     {
684         printf("marking window with str %s\n", $2);
685         owindow *current;
686
687         HANDLE_EMPTY_MATCH;
688
689         TAILQ_FOREACH(current, &owindows, owindows) {
690             printf("matching: %p / %s\n", current->con, current->con->name);
691             current->con->mark = sstrdup($2);
692         }
693
694         free($<string>2);
695
696         tree_render();
697     }
698     ;
699
700 nop:
701     TOK_NOP STR
702     {
703         printf("-------------------------------------------------\n");
704         printf("  NOP: %s\n", $2);
705         printf("-------------------------------------------------\n");
706         free($2);
707     }
708     ;
709
710 resize:
711     TOK_RESIZE resize_way direction resize_px resize_tiling
712     {
713         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
714         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
715         int direction = $3;
716         int px = $4;
717         int ppt = $5;
718         if ($2 == TOK_SHRINK) {
719             px *= -1;
720             ppt *= -1;
721         }
722
723         if (con_is_floating(focused)) {
724             printf("floating resize\n");
725             if (direction == TOK_UP) {
726                 focused->parent->rect.y -= px;
727                 focused->parent->rect.height += px;
728             } else if (direction == TOK_DOWN) {
729                 focused->rect.height += px;
730             } else if (direction == TOK_LEFT) {
731                 focused->rect.x -= px;
732                 focused->rect.width += px;
733             } else {
734                 focused->rect.width += px;
735             }
736         } else {
737             LOG("tiling resize\n");
738             /* get the default percentage */
739             int children = con_num_children(focused->parent);
740             Con *other;
741             LOG("ins. %d children\n", children);
742             double percentage = 1.0 / children;
743             LOG("default percentage = %f\n", percentage);
744
745             if (direction == TOK_UP || direction == TOK_LEFT) {
746                 other = TAILQ_PREV(focused, nodes_head, nodes);
747             } else {
748                 other = TAILQ_NEXT(focused, nodes);
749             }
750             if (other == TAILQ_END(workspaces)) {
751                 LOG("No other container in this direction found, cannot resize.\n");
752                 return 0;
753             }
754             LOG("other->percent = %f\n", other->percent);
755             LOG("focused->percent before = %f\n", focused->percent);
756             if (focused->percent == 0.0)
757                 focused->percent = percentage;
758             if (other->percent == 0.0)
759                 other->percent = percentage;
760             focused->percent += ((double)ppt / 100.0);
761             other->percent -= ((double)ppt / 100.0);
762             LOG("focused->percent after = %f\n", focused->percent);
763             LOG("other->percent after = %f\n", other->percent);
764         }
765
766         tree_render();
767     }
768     ;
769
770 resize_px:
771     /* empty */
772     {
773         $$ = 10;
774     }
775     | NUMBER TOK_PX
776     {
777         $$ = $1;
778     }
779     ;
780
781 resize_tiling:
782     /* empty */
783     {
784         $$ = 10;
785     }
786     | TOK_OR NUMBER TOK_PPT
787     {
788         $$ = $2;
789     }
790     ;
791
792 resize_way:
793     TOK_GROW        { $$ = TOK_GROW; }
794     | TOK_SHRINK    { $$ = TOK_SHRINK; }
795     ;
796
797 direction:
798     TOK_UP          { $$ = TOK_UP; }
799     | TOK_DOWN      { $$ = TOK_DOWN; }
800     | TOK_LEFT      { $$ = TOK_LEFT; }
801     | TOK_RIGHT     { $$ = TOK_RIGHT; }
802     ;
803
804 mode:
805     TOK_MODE STR
806     {
807         switch_mode($2);
808     }
809     ;