From patchwork Wed Sep 28 09:01:18 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 116725 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 0A432B6F7F for ; Wed, 28 Sep 2011 19:04:41 +1000 (EST) Received: from localhost ([::1]:45502 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R8q4A-0007q4-4q for incoming@patchwork.ozlabs.org; Wed, 28 Sep 2011 05:04:34 -0400 Received: from eggs.gnu.org ([140.186.70.92]:48621) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R8q3z-0007oG-RP for qemu-devel@nongnu.org; Wed, 28 Sep 2011 05:04:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1R8q3x-0005gu-Sn for qemu-devel@nongnu.org; Wed, 28 Sep 2011 05:04:23 -0400 Received: from e28smtp07.in.ibm.com ([122.248.162.7]:41078) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1R8q3w-0005fj-Pk for qemu-devel@nongnu.org; Wed, 28 Sep 2011 05:04:21 -0400 Received: from d28relay05.in.ibm.com (d28relay05.in.ibm.com [9.184.220.62]) by e28smtp07.in.ibm.com (8.14.4/8.13.1) with ESMTP id p8S94Eeq012916 for ; Wed, 28 Sep 2011 14:34:14 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay05.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p8S94CK81572988 for ; Wed, 28 Sep 2011 14:34:12 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p8SJWWRW002616 for ; Thu, 29 Sep 2011 01:02:33 +0530 Received: from Wayne-Libvirt-cim-VM.cn.ibm.com ([9.115.126.120]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p8SJWU4t001609; Thu, 29 Sep 2011 01:02:31 +0530 From: Wayne Xia To: qemu-devel@nongnu.org Date: Wed, 28 Sep 2011 17:01:18 +0800 Message-Id: <1317200478-5129-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.6 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 122.248.162.7 Cc: Wayne Xia Subject: [Qemu-devel] [PATCH 1/1] Sort the help info shown in monitor 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, and dump commands from them. As a result, command help and help info would show a more friendly sorted command list. Signed-off-by: Wayne Xia --- monitor.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 93 insertions(+), 4 deletions(-) diff --git a/monitor.c b/monitor.c index 8ec2c5e..88938d6 100644 --- a/monitor.c +++ b/monitor.c @@ -63,6 +63,7 @@ #endif #include "trace/control.h" #include "ui/qemu-spice.h" +#include "qemu-queue.h" //#define DEBUG //#define DEBUG_COMPLETION @@ -124,6 +125,17 @@ typedef struct mon_cmd_t { int flags; } mon_cmd_t; +struct HelpCmdElem { + QTAILQ_ENTRY(HelpCmdElem) entry; + const mon_cmd_t *cmd; +}; + +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 { @@ -193,6 +205,8 @@ static QLIST_HEAD(mon_list, Monitor) mon_list; static const mon_cmd_t mon_cmds[]; static const mon_cmd_t info_cmds[]; +static HelpCmdQueue *mon_cmds_queue; +static HelpCmdQueue *info_cmds_queue; static const mon_cmd_t qmp_cmds[]; static const mon_cmd_t qmp_query_cmds[]; @@ -560,12 +574,15 @@ static int compare_cmd(const char *name, const char *list) return 0; } -static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, +static void help_cmd_dump_queue(Monitor *mon, const HelpCmdQueue *queue, const char *prefix, const char *name) { const mon_cmd_t *cmd; + HelpCmdElem *elem = NULL; + HelpCmdElem *next = NULL; - for(cmd = cmds; cmd->name != NULL; cmd++) { + QTAILQ_FOREACH_SAFE(elem, queue, entry, next) { + cmd = elem->cmd; if (!name || !strcmp(name, cmd->name)) monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help); @@ -575,9 +592,9 @@ static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, static void help_cmd(Monitor *mon, const char *name) { if (name && !strcmp(name, "info")) { - help_cmd_dump(mon, info_cmds, "info ", NULL); + help_cmd_dump_queue(mon, info_cmds_queue, "info ", NULL); } else { - help_cmd_dump(mon, mon_cmds, "", name); + help_cmd_dump_queue(mon, mon_cmds_queue, "", name); if (name && !strcmp(name, "log")) { const CPULogItem *item; monitor_printf(mon, "Log items (comma separated):\n"); @@ -5275,6 +5292,76 @@ static void monitor_event(void *opaque, int event) * End: */ +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; + + if (*pqueue != NULL) { + /* queue already allocated. + because the cmd contents would not change when reboot, so skip */ + return 0; + } + cmdqueue = g_malloc(sizeof(HelpCmdQueue)); + if (cmdqueue == NULL) { + return -1; + } + QTAILQ_INIT(cmdqueue); + + for (cmd = cmdlist; cmd->name != NULL; cmd++) { + newelem = NULL; + newelem = g_malloc(sizeof(HelpCmdElem)); + if (newelem == NULL) { + return -1; + } + newelem->cmd = 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->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->name); + } + if ((cmpret1 >= 0) && (cmpret2 <= 0)) { + QTAILQ_INSERT_AFTER(cmdqueue, elem, newelem, entry); + break; + } + } + } + *pqueue = cmdqueue; + return 1; +} + +static int cmd_queue_init(void) +{ + 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); + } + 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 ret; +} + void monitor_init(CharDriverState *chr, int flags) { static int is_first_init = 1; @@ -5308,6 +5395,8 @@ void monitor_init(CharDriverState *chr, int flags) QLIST_INSERT_HEAD(&mon_list, mon, entry); if (!default_mon || (flags & MONITOR_IS_DEFAULT)) default_mon = mon; + + cmd_queue_init(); } static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)