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