[PATCH v2 32/32] RFC: command: Introduce functions to obtain command arguments

Simon Glass sjg at chromium.org
Thu Nov 16 05:10:32 CET 2023


Add some functions which provide an argument to a command, or NULL if
the argument does not exist.

Use the same numbering as argv[] since it seems less confusing than the
previous idea.

Signed-off-by: Simon Glass <sjg at chromium.org>
Suggested-by: Tom Rini <trini at konsulko.com>
---

Changes in v2:
- Add new patch to obtain command arguments

 boot/bootm.c       |  4 ++--
 cmd/booti.c        |  4 ++--
 cmd/bootz.c        |  4 ++--
 cmd/disk.c         |  4 ++--
 cmd/fuse.c         |  2 +-
 cmd/mmc.c          |  2 +-
 drivers/misc/gsc.c |  6 +++---
 fs/fs.c            |  4 ++--
 include/command.h  | 33 +++++++++++++++++++++++++++++++++
 test/cmd_ut.c      |  2 +-
 10 files changed, 49 insertions(+), 16 deletions(-)

diff --git a/boot/bootm.c b/boot/bootm.c
index 07aae26af01f..a0d17be7742c 100644
--- a/boot/bootm.c
+++ b/boot/bootm.c
@@ -1014,8 +1014,8 @@ int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
 		ulong img_addr;
 
 		img_addr = argc ? hextoul(argv[0], NULL) : image_load_addr;
-		ret = bootm_find_other(img_addr, argc > 1 ? argv[1] : NULL,
-				       argc > 2 ? argv[2] : NULL);
+		ret = bootm_find_other(img_addr, cmd_arg1(argc, argv),
+				       cmd_arg2(argc, argv));
 	}
 
 	if (IS_ENABLED(CONFIG_MEASURED_BOOT) && !ret &&
diff --git a/cmd/booti.c b/cmd/booti.c
index dc73c83f0db0..2db8f4a16ff2 100644
--- a/cmd/booti.c
+++ b/cmd/booti.c
@@ -95,8 +95,8 @@ static int booti_start(struct cmd_tbl *cmdtp, int flag, int argc,
 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
 	 * have a header that provide this informaiton.
 	 */
-	if (bootm_find_images(image_load_addr, argc > 1 ? argv[1] : NULL,
-			      argc > 2 ? argv[2] : NULL, relocated_addr,
+	if (bootm_find_images(image_load_addr, cmd_arg1(argc, argv),
+			      cmd_arg2(argc, argv), relocated_addr,
 			      image_size))
 		return 1;
 
diff --git a/cmd/bootz.c b/cmd/bootz.c
index bcf232b4f305..a652879ea5ec 100644
--- a/cmd/bootz.c
+++ b/cmd/bootz.c
@@ -54,8 +54,8 @@ static int bootz_start(struct cmd_tbl *cmdtp, int flag, int argc,
 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
 	 * have a header that provide this informaiton.
 	 */
-	if (bootm_find_images(image_load_addr, argc > 1 ? argv[1] : NULL,
-			      argc > 2 ? argv[2] : NULL, images->ep,
+	if (bootm_find_images(image_load_addr, cmd_arg1(argc, argv),
+			      cmd_arg2(argc, argv), images->ep,
 			      zi_end - zi_start))
 		return 1;
 
diff --git a/cmd/disk.c b/cmd/disk.c
index 3d7bc2f60189..92eaa02f4a13 100644
--- a/cmd/disk.c
+++ b/cmd/disk.c
@@ -40,8 +40,8 @@ int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
 
 	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
 
-	part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL,
-					&dev_desc, &info, 1);
+	part = blk_get_device_part_str(intf, cmd_arg2(argc, argv),
+				       &dev_desc, &info, 1);
 	if (part < 0) {
 		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
 		return 1;
diff --git a/cmd/fuse.c b/cmd/fuse.c
index 0676bb7a812f..f884c894fb00 100644
--- a/cmd/fuse.c
+++ b/cmd/fuse.c
@@ -44,7 +44,7 @@ static int confirm_prog(void)
 static int do_fuse(struct cmd_tbl *cmdtp, int flag, int argc,
 		   char *const argv[])
 {
-	const char *op = argc >= 2 ? argv[1] : NULL;
+	const char *op = cmd_arg1(argc, argv);
 	int confirmed = argc >= 3 && !strcmp(argv[2], "-y");
 	u32 bank, word, cnt, val, cmp;
 	ulong addr;
diff --git a/cmd/mmc.c b/cmd/mmc.c
index 96befb27eec6..2d5430a53079 100644
--- a/cmd/mmc.c
+++ b/cmd/mmc.c
@@ -946,7 +946,7 @@ static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
 	}
 
 	if (argc == 2 || argc == 3)
-		return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
+		return mmc_partconf_print(mmc, cmd_arg2(argc, argv));
 
 	ack = dectoul(argv[2], NULL);
 	part_num = dectoul(argv[3], NULL);
diff --git a/drivers/misc/gsc.c b/drivers/misc/gsc.c
index 65c9c2c6ce37..feb02f970650 100644
--- a/drivers/misc/gsc.c
+++ b/drivers/misc/gsc.c
@@ -531,10 +531,10 @@ static int do_gsc(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]
 		if (!gsc_wd_disable(dev))
 			return CMD_RET_SUCCESS;
 	} else if (strcasecmp(argv[1], "thermal") == 0) {
-		char *cmd, *val;
+		const char *cmd, *val;
 
-		cmd = (argc > 2) ? argv[2] : NULL;
-		val = (argc > 3) ? argv[3] : NULL;
+		cmd = cmd_arg2(argc, argv);
+		val = cmd_arg3(argc, argv);
 		if (!gsc_thermal(dev, cmd, val))
 			return CMD_RET_SUCCESS;
 	}
diff --git a/fs/fs.c b/fs/fs.c
index 4cb4310c9cc2..f33b85f92b61 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -749,7 +749,7 @@ int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	if (argc > 7)
 		return CMD_RET_USAGE;
 
-	if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
+	if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype)) {
 		log_err("Can't set block device\n");
 		return 1;
 	}
@@ -818,7 +818,7 @@ int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	if (argc > 4)
 		return CMD_RET_USAGE;
 
-	if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
+	if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype))
 		return 1;
 
 	if (fs_ls(argc >= 4 ? argv[3] : "/"))
diff --git a/include/command.h b/include/command.h
index 5bd3ecbe8f91..4cec63454534 100644
--- a/include/command.h
+++ b/include/command.h
@@ -60,6 +60,39 @@ struct cmd_tbl {
 #endif
 };
 
+/**
+ * cmd_arg_get() - Get a particular argument
+ *
+ * @argc: Number of arguments
+ * @argv: Argument vector of length @argc
+ * @argnum: Argument to get (0=first)
+ * Return: Pointer to argument @argnum if it exists, else NULL
+ */
+static inline const char *cmd_arg_get(int argc, char *const argv[], int argnum)
+{
+	return argc > argnum ? argv[argnum] : NULL;
+}
+
+static inline const char *cmd_arg0(int argc, char *const argv[])
+{
+	return cmd_arg_get(argc, argv, 0);
+}
+
+static inline const char *cmd_arg1(int argc, char *const argv[])
+{
+	return cmd_arg_get(argc, argv, 1);
+}
+
+static inline const char *cmd_arg2(int argc, char *const argv[])
+{
+	return cmd_arg_get(argc, argv, 2);
+}
+
+static inline const char *cmd_arg3(int argc, char *const argv[])
+{
+	return cmd_arg_get(argc, argv, 3);
+}
+
 #if defined(CONFIG_CMD_RUN)
 int do_run(struct cmd_tbl *cmdtp, int flag, int argc,
 	   char *const argv[]);
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index 2d5b80f992e3..2ead1c68e0ea 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -45,7 +45,7 @@ int cmd_ut_category(const char *name, const char *prefix,
 	}
 
 	ret = ut_run_list(name, prefix, tests, n_ents,
-			  argc > 1 ? argv[1] : NULL, runs_per_text, force_run,
+			  cmd_arg1(argc, argv), runs_per_text, force_run,
 			  test_insert);
 
 	return ret ? CMD_RET_FAILURE : 0;
-- 
2.43.0.rc0.421.g78406f8d94-goog



More information about the U-Boot mailing list