qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Mike Day <ncmike@ncultra.org>
To: qemu-devel@nongnu.org
Cc: Mike Day <ncmike@ncultra.org>,
	kwolf@redhat.com, cornelia.huck@de.ibm.com, stefanha@redhat.com,
	peter.maydell@linaro.org
Subject: [Qemu-devel] [PATCH] qemu-img: sort block formats in help message
Date: Tue, 13 May 2014 12:07:48 -0400	[thread overview]
Message-ID: <1399997268-9889-1-git-send-email-ncmike@ncultra.org> (raw)

The help message for qemu-img lists the supported block formats, of
which there are 27 as of version 2.0.50. The formats are printed in
the order of their driver's position in a linked list, which appears
random. This patch prints the formats in sorted order, making it
easier to read and to find a specific format in the list.

[Added suggestions from Fam Zheng <famz@redhat.com> to declare variables
at the top of the scope in help() and to omit explicit cast for void*
opaque.
--Stefan]

[Removed call to g_sequence_lookup because it breaks the build on
 machines with glib < 2.28.
--Mike]

Signed-off-by: Mike Day <ncmike@ncultra.org>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 qemu-img.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 96f4463..93e51d1 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -32,6 +32,7 @@
 #include "block/block_int.h"
 #include "block/qapi.h"
 #include <getopt.h>
+#include <glib.h>
 
 #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \
                           ", Copyright (c) 2004-2008 Fabrice Bellard\n"
@@ -55,9 +56,22 @@ typedef enum OutputFormat {
 #define BDRV_O_FLAGS BDRV_O_CACHE_WB
 #define BDRV_DEFAULT_CACHE "writeback"
 
-static void format_print(void *opaque, const char *name)
+static gint compare_data(gconstpointer a, gconstpointer b, gpointer user)
 {
-    printf(" %s", name);
+    return g_strcmp0(a, b);
+}
+
+static void print_format(gpointer data, gpointer user)
+{
+    printf(" %s", (char *)data);
+}
+
+static void add_format_to_seq(void *opaque, const char *fmt_name)
+{
+    GSequence *seq = opaque;
+
+    g_sequence_insert_sorted(seq, (gpointer)fmt_name,
+                             compare_data, NULL);
 }
 
 static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
@@ -142,10 +156,15 @@ static void QEMU_NORETURN help(void)
            "  '-f' first image format\n"
            "  '-F' second image format\n"
            "  '-s' run in Strict mode - fail on different image size or sector allocation\n";
+    GSequence *seq;
 
     printf("%s\nSupported formats:", help_msg);
-    bdrv_iterate_format(format_print, NULL);
+    seq = g_sequence_new(NULL);
+    bdrv_iterate_format(add_format_to_seq, seq);
+    g_sequence_foreach(seq, print_format, NULL);
     printf("\n");
+    g_sequence_free(seq);
+
     exit(EXIT_SUCCESS);
 }
 
-- 
1.9.0

             reply	other threads:[~2014-05-13 16:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-13 16:07 Mike Day [this message]
2014-05-13 17:48 ` [Qemu-devel] [PATCH] qemu-img: sort block formats in help message Markus Armbruster
2014-05-14  7:50 ` Kevin Wolf
2014-05-14  8:09   ` Fam Zheng
2014-05-14 13:02     ` Stefan Hajnoczi
2014-05-14 13:28       ` Mike Day
  -- strict thread matches above, loose matches on Subject: below --
2014-05-02 13:11 Mike Day
2014-05-05 13:42 ` Stefan Hajnoczi
2014-05-05 16:02   ` Mike Day

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1399997268-9889-1-git-send-email-ncmike@ncultra.org \
    --to=ncmike@ncultra.org \
    --cc=cornelia.huck@de.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).