2015-07-08 13:35:36 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015, Timothy Stack
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Timothy Stack nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
2022-03-16 22:38:08 +00:00
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2015-07-08 13:35:36 +00:00
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LNAV_COMMAND_EXECUTOR_H
|
|
|
|
#define LNAV_COMMAND_EXECUTOR_H
|
|
|
|
|
2016-12-06 00:34:30 +00:00
|
|
|
#include <future>
|
2022-03-16 22:38:08 +00:00
|
|
|
#include <stack>
|
2015-07-08 13:35:36 +00:00
|
|
|
#include <string>
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2022-03-31 15:59:19 +00:00
|
|
|
#include "base/auto_fd.hh"
|
2022-04-12 23:07:13 +00:00
|
|
|
#include "base/lnav.console.hh"
|
2020-11-25 22:46:39 +00:00
|
|
|
#include "bookmarks.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "fmt/format.h"
|
|
|
|
#include "ghc/filesystem.hpp"
|
2022-04-12 23:07:13 +00:00
|
|
|
#include "help_text.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "optional.hpp"
|
2022-03-13 22:49:41 +00:00
|
|
|
#include "shlex.resolver.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "vis_line.hh"
|
2017-03-31 14:01:11 +00:00
|
|
|
|
2016-12-06 00:34:30 +00:00
|
|
|
struct exec_context;
|
2022-03-13 22:49:41 +00:00
|
|
|
class attr_line_t;
|
|
|
|
class logline_value;
|
2022-07-25 06:29:46 +00:00
|
|
|
struct logline_value_vector;
|
2015-07-08 13:35:36 +00:00
|
|
|
|
2022-03-31 15:59:19 +00:00
|
|
|
using sql_callback_t = int (*)(exec_context&, sqlite3_stmt*);
|
2022-03-16 22:38:08 +00:00
|
|
|
int sql_callback(exec_context& ec, sqlite3_stmt* stmt);
|
2015-07-08 13:35:36 +00:00
|
|
|
|
2022-03-31 15:59:19 +00:00
|
|
|
using pipe_callback_t
|
|
|
|
= std::future<std::string> (*)(exec_context&, const std::string&, auto_fd&);
|
2016-12-06 00:34:30 +00:00
|
|
|
|
2022-05-11 04:58:32 +00:00
|
|
|
using error_callback_t
|
|
|
|
= std::function<void(const lnav::console::user_message&)>;
|
|
|
|
|
2016-12-06 00:34:30 +00:00
|
|
|
struct exec_context {
|
2021-03-18 05:57:00 +00:00
|
|
|
enum class perm_t {
|
|
|
|
READ_WRITE,
|
|
|
|
READ_ONLY,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
using output_t = std::pair<FILE*, int (*)(FILE*)>;
|
2021-08-22 20:50:53 +00:00
|
|
|
|
2022-07-25 06:29:46 +00:00
|
|
|
exec_context(logline_value_vector* line_values = nullptr,
|
2021-01-18 07:29:08 +00:00
|
|
|
sql_callback_t sql_callback = ::sql_callback,
|
2022-03-13 22:49:41 +00:00
|
|
|
pipe_callback_t pipe_callback = nullptr);
|
2018-05-17 14:06:50 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
bool is_read_write() const
|
|
|
|
{
|
2021-03-18 05:57:00 +00:00
|
|
|
return this->ec_perms == perm_t::READ_WRITE;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
bool is_read_only() const
|
|
|
|
{
|
2021-03-18 05:57:00 +00:00
|
|
|
return this->ec_perms == perm_t::READ_ONLY;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
exec_context& with_perms(perm_t perms)
|
|
|
|
{
|
2021-03-18 05:57:00 +00:00
|
|
|
this->ec_perms = perms;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-12 23:07:13 +00:00
|
|
|
void add_error_context(lnav::console::user_message& um);
|
2020-04-25 14:32:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
template<typename... Args>
|
2022-04-12 23:07:13 +00:00
|
|
|
Result<std::string, lnav::console::user_message> make_error(
|
|
|
|
fmt::string_view format_str, const Args&... args)
|
2022-03-16 22:38:08 +00:00
|
|
|
{
|
2022-04-12 23:07:13 +00:00
|
|
|
auto retval = lnav::console::user_message::error(
|
|
|
|
fmt::vformat(format_str, fmt::make_format_args(args...)));
|
|
|
|
|
|
|
|
this->add_error_context(retval);
|
|
|
|
|
|
|
|
return Err(retval);
|
2016-12-06 00:34:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
nonstd::optional<FILE*> get_output()
|
|
|
|
{
|
2018-10-12 14:12:35 +00:00
|
|
|
for (auto iter = this->ec_output_stack.rbegin();
|
|
|
|
iter != this->ec_output_stack.rend();
|
2022-03-16 22:38:08 +00:00
|
|
|
++iter)
|
|
|
|
{
|
2021-08-22 20:50:53 +00:00
|
|
|
if (iter->second && (*iter->second).first) {
|
|
|
|
return (*iter->second).first;
|
2018-10-12 14:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nonstd::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
void set_output(const std::string& name, FILE* file, int (*closer)(FILE*));
|
2021-02-25 23:47:36 +00:00
|
|
|
|
|
|
|
void clear_output();
|
|
|
|
|
2020-04-25 14:32:05 +00:00
|
|
|
struct source_guard {
|
2022-05-11 04:58:32 +00:00
|
|
|
source_guard(exec_context* context) : sg_context(context) {}
|
|
|
|
|
|
|
|
source_guard(const source_guard&) = delete;
|
|
|
|
|
|
|
|
source_guard(source_guard&& other) : sg_context(other.sg_context)
|
|
|
|
{
|
|
|
|
other.sg_context = nullptr;
|
|
|
|
}
|
2020-04-25 14:32:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
~source_guard()
|
|
|
|
{
|
2022-05-11 04:58:32 +00:00
|
|
|
if (this->sg_context != nullptr) {
|
2022-06-22 05:16:57 +00:00
|
|
|
this->sg_context->ec_source.pop_back();
|
2022-05-11 04:58:32 +00:00
|
|
|
}
|
2020-04-25 14:32:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 04:58:32 +00:00
|
|
|
exec_context* sg_context;
|
2020-04-25 14:32:05 +00:00
|
|
|
};
|
|
|
|
|
2021-02-25 23:47:36 +00:00
|
|
|
struct output_guard {
|
2022-03-16 22:38:08 +00:00
|
|
|
explicit output_guard(exec_context& context,
|
2021-02-25 23:47:36 +00:00
|
|
|
std::string name = "default",
|
2022-03-16 22:38:08 +00:00
|
|
|
const nonstd::optional<output_t>& file
|
|
|
|
= nonstd::nullopt);
|
2021-02-25 23:47:36 +00:00
|
|
|
|
|
|
|
~output_guard();
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
exec_context& sg_context;
|
2021-02-25 23:47:36 +00:00
|
|
|
};
|
|
|
|
|
2022-04-30 20:05:42 +00:00
|
|
|
source_guard enter_source(intern_string_t path,
|
2022-04-12 23:07:13 +00:00
|
|
|
int line_number,
|
2022-05-23 03:44:18 +00:00
|
|
|
const std::string& content);
|
2022-05-11 04:58:32 +00:00
|
|
|
|
|
|
|
struct error_cb_guard {
|
|
|
|
error_cb_guard(exec_context* context) : sg_context(context) {}
|
|
|
|
|
|
|
|
error_cb_guard(const error_cb_guard&) = delete;
|
|
|
|
error_cb_guard(error_cb_guard&& other) : sg_context(other.sg_context)
|
|
|
|
{
|
|
|
|
other.sg_context = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~error_cb_guard()
|
|
|
|
{
|
|
|
|
if (this->sg_context != nullptr) {
|
|
|
|
this->sg_context->ec_error_callback_stack.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exec_context* sg_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
error_cb_guard add_error_callback(error_callback_t cb)
|
|
|
|
{
|
|
|
|
this->ec_error_callback_stack.emplace_back(std::move(cb));
|
|
|
|
return {this};
|
2020-04-25 14:32:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
scoped_resolver create_resolver()
|
|
|
|
{
|
2020-10-01 04:55:37 +00:00
|
|
|
return {
|
|
|
|
&this->ec_local_vars.top(),
|
|
|
|
&this->ec_global_vars,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-11 04:58:32 +00:00
|
|
|
void execute(const std::string& cmdline);
|
|
|
|
|
|
|
|
using kv_pair_t = std::pair<std::string, std::string>;
|
|
|
|
|
|
|
|
void execute_with_int(const std::string& cmdline)
|
|
|
|
{
|
|
|
|
this->execute(cmdline);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void execute_with_int(const std::string& cmdline,
|
|
|
|
kv_pair_t pair,
|
|
|
|
Args... args)
|
|
|
|
{
|
|
|
|
this->ec_local_vars.top().template emplace(pair);
|
|
|
|
this->execute(cmdline, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void execute_with(const std::string& cmdline, Args... args)
|
|
|
|
{
|
|
|
|
this->ec_local_vars.push({});
|
|
|
|
this->execute_with_int(cmdline, args...);
|
|
|
|
this->ec_local_vars.pop();
|
|
|
|
}
|
|
|
|
|
2021-03-18 05:57:00 +00:00
|
|
|
vis_line_t ec_top_line{0_vl};
|
|
|
|
bool ec_dry_run{false};
|
|
|
|
perm_t ec_perms{perm_t::READ_WRITE};
|
2016-12-06 00:34:30 +00:00
|
|
|
|
|
|
|
std::map<std::string, std::string> ec_override;
|
2022-07-25 06:29:46 +00:00
|
|
|
logline_value_vector* ec_line_values;
|
2022-07-21 22:44:30 +00:00
|
|
|
std::stack<std::map<std::string, scoped_value_t>> ec_local_vars;
|
|
|
|
std::map<std::string, scoped_value_t> ec_global_vars;
|
2020-10-21 05:55:46 +00:00
|
|
|
std::vector<ghc::filesystem::path> ec_path_stack;
|
2022-06-22 05:16:57 +00:00
|
|
|
std::vector<lnav::console::snippet> ec_source;
|
2022-04-12 23:07:13 +00:00
|
|
|
help_text* ec_current_help{nullptr};
|
2021-08-22 20:50:53 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
std::vector<std::pair<std::string, nonstd::optional<output_t>>>
|
|
|
|
ec_output_stack;
|
2016-12-06 00:34:30 +00:00
|
|
|
|
2022-03-13 22:49:41 +00:00
|
|
|
std::unique_ptr<attr_line_t> ec_accumulator;
|
2016-12-06 00:34:30 +00:00
|
|
|
|
|
|
|
sql_callback_t ec_sql_callback;
|
|
|
|
pipe_callback_t ec_pipe_callback;
|
2022-05-11 04:58:32 +00:00
|
|
|
std::vector<error_callback_t> ec_error_callback_stack;
|
2016-12-06 00:34:30 +00:00
|
|
|
};
|
|
|
|
|
2022-04-12 23:07:13 +00:00
|
|
|
Result<std::string, lnav::console::user_message> execute_command(
|
|
|
|
exec_context& ec, const std::string& cmdline);
|
|
|
|
|
|
|
|
Result<std::string, lnav::console::user_message> execute_sql(
|
|
|
|
exec_context& ec, const std::string& sql, std::string& alt_msg);
|
|
|
|
Result<std::string, lnav::console::user_message> execute_file(
|
|
|
|
exec_context& ec, const std::string& path_and_args, bool multiline = true);
|
|
|
|
Result<std::string, lnav::console::user_message> execute_any(
|
|
|
|
exec_context& ec, const std::string& cmdline);
|
2022-03-16 22:38:08 +00:00
|
|
|
void execute_init_commands(
|
|
|
|
exec_context& ec,
|
2022-04-12 23:07:13 +00:00
|
|
|
std::vector<std::pair<Result<std::string, lnav::console::user_message>,
|
|
|
|
std::string>>& msgs);
|
2022-03-16 22:38:08 +00:00
|
|
|
|
|
|
|
std::future<std::string> pipe_callback(exec_context& ec,
|
|
|
|
const std::string& cmdline,
|
|
|
|
auto_fd& fd);
|
|
|
|
|
|
|
|
int sql_progress(const struct log_cursor& lc);
|
2020-04-25 14:32:05 +00:00
|
|
|
void sql_progress_finished();
|
2017-01-18 14:17:55 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
void add_global_vars(exec_context& ec);
|
2018-05-10 13:44:03 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
#endif // LNAV_COMMAND_EXECUTOR_H
|