lttng: add `help` command
[lttng-tools.git] / src / bin / lttng / commands / help.c
1 /*
2 * Copyright (C) 2015 - Philippe Proulx <pproulx@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <popt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "../command.h"
25 #include <common/utils.h>
26
27 enum {
28 OPT_HELP = 1,
29 OPT_LIST_OPTIONS,
30 };
31
32 static struct poptOption long_options[] = {
33 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
34 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
35 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
36 {0, 0, 0, 0, 0, 0, 0}
37 };
38
39 /*
40 * cmd_help
41 */
42 int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
43 {
44 int opt, ret = CMD_SUCCESS;
45 char *cmd_name;
46 static poptContext pc;
47 const struct cmd_struct *cmd;
48 int found = 0;
49
50 pc = poptGetContext(NULL, argc, argv, long_options, 0);
51 poptReadDefaultConfig(pc, 0);
52
53 while ((opt = poptGetNextOpt(pc)) != -1) {
54 switch (opt) {
55 case OPT_HELP:
56 SHOW_HELP();
57 goto end;
58 case OPT_LIST_OPTIONS:
59 list_cmd_options(stdout, long_options);
60 goto end;
61 default:
62 ret = CMD_UNDEFINED;
63 goto end;
64 }
65 }
66
67 /* Get command name */
68 cmd_name = (char *) poptGetArg(pc);
69
70 if (cmd_name == NULL) {
71 /* Fall back to lttng(1) */
72 ret = utils_show_man_page(1, "lttng");
73
74 if (ret) {
75 ERR("Cannot view man page lttng(1)");
76 perror("exec");
77 ret = CMD_ERROR;
78 goto end;
79 }
80 }
81
82 /* Make sure command name exists */
83 cmd = &commands[0];
84
85 while (cmd->name != NULL) {
86 if (strcmp(cmd->name, cmd_name) == 0) {
87 found = 1;
88 break;
89 }
90
91 cmd++;
92 }
93
94 if (!found) {
95 ERR("Unknown command \"%s\"", cmd_name);
96 ret = CMD_ERROR;
97 goto end;
98 }
99
100 /* Show command's man page */
101 ret = show_cmd_man_page(cmd_name);
102
103 if (ret) {
104 ERR("Cannot view man page lttng-%s(1)", cmd_name);
105 perror("exec");
106 ret = CMD_ERROR;
107 }
108
109 end:
110 poptFreeContext(pc);
111 return ret;
112 }
This page took 0.031639 seconds and 4 git commands to generate.