1 /**
2  * Copyright (c) 2013, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * @file lnav_config.hh
30  */
31 
32 #ifndef lnav_config_hh
33 #define lnav_config_hh
34 
35 #include <map>
36 #include <set>
37 #include <string>
38 #include <vector>
39 #include <functional>
40 #include <unordered_map>
41 
42 #include "base/result.h"
43 #include "base/file_range.hh"
44 #include "log_level.hh"
45 #include "styling.hh"
46 #include "ghc/filesystem.hpp"
47 
48 #include "lnav_config_fwd.hh"
49 #include "archive_manager.cfg.hh"
50 #include "file_vtab.cfg.hh"
51 #include "logfile.cfg.hh"
52 #include "tailer/tailer.looper.cfg.hh"
53 #include "sysclip.cfg.hh"
54 
55 /**
56  * Check if an experimental feature should be enabled by
57  * examining the LNAV_EXP environment variable.
58  *
59  * @param feature_name The feature name to check for in
60  *   the LNAV_EXP environment variable.
61  *
62  * @return True if the feature was mentioned in the env
63  *   var and should be enabled.
64  */
65 bool check_experimental(const char *feature_name);
66 
67 /**
68  * Ensure that the '.lnav' directory exists.
69  */
70 void ensure_dotlnav();
71 
72 bool install_from_git(const char *repo);
73 bool update_installs_from_git();
74 
75 void install_extra_formats();
76 
77 struct key_command {
78     std::string kc_cmd;
79     std::string kc_alt_msg;
80 };
81 
82 struct key_map {
83     std::map<std::string, key_command> km_seq_to_cmd;
84 };
85 
86 struct _lnav_config {
87     std::string lc_ui_clock_format;
88     bool lc_ui_dim_text;
89     bool lc_ui_default_colors{true};
90     std::string lc_ui_keymap;
91     std::string lc_ui_theme;
92     std::unordered_map<std::string, key_map> lc_ui_keymaps;
93     std::map<std::string, std::string> lc_ui_key_overrides;
94     std::map<std::string, std::string> lc_global_vars;
95     std::map<std::string, lnav_theme> lc_ui_theme_defs;
96 
97     key_map lc_active_keymap;
98 
99     archive_manager::config lc_archive_manager;
100     file_vtab::config lc_file_vtab;
101     lnav::logfile::config lc_logfile;
102     tailer::config lc_tailer;
103     sysclip::config lc_sysclip;
104 };
105 
106 extern struct _lnav_config lnav_config;
107 extern struct _lnav_config rollback_lnav_config;
108 extern std::map<intern_string_t, source_location> lnav_config_locations;
109 
110 extern struct json_path_container lnav_config_handlers;
111 
112 enum class config_file_type {
113     FORMAT,
114     CONFIG,
115 };
116 
117 Result<config_file_type, std::string> detect_config_file_type(const ghc::filesystem::path &path);
118 
119 void load_config(const std::vector<ghc::filesystem::path> &extra_paths,
120                  std::vector<std::string> &errors);
121 
122 void reset_config(const std::string &path);
123 
124 void reload_config(std::vector<std::string> &errors);
125 
126 std::string save_config();
127 
128 extern const char *DEFAULT_FORMAT_SCHEMA;
129 extern std::set<std::string> SUPPORTED_FORMAT_SCHEMAS;
130 
131 #endif
132