From patchwork Fri Sep 8 18:24:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Venekamp X-Patchwork-Id: 1831719 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=By/Iwzw3; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=HYYwZRvK; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Rj5t000mdz1yhG for ; Sat, 9 Sep 2023 05:35:01 +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=xOYUihrI/eDr8qcihutoFdcIt9F9DYcSi8G5fTUvZrA=; b=By/Iwzw3VIpTtB aT9fW7aHGIWPtnEE3K0jpSlMWumv8L8AM1y73wvKYKKnwyNnSkAPy2fEeUyTZzL35PQQadX1qDPpH xC5fmO80n/dm0Dp3ybZwtBa1xOuB/+ykjRx/lOpxibEvuW8aM5znX3EfXBCdebug5Nz70cYkqUyPK uUErPLFPJuRMyp21QmsB0XU5wBm/raXFdTWnW+G+DWNxhUkF+MQZ5mqfTKtZkQ3JEqCT5kjroSHbr s3utL3EgrhmIlhB8reHTD547D80C+FQTCV6zpb02Flrs5I6scTAgEmoXdzuiim2/DRNtwrHlKHA1F 7p+wgisMw/Da09VY69tg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qehCS-00EME2-32; Fri, 08 Sep 2023 19:31:28 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qeg9p-00EHYU-22 for openwrt-devel@bombadil.infradead.org; Fri, 08 Sep 2023 18:24:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:In-Reply-To:References; bh=5IRzoreLGvMC/MU19oYXKLudJRlihzTbsi63XBdE74M=; b=HYYwZRvKK5bA34Hr9ObTy8QFDd 59wU2ARLdtMW0T9mHL45YG57RUtT/c/A5ZvWxJUBBaohoPOcPOVVmDSC8I0VLRJIT+36vsDKzTUHS dlpnPq5sjyb+jMFAH35luGJ0xRT93Uv+ITEg5UNsB2cK+fMg8tEpNzIsahqKYK9hwUHqw9fwQ9X4a YwW6TciEHQOdtTlb6oaBlBpJzcGWdN/wogip1CK78tyoc6LJi2qDVLyi7OFxrDrAmNFumnVRPjunJ MuN1K/7/70PTihucdXmcT5tDFlyD1BkYhrU+SJxtrbeDmror8smyFZ81r+P3vKf576W7wquMwmdke FmvGWXXg==; Received: from virt1.bvwebdesign.nl ([149.210.228.112]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qeg9j-002fjk-1t for openwrt-devel@lists.openwrt.org; Fri, 08 Sep 2023 18:24:39 +0000 Received: from localhost.localdomain (83-82-122-37.cable.dynamic.v4.ziggo.nl [83.82.122.37]) by virt1.bvwebdesign.nl (Postfix) with ESMTPSA id 76CEC959F74; Fri, 8 Sep 2023 20:24:29 +0200 (CEST) From: Jan Venekamp To: openwrt-devel@lists.openwrt.org Cc: Jan Venekamp Subject: [PATCH uci] reduce code complexity of uci_show_package Date: Fri, 8 Sep 2023 20:24:22 +0200 Message-Id: <20230908182422.40316-1-jan@venekamp.net> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230908_192437_243749_4F7BB01D X-CRM114-Status: GOOD ( 16.36 ) X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "desiato.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: For the extended syntax on "uci show" a list of previous section types is build and iterated over to determine the current type index of a section. However, instead building this list and iterate over [...] 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_PASS SPF: HELO matches 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 For the extended syntax on "uci show" a list of previous section types is build and iterated over to determine the current type index of a section. However, instead building this list and iterate over that we can simply iterate over the previous sections to find the index. This reduces the code complexity of uci_show_package, uci_show_section and uci_show_option. Signed-off-by: Jan Venekamp --- cli.c | 132 +++++------------- .../references/show_parsing_multiline.data | 3 + .../show_parsing_multiline_package.result | 2 + 3 files changed, 37 insertions(+), 100 deletions(-) diff --git a/cli.c b/cli.c index f169e42..2cbfecf 100644 --- a/cli.c +++ b/cli.c @@ -55,88 +55,8 @@ enum { CMD_HELP, }; -struct uci_type_list { - unsigned int idx; - const char *name; - struct uci_type_list *next; -}; - -static struct uci_type_list *type_list = NULL; -static char *typestr = NULL; -static const char *cur_section_ref = NULL; - static int uci_cmd(int argc, char **argv); -static void -uci_reset_typelist(void) -{ - struct uci_type_list *type; - while (type_list != NULL) { - type = type_list; - type_list = type_list->next; - free(type); - } - if (typestr) { - free(typestr); - typestr = NULL; - } - cur_section_ref = NULL; -} - -static char * -uci_lookup_section_ref(struct uci_section *s) -{ - struct uci_type_list *ti = type_list; - char *ret; - int maxlen; - - if (!(flags & CLI_FLAG_SHOW_EXT)) - return s->e.name; - - /* look up in section type list */ - while (ti) { - if (strcmp(ti->name, s->type) == 0) - break; - ti = ti->next; - } - if (!ti) { - ti = calloc(1, sizeof(struct uci_type_list)); - if (!ti) - return NULL; - ti->next = type_list; - type_list = ti; - ti->name = s->type; - } - - if (s->anonymous) { - maxlen = strlen(s->type) + 1 + 2 + 10; - if (!typestr) { - typestr = malloc(maxlen); - if (!typestr) - return NULL; - } else { - void *p = realloc(typestr, maxlen); - if (!p) { - free(typestr); - return NULL; - } - - typestr = p; - } - - if (typestr) - sprintf(typestr, "@%s[%d]", ti->name, ti->idx); - - ret = typestr; - } else { - ret = s->e.name; - } - - ti->idx++; - - return ret; -} - static void uci_usage(void) { fprintf(stderr, @@ -243,40 +163,52 @@ static void uci_show_value(struct uci_option *o, bool quote) } } -static void uci_show_option(struct uci_option *o, bool quote) +static void uci_show_option(struct uci_option *o) { - printf("%s.%s.%s=", - o->section->package->e.name, - (cur_section_ref ? cur_section_ref : o->section->e.name), - o->e.name); - uci_show_value(o, quote); + printf("%s.%s.%s=", o->section->package->e.name, o->section->e.name, o->e.name); + uci_show_value(o, true); } static void uci_show_section(struct uci_section *s) { struct uci_element *e; - const char *cname; - const char *sname; - cname = s->package->e.name; - sname = (cur_section_ref ? cur_section_ref : s->e.name); - printf("%s.%s=%s\n", cname, sname, s->type); + printf("%s.%s=%s\n", s->package->e.name, s->e.name, s->type); uci_foreach_element(&s->options, e) { - uci_show_option(uci_to_option(e), true); + uci_show_option(uci_to_option(e)); } } static void uci_show_package(struct uci_package *p) { - struct uci_element *e; + struct uci_element *e0, *e1; + bool show_ext = flags & CLI_FLAG_SHOW_EXT; + + uci_foreach_element(&p->sections, e0) { + struct uci_section *s = uci_to_section(e0); + unsigned int idx = 0; + + if (!s->anonymous || !show_ext) { + uci_show_section(s); + continue; + } - uci_reset_typelist(); - uci_foreach_element( &p->sections, e) { - struct uci_section *s = uci_to_section(e); - cur_section_ref = uci_lookup_section_ref(s); - uci_show_section(s); + /* count preceding sections with same type */ + uci_foreach_element(&p->sections, e1) { + if (e1 == e0) + break; + if (strcmp(uci_to_section(e1)->type, s->type) == 0) + idx++; + } + + /* show anonymous section and its options with extended syntax */ + printf("%s.@%s[%d]=%s\n", s->package->e.name, s->type, idx, s->type); + uci_foreach_element(&s->options, e1) { + struct uci_option *o = uci_to_option(e1); + printf("%s.@%s[%d].%s=", s->package->e.name, s->type, idx, o->e.name); + uci_show_value(o, true); + } } - uci_reset_typelist(); } static void uci_show_changes(struct uci_package *p) @@ -348,7 +280,7 @@ static int package_cmd(int cmd, char *tuple) goto out; } if (ptr.o) - uci_show_option(ptr.o, true); + uci_show_option(ptr.o); else if (ptr.s) uci_show_section(ptr.s); else if (ptr.p) diff --git a/tests/shunit2/references/show_parsing_multiline.data b/tests/shunit2/references/show_parsing_multiline.data index 670e809..a114d67 100644 --- a/tests/shunit2/references/show_parsing_multiline.data +++ b/tests/shunit2/references/show_parsing_multiline.data @@ -18,3 +18,6 @@ config sockd 'instance0' from: 0.0.0.0/0 to: 0.0.0.0/0 log: connect } ' + +config sockd + option enabled 0 diff --git a/tests/shunit2/references/show_parsing_multiline_package.result b/tests/shunit2/references/show_parsing_multiline_package.result index 218082d..b1d9704 100644 --- a/tests/shunit2/references/show_parsing_multiline_package.result +++ b/tests/shunit2/references/show_parsing_multiline_package.result @@ -16,3 +16,5 @@ sockd.instance0.extra_config=' from: 0.0.0.0/0 to: 0.0.0.0/0 log: connect } ' +sockd.@sockd[1]=sockd +sockd.@sockd[1].enabled='0'