diff options
Diffstat (limited to 'sway/commands.c')
-rw-r--r-- | sway/commands.c | 79 |
1 files changed, 49 insertions, 30 deletions
diff --git a/sway/commands.c b/sway/commands.c index 971ff505..17c7d717 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -43,6 +43,8 @@ struct cmd_handler { | |||
43 | 43 | ||
44 | int sp_index = 0; | 44 | int sp_index = 0; |
45 | 45 | ||
46 | swayc_t *current_container = NULL; | ||
47 | |||
46 | // Returns error object, or NULL if check succeeds. | 48 | // Returns error object, or NULL if check succeeds. |
47 | struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { | 49 | struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { |
48 | struct cmd_results *error = NULL; | 50 | struct cmd_results *error = NULL; |
@@ -371,42 +373,37 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { | |||
371 | char *head = exec; | 373 | char *head = exec; |
372 | char *cmdlist; | 374 | char *cmdlist; |
373 | char *cmd; | 375 | char *cmd; |
374 | char *criteria __attribute__((unused)); | 376 | list_t *containers = NULL; |
375 | 377 | ||
376 | head = exec; | 378 | head = exec; |
377 | do { | 379 | do { |
378 | // Extract criteria (valid for this command list only). | 380 | // Extract criteria (valid for this command list only). |
379 | criteria = NULL; | ||
380 | if (*head == '[') { | 381 | if (*head == '[') { |
381 | ++head; | 382 | ++head; |
382 | criteria = argsep(&head, "]"); | 383 | char *criteria_string = argsep(&head, "]"); |
383 | if (head) { | 384 | if (head) { |
384 | ++head; | 385 | ++head; |
385 | // TODO handle criteria | 386 | list_t *tokens = create_list(); |
387 | char *error; | ||
388 | |||
389 | if ((error = extract_crit_tokens(tokens, criteria_string))) { | ||
390 | results = cmd_results_new(CMD_INVALID, criteria_string, | ||
391 | "Can't parse criteria string: %s", error); | ||
392 | free(error); | ||
393 | free(tokens); | ||
394 | goto cleanup; | ||
395 | } | ||
396 | containers = container_for(tokens); | ||
397 | |||
398 | free(tokens); | ||
386 | } else { | 399 | } else { |
387 | if (!results) { | 400 | if (!results) { |
388 | results = cmd_results_new(CMD_INVALID, criteria, "Unmatched ["); | 401 | results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched ["); |
389 | } | 402 | } |
390 | goto cleanup; | 403 | goto cleanup; |
391 | } | 404 | } |
392 | // Skip leading whitespace | 405 | // Skip leading whitespace |
393 | head += strspn(head, whitespace); | 406 | head += strspn(head, whitespace); |
394 | |||
395 | // TODO: it will yield unexpected results to execute commands | ||
396 | // (on any view) that where meant for certain views only. | ||
397 | if (!results) { | ||
398 | int len = strlen(criteria) + strlen(head) + 4; | ||
399 | char *tmp = malloc(len); | ||
400 | if (tmp) { | ||
401 | snprintf(tmp, len, "[%s] %s", criteria, head); | ||
402 | } else { | ||
403 | sway_log(L_DEBUG, "Unable to allocate criteria string for cmd result"); | ||
404 | } | ||
405 | results = cmd_results_new(CMD_INVALID, tmp, | ||
406 | "Can't handle criteria string: Refusing to execute command"); | ||
407 | free(tmp); | ||
408 | } | ||
409 | goto cleanup; | ||
410 | } | 407 | } |
411 | // Split command list | 408 | // Split command list |
412 | cmdlist = argsep(&head, ";"); | 409 | cmdlist = argsep(&head, ";"); |
@@ -450,21 +447,43 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { | |||
450 | free_argv(argc, argv); | 447 | free_argv(argc, argv); |
451 | goto cleanup; | 448 | goto cleanup; |
452 | } | 449 | } |
453 | struct cmd_results *res = handler->handle(argc-1, argv+1); | 450 | int i = 0; |
454 | if (res->status != CMD_SUCCESS) { | 451 | do { |
455 | free_argv(argc, argv); | 452 | if (!containers) { |
456 | if (results) { | 453 | current_container = get_focused_container(&root_container); |
457 | free_cmd_results(results); | 454 | } else if (containers->length == 0) { |
455 | break; | ||
456 | } else { | ||
457 | current_container = (swayc_t *)containers->items[i]; | ||
458 | } | 458 | } |
459 | results = res; | 459 | sway_log(L_INFO, "Running on container '%s'", current_container->name); |
460 | goto cleanup; | 460 | |
461 | } | 461 | struct cmd_results *res = handler->handle(argc-1, argv+1); |
462 | if (res->status != CMD_SUCCESS) { | ||
463 | free_argv(argc, argv); | ||
464 | if (results) { | ||
465 | free_cmd_results(results); | ||
466 | } | ||
467 | results = res; | ||
468 | goto cleanup; | ||
469 | } | ||
470 | free_cmd_results(res); | ||
471 | ++i; | ||
472 | } while(containers && i < containers->length); | ||
473 | |||
462 | free_argv(argc, argv); | 474 | free_argv(argc, argv); |
463 | free_cmd_results(res); | ||
464 | } while(cmdlist); | 475 | } while(cmdlist); |
476 | |||
477 | if (containers) { | ||
478 | list_free(containers); | ||
479 | containers = NULL; | ||
480 | } | ||
465 | } while(head); | 481 | } while(head); |
466 | cleanup: | 482 | cleanup: |
467 | free(exec); | 483 | free(exec); |
484 | if (containers) { | ||
485 | free(containers); | ||
486 | } | ||
468 | if (!results) { | 487 | if (!results) { |
469 | results = cmd_results_new(CMD_SUCCESS, NULL, NULL); | 488 | results = cmd_results_new(CMD_SUCCESS, NULL, NULL); |
470 | } | 489 | } |