From c8e4ca355d80d58160aaeebc8a94b7b67b9143cb Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Fri, 3 May 2019 12:49:33 -0400 Subject: swaymsg: fix quiet error reporting This makes it so swaymsg still returns the correct successful or failed error code when in quiet mode --- swaymsg/main.c | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) (limited to 'swaymsg') diff --git a/swaymsg/main.c b/swaymsg/main.c index 518993af..7ee553e7 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -397,6 +397,9 @@ int main(int argc, char **argv) { if (!socket_path) { socket_path = get_socketpath(); if (!socket_path) { + if (quiet) { + exit(EXIT_FAILURE); + } sway_abort("Unable to retrieve socket path"); } } @@ -430,13 +433,18 @@ int main(int argc, char **argv) { } else if (strcasecmp(cmdtype, "subscribe") == 0) { type = IPC_SUBSCRIBE; } else { + if (quiet) { + exit(EXIT_FAILURE); + } sway_abort("Unknown message type %s", cmdtype); } free(cmdtype); if (monitor && type != IPC_SUBSCRIBE) { - sway_log(SWAY_ERROR, "Monitor can only be used with -t SUBSCRIBE"); + if (!quiet) { + sway_log(SWAY_ERROR, "Monitor can only be used with -t SUBSCRIBE"); + } free(socket_path); return 1; } @@ -454,29 +462,29 @@ int main(int argc, char **argv) { ipc_set_recv_timeout(socketfd, timeout); uint32_t len = strlen(command); char *resp = ipc_single_command(socketfd, type, command, &len); - if (!quiet) { - // pretty print the json - json_object *obj = json_tokener_parse(resp); - if (obj == NULL) { + // pretty print the json + json_object *obj = json_tokener_parse(resp); + if (obj == NULL) { + if (!quiet) { fprintf(stderr, "ERROR: Could not parse json response from ipc. " "This is a bug in sway."); printf("%s\n", resp); + } + ret = 1; + } else { + if (!success(obj, true)) { ret = 1; - } else { - if (!success(obj, true)) { - ret = 1; - } - if (type != IPC_SUBSCRIBE || ret != 0) { - if (raw) { - printf("%s\n", json_object_to_json_string_ext(obj, - JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); - } else { - pretty_print(type, obj); - } + } + if (!quiet && (type != IPC_SUBSCRIBE || ret != 0)) { + if (raw) { + printf("%s\n", json_object_to_json_string_ext(obj, + JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); + } else { + pretty_print(type, obj); } - json_object_put(obj); } + json_object_put(obj); } free(command); free(resp); @@ -495,10 +503,14 @@ int main(int argc, char **argv) { json_object *obj = json_tokener_parse(reply->payload); if (obj == NULL) { - fprintf(stderr, "ERROR: Could not parse json response from ipc" - ". This is a bug in sway."); - ret = 1; + if (!quiet) { + fprintf(stderr, "ERROR: Could not parse json response from" + " ipc. This is a bug in sway."); + ret = 1; + } break; + } else if (quiet) { + json_object_put(obj); } else { if (raw) { printf("%s\n", json_object_to_json_string(obj)); -- cgit v1.2.3