Commit aa3d60e0 authored by Josh Poimboeuf's avatar Josh Poimboeuf Committed by Peter Zijlstra

libsubcmd: Fix OPTION_GROUP sorting

The OPTION_GROUP option type is a way of grouping certain options
together in the printed usage text.  It happens to be completely broken,
thanks to the fact that the subcmd option sorting just sorts everything,
without regard for grouping.  Luckily, nobody uses this option anyway,
though that will change shortly.

Fix it by sorting each group individually.
Signed-off-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: default avatarMiroslav Benes <mbenes@suse.cz>
Link: https://lkml.kernel.org/r/e167ea3a11e2a9800eb062c1fd0f13e9cd05140c.1650300597.git.jpoimboe@redhat.com
parent 3398b12d
...@@ -806,9 +806,9 @@ static int option__cmp(const void *va, const void *vb) ...@@ -806,9 +806,9 @@ static int option__cmp(const void *va, const void *vb)
static struct option *options__order(const struct option *opts) static struct option *options__order(const struct option *opts)
{ {
int nr_opts = 0, len; int nr_opts = 0, nr_group = 0, len;
const struct option *o = opts; const struct option *o = opts;
struct option *ordered; struct option *opt, *ordered, *group;
for (o = opts; o->type != OPTION_END; o++) for (o = opts; o->type != OPTION_END; o++)
++nr_opts; ++nr_opts;
...@@ -819,7 +819,18 @@ static struct option *options__order(const struct option *opts) ...@@ -819,7 +819,18 @@ static struct option *options__order(const struct option *opts)
goto out; goto out;
memcpy(ordered, opts, len); memcpy(ordered, opts, len);
qsort(ordered, nr_opts, sizeof(*o), option__cmp); /* sort each option group individually */
for (opt = group = ordered; opt->type != OPTION_END; opt++) {
if (opt->type == OPTION_GROUP) {
qsort(group, nr_group, sizeof(*opt), option__cmp);
group = opt + 1;
nr_group = 0;
continue;
}
nr_group++;
}
qsort(group, nr_group, sizeof(*opt), option__cmp);
out: out:
return ordered; return ordered;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment