]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
check for empty matches
[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     }
136     ;
137
138 optwhitespace:
139     | WHITESPACE
140     ;
141
142 command:
143     match optwhitespace operations
144     ;
145
146 match:
147     | matchstart optwhitespace criteria optwhitespace matchend
148     {
149         printf("match parsed\n");
150     }
151     ;
152
153 matchstart:
154     '['
155     {
156         printf("start\n");
157         memset(&current_match, '\0', sizeof(Match));
158         TAILQ_INIT(&owindows);
159         /* copy all_cons */
160         Con *con;
161         TAILQ_FOREACH(con, &all_cons, all_cons) {
162             owindow *ow = smalloc(sizeof(owindow));
163             ow->con = con;
164             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
165         }
166     }
167     ;
168
169 matchend:
170     ']'
171     {
172         owindow *next, *current;
173
174         printf("match specification finished, matching...\n");
175         /* copy the old list head to iterate through it and start with a fresh
176          * list which will contain only matching windows */
177         struct owindows_head old = owindows;
178         TAILQ_INIT(&owindows);
179         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
180             /* make a copy of the next pointer and advance the pointer to the
181              * next element as we are going to invalidate the element’s
182              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
183             current = next;
184             next = TAILQ_NEXT(next, owindows);
185
186             printf("checking if con %p / %s matches\n", current->con, current->con->name);
187             if (current_match.con_id != NULL) {
188                 if (current_match.con_id == current->con) {
189                     printf("matches container!\n");
190                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
191
192                 }
193             } else {
194                 if (current->con->window == NULL)
195                     continue;
196                 if (match_matches_window(&current_match, current->con->window)) {
197                     printf("matches window!\n");
198                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
199                 } else {
200                     printf("doesnt match\n");
201                     free(current);
202                 }
203             }
204         }
205
206         TAILQ_FOREACH(current, &owindows, owindows) {
207             printf("matching: %p / %s\n", current->con, current->con->name);
208         }
209
210     }
211     ;
212
213 criteria:
214     TOK_CLASS '=' STR
215     {
216         printf("criteria: class = %s\n", $<string>3);
217         current_match.class = $<string>3;
218     }
219     | TOK_CON_ID '=' STR
220     {
221         printf("criteria: id = %s\n", $<string>3);
222         /* TODO: correctly parse number */
223         current_match.con_id = atoi($<string>3);
224         printf("id as int = %d\n", current_match.con_id);
225     }
226     ;
227
228 operations:
229     operation
230     | operation optwhitespace
231     | operations ',' optwhitespace operation
232     ;
233
234 operation:
235     exec
236     | exit
237     /*| reload
238     | restart
239     | mark
240     | fullscreen
241     | layout
242     | border
243     | mode
244     | workspace
245     | move*/
246     | workspace
247     | attach
248     | focus
249     | kill
250     | open
251     ;
252
253 exec:
254     TOK_EXEC WHITESPACE STR
255     {
256         printf("should execute %s\n", $<string>3);
257     }
258     ;
259
260 exit:
261     TOK_EXIT
262     {
263         printf("exit, bye bye\n");
264     }
265     ;
266
267 attach:
268     TOK_ATTACH
269     {
270         printf("should attach\n");
271     }
272     ;
273
274 focus:
275     TOK_FOCUS
276     {
277         printf("should focus\n");
278     }
279     ;
280
281 kill:
282     TOK_KILL
283     {
284         owindow *current;
285
286         printf("killing!\n");
287         /* TODO: check if the match is empty, not if the result is empty */
288         if (match_is_empty(&current_match))
289             tree_close(focused);
290         else {
291         TAILQ_FOREACH(current, &owindows, owindows) {
292             printf("matching: %p / %s\n", current->con, current->con->name);
293             tree_close(current->con);
294         }
295         }
296
297     }
298     ;
299
300 workspace:
301     TOK_WORKSPACE WHITESPACE STR
302     {
303         printf("should switch to workspace %s\n", $<string>3);
304         workspace_show($<string>3);
305         free($<string>3);
306     }
307     ;
308
309 open:
310     TOK_OPEN
311     {
312         printf("opening new container\n");
313         tree_open_con(NULL);
314     }
315     ;