forked from osmcode/osmium-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_show.cpp
245 lines (199 loc) · 7.38 KB
/
command_show.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Osmium -- OpenStreetMap data manipulation command line tool
https://osmcode.org/osmium-tool/
Copyright (C) 2013-2025 Jochen Topf <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "command_show.hpp"
#include "exception.hpp"
#include "util.hpp"
#include <osmium/io/file.hpp>
#include <osmium/io/header.hpp>
#include <osmium/io/reader.hpp>
#include <osmium/io/writer.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/util/verbose_output.hpp>
#include <boost/program_options.hpp>
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <string>
#include <system_error>
#include <utility>
#include <vector>
#ifndef _WIN32
# include <unistd.h>
#endif
#ifndef _WIN32
void CommandShow::setup_pager_from_env() noexcept {
m_pager = "less";
const char* pager = ::getenv("OSMIUM_PAGER"); // NOLINT(concurrency-mt-unsafe)
if (pager) {
m_pager = pager;
} else {
pager = ::getenv("PAGER"); // NOLINT(concurrency-mt-unsafe)
if (pager) {
m_pager = pager;
}
}
if (m_pager == "cat") {
m_pager = "";
}
}
#endif
bool CommandShow::setup(const std::vector<std::string>& arguments) {
po::options_description opts_cmd{"COMMAND OPTIONS"};
opts_cmd.add_options()
("format-debug,d", "Use debug format")
("format-opl,o", "Use OPL format")
("format-xml,x", "Use XML format")
#ifndef _WIN32
("no-pager", "Do not run pager program")
#endif
("object-type,t", po::value<std::vector<std::string>>(), "Read only objects of given type (node, way, relation, changeset)")
("output-format,f", po::value<std::string>(), "Format of output file")
;
const po::options_description opts_common{add_common_options(false)};
const po::options_description opts_input{add_single_input_options()};
po::options_description hidden;
hidden.add_options()
("input-filename", po::value<std::string>(), "Input file")
;
po::options_description desc;
desc.add(opts_cmd).add(opts_common).add(opts_input);
po::options_description parsed_options;
parsed_options.add(desc).add(hidden);
po::positional_options_description positional;
positional.add("input-filename", 1);
po::variables_map vm;
po::store(po::command_line_parser(arguments).options(parsed_options).positional(positional).run(), vm);
po::notify(vm);
if (!setup_common(vm, desc)) {
return false;
}
setup_object_type_nwrc(vm);
setup_input_file(vm);
#ifndef _WIN32
if (vm.count("no-pager")) {
m_pager = "";
} else {
setup_pager_from_env();
}
#endif
if (vm.count("output-format") &&
vm.count("format-debug") &&
vm.count("format-opl") &&
vm.count("format-xml")) {
throw argument_error{"You can only use at most one of the following options: --output-format/-f, --format-debug/-d, --format-opl/-o, and --format-xml/-x."};
}
if (vm.count("output-format")) {
m_output_format = vm["output-format"].as<std::string>();
} else if (vm.count("format-debug")) {
m_output_format = "debug,color=true";
} else if (vm.count("format-opl")) {
m_output_format = "opl";
} else if (vm.count("format-xml")) {
m_output_format = "xml";
} else {
const char* output_format_from_env = ::getenv("OSMIUM_SHOW_FORMAT"); // NOLINT(concurrency-mt-unsafe)
if (output_format_from_env) {
m_output_format = output_format_from_env;
}
}
m_color_output = m_output_format.find("color=true") != std::string::npos;
return true;
}
void CommandShow::show_arguments() {
show_single_input_arguments(m_vout);
m_vout << " other options:\n";
m_vout << " file format: " << m_output_format << "\n";
m_vout << " use color: " << yes_no(m_color_output);
m_vout << " use pager: " << (m_pager.empty() ? "(no pager)" : m_pager) << "\n";
show_object_types(m_vout);
}
#ifndef _WIN32
namespace {
int execute_pager(const std::string& pager, bool with_color) {
int pipefd[2];
if (::pipe(pipefd) < 0) {
throw std::system_error{errno, std::system_category(), "Could not run pager: pipe() call failed"};
}
const pid_t pid = fork();
if (pid < 0) {
throw std::system_error{errno, std::system_category(), "Could not run pager: fork() call failed"};
}
if (pid == 0) {
// child
::close(pipefd[1]); // close write end of the pipe
::close(0); // close stdin
if (::dup2(pipefd[0], 0) < 0) { // put end of pipe as stdin
throw std::system_error{errno, std::system_category(), "Could not run pager: dup2() call failed"};
}
if (with_color && pager.size() >= 4 && pager.substr(pager.size() - 4, 4) == "less") {
::execlp(pager.c_str(), pager.c_str(), "-R", nullptr);
} else {
// execute pager without arguments
::execlp(pager.c_str(), pager.c_str(), nullptr);
}
// Exec will either succeed and never return here, or it fails and
// we'll exit.
throw std::system_error{errno, std::system_category(), "Could not run pager: execlp() call failed"};
}
// parent
::close(pipefd[0]); // close read end of the pipe
return pipefd[1];
}
} // anonymous namespace
#endif
bool CommandShow::run() {
osmium::io::Reader reader{m_input_file, osm_entity_bits()};
const osmium::io::Header header{reader.header()};
if (m_pager.empty()) {
const osmium::io::File file{"-", m_output_format};
osmium::io::Writer writer{file, header};
while (osmium::memory::Buffer buffer = reader.read()) {
writer(std::move(buffer));
}
writer.close();
} else {
#ifndef _WIN32
const int fd = execute_pager(m_pager, m_color_output);
if (::dup2(fd, 1) < 0) { // put end of pipe as stdout
throw std::system_error{errno, std::system_category(), "Could not run pager: dup2() call failed"};
}
::close(fd);
const osmium::io::File file{"-", m_output_format};
osmium::io::Writer writer{file, header};
try {
while (osmium::memory::Buffer buffer = reader.read()) {
writer(std::move(buffer));
}
} catch (const std::system_error& e) {
if (e.code().value() != EPIPE) {
throw;
}
}
writer.close();
::close(1); // close stdout to signal EOF to pager
int status = 0;
const int pid = ::wait(&status);
if (pid < 0) {
throw std::system_error{errno, std::system_category(), "Could not run pager: wait() call failed"};
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 1) { // NOLINT(hicpp-signed-bitwise)
throw argument_error{std::string{"Could not run pager '"} + m_pager + "'"};
}
#endif
}
reader.close();
return true;
}