From 8632b36b96d38a85f2e71603a6f90ec9e4e5e37e Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 18 Nov 2023 14:05:20 -0700 Subject: [PATCH] command: Introduce functions to obtain command arguments 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 Suggested-by: Tom Rini --- 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 07aae26af0..a0d17be774 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 dc73c83f0d..2db8f4a16f 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 bcf232b4f3..a652879ea5 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 3d7bc2f601..92eaa02f4a 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 0676bb7a81..f884c894fb 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 96befb27ee..2d5430a530 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 65c9c2c6ce..feb02f9706 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 4cb4310c9c..f33b85f92b 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 5bd3ecbe8f..4cec634545 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 2d5b80f992..2ead1c68e0 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.39.5