Wrap calls to fmt::format to catch formatting exceptions
[lttng-tools.git] / src / bin / lttng / commands / version.cpp
CommitLineData
eb9cb8b7 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
eb9cb8b7 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
eb9cb8b7 5 *
eb9cb8b7
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
28ab034a
JG
9#include "../command.hpp"
10#include "version.hpp"
11
12#include <common/mi-lttng.hpp>
13
eb9cb8b7
DG
14#include <popt.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20#include <unistd.h>
21
4fc83d94
PP
22#ifdef LTTNG_EMBED_HELP
23static const char help_msg[] =
24#include <lttng-version.1.h>
28ab034a 25 ;
4fc83d94
PP
26#endif
27
eb9cb8b7
DG
28enum {
29 OPT_HELP = 1,
679b4943 30 OPT_LIST_OPTIONS,
eb9cb8b7
DG
31};
32
c7e35b03
JR
33static const char *lttng_license = "lttng is free software and under the GPL license and part LGPL";
34
eb9cb8b7
DG
35static struct poptOption long_options[] = {
36 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b
JG
37 { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
38 { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr },
39 { nullptr, 0, 0, nullptr, 0, nullptr, nullptr }
eb9cb8b7
DG
40};
41
c7e35b03
JR
42/*
43 * create_version
44 */
48a40005 45static void create_version(struct mi_lttng_version_data *version)
c7e35b03
JR
46{
47 strncpy(version->version, VERSION, NAME_MAX);
48 version->version_major = VERSION_MAJOR;
49 version->version_minor = VERSION_MINOR;
50 version->version_patchlevel = VERSION_PATCHLEVEL;
50bba0f3 51 strncpy(version->version_commit, GIT_VERSION, NAME_MAX);
c7e35b03
JR
52 strncpy(version->version_name, VERSION_NAME, NAME_MAX);
53 strncpy(version->package_url, PACKAGE_URL, NAME_MAX);
54}
55
56/*
57 * Print the machine interface output of this command.
58 */
cd9adb8b 59static int print_mi()
c7e35b03
JR
60{
61 int ret = CMD_SUCCESS;
cd9adb8b 62 struct mi_writer *writer = nullptr;
48a40005 63 struct mi_lttng_version_data version;
c7e35b03
JR
64
65 create_version(&version);
66
67 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
68 if (!writer) {
69 ret = -LTTNG_ERR_NOMEM;
70 goto end;
71 }
72
73 /* Open the command element */
28ab034a 74 ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_version);
c7e35b03
JR
75 if (ret) {
76 ret = CMD_ERROR;
77 goto error;
78 }
79
80 /* Beginning of output */
28ab034a 81 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
c7e35b03
JR
82 if (ret) {
83 ret = CMD_ERROR;
84 goto error;
85 }
86
87 /* Print the machine interface of version */
28ab034a 88 ret = mi_lttng_version(writer, &version, VERSION_DESCRIPTION, lttng_license);
c7e35b03
JR
89 if (ret) {
90 ret = CMD_ERROR;
91 goto error;
92 }
93
94 /* Close the output element */
95 ret = mi_lttng_writer_close_element(writer);
96 if (ret) {
97 ret = CMD_ERROR;
98 goto error;
99 }
100
101 /* Close the command */
102 ret = mi_lttng_writer_command_close(writer);
103 if (ret) {
104 ret = CMD_ERROR;
105 }
106
107error:
108 /* Cleanup */
109 if (writer && mi_lttng_writer_destroy(writer)) {
110 /* Preserve original error code */
111 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
112 }
113
114end:
115 return ret;
116}
117
eb9cb8b7
DG
118/*
119 * cmd_version
120 */
121int cmd_version(int argc, const char **argv)
122{
123 int opt, ret = CMD_SUCCESS;
124 static poptContext pc;
125
cd9adb8b 126 pc = poptGetContext(nullptr, argc, argv, long_options, 0);
eb9cb8b7
DG
127 poptReadDefaultConfig(pc, 0);
128
129 while ((opt = poptGetNextOpt(pc)) != -1) {
130 switch (opt) {
131 case OPT_HELP:
4ba92f18 132 SHOW_HELP();
eb9cb8b7 133 goto end;
679b4943
SM
134 case OPT_LIST_OPTIONS:
135 list_cmd_options(stdout, long_options);
679b4943 136 goto end;
eb9cb8b7 137 default:
eb9cb8b7
DG
138 ret = CMD_UNDEFINED;
139 goto end;
140 }
141 }
142
c7e35b03
JR
143 if (lttng_opt_mi) {
144 ret = print_mi();
145 } else {
4c6ac053 146 MSG("lttng version " VERSION " - " VERSION_NAME "%s",
28ab034a 147 GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
c7e35b03 148 MSG("\n" VERSION_DESCRIPTION "\n");
a18d9544 149 MSG("Web site: https://lttng.org");
c7e35b03 150 MSG("\n%s", lttng_license);
a3bc3918
JR
151 if (EXTRA_VERSION_NAME[0] != '\0') {
152 MSG("\nExtra version name: " EXTRA_VERSION_NAME);
153 }
154 if (EXTRA_VERSION_DESCRIPTION[0] != '\0') {
155 MSG("\nExtra version description:\n\t" EXTRA_VERSION_DESCRIPTION);
156 }
7f5ed73a
JR
157 if (EXTRA_VERSION_PATCHES[0] != '\0') {
158 MSG("\nExtra version patches:\n\t" EXTRA_VERSION_PATCHES);
159 }
c7e35b03 160 }
eb9cb8b7
DG
161
162end:
ca1c3607 163 poptFreeContext(pc);
eb9cb8b7
DG
164 return ret;
165}
This page took 0.102688 seconds and 4 git commands to generate.