From patchwork Tue Mar 30 04:01:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Kicinski X-Patchwork-Id: 12171579 X-Patchwork-Delegate: mkubecek+ethtool@suse.cz Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BD4CC433E0 for ; Tue, 30 Mar 2021 04:02:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C482461959 for ; Tue, 30 Mar 2021 04:02:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231219AbhC3ECM (ORCPT ); Tue, 30 Mar 2021 00:02:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:46768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231145AbhC3EB5 (ORCPT ); Tue, 30 Mar 2021 00:01:57 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id F087E6196C; Tue, 30 Mar 2021 04:01:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1617076917; bh=n4sEnSDmhYKsaExK7os5FLh2G6mjLQTpU3+v8OcDH4o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=luZbY140k9rPB9hgobmXyzOaDrR/L1AVWyB2bNvQHzNwHPQMaMOLXMcSeEN/xEN/+ PdS0uS4nz3FnxXhOkEqu7iHagS2SHgYo4AciEB8Ll0DRB+QEO5XHIbIOttfXIdKB/p Z38UxnAmefVXdcyDgCeM/kIFc65IXku/SNuS2JhSVcTCfi8p9KklGoZ0JuLueu1eRE jQpahMDnOSjNWSsKXyZPY6iyeTSvpFiSBFOcwL62EIyZPIpOQDeuKzEzKsLVsHkAW8 Vsgdj90/YtCbX8Gbaqik9zV1mpbnzwsO/sMDF6I/4RSRKTKqkX1JBtaGQRaUA5sXWd PNxBWv7O5h2Zw== From: Jakub Kicinski To: mkubecek@suse.cz, andrew@lunn.ch Cc: netdev@vger.kernel.org, ecree.xilinx@gmail.com, Jakub Kicinski Subject: [RFC ethtool-next 2/3] json: simplify array print API Date: Mon, 29 Mar 2021 21:01:53 -0700 Message-Id: <20210330040154.1218028-2-kuba@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210330040154.1218028-1-kuba@kernel.org> References: <20210330040154.1218028-1-kuba@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: mkubecek+ethtool@suse.cz X-Patchwork-State: RFC In ethtool when we print an array we usually have a label (non-JSON) and a key (JSON), because arrays are most often printed on a single line (unlike iproute2 where the output has multiple params on a line to cater to multi-interface dumps well). Build this into the json array API. Signed-off-by: Jakub Kicinski --- json_print.c | 20 ++++++++++---------- json_print.h | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/json_print.c b/json_print.c index 56d5b4337e49..4f62767bdbc9 100644 --- a/json_print.c +++ b/json_print.c @@ -73,15 +73,15 @@ void close_json_object(void) /* * Start json array or string array using * the provided string as json key (if not null) - * or as array delimiter in non-json context. + * or array delimiter in non-json context. */ -void open_json_array(enum output_type type, const char *str) +void open_json_array(const char *key, const char *str) { - if (_IS_JSON_CONTEXT(type)) { - if (str) - jsonw_name(_jw, str); + if (is_json_context()) { + if (key) + jsonw_name(_jw, key); jsonw_start_array(_jw); - } else if (_IS_FP_CONTEXT(type)) { + } else { printf("%s", str); } } @@ -89,12 +89,12 @@ void open_json_array(enum output_type type, const char *str) /* * End json array or string array */ -void close_json_array(enum output_type type, const char *str) +void close_json_array(const char *delim) { - if (_IS_JSON_CONTEXT(type)) + if (is_json_context()) jsonw_end_array(_jw); - else if (_IS_FP_CONTEXT(type)) - printf("%s", str); + else + printf("%s", delim); } /* diff --git a/json_print.h b/json_print.h index cc0c2ea19b59..df15314bafe2 100644 --- a/json_print.h +++ b/json_print.h @@ -37,8 +37,8 @@ void fflush_fp(void); void open_json_object(const char *str); void close_json_object(void); -void open_json_array(enum output_type type, const char *delim); -void close_json_array(enum output_type type, const char *delim); +void open_json_array(const char *key, const char *str); +void close_json_array(const char *delim); void print_nl(void);