3 * vim:ts=4:sw=4:expandtab
5 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
8 * cmdparse.y: the parser for commands you send to i3 (or bind on keys)
12 #include <sys/types.h>
19 typedef struct yy_buffer_state *YY_BUFFER_STATE;
20 extern int cmdyylex(struct context *context);
21 extern int cmdyyparse(void);
23 YY_BUFFER_STATE cmdyy_scan_string(const char *);
25 static struct bindings_head *current_bindings;
26 static struct context *context;
27 static Match current_match;
30 * Helper data structure for an operation window (window on which the operation
31 * will be performed). Used to build the TAILQ owindows.
34 typedef struct owindow {
36 TAILQ_ENTRY(owindow) owindows;
38 static TAILQ_HEAD(owindows_head, owindow) owindows;
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. */
45 void cmdyyerror(const char *error_message) {
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);
52 for (int c = 1; c <= context->last_column; c++)
53 if (c >= context->first_column)
64 void parse_cmd(const char *new) {
66 //const char *new = "[level-up workspace] attach $output, focus";
68 cmdyy_scan_string(new);
70 context = scalloc(sizeof(struct context));
71 context->filename = "cmd";
72 if (cmdyyparse() != 0) {
73 fprintf(stderr, "Could not parse configfile\n");
78 FREE(context->line_copy);
85 %lex-param { struct context *context }
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"
114 %token TOK_CLASS "class"
116 %token TOK_CON_ID "con_id"
118 %token WHITESPACE "<whitespace>"
119 %token STR "<string>"
123 commands: /* empty */
124 | commands optwhitespace ';' optwhitespace command
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);
143 match optwhitespace operations
147 | matchstart optwhitespace criteria optwhitespace matchend
149 printf("match parsed\n");
157 memset(¤t_match, '\0', sizeof(Match));
158 TAILQ_INIT(&owindows);
161 TAILQ_FOREACH(con, &all_cons, all_cons) {
162 owindow *ow = smalloc(sizeof(owindow));
164 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
172 owindow *next, *current;
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 */
184 next = TAILQ_NEXT(next, owindows);
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);
194 if (current->con->window == NULL)
196 if (match_matches_window(¤t_match, current->con->window)) {
197 printf("matches window!\n");
198 TAILQ_INSERT_TAIL(&owindows, current, owindows);
200 printf("doesnt match\n");
206 TAILQ_FOREACH(current, &owindows, owindows) {
207 printf("matching: %p / %s\n", current->con, current->con->name);
216 printf("criteria: class = %s\n", $<string>3);
217 current_match.class = $<string>3;
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);
230 | operation optwhitespace
231 | operations ',' optwhitespace operation
254 TOK_EXEC WHITESPACE STR
256 printf("should execute %s\n", $<string>3);
263 printf("exit, bye bye\n");
270 printf("should attach\n");
277 printf("should focus\n");
286 printf("killing!\n");
287 /* TODO: check if the match is empty, not if the result is empty */
288 if (match_is_empty(¤t_match))
291 TAILQ_FOREACH(current, &owindows, owindows) {
292 printf("matching: %p / %s\n", current->con, current->con->name);
293 tree_close(current->con);
301 TOK_WORKSPACE WHITESPACE STR
303 printf("should switch to workspace %s\n", $<string>3);
304 workspace_show($<string>3);
312 printf("opening new container\n");