From patchwork Tue Oct 11 07:16:12 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 118869 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DF1E1B6F70 for ; Tue, 11 Oct 2011 18:17:43 +1100 (EST) Received: from localhost ([::1]:35486 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RDWap-0000jn-SS for incoming@patchwork.ozlabs.org; Tue, 11 Oct 2011 03:17:39 -0400 Received: from eggs.gnu.org ([140.186.70.92]:57294) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RDWak-0000j3-5O for qemu-devel@nongnu.org; Tue, 11 Oct 2011 03:17:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RDWai-0001fm-Py for qemu-devel@nongnu.org; Tue, 11 Oct 2011 03:17:34 -0400 Received: from e23smtp09.au.ibm.com ([202.81.31.142]:37581) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RDWai-0001fH-8I for qemu-devel@nongnu.org; Tue, 11 Oct 2011 03:17:32 -0400 Received: from /spool/local by au.ibm.com with XMail ESMTP for from ; Tue, 11 Oct 2011 08:12:48 +1000 Received: from d23relay04.au.ibm.com ([202.81.31.246]) by au.ibm.com ([202.81.31.206]) with XMail ESMTP; Tue, 11 Oct 2011 08:12:46 +1000 Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay04.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p9B7EsLr1658986 for ; Tue, 11 Oct 2011 18:14:55 +1100 Received: from d23av04.au.ibm.com (loopback [127.0.0.1]) by d23av04.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p9B7HDlW031388 for ; Tue, 11 Oct 2011 18:17:14 +1100 Received: from Wayne-Libvirt-cim-VM.cn.ibm.com ([9.115.127.37]) by d23av04.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p9B7H9as030355; Tue, 11 Oct 2011 18:17:12 +1100 From: Wayne Xia To: qemu-devel@nongnu.org Date: Tue, 11 Oct 2011 15:16:12 +0800 Message-Id: <1318317372-6604-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.6 x-cbid: 11101022-3568-0000-0000-000000814BBF X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 202.81.31.142 Cc: Wayne Xia Subject: [Qemu-devel] [PATCH v2] Sort the help info shown in monitor at runtime X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Introduced two queues to save sorted command list in it. As a result, command help and help info would show a more friendly sorted command list. For eg: (qemu)help acl_add acl_policy acl_remove acl_reset acl_show balloon block_passwd ... the command list is sorted. v2: write sorted command list back to original array. Signed-off-by: Wayne Xia --- monitor.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 109 insertions(+), 4 deletions(-) diff --git a/monitor.c b/monitor.c index 31b212a..122f950 100644 --- a/monitor.c +++ b/monitor.c @@ -66,6 +66,7 @@ #include "memory.h" #include "qmp-commands.h" #include "hmp.h" +#include "qemu-queue.h" //#define DEBUG //#define DEBUG_COMPLETION @@ -128,6 +129,18 @@ typedef struct mon_cmd_t { int flags; } mon_cmd_t; +/* queue structure used for runtime sorting */ +struct HelpCmdElem { + QTAILQ_ENTRY(HelpCmdElem) entry; + mon_cmd_t cmd_copy; +}; + +typedef struct HelpCmdElem HelpCmdElem; + +QTAILQ_HEAD(HelpCmdQueue, HelpCmdElem); + +typedef struct HelpCmdQueue HelpCmdQueue; + /* file descriptors passed via SCM_RIGHTS */ typedef struct mon_fd_t mon_fd_t; struct mon_fd_t { @@ -195,8 +208,8 @@ static inline int mon_print_count_get(const Monitor *mon) { return 0; } static QLIST_HEAD(mon_list, Monitor) mon_list; -static const mon_cmd_t mon_cmds[]; -static const mon_cmd_t info_cmds[]; +static mon_cmd_t mon_cmds[]; +static mon_cmd_t info_cmds[]; static const mon_cmd_t qmp_cmds[]; static const mon_cmd_t qmp_query_cmds[]; @@ -2726,13 +2739,14 @@ int monitor_get_fd(Monitor *mon, const char *fdname) return -1; } -static const mon_cmd_t mon_cmds[] = { +/* mon_cmds and info_cmds would be sorted at runtime */ +static mon_cmd_t mon_cmds[] = { #include "hmp-commands.h" { NULL, NULL, }, }; /* Please update hmp-commands.hx when adding or changing commands */ -static const mon_cmd_t info_cmds[] = { +static mon_cmd_t info_cmds[] = { { .name = "version", .args_type = "", @@ -5068,6 +5082,94 @@ static void monitor_event(void *opaque, int event) } } +static int cmdlist_sortto_queue(const mon_cmd_t *cmdlist, + HelpCmdQueue **pqueue) +{ + const mon_cmd_t *cmd = NULL; + HelpCmdElem *elem = NULL; + HelpCmdElem *newelem = NULL; + HelpCmdElem *next = NULL; + HelpCmdQueue *cmdqueue = NULL; + int cmpret1, cmpret2; + + cmdqueue = g_malloc(sizeof(HelpCmdQueue)); + if (cmdqueue == NULL) { + return -1; + } + + for (cmd = cmdlist; cmd->name != NULL; cmd++) { + newelem = NULL; + newelem = g_malloc(sizeof(HelpCmdElem)); + if (newelem == NULL) { + return -1; + } + newelem->cmd_copy = *cmd; + + if (QTAILQ_EMPTY(cmdqueue)) { + QTAILQ_INSERT_HEAD(cmdqueue, newelem, entry); + continue; + } + QTAILQ_FOREACH_SAFE(elem, cmdqueue, entry, next) { + /* search for proper postion */ + cmpret1 = strcmp(cmd->name, elem->cmd_copy.name); + if (next == NULL) { + if (cmpret1 >= 0) { + QTAILQ_INSERT_TAIL(cmdqueue, newelem, entry); + } else { + QTAILQ_INSERT_HEAD(cmdqueue, newelem, entry); + } + break; + } else { + cmpret2 = strcmp(cmd->name, next->cmd_copy.name); + } + if ((cmpret1 >= 0) && (cmpret2 <= 0)) { + QTAILQ_INSERT_AFTER(cmdqueue, elem, newelem, entry); + break; + } + } + } + *pqueue = cmdqueue; + return 1; +} + +static void queue_to_cmdlist(HelpCmdQueue **pqueue, + mon_cmd_t *cmdlist) +{ + HelpCmdElem *elem = NULL; + HelpCmdElem *next = NULL; + HelpCmdQueue *cmdqueue = *pqueue; + mon_cmd_t *pcmdlist = cmdlist; + + QTAILQ_FOREACH_SAFE(elem, cmdqueue, entry, next) { + *(pcmdlist++) = elem->cmd_copy; + QTAILQ_REMOVE(cmdqueue, elem, entry); + g_free(elem); + } + g_free(cmdqueue); + *pqueue = NULL; +} + +static void sortcmdlist(void) +{ + HelpCmdQueue *mon_cmds_queue = NULL; + HelpCmdQueue *info_cmds_queue = NULL; + int ret; + + ret = cmdlist_sortto_queue(mon_cmds, &mon_cmds_queue); + if (ret < 0) { + printf("error in initilize mon cmd queue, return is %d.\n", ret); + return; + } + queue_to_cmdlist(&mon_cmds_queue, mon_cmds); + + ret = cmdlist_sortto_queue(info_cmds, &info_cmds_queue); + if (ret < 0) { + printf("error in initilize info cmd queue, return is %d.\n", ret); + return; + } + queue_to_cmdlist(&info_cmds_queue, info_cmds); +} + /* * Local variables: @@ -5110,6 +5212,9 @@ void monitor_init(CharDriverState *chr, int flags) QLIST_INSERT_HEAD(&mon_list, mon, entry); if (!default_mon || (flags & MONITOR_IS_DEFAULT)) default_mon = mon; + + sortcmdlist(); + } static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)