From patchwork Sun Jan 12 19:05:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 239492 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Sun, 12 Jan 2020 12:05:52 -0700 Subject: [PATCH 01/33] sandbox: Sort the help options In-Reply-To: <20200112190624.79077-1-sjg@chromium.org> References: <20200112190624.79077-1-sjg@chromium.org> Message-ID: <20200112120216.1.Ia614e51f23d2db29603dc516a3413c4855c93eb7@changeid> At present options are presented in essentially random order. It is easier to browse them if they are sorted into alphabetical order. Adjust the help function to handle this. Signed-off-by: Simon Glass --- arch/sandbox/cpu/start.c | 46 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index fff9cbdd79..d3ce61856e 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -9,13 +9,45 @@ #include #include #include +#include #include #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; +/* Compare two options so that they can be sorted into alphabetical order */ +static int h_compare_opt(const void *p1, const void *p2) +{ + const struct sandbox_cmdline_option *opt1 = p1; + const struct sandbox_cmdline_option *opt2 = p2; + const char *str1, *str2; + char flag1[2], flag2[2]; + + opt1 = *(struct sandbox_cmdline_option **)p1; + opt2 = *(struct sandbox_cmdline_option **)p2; + flag1[1] = '\0'; + flag2[1] = '\0'; + + *flag1 = opt1->flag_short < 0x100 ? opt1->flag_short : '\0'; + *flag2 = opt2->flag_short < 0x100 ? opt2->flag_short : '\0'; + + str1 = *flag1 ? flag1 : opt1->flag; + str2 = *flag2 ? flag2 : opt2->flag; + + /* + * Force lower-case flags to come before upper-case ones. We only + * support upper-case for short flags. + */ + if (isalpha(*str1) && isalpha(*str2) && + tolower(*str1) == tolower(*str2)) + return isupper(*str1) - isupper(*str2); + + return strcasecmp(str1, str2); +} + int sandbox_early_getopt_check(void) { struct sandbox_state *state = state_get_current(); @@ -23,6 +55,8 @@ int sandbox_early_getopt_check(void) size_t num_options = __u_boot_sandbox_option_count(); size_t i; int max_arg_len, max_noarg_len; + struct sandbox_cmdline_option **sorted_opt; + int size; /* parse_err will be a string of the faulting option */ if (!state->parse_err) @@ -45,8 +79,18 @@ int sandbox_early_getopt_check(void) max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len); max_noarg_len = max_arg_len + 7; + /* Sort the options */ + size = sizeof(*sorted_opt) * num_options; + sorted_opt = malloc(size); + if (!sorted_opt) { + printf("No memory to sort options\n"); + os_exit(1); + } + memcpy(sorted_opt, sb_opt, size); + qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt); + for (i = 0; i < num_options; ++i) { - struct sandbox_cmdline_option *opt = sb_opt[i]; + struct sandbox_cmdline_option *opt = sorted_opt[i]; /* first output the short flag if it has one */ if (opt->flag_short >= 0x100)