Commit | Line | Data |
---|---|---|
960afba4 PP |
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> | |
6828df29 | 23 | #include <assert.h> |
960afba4 PP |
24 | |
25 | #include "../command.h" | |
26 | #include <common/utils.h> | |
27 | ||
4fc83d94 PP |
28 | static const char *help_msg = |
29 | #ifdef LTTNG_EMBED_HELP | |
30 | #include <lttng-help.1.h> | |
31 | #else | |
32 | NULL | |
33 | #endif | |
34 | ; | |
35 | ||
36 | static const char *lttng_help_msg = | |
37 | #ifdef LTTNG_EMBED_HELP | |
38 | #include <lttng.1.h> | |
39 | #else | |
40 | NULL | |
41 | #endif | |
42 | ; | |
43 | ||
960afba4 PP |
44 | enum { |
45 | OPT_HELP = 1, | |
46 | OPT_LIST_OPTIONS, | |
47 | }; | |
48 | ||
49 | static struct poptOption long_options[] = { | |
50 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
51 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
52 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
53 | {0, 0, 0, 0, 0, 0, 0} | |
54 | }; | |
55 | ||
56 | /* | |
57 | * cmd_help | |
58 | */ | |
59 | int cmd_help(int argc, const char **argv, const struct cmd_struct commands[]) | |
60 | { | |
61 | int opt, ret = CMD_SUCCESS; | |
62 | char *cmd_name; | |
63 | static poptContext pc; | |
64 | const struct cmd_struct *cmd; | |
65 | int found = 0; | |
6828df29 | 66 | const char *cmd_argv[2]; |
960afba4 PP |
67 | |
68 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
69 | poptReadDefaultConfig(pc, 0); | |
70 | ||
71 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
72 | switch (opt) { | |
73 | case OPT_HELP: | |
74 | SHOW_HELP(); | |
75 | goto end; | |
76 | case OPT_LIST_OPTIONS: | |
77 | list_cmd_options(stdout, long_options); | |
78 | goto end; | |
79 | default: | |
80 | ret = CMD_UNDEFINED; | |
81 | goto end; | |
82 | } | |
83 | } | |
84 | ||
85 | /* Get command name */ | |
86 | cmd_name = (char *) poptGetArg(pc); | |
87 | ||
88 | if (cmd_name == NULL) { | |
89 | /* Fall back to lttng(1) */ | |
4fc83d94 | 90 | ret = utils_show_help(1, "lttng", lttng_help_msg); |
960afba4 | 91 | if (ret) { |
4fc83d94 | 92 | ERR("Cannot show --help for `lttng`"); |
960afba4 PP |
93 | perror("exec"); |
94 | ret = CMD_ERROR; | |
960afba4 | 95 | } |
4fc83d94 PP |
96 | |
97 | goto end; | |
960afba4 PP |
98 | } |
99 | ||
6828df29 PP |
100 | /* Help about help? */ |
101 | if (strcmp(cmd_name, "help") == 0) { | |
102 | SHOW_HELP(); | |
103 | goto end; | |
104 | } | |
105 | ||
960afba4 PP |
106 | /* Make sure command name exists */ |
107 | cmd = &commands[0]; | |
108 | ||
109 | while (cmd->name != NULL) { | |
110 | if (strcmp(cmd->name, cmd_name) == 0) { | |
111 | found = 1; | |
112 | break; | |
113 | } | |
114 | ||
115 | cmd++; | |
116 | } | |
117 | ||
118 | if (!found) { | |
119 | ERR("Unknown command \"%s\"", cmd_name); | |
120 | ret = CMD_ERROR; | |
121 | goto end; | |
122 | } | |
123 | ||
6828df29 PP |
124 | /* Show command's help */ |
125 | cmd_argv[0] = cmd->name; | |
126 | cmd_argv[1] = "--help"; | |
127 | assert(cmd->func); | |
128 | ret = cmd->func(2, cmd_argv); | |
960afba4 PP |
129 | |
130 | end: | |
131 | poptFreeContext(pc); | |
132 | return ret; | |
133 | } |