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