Add --enable-embedded-help option to embed --help messages in binaries
[lttng-tools.git] / src / bin / lttng / commands / help.c
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>
23
24 #include "../command.h"
25 #include <common/utils.h>
26
27 static const char *help_msg =
28 #ifdef LTTNG_EMBED_HELP
29 #include <lttng-help.1.h>
30 #else
31 NULL
32 #endif
33 ;
34
35 static const char *lttng_help_msg =
36 #ifdef LTTNG_EMBED_HELP
37 #include <lttng.1.h>
38 #else
39 NULL
40 #endif
41 ;
42
43 enum {
44 OPT_HELP = 1,
45 OPT_LIST_OPTIONS,
46 };
47
48 static struct poptOption long_options[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
51 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
52 {0, 0, 0, 0, 0, 0, 0}
53 };
54
55 /*
56 * cmd_help
57 */
58 int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
59 {
60 int opt, ret = CMD_SUCCESS;
61 char *cmd_name;
62 static poptContext pc;
63 const struct cmd_struct *cmd;
64 int found = 0;
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) */
88 ret = utils_show_help(1, "lttng", lttng_help_msg);
89 if (ret) {
90 ERR("Cannot show --help for `lttng`");
91 perror("exec");
92 ret = CMD_ERROR;
93 }
94
95 goto end;
96 }
97
98 /* Make sure command name exists */
99 cmd = &commands[0];
100
101 while (cmd->name != NULL) {
102 if (strcmp(cmd->name, cmd_name) == 0) {
103 found = 1;
104 break;
105 }
106
107 cmd++;
108 }
109
110 if (!found) {
111 ERR("Unknown command \"%s\"", cmd_name);
112 ret = CMD_ERROR;
113 goto end;
114 }
115
116 /* Show command's man page */
117 ret = show_cmd_man_page(cmd_name);
118
119 if (ret) {
120 ERR("Cannot view man page lttng-%s(1)", cmd_name);
121 perror("exec");
122 ret = CMD_ERROR;
123 }
124
125 end:
126 poptFreeContext(pc);
127 return ret;
128 }
This page took 0.031275 seconds and 4 git commands to generate.