Commit | Line | Data |
---|---|---|
960afba4 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2015 Philippe Proulx <pproulx@efficios.com> |
960afba4 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
960afba4 | 5 | * |
960afba4 PP |
6 | */ |
7 | ||
8 | #define _LGPL_SOURCE | |
9 | #include <popt.h> | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | ||
c9e313bc SM |
14 | #include "../command.hpp" |
15 | #include <common/utils.hpp> | |
960afba4 | 16 | |
4fc83d94 | 17 | #ifdef LTTNG_EMBED_HELP |
d279f57d | 18 | static const char *help_msg = |
4fc83d94 | 19 | #include <lttng-help.1.h> |
4fc83d94 | 20 | ; |
d279f57d | 21 | #endif |
4fc83d94 PP |
22 | |
23 | static const char *lttng_help_msg = | |
24 | #ifdef LTTNG_EMBED_HELP | |
25 | #include <lttng.1.h> | |
26 | #else | |
27 | NULL | |
28 | #endif | |
29 | ; | |
30 | ||
960afba4 PP |
31 | enum { |
32 | OPT_HELP = 1, | |
33 | OPT_LIST_OPTIONS, | |
34 | }; | |
35 | ||
36 | static struct poptOption long_options[] = { | |
37 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
38 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
39 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
40 | {0, 0, 0, 0, 0, 0, 0} | |
41 | }; | |
42 | ||
43 | /* | |
44 | * cmd_help | |
45 | */ | |
46 | int cmd_help(int argc, const char **argv, const struct cmd_struct commands[]) | |
47 | { | |
48 | int opt, ret = CMD_SUCCESS; | |
49 | char *cmd_name; | |
50 | static poptContext pc; | |
51 | const struct cmd_struct *cmd; | |
52 | int found = 0; | |
6828df29 | 53 | const char *cmd_argv[2]; |
960afba4 PP |
54 | |
55 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
56 | poptReadDefaultConfig(pc, 0); | |
57 | ||
58 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
59 | switch (opt) { | |
60 | case OPT_HELP: | |
61 | SHOW_HELP(); | |
62 | goto end; | |
63 | case OPT_LIST_OPTIONS: | |
64 | list_cmd_options(stdout, long_options); | |
65 | goto end; | |
66 | default: | |
67 | ret = CMD_UNDEFINED; | |
68 | goto end; | |
69 | } | |
70 | } | |
71 | ||
72 | /* Get command name */ | |
73 | cmd_name = (char *) poptGetArg(pc); | |
74 | ||
75 | if (cmd_name == NULL) { | |
76 | /* Fall back to lttng(1) */ | |
4fc83d94 | 77 | ret = utils_show_help(1, "lttng", lttng_help_msg); |
960afba4 | 78 | if (ret) { |
4fc83d94 | 79 | ERR("Cannot show --help for `lttng`"); |
960afba4 PP |
80 | perror("exec"); |
81 | ret = CMD_ERROR; | |
960afba4 | 82 | } |
4fc83d94 PP |
83 | |
84 | goto end; | |
960afba4 PP |
85 | } |
86 | ||
6828df29 PP |
87 | /* Help about help? */ |
88 | if (strcmp(cmd_name, "help") == 0) { | |
89 | SHOW_HELP(); | |
90 | goto end; | |
91 | } | |
92 | ||
960afba4 PP |
93 | /* Make sure command name exists */ |
94 | cmd = &commands[0]; | |
95 | ||
96 | while (cmd->name != NULL) { | |
97 | if (strcmp(cmd->name, cmd_name) == 0) { | |
98 | found = 1; | |
99 | break; | |
100 | } | |
101 | ||
102 | cmd++; | |
103 | } | |
104 | ||
105 | if (!found) { | |
106 | ERR("Unknown command \"%s\"", cmd_name); | |
107 | ret = CMD_ERROR; | |
108 | goto end; | |
109 | } | |
110 | ||
6828df29 PP |
111 | /* Show command's help */ |
112 | cmd_argv[0] = cmd->name; | |
113 | cmd_argv[1] = "--help"; | |
a0377dfe | 114 | LTTNG_ASSERT(cmd->func); |
6828df29 | 115 | ret = cmd->func(2, cmd_argv); |
960afba4 PP |
116 | |
117 | end: | |
118 | poptFreeContext(pc); | |
119 | return ret; | |
120 | } |