Add --enable-embedded-help option to embed --help messages in binaries
[lttng-tools.git] / src / bin / lttng / commands / version.c
CommitLineData
eb9cb8b7
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
eb9cb8b7
DG
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 *
d14d33bf
AM
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.
eb9cb8b7
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
eb9cb8b7
DG
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
c7e35b03
JR
27#include <common/mi-lttng.h>
28
c399183f 29#include "../command.h"
eb9cb8b7 30
4fc83d94
PP
31#ifdef LTTNG_EMBED_HELP
32static const char help_msg[] =
33#include <lttng-version.1.h>
34;
35#endif
36
eb9cb8b7
DG
37enum {
38 OPT_HELP = 1,
679b4943 39 OPT_LIST_OPTIONS,
eb9cb8b7
DG
40};
41
c7e35b03
JR
42static const char *lttng_license = "lttng is free software and under the GPL license and part LGPL";
43
eb9cb8b7
DG
44static struct poptOption long_options[] = {
45 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
46 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
679b4943 47 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
eb9cb8b7
DG
48 {0, 0, 0, 0, 0, 0, 0}
49};
50
c7e35b03
JR
51/*
52 * create_version
53 */
54static void create_version(struct mi_lttng_version *version)
55{
56 strncpy(version->version, VERSION, NAME_MAX);
57 version->version_major = VERSION_MAJOR;
58 version->version_minor = VERSION_MINOR;
59 version->version_patchlevel = VERSION_PATCHLEVEL;
50bba0f3 60 strncpy(version->version_commit, GIT_VERSION, NAME_MAX);
c7e35b03
JR
61 strncpy(version->version_name, VERSION_NAME, NAME_MAX);
62 strncpy(version->package_url, PACKAGE_URL, NAME_MAX);
63}
64
65/*
66 * Print the machine interface output of this command.
67 */
68static int print_mi()
69{
70 int ret = CMD_SUCCESS;
71 struct mi_writer *writer = NULL;
72 struct mi_lttng_version version;
73
74 create_version(&version);
75
76 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
77 if (!writer) {
78 ret = -LTTNG_ERR_NOMEM;
79 goto end;
80 }
81
82 /* Open the command element */
83 ret = mi_lttng_writer_command_open(writer,
84 mi_lttng_element_command_version);
85 if (ret) {
86 ret = CMD_ERROR;
87 goto error;
88 }
89
90 /* Beginning of output */
91 ret = mi_lttng_writer_open_element(writer,
92 mi_lttng_element_command_output);
93 if (ret) {
94 ret = CMD_ERROR;
95 goto error;
96 }
97
98 /* Print the machine interface of version */
99 ret = mi_lttng_version(writer, &version,
100 VERSION_DESCRIPTION, lttng_license);
101 if (ret) {
102 ret = CMD_ERROR;
103 goto error;
104 }
105
106 /* Close the output element */
107 ret = mi_lttng_writer_close_element(writer);
108 if (ret) {
109 ret = CMD_ERROR;
110 goto error;
111 }
112
113 /* Close the command */
114 ret = mi_lttng_writer_command_close(writer);
115 if (ret) {
116 ret = CMD_ERROR;
117 }
118
119error:
120 /* Cleanup */
121 if (writer && mi_lttng_writer_destroy(writer)) {
122 /* Preserve original error code */
123 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
124 }
125
126end:
127 return ret;
128}
129
eb9cb8b7
DG
130/*
131 * cmd_version
132 */
133int cmd_version(int argc, const char **argv)
134{
135 int opt, ret = CMD_SUCCESS;
136 static poptContext pc;
137
138 pc = poptGetContext(NULL, argc, argv, long_options, 0);
139 poptReadDefaultConfig(pc, 0);
140
141 while ((opt = poptGetNextOpt(pc)) != -1) {
142 switch (opt) {
143 case OPT_HELP:
4ba92f18 144 SHOW_HELP();
eb9cb8b7 145 goto end;
679b4943
SM
146 case OPT_LIST_OPTIONS:
147 list_cmd_options(stdout, long_options);
679b4943 148 goto end;
eb9cb8b7 149 default:
eb9cb8b7
DG
150 ret = CMD_UNDEFINED;
151 goto end;
152 }
153 }
154
c7e35b03
JR
155 if (lttng_opt_mi) {
156 ret = print_mi();
157 } else {
4c6ac053
MD
158 MSG("lttng version " VERSION " - " VERSION_NAME "%s",
159 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
c7e35b03
JR
160 MSG("\n" VERSION_DESCRIPTION "\n");
161 MSG("Web site: http://lttng.org");
162 MSG("\n%s", lttng_license);
163 }
eb9cb8b7
DG
164
165end:
ca1c3607 166 poptFreeContext(pc);
eb9cb8b7
DG
167 return ret;
168}
This page took 0.063776 seconds and 5 git commands to generate.