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);
135 memset(¤t_match, 0, sizeof(Match));
144 match optwhitespace operations
148 | matchstart optwhitespace criteria optwhitespace matchend
150 printf("match parsed\n");
158 memset(¤t_match, '\0', sizeof(Match));
159 TAILQ_INIT(&owindows);
162 TAILQ_FOREACH(con, &all_cons, all_cons) {
163 owindow *ow = smalloc(sizeof(owindow));
165 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
173 owindow *next, *current;
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 */
185 next = TAILQ_NEXT(next, owindows);
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);
195 if (current->con->window == NULL)
197 if (match_matches_window(¤t_match, current->con->window)) {
198 printf("matches window!\n");
199 TAILQ_INSERT_TAIL(&owindows, current, owindows);
201 printf("doesnt match\n");
207 TAILQ_FOREACH(current, &owindows, owindows) {
208 printf("matching: %p / %s\n", current->con, current->con->name);
217 printf("criteria: class = %s\n", $<string>3);
218 current_match.class = $<string>3;
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);
231 | operation optwhitespace
232 | operations ',' optwhitespace operation
255 TOK_EXEC WHITESPACE STR
257 printf("should execute %s\n", $<string>3);
264 printf("exit, bye bye\n");
271 printf("should attach\n");
278 printf("should focus\n");
287 printf("killing!\n");
288 /* TODO: check if the match is empty, not if the result is empty */
289 if (match_is_empty(¤t_match))
292 TAILQ_FOREACH(current, &owindows, owindows) {
293 printf("matching: %p / %s\n", current->con, current->con->name);
294 tree_close(current->con);
302 TOK_WORKSPACE WHITESPACE STR
304 printf("should switch to workspace %s\n", $<string>3);
305 workspace_show($<string>3);
313 printf("opening new container\n");