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