]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
Implement mark/goto, modify testcase
[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 %token TOK_MARK "mark"
128
129 %token TOK_CLASS "class"
130 %token TOK_ID "id"
131 %token TOK_CON_ID "con_id"
132
133 %token WHITESPACE "<whitespace>"
134 %token STR "<string>"
135
136 %%
137
138 commands: /* empty */
139     | commands optwhitespace ';' optwhitespace command
140     | command
141     {
142         owindow *current;
143
144         printf("single command completely parsed, dropping state...\n");
145         while (!TAILQ_EMPTY(&owindows)) {
146             current = TAILQ_FIRST(&owindows);
147             TAILQ_REMOVE(&owindows, current, owindows);
148             free(current);
149         }
150         memset(&current_match, 0, sizeof(Match));
151     }
152     ;
153
154 optwhitespace:
155     | WHITESPACE
156     ;
157
158 command:
159     match optwhitespace operations
160     ;
161
162 match:
163     | matchstart optwhitespace criteria optwhitespace matchend
164     {
165         printf("match parsed\n");
166     }
167     ;
168
169 matchstart:
170     '['
171     {
172         printf("start\n");
173         memset(&current_match, '\0', sizeof(Match));
174         TAILQ_INIT(&owindows);
175         /* copy all_cons */
176         Con *con;
177         TAILQ_FOREACH(con, &all_cons, all_cons) {
178             owindow *ow = smalloc(sizeof(owindow));
179             ow->con = con;
180             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
181         }
182     }
183     ;
184
185 matchend:
186     ']'
187     {
188         owindow *next, *current;
189
190         printf("match specification finished, matching...\n");
191         /* copy the old list head to iterate through it and start with a fresh
192          * list which will contain only matching windows */
193         struct owindows_head old = owindows;
194         TAILQ_INIT(&owindows);
195         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
196             /* make a copy of the next pointer and advance the pointer to the
197              * next element as we are going to invalidate the element’s
198              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
199             current = next;
200             next = TAILQ_NEXT(next, owindows);
201
202             printf("checking if con %p / %s matches\n", current->con, current->con->name);
203             if (current_match.con_id != NULL) {
204                 if (current_match.con_id == current->con) {
205                     printf("matches container!\n");
206                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
207
208                 }
209             } else if (current_match.mark != NULL && current->con->mark != NULL &&
210                     strcasecmp(current_match.mark, current->con->mark) == 0) {
211                 printf("match by mark\n");
212                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
213
214             } else {
215                 if (current->con->window == NULL)
216                     continue;
217                 if (match_matches_window(&current_match, current->con->window)) {
218                     printf("matches window!\n");
219                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
220                 } else {
221                     printf("doesnt match\n");
222                     free(current);
223                 }
224             }
225         }
226
227         TAILQ_FOREACH(current, &owindows, owindows) {
228             printf("matching: %p / %s\n", current->con, current->con->name);
229         }
230
231     }
232     ;
233
234 criteria:
235     TOK_CLASS '=' STR
236     {
237         printf("criteria: class = %s\n", $<string>3);
238         current_match.class = $<string>3;
239     }
240     | TOK_CON_ID '=' STR
241     {
242         printf("criteria: id = %s\n", $<string>3);
243         /* TODO: correctly parse number */
244         current_match.con_id = atoi($<string>3);
245         printf("id as int = %d\n", current_match.con_id);
246     }
247     | TOK_ID '=' STR
248     {
249         printf("criteria: window id = %s\n", $<string>3);
250         /* TODO: correctly parse number */
251         current_match.id = atoi($<string>3);
252         printf("window id as int = %d\n", current_match.id);
253     }
254     | TOK_MARK '=' STR
255     {
256         printf("criteria: mark = %s\n", $<string>3);
257         current_match.mark = $<string>3;
258     }
259     ;
260
261 operations:
262     operation
263     | operation optwhitespace
264     | operations ',' optwhitespace operation
265     ;
266
267 operation:
268     exec
269     | exit
270     | restart
271     /*| reload
272     | mark
273     | border */
274     | layout
275     | restore
276     | move
277     | workspace
278     | attach
279     | focus
280     | kill
281     | open
282     | fullscreen
283     | next
284     | prev
285     | split
286     | mode
287     | level
288     | mark
289     ;
290
291 exec:
292     TOK_EXEC WHITESPACE STR
293     {
294         printf("should execute %s\n", $<string>3);
295         start_application($<string>3);
296     }
297     ;
298
299 exit:
300     TOK_EXIT
301     {
302         printf("exit, bye bye\n");
303         exit(0);
304     }
305     ;
306
307 restart:
308     TOK_RESTART
309     {
310         printf("restarting i3\n");
311         i3_restart();
312     }
313     ;
314
315 attach:
316     TOK_ATTACH
317     {
318         printf("should attach\n");
319     }
320     ;
321
322 focus:
323     TOK_FOCUS
324     {
325         owindow *current;
326
327         printf("should focus\n");
328         if (match_is_empty(&current_match)) {
329             /* TODO: better error message */
330             LOG("Error: The foucs command requires you to use some criteria.\n");
331             return;
332         }
333
334         /* TODO: warning if the match contains more than one entry. does not
335          * make so much sense when focusing */
336         TAILQ_FOREACH(current, &owindows, owindows) {
337             LOG("focusing %p / %s\n", current->con, current->con->name);
338             con_focus(current->con);
339         }
340     }
341     ;
342
343 kill:
344     TOK_KILL
345     {
346         owindow *current;
347
348         printf("killing!\n");
349         /* check if the match is empty, not if the result is empty */
350         if (match_is_empty(&current_match))
351             tree_close(focused, true);
352         else {
353             TAILQ_FOREACH(current, &owindows, owindows) {
354                 printf("matching: %p / %s\n", current->con, current->con->name);
355                 tree_close(current->con, true);
356             }
357         }
358
359     }
360     ;
361
362 workspace:
363     TOK_WORKSPACE WHITESPACE STR
364     {
365         printf("should switch to workspace %s\n", $<string>3);
366         workspace_show($<string>3);
367         free($<string>3);
368     }
369     ;
370
371 open:
372     TOK_OPEN
373     {
374         printf("opening new container\n");
375         tree_open_con(NULL);
376     }
377     ;
378
379 fullscreen:
380     TOK_FULLSCREEN
381     {
382         printf("toggling fullscreen\n");
383         owindow *current;
384
385         /* check if the match is empty, not if the result is empty */
386         if (match_is_empty(&current_match))
387             con_toggle_fullscreen(focused);
388         else {
389             TAILQ_FOREACH(current, &owindows, owindows) {
390                 printf("matching: %p / %s\n", current->con, current->con->name);
391                 con_toggle_fullscreen(current->con);
392             }
393         }
394
395     }
396     ;
397
398 next:
399     TOK_NEXT WHITESPACE direction
400     {
401         /* TODO: use matches */
402         printf("should select next window in direction %c\n", $<chr>3);
403         tree_next('n', ($<chr>3 == 'v' ? VERT : HORIZ));
404     }
405     ;
406
407 prev:
408     TOK_PREV WHITESPACE direction
409     {
410         /* TODO: use matches */
411         printf("should select prev window in direction %c\n", $<chr>3);
412         tree_next('p', ($<chr>3 == 'v' ? VERT : HORIZ));
413     }
414     ;
415
416 split:
417     TOK_SPLIT WHITESPACE direction
418     {
419         /* TODO: use matches */
420         printf("splitting in direction %c\n", $<chr>3);
421         tree_split(focused, ($<chr>3 == 'v' ? VERT : HORIZ));
422     }
423     ;
424
425 direction:
426     TOK_HORIZONTAL  { $<chr>$ = 'h'; }
427     | 'h'           { $<chr>$ = 'h'; }
428     | TOK_VERTICAL  { $<chr>$ = 'v'; }
429     | 'v'           { $<chr>$ = 'v'; }
430     ;
431
432 mode:
433     TOK_MODE WHITESPACE window_mode
434     {
435         if ($<number>3 == TOK_TOGGLE) {
436             printf("should toggle mode\n");
437             toggle_floating_mode(focused, false);
438         } else {
439             printf("should switch mode to %s\n", ($<number>3 == TOK_FLOATING ? "floating" : "tiling"));
440             /* TODO: actually switch mode (not toggle) */
441         }
442     }
443     ;
444
445 window_mode:
446     TOK_FLOATING    { $<number>$ = TOK_FLOATING; }
447     | TOK_TILING    { $<number>$ = TOK_TILING; }
448     | TOK_TOGGLE    { $<number>$ = TOK_TOGGLE; }
449     ;
450
451 level:
452     TOK_LEVEL WHITESPACE level_direction
453     {
454         printf("level %c\n", $<chr>3);
455         if ($<chr>3 == 'u')
456             level_up();
457         else level_down();
458     }
459     ;
460
461 level_direction:
462     TOK_UP     { $<chr>$ = 'u'; }
463     | TOK_DOWN { $<chr>$ = 'd'; }
464     ;
465
466 move:
467     TOK_MOVE WHITESPACE before_after WHITESPACE direction
468     {
469         printf("moving: %s and %c\n", ($<number>3 == TOK_BEFORE ? "before" : "after"), $<chr>5);
470         /* TODO: change API for the next call, we need to convert in both directions while ideally
471          * we should not need any of both */
472         tree_move(($<number>3 == TOK_BEFORE ? 'p' : 'n'), ($<chr>5 == 'v' ? VERT : HORIZ));
473     }
474     ;
475
476 before_after:
477     TOK_BEFORE { $<number>$ = TOK_BEFORE; }
478     | TOK_AFTER { $<number>$ = TOK_AFTER; }
479     ;
480
481 restore:
482     TOK_RESTORE WHITESPACE STR
483     {
484         printf("restoring \"%s\"\n", $<string>3);
485         tree_append_json($<string>3);
486     }
487     ;
488
489 layout:
490     TOK_LAYOUT WHITESPACE layout_mode
491     {
492         printf("changing layout to %d\n", $<number>3);
493         owindow *current;
494
495         /* check if the match is empty, not if the result is empty */
496         if (match_is_empty(&current_match))
497             focused->parent->layout = $<number>3;
498         else {
499             TAILQ_FOREACH(current, &owindows, owindows) {
500                 printf("matching: %p / %s\n", current->con, current->con->name);
501                 current->con->layout = $<number>3;
502             }
503         }
504
505     }
506     ;
507
508 layout_mode:
509     TOK_DEFAULT   { $<number>$ = L_DEFAULT; }
510     | TOK_STACKED { $<number>$ = L_STACKED; }
511     | TOK_TABBED  { $<number>$ = L_TABBED; }
512     ;
513
514 mark:
515     TOK_MARK WHITESPACE STR
516     {
517         printf("marking window with str %s\n", $<string>3);
518         owindow *current;
519
520         /* check if the match is empty, not if the result is empty */
521         if (match_is_empty(&current_match))
522             focused->mark = sstrdup($<string>3);
523         else {
524             TAILQ_FOREACH(current, &owindows, owindows) {
525                 printf("matching: %p / %s\n", current->con, current->con->name);
526                 current->con->mark = sstrdup($<string>3);
527             }
528         }
529
530         free($<string>3);
531     }
532     ;