[PATCH 2/2] trace-cmd: improve main readability

From: Federico Vaga
Date: Wed Apr 26 2017 - 16:31:50 EST


Since now there is an uniform command implementation I can reduce the
`main()` function to the minimum by using a lookup table. People can
now directly focus on a command implementation because there is
"nothing" in the `main()` function.

Signed-off-by: Federico Vaga <federico.vaga@xxxxxxxxxx>
---
trace-cmd.c | 116 ++++++++++++++++++++++++++++------------------------------
trace-list.c | 8 +++-
trace-local.h | 4 +-
trace-usage.c | 6 +++
4 files changed, 71 insertions(+), 63 deletions(-)

diff --git a/trace-cmd.c b/trace-cmd.c
index 39bcc06..0f71e12 100644
--- a/trace-cmd.c
+++ b/trace-cmd.c
@@ -76,72 +76,66 @@ void *malloc_or_die(unsigned int size)
}


+/**
+ * struct command
+ * @name command name
+ * @run function to execute on command `name`
+ */
+struct command {
+ char *name;
+ void (*run)(int argc, char **argv);
+};
+
+
+/**
+ * Lookup table that maps command names to functions
+ */
+struct command commands[] = {
+ {"report", trace_report},
+ {"snapshot", trace_snapshot},
+ {"hist", trace_hist},
+ {"mem", trace_mem},
+ {"listen", trace_listen},
+ {"split", trace_split},
+ {"restore", trace_restore},
+ {"stack", trace_stack},
+ {"check-events", trace_check_events},
+ {"record", trace_record},
+ {"start", trace_record},
+ {"extract", trace_record},
+ {"stop", trace_record},
+ {"stream", trace_record},
+ {"profile", trace_record},
+ {"restart", trace_record},
+ {"reset", trace_record},
+ {"stat", trace_stat},
+ {"options", trace_option},
+ {"show", trace_show},
+ {"list", trace_list},
+ {"help", trace_usage},
+ {"-h", trace_usage},
+};
+
+#define ARRAY_SIZE(_a) (sizeof(_a) / sizeof(_a[0]))
+
int main (int argc, char **argv)
{
+ int i;
+
errno = 0;

if (argc < 2)
- usage(argv);
-
- if (strcmp(argv[1], "report") == 0) {
- trace_report(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "snapshot") == 0) {
- trace_snapshot(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "hist") == 0) {
- trace_hist(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "mem") == 0) {
- trace_mem(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "listen") == 0) {
- trace_listen(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "split") == 0) {
- trace_split(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "restore") == 0) {
- trace_restore(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "stack") == 0) {
- trace_stack(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "check-events") == 0) {
- trace_check_events(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "record") == 0 ||
- strcmp(argv[1], "start") == 0 ||
- strcmp(argv[1], "extract") == 0 ||
- strcmp(argv[1], "stop") == 0 ||
- strcmp(argv[1], "stream") == 0 ||
- strcmp(argv[1], "profile") == 0 ||
- strcmp(argv[1], "restart") == 0 ||
- strcmp(argv[1], "reset") == 0) {
- trace_record(argc, argv);
- exit(0);
-
- } else if (strcmp(argv[1], "stat") == 0) {
- trace_stat(argc, argv);
- exit(0);
-
- } else if (strcmp(argv[1], "options") == 0) {
- show_plugin_options();
- exit(0);
- } else if (strcmp(argv[1], "show") == 0) {
- trace_show(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "list") == 0) {
- trace_list(argc, argv);
- exit(0);
- } else if (strcmp(argv[1], "-h") == 0 ||
- strcmp(argv[1], "help") == 0) {
- usage(argv);
- } else {
- fprintf(stderr, "unknown command: %s\n", argv[1]);
- usage(argv);
+ trace_usage(argc, argv);
+
+ for (i = 0; i < ARRAY_SIZE(commands); ++i) {
+ if (strcmp(argv[1], commands[i].name) == 0 ){
+ commands[i].run(argc, argv);
+ goto out;
+ }
}

- return 0;
+ /* No valid command found, show help */
+ trace_usage(argc, argv);
+out:
+ exit(0);
}
-
diff --git a/trace-list.c b/trace-list.c
index 3fdca34..293635f 100644
--- a/trace-list.c
+++ b/trace-list.c
@@ -319,7 +319,7 @@ static void show_buffers(void)
}


-void show_plugin_options(void)
+static void show_plugin_options(void)
{
struct pevent *pevent;
struct plugin_list *list;
@@ -341,6 +341,12 @@ void show_plugin_options(void)
}


+void trace_option(int argc, char **argv)
+{
+ show_plugin_options();
+}
+
+
static void show_plugins(void)
{
struct pevent *pevent;
diff --git a/trace-local.h b/trace-local.h
index ee518fb..fa987bc 100644
--- a/trace-local.h
+++ b/trace-local.h
@@ -83,6 +83,8 @@ void trace_show(int argc, char **argv);

void trace_list(int argc, char **argv);

+void trace_usage(int argc, char **argv);
+
struct hook_list;

void trace_init_profile(struct tracecmd_input *handle, struct hook_list *hooks,
@@ -189,7 +191,7 @@ char *get_instance_file(struct buffer_instance *instance, const char *file);
void update_first_instance(struct buffer_instance *instance, int topt);

void show_instance_file(struct buffer_instance *instance, const char *name);
-void show_plugin_options(void);
+
int count_cpus(void);

/* No longer in event-utils.h */
diff --git a/trace-usage.c b/trace-usage.c
index 5c1a692..9664aa6 100644
--- a/trace-usage.c
+++ b/trace-usage.c
@@ -327,3 +327,9 @@ void usage(char **argv)
printf("\n");
exit(-1);
}
+
+
+void trace_usage(int argc, char **argv)
+{
+ usage(argv);
+}
--
2.9.3