| 1 | /* |
| 2 | * Copyright (C) 2015 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include "../command.hpp" |
| 10 | |
| 11 | #include <common/utils.hpp> |
| 12 | |
| 13 | #include <popt.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #ifdef LTTNG_EMBED_HELP |
| 19 | static const char *help_msg = |
| 20 | #include <lttng-help.1.h> |
| 21 | ; |
| 22 | #endif |
| 23 | |
| 24 | static const char *lttng_help_msg = |
| 25 | #ifdef LTTNG_EMBED_HELP |
| 26 | #include <lttng.1.h> |
| 27 | #else |
| 28 | nullptr |
| 29 | #endif |
| 30 | ; |
| 31 | |
| 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 */ |
| 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 } |
| 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; |
| 50 | const char *arg_cmd_name; |
| 51 | static poptContext pc; |
| 52 | const struct cmd_struct *cmd; |
| 53 | bool found = false; |
| 54 | const char *cmd_argv[2]; |
| 55 | |
| 56 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
| 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 */ |
| 74 | arg_cmd_name = poptGetArg(pc); |
| 75 | if (arg_cmd_name == nullptr) { |
| 76 | /* Fall back to lttng(1) */ |
| 77 | ret = utils_show_help(1, "lttng", lttng_help_msg); |
| 78 | if (ret) { |
| 79 | ERR("Cannot show --help for `lttng`"); |
| 80 | perror("exec"); |
| 81 | ret = CMD_ERROR; |
| 82 | } |
| 83 | |
| 84 | goto end; |
| 85 | } |
| 86 | |
| 87 | /* Help about help? */ |
| 88 | if (strcmp(arg_cmd_name, "help") == 0) { |
| 89 | SHOW_HELP(); |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | /* Make sure command name exists */ |
| 94 | cmd = &commands[0]; |
| 95 | |
| 96 | while (cmd->name != nullptr) { |
| 97 | if (strcmp(cmd->name, arg_cmd_name) == 0) { |
| 98 | found = true; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | cmd++; |
| 103 | } |
| 104 | |
| 105 | if (!found) { |
| 106 | ERR("Unknown command \"%s\"", arg_cmd_name); |
| 107 | ret = CMD_ERROR; |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | /* Show command's help */ |
| 112 | cmd_argv[0] = cmd->name; |
| 113 | cmd_argv[1] = "--help"; |
| 114 | LTTNG_ASSERT(cmd->func); |
| 115 | ret = cmd->func(2, cmd_argv); |
| 116 | |
| 117 | end: |
| 118 | poptFreeContext(pc); |
| 119 | return ret; |
| 120 | } |