diff options
-rw-r--r-- | sway/commands.c | 84 |
1 files changed, 53 insertions, 31 deletions
diff --git a/sway/commands.c b/sway/commands.c index f4de72d8..96245c8a 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -30,13 +30,44 @@ struct modifier_key modifiers[] = { | |||
30 | { "Mod5", WLC_BIT_MOD_MOD5 }, | 30 | { "Mod5", WLC_BIT_MOD_MOD5 }, |
31 | }; | 31 | }; |
32 | 32 | ||
33 | enum expected_args { | ||
34 | EXPECTED_MORE_THEN, | ||
35 | EXPECTED_LESS_THEN, | ||
36 | EXPECTED_SAME_AS | ||
37 | }; | ||
33 | 38 | ||
39 | static bool checkarg(int argc, char *name, enum expected_args type, int val) { | ||
40 | switch(type) { | ||
41 | case EXPECTED_MORE_THEN: | ||
42 | if (argc > val) { | ||
43 | return true; | ||
44 | } | ||
45 | sway_log(L_ERROR, "Invalid %s command." | ||
46 | "(expected more then %d arguments, got %d", name, val, argc); | ||
47 | break; | ||
48 | case EXPECTED_LESS_THEN: | ||
49 | if (argc < val) { | ||
50 | return true; | ||
51 | }; | ||
52 | sway_log(L_ERROR, "Invalid %s command." | ||
53 | "(expected less then %d arguments, got %d", name, val, argc); | ||
54 | break; | ||
55 | case EXPECTED_SAME_AS: | ||
56 | if (argc == val) { | ||
57 | return true; | ||
58 | }; | ||
59 | sway_log(L_ERROR, "Invalid %s command." | ||
60 | "(expected %d arguments, got %d", name, val, argc); | ||
61 | break; | ||
62 | } | ||
63 | return false; | ||
64 | } | ||
34 | 65 | ||
35 | bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { | 66 | |
36 | if (argc < 2) { | 67 | static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { |
37 | sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); | 68 | if (checkarg(argc, "bindsym", EXPECTED_MORE_THEN, 1) == false) { |
38 | return false; | 69 | return false; |
39 | } | 70 | }; |
40 | 71 | ||
41 | struct sway_binding *binding = malloc(sizeof(struct sway_binding)); | 72 | struct sway_binding *binding = malloc(sizeof(struct sway_binding)); |
42 | binding->keys = create_list(); | 73 | binding->keys = create_list(); |
@@ -89,8 +120,7 @@ static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { | |||
89 | cleanup = true; | 120 | cleanup = true; |
90 | } | 121 | } |
91 | 122 | ||
92 | if (argc < 1) { | 123 | if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { |
93 | sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc); | ||
94 | return false; | 124 | return false; |
95 | } | 125 | } |
96 | 126 | ||
@@ -125,8 +155,7 @@ static bool cmd_exec(struct sway_config *config, int argc, char **argv) { | |||
125 | } | 155 | } |
126 | 156 | ||
127 | static bool cmd_exit(struct sway_config *config, int argc, char **argv) { | 157 | static bool cmd_exit(struct sway_config *config, int argc, char **argv) { |
128 | if (argc != 0) { | 158 | if (checkarg(argc, "exit", EXPECTED_SAME_AS, 0) == false) { |
129 | sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc); | ||
130 | return false; | 159 | return false; |
131 | } | 160 | } |
132 | // TODO: Some kind of clean up is probably in order | 161 | // TODO: Some kind of clean up is probably in order |
@@ -135,8 +164,7 @@ static bool cmd_exit(struct sway_config *config, int argc, char **argv) { | |||
135 | } | 164 | } |
136 | 165 | ||
137 | static bool cmd_focus(struct sway_config *config, int argc, char **argv) { | 166 | static bool cmd_focus(struct sway_config *config, int argc, char **argv) { |
138 | if (argc != 1) { | 167 | if (checkarg(argc, "focus", EXPECTED_SAME_AS, 1) == false) { |
139 | sway_log(L_ERROR, "Invalid focus command (expected 1 arguments, got %d)", argc); | ||
140 | return false; | 168 | return false; |
141 | } | 169 | } |
142 | if (strcasecmp(argv[0], "left") == 0) { | 170 | if (strcasecmp(argv[0], "left") == 0) { |
@@ -154,8 +182,7 @@ static bool cmd_focus(struct sway_config *config, int argc, char **argv) { | |||
154 | } | 182 | } |
155 | 183 | ||
156 | static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { | 184 | static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { |
157 | if (argc != 1) { | 185 | if (checkarg(argc, "focus_follows_mouse", EXPECTED_SAME_AS, 1) == false) { |
158 | sway_log(L_ERROR, "Invalid focus_follows_mouse command (expected 1 arguments, got %d)", argc); | ||
159 | return false; | 186 | return false; |
160 | } | 187 | } |
161 | 188 | ||
@@ -164,8 +191,7 @@ static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char * | |||
164 | } | 191 | } |
165 | 192 | ||
166 | static bool cmd_layout(struct sway_config *config, int argc, char **argv) { | 193 | static bool cmd_layout(struct sway_config *config, int argc, char **argv) { |
167 | if (argc < 1) { | 194 | if (checkarg(argc, "layout", EXPECTED_MORE_THEN, 0) == false) { |
168 | sway_log(L_ERROR, "Invalid layout command (expected at least 1 argument, got %d)", argc); | ||
169 | return false; | 195 | return false; |
170 | } | 196 | } |
171 | swayc_t *parent = get_focused_container(&root_container); | 197 | swayc_t *parent = get_focused_container(&root_container); |
@@ -189,8 +215,7 @@ static bool cmd_layout(struct sway_config *config, int argc, char **argv) { | |||
189 | } | 215 | } |
190 | 216 | ||
191 | static bool cmd_reload(struct sway_config *config, int argc, char **argv) { | 217 | static bool cmd_reload(struct sway_config *config, int argc, char **argv) { |
192 | if (argc != 0) { | 218 | if (checkarg(argc, "reload", EXPECTED_SAME_AS, 0) == false) { |
193 | sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc); | ||
194 | return false; | 219 | return false; |
195 | } | 220 | } |
196 | if (!load_config()) { | 221 | if (!load_config()) { |
@@ -201,8 +226,7 @@ static bool cmd_reload(struct sway_config *config, int argc, char **argv) { | |||
201 | } | 226 | } |
202 | 227 | ||
203 | static bool cmd_set(struct sway_config *config, int argc, char **argv) { | 228 | static bool cmd_set(struct sway_config *config, int argc, char **argv) { |
204 | if (argc != 2) { | 229 | if (checkarg(argc, "set", EXPECTED_SAME_AS, 2) == false) { |
205 | sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); | ||
206 | return false; | 230 | return false; |
207 | } | 231 | } |
208 | struct sway_variable *var = malloc(sizeof(struct sway_variable)); | 232 | struct sway_variable *var = malloc(sizeof(struct sway_variable)); |
@@ -215,8 +239,10 @@ static bool cmd_set(struct sway_config *config, int argc, char **argv) { | |||
215 | } | 239 | } |
216 | 240 | ||
217 | static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) { | 241 | static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) { |
218 | if (argc != 0) { | 242 | char *name = layout == L_VERT ? "splitv": |
219 | sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc); | 243 | layout == L_HORIZ ? "splith": |
244 | "split"; | ||
245 | if (checkarg(argc, name, EXPECTED_SAME_AS, 0) == false) { | ||
220 | return false; | 246 | return false; |
221 | } | 247 | } |
222 | swayc_t *focused = get_focused_container(&root_container); | 248 | swayc_t *focused = get_focused_container(&root_container); |
@@ -248,11 +274,9 @@ static bool cmd_splith(struct sway_config *config, int argc, char **argv) { | |||
248 | } | 274 | } |
249 | 275 | ||
250 | static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { | 276 | static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { |
251 | if (argc != 1) { | 277 | if (checkarg(argc, "log_colors", EXPECTED_SAME_AS, 1) == false) { |
252 | sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc); | ||
253 | return false; | 278 | return false; |
254 | } | 279 | } |
255 | |||
256 | if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { | 280 | if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { |
257 | sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); | 281 | sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); |
258 | return false; | 282 | return false; |
@@ -263,8 +287,7 @@ static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { | |||
263 | } | 287 | } |
264 | 288 | ||
265 | static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { | 289 | static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { |
266 | if (argc != 1) { | 290 | if (checkarg(argc, "fullscreen", EXPECTED_SAME_AS, 1) == false) { |
267 | sway_log(L_ERROR, "Invalid fullscreen command (expected 1 arguments, got %d)", argc); | ||
268 | return false; | 291 | return false; |
269 | } | 292 | } |
270 | 293 | ||
@@ -277,8 +300,7 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { | |||
277 | } | 300 | } |
278 | 301 | ||
279 | static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { | 302 | static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { |
280 | if (argc != 1) { | 303 | if (checkarg(argc, "workspace", EXPECTED_SAME_AS, 1) == false) { |
281 | sway_log(L_ERROR, "Invalid workspace command (expected 1 arguments, got %d)", argc); | ||
282 | return false; | 304 | return false; |
283 | } | 305 | } |
284 | 306 | ||
@@ -292,7 +314,7 @@ static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { | |||
292 | } | 314 | } |
293 | 315 | ||
294 | /* Keep alphabetized */ | 316 | /* Keep alphabetized */ |
295 | struct cmd_handler handlers[] = { | 317 | static struct cmd_handler handlers[] = { |
296 | { "bindsym", cmd_bindsym }, | 318 | { "bindsym", cmd_bindsym }, |
297 | { "exec", cmd_exec }, | 319 | { "exec", cmd_exec }, |
298 | { "exec_always", cmd_exec_always }, | 320 | { "exec_always", cmd_exec_always }, |
@@ -309,7 +331,7 @@ struct cmd_handler handlers[] = { | |||
309 | { "workspace", cmd_workspace } | 331 | { "workspace", cmd_workspace } |
310 | }; | 332 | }; |
311 | 333 | ||
312 | char **split_directive(char *line, int *argc) { | 334 | static char **split_directive(char *line, int *argc) { |
313 | const char *delimiters = " "; | 335 | const char *delimiters = " "; |
314 | *argc = 0; | 336 | *argc = 0; |
315 | while (isspace(*line) && *line) ++line; | 337 | while (isspace(*line) && *line) ++line; |
@@ -361,13 +383,13 @@ char **split_directive(char *line, int *argc) { | |||
361 | return parts; | 383 | return parts; |
362 | } | 384 | } |
363 | 385 | ||
364 | int handler_compare(const void *_a, const void *_b) { | 386 | static int handler_compare(const void *_a, const void *_b) { |
365 | const struct cmd_handler *a = _a; | 387 | const struct cmd_handler *a = _a; |
366 | const struct cmd_handler *b = _b; | 388 | const struct cmd_handler *b = _b; |
367 | return strcasecmp(a->command, b->command); | 389 | return strcasecmp(a->command, b->command); |
368 | } | 390 | } |
369 | 391 | ||
370 | struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) { | 392 | static struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) { |
371 | struct cmd_handler d = { .command=line }; | 393 | struct cmd_handler d = { .command=line }; |
372 | struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare); | 394 | struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare); |
373 | return res; | 395 | return res; |