Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / bin / lttng / commands / help.c
CommitLineData
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>
6828df29 13#include <assert.h>
960afba4
PP
14
15#include "../command.h"
16#include <common/utils.h>
17
4fc83d94 18#ifdef LTTNG_EMBED_HELP
d279f57d 19static const char *help_msg =
4fc83d94 20#include <lttng-help.1.h>
4fc83d94 21;
d279f57d 22#endif
4fc83d94
PP
23
24static const char *lttng_help_msg =
25#ifdef LTTNG_EMBED_HELP
26#include <lttng.1.h>
27#else
28NULL
29#endif
30;
31
960afba4
PP
32enum {
33 OPT_HELP = 1,
34 OPT_LIST_OPTIONS,
35};
36
37static struct poptOption long_options[] = {
38 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
39 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
40 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
41 {0, 0, 0, 0, 0, 0, 0}
42};
43
44/*
45 * cmd_help
46 */
47int cmd_help(int argc, const char **argv, const struct cmd_struct commands[])
48{
49 int opt, ret = CMD_SUCCESS;
50 char *cmd_name;
51 static poptContext pc;
52 const struct cmd_struct *cmd;
53 int found = 0;
6828df29 54 const char *cmd_argv[2];
960afba4
PP
55
56 pc = poptGetContext(NULL, 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 cmd_name = (char *) poptGetArg(pc);
75
76 if (cmd_name == NULL) {
77 /* Fall back to lttng(1) */
4fc83d94 78 ret = utils_show_help(1, "lttng", lttng_help_msg);
960afba4 79 if (ret) {
4fc83d94 80 ERR("Cannot show --help for `lttng`");
960afba4
PP
81 perror("exec");
82 ret = CMD_ERROR;
960afba4 83 }
4fc83d94
PP
84
85 goto end;
960afba4
PP
86 }
87
6828df29
PP
88 /* Help about help? */
89 if (strcmp(cmd_name, "help") == 0) {
90 SHOW_HELP();
91 goto end;
92 }
93
960afba4
PP
94 /* Make sure command name exists */
95 cmd = &commands[0];
96
97 while (cmd->name != NULL) {
98 if (strcmp(cmd->name, cmd_name) == 0) {
99 found = 1;
100 break;
101 }
102
103 cmd++;
104 }
105
106 if (!found) {
107 ERR("Unknown command \"%s\"", cmd_name);
108 ret = CMD_ERROR;
109 goto end;
110 }
111
6828df29
PP
112 /* Show command's help */
113 cmd_argv[0] = cmd->name;
114 cmd_argv[1] = "--help";
115 assert(cmd->func);
116 ret = cmd->func(2, cmd_argv);
960afba4
PP
117
118end:
119 poptFreeContext(pc);
120 return ret;
121}
This page took 0.036916 seconds and 4 git commands to generate.