From patchwork Fri Apr 14 18:37:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Prindeville X-Patchwork-Id: 1769165 X-Patchwork-Delegate: lynxis@fe80.eu Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=nDrJiTvy; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Pylcb4XHrz1yZr for ; Sat, 15 Apr 2023 04:40:15 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=WpMistfvbhj2ICz3Zk5TcZzvHzM8eHfeJ264wBaM904=; b=nDrJiTvyLOODEz moLVRQ5AuMJcvAUeDG0v+nwzZHh0t6W1yvtoJ3V6jM9bsfP4utaDa3pQqJMIqoHajuJ12gR/k16kr yWFJx7WqizlPpFL3B7CECXM/8x0KPmt1xWipMi/LbUUUDxWJFl5vwQlJZQTXh1+QHSQtmtxHPyHAH M00EWgvhg/lIp1AJYkguO2A/iBptVCoE0JT7vJzsTFO704H9w8L1hXhgnfC15PcP0TyuZAcWAtbV4 JHEy/TnhBksmpF+J8oxONPSkdjl+uD3uKETVyc1nBB2NZtK0Z3dDSd1vucH+91f3eiFnqzEHL8FmC pdvrDTpkvgm/2md1hRcg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnOIT-00ANmY-2w; Fri, 14 Apr 2023 18:37:21 +0000 Received: from mail.redfish-solutions.com ([24.116.100.90]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnOIM-00ANiM-2b for openwrt-devel@lists.openwrt.org; Fri, 14 Apr 2023 18:37:17 +0000 Received: from ubuntu22.redfish-solutions.com (ubuntu22.redfish-solutions.com [192.168.8.33]) (authenticated bits=0) by mail.redfish-solutions.com (8.17.1/8.16.1) with ESMTPSA id 33EIb5I2275008 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 14 Apr 2023 12:37:05 -0600 From: "Philip Prindeville" To: openwrt-devel@lists.openwrt.org Cc: Philip Prindeville Subject: [PATCH 1/2] jshn.sh: Add pretty-printing to json_dump Date: Fri, 14 Apr 2023 12:37:05 -0600 Message-Id: <20230414183705.1531142-1-philipp@redfish-solutions.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.3 on 192.168.8.3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_113714_961945_071C6A5D X-CRM114-Status: UNSURE ( 9.80 ) X-CRM114-Notice: Please train this message. X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Philip Prindeville If a JSON file might be read by a human, say for debugging, it could be useful to pretty-print it. We do this in places by calling "json_dump -i" but it shouldn't be necessary to know the arguments to [...] Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Philip Prindeville If a JSON file might be read by a human, say for debugging, it could be useful to pretty-print it. We do this in places by calling "json_dump -i" but it shouldn't be necessary to know the arguments to "jshn" (and indeed, that's not portable if we retool the underlying implementation). Conversely output that's ephemeral doesn't need to be pretty (say being piped as input to another command). Signed-off-by: Philip Prindeville --- sh/jshn.sh | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sh/jshn.sh b/sh/jshn.sh index c97369230a93d144fbba06b5a8fa79c9acc41238..1d3055711820e5a21ea59cae49e7bf3f56b626d8 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -199,6 +199,16 @@ json_add_fields() { # functions read access to json variables +json_compact() { + JSON_NONEWLINE=1 + JSON_INDENT= +} + +json_pretty() { + JSON_NONEWLINE= + JSON_INDENT=1 +} + json_load() { eval "`jshn -r "$1"`" } @@ -208,7 +218,7 @@ json_load_file() { } json_dump() { - jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w + jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} ${JSON_NONEWLINE:+-n} ${JSON_INDENT:+-i} -w } json_get_type() { From patchwork Fri Apr 14 18:37:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Prindeville X-Patchwork-Id: 1769166 X-Patchwork-Delegate: lynxis@fe80.eu Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=YWL51Mrx; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Pylcb4bc8z23rS for ; Sat, 15 Apr 2023 04:40:15 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=22yiHY5b1eb7H+RGDacKdNWmvNcdEYTIlpoliDTgG0Y=; b=YWL51MrxpwwPSJ 9Ur8e17mkzj6R2k7og1s/a3YQx6ta8koWNgG3SpT/+d6dNqnqCPxxH+rA89Aq1LsUUcV0q1FTwLzL DDcOuGmU/A6jq7/5CqwD9BC0Qtb2/jAYWwUA7KNxbl9wa22Cia6K7HSQr4n58JSWpj9R+gx7ziUN6 zpVxgcm93gA0ggIHYxtOyNPXipNIFMk+spX0EyxhZ5frGFg79A6rBOe6rvhjqwJkRK1vzW59WdLNz KGvijGgaT/8H2SaGuM7eBJ8VrSDR/YW/U5qnJjKXyqYOGPYn+6Ee12fF4AquAgrIhp1ne3w+5C1ue +Eo/Mf7oLGAWI6/JlmrQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnOIQ-00ANlQ-2x; Fri, 14 Apr 2023 18:37:18 +0000 Received: from mail.redfish-solutions.com ([24.116.100.90]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pnOIM-00ANiR-2Q for openwrt-devel@lists.openwrt.org; Fri, 14 Apr 2023 18:37:16 +0000 Received: from ubuntu22.redfish-solutions.com (ubuntu22.redfish-solutions.com [192.168.8.33]) (authenticated bits=0) by mail.redfish-solutions.com (8.17.1/8.16.1) with ESMTPSA id 33EIb6cX275013 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 14 Apr 2023 12:37:06 -0600 From: "Philip Prindeville" To: openwrt-devel@lists.openwrt.org Cc: Philip Prindeville Subject: [PATCH 2/2] blobmsg: Don't do at run-time what can be done at compile-time Date: Fri, 14 Apr 2023 12:37:06 -0600 Message-Id: <20230414183706.1531155-1-philipp@redfish-solutions.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.3 on 192.168.8.3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_113714_881408_388FCE24 X-CRM114-Status: UNSURE ( 9.17 ) X-CRM114-Notice: Please train this message. X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Philip Prindeville Repeatedly calling a run-time function like strlen() on an invariant value is inefficient, especially if that value can be computed once (at initialization) or better yet, computed at compile-time. Content analysis details: (0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Philip Prindeville Repeatedly calling a run-time function like strlen() on an invariant value is inefficient, especially if that value can be computed once (at initialization) or better yet, computed at compile-time. Signed-off-by: Philip Prindeville --- blobmsg_json.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blobmsg_json.c b/blobmsg_json.c index dce81e991ef7b83ac4906bca6f875369d3057f36..ec8b482c30c96a355aca58651632bc509a16bedf 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -151,15 +151,15 @@ static bool blobmsg_puts(struct strbuf *s, const char *c, int len) static void add_separator(struct strbuf *s) { - const char *indent_chars = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; + const char indent_chars[] = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; size_t len; if (!s->indent) return; len = s->indent_level + 1; - if (len > strlen(indent_chars)) - len = strlen(indent_chars); + if (len > sizeof(indent_chars) - 1) + len = sizeof(indent_chars) - 1; blobmsg_puts(s, indent_chars, len); }