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