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