From: Philippe Proulx Date: Tue, 17 Nov 2015 06:59:11 +0000 (-0500) Subject: lttng: add `help` command X-Git-Tag: v2.8.0-rc1~27 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=960afba4f737174cd305af2019d9c2c781ec6c4e;p=lttng-tools.git lttng: add `help` command Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- diff --git a/src/bin/lttng/Makefile.am b/src/bin/lttng/Makefile.am index 93a563ff5..b911f334c 100644 --- a/src/bin/lttng/Makefile.am +++ b/src/bin/lttng/Makefile.am @@ -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 \ diff --git a/src/bin/lttng/command.h b/src/bin/lttng/command.h index 05c51663b..72cf4e0ff 100644 --- a/src/bin/lttng/command.h +++ b/src/bin/lttng/command.h @@ -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 index 000000000..29f56bab1 --- /dev/null +++ b/src/bin/lttng/commands/help.c @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2015 - Philippe Proulx + * + * 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 +#include +#include +#include + +#include "../command.h" +#include + +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; +} diff --git a/src/bin/lttng/lttng.c b/src/bin/lttng/lttng.c index aa13bc45d..bcfbf88e6 100644 --- a/src/bin/lttng/lttng.c +++ b/src/bin/lttng/lttng.c @@ -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);