]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
bugfix: really return focus list in IPC tree dump (instead of nodes list)
[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 }
90
91 %token TOK_ATTACH "attach"
92 %token TOK_EXEC "exec"
93 %token TOK_EXIT "exit"
94 %token TOK_RELOAD "reload"
95 %token TOK_RESTART "restart"
96 %token TOK_KILL "kill"
97 %token TOK_FULLSCREEN "fullscreen"
98 %token TOK_GLOBAL "global"
99 %token TOK_LAYOUT "layout"
100 %token TOK_DEFAULT "default"
101 %token TOK_STACKED "stacked"
102 %token TOK_TABBED "tabbed"
103 %token TOK_BORDER "border"
104 %token TOK_NONE "none"
105 %token TOK_1PIXEL "1pixel"
106 %token TOK_MODE "mode"
107 %token TOK_TILING "tiling"
108 %token TOK_FLOATING "floating"
109 %token TOK_WORKSPACE "workspace"
110 %token TOK_FOCUS "focus"
111 %token TOK_MOVE "move"
112 %token TOK_OPEN "open"
113
114 %token TOK_CLASS "class"
115 %token TOK_ID "id"
116 %token TOK_CON_ID "con_id"
117
118 %token WHITESPACE "<whitespace>"
119 %token STR "<string>"
120
121 %%
122
123 commands: /* empty */
124     | commands optwhitespace ';' optwhitespace command
125     | command
126     {
127         owindow *current;
128
129         printf("single command completely parsed, dropping state...\n");
130         while (!TAILQ_EMPTY(&owindows)) {
131             current = TAILQ_FIRST(&owindows);
132             TAILQ_REMOVE(&owindows, current, owindows);
133             free(current);
134         }
135         memset(&current_match, 0, sizeof(Match));
136     }
137     ;
138
139 optwhitespace:
140     | WHITESPACE
141     ;
142
143 command:
144     match optwhitespace operations
145     ;
146
147 match:
148     | matchstart optwhitespace criteria optwhitespace matchend
149     {
150         printf("match parsed\n");
151     }
152     ;
153
154 matchstart:
155     '['
156     {
157         printf("start\n");
158         memset(&current_match, '\0', sizeof(Match));
159         TAILQ_INIT(&owindows);
160         /* copy all_cons */
161         Con *con;
162         TAILQ_FOREACH(con, &all_cons, all_cons) {
163             owindow *ow = smalloc(sizeof(owindow));
164             ow->con = con;
165             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
166         }
167     }
168     ;
169
170 matchend:
171     ']'
172     {
173         owindow *next, *current;
174
175         printf("match specification finished, matching...\n");
176         /* copy the old list head to iterate through it and start with a fresh
177          * list which will contain only matching windows */
178         struct owindows_head old = owindows;
179         TAILQ_INIT(&owindows);
180         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
181             /* make a copy of the next pointer and advance the pointer to the
182              * next element as we are going to invalidate the element’s
183              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
184             current = next;
185             next = TAILQ_NEXT(next, owindows);
186
187             printf("checking if con %p / %s matches\n", current->con, current->con->name);
188             if (current_match.con_id != NULL) {
189                 if (current_match.con_id == current->con) {
190                     printf("matches container!\n");
191                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
192
193                 }
194             } else {
195                 if (current->con->window == NULL)
196                     continue;
197                 if (match_matches_window(&current_match, current->con->window)) {
198                     printf("matches window!\n");
199                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
200                 } else {
201                     printf("doesnt match\n");
202                     free(current);
203                 }
204             }
205         }
206
207         TAILQ_FOREACH(current, &owindows, owindows) {
208             printf("matching: %p / %s\n", current->con, current->con->name);
209         }
210
211     }
212     ;
213
214 criteria:
215     TOK_CLASS '=' STR
216     {
217         printf("criteria: class = %s\n", $<string>3);
218         current_match.class = $<string>3;
219     }
220     | TOK_CON_ID '=' STR
221     {
222         printf("criteria: id = %s\n", $<string>3);
223         /* TODO: correctly parse number */
224         current_match.con_id = atoi($<string>3);
225         printf("id as int = %d\n", current_match.con_id);
226     }
227     ;
228
229 operations:
230     operation
231     | operation optwhitespace
232     | operations ',' optwhitespace operation
233     ;
234
235 operation:
236     exec
237     | exit
238     /*| reload
239     | restart
240     | mark
241     | layout
242     | border
243     | mode
244     | workspace
245     | move*/
246     | workspace
247     | attach
248     | focus
249     | kill
250     | open
251     | fullscreen
252     ;
253
254 exec:
255     TOK_EXEC WHITESPACE STR
256     {
257         printf("should execute %s\n", $<string>3);
258     }
259     ;
260
261 exit:
262     TOK_EXIT
263     {
264         printf("exit, bye bye\n");
265     }
266     ;
267
268 attach:
269     TOK_ATTACH
270     {
271         printf("should attach\n");
272     }
273     ;
274
275 focus:
276     TOK_FOCUS
277     {
278         printf("should focus\n");
279     }
280     ;
281
282 kill:
283     TOK_KILL
284     {
285         owindow *current;
286
287         printf("killing!\n");
288         /* check if the match is empty, not if the result is empty */
289         if (match_is_empty(&current_match))
290             tree_close(focused);
291         else {
292             TAILQ_FOREACH(current, &owindows, owindows) {
293                 printf("matching: %p / %s\n", current->con, current->con->name);
294                 tree_close(current->con);
295             }
296         }
297
298     }
299     ;
300
301 workspace:
302     TOK_WORKSPACE WHITESPACE STR
303     {
304         printf("should switch to workspace %s\n", $<string>3);
305         workspace_show($<string>3);
306         free($<string>3);
307     }
308     ;
309
310 open:
311     TOK_OPEN
312     {
313         printf("opening new container\n");
314         tree_open_con(NULL);
315     }
316     ;
317
318 fullscreen:
319     TOK_FULLSCREEN
320     {
321         printf("toggling fullscreen\n");
322         owindow *current;
323
324         /* check if the match is empty, not if the result is empty */
325         if (match_is_empty(&current_match))
326             con_toggle_fullscreen(focused);
327         else {
328             TAILQ_FOREACH(current, &owindows, owindows) {
329                 printf("matching: %p / %s\n", current->con, current->con->name);
330                 con_toggle_fullscreen(current->con);
331             }
332         }
333
334     }
335     ;