2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* FlowLineNoise.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbcli/FlowLineNoise.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "flow/IThreadPool.h"
|
|
|
|
|
|
|
|
#define BOOST_SYSTEM_NO_LIB
|
|
|
|
#define BOOST_DATE_TIME_NO_LIB
|
|
|
|
#define BOOST_REGEX_NO_LIB
|
|
|
|
#include "boost/asio.hpp"
|
|
|
|
|
|
|
|
#include "flow/ThreadHelper.actor.h"
|
|
|
|
|
|
|
|
#if __unixish__
|
|
|
|
#define HAVE_LINENOISE 1
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbcli/linenoise/linenoise.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#else
|
|
|
|
#define HAVE_LINENOISE 0
|
|
|
|
#endif
|
2018-08-11 06:18:24 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-08 09:41:19 +08:00
|
|
|
struct LineNoiseReader final : IThreadPoolReceiver {
|
|
|
|
void init() override {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-08 12:58:24 +08:00
|
|
|
struct Read final : TypedAction<LineNoiseReader, Read> {
|
|
|
|
std::string prompt;
|
2017-05-26 04:48:44 +08:00
|
|
|
ThreadReturnPromise<Optional<std::string>> result;
|
|
|
|
|
2020-10-08 09:41:19 +08:00
|
|
|
double getTimeEstimate() const override { return 0.0; }
|
|
|
|
explicit Read(std::string const& prompt) : prompt(prompt) {}
|
2020-10-08 12:58:24 +08:00
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-08 12:58:24 +08:00
|
|
|
void action(Read& r) {
|
2017-05-26 04:48:44 +08:00
|
|
|
try {
|
|
|
|
r.result.send( read(r.prompt) );
|
|
|
|
} catch (Error& e) {
|
|
|
|
r.result.sendError(e);
|
|
|
|
} catch (...) {
|
|
|
|
r.result.sendError(unknown_error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Optional<std::string> read(std::string const& prompt) {
|
|
|
|
#if HAVE_LINENOISE
|
|
|
|
errno = 0;
|
|
|
|
char* line = linenoise(prompt.c_str());
|
|
|
|
if (line) {
|
|
|
|
std::string s(line);
|
|
|
|
free(line);
|
|
|
|
return s;
|
|
|
|
} else {
|
|
|
|
if (errno == EAGAIN) // Ctrl-C
|
|
|
|
return std::string();
|
|
|
|
return Optional<std::string>();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
std::string line;
|
|
|
|
std::fputs( prompt.c_str(), stdout );
|
|
|
|
if (!std::getline( std::cin, line ).eof()) {
|
|
|
|
return line;
|
|
|
|
} else
|
|
|
|
return Optional<std::string>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
LineNoise::LineNoise(
|
|
|
|
std::function< void(std::string const&, std::vector<std::string>&) > _completion_callback,
|
|
|
|
std::function< Hint(std::string const&) > _hint_callback,
|
|
|
|
int maxHistoryLines,
|
|
|
|
bool multiline )
|
|
|
|
: threadPool( createGenericThreadPool() )
|
|
|
|
{
|
|
|
|
reader = new LineNoiseReader();
|
|
|
|
|
|
|
|
#if HAVE_LINENOISE
|
|
|
|
// It should be OK to call these functions from this thread, since read() can't be called yet
|
|
|
|
// The callbacks passed to linenoise*() will be invoked from the thread pool, and use onMainThread() to safely invoke the callbacks we've been given
|
|
|
|
|
|
|
|
// linenoise doesn't provide any form of data parameter to callbacks, so we have to use static variables
|
|
|
|
static std::function< void(std::string const&, std::vector<std::string>&) > completion_callback;
|
|
|
|
static std::function< Hint(std::string const&) > hint_callback;
|
|
|
|
completion_callback = _completion_callback;
|
|
|
|
hint_callback = _hint_callback;
|
|
|
|
|
|
|
|
linenoiseHistorySetMaxLen( maxHistoryLines );
|
|
|
|
linenoiseSetMultiLine( multiline );
|
|
|
|
linenoiseSetCompletionCallback( [](const char* line, linenoiseCompletions* lc) {
|
|
|
|
// This code will run in the thread pool
|
|
|
|
std::vector<std::string> completions;
|
|
|
|
onMainThread( [line, &completions]() -> Future<Void> {
|
|
|
|
completion_callback(line, completions);
|
|
|
|
return Void();
|
|
|
|
}).getBlocking();
|
|
|
|
for( auto const& c : completions )
|
|
|
|
linenoiseAddCompletion( lc, c.c_str() );
|
|
|
|
});
|
2020-02-22 08:50:59 +08:00
|
|
|
linenoiseSetHintsCallback( [](const char* line, int* color, int*bold) -> char* {
|
2017-05-26 04:48:44 +08:00
|
|
|
Hint h = onMainThread( [line]() -> Future<Hint> {
|
|
|
|
return hint_callback(line);
|
|
|
|
}).getBlocking();
|
2020-08-19 05:30:20 +08:00
|
|
|
if (!h.valid) return nullptr;
|
2017-05-26 04:48:44 +08:00
|
|
|
*color = h.color;
|
|
|
|
*bold = h.bold;
|
|
|
|
return strdup( h.text.c_str() );
|
|
|
|
});
|
2020-02-22 08:50:59 +08:00
|
|
|
linenoiseSetFreeHintsCallback( free );
|
2017-05-26 04:48:44 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
threadPool->addThread(reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
LineNoise::~LineNoise() {
|
|
|
|
threadPool.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> LineNoise::read( std::string const& prompt ) {
|
|
|
|
auto r = new LineNoiseReader::Read(prompt);
|
|
|
|
auto f = r->result.getFuture();
|
|
|
|
threadPool->post(r);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Void> waitKeyboardInterrupt(boost::asio::io_service* ios) {
|
|
|
|
state boost::asio::signal_set signals(*ios, SIGINT);
|
|
|
|
Promise<Void> result;
|
|
|
|
signals.async_wait([result](const boost::system::error_code& error, int signal_number) {
|
|
|
|
if (error) {
|
|
|
|
result.sendError(io_error());
|
|
|
|
} else {
|
|
|
|
result.send(Void());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(result.getFuture());
|
2017-05-26 04:48:44 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Void> LineNoise::onKeyboardInterrupt() {
|
|
|
|
boost::asio::io_service* ios = (boost::asio::io_service*)g_network->global(INetwork::enASIOService);
|
|
|
|
if (!ios) return Never();
|
|
|
|
return waitKeyboardInterrupt(ios);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineNoise::historyAdd( std::string const& line ) {
|
|
|
|
#if HAVE_LINENOISE
|
|
|
|
linenoiseHistoryAdd( line.c_str() );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void LineNoise::historyLoad( std::string const& filename ) {
|
|
|
|
#if HAVE_LINENOISE
|
|
|
|
if(linenoiseHistoryLoad(filename.c_str()) != 0) {
|
|
|
|
throw io_error();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void LineNoise::historySave( std::string const& filename ) {
|
|
|
|
#if HAVE_LINENOISE
|
|
|
|
if(linenoiseHistorySave(filename.c_str()) != 0) {
|
|
|
|
throw io_error();
|
|
|
|
}
|
|
|
|
#endif
|
2018-08-11 06:18:24 +08:00
|
|
|
}
|