bin: compile lttng as C++
[lttng-tools.git] / src / bin / lttng / commands / help.cpp
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 <popt.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "../command.h"
15 #include <common/utils.h>
16
17 #ifdef LTTNG_EMBED_HELP
18 static const char *help_msg =
19 #include <lttng-help.1.h>
20 ;
21 #endif
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
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;
53 const char *cmd_argv[2];
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) */
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(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 != 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
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 }
This page took 0.031664 seconds and 4 git commands to generate.