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