From mboxrd@z Thu Jan 1 00:00:00 1970 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=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,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 58833C11F64 for ; Thu, 1 Jul 2021 06:20:44 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D64DA61623 for ; Thu, 1 Jul 2021 06:20:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D64DA61623 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6F62F832C4; Thu, 1 Jul 2021 08:17:29 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Gb0+y9x3"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 15B918329A; Thu, 1 Jul 2021 08:17:08 +0200 (CEST) Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 26C738327D for ; Thu, 1 Jul 2021 08:16:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qt1-x833.google.com with SMTP id c13so3356564qtb.12 for ; Wed, 30 Jun 2021 23:16:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZXvUJ7UMJ+/KiTPWdMfD4v4jVIySwvkPdschswFrCjI=; b=Gb0+y9x37g7pHmG1a6Vb/f9UixSPLDZ3IPi3Ubv/3O58hKGTOERgtZjkrms3USHqRT Nj6gK+NTv43Rg4kp26lNRpxSZtRcWahOgJ6/RwL+6e1vdZ5y5TAKVwtCrQIYSAzWBb3X 86YnKpvNzBNCvVH5VV/w1ST5J29Pn8yj/lzzCBVzAFEFEeO1q2jGNd3jpziy5z4BQQxp uGyii0U5yWX9wANIOdW9pG14NI5FMbI9tOqRYeVaUoz50e0iNQ3xkIm7Sqs6qZ1zyYQH GpCl9A/chYKph3bIwkSDaBEOonqEL6j8Op5eZy2sPsY3azMvR63qUZvO6PHQPipFhcCE TQ3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZXvUJ7UMJ+/KiTPWdMfD4v4jVIySwvkPdschswFrCjI=; b=qqL8ff5w9cCAuMXG/98ZnUdDF7j8POiwPqUtaB6FkkpgWiXtZRLilrcPt7yX83vGgh wg0gmNXEdDnstKbW1S7OS8TLp7/0hX14xsONFwdnK8NwCeYWQh+uklOVlw1fT0rNCaLB LuzkkurvrZvqKlaG2gi5FeWp1/8RShBSJFG2pV7F0oVk8uKaO+SjbhwEZ6LWPj56GEmN FGasVnD1vRtCKARTB6aL3SXsBJqtsq9N35wsPzb4Nqi6TXGzFde9BoI4qVbujpqM3GBA T89rHV/GsjhYSgozHrM4DY2spJeN3nNwcs/RUhMI47w9MA+jc1eyxudtwLK+jD1ADaHm QOVw== X-Gm-Message-State: AOAM532F2A1JSya7wgE0s1HNimQA7xDIp9v5Hfp2Z8mToMwBce9U2XZ9 HB9TwhCqa3B+bNq+jJq1wYggDKpHbLk= X-Google-Smtp-Source: ABdhPJyuIDapthVlbguMddmezvBRfAYt8cOlF5pLDy5bJFhGt5ZlTLGaR3QKcKu58HoctXYhJLBxYw== X-Received: by 2002:ac8:4618:: with SMTP id p24mr26089340qtn.324.1625120192879; Wed, 30 Jun 2021 23:16:32 -0700 (PDT) Received: from godwin.fios-router.home (pool-74-96-87-9.washdc.fios.verizon.net. [74.96.87.9]) by smtp.gmail.com with ESMTPSA id g21sm1684673qts.90.2021.06.30.23.16.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 23:16:32 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de, Tom Rini Cc: =?UTF-8?q?Marek=20Beh=C3=BAn?= , Wolfgang Denk , Simon Glass , Roland Gaudig , Heinrich Schuchardt , Kostas Michalopoulos , Sean Anderson Subject: [RFC PATCH 24/28] cli: lil: Make proc always take 3 arguments Date: Thu, 1 Jul 2021 02:16:07 -0400 Message-Id: <20210701061611.957918-25-seanga2@gmail.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210701061611.957918-1-seanga2@gmail.com> References: <20210701061611.957918-1-seanga2@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean This rewrites proc to always take 3 arguments. It also adds proper error handling. TCL does not allow for anonymous functions to be created with proc. Allowing for a variable number of arguments makes the code much more complex when adding error handling. Since fnc_proc was the last user of lil_unused_name (other than fnc_unusedname), remove it. Signed-off-by: Sean Anderson --- common/cli_lil.c | 103 ++++++++++++++++------------------------------- test/cmd/lil.c | 6 ++- 2 files changed, 38 insertions(+), 71 deletions(-) diff --git a/common/cli_lil.c b/common/cli_lil.c index 7ec73675f3..1c7c340bda 100644 --- a/common/cli_lil.c +++ b/common/cli_lil.c @@ -2985,32 +2985,6 @@ struct lil_value *lil_eval_expr(struct lil *lil, struct lil_value *code) return lil_alloc_integer(ee.ival); } -struct lil_value *lil_unused_name(struct lil *lil, const char *part) -{ - char *name = malloc(strlen(part) + 64); - struct lil_value *val; - size_t i; - - for (i = 0; i < (size_t)-1; i++) { - sprintf(name, "!!un!%s!%09u!nu!!", part, (unsigned int)i); - if (lil_find_cmd(lil, name)) - continue; - - if (lil_find_var(lil, lil->env, name)) - continue; - - val = lil_alloc_string(name); - free(name); - return val; - } - return NULL; -} - -struct lil_value *lil_arg(struct lil_value **argv, size_t index) -{ - return argv ? argv[index] : NULL; -} - const char *lil_to_string(struct lil_value *val) { return (val && val->l) ? val->d : ""; @@ -3237,47 +3211,46 @@ static struct lil_value *fnc_reflect(struct lil *lil, size_t argc, static struct lil_value *fnc_proc(struct lil *lil, size_t argc, struct lil_value **argv) { - struct lil_value *name; struct lil_func *cmd; - struct lil_list *fargs; + struct lil_list *args; + struct lil_value *name, *code; - if (argc < 1) + if (argc != 3) { + lil_set_error_argc(lil, 3); return NULL; - - if (argc >= 3) { - name = lil_clone_value(argv[0]); - fargs = lil_subst_to_list(lil, argv[1]); - cmd = add_func(lil, lil_to_string(argv[0])); - if (!cmd) - return NULL; - - cmd->argnames = fargs; - cmd->code = lil_clone_value(argv[2]); - } else { - name = lil_unused_name(lil, "anonymous-function"); - if (argc < 2) { - struct lil_value *tmp = lil_alloc_string("args"); - - fargs = lil_subst_to_list(lil, tmp); - lil_free_value(tmp); - cmd = add_func(lil, lil_to_string(name)); - if (!cmd) - return NULL; - - cmd->argnames = fargs; - cmd->code = lil_clone_value(argv[0]); - } else { - fargs = lil_subst_to_list(lil, argv[0]); - cmd = add_func(lil, lil_to_string(name)); - if (!cmd) - return NULL; - - cmd->argnames = fargs; - cmd->code = lil_clone_value(argv[1]); - } } + name = lil_clone_value(argv[0]); + if (!name) { + lil_set_error_oom(lil); + return NULL; + } + + args = lil_subst_to_list(lil, argv[1]); + if (!args) + goto err_args; + + code = lil_clone_value(argv[2]); + if (!code) { + lil_set_error_oom(lil); + goto err_code; + } + + cmd = add_func(lil, lil_to_string(name)); + if (!cmd) + goto err_func; + cmd->argnames = args; + cmd->code = code; + return name; + +err_func: + lil_free_value(code); +err_code: + lil_free_list(args); +err_args: + lil_free_value(name); + return NULL; } static struct lil_value *fnc_rename(struct lil *lil, size_t argc, @@ -3312,13 +3285,6 @@ static struct lil_value *fnc_rename(struct lil *lil, size_t argc, return r; } -static struct lil_value *fnc_unusedname(struct lil *lil, size_t argc, - struct lil_value **argv) -{ - return lil_unused_name(lil, argc > 0 ? lil_to_string(argv[0]) : - "unusedname"); -} - static struct lil_value *fnc_quote(struct lil *lil, size_t argc, struct lil_value **argv) { @@ -4310,7 +4276,6 @@ static void register_stdcmds(struct lil *lil) lil_register(lil, "substr", fnc_substr); lil_register(lil, "topeval", fnc_topeval); lil_register(lil, "trim", fnc_trim); - lil_register(lil, "unusedname", fnc_unusedname); lil_register(lil, "upeval", fnc_upeval); } } diff --git a/test/cmd/lil.c b/test/cmd/lil.c index fb33fa83a6..58bc6ee842 100644 --- a/test/cmd/lil.c +++ b/test/cmd/lil.c @@ -149,9 +149,11 @@ static const struct { "[list eh??]" "];" "asserteq_list [lapply $list length] [list 9 10 4];" - "asserteq_list [lapply $list [proc {a} {" + "proc firstword {a} {" "return [index [split $a] 0]" - "}]] [list {bad's} {good's} eh??]" + "};" + "asserteq_list [lapply $list firstword] " + "[list {bad's} {good's} eh??]" }, {"lists", "set l [list foo bar baz bad];" -- 2.32.0