aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
diff options
context:
space:
mode:
authorLibravatar M Stoeckl <code@mstoeckl.com>2019-01-10 18:27:21 -0500
committerLibravatar M Stoeckl <code@mstoeckl.com>2019-01-14 08:05:29 -0500
commit2a684cad5fc8e12a8e47a7fd00e2b7c66b43afb0 (patch)
tree56332b9c150459beb5aef94605372ef179ec8854 /sway/commands.c
parentRemove 'input' field of IPC command return json (diff)
downloadsway-2a684cad5fc8e12a8e47a7fd00e2b7c66b43afb0.tar.gz
sway-2a684cad5fc8e12a8e47a7fd00e2b7c66b43afb0.tar.zst
sway-2a684cad5fc8e12a8e47a7fd00e2b7c66b43afb0.zip
Remove now-unused "input" argument of cmd_results_new
Patch tested by compiling with `__attribute__ ((format (printf, 2, 3)))` applied to `cmd_results_new`. String usage constants have been converted from pointers to arrays when encountered. General handler format strings were sometimes modified to include the old input string, especially for unknown command errors.
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c59
1 files changed, 28 insertions, 31 deletions
diff --git a/sway/commands.c b/sway/commands.c
index 9fae6a35..6f786035 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -36,7 +36,7 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type
36 } 36 }
37 } 37 }
38 return error_name ? 38 return error_name ?
39 cmd_results_new(CMD_INVALID, name, "Invalid %s command " 39 cmd_results_new(CMD_INVALID, "Invalid %s command "
40 "(expected %s%d argument%s, got %d)", 40 "(expected %s%d argument%s, got %d)",
41 name, error_name, val, val != 1 ? "s" : "", argc) 41 name, error_name, val, val != 1 ? "s" : "", argc)
42 : NULL; 42 : NULL;
@@ -228,8 +228,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
228 char *error = NULL; 228 char *error = NULL;
229 struct criteria *criteria = criteria_parse(head, &error); 229 struct criteria *criteria = criteria_parse(head, &error);
230 if (!criteria) { 230 if (!criteria) {
231 list_add(res_list, cmd_results_new(CMD_INVALID, head, 231 list_add(res_list, cmd_results_new(CMD_INVALID, "%s", error));
232 "%s", error));
233 free(error); 232 free(error);
234 goto cleanup; 233 goto cleanup;
235 } 234 }
@@ -265,8 +264,8 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
265 } 264 }
266 struct cmd_handler *handler = find_handler(argv[0], NULL, 0); 265 struct cmd_handler *handler = find_handler(argv[0], NULL, 0);
267 if (!handler) { 266 if (!handler) {
268 list_add(res_list, cmd_results_new(CMD_INVALID, cmd, 267 list_add(res_list, cmd_results_new(CMD_INVALID,
269 "Unknown/invalid command")); 268 "Unknown/invalid command '%s'", argv[0]));
270 free_argv(argc, argv); 269 free_argv(argc, argv);
271 goto cleanup; 270 goto cleanup;
272 } 271 }
@@ -323,20 +322,20 @@ struct cmd_results *config_command(char *exec, char **new_block) {
323 322
324 // Check for empty lines 323 // Check for empty lines
325 if (!argc) { 324 if (!argc) {
326 results = cmd_results_new(CMD_SUCCESS, NULL, NULL); 325 results = cmd_results_new(CMD_SUCCESS, NULL);
327 goto cleanup; 326 goto cleanup;
328 } 327 }
329 328
330 // Check for the start of a block 329 // Check for the start of a block
331 if (argc > 1 && strcmp(argv[argc - 1], "{") == 0) { 330 if (argc > 1 && strcmp(argv[argc - 1], "{") == 0) {
332 *new_block = join_args(argv, argc - 1); 331 *new_block = join_args(argv, argc - 1);
333 results = cmd_results_new(CMD_BLOCK, NULL, NULL); 332 results = cmd_results_new(CMD_BLOCK, NULL);
334 goto cleanup; 333 goto cleanup;
335 } 334 }
336 335
337 // Check for the end of a block 336 // Check for the end of a block
338 if (strcmp(argv[argc - 1], "}") == 0) { 337 if (strcmp(argv[argc - 1], "}") == 0) {
339 results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); 338 results = cmd_results_new(CMD_BLOCK_END, NULL);
340 goto cleanup; 339 goto cleanup;
341 } 340 }
342 341
@@ -348,7 +347,7 @@ struct cmd_results *config_command(char *exec, char **new_block) {
348 argv = split_args(temp, &argc); 347 argv = split_args(temp, &argc);
349 free(temp); 348 free(temp);
350 if (!argc) { 349 if (!argc) {
351 results = cmd_results_new(CMD_SUCCESS, NULL, NULL); 350 results = cmd_results_new(CMD_SUCCESS, NULL);
352 goto cleanup; 351 goto cleanup;
353 } 352 }
354 } 353 }
@@ -357,11 +356,10 @@ struct cmd_results *config_command(char *exec, char **new_block) {
357 wlr_log(WLR_INFO, "Config command: %s", exec); 356 wlr_log(WLR_INFO, "Config command: %s", exec);
358 struct cmd_handler *handler = find_handler(argv[0], NULL, 0); 357 struct cmd_handler *handler = find_handler(argv[0], NULL, 0);
359 if (!handler || !handler->handle) { 358 if (!handler || !handler->handle) {
360 char *input = argv[0] ? argv[0] : "(empty)"; 359 const char *error = handler
361 char *error = handler 360 ? "Command '%s' is shimmed, but unimplemented"
362 ? "This command is shimmed, but unimplemented" 361 : "Unknown/invalid command '%s'";
363 : "Unknown/invalid command"; 362 results = cmd_results_new(CMD_INVALID, error, argv[0]);
364 results = cmd_results_new(CMD_INVALID, input, error);
365 goto cleanup; 363 goto cleanup;
366 } 364 }
367 365
@@ -410,14 +408,14 @@ struct cmd_results *config_subcommand(char **argv, int argc,
410 struct cmd_handler *handler = find_handler(argv[0], handlers, 408 struct cmd_handler *handler = find_handler(argv[0], handlers,
411 handlers_size); 409 handlers_size);
412 if (!handler) { 410 if (!handler) {
413 char *input = argv[0] ? argv[0] : "(empty)"; 411 return cmd_results_new(CMD_INVALID,
414 return cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); 412 "Unknown/invalid command '%s'", argv[0]);
415 } 413 }
416 if (handler->handle) { 414 if (handler->handle) {
417 return handler->handle(argc - 1, argv + 1); 415 return handler->handle(argc - 1, argv + 1);
418 } 416 }
419 return cmd_results_new(CMD_INVALID, argv[0], 417 return cmd_results_new(CMD_INVALID,
420 "This command is shimmed, but unimplemented"); 418 "The command '%s' is shimmed, but unimplemented", argv[0]);
421} 419}
422 420
423struct cmd_results *config_commands_command(char *exec) { 421struct cmd_results *config_commands_command(char *exec) {
@@ -425,7 +423,7 @@ struct cmd_results *config_commands_command(char *exec) {
425 int argc; 423 int argc;
426 char **argv = split_args(exec, &argc); 424 char **argv = split_args(exec, &argc);
427 if (!argc) { 425 if (!argc) {
428 results = cmd_results_new(CMD_SUCCESS, NULL, NULL); 426 results = cmd_results_new(CMD_SUCCESS, NULL);
429 goto cleanup; 427 goto cleanup;
430 } 428 }
431 429
@@ -433,13 +431,14 @@ struct cmd_results *config_commands_command(char *exec) {
433 char *cmd = argv[0]; 431 char *cmd = argv[0];
434 432
435 if (strcmp(cmd, "}") == 0) { 433 if (strcmp(cmd, "}") == 0) {
436 results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); 434 results = cmd_results_new(CMD_BLOCK_END, NULL);
437 goto cleanup; 435 goto cleanup;
438 } 436 }
439 437
440 struct cmd_handler *handler = find_handler(cmd, NULL, 0); 438 struct cmd_handler *handler = find_handler(cmd, NULL, 0);
441 if (!handler && strcmp(cmd, "*") != 0) { 439 if (!handler && strcmp(cmd, "*") != 0) {
442 results = cmd_results_new(CMD_INVALID, cmd, "Unknown/invalid command"); 440 results = cmd_results_new(CMD_INVALID,
441 "Unknown/invalid command '%s'", cmd);
443 goto cleanup; 442 goto cleanup;
444 } 443 }
445 444
@@ -464,7 +463,7 @@ struct cmd_results *config_commands_command(char *exec) {
464 } 463 }
465 } 464 }
466 if (j == sizeof(context_names) / sizeof(context_names[0])) { 465 if (j == sizeof(context_names) / sizeof(context_names[0])) {
467 results = cmd_results_new(CMD_INVALID, cmd, 466 results = cmd_results_new(CMD_INVALID,
468 "Invalid command context %s", argv[i]); 467 "Invalid command context %s", argv[i]);
469 goto cleanup; 468 goto cleanup;
470 } 469 }
@@ -482,7 +481,7 @@ struct cmd_results *config_commands_command(char *exec) {
482 if (!policy) { 481 if (!policy) {
483 policy = alloc_command_policy(cmd); 482 policy = alloc_command_policy(cmd);
484 if (!sway_assert(policy, "Unable to allocate security policy")) { 483 if (!sway_assert(policy, "Unable to allocate security policy")) {
485 results = cmd_results_new(CMD_INVALID, cmd, 484 results = cmd_results_new(CMD_INVALID,
486 "Unable to allocate memory"); 485 "Unable to allocate memory");
487 goto cleanup; 486 goto cleanup;
488 } 487 }
@@ -493,7 +492,7 @@ struct cmd_results *config_commands_command(char *exec) {
493 wlr_log(WLR_INFO, "Set command policy for %s to %d", 492 wlr_log(WLR_INFO, "Set command policy for %s to %d",
494 policy->command, policy->context); 493 policy->command, policy->context);
495 494
496 results = cmd_results_new(CMD_SUCCESS, NULL, NULL); 495 results = cmd_results_new(CMD_SUCCESS, NULL);
497 496
498cleanup: 497cleanup:
499 free_argv(argc, argv); 498 free_argv(argc, argv);
@@ -501,14 +500,13 @@ cleanup:
501} 500}
502 501
503struct cmd_results *cmd_results_new(enum cmd_status status, 502struct cmd_results *cmd_results_new(enum cmd_status status,
504 const char *input, const char *format, ...) { 503 const char *format, ...) {
505 struct cmd_results *results = malloc(sizeof(struct cmd_results)); 504 struct cmd_results *results = malloc(sizeof(struct cmd_results));
506 if (!results) { 505 if (!results) {
507 wlr_log(WLR_ERROR, "Unable to allocate command results"); 506 wlr_log(WLR_ERROR, "Unable to allocate command results");
508 return NULL; 507 return NULL;
509 } 508 }
510 results->status = status; 509 results->status = status;
511 // NOTE: `input` argument is unused, remove
512 if (format) { 510 if (format) {
513 char *error = malloc(256); 511 char *error = malloc(256);
514 va_list args; 512 va_list args;
@@ -557,20 +555,19 @@ char *cmd_results_to_json(list_t *res_list) {
557 * 555 *
558 * return error object, or NULL if color is valid. 556 * return error object, or NULL if color is valid.
559 */ 557 */
560struct cmd_results *add_color(const char *name, 558struct cmd_results *add_color(char *buffer, const char *color) {
561 char *buffer, const char *color) {
562 int len = strlen(color); 559 int len = strlen(color);
563 if (len != 7 && len != 9) { 560 if (len != 7 && len != 9) {
564 return cmd_results_new(CMD_INVALID, name, 561 return cmd_results_new(CMD_INVALID,
565 "Invalid color definition %s", color); 562 "Invalid color definition %s", color);
566 } 563 }
567 if (color[0] != '#') { 564 if (color[0] != '#') {
568 return cmd_results_new(CMD_INVALID, name, 565 return cmd_results_new(CMD_INVALID,
569 "Invalid color definition %s", color); 566 "Invalid color definition %s", color);
570 } 567 }
571 for (int i = 1; i < len; ++i) { 568 for (int i = 1; i < len; ++i) {
572 if (!isxdigit(color[i])) { 569 if (!isxdigit(color[i])) {
573 return cmd_results_new(CMD_INVALID, name, 570 return cmd_results_new(CMD_INVALID,
574 "Invalid color definition %s", color); 571 "Invalid color definition %s", color);
575 } 572 }
576 } 573 }