Skip to content

Commit c8e4ca3

Browse files
RedSoxFanddevault
authored andcommitted
swaymsg: fix quiet error reporting
This makes it so swaymsg still returns the correct successful or failed error code when in quiet mode
1 parent c118618 commit c8e4ca3

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

swaymsg/main.c

+32-20
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ int main(int argc, char **argv) {
397397
if (!socket_path) {
398398
socket_path = get_socketpath();
399399
if (!socket_path) {
400+
if (quiet) {
401+
exit(EXIT_FAILURE);
402+
}
400403
sway_abort("Unable to retrieve socket path");
401404
}
402405
}
@@ -430,13 +433,18 @@ int main(int argc, char **argv) {
430433
} else if (strcasecmp(cmdtype, "subscribe") == 0) {
431434
type = IPC_SUBSCRIBE;
432435
} else {
436+
if (quiet) {
437+
exit(EXIT_FAILURE);
438+
}
433439
sway_abort("Unknown message type %s", cmdtype);
434440
}
435441

436442
free(cmdtype);
437443

438444
if (monitor && type != IPC_SUBSCRIBE) {
439-
sway_log(SWAY_ERROR, "Monitor can only be used with -t SUBSCRIBE");
445+
if (!quiet) {
446+
sway_log(SWAY_ERROR, "Monitor can only be used with -t SUBSCRIBE");
447+
}
440448
free(socket_path);
441449
return 1;
442450
}
@@ -454,29 +462,29 @@ int main(int argc, char **argv) {
454462
ipc_set_recv_timeout(socketfd, timeout);
455463
uint32_t len = strlen(command);
456464
char *resp = ipc_single_command(socketfd, type, command, &len);
457-
if (!quiet) {
458-
// pretty print the json
459-
json_object *obj = json_tokener_parse(resp);
460465

461-
if (obj == NULL) {
466+
// pretty print the json
467+
json_object *obj = json_tokener_parse(resp);
468+
if (obj == NULL) {
469+
if (!quiet) {
462470
fprintf(stderr, "ERROR: Could not parse json response from ipc. "
463471
"This is a bug in sway.");
464472
printf("%s\n", resp);
473+
}
474+
ret = 1;
475+
} else {
476+
if (!success(obj, true)) {
465477
ret = 1;
466-
} else {
467-
if (!success(obj, true)) {
468-
ret = 1;
469-
}
470-
if (type != IPC_SUBSCRIBE || ret != 0) {
471-
if (raw) {
472-
printf("%s\n", json_object_to_json_string_ext(obj,
473-
JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));
474-
} else {
475-
pretty_print(type, obj);
476-
}
478+
}
479+
if (!quiet && (type != IPC_SUBSCRIBE || ret != 0)) {
480+
if (raw) {
481+
printf("%s\n", json_object_to_json_string_ext(obj,
482+
JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED));
483+
} else {
484+
pretty_print(type, obj);
477485
}
478-
json_object_put(obj);
479486
}
487+
json_object_put(obj);
480488
}
481489
free(command);
482490
free(resp);
@@ -495,10 +503,14 @@ int main(int argc, char **argv) {
495503

496504
json_object *obj = json_tokener_parse(reply->payload);
497505
if (obj == NULL) {
498-
fprintf(stderr, "ERROR: Could not parse json response from ipc"
499-
". This is a bug in sway.");
500-
ret = 1;
506+
if (!quiet) {
507+
fprintf(stderr, "ERROR: Could not parse json response from"
508+
" ipc. This is a bug in sway.");
509+
ret = 1;
510+
}
501511
break;
512+
} else if (quiet) {
513+
json_object_put(obj);
502514
} else {
503515
if (raw) {
504516
printf("%s\n", json_object_to_json_string(obj));

0 commit comments

Comments
 (0)