Refactor: embed mi in "del_record" to remove code duplication
[deliverable/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
c7e35b03
JR
45/*
46 * create_version
47 */
48static void create_version(struct mi_lttng_version *version)
49{
50 strncpy(version->version, VERSION, NAME_MAX);
51 version->version_major = VERSION_MAJOR;
52 version->version_minor = VERSION_MINOR;
53 version->version_patchlevel = VERSION_PATCHLEVEL;
50bba0f3 54 strncpy(version->version_commit, GIT_VERSION, NAME_MAX);
c7e35b03
JR
55 strncpy(version->version_name, VERSION_NAME, NAME_MAX);
56 strncpy(version->package_url, PACKAGE_URL, NAME_MAX);
57}
58
59/*
60 * Print the machine interface output of this command.
61 */
62static int print_mi()
63{
64 int ret = CMD_SUCCESS;
65 struct mi_writer *writer = NULL;
66 struct mi_lttng_version version;
67
68 create_version(&version);
69
70 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
71 if (!writer) {
72 ret = -LTTNG_ERR_NOMEM;
73 goto end;
74 }
75
76 /* Open the command element */
77 ret = mi_lttng_writer_command_open(writer,
78 mi_lttng_element_command_version);
79 if (ret) {
80 ret = CMD_ERROR;
81 goto error;
82 }
83
84 /* Beginning of output */
85 ret = mi_lttng_writer_open_element(writer,
86 mi_lttng_element_command_output);
87 if (ret) {
88 ret = CMD_ERROR;
89 goto error;
90 }
91
92 /* Print the machine interface of version */
93 ret = mi_lttng_version(writer, &version,
94 VERSION_DESCRIPTION, lttng_license);
95 if (ret) {
96 ret = CMD_ERROR;
97 goto error;
98 }
99
100 /* Close the output element */
101 ret = mi_lttng_writer_close_element(writer);
102 if (ret) {
103 ret = CMD_ERROR;
104 goto error;
105 }
106
107 /* Close the command */
108 ret = mi_lttng_writer_command_close(writer);
109 if (ret) {
110 ret = CMD_ERROR;
111 }
112
113error:
114 /* Cleanup */
115 if (writer && mi_lttng_writer_destroy(writer)) {
116 /* Preserve original error code */
117 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
118 }
119
120end:
121 return ret;
122}
123
eb9cb8b7
DG
124/*
125 * cmd_version
126 */
127int cmd_version(int argc, const char **argv)
128{
129 int opt, ret = CMD_SUCCESS;
130 static poptContext pc;
131
132 pc = poptGetContext(NULL, argc, argv, long_options, 0);
133 poptReadDefaultConfig(pc, 0);
134
135 while ((opt = poptGetNextOpt(pc)) != -1) {
136 switch (opt) {
137 case OPT_HELP:
4ba92f18 138 SHOW_HELP();
eb9cb8b7 139 goto end;
679b4943
SM
140 case OPT_LIST_OPTIONS:
141 list_cmd_options(stdout, long_options);
679b4943 142 goto end;
eb9cb8b7 143 default:
eb9cb8b7
DG
144 ret = CMD_UNDEFINED;
145 goto end;
146 }
147 }
148
c7e35b03
JR
149 if (lttng_opt_mi) {
150 ret = print_mi();
151 } else {
4c6ac053
MD
152 MSG("lttng version " VERSION " - " VERSION_NAME "%s",
153 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
c7e35b03
JR
154 MSG("\n" VERSION_DESCRIPTION "\n");
155 MSG("Web site: http://lttng.org");
156 MSG("\n%s", lttng_license);
157 }
eb9cb8b7
DG
158
159end:
ca1c3607 160 poptFreeContext(pc);
eb9cb8b7
DG
161 return ret;
162}
This page took 0.07503 seconds and 5 git commands to generate.