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