MCPcopy Create free account
hub / github.com/ByConity/ByConity / openEditor

Method openEditor

base/common/ReplxxLineReader.cpp:279–339  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

277}
278
279void ReplxxLineReader::openEditor()
280{
281 char filename[] = "clickhouse_replxx_XXXXXX.sql";
282 int fd = ::mkstemps(filename, 4);
283 if (-1 == fd)
284 {
285 rx.print("Cannot create temporary file to edit query: %s\n", errnoToString(errno).c_str());
286 return;
287 }
288
289 const char * editor = std::getenv("EDITOR");
290 if (!editor || !*editor)
291 editor = "vim";
292
293 replxx::Replxx::State state(rx.get_state());
294
295 size_t bytes_written = 0;
296 const char * begin = state.text();
297 size_t offset = strlen(state.text());
298 while (bytes_written != offset)
299 {
300 ssize_t res = ::write(fd, begin + bytes_written, offset - bytes_written);
301 if ((-1 == res || 0 == res) && errno != EINTR)
302 {
303 rx.print("Cannot write to temporary query file %s: %s\n", filename, errnoToString(errno).c_str());
304 break;
305 }
306 bytes_written += res;
307 }
308
309 if (0 != ::close(fd))
310 {
311 rx.print("Cannot close temporary query file %s: %s\n", filename, errnoToString(errno).c_str());
312 return;
313 }
314
315 if (0 == execute(fmt::format("{} {}", editor, filename)))
316 {
317 try
318 {
319 std::ifstream t(filename);
320 std::string str;
321 t.seekg(0, std::ios::end);
322 str.reserve(t.tellg());
323 t.seekg(0, std::ios::beg);
324 str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
325 rx.set_state(replxx::Replxx::State(str.c_str(), str.size()));
326 }
327 catch (...)
328 {
329 rx.print("Cannot read from temporary query file %s: %s\n", filename, errnoToString(errno).c_str());
330 return;
331 }
332 }
333
334 if (bracketed_paste_enabled)
335 enableBracketedPaste();
336

Callers

nothing calls this directly

Calls 12

mkstempsFunction · 0.85
errnoToStringFunction · 0.85
getenvFunction · 0.85
closeFunction · 0.85
textMethod · 0.80
writeFunction · 0.50
executeFunction · 0.50
StateClass · 0.50
printMethod · 0.45
reserveMethod · 0.45
assignMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected