Message ID | 20230525220927.3544192-1-namhyung@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp95703vqr; Thu, 25 May 2023 16:00:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ71lmfrf9WKvMs5UXyHc2mWhzW+JvoHhRC0ZrmBnZHTuqIEzsfqRfB+zPJMtpEh5fhvpSwu X-Received: by 2002:a17:902:a710:b0:19f:3d59:e0ac with SMTP id w16-20020a170902a71000b0019f3d59e0acmr219400plq.44.1685055607782; Thu, 25 May 2023 16:00:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685055607; cv=none; d=google.com; s=arc-20160816; b=qgOV/R9GskEdARASyL5VNkbuLMwZVXqsWVD/3aC+Kxcr10MoLGPfSexLf628zoR8iI dKYuxEGrp+dB/kdOhL+YQ7hC2wTnvQyDwbrK5WN4ka7hvM5HorswJsu5raWZk894s28L FYjfNc+ahs+udxocV2pxRuR37nrwvDp1gQn9n8Tr5Qng6FsG2U1lN30RDRg0YRJdL9cz FrEaktKDY/iMXi8lLJzXswwsqGurcSU+2ACsIF2CMfhfJEet6NU1FP0eLTAYXfbCSPKh p51fGP2FYCAAnp0K9IV3QStuJiLMr6I4A0DkPTlrJJrcQo1/zfG1eb8n75K0PpszCyVf s41g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=9M8nWoD6KWrYcLP+c2xne+17Aj3PiuijAzZBCy4tnBM=; b=QkN5s3WKA97IJDitPheZYxajzMJmb0yzUj792iSZyslxjVjhmE/yL2MDQJmRHORNxk KLwyy6hf8XCjEwp4+WwTJsrcS/RUkvpaj/bZ0INmv+pHbE/AbA5+vEmYj8bDlbBEo0dA Y9BtPO82Ij6tIStK8p2ZLGSR4BzBr7hyJqvVxDT/M8bM9+21VlypeCX3AEzmLUPbJ+Oi ab4UsKQfPNmLvDEivMsCmcagFDn7nv/Z588LbfgYnfzZvXn5ZBiuwF8dAjwDVql8FONb kMh+2aQkxelnmJyN1lOU6vvJzGNfEbzk3BvTrCgp3s/Zy1FBpnPeSiso2R4NZ47/B0MM sPfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=UPuy1ySX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c2-20020a170902d90200b001a9581bfb91si2347370plz.543.2023.05.25.15.59.55; Thu, 25 May 2023 16:00:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=UPuy1ySX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241910AbjEYWJd (ORCPT <rfc822;jian.xie.xdx@gmail.com> + 99 others); Thu, 25 May 2023 18:09:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229931AbjEYWJb (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 25 May 2023 18:09:31 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD21512F; Thu, 25 May 2023 15:09:30 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-53f04fdd77dso30938a12.3; Thu, 25 May 2023 15:09:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685052570; x=1687644570; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=9M8nWoD6KWrYcLP+c2xne+17Aj3PiuijAzZBCy4tnBM=; b=UPuy1ySXcvpF9xExgdv8gW5kMgOc2DwvioFjAxKAKh+6uTzCldGq2K7xENFEspkCu+ qZWDD88GK3bkynGTXLt13sfLNqKlBvkpQgUb9UK3KdYvYShADVw0zOhDJU3qXELSBGFW oUZ6tt9i4w+/Uilw7d7sp+xQUj7z1DtjwAyica3LRQv47gBekQad074dUmna9zEFbOUZ dX9+epKGt6YUAAUABolRNL5OE+uyq+VyLLGPoeZC1/5BZNjGj2gL+bx4C8JXklRvvqLK n6HLZ4v7tCua1qpM2vL5y+WyleIoO8y9ODPU4MT1XXtjYG2p29ICT7CYZwhpxAcRTXRU SrRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685052570; x=1687644570; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=9M8nWoD6KWrYcLP+c2xne+17Aj3PiuijAzZBCy4tnBM=; b=eS+Qor0hLklVu6wEMrJaRgZC3/eWLjGtL5LsFT5zNJsSSIaasQfPWfsUBWFjOrTuQ0 0O+Pd252nPj2yp45dgojF/Zoz2TDu63gfGXMPTGGz8xk+0vma+JXs+0scgDxyZdfSVr/ 8ajRj92MWs1j0vSfFU2JSEB9Y+NWjlX6uMCOOG6Oaze9SlJENiiRQL6EsLZb+3GMtpvw 9vzqIr7gz35K/PpIvVHANVdPnKNaBeuHy8k+lv302bwi7cG+Ek11TcX30u2cMYqcN6SQ a9jdGTxZRi43Er+rzOoHDWU+KW3x7yxUSpJaO/wzUjCTnCjz9aZVFArUyxOlbQyavr5U 7aaw== X-Gm-Message-State: AC+VfDyipTZEKcbNFMtUF8TB9Ffed3qS0c5qHFhCM8Tw4GOAcOunKkp+ 03jOmCMHp51VpMSV6k7btj4= X-Received: by 2002:a17:902:8210:b0:1ae:6882:5bc4 with SMTP id x16-20020a170902821000b001ae68825bc4mr104977pln.64.1685052570159; Thu, 25 May 2023 15:09:30 -0700 (PDT) Received: from moohyul.svl.corp.google.com ([2620:15c:2d4:203:7217:977f:12fc:72e0]) by smtp.gmail.com with ESMTPSA id x6-20020a170902ea8600b001a6ed2d0ef8sm1851390plb.273.2023.05.25.15.09.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 May 2023 15:09:29 -0700 (PDT) Sender: Namhyung Kim <namhyung@gmail.com> From: Namhyung Kim <namhyung@kernel.org> To: Arnaldo Carvalho de Melo <acme@kernel.org>, Jiri Olsa <jolsa@kernel.org> Cc: Ian Rogers <irogers@google.com>, Adrian Hunter <adrian.hunter@intel.com>, Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@kernel.org>, LKML <linux-kernel@vger.kernel.org>, linux-perf-users@vger.kernel.org Subject: [PATCH] tools lib subcmd: Show parent options in help Date: Thu, 25 May 2023 15:09:27 -0700 Message-ID: <20230525220927.3544192-1-namhyung@kernel.org> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766908868908546272?= X-GMAIL-MSGID: =?utf-8?q?1766908868908546272?= |
Series |
tools lib subcmd: Show parent options in help
|
|
Commit Message
Namhyung Kim
May 25, 2023, 10:09 p.m. UTC
I've just realized that help message in a subcommand didn't show one
in the parent command. Since the option parser understands the parent,
display code should do the same. For example, `perf ftrace latency -h`
should show options in the `perf ftrace` command too.
Before:
$ perf ftrace latency -h
Usage: perf ftrace [<options>] [<command>]
or: perf ftrace [<options>] -- [<command>] [<options>]
or: perf ftrace {trace|latency} [<options>] [<command>]
or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
-b, --use-bpf Use BPF to measure function latency
-n, --use-nsec Use nano-second histogram
-T, --trace-funcs <func>
Show latency of given function
After:
$ perf ftrace latency -h
Usage: perf ftrace [<options>] [<command>]
or: perf ftrace [<options>] -- [<command>] [<options>]
or: perf ftrace {trace|latency} [<options>] [<command>]
or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
-a, --all-cpus System-wide collection from all CPUs
-b, --use-bpf Use BPF to measure function latency
-C, --cpu <cpu> List of cpus to monitor
-n, --use-nsec Use nano-second histogram
-p, --pid <pid> Trace on existing process id
-T, --trace-funcs <func>
Show latency of given function
-v, --verbose Be more verbose
--tid <tid> Trace on existing thread id (exclusive to --pid)
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)
Comments
On Thu, May 25, 2023 at 3:09 PM Namhyung Kim <namhyung@kernel.org> wrote: > > I've just realized that help message in a subcommand didn't show one > in the parent command. Since the option parser understands the parent, > display code should do the same. For example, `perf ftrace latency -h` > should show options in the `perf ftrace` command too. > > Before: > > $ perf ftrace latency -h > > Usage: perf ftrace [<options>] [<command>] > or: perf ftrace [<options>] -- [<command>] [<options>] > or: perf ftrace {trace|latency} [<options>] [<command>] > or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] > > -b, --use-bpf Use BPF to measure function latency > -n, --use-nsec Use nano-second histogram > -T, --trace-funcs <func> > Show latency of given function > > After: > > $ perf ftrace latency -h > > Usage: perf ftrace [<options>] [<command>] > or: perf ftrace [<options>] -- [<command>] [<options>] > or: perf ftrace {trace|latency} [<options>] [<command>] > or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] > > -a, --all-cpus System-wide collection from all CPUs > -b, --use-bpf Use BPF to measure function latency > -C, --cpu <cpu> List of cpus to monitor > -n, --use-nsec Use nano-second histogram > -p, --pid <pid> Trace on existing process id > -T, --trace-funcs <func> > Show latency of given function > -v, --verbose Be more verbose > --tid <tid> Trace on existing thread id (exclusive to --pid) > > Signed-off-by: Namhyung Kim <namhyung@kernel.org> > --- > tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++-------- > 1 file changed, 18 insertions(+), 8 deletions(-) > > diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c > index 9fa75943f2ed..41de97671c72 100644 > --- a/tools/lib/subcmd/parse-options.c > +++ b/tools/lib/subcmd/parse-options.c > @@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb) > > static struct option *options__order(const struct option *opts) > { > - int nr_opts = 0, nr_group = 0, len; > - const struct option *o = opts; > - struct option *opt, *ordered, *group; > + int nr_opts = 0, nr_group = 0, nr_parent = 0, len; > + const struct option *o, *p = opts; > + struct option *opt, *ordered = NULL, *group; > > - for (o = opts; o->type != OPTION_END; o++) > +retry: Why use "goto retry" and not compute the size with the parent upfront? > + for (o = p; o->type != OPTION_END; o++) > ++nr_opts; > > - len = sizeof(*o) * (nr_opts + 1); > - ordered = malloc(len); > - if (!ordered) > + len = sizeof(*o) * (nr_opts + !o->parent); It'd be nice to comment on why the "!o->parent" here. Thanks, Ian > + group = realloc(ordered, len); > + if (!group) > goto out; > - memcpy(ordered, opts, len); > + ordered = group; > + memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent)); > + > + if (o->parent) { > + p = o->parent; > + nr_parent = nr_opts; > + goto retry; > + } > + /* copy the last OPTION_END */ > + memcpy(&ordered[nr_opts], o, sizeof(*o)); > > /* sort each option group individually */ > for (opt = group = ordered; opt->type != OPTION_END; opt++) { > -- > 2.41.0.rc0.172.g3f132b7071-goog >
On Wed, Jun 21, 2023 at 12:05 AM Ian Rogers <irogers@google.com> wrote: > > On Thu, May 25, 2023 at 3:09 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > I've just realized that help message in a subcommand didn't show one > > in the parent command. Since the option parser understands the parent, > > display code should do the same. For example, `perf ftrace latency -h` > > should show options in the `perf ftrace` command too. > > > > Before: > > > > $ perf ftrace latency -h > > > > Usage: perf ftrace [<options>] [<command>] > > or: perf ftrace [<options>] -- [<command>] [<options>] > > or: perf ftrace {trace|latency} [<options>] [<command>] > > or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] > > > > -b, --use-bpf Use BPF to measure function latency > > -n, --use-nsec Use nano-second histogram > > -T, --trace-funcs <func> > > Show latency of given function > > > > After: > > > > $ perf ftrace latency -h > > > > Usage: perf ftrace [<options>] [<command>] > > or: perf ftrace [<options>] -- [<command>] [<options>] > > or: perf ftrace {trace|latency} [<options>] [<command>] > > or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] > > > > -a, --all-cpus System-wide collection from all CPUs > > -b, --use-bpf Use BPF to measure function latency > > -C, --cpu <cpu> List of cpus to monitor > > -n, --use-nsec Use nano-second histogram > > -p, --pid <pid> Trace on existing process id > > -T, --trace-funcs <func> > > Show latency of given function > > -v, --verbose Be more verbose > > --tid <tid> Trace on existing thread id (exclusive to --pid) > > > > Signed-off-by: Namhyung Kim <namhyung@kernel.org> > > --- > > tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++-------- > > 1 file changed, 18 insertions(+), 8 deletions(-) > > > > diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c > > index 9fa75943f2ed..41de97671c72 100644 > > --- a/tools/lib/subcmd/parse-options.c > > +++ b/tools/lib/subcmd/parse-options.c > > @@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb) > > > > static struct option *options__order(const struct option *opts) > > { > > - int nr_opts = 0, nr_group = 0, len; > > - const struct option *o = opts; > > - struct option *opt, *ordered, *group; > > + int nr_opts = 0, nr_group = 0, nr_parent = 0, len; > > + const struct option *o, *p = opts; > > + struct option *opt, *ordered = NULL, *group; > > > > - for (o = opts; o->type != OPTION_END; o++) > > +retry: > > Why use "goto retry" and not compute the size with the parent upfront? No specific reason, just followed the same pattern as in parse_{short,long}_opt(). :) > > > + for (o = p; o->type != OPTION_END; o++) > > ++nr_opts; > > > > - len = sizeof(*o) * (nr_opts + 1); > > - ordered = malloc(len); > > - if (!ordered) > > + len = sizeof(*o) * (nr_opts + !o->parent); > > It'd be nice to comment on why the "!o->parent" here. What about this? /* It needs a terminating NULL entry when there's no parent */ Thanks, Namhyung > > Thanks, > Ian > > > + group = realloc(ordered, len); > > + if (!group) > > goto out; > > - memcpy(ordered, opts, len); > > + ordered = group; > > + memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent)); > > + > > + if (o->parent) { > > + p = o->parent; > > + nr_parent = nr_opts; > > + goto retry; > > + } > > + /* copy the last OPTION_END */ > > + memcpy(&ordered[nr_opts], o, sizeof(*o)); > > > > /* sort each option group individually */ > > for (opt = group = ordered; opt->type != OPTION_END; opt++) { > > -- > > 2.41.0.rc0.172.g3f132b7071-goog > >
On Thu, Jun 22, 2023 at 3:28 PM Namhyung Kim <namhyung@kernel.org> wrote: > > On Wed, Jun 21, 2023 at 12:05 AM Ian Rogers <irogers@google.com> wrote: > > > > On Thu, May 25, 2023 at 3:09 PM Namhyung Kim <namhyung@kernel.org> wrote: > > > > > > I've just realized that help message in a subcommand didn't show one > > > in the parent command. Since the option parser understands the parent, > > > display code should do the same. For example, `perf ftrace latency -h` > > > should show options in the `perf ftrace` command too. > > > > > > Before: > > > > > > $ perf ftrace latency -h > > > > > > Usage: perf ftrace [<options>] [<command>] > > > or: perf ftrace [<options>] -- [<command>] [<options>] > > > or: perf ftrace {trace|latency} [<options>] [<command>] > > > or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] > > > > > > -b, --use-bpf Use BPF to measure function latency > > > -n, --use-nsec Use nano-second histogram > > > -T, --trace-funcs <func> > > > Show latency of given function > > > > > > After: > > > > > > $ perf ftrace latency -h > > > > > > Usage: perf ftrace [<options>] [<command>] > > > or: perf ftrace [<options>] -- [<command>] [<options>] > > > or: perf ftrace {trace|latency} [<options>] [<command>] > > > or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>] > > > > > > -a, --all-cpus System-wide collection from all CPUs > > > -b, --use-bpf Use BPF to measure function latency > > > -C, --cpu <cpu> List of cpus to monitor > > > -n, --use-nsec Use nano-second histogram > > > -p, --pid <pid> Trace on existing process id > > > -T, --trace-funcs <func> > > > Show latency of given function > > > -v, --verbose Be more verbose > > > --tid <tid> Trace on existing thread id (exclusive to --pid) > > > > > > Signed-off-by: Namhyung Kim <namhyung@kernel.org> > > > --- > > > tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++-------- > > > 1 file changed, 18 insertions(+), 8 deletions(-) > > > > > > diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c > > > index 9fa75943f2ed..41de97671c72 100644 > > > --- a/tools/lib/subcmd/parse-options.c > > > +++ b/tools/lib/subcmd/parse-options.c > > > @@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb) > > > > > > static struct option *options__order(const struct option *opts) > > > { > > > - int nr_opts = 0, nr_group = 0, len; > > > - const struct option *o = opts; > > > - struct option *opt, *ordered, *group; > > > + int nr_opts = 0, nr_group = 0, nr_parent = 0, len; > > > + const struct option *o, *p = opts; > > > + struct option *opt, *ordered = NULL, *group; > > > > > > - for (o = opts; o->type != OPTION_END; o++) > > > +retry: > > > > Why use "goto retry" and not compute the size with the parent upfront? > > No specific reason, just followed the same pattern as in > parse_{short,long}_opt(). :) :-) Could those loops not be: for (; options; options = options->parent) { ... } rather than using a goto? > > > > > + for (o = p; o->type != OPTION_END; o++) > > > ++nr_opts; > > > > > > - len = sizeof(*o) * (nr_opts + 1); > > > - ordered = malloc(len); > > > - if (!ordered) > > > + len = sizeof(*o) * (nr_opts + !o->parent); > > > > It'd be nice to comment on why the "!o->parent" here. > > What about this? > > /* It needs a terminating NULL entry when there's no parent */ Perhaps: the length is given by the number of options plus a null terminator for the last loop iteration. It may be cleaner to just compute this after nr_opts settles. perhaps something along the lines of: for (p = .. ; p; p = p->parent) for (o = p; o->type != OPTION_END; o++) ++nr_opts; len = sizeof(*o) * (nr_opts + 1); /* +1 for terminator */ Thanks, Ian > Thanks, > Namhyung > > > > > > Thanks, > > Ian > > > > > + group = realloc(ordered, len); > > > + if (!group) > > > goto out; > > > - memcpy(ordered, opts, len); > > > + ordered = group; > > > + memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent)); > > > + > > > + if (o->parent) { > > > + p = o->parent; > > > + nr_parent = nr_opts; > > > + goto retry; > > > + } > > > + /* copy the last OPTION_END */ > > > + memcpy(&ordered[nr_opts], o, sizeof(*o)); > > > > > > /* sort each option group individually */ > > > for (opt = group = ordered; opt->type != OPTION_END; opt++) { > > > -- > > > 2.41.0.rc0.172.g3f132b7071-goog > > >
diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c index 9fa75943f2ed..41de97671c72 100644 --- a/tools/lib/subcmd/parse-options.c +++ b/tools/lib/subcmd/parse-options.c @@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb) static struct option *options__order(const struct option *opts) { - int nr_opts = 0, nr_group = 0, len; - const struct option *o = opts; - struct option *opt, *ordered, *group; + int nr_opts = 0, nr_group = 0, nr_parent = 0, len; + const struct option *o, *p = opts; + struct option *opt, *ordered = NULL, *group; - for (o = opts; o->type != OPTION_END; o++) +retry: + for (o = p; o->type != OPTION_END; o++) ++nr_opts; - len = sizeof(*o) * (nr_opts + 1); - ordered = malloc(len); - if (!ordered) + len = sizeof(*o) * (nr_opts + !o->parent); + group = realloc(ordered, len); + if (!group) goto out; - memcpy(ordered, opts, len); + ordered = group; + memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent)); + + if (o->parent) { + p = o->parent; + nr_parent = nr_opts; + goto retry; + } + /* copy the last OPTION_END */ + memcpy(&ordered[nr_opts], o, sizeof(*o)); /* sort each option group individually */ for (opt = group = ordered; opt->type != OPTION_END; opt++) {