lttng: add `help` command
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 17 Nov 2015 06:59:11 +0000 (01:59 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 18 Mar 2016 01:35:59 +0000 (21:35 -0400)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng/Makefile.am
src/bin/lttng/command.h
src/bin/lttng/commands/help.c [new file with mode: 0644]
src/bin/lttng/lttng.c

index 93a563ff56e88e70d1d0bad6d1c59bdc8e5db326..b911f334c7bc39f1012711256884bd8d59101fc9 100644 (file)
@@ -18,6 +18,7 @@ lttng_SOURCES = command.h conf.c conf.h commands/start.c \
                                commands/track-untrack.c \
                                commands/status.c \
                                commands/metadata.c \
+                               commands/help.c \
                                utils.c utils.h lttng.c
 
 lttng_LDADD = $(top_builddir)/src/lib/lttng-ctl/liblttng-ctl.la \
index 05c51663b90e011d3174100e04915593a365f8d2..72cf4e0ff0334ab5add1bf629b0b6aec4f3b8992 100644 (file)
@@ -35,6 +35,7 @@
                if (ret) {                                              \
                        ERR("Cannot view man page lttng-%s(1)", argv[0]); \
                        perror("exec");                                 \
+                       ret = CMD_ERROR;                                \
                }                                                       \
        } while (0)
 
@@ -76,4 +77,7 @@ DECL_COMMAND(track);
 DECL_COMMAND(untrack);
 DECL_COMMAND(metadata);
 
+extern int cmd_help(int argc, const char **argv,
+               const struct cmd_struct commands[]);
+
 #endif /* _LTTNG_CMD_H */
diff --git a/src/bin/lttng/commands/help.c b/src/bin/lttng/commands/help.c
new file mode 100644 (file)
index 0000000..29f56ba
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2015 - Philippe Proulx <pproulx@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#define _LGPL_SOURCE
+#include <popt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../command.h"
+#include <common/utils.h>
+
+enum {
+       OPT_HELP = 1,
+       OPT_LIST_OPTIONS,
+};
+
+static struct poptOption long_options[] = {
+       /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
+       {"help",      'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
+       {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
+       {0, 0, 0, 0, 0, 0, 0}
+};
+
+/*
+ *  cmd_help
+ */
+int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
+{
+       int opt, ret = CMD_SUCCESS;
+       char *cmd_name;
+       static poptContext pc;
+       const struct cmd_struct *cmd;
+       int found = 0;
+
+       pc = poptGetContext(NULL, argc, argv, long_options, 0);
+       poptReadDefaultConfig(pc, 0);
+
+       while ((opt = poptGetNextOpt(pc)) != -1) {
+               switch (opt) {
+               case OPT_HELP:
+                       SHOW_HELP();
+                       goto end;
+               case OPT_LIST_OPTIONS:
+                       list_cmd_options(stdout, long_options);
+                       goto end;
+               default:
+                       ret = CMD_UNDEFINED;
+                       goto end;
+               }
+       }
+
+       /* Get command name */
+       cmd_name = (char *) poptGetArg(pc);
+
+       if (cmd_name == NULL) {
+               /* Fall back to lttng(1) */
+               ret = utils_show_man_page(1, "lttng");
+
+               if (ret) {
+                       ERR("Cannot view man page lttng(1)");
+                       perror("exec");
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
+       /* Make sure command name exists */
+       cmd = &commands[0];
+
+       while (cmd->name != NULL) {
+               if (strcmp(cmd->name, cmd_name) == 0) {
+                       found = 1;
+                       break;
+               }
+
+               cmd++;
+       }
+
+       if (!found) {
+               ERR("Unknown command \"%s\"", cmd_name);
+               ret = CMD_ERROR;
+               goto end;
+       }
+
+       /* Show command's man page */
+       ret = show_cmd_man_page(cmd_name);
+
+       if (ret) {
+               ERR("Cannot view man page lttng-%s(1)", cmd_name);
+               perror("exec");
+               ret = CMD_ERROR;
+       }
+
+end:
+       poptFreeContext(pc);
+       return ret;
+}
index aa13bc45dd57f621e539b62d4e1aa6f401947767..bcfbf88e65aed6c701e5d2e2a458a1c2e9918dd0 100644 (file)
@@ -86,6 +86,7 @@ static struct cmd_struct commands[] =  {
        { "track", cmd_track},
        { "untrack", cmd_untrack},
        { "metadata", cmd_metadata},
+       { "help", NULL},
        { NULL, NULL}   /* Array closure */
 };
 
@@ -216,8 +217,14 @@ static int handle_command(int argc, char **argv)
                goto end;
        }
 
+       /* Special case for help command which needs the commands array */
+       if (strcmp(argv[0], "help") == 0) {
+               ret = cmd_help(argc, (const char**) argv, commands);
+               goto end;
+       }
+
        cmd = &commands[i];
-       while (cmd->func != NULL) {
+       while (cmd->name != NULL) {
                /* Find command */
                if (strcmp(argv[0], cmd->name) == 0) {
                        ret = cmd->func(argc, (const char**) argv);
This page took 0.027715 seconds and 4 git commands to generate.