[RFC PATCH 11/28] cli: lil: Add several helper functions for errors

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


This adds several helper functions for common error types. This helps keep
down code duplication, and also ensures that each of these errors uses the
same message.

Signed-off-by: Sean Anderson <seanga2 at gmail.com>
---

 common/cli_lil.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/common/cli_lil.c b/common/cli_lil.c
index f29c8065d8..c19a25b2bf 100644
--- a/common/cli_lil.c
+++ b/common/cli_lil.c
@@ -116,6 +116,9 @@ static void lil_set_error(struct lil *lil, enum lil_error err, const char *msg);
 static void lil_set_errorf(struct lil *lil, enum lil_error err,
 			   const char *fmt, ...)
 	__attribute((format(__printf__, 3, 4)));
+static void lil_set_error_oom(struct lil *lil);
+static void lil_set_error_nocmd(struct lil *lil, const char *cmdname);
+static void lil_set_error_intr(struct lil *lil);
 
 #ifdef LIL_ENABLE_POOLS
 static struct lil_value **pool;
@@ -1124,9 +1127,8 @@ struct lil_value *lil_parse(struct lil *lil, const char *code, size_t codelen,
 
 			if (!cmd) {
 				if (words->v[0]->l) {
-					lil_set_errorf(lil, LIL_ERR_NOCMD,
-						       "unknown function %s",
-						       words->v[0]->d);
+					lil_set_error_nocmd(lil,
+							    words->v[0]->d);
 					goto cleanup;
 				}
 			} else {
@@ -1204,6 +1206,33 @@ static void lil_set_errorf(struct lil *lil, enum lil_error err,
 	va_end(saveargs);
 }
 
+/*
+ * The next several functions provide helpers for throwing some formulaic
+ * errors. Their purpose is to ensure that the same wording is used everywhere
+ * the error is used. This reduces data size.
+ */
+
+static void lil_set_error_oom(struct lil *lil)
+{
+	lil_set_error(lil, LIL_ERR_OOM, NULL);
+}
+
+static void lil_set_error_argc(struct lil *lil, size_t expected)
+{
+	lil_set_errorf(lil, LIL_ERR_ARGC, "%s: expected %zu arguments",
+		       lil->env->proc ?: lil->env->func->name, expected);
+}
+
+static void lil_set_error_nocmd(struct lil *lil, const char *cmdname)
+{
+	lil_set_errorf(lil, LIL_ERR_NOCMD, "no such command %s", cmdname);
+}
+
+static void lil_set_error_intr(struct lil *lil)
+{
+	lil_set_error(lil, LIL_ERR_INTR, "interrupted");
+}
+
 enum lil_error lil_error(struct lil *lil, const char **msg)
 {
 	enum lil_error err = lil->err;
@@ -1992,8 +2021,7 @@ static struct lil_value *fnc_rename(struct lil *lil, size_t argc,
 	newname = lil_to_string(argv[1]);
 	func = lil_find_cmd(lil, oldname);
 	if (!func) {
-		lil_set_errorf(lil, LIL_ERR_NOCMD, "unknown function %s",
-			       oldname);
+		lil_set_error_nocmd(lil, oldname);
 		return NULL;
 	}
 
-- 
2.32.0



More information about the U-Boot mailing list