]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
Implement focus command (and extend t/21-next-prev.t to test it)
[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 /* We don’t need yydebug for now, as we got decent error messages using
41  * yyerror(). Should you ever want to extend the parser, it might be handy
42  * to just comment it in again, so it stays here. */
43 //int cmdyydebug = 1;
44
45 void cmdyyerror(const char *error_message) {
46     ELOG("\n");
47     ELOG("CMD: %s\n", error_message);
48     ELOG("CMD: in file \"%s\", line %d:\n",
49             context->filename, context->line_number);
50     ELOG("CMD:   %s\n", context->line_copy);
51     ELOG("CMD:   ");
52     for (int c = 1; c <= context->last_column; c++)
53         if (c >= context->first_column)
54                 printf("^");
55         else printf(" ");
56     printf("\n");
57     ELOG("\n");
58 }
59
60 int cmdyywrap() {
61     return 1;
62 }
63
64 void parse_cmd(const char *new) {
65
66     //const char *new = "[level-up workspace] attach $output, focus";
67
68     cmdyy_scan_string(new);
69
70     context = scalloc(sizeof(struct context));
71     context->filename = "cmd";
72     if (cmdyyparse() != 0) {
73             fprintf(stderr, "Could not parse configfile\n");
74             exit(1);
75     }
76     printf("done\n");
77
78     FREE(context->line_copy);
79     free(context);
80 }
81
82 %}
83
84 %error-verbose
85 %lex-param { struct context *context }
86
87 %union {
88     char *string;
89     char chr;
90     int number;
91 }
92
93 %token TOK_ATTACH "attach"
94 %token TOK_EXEC "exec"
95 %token TOK_EXIT "exit"
96 %token TOK_RELOAD "reload"
97 %token TOK_RESTART "restart"
98 %token TOK_KILL "kill"
99 %token TOK_FULLSCREEN "fullscreen"
100 %token TOK_GLOBAL "global"
101 %token TOK_LAYOUT "layout"
102 %token TOK_DEFAULT "default"
103 %token TOK_STACKED "stacked"
104 %token TOK_TABBED "tabbed"
105 %token TOK_BORDER "border"
106 %token TOK_NONE "none"
107 %token TOK_1PIXEL "1pixel"
108 %token TOK_MODE "mode"
109 %token TOK_TILING "tiling"
110 %token TOK_FLOATING "floating"
111 %token TOK_WORKSPACE "workspace"
112 %token TOK_TOGGLE "toggle"
113 %token TOK_FOCUS "focus"
114 %token TOK_MOVE "move"
115 %token TOK_OPEN "open"
116 %token TOK_NEXT "next"
117 %token TOK_PREV "prev"
118 %token TOK_SPLIT "split"
119 %token TOK_HORIZONTAL "horizontal"
120 %token TOK_VERTICAL "vertical"
121 %token TOK_LEVEL "level"
122 %token TOK_UP "up"
123 %token TOK_DOWN "down"
124 %token TOK_AFTER "after"
125 %token TOK_BEFORE "before"
126 %token TOK_RESTORE "restore"
127
128 %token TOK_CLASS "class"
129 %token TOK_ID "id"
130 %token TOK_CON_ID "con_id"
131
132 %token WHITESPACE "<whitespace>"
133 %token STR "<string>"
134
135 %%
136
137 commands: /* empty */
138     | commands optwhitespace ';' optwhitespace command
139     | command
140     {
141         owindow *current;
142
143         printf("single command completely parsed, dropping state...\n");
144         while (!TAILQ_EMPTY(&owindows)) {
145             current = TAILQ_FIRST(&owindows);
146             TAILQ_REMOVE(&owindows, current, owindows);
147             free(current);
148         }
149         memset(&current_match, 0, sizeof(Match));
150     }
151     ;
152
153 optwhitespace:
154     | WHITESPACE
155     ;
156
157 command:
158     match optwhitespace operations
159     ;
160
161 match:
162     | matchstart optwhitespace criteria optwhitespace matchend
163     {
164         printf("match parsed\n");
165     }
166     ;
167
168 matchstart:
169     '['
170     {
171         printf("start\n");
172         memset(&current_match, '\0', sizeof(Match));
173         TAILQ_INIT(&owindows);
174         /* copy all_cons */
175         Con *con;
176         TAILQ_FOREACH(con, &all_cons, all_cons) {
177             owindow *ow = smalloc(sizeof(owindow));
178             ow->con = con;
179             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
180         }
181     }
182     ;
183
184 matchend:
185     ']'
186     {
187         owindow *next, *current;
188
189         printf("match specification finished, matching...\n");
190         /* copy the old list head to iterate through it and start with a fresh
191          * list which will contain only matching windows */
192         struct owindows_head old = owindows;
193         TAILQ_INIT(&owindows);
194         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
195             /* make a copy of the next pointer and advance the pointer to the
196              * next element as we are going to invalidate the element’s
197              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
198             current = next;
199             next = TAILQ_NEXT(next, owindows);
200
201             printf("checking if con %p / %s matches\n", current->con, current->con->name);
202             if (current_match.con_id != NULL) {
203                 if (current_match.con_id == current->con) {
204                     printf("matches container!\n");
205                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
206
207                 }
208             } else {
209                 if (current->con->window == NULL)
210                     continue;
211                 if (match_matches_window(&current_match, current->con->window)) {
212                     printf("matches window!\n");
213                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
214                 } else {
215                     printf("doesnt match\n");
216                     free(current);
217                 }
218             }
219         }
220
221         TAILQ_FOREACH(current, &owindows, owindows) {
222             printf("matching: %p / %s\n", current->con, current->con->name);
223         }
224
225     }
226     ;
227
228 criteria:
229     TOK_CLASS '=' STR
230     {
231         printf("criteria: class = %s\n", $<string>3);
232         current_match.class = $<string>3;
233     }
234     | TOK_CON_ID '=' STR
235     {
236         printf("criteria: id = %s\n", $<string>3);
237         /* TODO: correctly parse number */
238         current_match.con_id = atoi($<string>3);
239         printf("id as int = %d\n", current_match.con_id);
240     }
241     ;
242
243 operations:
244     operation
245     | operation optwhitespace
246     | operations ',' optwhitespace operation
247     ;
248
249 operation:
250     exec
251     | exit
252     | restart
253     /*| reload
254     | mark
255     | layout
256     | border */
257     | restore
258     | move
259     | workspace
260     | attach
261     | focus
262     | kill
263     | open
264     | fullscreen
265     | next
266     | prev
267     | split
268     | mode
269     | level
270     ;
271
272 exec:
273     TOK_EXEC WHITESPACE STR
274     {
275         printf("should execute %s\n", $<string>3);
276         start_application($<string>3);
277     }
278     ;
279
280 exit:
281     TOK_EXIT
282     {
283         printf("exit, bye bye\n");
284         exit(0);
285     }
286     ;
287
288 restart:
289     TOK_RESTART
290     {
291         printf("restarting i3\n");
292         i3_restart();
293     }
294     ;
295
296 attach:
297     TOK_ATTACH
298     {
299         printf("should attach\n");
300     }
301     ;
302
303 focus:
304     TOK_FOCUS
305     {
306         owindow *current;
307
308         printf("should focus\n");
309         if (match_is_empty(&current_match)) {
310             /* TODO: better error message */
311             LOG("Error: The foucs command requires you to use some criteria.\n");
312             return;
313         }
314
315         /* TODO: warning if the match contains more than one entry. does not
316          * make so much sense when focusing */
317         TAILQ_FOREACH(current, &owindows, owindows) {
318             LOG("focusing %p / %s\n", current->con, current->con->name);
319             con_focus(current->con);
320         }
321     }
322     ;
323
324 kill:
325     TOK_KILL
326     {
327         owindow *current;
328
329         printf("killing!\n");
330         /* check if the match is empty, not if the result is empty */
331         if (match_is_empty(&current_match))
332             tree_close(focused, true);
333         else {
334             TAILQ_FOREACH(current, &owindows, owindows) {
335                 printf("matching: %p / %s\n", current->con, current->con->name);
336                 tree_close(current->con, true);
337             }
338         }
339
340     }
341     ;
342
343 workspace:
344     TOK_WORKSPACE WHITESPACE STR
345     {
346         printf("should switch to workspace %s\n", $<string>3);
347         workspace_show($<string>3);
348         free($<string>3);
349     }
350     ;
351
352 open:
353     TOK_OPEN
354     {
355         printf("opening new container\n");
356         tree_open_con(NULL);
357     }
358     ;
359
360 fullscreen:
361     TOK_FULLSCREEN
362     {
363         printf("toggling fullscreen\n");
364         owindow *current;
365
366         /* check if the match is empty, not if the result is empty */
367         if (match_is_empty(&current_match))
368             con_toggle_fullscreen(focused);
369         else {
370             TAILQ_FOREACH(current, &owindows, owindows) {
371                 printf("matching: %p / %s\n", current->con, current->con->name);
372                 con_toggle_fullscreen(current->con);
373             }
374         }
375
376     }
377     ;
378
379 next:
380     TOK_NEXT WHITESPACE direction
381     {
382         /* TODO: use matches */
383         printf("should select next window in direction %c\n", $<chr>3);
384         tree_next('n', ($<chr>3 == 'v' ? VERT : HORIZ));
385     }
386     ;
387
388 prev:
389     TOK_PREV WHITESPACE direction
390     {
391         /* TODO: use matches */
392         printf("should select prev window in direction %c\n", $<chr>3);
393         tree_next('p', ($<chr>3 == 'v' ? VERT : HORIZ));
394     }
395     ;
396
397 split:
398     TOK_SPLIT WHITESPACE direction
399     {
400         /* TODO: use matches */
401         printf("splitting in direction %c\n", $<chr>3);
402         tree_split(focused, ($<chr>3 == 'v' ? VERT : HORIZ));
403     }
404     ;
405
406 direction:
407     TOK_HORIZONTAL  { $<chr>$ = 'h'; }
408     | 'h'           { $<chr>$ = 'h'; }
409     | TOK_VERTICAL  { $<chr>$ = 'v'; }
410     | 'v'           { $<chr>$ = 'v'; }
411     ;
412
413 mode:
414     TOK_MODE WHITESPACE window_mode
415     {
416         if ($<number>3 == TOK_TOGGLE) {
417             printf("should toggle mode\n");
418             toggle_floating_mode(focused, false);
419         } else {
420             printf("should switch mode to %s\n", ($<number>3 == TOK_FLOATING ? "floating" : "tiling"));
421             /* TODO: actually switch mode (not toggle) */
422         }
423     }
424     ;
425
426 window_mode:
427     TOK_FLOATING    { $<number>$ = TOK_FLOATING; }
428     | TOK_TILING    { $<number>$ = TOK_TILING; }
429     | TOK_TOGGLE    { $<number>$ = TOK_TOGGLE; }
430     ;
431
432 level:
433     TOK_LEVEL WHITESPACE level_direction
434     {
435         printf("level %c\n", $<chr>3);
436         if ($<chr>3 == 'u')
437             level_up();
438         else level_down();
439     }
440     ;
441
442 level_direction:
443     TOK_UP     { $<chr>$ = 'u'; }
444     | TOK_DOWN { $<chr>$ = 'd'; }
445     ;
446
447 move:
448     TOK_MOVE WHITESPACE before_after WHITESPACE direction
449     {
450         printf("moving: %s and %c\n", ($<number>3 == TOK_BEFORE ? "before" : "after"), $<chr>5);
451         /* TODO: change API for the next call, we need to convert in both directions while ideally
452          * we should not need any of both */
453         tree_move(($<number>3 == TOK_BEFORE ? 'p' : 'n'), ($<chr>5 == 'v' ? VERT : HORIZ));
454     }
455     ;
456
457 before_after:
458     TOK_BEFORE { $<number>$ = TOK_BEFORE; }
459     | TOK_AFTER { $<number>$ = TOK_AFTER; }
460     ;
461
462 restore:
463     TOK_RESTORE WHITESPACE STR
464     {
465         printf("restoring \"%s\"\n", $<string>3);
466         tree_append_json($<string>3);
467     }
468     ;