Commit | Line | Data |
---|---|---|
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 | ||
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> | |
3bd1e081 | 26 | #include <config.h> |
eb9cb8b7 | 27 | |
c7e35b03 JR |
28 | #include <common/mi-lttng.h> |
29 | ||
c399183f | 30 | #include "../command.h" |
eb9cb8b7 DG |
31 | |
32 | enum { | |
33 | OPT_HELP = 1, | |
679b4943 | 34 | OPT_LIST_OPTIONS, |
eb9cb8b7 DG |
35 | }; |
36 | ||
c7e35b03 JR |
37 | static const char *lttng_license = "lttng is free software and under the GPL license and part LGPL"; |
38 | ||
eb9cb8b7 DG |
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}, | |
679b4943 | 42 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
eb9cb8b7 DG |
43 | {0, 0, 0, 0, 0, 0, 0} |
44 | }; | |
45 | ||
46 | /* | |
47 | * usage | |
48 | */ | |
49 | static void usage(FILE *ofp) | |
50 | { | |
32a6298d | 51 | fprintf(ofp, "usage: lttng version [OPTIONS]\n"); |
eb9cb8b7 | 52 | fprintf(ofp, "\n"); |
32a6298d | 53 | fprintf(ofp, "Options:\n"); |
eb9cb8b7 | 54 | fprintf(ofp, " -h, --help Show this help\n"); |
679b4943 | 55 | fprintf(ofp, " --list-options Simple listing of options\n"); |
eb9cb8b7 DG |
56 | fprintf(ofp, "\n"); |
57 | } | |
58 | ||
c7e35b03 JR |
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; | |
50bba0f3 | 68 | strncpy(version->version_commit, GIT_VERSION, NAME_MAX); |
c7e35b03 JR |
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 | ||
eb9cb8b7 DG |
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: | |
ca1c3607 | 152 | usage(stdout); |
eb9cb8b7 | 153 | goto end; |
679b4943 SM |
154 | case OPT_LIST_OPTIONS: |
155 | list_cmd_options(stdout, long_options); | |
679b4943 | 156 | goto end; |
eb9cb8b7 DG |
157 | default: |
158 | usage(stderr); | |
159 | ret = CMD_UNDEFINED; | |
160 | goto end; | |
161 | } | |
162 | } | |
163 | ||
c7e35b03 JR |
164 | if (lttng_opt_mi) { |
165 | ret = print_mi(); | |
166 | } else { | |
4c6ac053 MD |
167 | MSG("lttng version " VERSION " - " VERSION_NAME "%s", |
168 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); | |
c7e35b03 JR |
169 | MSG("\n" VERSION_DESCRIPTION "\n"); |
170 | MSG("Web site: http://lttng.org"); | |
171 | MSG("\n%s", lttng_license); | |
172 | } | |
eb9cb8b7 DG |
173 | |
174 | end: | |
ca1c3607 | 175 | poptFreeContext(pc); |
eb9cb8b7 DG |
176 | return ret; |
177 | } |