From patchwork Fri Apr 8 13:15:36 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prasad Joshi X-Patchwork-Id: 694541 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p38DFhJv005958 for ; Fri, 8 Apr 2011 13:15:43 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756713Ab1DHNPh (ORCPT ); Fri, 8 Apr 2011 09:15:37 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:56873 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756666Ab1DHNPg (ORCPT ); Fri, 8 Apr 2011 09:15:36 -0400 Received: by wya21 with SMTP id 21so3082170wya.19 for ; Fri, 08 Apr 2011 06:15:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=dGs3omcdCtBgrAcojepA2xdmXz63Dwvr9KPZ96+5nY8=; b=d0IkJt1aSj4oDLk7GvapkJ2A20iu+XWEHHvFVrOR0BQDLpoLgYl2uzQV0deHDGzeSC nutoTtPPdIhloK45qOqKr6IJ4txM2kRO2zKb4NPlWQj6klpz3YeISAWAjrqKn3UocZXI RIZcemJPJ8D2krPs2nB2e6/2RDOsaBS9SI2oQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=kuJOKU9UI727Aws2SXd0OV6lIzhiEQsAGOudWJsZOIwgNmgfFY1+DbwANanY0pXPIX vzWR3ieViUxqCw9HI75oD47j9Du6PGMCkuqAUH+le8hg6nfy34xak5tx7YqoP8IIljWe FX9FyPN9V3l60PDuoJeON8rwFwtRqxA9UMjPo= Received: by 10.227.168.132 with SMTP id u4mr1606743wby.50.1302268535176; Fri, 08 Apr 2011 06:15:35 -0700 (PDT) Received: from prasad-kvm.localdomain (pineapple.rdg.ac.uk [134.225.206.123]) by mx.google.com with ESMTPS id bs4sm1704173wbb.18.2011.04.08.06.15.32 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 08 Apr 2011 06:15:33 -0700 (PDT) Received: by prasad-kvm.localdomain (Postfix, from userid 1000) id 74ED926E006F; Fri, 8 Apr 2011 14:15:38 +0100 (BST) From: Prasad Joshi To: prasadjoshi12@gmail.com Cc: Prasad Joshi , mingo@elte.hu, kvm@vger.kernel.org, penberg@kernel.org, asias.hejun@gmail.com, gorcunov@gmail.com, oswaldo.cadenas@gmail.com Subject: [PATCH v2 kvm tool: 3/4] Provides the basic Gitish framework Date: Fri, 8 Apr 2011 14:15:36 +0100 Message-Id: <1302268537-10067-3-git-send-email-prasadjoshi124@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1302268537-10067-1-git-send-email-prasadjoshi124@gmail.com> References: <1302268537-10067-1-git-send-email-prasadjoshi124@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Fri, 08 Apr 2011 13:15:43 +0000 (UTC) - kvm-cmd.h: Adds a new structure cmd_struct to create a table of commands and callback function. The structure was copied from tools/perf - kvm-cmd.c: implements two main functions for command processing. kvm_get_command(): searches table for specific command. handle_command(): invokes the callback function for a given command. - kvm-help.[ch] Implements the kvm help command. The function list_common_cmds_help() is a copy of similar function in tools/perf. Signed-off-by: Prasad Joshi --- tools/kvm/include/kvm/kvm-cmd.h | 12 ++++++++ tools/kvm/include/kvm/kvm-help.h | 6 ++++ tools/kvm/kvm-cmd.c | 55 ++++++++++++++++++++++++++++++++++++++ tools/kvm/kvm-help.c | 43 +++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 0 deletions(-) create mode 100644 tools/kvm/include/kvm/kvm-cmd.h create mode 100644 tools/kvm/include/kvm/kvm-help.h create mode 100644 tools/kvm/kvm-cmd.c create mode 100644 tools/kvm/kvm-help.c diff --git a/tools/kvm/include/kvm/kvm-cmd.h b/tools/kvm/include/kvm/kvm-cmd.h new file mode 100644 index 0000000..8d5fca5 --- /dev/null +++ b/tools/kvm/include/kvm/kvm-cmd.h @@ -0,0 +1,12 @@ +#ifndef __KVM_CMD_H__ +#define __KVM_CMD_H__ + +struct cmd_struct { + const char *cmd; + int (*fn)(int, const char **, const char *); + int option; +}; + +int handle_command(struct cmd_struct *command, int argc, const char **argv); + +#endif diff --git a/tools/kvm/include/kvm/kvm-help.h b/tools/kvm/include/kvm/kvm-help.h new file mode 100644 index 0000000..2946743 --- /dev/null +++ b/tools/kvm/include/kvm/kvm-help.h @@ -0,0 +1,6 @@ +#ifndef __KVM_HELP_H__ +#define __KVM_HELP_H__ + +int kvm_cmd_help(int argc, const char **argv, const char *prefix); + +#endif diff --git a/tools/kvm/kvm-cmd.c b/tools/kvm/kvm-cmd.c new file mode 100644 index 0000000..ef9a454 --- /dev/null +++ b/tools/kvm/kvm-cmd.c @@ -0,0 +1,55 @@ +#include +#include +#include + +#include + +/* user defined header files */ +#include + +/* kvm_get_command: Searches the command in an array of the commands and + returns a pointer to cmd_struct if a match is found. + + Input parameters: + command: Array of possible commands. The last entry in the array must be + NULL. + cmd: A string command to search in the array + + Return Value: + NULL: If the cmd is not matched with any of the command in the command array + p: Pointer to cmd_struct of the matching command + */ +static struct cmd_struct *kvm_get_command(struct cmd_struct *command, + const char *cmd) +{ + struct cmd_struct *p = command; + + while (p->cmd) { + if (!strcmp(p->cmd, cmd)) + return p; + p++; + } + return NULL; +} + +int handle_command(struct cmd_struct *command, int argc, const char **argv) +{ + struct cmd_struct *p; + const char *prefix = NULL; + + if (!argv || !*argv) { + p = kvm_get_command(command, "help"); + assert(p); + return p->fn(argc, argv, prefix); + } + + p = kvm_get_command(command, argv[0]); + if (!p) { + p = kvm_get_command(command, "help"); + assert(p); + p->fn(argc, argv, prefix); + return EINVAL; + } + + return p->fn(argc - 1, &argv[1], prefix); +} diff --git a/tools/kvm/kvm-help.c b/tools/kvm/kvm-help.c new file mode 100644 index 0000000..fd133a9 --- /dev/null +++ b/tools/kvm/kvm-help.c @@ -0,0 +1,43 @@ +#include +#include + +/* user defined headers */ +#include + +#include +#include + + +const char kvm_usage_string[] = + "kvm [--version] [--help] COMMAND [ARGS]"; + +const char kvm_more_info_string[] = + "See 'kvm help COMMAND' for more information on a specific command."; + + +static void list_common_cmds_help(void) +{ + unsigned int i, longest = 0; + + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { + if (longest < strlen(common_cmds[i].name)) + longest = strlen(common_cmds[i].name); + } + + puts(" The most commonly used kvm commands are:"); + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { + printf(" %-*s ", longest, common_cmds[i].name); + puts(common_cmds[i].help); + } +} + +int kvm_cmd_help(int argc, const char **argv, const char *prefix) +{ + if (!argv || !*argv) { + printf("\n usage: %s\n\n", kvm_usage_string); + list_common_cmds_help(); + printf("\n %s\n\n", kvm_more_info_string); + return 0; + } + return 0; +}