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 | |
28ab034a JG |
9 | #include "../command.hpp" |
10 | ||
11 | #include <common/utils.hpp> | |
12 | ||
960afba4 PP |
13 | #include <popt.h> |
14 | #include <stdio.h> | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | ||
4fc83d94 | 18 | #ifdef LTTNG_EMBED_HELP |
d279f57d | 19 | static const char *help_msg = |
4fc83d94 | 20 | #include <lttng-help.1.h> |
28ab034a | 21 | ; |
d279f57d | 22 | #endif |
4fc83d94 PP |
23 | |
24 | static const char *lttng_help_msg = | |
25 | #ifdef LTTNG_EMBED_HELP | |
26 | #include <lttng.1.h> | |
27 | #else | |
cd9adb8b | 28 | nullptr |
4fc83d94 | 29 | #endif |
28ab034a | 30 | ; |
4fc83d94 | 31 | |
960afba4 PP |
32 | enum { |
33 | OPT_HELP = 1, | |
34 | OPT_LIST_OPTIONS, | |
35 | }; | |
36 | ||
37 | static struct poptOption long_options[] = { | |
38 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
39 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
40 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, | |
41 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } | |
960afba4 PP |
42 | }; |
43 | ||
44 | /* | |
45 | * cmd_help | |
46 | */ | |
47 | int cmd_help(int argc, const char **argv, const struct cmd_struct commands[]) | |
48 | { | |
49 | int opt, ret = CMD_SUCCESS; | |
5b915816 | 50 | const char *arg_cmd_name; |
960afba4 PP |
51 | static poptContext pc; |
52 | const struct cmd_struct *cmd; | |
ff01a574 | 53 | bool found = false; |
6828df29 | 54 | const char *cmd_argv[2]; |
960afba4 | 55 | |
cd9adb8b | 56 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
960afba4 PP |
57 | poptReadDefaultConfig(pc, 0); |
58 | ||
59 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
60 | switch (opt) { | |
61 | case OPT_HELP: | |
62 | SHOW_HELP(); | |
63 | goto end; | |
64 | case OPT_LIST_OPTIONS: | |
65 | list_cmd_options(stdout, long_options); | |
66 | goto end; | |
67 | default: | |
68 | ret = CMD_UNDEFINED; | |
69 | goto end; | |
70 | } | |
71 | } | |
72 | ||
73 | /* Get command name */ | |
5b915816 | 74 | arg_cmd_name = poptGetArg(pc); |
cd9adb8b | 75 | if (arg_cmd_name == nullptr) { |
960afba4 | 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 | 87 | /* Help about help? */ |
5b915816 | 88 | if (strcmp(arg_cmd_name, "help") == 0) { |
6828df29 PP |
89 | SHOW_HELP(); |
90 | goto end; | |
91 | } | |
92 | ||
960afba4 PP |
93 | /* Make sure command name exists */ |
94 | cmd = &commands[0]; | |
95 | ||
cd9adb8b | 96 | while (cmd->name != nullptr) { |
5b915816 | 97 | if (strcmp(cmd->name, arg_cmd_name) == 0) { |
ff01a574 | 98 | found = true; |
960afba4 PP |
99 | break; |
100 | } | |
101 | ||
102 | cmd++; | |
103 | } | |
104 | ||
105 | if (!found) { | |
5b915816 | 106 | ERR("Unknown command \"%s\"", arg_cmd_name); |
960afba4 PP |
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 | } |