From b98a91bf2ef4c8189dd9b483ed530e95f79ba53c Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@openwrt.org>
Date: Sat, 8 Jun 2013 22:59:51 +0200
Subject: [PATCH] add an option for printing output as a single line

---
 commands.c | 3 ++-
 commands.h | 1 +
 main.c     | 7 ++++++-
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/commands.c b/commands.c
index 54fcbb0..4dbdd17 100644
--- a/commands.c
+++ b/commands.c
@@ -11,6 +11,7 @@
 #include "commands.h"
 
 static struct blob_buf status;
+bool single_line = false;
 
 static void no_cb(struct qmi_dev *qmi, struct qmi_request *req, struct qmi_msg *msg)
 {
@@ -131,7 +132,7 @@ static void uqmi_print_result(struct blob_attr *data)
 	if (!blob_len(data))
 		return;
 
-	str = blobmsg_format_json_indent(blob_data(data), false, 0);
+	str = blobmsg_format_json_indent(blob_data(data), false, single_line ? -1 : 0);
 	if (!str)
 		return;
 
diff --git a/commands.h b/commands.h
index 44971c5..b54b34f 100644
--- a/commands.h
+++ b/commands.h
@@ -46,6 +46,7 @@ enum uqmi_command {
 };
 #undef __uqmi_command
 
+extern bool single_line;
 extern const struct uqmi_cmd_handler uqmi_cmd_handler[];
 void uqmi_add_command(char *arg, int longidx);
 bool uqmi_run_commands(struct qmi_dev *qmi);
diff --git a/main.c b/main.c
index f2f2e18..4b030e9 100644
--- a/main.c
+++ b/main.c
@@ -18,6 +18,7 @@ static const char *device;
 #define __uqmi_command(_name, _optname, _arg, _option) { #_optname, _arg##_argument, NULL, CMD_OPT(__UQMI_COMMAND_##_name) }
 static const struct option uqmi_getopt[] = {
 	__uqmi_commands,
+	{ "single", no_argument, NULL, 's' },
 	{ "device", required_argument, NULL, 'd' },
 	{ "keep-client-id", required_argument, NULL, 'k' },
 	{ "release-client-id", required_argument, NULL, 'r' },
@@ -29,6 +30,7 @@ static int usage(const char *progname)
 {
 	fprintf(stderr, "Usage: %s <options|actions>\n"
 		"Options:\n"
+		"  --single, -s:                     Print output as a single line (for scripts)\n"
 		"  --device=NAME, -d NAME:           Set device name to NAME (required)\n"
 		"  --keep-client-id <name>:          Keep Client ID for service <name>\n"
 		"                                    (implies --keep-client-id)\n"
@@ -84,7 +86,7 @@ int main(int argc, char **argv)
 	signal(SIGINT, handle_exit_signal);
 	signal(SIGTERM, handle_exit_signal);
 
-	while ((ch = getopt_long(argc, argv, "d:k:", uqmi_getopt, NULL)) != -1) {
+	while ((ch = getopt_long(argc, argv, "d:k:s", uqmi_getopt, NULL)) != -1) {
 		int cmd_opt = CMD_OPT(ch);
 
 		if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
@@ -102,6 +104,9 @@ int main(int argc, char **argv)
 		case 'd':
 			device = optarg;
 			break;
+		case 's':
+			single_line = true;
+			break;
 		default:
 			return usage(argv[0]);
 		}
-- 
2.25.1