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