From c6561f88e4583631b3bc9871b29c1ffbac0be63a Mon Sep 17 00:00:00 2001 From: Cole Mickens Date: Sat, 23 Jan 2016 20:25:41 -0800 Subject: [PATCH 1/3] add pretty output to swaymsg eventually this will pass a flag to json-c to ignore slashes --- swaymsg/CMakeLists.txt | 5 +++++ swaymsg/main.c | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/swaymsg/CMakeLists.txt b/swaymsg/CMakeLists.txt index 06939991d..53f251c56 100644 --- a/swaymsg/CMakeLists.txt +++ b/swaymsg/CMakeLists.txt @@ -1,9 +1,14 @@ +include_directories( + ${JSONC_INCLUDE_DIRS} +) + add_executable(swaymsg main.c ) target_link_libraries(swaymsg sway-common + ${JSONC_LIBRARIES} ) install( diff --git a/swaymsg/main.c b/swaymsg/main.c index 22572b6fc..ed144f388 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "stringop.h" #include "ipc-client.h" #include "readline.h" @@ -17,6 +19,7 @@ void sway_terminate(void) { int main(int argc, char **argv) { static int quiet = 0; + static int pretty = 0; char *socket_path = NULL; char *cmdtype = NULL; @@ -35,6 +38,7 @@ int main(int argc, char **argv) { "Usage: swaymsg [options] [message]\n" "\n" " -h, --help Show help message and quit.\n" + " -p, --pretty Decode the JSON response and format it.\n" " -q, --quiet Be quiet.\n" " -v, --version Show the version number and quit.\n" " -s, --socket Use the specified socket.\n" @@ -43,7 +47,7 @@ int main(int argc, char **argv) { int c; while (1) { int option_index = 0; - c = getopt_long(argc, argv, "hqvs:t:", long_options, &option_index); + c = getopt_long(argc, argv, "hpqvs:t:", long_options, &option_index); if (c == -1) { break; } @@ -51,6 +55,9 @@ int main(int argc, char **argv) { case 'q': // Quiet quiet = 1; break; + case 'p': + pretty = 1; + break; case 's': // Socket socket_path = strdup(optarg); break; @@ -112,7 +119,20 @@ int main(int argc, char **argv) { int socketfd = ipc_open_socket(socket_path); uint32_t len = strlen(command); char *resp = ipc_single_command(socketfd, type, command, &len); - if (!quiet) { + + if (pretty) { + struct printbuf *pb; + if(!(pb = printbuf_new())) { + return -1; + } + printbuf_memappend(pb, resp, sizeof(char)*strlen(resp)); + struct json_object *obj = json_tokener_parse(pb->buf); + + const char *resp_json = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY); + printf("%s\n", resp_json); + free((char*)resp_json); + printbuf_free(pb); + } else if (!quiet) { printf("%s\n", resp); } close(socketfd); From 2b5ddfa35ee414b59bc7835d9ff925530d67c611 Mon Sep 17 00:00:00 2001 From: Cole Mickens Date: Sun, 24 Jan 2016 16:07:56 -0800 Subject: [PATCH 2/3] temp fix to unescape fwd slashes in pretty mode this can be removed whenever json-c has a release which includes the flag for not escaping these --- swaymsg/main.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/swaymsg/main.c b/swaymsg/main.c index ed144f388..42b256b20 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -17,6 +17,21 @@ void sway_terminate(void) { exit(EXIT_FAILURE); } +void scrub_escaped_forward_slash(const char *line) { + char *sp; + const char *search = "\\/"; + const char *replace = "/"; + int search_len = strlen(search); + int replace_len = strlen(replace); + + // this is only safe because replace_len < search_len + while ((sp = strstr(line, search)) != NULL) { + int tail_len = strlen(sp+search_len); + memmove(sp+replace_len, sp+search_len, tail_len+1); + memcpy(sp, replace, replace_len); + } +}; + int main(int argc, char **argv) { static int quiet = 0; static int pretty = 0; @@ -129,6 +144,12 @@ int main(int argc, char **argv) { struct json_object *obj = json_tokener_parse(pb->buf); const char *resp_json = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY); + + // Temporary workaround until json-c has a new release + // at which point json_object_to_json_string_ext has a flag that + // prevents the need for the following line. + scrub_escaped_forward_slash(resp_json); + printf("%s\n", resp_json); free((char*)resp_json); printbuf_free(pb); From ae044d29cd7da1b96298b7c1682b54894e033990 Mon Sep 17 00:00:00 2001 From: Cole Mickens Date: Sun, 24 Jan 2016 16:15:19 -0800 Subject: [PATCH 3/3] unescape forward slashes in non-pretty mode --- swaymsg/main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/swaymsg/main.c b/swaymsg/main.c index 42b256b20..46a3edd5e 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -144,7 +144,6 @@ int main(int argc, char **argv) { struct json_object *obj = json_tokener_parse(pb->buf); const char *resp_json = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY); - // Temporary workaround until json-c has a new release // at which point json_object_to_json_string_ext has a flag that // prevents the need for the following line. @@ -154,6 +153,10 @@ int main(int argc, char **argv) { free((char*)resp_json); printbuf_free(pb); } else if (!quiet) { + // Temporary workaround until json-c has a new release + // at which point json_object_to_json_string_ext has a flag that + // prevents the need for the following line. + scrub_escaped_forward_slash(resp); printf("%s\n", resp); } close(socketfd);