diff mbox

[3/4] Provides the basic Gitish framework

Message ID 1302205647-7435-3-git-send-email-prasadjoshi124@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Prasad Joshi April 7, 2011, 7:47 p.m. UTC
- kvm-cmd.h: Adds a new structure cmd_struct to create a table of commands
  and callback function.

- 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.

Signed-off-by: Prasad Joshi <prasadjoshi124@gmail.com>
---
 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

Comments

Ingo Molnar April 7, 2011, 8:14 p.m. UTC | #1
* Prasad Joshi <prasadjoshi124@gmail.com> wrote:

> - kvm-cmd.h: Adds a new structure cmd_struct to create a table of commands
>   and callback function.
> 
> - 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.

Please credit the people/projects from where you took these bits.

Thanks,

	Ingo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Prasad Joshi April 7, 2011, 10:33 p.m. UTC | #2
On Thu, Apr 7, 2011 at 9:14 PM, Ingo Molnar <mingo@elte.hu> wrote:
>
> * Prasad Joshi <prasadjoshi124@gmail.com> wrote:
>
>> - kvm-cmd.h: Adds a new structure cmd_struct to create a table of commands
>>   and callback function.
>>
>> - 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.
>
> Please credit the people/projects from where you took these bits.

Oh yes!
struct cmd_struct and list_common_cmds_help() is copied from perf.

>
> Thanks,
>
>        Ingo
>
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Pekka Enberg April 8, 2011, 5:44 a.m. UTC | #3
On Thu, 2011-04-07 at 23:33 +0100, Prasad Joshi wrote:
> On Thu, Apr 7, 2011 at 9:14 PM, Ingo Molnar <mingo@elte.hu> wrote:
> >
> > * Prasad Joshi <prasadjoshi124@gmail.com> wrote:
> >
> >> - kvm-cmd.h: Adds a new structure cmd_struct to create a table of commands
> >>   and callback function.
> >>
> >> - 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.
> >
> > Please credit the people/projects from where you took these bits.
> 
> Oh yes!
> struct cmd_struct and list_common_cmds_help() is copied from perf.

Please just add that to the changelog.

			Pekka

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Ingo Molnar April 8, 2011, 6:10 a.m. UTC | #4
* Pekka Enberg <penberg@kernel.org> wrote:

> On Thu, 2011-04-07 at 23:33 +0100, Prasad Joshi wrote:
> > On Thu, Apr 7, 2011 at 9:14 PM, Ingo Molnar <mingo@elte.hu> wrote:
> > >
> > > * Prasad Joshi <prasadjoshi124@gmail.com> wrote:
> > >
> > >> - kvm-cmd.h: Adds a new structure cmd_struct to create a table of commands
> > >>   and callback function.
> > >>
> > >> - 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.
> > >
> > > Please credit the people/projects from where you took these bits.
> > 
> > Oh yes!
> > struct cmd_struct and list_common_cmds_help() is copied from perf.
> 
> Please just add that to the changelog.

Yeah - and perf took it from Git :-)

The way we credited the Git project in perf was this commit:

  1b173f77dd0d: perf_counter tools: Add CREDITS file for Git contributors

basically a CREDITS file for Git library contributors. If you expect to take 
more bits, such as the help system (look at tools/perf/Documentation/) and 
other bits, the easiest solution would be for you to take the CREDITS file and 
name it CREDITS-lib or so.

This also serves as a way to grandfather in other history (and copyrights), the 
exact upstream Git sha1 is mentioned. The Git project is GPLv2 as well like the 
kernel and like tools/perf/, so this works out well and you do not have to 
re-credit again and again so precisely, as you take more bits of Git's library 
code.

Thanks,

	Ingo
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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 <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <assert.h>
+
+/* user defined header files */
+#include <kvm/kvm-cmd.h>
+
+/* 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 <stdio.h>
+#include <string.h>
+
+/* user defined headers */
+#include <common-cmds.h>
+
+#include <kvm/util.h>
+#include <kvm/kvm-help.h>
+
+
+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;
+}