2013-06-06 14:01:32 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013, 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;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @file lnav_config.cc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2013-06-06 14:10:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-03-02 00:35:30 +00:00
|
|
|
#include <glob.h>
|
2013-06-06 14:01:32 +00:00
|
|
|
#include <sys/stat.h>
|
2015-08-10 04:03:23 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2018-11-09 17:45:19 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
|
2021-01-29 19:12:02 +00:00
|
|
|
#include <regex>
|
2019-07-30 05:18:32 +00:00
|
|
|
#include <chrono>
|
2018-11-09 17:45:19 +00:00
|
|
|
#include <iostream>
|
2019-05-03 20:50:19 +00:00
|
|
|
#include <stdexcept>
|
2019-05-08 12:30:59 +00:00
|
|
|
#include <fmt/format.h>
|
2015-08-10 04:03:23 +00:00
|
|
|
|
2015-09-15 07:24:56 +00:00
|
|
|
#include "auto_fd.hh"
|
2021-01-17 06:23:20 +00:00
|
|
|
#include "base/injector.hh"
|
|
|
|
#include "base/injector.bind.hh"
|
2020-10-29 04:23:46 +00:00
|
|
|
#include "base/string_util.hh"
|
2019-05-08 12:30:59 +00:00
|
|
|
#include "base/lnav_log.hh"
|
2019-05-03 20:50:19 +00:00
|
|
|
#include "lnav_util.hh"
|
2014-03-02 00:35:30 +00:00
|
|
|
#include "auto_mem.hh"
|
2015-08-10 04:03:23 +00:00
|
|
|
#include "auto_pid.hh"
|
2013-06-06 14:01:32 +00:00
|
|
|
#include "lnav_config.hh"
|
2019-05-08 12:30:59 +00:00
|
|
|
#include "yajlpp/yajlpp.hh"
|
|
|
|
#include "yajlpp/yajlpp_def.hh"
|
2019-05-03 20:50:19 +00:00
|
|
|
#include "styling.hh"
|
2020-12-30 21:49:29 +00:00
|
|
|
#include "bin2c.hh"
|
2019-05-15 16:13:56 +00:00
|
|
|
#include "default-config.h"
|
2013-06-06 14:01:32 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-03-02 00:35:30 +00:00
|
|
|
static const int MAX_CRASH_LOG_COUNT = 16;
|
2019-07-30 05:18:32 +00:00
|
|
|
static const auto STDIN_CAPTURE_RETENTION = 24h;
|
2014-03-02 00:35:30 +00:00
|
|
|
|
2016-01-05 14:18:58 +00:00
|
|
|
struct _lnav_config lnav_config;
|
2019-05-03 20:50:19 +00:00
|
|
|
struct _lnav_config rollback_lnav_config;
|
2016-01-05 14:18:58 +00:00
|
|
|
static struct _lnav_config lnav_default_config;
|
|
|
|
|
2019-05-03 20:50:19 +00:00
|
|
|
std::map<intern_string_t, source_location> lnav_config_locations;
|
|
|
|
|
2016-05-02 03:35:37 +00:00
|
|
|
lnav_config_listener *lnav_config_listener::LISTENER_LIST;
|
|
|
|
|
2021-01-17 06:23:20 +00:00
|
|
|
static auto a = injector::bind<archive_manager::config>::to_instance(+[]() {
|
|
|
|
return &lnav_config.lc_archive_manager;
|
|
|
|
});
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
ghc::filesystem::path dotlnav_path()
|
2013-06-06 14:01:32 +00:00
|
|
|
{
|
2019-07-30 05:18:32 +00:00
|
|
|
auto home_env = getenv("HOME");
|
2019-08-01 14:14:47 +00:00
|
|
|
auto xdg_config_home = getenv("XDG_CONFIG_HOME");
|
2013-06-06 14:01:32 +00:00
|
|
|
|
2019-08-01 14:14:47 +00:00
|
|
|
if (home_env != nullptr) {
|
2020-10-21 05:55:46 +00:00
|
|
|
auto home_path = ghc::filesystem::path(home_env);
|
2013-06-06 14:01:32 +00:00
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (ghc::filesystem::is_directory(home_path)) {
|
2019-08-01 14:14:47 +00:00
|
|
|
auto home_lnav = home_path / ".lnav";
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (ghc::filesystem::is_directory(home_lnav)) {
|
2019-08-01 14:14:47 +00:00
|
|
|
return home_lnav;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xdg_config_home != nullptr) {
|
2020-10-21 05:55:46 +00:00
|
|
|
auto xdg_path = ghc::filesystem::path(xdg_config_home);
|
2019-08-01 14:14:47 +00:00
|
|
|
|
|
|
|
return xdg_path / "lnav";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto home_config = home_path / ".config";
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (ghc::filesystem::is_directory(home_config)) {
|
2019-08-01 14:14:47 +00:00
|
|
|
return home_config / "lnav";
|
|
|
|
}
|
|
|
|
|
|
|
|
return home_lnav;
|
2019-07-30 05:18:32 +00:00
|
|
|
}
|
2013-06-06 14:01:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
return ghc::filesystem::current_path();
|
2013-06-06 14:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool check_experimental(const char *feature_name)
|
|
|
|
{
|
|
|
|
const char *env_value = getenv("LNAV_EXP");
|
|
|
|
|
2021-01-17 06:23:20 +00:00
|
|
|
require(feature_name != nullptr);
|
2013-06-06 14:01:32 +00:00
|
|
|
|
|
|
|
if (env_value && strcasestr(env_value, feature_name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
void ensure_dotlnav()
|
2013-06-06 14:01:32 +00:00
|
|
|
{
|
2019-07-30 05:18:32 +00:00
|
|
|
static const char *subdirs[] = {
|
|
|
|
"",
|
2020-05-07 14:08:59 +00:00
|
|
|
"configs",
|
|
|
|
"configs/default",
|
|
|
|
"configs/installed",
|
2019-07-30 05:18:32 +00:00
|
|
|
"formats",
|
|
|
|
"formats/default",
|
|
|
|
"formats/installed",
|
2020-05-07 14:08:59 +00:00
|
|
|
"staging",
|
2019-07-30 05:18:32 +00:00
|
|
|
"stdin-captures",
|
|
|
|
"crash",
|
|
|
|
};
|
2013-06-06 14:01:32 +00:00
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
auto path = dotlnav_path();
|
2013-07-23 12:55:08 +00:00
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
for (auto sub_path : subdirs) {
|
|
|
|
auto full_path = path / sub_path;
|
2014-11-05 17:01:09 +00:00
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
log_perror(mkdir(full_path.c_str(), 0755));
|
2014-11-05 17:01:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
lnav_log_crash_dir = strdup(path.c_str());
|
2014-03-02 00:35:30 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
static_root_mem<glob_t, globfree> gl;
|
2019-07-30 05:18:32 +00:00
|
|
|
auto crash_glob = path / "crash/*";
|
2014-03-02 00:35:30 +00:00
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (glob(crash_glob.c_str(), GLOB_NOCHECK, nullptr, gl.inout()) == 0) {
|
2014-03-02 00:35:30 +00:00
|
|
|
for (int lpc = 0;
|
2014-03-02 07:40:12 +00:00
|
|
|
lpc < ((int)gl->gl_pathc - MAX_CRASH_LOG_COUNT);
|
2014-03-02 00:35:30 +00:00
|
|
|
lpc++) {
|
2015-09-18 03:55:31 +00:00
|
|
|
log_perror(remove(gl->gl_pathv[lpc]));
|
2014-03-02 00:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 05:18:32 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
static_root_mem<glob_t, globfree> gl;
|
|
|
|
auto cap_glob = path / "stdin-captures/*";
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (glob(cap_glob.c_str(), GLOB_NOCHECK, nullptr, gl.inout()) == 0) {
|
2019-07-30 05:18:32 +00:00
|
|
|
auto old_time = std::chrono::system_clock::now() -
|
|
|
|
STDIN_CAPTURE_RETENTION;
|
|
|
|
|
2020-09-03 05:36:26 +00:00
|
|
|
for (size_t lpc = 0; lpc < gl->gl_pathc; lpc++) {
|
2019-07-30 05:18:32 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(gl->gl_pathv[lpc], &st) == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chrono::system_clock::from_time_t(st.st_mtime) > old_time) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("Removing old stdin capture: %s", gl->gl_pathv[lpc]);
|
|
|
|
log_perror(remove(gl->gl_pathv[lpc]));
|
|
|
|
}
|
|
|
|
}
|
2013-08-29 04:22:04 +00:00
|
|
|
}
|
2013-06-06 14:01:32 +00:00
|
|
|
}
|
2015-08-10 04:03:23 +00:00
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
bool install_from_git(const char *repo)
|
2015-08-10 04:03:23 +00:00
|
|
|
{
|
2021-02-01 23:56:37 +00:00
|
|
|
static const std::regex repo_name_converter("[^\\w]");
|
2015-08-10 04:03:23 +00:00
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
auto formats_path = dotlnav_path() / "formats";
|
|
|
|
auto configs_path = dotlnav_path() / "configs";
|
|
|
|
auto staging_path = dotlnav_path() / "staging";
|
2021-01-29 19:12:02 +00:00
|
|
|
string local_name = std::regex_replace(repo, repo_name_converter, "_");
|
2020-05-07 14:08:59 +00:00
|
|
|
|
|
|
|
auto local_formats_path = formats_path / local_name;
|
|
|
|
auto local_configs_path = configs_path / local_name;
|
|
|
|
auto local_staging_path = staging_path / local_name;
|
|
|
|
|
2015-08-10 04:03:23 +00:00
|
|
|
auto_pid git_cmd(fork());
|
|
|
|
|
|
|
|
if (git_cmd.in_child()) {
|
2020-10-21 05:55:46 +00:00
|
|
|
if (ghc::filesystem::is_directory(local_formats_path)) {
|
2015-08-10 04:03:23 +00:00
|
|
|
printf("Updating format repo: %s\n", repo);
|
2020-10-21 05:55:46 +00:00
|
|
|
log_perror(chdir(local_formats_path.c_str()));
|
2020-05-07 14:08:59 +00:00
|
|
|
execlp("git", "git", "pull", nullptr);
|
|
|
|
}
|
2020-10-21 05:55:46 +00:00
|
|
|
else if (ghc::filesystem::is_directory(local_configs_path)) {
|
2020-05-07 14:08:59 +00:00
|
|
|
printf("Updating config repo: %s\n", repo);
|
2020-10-21 05:55:46 +00:00
|
|
|
log_perror(chdir(local_configs_path.c_str()));
|
2019-07-30 05:18:32 +00:00
|
|
|
execlp("git", "git", "pull", nullptr);
|
2015-08-10 04:03:23 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-10-21 05:55:46 +00:00
|
|
|
execlp("git", "git", "clone", repo, local_staging_path.c_str(), nullptr);
|
2015-08-10 04:03:23 +00:00
|
|
|
}
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
git_cmd.wait_for_child();
|
2020-05-07 14:08:59 +00:00
|
|
|
|
|
|
|
if (!git_cmd.was_normal_exit() || git_cmd.exit_status() != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (ghc::filesystem::is_directory(local_staging_path)) {
|
2020-05-07 14:08:59 +00:00
|
|
|
auto config_path = local_staging_path / "*.json";
|
|
|
|
static_root_mem<glob_t, globfree> gl;
|
|
|
|
bool found_config_file = false, found_format_file = false;
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (glob(config_path.c_str(), 0, nullptr, gl.inout()) == 0) {
|
2020-05-07 14:08:59 +00:00
|
|
|
for (size_t lpc = 0; lpc < gl->gl_pathc; lpc++) {
|
|
|
|
auto json_file_path = gl->gl_pathv[lpc];
|
|
|
|
auto file_type_result = detect_config_file_type(json_file_path);
|
|
|
|
|
|
|
|
if (file_type_result.isErr()) {
|
|
|
|
fprintf(stderr, "error: %s\n",
|
|
|
|
file_type_result.unwrapErr().c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (file_type_result.unwrap() == config_file_type::CONFIG) {
|
|
|
|
found_config_file = true;
|
|
|
|
} else {
|
|
|
|
found_format_file = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_config_file) {
|
2020-10-21 05:55:46 +00:00
|
|
|
rename(local_staging_path.c_str(),
|
|
|
|
local_configs_path.c_str());
|
2020-05-07 14:08:59 +00:00
|
|
|
fprintf(stderr, "info: installed configuration repo -- %s\n",
|
2020-10-21 05:55:46 +00:00
|
|
|
local_configs_path.c_str());
|
2020-05-07 14:08:59 +00:00
|
|
|
} else if (found_format_file) {
|
2020-10-21 05:55:46 +00:00
|
|
|
rename(local_staging_path.c_str(),
|
|
|
|
local_formats_path.c_str());
|
2020-05-07 14:08:59 +00:00
|
|
|
fprintf(stderr, "info: installed format repo -- %s\n",
|
2020-10-21 05:55:46 +00:00
|
|
|
local_formats_path.c_str());
|
2020-05-07 14:08:59 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: cannot find a valid lnav configuration or format file\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-08-10 04:03:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
bool update_installs_from_git()
|
2018-11-09 17:45:19 +00:00
|
|
|
{
|
|
|
|
static_root_mem<glob_t, globfree> gl;
|
2019-07-30 05:18:32 +00:00
|
|
|
auto git_formats = dotlnav_path() / "formats/*/.git";
|
2018-11-09 17:45:19 +00:00
|
|
|
bool found = false, retval = true;
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (glob(git_formats.c_str(), GLOB_NOCHECK, nullptr, gl.inout()) == 0) {
|
2018-11-09 17:45:19 +00:00
|
|
|
for (int lpc = 0; lpc < (int) gl->gl_pathc; lpc++) {
|
|
|
|
char *git_dir = dirname(gl->gl_pathv[lpc]);
|
|
|
|
char pull_cmd[1024];
|
|
|
|
|
|
|
|
printf("Updating formats in %s\n", git_dir);
|
|
|
|
snprintf(pull_cmd, sizeof(pull_cmd),
|
|
|
|
"cd %s && git pull",
|
|
|
|
git_dir);
|
|
|
|
int ret = system(pull_cmd);
|
|
|
|
if (ret == -1) {
|
|
|
|
std::cerr << "Failed to spawn command "
|
|
|
|
<< "\"" << pull_cmd << "\": "
|
|
|
|
<< strerror(errno) << std::endl;
|
|
|
|
retval = false;
|
|
|
|
}
|
|
|
|
else if (ret > 0) {
|
|
|
|
std::cerr << "Command "
|
|
|
|
<< "\"" << pull_cmd << "\" failed: "
|
|
|
|
<< strerror(errno) << std::endl;
|
|
|
|
retval = false;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
printf("No formats from git repositories found, "
|
|
|
|
"use 'lnav -i extra' to install third-party foramts\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-08-10 04:03:23 +00:00
|
|
|
static int read_repo_path(yajlpp_parse_context *ypc, const unsigned char *str, size_t len)
|
|
|
|
{
|
|
|
|
string path = string((const char *)str, len);
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
install_from_git(path.c_str());
|
2015-08-10 04:03:23 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container format_handlers = {
|
|
|
|
json_path_handler("format-repos#", read_repo_path)
|
2015-08-10 04:03:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void install_extra_formats()
|
|
|
|
{
|
2019-07-30 05:18:32 +00:00
|
|
|
auto config_root = dotlnav_path() / "remote-config";
|
2015-09-15 07:24:56 +00:00
|
|
|
auto_fd fd;
|
2015-08-10 04:03:23 +00:00
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (access(config_root.c_str(), R_OK) == 0) {
|
2015-08-10 04:03:23 +00:00
|
|
|
char pull_cmd[1024];
|
|
|
|
|
|
|
|
printf("Updating lnav remote config repo...\n");
|
|
|
|
snprintf(pull_cmd, sizeof(pull_cmd),
|
|
|
|
"cd '%s' && git pull",
|
2020-10-21 05:55:46 +00:00
|
|
|
config_root.c_str());
|
2016-05-04 04:01:39 +00:00
|
|
|
log_perror(system(pull_cmd));
|
2015-08-10 04:03:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
char clone_cmd[1024];
|
|
|
|
|
|
|
|
printf("Cloning lnav remote config repo...\n");
|
|
|
|
snprintf(clone_cmd, sizeof(clone_cmd),
|
|
|
|
"git clone https://github.com/tstack/lnav-config.git %s",
|
2020-10-21 05:55:46 +00:00
|
|
|
config_root.c_str());
|
2016-05-04 04:01:39 +00:00
|
|
|
log_perror(system(clone_cmd));
|
2015-08-10 04:03:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
auto config_json = config_root / "remote-config.json";
|
|
|
|
if ((fd = openp(config_json, O_RDONLY)) == -1) {
|
2015-08-10 04:03:23 +00:00
|
|
|
perror("Unable to open remote-config.json");
|
|
|
|
}
|
|
|
|
else {
|
2020-10-21 05:55:46 +00:00
|
|
|
yajlpp_parse_context ypc_config(config_root.string(), &format_handlers);
|
2015-08-10 04:03:23 +00:00
|
|
|
auto_mem<yajl_handle_t> jhandle(yajl_free);
|
|
|
|
unsigned char buffer[4096];
|
|
|
|
ssize_t rc;
|
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
jhandle = yajl_alloc(&ypc_config.ypc_callbacks, nullptr, &ypc_config);
|
2015-08-10 04:03:23 +00:00
|
|
|
yajl_config(jhandle, yajl_allow_comments, 1);
|
|
|
|
while ((rc = read(fd, buffer, sizeof(buffer))) > 0) {
|
|
|
|
if (yajl_parse(jhandle,
|
|
|
|
buffer,
|
|
|
|
rc) != yajl_status_ok) {
|
2020-12-31 23:24:28 +00:00
|
|
|
auto msg = yajl_get_error(jhandle, 1, buffer, rc);
|
|
|
|
fprintf(stderr,
|
|
|
|
"Unable to parse remote-config.json -- %s",
|
|
|
|
msg);
|
|
|
|
yajl_free_error(jhandle, msg);
|
2015-08-10 04:03:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 14:18:58 +00:00
|
|
|
if (yajl_complete_parse(jhandle) != yajl_status_ok) {
|
2020-12-31 23:24:28 +00:00
|
|
|
auto msg = yajl_get_error(jhandle, 1, buffer, rc);
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
"Unable to parse remote-config.json -- %s",
|
|
|
|
msg);
|
|
|
|
yajl_free_error(jhandle, msg);
|
2016-01-05 14:18:58 +00:00
|
|
|
}
|
2015-08-10 04:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 14:18:58 +00:00
|
|
|
|
|
|
|
struct userdata {
|
|
|
|
userdata(vector<string> &errors) : ud_errors(errors) {};
|
|
|
|
|
|
|
|
vector<string> &ud_errors;
|
|
|
|
};
|
|
|
|
|
2019-05-03 20:50:19 +00:00
|
|
|
static void config_error_reporter(const yajlpp_parse_context &ypc,
|
|
|
|
lnav_log_level_t level,
|
|
|
|
const char *msg)
|
|
|
|
{
|
2019-05-22 05:14:36 +00:00
|
|
|
if (level >= lnav_log_level_t::ERROR) {
|
2019-05-03 20:50:19 +00:00
|
|
|
struct userdata *ud = (userdata *) ypc.ypc_userdata;
|
|
|
|
|
|
|
|
ud->ud_errors.emplace_back(msg);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "warning:%s\n", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container key_command_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("command")
|
2017-01-21 15:41:28 +00:00
|
|
|
.with_synopsis("<command>")
|
2021-02-15 06:44:14 +00:00
|
|
|
.with_description(
|
|
|
|
"The command to execute for the given key sequence. Use a script "
|
|
|
|
"to execute more complicated operations.")
|
2017-01-21 15:41:28 +00:00
|
|
|
.with_pattern("[:|;].*")
|
2021-02-15 06:44:14 +00:00
|
|
|
.with_example(":goto next hour")
|
2019-05-23 13:28:42 +00:00
|
|
|
.FOR_FIELD(key_command, kc_cmd),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("alt-msg")
|
2019-05-23 13:28:42 +00:00
|
|
|
.with_synopsis("<msg>")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("The help message to display after the key is pressed.")
|
|
|
|
.FOR_FIELD(key_command, kc_alt_msg)
|
2019-05-23 13:28:42 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container keymap_def_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::pattern_property_handler("(?<key_seq>(?:x[0-9a-f]{2})+)")
|
2021-02-15 06:44:14 +00:00
|
|
|
.with_synopsis("<utf8-key-code-in-hex>")
|
|
|
|
.with_description(
|
|
|
|
"Map of key codes to commands to execute. The field names are "
|
|
|
|
"the keys to be mapped using as a hexadecimal representation of "
|
|
|
|
"the UTF-8 encoding. Each byte of the UTF-8 should start with "
|
|
|
|
"an 'x' followed by the hexadecimal representation of the byte.")
|
2019-05-23 13:28:42 +00:00
|
|
|
.with_obj_provider<key_command, key_map>([](const yajlpp_provider_context &ypc, key_map *km) {
|
|
|
|
key_command &retval = km->km_seq_to_cmd[ypc.ypc_extractor.get_substr("key_seq")];
|
|
|
|
|
|
|
|
return &retval;
|
|
|
|
})
|
2017-01-21 15:41:28 +00:00
|
|
|
.with_path_provider<key_map>([](key_map *km, vector<string> &paths_out) {
|
|
|
|
for (const auto &iter : km->km_seq_to_cmd) {
|
2019-05-03 20:50:19 +00:00
|
|
|
paths_out.emplace_back(iter.first);
|
2017-01-21 15:41:28 +00:00
|
|
|
}
|
|
|
|
})
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_children(key_command_handlers)
|
2017-01-21 15:41:28 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container keymap_defs_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::pattern_property_handler("(?<keymap_name>[\\w\\-]+)")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("The keymap definitions")
|
2017-01-21 15:41:28 +00:00
|
|
|
.with_obj_provider<key_map, _lnav_config>([](const yajlpp_provider_context &ypc, _lnav_config *root) {
|
2019-05-15 16:13:56 +00:00
|
|
|
key_map &retval = root->lc_ui_keymaps[ypc.ypc_extractor.get_substr("keymap_name")];
|
2017-01-21 15:41:28 +00:00
|
|
|
return &retval;
|
|
|
|
})
|
|
|
|
.with_path_provider<_lnav_config>([](struct _lnav_config *cfg, vector<string> &paths_out) {
|
|
|
|
for (const auto &iter : cfg->lc_ui_keymaps) {
|
2019-05-03 20:50:19 +00:00
|
|
|
paths_out.emplace_back(iter.first);
|
2017-01-21 15:41:28 +00:00
|
|
|
}
|
|
|
|
})
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_children(keymap_def_handlers)
|
2017-01-21 15:41:28 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container global_var_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::pattern_property_handler("(?<var_name>\\w+)")
|
2017-03-01 16:44:16 +00:00
|
|
|
.with_synopsis("<name>")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description(
|
|
|
|
"A global variable definition. Global variables can be referenced "
|
|
|
|
"in scripts, SQL statements, or commands.")
|
|
|
|
.with_path_provider<_lnav_config>(
|
|
|
|
[](struct _lnav_config *cfg, vector<string> &paths_out) {
|
|
|
|
for (const auto &iter : cfg->lc_global_vars) {
|
2019-05-03 20:50:19 +00:00
|
|
|
paths_out.emplace_back(iter.first);
|
2020-05-07 14:08:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.FOR_FIELD(_lnav_config, lc_global_vars)};
|
|
|
|
|
|
|
|
static struct json_path_container style_config_handlers =
|
|
|
|
json_path_container{
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("color")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_synopsis("#hex|color_name")
|
|
|
|
.with_description(
|
|
|
|
"The foreground color value for this style. The value can be "
|
|
|
|
"the name of an xterm color, the hexadecimal value, or a theme "
|
|
|
|
"variable reference.")
|
|
|
|
.with_example("#fff")
|
|
|
|
.with_example("Green")
|
|
|
|
.with_example("$black")
|
|
|
|
.FOR_FIELD(style_config, sc_color),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("background-color")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_synopsis("#hex|color_name")
|
|
|
|
.with_description(
|
2021-02-06 00:04:34 +00:00
|
|
|
"The background color value for this style. The value can be "
|
2020-05-07 14:08:59 +00:00
|
|
|
"the name of an xterm color, the hexadecimal value, or a theme "
|
|
|
|
"variable reference.")
|
|
|
|
.with_example("#2d2a2e")
|
|
|
|
.with_example("Green")
|
|
|
|
.FOR_FIELD(style_config, sc_background_color),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("underline")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Indicates that the text should be underlined.")
|
|
|
|
.FOR_FIELD(style_config, sc_underline),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("bold")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Indicates that the text should be bolded.")
|
|
|
|
.FOR_FIELD(style_config, sc_bold),
|
|
|
|
}
|
|
|
|
.with_definition_id("style");
|
2019-05-03 20:50:19 +00:00
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container theme_styles_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("identifier")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for identifiers in logs")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_identifier;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("text")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for plain text")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_text;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("alt-text")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for plain text when alternating")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_alt_text;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("error")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for error messages")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_error;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("ok")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for success messages")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_ok;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("warning")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for warning messages")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_warning;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("hidden")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for hidden fields in logs")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_hidden;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("adjusted-time")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for timestamps that have been adjusted")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_adjusted_time;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("skewed-time")
|
2020-12-06 05:51:46 +00:00
|
|
|
.with_description("Styling for timestamps that are different from the received time")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_skewed_time;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("offset-time")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for hidden fields")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_offset_time;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-12-06 05:51:46 +00:00
|
|
|
yajlpp::property_handler("invalid-msg")
|
|
|
|
.with_description("Styling for invalid log messages")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_invalid_msg;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("popup")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for popup windows")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_popup;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2021-01-23 21:00:51 +00:00
|
|
|
yajlpp::property_handler("focused")
|
|
|
|
.with_description("Styling for a focused row in a list view")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_focused;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
|
|
|
yajlpp::property_handler("disabled-focused")
|
|
|
|
.with_description("Styling for a disabled focused row in a list view")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_disabled_focused;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("scrollbar")
|
2019-05-12 13:53:40 +00:00
|
|
|
.with_description("Styling for scrollbars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_scrollbar;
|
|
|
|
})
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_children(style_config_handlers)
|
2019-05-03 20:50:19 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container theme_syntax_styles_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("keyword")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for keywords in source files")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_keyword;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("string")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for single/double-quoted strings in text")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_string;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("comment")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for comments in source files")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_comment;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("doc-directive")
|
2020-10-29 04:19:57 +00:00
|
|
|
.with_description("Styling for documentation directives in source files")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_doc_directive;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("variable")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for variables in text")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_variable;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("symbol")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for symbols in source files")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_symbol;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("number")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for numbers in source files")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_number;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("re-special")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for special characters in regular expressions")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_re_special;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("re-repeat")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for repeats in regular expressions")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_re_repeat;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("diff-delete")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for deleted lines in diffs")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_diff_delete;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("diff-add")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for added lines in diffs")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_diff_add;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("diff-section")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for diffs")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_diff_section;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("file")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for file names in source files")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_file;
|
|
|
|
})
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_children(style_config_handlers)
|
2019-05-03 20:50:19 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container theme_status_styles_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("text")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_status;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("warn")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for warnings in status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_warn_status;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("alert")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for alerts in status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_alert_status;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("active")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for activity in status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_active_status;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("inactive")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for inactive status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_inactive_status;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:24:23 +00:00
|
|
|
yajlpp::property_handler("title-hotkey")
|
|
|
|
.with_description("Styling for hotkey highlights in titles")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_status_title_hotkey;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("title")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for title sections of status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_status_title;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:24:23 +00:00
|
|
|
yajlpp::property_handler("disabled-title")
|
|
|
|
.with_description("Styling for title sections of status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_status_disabled_title;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("subtitle")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_description("Styling for subtitle sections of status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_status_subtitle;
|
|
|
|
})
|
2020-10-29 04:24:23 +00:00
|
|
|
.with_children(style_config_handlers),
|
|
|
|
yajlpp::property_handler("hotkey")
|
|
|
|
.with_description("Styling for hotkey highlights of status bars")
|
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
return &root->lt_style_status_hotkey;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
2019-05-03 20:50:19 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container theme_log_level_styles_handlers = {
|
2020-12-06 05:51:46 +00:00
|
|
|
yajlpp::pattern_property_handler("(?<level>trace|debug5|debug4|debug3|debug2|debug|info|stats|notice|warning|error|critical|fatal|invalid)")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_obj_provider<style_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
style_config &sc = root->lt_level_styles[
|
|
|
|
string2level(ypc.ypc_extractor.get_substr_i("level").get())];
|
|
|
|
|
|
|
|
return ≻
|
|
|
|
})
|
|
|
|
.with_path_provider<lnav_theme>([](struct lnav_theme *cfg, vector<string> &paths_out) {
|
|
|
|
for (int lpc = LEVEL_TRACE; lpc < LEVEL__MAX; lpc++) {
|
|
|
|
paths_out.emplace_back(level_names[lpc]);
|
|
|
|
}
|
|
|
|
})
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_children(style_config_handlers)
|
2019-05-03 20:50:19 +00:00
|
|
|
};
|
|
|
|
|
2020-10-29 04:19:57 +00:00
|
|
|
static struct json_path_container highlighter_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("pattern")
|
2020-10-29 04:19:57 +00:00
|
|
|
.with_synopsis("regular expression")
|
|
|
|
.with_description("The regular expression to highlight")
|
|
|
|
.FOR_FIELD(highlighter_config, hc_regex),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("style")
|
2020-10-29 04:19:57 +00:00
|
|
|
.with_description("The styling for the text that matches the associated pattern")
|
|
|
|
.with_obj_provider<style_config, highlighter_config>([](const yajlpp_provider_context &ypc, highlighter_config *root) {
|
|
|
|
return &root->hc_style;
|
|
|
|
})
|
|
|
|
.with_children(style_config_handlers),
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct json_path_container theme_highlights_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::pattern_property_handler("(?<highlight_name>\\w+)")
|
2020-10-29 04:19:57 +00:00
|
|
|
.with_obj_provider<highlighter_config, lnav_theme>([](const yajlpp_provider_context &ypc, lnav_theme *root) {
|
|
|
|
highlighter_config &hc = root->lt_highlights[
|
|
|
|
ypc.ypc_extractor.get_substr_i("highlight_name").get()];
|
|
|
|
|
|
|
|
return &hc;
|
|
|
|
})
|
|
|
|
.with_path_provider<lnav_theme>([](struct lnav_theme *cfg, vector<string> &paths_out) {
|
|
|
|
for (const auto& pair : cfg->lt_highlights) {
|
|
|
|
paths_out.emplace_back(pair.first);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.with_children(highlighter_handlers)
|
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container theme_vars_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::pattern_property_handler("(?<var_name>\\w+)")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_synopsis("name")
|
|
|
|
.with_description("A theme variable definition")
|
|
|
|
.with_path_provider<lnav_theme>([](struct lnav_theme *lt, vector<string> &paths_out) {
|
|
|
|
for (const auto &iter : lt->lt_vars) {
|
|
|
|
paths_out.emplace_back(iter.first);
|
|
|
|
}
|
|
|
|
})
|
2020-05-07 14:08:59 +00:00
|
|
|
.FOR_FIELD(lnav_theme, lt_vars)
|
2019-05-03 20:50:19 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container theme_def_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("vars")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Variables definitions that are used in this theme.")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_children(theme_vars_handlers),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("styles")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Styles for log messages.")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_children(theme_styles_handlers),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("syntax-styles")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Styles for syntax highlighting in text files.")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_children(theme_syntax_styles_handlers),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("status-styles")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Styles for the user-interface components.")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_children(theme_status_styles_handlers),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("log-level-styles")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Styles for each log message level.")
|
2020-10-29 04:19:57 +00:00
|
|
|
.with_children(theme_log_level_styles_handlers),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("highlights")
|
2020-10-29 04:19:57 +00:00
|
|
|
.with_description("Styles for text highlights.")
|
|
|
|
.with_children(theme_highlights_handlers),
|
2019-05-03 20:50:19 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container theme_defs_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::pattern_property_handler("(?<theme_name>[\\w\\-]+)")
|
2019-05-03 20:50:19 +00:00
|
|
|
.with_obj_provider<lnav_theme, _lnav_config>([](const yajlpp_provider_context &ypc, _lnav_config *root) {
|
|
|
|
lnav_theme < = root->lc_ui_theme_defs[ypc.ypc_extractor.get_substr("theme_name")];
|
|
|
|
|
|
|
|
return <
|
|
|
|
})
|
|
|
|
.with_path_provider<_lnav_config>([](struct _lnav_config *cfg, vector<string> &paths_out) {
|
|
|
|
for (const auto &iter : cfg->lc_ui_theme_defs) {
|
|
|
|
paths_out.emplace_back(iter.first);
|
|
|
|
}
|
|
|
|
})
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_children(theme_def_handlers)
|
2019-05-03 20:50:19 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static struct json_path_container ui_handlers = {
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("clock-format")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_synopsis("format")
|
|
|
|
.with_description("The format for the clock displayed in "
|
|
|
|
"the top-left corner using strftime(3) conversions")
|
|
|
|
.with_example("%a %b %d %H:%M:%S %Z")
|
|
|
|
.FOR_FIELD(_lnav_config, lc_ui_clock_format),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("dim-text")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_synopsis("bool")
|
|
|
|
.with_description("Reduce the brightness of text (useful for xterms). "
|
|
|
|
"This setting can be useful when running in an xterm "
|
|
|
|
"where the white color is very bright.")
|
|
|
|
.FOR_FIELD(_lnav_config, lc_ui_dim_text),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("default-colors")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_synopsis("bool")
|
|
|
|
.with_description(
|
|
|
|
"Use default terminal background and foreground colors "
|
|
|
|
"instead of black and white for all text coloring. This setting "
|
|
|
|
"can be useful when transparent background or alternate color "
|
|
|
|
"theme terminal is used.")
|
|
|
|
.FOR_FIELD(_lnav_config, lc_ui_default_colors),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("keymap")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_synopsis("keymap_name")
|
|
|
|
.with_description("The name of the keymap to use.")
|
|
|
|
.FOR_FIELD(_lnav_config, lc_ui_keymap),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("theme")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_synopsis("theme_name")
|
|
|
|
.with_description("The name of the theme to use.")
|
|
|
|
.FOR_FIELD(_lnav_config, lc_ui_theme),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("theme-defs")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Theme definitions.")
|
|
|
|
.with_children(theme_defs_handlers),
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("keymap-defs")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Keymap definitions.")
|
2021-01-17 06:23:20 +00:00
|
|
|
.with_children(keymap_defs_handlers),
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct json_path_container archive_handlers = {
|
2021-01-23 21:00:51 +00:00
|
|
|
yajlpp::property_handler("min-free-space")
|
2021-02-15 06:44:14 +00:00
|
|
|
.with_synopsis("<bytes>")
|
2021-02-08 05:30:02 +00:00
|
|
|
.with_description(
|
|
|
|
"The minimum free space, in bytes, to maintain when unpacking "
|
|
|
|
"archives")
|
2021-01-23 21:00:51 +00:00
|
|
|
.with_min_value(0)
|
|
|
|
.for_field(&_lnav_config::lc_archive_manager,
|
|
|
|
&archive_manager::config::amc_min_free_space),
|
2021-01-17 06:23:20 +00:00
|
|
|
yajlpp::property_handler("cache-ttl")
|
2021-02-15 06:44:14 +00:00
|
|
|
.with_synopsis("<duration>")
|
2021-02-08 05:30:02 +00:00
|
|
|
.with_description(
|
|
|
|
"The time-to-live for unpacked archives, expressed as a duration "
|
|
|
|
"(e.g. '3d' for three days)")
|
2021-02-15 06:44:14 +00:00
|
|
|
.with_example("3d")
|
|
|
|
.with_example("12h")
|
2021-01-17 06:23:20 +00:00
|
|
|
.for_field(&_lnav_config::lc_archive_manager,
|
|
|
|
&archive_manager::config::amc_cache_ttl),
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct json_path_container tuning_handlers = {
|
|
|
|
yajlpp::property_handler("archive-manager")
|
|
|
|
.with_description("Settings related to opening archive files")
|
|
|
|
.with_children(archive_handlers),
|
|
|
|
};
|
2020-05-07 14:08:59 +00:00
|
|
|
|
2020-12-11 05:18:56 +00:00
|
|
|
static set<string> SUPPORTED_CONFIG_SCHEMAS = {
|
2020-05-07 14:08:59 +00:00
|
|
|
"https://lnav.org/schemas/config-v1.schema.json",
|
2016-01-05 14:18:58 +00:00
|
|
|
};
|
|
|
|
|
2020-12-11 06:32:01 +00:00
|
|
|
const char *DEFAULT_FORMAT_SCHEMA =
|
|
|
|
"https://lnav.org/schemas/format-v1.schema.json";
|
|
|
|
|
2020-12-11 05:18:56 +00:00
|
|
|
set<string> SUPPORTED_FORMAT_SCHEMAS = {
|
2020-12-11 06:32:01 +00:00
|
|
|
DEFAULT_FORMAT_SCHEMA,
|
2020-12-11 05:18:56 +00:00
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
static int read_id(yajlpp_parse_context *ypc, const unsigned char *str, size_t len)
|
|
|
|
{
|
|
|
|
auto file_id = string((const char *) str, len);
|
|
|
|
|
2020-12-11 05:18:56 +00:00
|
|
|
if (SUPPORTED_CONFIG_SCHEMAS.count(file_id) == 0) {
|
|
|
|
ypc->report_error(
|
|
|
|
lnav_log_level_t::ERROR,
|
|
|
|
"%s:%d: error: unsupported configuration $schema -- %s\n",
|
|
|
|
ypc->ypc_source.c_str(),
|
|
|
|
ypc->get_line_number(),
|
|
|
|
file_id.c_str());
|
2020-05-07 14:08:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct json_path_container lnav_config_handlers = json_path_container {
|
|
|
|
json_path_handler("$schema", read_id)
|
|
|
|
.with_synopsis("The URI of the schema for this file")
|
|
|
|
.with_description("Specifies the type of this file"),
|
2016-01-05 14:18:58 +00:00
|
|
|
|
2021-01-17 06:23:20 +00:00
|
|
|
yajlpp::property_handler("tuning")
|
|
|
|
.with_description("Internal settings")
|
|
|
|
.with_children(tuning_handlers),
|
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("ui")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("User-interface settings")
|
|
|
|
.with_children(ui_handlers),
|
2019-05-15 16:13:56 +00:00
|
|
|
|
2020-10-29 04:23:25 +00:00
|
|
|
yajlpp::property_handler("global")
|
2020-05-07 14:08:59 +00:00
|
|
|
.with_description("Global variable definitions")
|
|
|
|
.with_children(global_var_handlers)
|
|
|
|
}
|
2020-12-11 05:18:56 +00:00
|
|
|
.with_schema_id(*SUPPORTED_CONFIG_SCHEMAS.cbegin());
|
2020-05-07 14:08:59 +00:00
|
|
|
|
2020-09-04 21:39:04 +00:00
|
|
|
class active_key_map_listener : public lnav_config_listener {
|
|
|
|
public:
|
|
|
|
void reload_config(error_reporter &reporter) override
|
|
|
|
{
|
|
|
|
lnav_config.lc_active_keymap = lnav_config.lc_ui_keymaps["default"];
|
|
|
|
for (const auto &pair :
|
|
|
|
lnav_config.lc_ui_keymaps[lnav_config.lc_ui_keymap].km_seq_to_cmd) {
|
|
|
|
lnav_config.lc_active_keymap.km_seq_to_cmd[pair.first] = pair.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static active_key_map_listener KEYMAP_LISTENER;
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
Result<config_file_type, std::string>
|
2020-10-21 05:55:46 +00:00
|
|
|
detect_config_file_type(const ghc::filesystem::path &path)
|
2020-05-07 14:08:59 +00:00
|
|
|
{
|
|
|
|
static const char *id_path[] = {"$schema", nullptr};
|
|
|
|
string content;
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (!read_file(path.string(), content)) {
|
|
|
|
return Err(fmt::format("unable to open file: {}", path.string()));
|
2020-05-07 14:08:59 +00:00
|
|
|
}
|
|
|
|
if (startswith(content, "#")) {
|
|
|
|
content.insert(0, "//");
|
|
|
|
}
|
|
|
|
|
|
|
|
char error_buffer[1024];
|
|
|
|
auto content_tree = unique_ptr<yajl_val_s, decltype(&yajl_tree_free)>(
|
|
|
|
yajl_tree_parse(content.c_str(), error_buffer, sizeof(error_buffer)),
|
|
|
|
yajl_tree_free);
|
|
|
|
if (content_tree == nullptr) {
|
|
|
|
return Err(fmt::format("unable to parse file: {} -- {}",
|
2020-10-21 05:55:46 +00:00
|
|
|
path.string(), error_buffer));
|
2020-05-07 14:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto id_val = yajl_tree_get(content_tree.get(), id_path, yajl_t_string);
|
|
|
|
if (id_val != nullptr) {
|
2020-12-11 05:18:56 +00:00
|
|
|
if (SUPPORTED_CONFIG_SCHEMAS.count(id_val->u.string)) {
|
2020-05-07 14:08:59 +00:00
|
|
|
return Ok(config_file_type::CONFIG);
|
2020-12-11 05:18:56 +00:00
|
|
|
} else if (SUPPORTED_FORMAT_SCHEMAS.count(id_val->u.string)) {
|
|
|
|
return Ok(config_file_type::FORMAT);
|
2020-05-07 14:08:59 +00:00
|
|
|
} else {
|
|
|
|
return Err(fmt::format("unsupported configuration version in file: {} -- {}",
|
2020-10-21 05:55:46 +00:00
|
|
|
path.string(), id_val->u.string));
|
2020-05-07 14:08:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Ok(config_file_type::FORMAT);
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 14:18:58 +00:00
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
static void load_config_from(const ghc::filesystem::path &path, vector<string> &errors)
|
2016-01-05 14:18:58 +00:00
|
|
|
{
|
2020-10-21 05:55:46 +00:00
|
|
|
yajlpp_parse_context ypc(path.string(), &lnav_config_handlers);
|
2016-01-05 14:18:58 +00:00
|
|
|
struct userdata ud(errors);
|
|
|
|
auto_fd fd;
|
|
|
|
|
2019-05-03 20:50:19 +00:00
|
|
|
ypc.ypc_locations = &lnav_config_locations;
|
2016-01-05 14:18:58 +00:00
|
|
|
ypc.with_obj(lnav_config);
|
|
|
|
ypc.ypc_userdata = &ud;
|
2019-05-04 14:07:39 +00:00
|
|
|
ypc.with_error_reporter(config_error_reporter);
|
2019-07-30 05:18:32 +00:00
|
|
|
if ((fd = openp(path, O_RDONLY)) == -1) {
|
2016-01-05 14:18:58 +00:00
|
|
|
if (errno != ENOENT) {
|
|
|
|
char errmsg[1024];
|
|
|
|
|
|
|
|
snprintf(errmsg, sizeof(errmsg),
|
|
|
|
"error: unable to open format file -- %s",
|
2020-10-21 05:55:46 +00:00
|
|
|
path.c_str());
|
2018-05-25 13:32:01 +00:00
|
|
|
errors.emplace_back(errmsg);
|
2016-01-05 14:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto_mem<yajl_handle_t> handle(yajl_free);
|
|
|
|
char buffer[2048];
|
|
|
|
off_t offset = 0;
|
|
|
|
ssize_t rc = -1;
|
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
handle = yajl_alloc(&ypc.ypc_callbacks, nullptr, &ypc);
|
2016-01-05 14:18:58 +00:00
|
|
|
yajl_config(handle, yajl_allow_comments, 1);
|
2019-05-03 20:50:19 +00:00
|
|
|
yajl_config(handle, yajl_allow_multiple_values, 1);
|
|
|
|
ypc.ypc_handle = handle;
|
2016-01-05 14:18:58 +00:00
|
|
|
while (true) {
|
|
|
|
rc = read(fd, buffer, sizeof(buffer));
|
|
|
|
if (rc == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (rc == -1) {
|
2020-10-21 05:55:46 +00:00
|
|
|
errors.push_back(path.string() +
|
2016-01-05 14:18:58 +00:00
|
|
|
":unable to read file -- " +
|
|
|
|
string(strerror(errno)));
|
|
|
|
break;
|
|
|
|
}
|
2019-05-03 20:50:19 +00:00
|
|
|
if (ypc.parse((const unsigned char *)buffer, rc) != yajl_status_ok) {
|
2016-01-05 14:18:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += rc;
|
|
|
|
}
|
|
|
|
if (rc == 0) {
|
2019-05-04 14:07:39 +00:00
|
|
|
ypc.complete_parse();
|
2016-01-05 14:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 16:13:56 +00:00
|
|
|
static void load_default_config(struct _lnav_config &config_obj,
|
|
|
|
const std::string &path,
|
2020-12-30 21:49:29 +00:00
|
|
|
const bin_src_file &bsf,
|
2016-01-05 14:18:58 +00:00
|
|
|
vector<string> &errors)
|
|
|
|
{
|
2020-12-30 21:49:29 +00:00
|
|
|
yajlpp_parse_context ypc_builtin(bsf.get_name(), &lnav_config_handlers);
|
2016-01-05 14:18:58 +00:00
|
|
|
auto_mem<yajl_handle_t> handle(yajl_free);
|
|
|
|
struct userdata ud(errors);
|
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
handle = yajl_alloc(&ypc_builtin.ypc_callbacks, nullptr, &ypc_builtin);
|
2019-05-15 16:13:56 +00:00
|
|
|
ypc_builtin.ypc_locations = &lnav_config_locations;
|
2019-05-03 20:50:19 +00:00
|
|
|
ypc_builtin.with_handle(handle);
|
2016-01-05 14:18:58 +00:00
|
|
|
ypc_builtin.with_obj(config_obj);
|
2019-05-03 20:50:19 +00:00
|
|
|
ypc_builtin.with_error_reporter(config_error_reporter);
|
2016-01-05 14:18:58 +00:00
|
|
|
ypc_builtin.ypc_userdata = &ud;
|
2019-05-15 16:13:56 +00:00
|
|
|
|
|
|
|
if (path != "*") {
|
|
|
|
ypc_builtin.ypc_ignore_unused = true;
|
|
|
|
ypc_builtin.ypc_active_paths.insert(path);
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:18:58 +00:00
|
|
|
yajl_config(handle, yajl_allow_comments, 1);
|
2019-05-03 20:50:19 +00:00
|
|
|
yajl_config(handle, yajl_allow_multiple_values, 1);
|
2020-12-30 21:49:29 +00:00
|
|
|
if (ypc_builtin.parse(bsf.to_string_fragment()) == yajl_status_ok) {
|
2019-05-04 14:07:39 +00:00
|
|
|
ypc_builtin.complete_parse();
|
2016-01-05 14:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 16:13:56 +00:00
|
|
|
static void load_default_configs(struct _lnav_config &config_obj,
|
|
|
|
const std::string &path,
|
|
|
|
vector<string> &errors)
|
2016-01-05 14:18:58 +00:00
|
|
|
{
|
2020-12-30 21:49:29 +00:00
|
|
|
for (auto& bsf : lnav_config_json) {
|
|
|
|
load_default_config(config_obj, path, bsf, errors);
|
2017-01-21 15:41:28 +00:00
|
|
|
}
|
2019-05-15 16:13:56 +00:00
|
|
|
}
|
2017-01-21 15:41:28 +00:00
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
void load_config(const vector<ghc::filesystem::path> &extra_paths, vector<string> &errors)
|
2019-05-15 16:13:56 +00:00
|
|
|
{
|
2019-07-30 05:18:32 +00:00
|
|
|
auto user_config = dotlnav_path() / "config.json";
|
2018-05-25 13:32:01 +00:00
|
|
|
|
2020-12-30 21:49:29 +00:00
|
|
|
for (auto& bsf : lnav_config_json) {
|
2020-05-07 14:08:59 +00:00
|
|
|
auto sample_path = dotlnav_path() /
|
|
|
|
"configs" /
|
|
|
|
"default" /
|
2020-12-30 21:49:29 +00:00
|
|
|
fmt::format("{}.sample", bsf.get_name());
|
2020-05-07 14:08:59 +00:00
|
|
|
|
|
|
|
auto fd = auto_fd(openp(sample_path, O_WRONLY|O_TRUNC|O_CREAT, 0644));
|
2020-12-30 21:49:29 +00:00
|
|
|
auto sf = bsf.to_string_fragment();
|
|
|
|
if (fd == -1 || write(fd.get(), sf.data(), sf.length()) == -1) {
|
2020-05-07 14:08:59 +00:00
|
|
|
perror("error: unable to write default config file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 15:41:28 +00:00
|
|
|
{
|
2020-12-30 21:49:29 +00:00
|
|
|
load_default_configs(lnav_default_config, "*", errors);
|
|
|
|
load_default_configs(lnav_config, "*", errors);
|
2019-05-03 20:50:19 +00:00
|
|
|
|
|
|
|
for (const auto &extra_path : extra_paths) {
|
2020-05-07 14:08:59 +00:00
|
|
|
auto config_path = extra_path / "configs/*/*.json";
|
2019-05-03 20:50:19 +00:00
|
|
|
static_root_mem<glob_t, globfree> gl;
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (glob(config_path.c_str(), 0, nullptr, gl.inout()) == 0) {
|
2020-05-07 14:08:59 +00:00
|
|
|
for (size_t lpc = 0; lpc < gl->gl_pathc; lpc++) {
|
|
|
|
load_config_from(gl->gl_pathv[lpc], errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &extra_path : extra_paths) {
|
|
|
|
auto config_path = extra_path / "formats/*/config.*.json";
|
|
|
|
static_root_mem<glob_t, globfree> gl;
|
2019-05-03 20:50:19 +00:00
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
if (glob(config_path.c_str(), 0, nullptr, gl.inout()) == 0) {
|
2020-05-07 14:08:59 +00:00
|
|
|
for (size_t lpc = 0; lpc < gl->gl_pathc; lpc++) {
|
|
|
|
load_config_from(gl->gl_pathv[lpc], errors);
|
2019-05-03 20:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 15:41:28 +00:00
|
|
|
load_config_from(user_config, errors);
|
|
|
|
}
|
2016-05-02 03:35:37 +00:00
|
|
|
|
2019-05-03 20:50:19 +00:00
|
|
|
reload_config(errors);
|
|
|
|
|
|
|
|
rollback_lnav_config = lnav_config;
|
2016-01-05 14:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_config(const std::string &path)
|
|
|
|
{
|
|
|
|
vector<string> errors;
|
|
|
|
|
2020-12-30 21:49:29 +00:00
|
|
|
load_default_configs(lnav_config, path, errors);
|
2019-05-03 20:50:19 +00:00
|
|
|
|
|
|
|
reload_config(errors);
|
2020-05-07 14:08:59 +00:00
|
|
|
|
|
|
|
for (auto &err: errors) {
|
|
|
|
log_debug("reset %s", err.c_str());
|
|
|
|
}
|
2016-01-05 14:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string save_config()
|
|
|
|
{
|
2019-05-08 12:30:59 +00:00
|
|
|
yajlpp_gen gen;
|
2019-07-30 05:18:32 +00:00
|
|
|
auto filename = fmt::format("config.json.{}.tmp", getpid());
|
|
|
|
auto user_config_tmp = dotlnav_path() / filename;
|
|
|
|
auto user_config = dotlnav_path() / "config.json";
|
2016-01-05 14:18:58 +00:00
|
|
|
|
2019-05-08 12:30:59 +00:00
|
|
|
yajl_gen_config(gen, yajl_gen_beautify, true);
|
|
|
|
yajlpp_gen_context ygc(gen, lnav_config_handlers);
|
|
|
|
vector<string> errors;
|
2016-01-05 14:18:58 +00:00
|
|
|
|
2019-05-08 12:30:59 +00:00
|
|
|
ygc.with_default_obj(lnav_default_config)
|
|
|
|
.with_obj(lnav_config);
|
|
|
|
ygc.gen();
|
2016-01-05 14:18:58 +00:00
|
|
|
|
2019-05-08 12:30:59 +00:00
|
|
|
{
|
|
|
|
auto_fd fd;
|
2016-01-05 14:18:58 +00:00
|
|
|
|
2019-07-30 05:18:32 +00:00
|
|
|
if ((fd = openp(user_config_tmp,
|
|
|
|
O_WRONLY | O_CREAT | O_TRUNC, 0600)) == -1) {
|
2019-05-08 12:30:59 +00:00
|
|
|
return "error: unable to save configuration -- " +
|
|
|
|
string(strerror(errno));
|
|
|
|
} else {
|
|
|
|
string_fragment bits = gen.to_string_fragment();
|
2016-01-05 14:18:58 +00:00
|
|
|
|
2019-05-08 12:30:59 +00:00
|
|
|
log_perror(write(fd, bits.data(), bits.length()));
|
2016-01-05 14:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
rename(user_config_tmp.c_str(), user_config.c_str());
|
2019-05-08 12:30:59 +00:00
|
|
|
|
2016-01-05 14:18:58 +00:00
|
|
|
return "info: configuration saved";
|
|
|
|
}
|
2016-05-02 03:35:37 +00:00
|
|
|
|
2019-05-03 20:50:19 +00:00
|
|
|
void reload_config(vector<string> &errors)
|
2016-05-02 03:35:37 +00:00
|
|
|
{
|
|
|
|
lnav_config_listener *curr = lnav_config_listener::LISTENER_LIST;
|
|
|
|
|
2020-10-21 05:55:46 +00:00
|
|
|
while (curr != nullptr) {
|
2019-05-03 20:50:19 +00:00
|
|
|
auto reporter = [&errors](const void *cfg_value, const std::string &errmsg) {
|
|
|
|
auto cb = [&cfg_value, &errors, &errmsg](
|
|
|
|
const json_path_handler_base &jph,
|
|
|
|
const string &path,
|
|
|
|
void *mem) {
|
|
|
|
if (mem != cfg_value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto loc_iter = lnav_config_locations.find(intern_string::lookup(path));
|
|
|
|
if (loc_iter == lnav_config_locations.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char msg[1024];
|
|
|
|
|
|
|
|
snprintf(msg, sizeof(msg),
|
|
|
|
"%s:%d:%s",
|
|
|
|
loc_iter->second.sl_source.get(),
|
|
|
|
loc_iter->second.sl_line_number,
|
|
|
|
errmsg.c_str());
|
|
|
|
|
|
|
|
errors.emplace_back(msg);
|
|
|
|
};
|
|
|
|
|
2020-05-07 14:08:59 +00:00
|
|
|
for (auto &jph : lnav_config_handlers.jpc_children) {
|
|
|
|
jph.walk(cb, &lnav_config);
|
2019-05-03 20:50:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
curr->reload_config(reporter);
|
2016-05-02 03:35:37 +00:00
|
|
|
curr = curr->lcl_next;
|
|
|
|
}
|
2018-05-10 13:44:03 +00:00
|
|
|
}
|