[RFC PATCH 07/28] cli: lil: Simplify callbacks

Sean Anderson seanga2 at gmail.com
Thu Jul 1 08:15:50 CEST 2021


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 <seanga2 at gmail.com>
---

 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 <stdint.h>
 #include <inttypes.h>
 
@@ -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



More information about the U-Boot mailing list