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 D860AC11F64 for ; Thu, 1 Jul 2021 06:17:46 +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 563B16148E for ; Thu, 1 Jul 2021 06:17:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 563B16148E 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 1B0528328C; Thu, 1 Jul 2021 08:16:43 +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="pl9ESLPv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6F2AB83288; Thu, 1 Jul 2021 08:16:36 +0200 (CEST) Received: from mail-qv1-xf2a.google.com (mail-qv1-xf2a.google.com [IPv6:2607:f8b0:4864:20::f2a]) (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 3A0A580C58 for ; Thu, 1 Jul 2021 08:16:21 +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-qv1-xf2a.google.com with SMTP id m15so2418826qvc.9 for ; Wed, 30 Jun 2021 23:16:21 -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=FFjmfHBG0eN99hMAiWv4/os41SxMb64zojoinmB8qU8=; b=pl9ESLPv3wzgeXj3ghkKTbIOI0+4wzT3I9aLVTxaYqdVORYz+xgIWDsfREN5TK1SiE BrSHDdTEvNXPQoRxx4r8CvrM+JmBttiGAlVWHe2/wAA/qFF2nxN4odD+AMYwsSVghtyr Y9NomaRcvRFRhVnnNq1OLlowZvMFnnB/+Jc2BmhDdZIp4Gp2bsyRxz2DKtWMA18J7mOv qUsD2DS/uLzWNVQHmxfyh4YCcInzkIblNxFR1fwdYZClXA+EOsDM+XKJjjVWNQC43hyv mO7a2pSn0ms62c22X2OUlHRjhJM2hE/oYGNOgMOaRifhbKpq7IQz+Dxzc71Jb1oSWrWV xzrA== 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=FFjmfHBG0eN99hMAiWv4/os41SxMb64zojoinmB8qU8=; b=VUSnPduD9fVdc/ahHGULyFJ/hK3Jqj+KJ/cOYmIFpaGT0u7f2l8OzIDE8DY5FLlcPs 1oYT6KFd5GuShWY5Meq5+QB8MAkyG8BOvjczy80rtJejkJFGYmzHJ7aEopMeN/RYKK6v GF5zwEC8d8e5Nm2Lbu6fOc3SE6L4Sx1fxvh+/V3JZOlgfdam6ncdFOWVHIEbdNt7wLGI /+513xxwVREYSDF5HGX0mZprgYSYSkMiWg2gG04uKDaG/Q4pNYowglm9H6lcOURygNri 3+dDmGn+MhwsfpnTpEMUskf450TOFWCoqEylbWvojPOydqoP5ZPhrgW9YX2JrYVrpG+6 K3Ug== X-Gm-Message-State: AOAM533o9lLy34m6qL0due4RZjuVmNfAvJIWD8Rw1eqFdsd/a4M8ucIh 3lb4El/94sqFheWIP5lcQhha1AhpJ3k= X-Google-Smtp-Source: ABdhPJww4QwuDI+qwl53ZV1jmA6DEdkSYEyj1kiAhTcWmFXZPPH/EdV9u5AOk11ygNkau2ARbelilA== X-Received: by 2002:a05:6214:a66:: with SMTP id ef6mr18136518qvb.60.1625120179935; Wed, 30 Jun 2021 23:16:19 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 23:16:19 -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 07/28] cli: lil: Simplify callbacks Date: Thu, 1 Jul 2021 02:15:50 -0400 Message-Id: <20210701061611.957918-8-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 We only need the getvar and setvar callbacks for now (to integrate with the environment). So remove all the other callbacks. Instead of allowing callbacks to be set at any time, just set them once in lil_new. Lastly, get rid of the typedefs and use a struct to pass in the callbacks instead. Signed-off-by: Sean Anderson --- common/cli_lil.c | 45 ++++++++++++--------------------------------- include/cli_lil.h | 34 ++++++++-------------------------- 2 files changed, 20 insertions(+), 59 deletions(-) diff --git a/common/cli_lil.c b/common/cli_lil.c index 3d1e6181f9..539a4a3238 100644 --- a/common/cli_lil.c +++ b/common/cli_lil.c @@ -24,7 +24,6 @@ * overflows and is also useful when running through an automated fuzzer like AFL */ /*#define LIL_ENABLE_RECLIMIT 10000*/ -#define CALLBACKS 8 #define HASHMAP_CELLS 256 #define HASHMAP_CELLMASK 0xFF @@ -100,7 +99,7 @@ struct lil { } error; size_t err_head; char *err_msg; - lil_callback_proc_t callback[CALLBACKS]; + struct lil_callbacks callbacks; size_t parse_depth; }; @@ -667,14 +666,10 @@ struct lil_var *lil_set_var(struct lil *lil, const char *name, var->env == lil->rootenv && var->env != env) var = NULL; - if (((!var && env == lil->rootenv) || - (var && var->env == lil->rootenv)) && - lil->callback[LIL_CALLBACK_SETVAR]) { - lil_setvar_callback_proc_t proc = - (lil_setvar_callback_proc_t) - lil->callback[LIL_CALLBACK_SETVAR]; + if (lil->callbacks.setvar && ((!var && env == lil->rootenv) || + (var && var->env == lil->rootenv))) { struct lil_value *newval = val; - int r = proc(lil, name, &newval); + int r = lil->callbacks.setvar(lil, name, &newval); if (r < 0) { return NULL; @@ -717,14 +712,10 @@ struct lil_value *lil_get_var_or(struct lil *lil, const char *name, struct lil_var *var = lil_find_var(lil, lil->env, name); struct lil_value *retval = var ? var->v : defvalue; - if (lil->callback[LIL_CALLBACK_GETVAR] && - (!var || var->env == lil->rootenv)) { - lil_getvar_callback_proc_t proc = - (lil_getvar_callback_proc_t) - lil->callback[LIL_CALLBACK_GETVAR]; + if (lil->callbacks.getvar && (!var || var->env == lil->rootenv)) { struct lil_value *newretval = retval; - if (proc(lil, name, &newretval)) + if (lil->callbacks.getvar(lil, name, &newretval)) retval = newretval; } return retval; @@ -748,10 +739,15 @@ void lil_pop_env(struct lil *lil) } } -struct lil *lil_new(void) +struct lil *lil_new(const struct lil_callbacks *callbacks) { struct lil *lil = calloc(1, sizeof(struct lil)); + if (!lil) + return NULL; + + if (callbacks) + memcpy(&lil->callbacks, callbacks, sizeof(lil->callbacks)); lil->rootenv = lil->env = lil_alloc_env(NULL); lil->empty = alloc_value(NULL); hm_init(&lil->cmdmap); @@ -1148,15 +1144,6 @@ struct lil_value *lil_parse(struct lil *lil, const char *code, size_t codelen, } cleanup: - if (lil->error && lil->callback[LIL_CALLBACK_ERROR] && - lil->parse_depth == 1) { - lil_error_callback_proc_t proc = - (lil_error_callback_proc_t) - lil->callback[LIL_CALLBACK_ERROR]; - - proc(lil, lil->err_head, lil->err_msg); - } - if (words) lil_free_list(words); lil->code = save_code; @@ -1185,14 +1172,6 @@ struct lil_value *lil_parse_value(struct lil *lil, struct lil_value *val, return lil_parse(lil, val->d, val->l, funclevel); } -void lil_callback(struct lil *lil, int cb, lil_callback_proc_t proc) -{ - if (cb < 0 || cb > CALLBACKS) - return; - - lil->callback[cb] = proc; -} - void lil_set_error(struct lil *lil, const char *msg) { if (lil->error) diff --git a/include/cli_lil.h b/include/cli_lil.h index 48735e0605..b8df94a766 100644 --- a/include/cli_lil.h +++ b/include/cli_lil.h @@ -20,15 +20,6 @@ enum lil_setvar { LIL_SETVAR_LOCAL_ONLY, }; -#define LIL_CALLBACK_EXIT 0 -#define LIL_CALLBACK_WRITE 1 -#define LIL_CALLBACK_READ 2 -#define LIL_CALLBACK_STORE 3 -#define LIL_CALLBACK_SOURCE 4 -#define LIL_CALLBACK_ERROR 5 -#define LIL_CALLBACK_SETVAR 6 -#define LIL_CALLBACK_GETVAR 7 - #include #include @@ -40,22 +31,15 @@ struct lil_list; struct lil; typedef struct lil_value *(*lil_func_proc_t)(struct lil *lil, size_t argc, struct lil_value **argv); -typedef void (*lil_exit_callback_proc_t)(struct lil *lil, - struct lil_value *arg); -typedef void (*lil_write_callback_proc_t)(struct lil *lil, const char *msg); -typedef char *(*lil_read_callback_proc_t)(struct lil *lil, const char *name); -typedef char *(*lil_source_callback_proc_t)(struct lil *lil, const char *name); -typedef void (*lil_store_callback_proc_t)(struct lil *lil, const char *name, - const char *data); -typedef void (*lil_error_callback_proc_t)(struct lil *lil, size_t pos, - const char *msg); -typedef int (*lil_setvar_callback_proc_t)(struct lil *lil, const char *name, - struct lil_value **value); -typedef int (*lil_getvar_callback_proc_t)(struct lil *lil, const char *name, - struct lil_value **value); -typedef void (*lil_callback_proc_t)(void); -struct lil *lil_new(void); +struct lil_callbacks { + int (*setvar)(struct lil *lil, const char *name, + struct lil_value **value); + int (*getvar)(struct lil *lil, const char *name, + struct lil_value **value); +}; + +struct lil *lil_new(const struct lil_callbacks *callbacks); void lil_free(struct lil *lil); int lil_register(struct lil *lil, const char *name, lil_func_proc_t proc); @@ -65,8 +49,6 @@ struct lil_value *lil_parse(struct lil *lil, const char *code, size_t codelen, struct lil_value *lil_parse_value(struct lil *lil, struct lil_value *val, int funclevel); -void lil_callback(struct lil *lil, int cb, lil_callback_proc_t proc); - void lil_set_error(struct lil *lil, const char *msg); void lil_set_error_at(struct lil *lil, size_t pos, const char *msg); int lil_error(struct lil *lil, const char **msg, size_t *pos); -- 2.32.0