From patchwork Wed Dec 30 07:21:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Leonardo_M=C3=B6rlein?= X-Patchwork-Id: 1421322 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=irrelefant.net Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=JvZHFqsj; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=ZF1FgnNr; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4D5N875Hs1z9sVl for ; Wed, 30 Dec 2020 18:24:22 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject: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=EcZXdoSycgQ+WytXtYji05UXXdpLixUHfAUoRFrA5hs=; b=JvZHFqsj916d/BZoRYP2B2t8X6 fmE8liZtDjLzFVLd3hy1Dw235C5uHclZAVBxqWKHictUpdb8NAoSuBL3GGRe6nFotcUX0FRp0fRvb maXIyjHod9xIraDiHPEt5ItyasEB/gZ6igzxg15byq+0cQJa+wXRNMTCru10zXfASCiXRXopH05Cu pJS0gRgUtmHDH1rZ2VnJlKgJGEXjKvHAloZ3PwtQVqRNTOt8Uc+NX3uQw0nM/URapvBzKReqkKXlT dPo1YFx+av+hH5Ej2fmBER9LR4allvQasjzNIu2MmkzWQPp4airP/JpZ9i6drAixfw7Mc9JmPuiQa FBkm3DbA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kuVnZ-0005Hm-MJ; Wed, 30 Dec 2020 07:21:33 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kuVnW-0005HE-Cv for openwrt-devel@merlin.infradead.org; Wed, 30 Dec 2020 07:21:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:Content-Type: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:In-Reply-To:References; bh=e2gchKH/ne1choeW4f3BPPfGHep6dXEmkxjRH3z5no0=; b=ZF1FgnNrKW6ZNVmYsJJWLyIS6G Q06X/IrwB6IxNYOchm7UU0BqL2N213A8cXazTBw8RE8gy33m1ROIMGXmkFlYGbb8HhbBp0ziuEz0f xSbjavSuXIqtNg49qZY6kO8PxLwOAg/rm4NPuEfQN2Y45beJb8OtF1DkN6RWqfWAOCoc2gsz5AQ80 FSXPib/XHks3JjY3vGC1KdNYr7f8qsmi0OHz/j9+Cek2QoQMc6j8Se0v9jHoqjTIRjMGHy/lVRxs5 OBHrXPbuX3hhI0+3W+CaV4jz9Y7M5fkGmDCU0raNhVueXa8DaI1N2B4wNyURnyM5KDGXu/kGZHc0l 89zSE2hg==; Received: from smtprelay08.ispgateway.de ([134.119.228.98]) by casper.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1kuVnT-000uuz-EM for openwrt-devel@lists.openwrt.org; Wed, 30 Dec 2020 07:21:28 +0000 Received: from [81.3.6.94] (helo=orange.ffh.zone) by smtprelay08.ispgateway.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.3) (envelope-from ) id 1kuVmE-00081z-VG; Wed, 30 Dec 2020 08:20:11 +0100 From: =?utf-8?q?Leonardo_M=C3=B6rlein?= To: openwrt-devel@lists.openwrt.org Subject: [PATCHv2 libubox] sh/jshn.sh: Add JSHN_DEBUG flag Date: Wed, 30 Dec 2020 08:21:02 +0100 Message-Id: <20201230072102.239207-1-me@irrelefant.net> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-Df-Sender: bWVAaXJyZWxlZmFudC5uZXQ= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201230_072128_530183_ED4B927F X-CRM114-Status: GOOD ( 12.61 ) X-Spam-Note: SpamAssassin invocation failed X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Leonardo_M=C3=B6rlein?= , Felix Fietkau Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This flag can be set to JSHN_DEBUG=1 to print all calls to json_* to stderr. The variable JSHN_DEBUG_PREFIX can be used optionally to add a string in front of every debug print. The json_* commands internally use other json_* commands. As it is not desired that this inner commands are also printed, a variable JSHN_DEBUG_DEPTH is used. It is initialized to JSHN_DEBUG_DEPTH=0, increased on any json_* call and decreased when leaving the json_* call. Debug prints are only fired if JSHN_DEBUG_DEPTH=0. Other scripts may abuse this variable to their own needs, if they do it carefully. ---snip--- JSHN_DEBUG=1 JSHN_DEBUG_PREFIX="json_debug: " json_init json_add_string "foo" "bar" json_add_string "val" "12" json_dump ---stderr--- json_debug: json_init json_debug: json_add_string foo bar json_debug: json_add_string val 12 json_debug: json_dump ---stdout--- { "foo": "bar", "val": "12" } ---snap--- This is a preparatory commit to eventually allow debugging netifd protos in a better way. Signed-off-by: Leonardo Mörlein --- sh/jshn.sh | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/sh/jshn.sh b/sh/jshn.sh index 7b0155d..3cc07fb 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -1,5 +1,18 @@ # functions for parsing and generating json +JSHN_DEBUG_DEPTH=0 + +_json_debug() { + JSHN_DEBUG_DEPTH="$((JSHN_DEBUG_DEPTH+1))" + if [ -n "$JSHN_DEBUG" ] && [ "$JSHN_DEBUG" -eq 1 ] && [ "$JSHN_DEBUG_DEPTH" -eq 1 ]; then + echo "${JSHN_DEBUG_PREFIX}$*" 1>&2 + fi +} + +_json_debug_end() { + JSHN_DEBUG_DEPTH="$((JSHN_DEBUG_DEPTH-1))" +} + _json_get_var() { # dest=$1 # var=$2 @@ -93,14 +106,17 @@ _json_close_table() { } json_set_namespace() { + _json_debug json_set_namespace "$@" local _new="$1" local _old="$2" [ -n "$_old" ] && _set_var "$_old" "$JSON_PREFIX" JSON_PREFIX="$_new" + _json_debug_end } json_cleanup() { + _json_debug json_cleanup "$@" local unset tmp _json_get_var unset JSON_UNSET @@ -118,86 +134,116 @@ json_cleanup() { ${JSON_PREFIX}JSON_SEQ \ ${JSON_PREFIX}JSON_CUR \ ${JSON_PREFIX}JSON_UNSET + _json_debug_end } json_init() { + _json_debug json_init "$@" json_cleanup export -n ${JSON_PREFIX}JSON_SEQ=0 export -- \ ${JSON_PREFIX}JSON_CUR="J_V" \ ${JSON_PREFIX}K_J_V= + _json_debug_end } json_add_object() { + _json_debug json_add_object "$@" _json_add_table "$1" object T + _json_debug_end } json_close_object() { + _json_debug json_close_object "$@" _json_close_table + _json_debug_end } json_add_array() { + _json_debug json_add_array "$@" _json_add_table "$1" array A + _json_debug_end } json_close_array() { + _json_debug json_close_array "$@" _json_close_table + _json_debug_end } json_add_string() { + _json_debug json_add_string "$@" local cur _json_get_var cur JSON_CUR _json_add_generic string "$1" "$2" "$cur" + _json_debug_end } json_add_int() { + _json_debug json_add_int "$@" local cur _json_get_var cur JSON_CUR _json_add_generic int "$1" "$2" "$cur" + _json_debug_end } json_add_boolean() { + _json_debug json_add_boolean "$@" local cur _json_get_var cur JSON_CUR _json_add_generic boolean "$1" "$2" "$cur" + _json_debug_end } json_add_double() { + _json_debug json_add_double "$@" local cur _json_get_var cur JSON_CUR _json_add_generic double "$1" "$2" "$cur" + _json_debug_end } json_add_null() { + _json_debug json_add_null "$@" local cur _json_get_var cur JSON_CUR _json_add_generic null "$1" "" "$cur" + _json_debug_end } # functions read access to json variables json_load() { + _json_debug json_load "$@" eval "`jshn -r "$1"`" + _json_debug_end } json_load_file() { + _json_debug json_load_file "$@" eval "`jshn -R "$1"`" + _json_debug_end } json_dump() { - jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w + _json_debug json_dump "$@" + jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w + _json_debug_end } json_get_type() { + _json_debug json_get_type "$@" local __dest="$1" local __cur _json_get_var __cur JSON_CUR + _json_debug_end local __var="${JSON_PREFIX}T_${__cur}_${2//[^a-zA-Z0-9_]/_}" eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" } json_get_keys() { + _json_debug json_get_keys "$@" local __dest="$1" local _tbl_cur @@ -206,11 +252,13 @@ json_get_keys() { else _json_get_var _tbl_cur JSON_CUR fi + _json_debug_end local __var="${JSON_PREFIX}K_${_tbl_cur}" eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" } json_get_values() { + _json_debug json_get_values "$@" local _v_dest="$1" local _v_keys _v_val _select= local _json_no_warning=1 @@ -230,19 +278,23 @@ json_get_values() { done [ -n "$_select" ] && json_select .. + _json_debug_end return 0 } json_get_var() { + _json_debug json_get_var "$@" local __dest="$1" local __cur _json_get_var __cur JSON_CUR + _json_debug_end local __var="${JSON_PREFIX}${__cur}_${2//[^a-zA-Z0-9_]/_}" eval "export -- \"$__dest=\${$__var:-$3}\"; [ -n \"\${$__var+x}\${3+x}\" ]" } json_get_vars() { + _json_debug json_get_vars "$@" while [ "$#" -gt 0 ]; do local _var="$1"; shift if [ "$_var" != "${_var#*:}" ]; then @@ -251,21 +303,25 @@ json_get_vars() { json_get_var "$_var" "$_var" fi done + _json_debug_end } json_select() { + _json_debug json_select "$@" local target="$1" local type local cur [ -z "$1" ] && { _json_set_var JSON_CUR "J_V" + _json_debug_end return 0 } [[ "$1" == ".." ]] && { _json_get_var cur JSON_CUR _json_get_var cur "U_$cur" _json_set_var JSON_CUR "$cur" + _json_debug_end return 0 } json_get_type type "$target" @@ -277,20 +333,28 @@ json_select() { *) [ -n "$_json_no_warning" ] || \ echo "WARNING: Variable '$target' does not exist or is not an array/object" + _json_debug_end return 1 ;; esac + _json_debug_end } json_is_a() { + _json_debug json_is_a "$@" local type json_get_type type "$1" + _json_debug_end [ "$type" = "$2" ] } json_for_each_item() { - [ "$#" -ge 2 ] || return 0 + _json_debug json_for_each_item "$@" + if [ "$#" -lt 2 ]; then + _json_debug_end + return 0 + fi local function="$1"; shift local target="$1"; shift local type val @@ -312,4 +376,5 @@ json_for_each_item() { eval "$function \"\$val\" \"\" \"\$@\"" ;; esac + _json_debug_end }