]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
47faa7f7d93b9e96cda3ffd12c6fb2d7d392d625
[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_FOCUS "focus"
113 %token TOK_MOVE "move"
114 %token TOK_OPEN "open"
115 %token TOK_NEXT "next"
116 %token TOK_PREV "prev"
117 %token TOK_SPLIT "split"
118 %token TOK_HORIZONTAL "horizontal"
119 %token TOK_VERTICAL "vertical"
120 %token TOK_LEVEL "level"
121 %token TOK_UP "up"
122 %token TOK_DOWN "down"
123 %token TOK_AFTER "after"
124 %token TOK_BEFORE "before"
125 %token TOK_RESTORE "restore"
126
127 %token TOK_CLASS "class"
128 %token TOK_ID "id"
129 %token TOK_CON_ID "con_id"
130
131 %token WHITESPACE "<whitespace>"
132 %token STR "<string>"
133
134 %%
135
136 commands: /* empty */
137     | commands optwhitespace ';' optwhitespace command
138     | command
139     {
140         owindow *current;
141
142         printf("single command completely parsed, dropping state...\n");
143         while (!TAILQ_EMPTY(&owindows)) {
144             current = TAILQ_FIRST(&owindows);
145             TAILQ_REMOVE(&owindows, current, owindows);
146             free(current);
147         }
148         memset(&current_match, 0, sizeof(Match));
149     }
150     ;
151
152 optwhitespace:
153     | WHITESPACE
154     ;
155
156 command:
157     match optwhitespace operations
158     ;
159
160 match:
161     | matchstart optwhitespace criteria optwhitespace matchend
162     {
163         printf("match parsed\n");
164     }
165     ;
166
167 matchstart:
168     '['
169     {
170         printf("start\n");
171         memset(&current_match, '\0', sizeof(Match));
172         TAILQ_INIT(&owindows);
173         /* copy all_cons */
174         Con *con;
175         TAILQ_FOREACH(con, &all_cons, all_cons) {
176             owindow *ow = smalloc(sizeof(owindow));
177             ow->con = con;
178             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
179         }
180     }
181     ;
182
183 matchend:
184     ']'
185     {
186         owindow *next, *current;
187
188         printf("match specification finished, matching...\n");
189         /* copy the old list head to iterate through it and start with a fresh
190          * list which will contain only matching windows */
191         struct owindows_head old = owindows;
192         TAILQ_INIT(&owindows);
193         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
194             /* make a copy of the next pointer and advance the pointer to the
195              * next element as we are going to invalidate the element’s
196              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
197             current = next;
198             next = TAILQ_NEXT(next, owindows);
199
200             printf("checking if con %p / %s matches\n", current->con, current->con->name);
201             if (current_match.con_id != NULL) {
202                 if (current_match.con_id == current->con) {
203                     printf("matches container!\n");
204                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
205
206                 }
207             } else {
208                 if (current->con->window == NULL)
209                     continue;
210                 if (match_matches_window(&current_match, current->con->window)) {
211                     printf("matches window!\n");
212                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
213                 } else {
214                     printf("doesnt match\n");
215                     free(current);
216                 }
217             }
218         }
219
220         TAILQ_FOREACH(current, &owindows, owindows) {
221             printf("matching: %p / %s\n", current->con, current->con->name);
222         }
223
224     }
225     ;
226
227 criteria:
228     TOK_CLASS '=' STR
229     {
230         printf("criteria: class = %s\n", $<string>3);
231         current_match.class = $<string>3;
232     }
233     | TOK_CON_ID '=' STR
234     {
235         printf("criteria: id = %s\n", $<string>3);
236         /* TODO: correctly parse number */
237         current_match.con_id = atoi($<string>3);
238         printf("id as int = %d\n", current_match.con_id);
239     }
240     ;
241
242 operations:
243     operation
244     | operation optwhitespace
245     | operations ',' optwhitespace operation
246     ;
247
248 operation:
249     exec
250     | exit
251     | restart
252     /*| reload
253     | mark
254     | layout
255     | border */
256     | restore
257     | move
258     | workspace
259     | attach
260     | focus
261     | kill
262     | open
263     | fullscreen
264     | next
265     | prev
266     | split
267     | mode
268     | level
269     ;
270
271 exec:
272     TOK_EXEC WHITESPACE STR
273     {
274         printf("should execute %s\n", $<string>3);
275         start_application($<string>3);
276     }
277     ;
278
279 exit:
280     TOK_EXIT
281     {
282         printf("exit, bye bye\n");
283         exit(0);
284     }
285     ;
286
287 restart:
288     TOK_RESTART
289     {
290         printf("restarting i3\n");
291         i3_restart();
292     }
293     ;
294
295 attach:
296     TOK_ATTACH
297     {
298         printf("should attach\n");
299     }
300     ;
301
302 focus:
303     TOK_FOCUS
304     {
305         printf("should focus\n");
306     }
307     ;
308
309 kill:
310     TOK_KILL
311     {
312         owindow *current;
313
314         printf("killing!\n");
315         /* check if the match is empty, not if the result is empty */
316         if (match_is_empty(&current_match))
317             tree_close(focused);
318         else {
319             TAILQ_FOREACH(current, &owindows, owindows) {
320                 printf("matching: %p / %s\n", current->con, current->con->name);
321                 tree_close(current->con);
322             }
323         }
324
325     }
326     ;
327
328 workspace:
329     TOK_WORKSPACE WHITESPACE STR
330     {
331         printf("should switch to workspace %s\n", $<string>3);
332         workspace_show($<string>3);
333         free($<string>3);
334     }
335     ;
336
337 open:
338     TOK_OPEN
339     {
340         printf("opening new container\n");
341         tree_open_con(NULL);
342     }
343     ;
344
345 fullscreen:
346     TOK_FULLSCREEN
347     {
348         printf("toggling fullscreen\n");
349         owindow *current;
350
351         /* check if the match is empty, not if the result is empty */
352         if (match_is_empty(&current_match))
353             con_toggle_fullscreen(focused);
354         else {
355             TAILQ_FOREACH(current, &owindows, owindows) {
356                 printf("matching: %p / %s\n", current->con, current->con->name);
357                 con_toggle_fullscreen(current->con);
358             }
359         }
360
361     }
362     ;
363
364 next:
365     TOK_NEXT WHITESPACE direction
366     {
367         /* TODO: use matches */
368         printf("should select next window in direction %c\n", $<chr>3);
369         tree_next('n', ($<chr>3 == 'v' ? VERT : HORIZ));
370     }
371     ;
372
373 prev:
374     TOK_PREV WHITESPACE direction
375     {
376         /* TODO: use matches */
377         printf("should select prev window in direction %c\n", $<chr>3);
378         tree_next('p', ($<chr>3 == 'v' ? VERT : HORIZ));
379     }
380     ;
381
382 split:
383     TOK_SPLIT WHITESPACE direction
384     {
385         /* TODO: use matches */
386         printf("splitting in direction %c\n", $<chr>3);
387         tree_split(focused, ($<chr>3 == 'v' ? VERT : HORIZ));
388     }
389     ;
390
391 direction:
392     TOK_HORIZONTAL  { $<chr>$ = 'h'; }
393     | 'h'           { $<chr>$ = 'h'; }
394     | TOK_VERTICAL  { $<chr>$ = 'v'; }
395     | 'v'           { $<chr>$ = 'v'; }
396     ;
397
398 mode:
399     TOK_MODE WHITESPACE window_mode
400     {
401         printf("should switch mode to %s\n", ($<number>3 == TOK_FLOATING ? "floating" : "tiling"));
402         /* TODO: actually switch mode (not toggle) */
403     }
404     ;
405
406 window_mode:
407     TOK_FLOATING    { $<number>$ = TOK_FLOATING; }
408     | TOK_TILING    { $<number>$ = TOK_TILING; }
409     ;
410
411 level:
412     TOK_LEVEL WHITESPACE level_direction
413     {
414         printf("level %c\n", $<chr>3);
415         if ($<chr>3 == 'u')
416             level_up();
417         else level_down();
418     }
419     ;
420
421 level_direction:
422     TOK_UP     { $<chr>$ = 'u'; }
423     | TOK_DOWN { $<chr>$ = 'd'; }
424     ;
425
426 move:
427     TOK_MOVE WHITESPACE before_after WHITESPACE direction
428     {
429         printf("moving: %s and %c\n", ($<number>3 == TOK_BEFORE ? "before" : "after"), $<chr>5);
430         /* TODO: change API for the next call, we need to convert in both directions while ideally
431          * we should not need any of both */
432         tree_move(($<number>3 == TOK_BEFORE ? 'p' : 'n'), ($<chr>5 == 'v' ? VERT : HORIZ));
433     }
434     ;
435
436 before_after:
437     TOK_BEFORE { $<number>$ = TOK_BEFORE; }
438     | TOK_AFTER { $<number>$ = TOK_AFTER; }
439     ;
440
441 restore:
442     TOK_RESTORE WHITESPACE STR
443     {
444         printf("restoring \"%s\"\n", $<string>3);
445         tree_append_json($<string>3);
446     }
447     ;