]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
cmdparse: don’t allow empty commands
[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:
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 optwhitespace
281     | operations ',' optwhitespace operation
282     ;
283
284 operation:
285     exec
286     | exit
287     | restart
288     | reload
289     | border
290     | layout
291     | restore
292     | move
293     | workspace
294     | attach
295     | focus
296     | kill
297     | open
298     | fullscreen
299     | next
300     | prev
301     | split
302     | mode
303     | level
304     | mark
305     | resize
306     | nop
307     ;
308
309 exec:
310     TOK_EXEC WHITESPACE STR
311     {
312         printf("should execute %s\n", $<string>3);
313         start_application($<string>3);
314         free($<string>3);
315     }
316     ;
317
318 exit:
319     TOK_EXIT
320     {
321         printf("exit, bye bye\n");
322         exit(0);
323     }
324     ;
325
326 reload:
327     TOK_RELOAD
328     {
329         printf("reloading\n");
330         load_configuration(conn, NULL, true);
331         /* Send an IPC event just in case the ws names have changed */
332         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
333     }
334     ;
335
336 restart:
337     TOK_RESTART
338     {
339         printf("restarting i3\n");
340         i3_restart(false);
341     }
342     ;
343
344 attach:
345     TOK_ATTACH
346     {
347         printf("should attach\n");
348     }
349     ;
350
351 focus:
352     TOK_FOCUS
353     {
354         owindow *current;
355
356         printf("should focus\n");
357         if (match_is_empty(&current_match)) {
358             /* TODO: better error message */
359             LOG("Error: The focus command requires you to use some criteria.\n");
360             break;
361         }
362
363         /* TODO: warning if the match contains more than one entry. does not
364          * make so much sense when focusing */
365         TAILQ_FOREACH(current, &owindows, owindows) {
366             LOG("focusing %p / %s\n", current->con, current->con->name);
367             con_focus(current->con);
368         }
369     }
370     ;
371
372 kill:
373     TOK_KILL
374     {
375         owindow *current;
376
377         printf("killing!\n");
378         /* check if the match is empty, not if the result is empty */
379         if (match_is_empty(&current_match))
380             tree_close_con();
381         else {
382             TAILQ_FOREACH(current, &owindows, owindows) {
383                 printf("matching: %p / %s\n", current->con, current->con->name);
384                 tree_close(current->con, true, false);
385             }
386         }
387
388     }
389     ;
390
391 workspace:
392     TOK_WORKSPACE WHITESPACE STR
393     {
394         printf("should switch to workspace %s\n", $<string>3);
395         workspace_show($<string>3);
396         free($<string>3);
397     }
398     ;
399
400 open:
401     TOK_OPEN
402     {
403         printf("opening new container\n");
404         Con *con = tree_open_con(NULL);
405         con_focus(con);
406         asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (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 direction
528     {
529         printf("moving in direction %d\n", $<number>3);
530         tree_move($<number>3);
531     }
532     | TOK_MOVE WHITESPACE TOK_WORKSPACE WHITESPACE STR
533     {
534         owindow *current;
535
536         printf("should move window to workspace %s\n", $<string>5);
537         /* get the workspace */
538         Con *ws = workspace_get($<string>5, NULL);
539         free($<string>5);
540
541         /* check if the match is empty, not if the result is empty */
542         if (match_is_empty(&current_match))
543             con_move_to_workspace(focused, ws);
544         else {
545             TAILQ_FOREACH(current, &owindows, owindows) {
546                 printf("matching: %p / %s\n", current->con, current->con->name);
547                 con_move_to_workspace(current->con, ws);
548             }
549         }
550     }
551     ;
552
553 restore:
554     TOK_RESTORE WHITESPACE STR
555     {
556         printf("restoring \"%s\"\n", $<string>3);
557         tree_append_json($<string>3);
558         free($<string>3);
559     }
560     ;
561
562 layout:
563     TOK_LAYOUT WHITESPACE layout_mode
564     {
565         printf("changing layout to %d\n", $<number>3);
566         owindow *current;
567
568         /* check if the match is empty, not if the result is empty */
569         if (match_is_empty(&current_match))
570             con_set_layout(focused->parent, $<number>3);
571         else {
572             TAILQ_FOREACH(current, &owindows, owindows) {
573                 printf("matching: %p / %s\n", current->con, current->con->name);
574                 con_set_layout(current->con, $<number>3);
575             }
576         }
577
578     }
579     ;
580
581 layout_mode:
582     TOK_DEFAULT   { $<number>$ = L_DEFAULT; }
583     | TOK_STACKED { $<number>$ = L_STACKED; }
584     | TOK_TABBED  { $<number>$ = L_TABBED; }
585     ;
586
587 mark:
588     TOK_MARK WHITESPACE STR
589     {
590         printf("marking window with str %s\n", $<string>3);
591         owindow *current;
592
593         /* check if the match is empty, not if the result is empty */
594         if (match_is_empty(&current_match))
595             focused->mark = sstrdup($<string>3);
596         else {
597             TAILQ_FOREACH(current, &owindows, owindows) {
598                 printf("matching: %p / %s\n", current->con, current->con->name);
599                 current->con->mark = sstrdup($<string>3);
600             }
601         }
602
603         free($<string>3);
604     }
605     ;
606
607 nop:
608     TOK_NOP WHITESPACE STR
609     {
610         printf("-------------------------------------------------\n");
611         printf("  NOP: %s\n", $<string>3);
612         printf("-------------------------------------------------\n");
613         free($<string>3);
614     }
615     ;
616
617 resize:
618     TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling
619     {
620         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
621         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $<number>3, $<number>5, $<number>6, $<number>7);
622         int direction = $<number>5;
623         int px = $<number>6;
624         int ppt = $<number>7;
625         if ($<number>3 == TOK_SHRINK) {
626             px *= -1;
627             ppt *= -1;
628         }
629
630         if (con_is_floating(focused)) {
631             printf("floating resize\n");
632             if (direction == TOK_UP) {
633                 focused->parent->rect.y -= px;
634                 focused->parent->rect.height += px;
635             } else if (direction == TOK_DOWN) {
636                 focused->rect.height += px;
637             } else if (direction == TOK_LEFT) {
638                 focused->rect.x -= px;
639                 focused->rect.width += px;
640             } else {
641                 focused->rect.width += px;
642             }
643         } else {
644             LOG("tiling resize\n");
645             /* get the default percentage */
646             int children = con_num_children(focused->parent);
647             Con *other;
648             LOG("ins. %d children\n", children);
649             double percentage = 1.0 / children;
650             LOG("default percentage = %f\n", percentage);
651
652             if (direction == TOK_UP || direction == TOK_LEFT) {
653                 other = TAILQ_PREV(focused, nodes_head, nodes);
654             } else {
655                 other = TAILQ_NEXT(focused, nodes);
656             }
657             if (other == TAILQ_END(workspaces)) {
658                 LOG("No other container in this direction found, cannot resize.\n");
659                 return 0;
660             }
661             LOG("other->percent = %f\n", other->percent);
662             LOG("focused->percent before = %f\n", focused->percent);
663             if (focused->percent == 0.0)
664                 focused->percent = percentage;
665             if (other->percent == 0.0)
666                 other->percent = percentage;
667             focused->percent += ((double)ppt / 100.0);
668             other->percent -= ((double)ppt / 100.0);
669             LOG("focused->percent after = %f\n", focused->percent);
670             LOG("other->percent after = %f\n", other->percent);
671         }
672     }
673     ;
674
675 resize_px:
676     /* empty */
677     {
678         $<number>$ = 10;
679     }
680     | WHITESPACE NUMBER WHITESPACE TOK_PX
681     {
682         $<number>$ = $<number>2;
683     }
684     ;
685
686 resize_tiling:
687     /* empty */
688     {
689         $<number>$ = 10;
690     }
691     | WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT
692     {
693         $<number>$ = $<number>4;
694     }
695     ;
696
697 resize_way:
698     TOK_GROW        { $<number>$ = TOK_GROW; }
699     | TOK_SHRINK    { $<number>$ = TOK_SHRINK; }
700     ;
701
702 direction:
703     TOK_UP          { $<number>$ = TOK_UP; }
704     | TOK_DOWN      { $<number>$ = TOK_DOWN; }
705     | TOK_LEFT      { $<number>$ = TOK_LEFT; }
706     | TOK_RIGHT     { $<number>$ = TOK_RIGHT; }
707     ;