CLI: show event filter string
[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
DG
30
31enum {
32 OPT_HELP = 1,
679b4943 33 OPT_LIST_OPTIONS,
eb9cb8b7
DG
34};
35
c7e35b03
JR
36static const char *lttng_license = "lttng is free software and under the GPL license and part LGPL";
37
eb9cb8b7
DG
38static struct poptOption long_options[] = {
39 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
40 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
679b4943 41 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
eb9cb8b7
DG
42 {0, 0, 0, 0, 0, 0, 0}
43};
44
45/*
46 * usage
47 */
48static void usage(FILE *ofp)
49{
32a6298d 50 fprintf(ofp, "usage: lttng version [OPTIONS]\n");
eb9cb8b7 51 fprintf(ofp, "\n");
32a6298d 52 fprintf(ofp, "Options:\n");
eb9cb8b7 53 fprintf(ofp, " -h, --help Show this help\n");
679b4943 54 fprintf(ofp, " --list-options Simple listing of options\n");
eb9cb8b7
DG
55 fprintf(ofp, "\n");
56}
57
c7e35b03
JR
58/*
59 * create_version
60 */
61static void create_version(struct mi_lttng_version *version)
62{
63 strncpy(version->version, VERSION, NAME_MAX);
64 version->version_major = VERSION_MAJOR;
65 version->version_minor = VERSION_MINOR;
66 version->version_patchlevel = VERSION_PATCHLEVEL;
50bba0f3 67 strncpy(version->version_commit, GIT_VERSION, NAME_MAX);
c7e35b03
JR
68 strncpy(version->version_name, VERSION_NAME, NAME_MAX);
69 strncpy(version->package_url, PACKAGE_URL, NAME_MAX);
70}
71
72/*
73 * Print the machine interface output of this command.
74 */
75static int print_mi()
76{
77 int ret = CMD_SUCCESS;
78 struct mi_writer *writer = NULL;
79 struct mi_lttng_version version;
80
81 create_version(&version);
82
83 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
84 if (!writer) {
85 ret = -LTTNG_ERR_NOMEM;
86 goto end;
87 }
88
89 /* Open the command element */
90 ret = mi_lttng_writer_command_open(writer,
91 mi_lttng_element_command_version);
92 if (ret) {
93 ret = CMD_ERROR;
94 goto error;
95 }
96
97 /* Beginning of output */
98 ret = mi_lttng_writer_open_element(writer,
99 mi_lttng_element_command_output);
100 if (ret) {
101 ret = CMD_ERROR;
102 goto error;
103 }
104
105 /* Print the machine interface of version */
106 ret = mi_lttng_version(writer, &version,
107 VERSION_DESCRIPTION, lttng_license);
108 if (ret) {
109 ret = CMD_ERROR;
110 goto error;
111 }
112
113 /* Close the output element */
114 ret = mi_lttng_writer_close_element(writer);
115 if (ret) {
116 ret = CMD_ERROR;
117 goto error;
118 }
119
120 /* Close the command */
121 ret = mi_lttng_writer_command_close(writer);
122 if (ret) {
123 ret = CMD_ERROR;
124 }
125
126error:
127 /* Cleanup */
128 if (writer && mi_lttng_writer_destroy(writer)) {
129 /* Preserve original error code */
130 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
131 }
132
133end:
134 return ret;
135}
136
eb9cb8b7
DG
137/*
138 * cmd_version
139 */
140int cmd_version(int argc, const char **argv)
141{
142 int opt, ret = CMD_SUCCESS;
143 static poptContext pc;
144
145 pc = poptGetContext(NULL, argc, argv, long_options, 0);
146 poptReadDefaultConfig(pc, 0);
147
148 while ((opt = poptGetNextOpt(pc)) != -1) {
149 switch (opt) {
150 case OPT_HELP:
ca1c3607 151 usage(stdout);
eb9cb8b7 152 goto end;
679b4943
SM
153 case OPT_LIST_OPTIONS:
154 list_cmd_options(stdout, long_options);
679b4943 155 goto end;
eb9cb8b7
DG
156 default:
157 usage(stderr);
158 ret = CMD_UNDEFINED;
159 goto end;
160 }
161 }
162
c7e35b03
JR
163 if (lttng_opt_mi) {
164 ret = print_mi();
165 } else {
4c6ac053
MD
166 MSG("lttng version " VERSION " - " VERSION_NAME "%s",
167 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
c7e35b03
JR
168 MSG("\n" VERSION_DESCRIPTION "\n");
169 MSG("Web site: http://lttng.org");
170 MSG("\n%s", lttng_license);
171 }
eb9cb8b7
DG
172
173end:
ca1c3607 174 poptFreeContext(pc);
eb9cb8b7
DG
175 return ret;
176}
This page took 0.045631 seconds and 4 git commands to generate.