Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
Jan Kundrát | a274044 | 2018-03-22 16:56:43 +0100 | [diff] [blame] | 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 4 | * |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 6 | * |
| 7 | */ |
Václav Kubernát | fd26116 | 2020-11-12 02:36:29 +0100 | [diff] [blame] | 8 | #include <atomic> |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 9 | #include <boost/algorithm/string.hpp> |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 10 | #include <docopt.h> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 11 | #include <iostream> |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 12 | #include <optional> |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 13 | #include <replxx.hxx> |
Václav Kubernát | 90de950 | 2019-11-20 17:19:44 +0100 | [diff] [blame] | 14 | #include <sstream> |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 15 | #include "NETCONF_CLI_VERSION.h" |
Václav Kubernát | 96344a1 | 2018-05-28 16:33:39 +0200 | [diff] [blame] | 16 | #include "interpreter.hpp" |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 17 | #include "proxy_datastore.hpp" |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 18 | #include "yang_schema.hpp" |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 19 | #if defined(SYSREPO_CLI) |
Václav Kubernát | 6415b82 | 2018-08-22 17:40:01 +0200 | [diff] [blame] | 20 | #include "sysrepo_access.hpp" |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 21 | #define PROGRAM_NAME "sysrepo-cli" |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 22 | static const auto usage = R"(CLI interface to sysrepo |
| 23 | |
| 24 | Usage: |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 25 | sysrepo-cli [-d <datastore_target>] |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 26 | sysrepo-cli (-h | --help) |
| 27 | sysrepo-cli --version |
| 28 | |
| 29 | Options: |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 30 | -d <datastore_target> can be "running", "startup" or "operational" [default: operational])"; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 31 | #elif defined(YANG_CLI) |
| 32 | #include <boost/spirit/home/x3.hpp> |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 33 | #include <filesystem> |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 34 | #include "yang_access.hpp" |
| 35 | #define PROGRAM_NAME "yang-cli" |
| 36 | static const auto usage = R"(CLI interface for creating local YANG data instances |
| 37 | |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 38 | The <schema_file_or_module_name> argument is treated as a file name if a file |
| 39 | with such a path exists, otherwise it's treated as a module name. Search dirs |
| 40 | will be used to find a schema for that module. |
| 41 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 42 | Usage: |
Václav Kubernát | 0c90dd4 | 2022-01-18 00:07:29 +0100 | [diff] [blame] | 43 | yang-cli [--configonly] [--ignore-unknown-data] [-s <search_dir>] [-e enable_features]... [-i data_file]... <schema_file_or_module_name>... |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 44 | yang-cli (-h | --help) |
| 45 | yang-cli --version |
| 46 | |
| 47 | Options: |
Václav Kubernát | 0c90dd4 | 2022-01-18 00:07:29 +0100 | [diff] [blame] | 48 | -s <search_dir> Set search for schema lookup |
| 49 | -e <enable_features> Feature to enable after modules are loaded. This option can be supplied more than once. Format: <module_name>:<feature> |
| 50 | -i <data_file> File to import data from |
| 51 | --configonly Disable editing of operational data |
| 52 | --ignore-unknown-data Silently ignore data with no available schema file)"; |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 53 | #elif defined(NETCONF_CLI) |
| 54 | // FIXME: improve usage |
| 55 | static const auto usage = R"(CLI interface for NETCONF |
| 56 | |
| 57 | Usage: |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 58 | netconf-cli [-v] [-d <datastore_target>] [-p <port>] <host> |
| 59 | netconf-cli [-v] [-d <datastore_target>] --socket <path> |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 60 | netconf-cli (-h | --help) |
| 61 | netconf-cli --version |
| 62 | |
| 63 | Options: |
| 64 | -v enable verbose mode |
| 65 | -p <port> port number [default: 830] |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 66 | -d <datastore_target> can be "running", "startup" or "operational" [default: operational])"; |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 67 | #include "cli-netconf.hpp" |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 68 | #include "netconf_access.hpp" |
Jan Kundrát | 9435bc0 | 2021-01-04 18:03:01 +0100 | [diff] [blame] | 69 | #define PROGRAM_NAME "netconf-cli" |
Václav Kubernát | fd26116 | 2020-11-12 02:36:29 +0100 | [diff] [blame] | 70 | // FIXME: this should be replaced by C++20 std::jthread at some point |
| 71 | struct PoorMansJThread { |
| 72 | ~PoorMansJThread() |
| 73 | { |
| 74 | if (thread.joinable()) { |
| 75 | thread.join(); |
| 76 | } |
| 77 | } |
| 78 | std::thread thread; |
| 79 | }; |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 80 | #else |
| 81 | #error "Unknown CLI backend" |
| 82 | #endif |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 83 | |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 84 | const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history"; |
| 85 | |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 86 | int main(int argc, char* argv[]) |
| 87 | { |
| 88 | auto args = docopt::docopt(usage, |
| 89 | {argv + 1, argv + argc}, |
| 90 | true, |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 91 | PROGRAM_NAME " " NETCONF_CLI_VERSION, |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 92 | true); |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 93 | WritableOps writableOps = WritableOps::No; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 94 | |
Václav Kubernát | fd26116 | 2020-11-12 02:36:29 +0100 | [diff] [blame] | 95 | using replxx::Replxx; |
| 96 | Replxx lineEditor; |
| 97 | std::atomic<int> backendReturnCode = 0; |
| 98 | |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 99 | // For some reason, GCC10 still needs [[maybe_unused]] because of conditional compilation... |
| 100 | [[maybe_unused]] auto datastoreTarget = DatastoreTarget::Operational; |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 101 | if (const auto& ds = args["-d"]) { |
| 102 | if (ds.asString() == "startup") { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 103 | datastoreTarget = DatastoreTarget::Startup; |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 104 | } else if (ds.asString() == "running") { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 105 | datastoreTarget = DatastoreTarget::Running; |
| 106 | } else if (ds.asString() == "operational") { |
| 107 | datastoreTarget = DatastoreTarget::Operational; |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 108 | } else { |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 109 | std::cerr << PROGRAM_NAME << ": unknown datastore target: " << ds.asString() << "\n"; |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 110 | return 1; |
| 111 | } |
| 112 | } |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 113 | |
| 114 | auto datastoreTargetString = args["-d"] ? args["-d"].asString() : std::string("operational"); |
| 115 | |
| 116 | #if defined(SYSREPO_CLI) |
| 117 | auto datastore = std::make_shared<SysrepoAccess>(); |
| 118 | std::cout << "Connected to sysrepo [datastore target: " << datastoreTargetString << "]" << std::endl; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 119 | #elif defined(YANG_CLI) |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 120 | auto datastore = std::make_shared<YangAccess>(); |
Václav Kubernát | 28cf336 | 2020-06-29 17:52:51 +0200 | [diff] [blame] | 121 | if (args["--configonly"].asBool()) { |
| 122 | writableOps = WritableOps::No; |
| 123 | } else { |
| 124 | writableOps = WritableOps::Yes; |
| 125 | std::cout << "ops is writable" << std::endl; |
| 126 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 127 | if (const auto& search_dir = args["-s"]) { |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 128 | datastore->addSchemaDir(search_dir.asString()); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 129 | } |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 130 | for (const auto& schemaFile : args["<schema_file_or_module_name>"].asStringList()) { |
| 131 | if (std::filesystem::exists(schemaFile)) { |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 132 | datastore->addSchemaFile(schemaFile); |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 133 | } else if (schemaFile.find('/') == std::string::npos) { // Module names cannot have a slash |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 134 | datastore->loadModule(schemaFile); |
Václav Kubernát | 619e654 | 2020-06-29 14:13:43 +0200 | [diff] [blame] | 135 | } else { |
| 136 | std::cerr << "Cannot load YANG module " << schemaFile << "\n"; |
| 137 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 138 | } |
| 139 | if (const auto& enableFeatures = args["-e"]) { |
| 140 | namespace x3 = boost::spirit::x3; |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 141 | std::map<std::string, std::vector<std::string>> toEnable; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 142 | auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":"); |
| 143 | for (const auto& enableFeature : enableFeatures.asStringList()) { |
| 144 | std::pair<std::string, std::string> parsed; |
| 145 | auto it = enableFeature.begin(); |
| 146 | auto res = x3::parse(it, enableFeature.cend(), grammar, parsed); |
| 147 | if (!res || it != enableFeature.cend()) { |
| 148 | std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n"; |
| 149 | return 1; |
| 150 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 151 | toEnable[parsed.first].emplace_back(parsed.second); |
| 152 | } |
| 153 | try { |
| 154 | for (const auto& [moduleName, features] : toEnable) { |
| 155 | datastore->setEnabledFeatures(moduleName, features); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 156 | } |
Václav Kubernát | cfdb922 | 2021-07-07 22:36:24 +0200 | [diff] [blame] | 157 | } catch (std::runtime_error& ex) { |
| 158 | std::cerr << ex.what() << "\n"; |
| 159 | return 1; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 160 | } |
| 161 | } |
Václav Kubernát | 0c90dd4 | 2022-01-18 00:07:29 +0100 | [diff] [blame] | 162 | |
| 163 | auto strict = args.at("--ignore-unknown-data").asBool() ? StrictDataParsing::No : StrictDataParsing::Yes; |
| 164 | |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 165 | if (const auto& dataFiles = args["-i"]) { |
| 166 | for (const auto& dataFile : dataFiles.asStringList()) { |
Václav Kubernát | 0c90dd4 | 2022-01-18 00:07:29 +0100 | [diff] [blame] | 167 | datastore->addDataFile(dataFile, strict); |
Václav Kubernát | 548cb19 | 2020-06-26 14:00:42 +0200 | [diff] [blame] | 168 | } |
| 169 | } |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 170 | #elif defined(NETCONF_CLI) |
| 171 | auto verbose = args.at("-v").asBool(); |
| 172 | if (verbose) { |
Václav Kubernát | bde37ba | 2022-03-25 15:18:12 +0100 | [diff] [blame] | 173 | NetconfAccess::setNcLogLevel(libnetconf::LogLevel::Debug); |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | SshProcess process; |
Václav Kubernát | fd26116 | 2020-11-12 02:36:29 +0100 | [diff] [blame] | 177 | PoorMansJThread processWatcher; |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 178 | std::shared_ptr<NetconfAccess> datastore; |
| 179 | |
Jan Kundrát | 1ccf0d4 | 2021-02-05 09:09:57 +0100 | [diff] [blame] | 180 | if (args.at("--socket").asBool()) { |
| 181 | try { |
| 182 | datastore = std::make_shared<NetconfAccess>(args.at("<path>").asString()); |
| 183 | } catch (std::runtime_error& ex) { |
| 184 | std::cerr << "UNIX socket connection failed: " << ex.what() << std::endl; |
| 185 | return 1; |
| 186 | } |
| 187 | } else { |
| 188 | try { |
| 189 | process = sshProcess(args.at("<host>").asString(), args.at("-p").asString()); |
| 190 | processWatcher.thread = std::thread{std::thread{[&process, &lineEditor, &backendReturnCode] () { |
| 191 | process.process.wait(); |
| 192 | backendReturnCode = process.process.exit_code(); |
| 193 | // CTRL-U clears from the cursor to the start of the line |
| 194 | // CTRL-K clears from the cursor to the end of the line |
| 195 | // CTRL-D send EOF |
| 196 | lineEditor.emulate_key_press(replxx::Replxx::KEY::control('U')); |
| 197 | lineEditor.emulate_key_press(replxx::Replxx::KEY::control('K')); |
| 198 | lineEditor.emulate_key_press(replxx::Replxx::KEY::control('D')); |
| 199 | }}}; |
| 200 | datastore = std::make_shared<NetconfAccess>(process.std_out.native_source(), process.std_in.native_sink()); |
| 201 | } catch (std::runtime_error& ex) { |
| 202 | std::cerr << "SSH connection failed: " << ex.what() << std::endl; |
| 203 | return 1; |
| 204 | } |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 205 | } |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 206 | std::cout << "Connected via NETCONF [datastore target: " << datastoreTargetString << "]" << std::endl; |
Václav Kubernát | b79f3ca | 2020-02-04 15:56:01 +0100 | [diff] [blame] | 207 | #else |
| 208 | #error "Unknown CLI backend" |
| 209 | #endif |
| 210 | |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 211 | datastore->setTarget(datastoreTarget); |
| 212 | |
Václav Kubernát | e2e15ee | 2020-02-05 17:38:13 +0100 | [diff] [blame] | 213 | #if defined(SYSREPO_CLI) || defined(NETCONF_CLI) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 214 | auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) { |
| 215 | return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema())); |
| 216 | }; |
| 217 | #elif defined(YANG_CLI) |
| 218 | auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>&) { |
| 219 | return nullptr; |
| 220 | }; |
| 221 | #endif |
| 222 | |
| 223 | ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 224 | auto dataQuery = std::make_shared<DataQuery>(*datastore); |
| 225 | Parser parser(datastore->schema(), writableOps, dataQuery); |
Václav Kubernát | 395d92c | 2020-01-24 12:18:18 +0100 | [diff] [blame] | 226 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 227 | lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [&lineEditor](const auto& code) { |
Václav Kubernát | 4863729 | 2020-07-08 16:54:13 +0200 | [diff] [blame] | 228 | return lineEditor.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code); |
| 229 | }); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 230 | lineEditor.bind_key(Replxx::KEY::control('W'), [&lineEditor](const auto& code) { |
Václav Kubernát | 4863729 | 2020-07-08 16:54:13 +0200 | [diff] [blame] | 231 | return lineEditor.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code); |
| 232 | }); |
Václav Kubernát | 395d92c | 2020-01-24 12:18:18 +0100 | [diff] [blame] | 233 | |
| 234 | lineEditor.set_word_break_characters("\t _[]/:'\"=-%"); |
| 235 | |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 236 | lineEditor.set_completion_callback([&parser](const std::string& input, int& context) { |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 237 | std::stringstream stream; |
Václav Kubernát | 5c4f8a3 | 2021-08-24 22:57:41 +0200 | [diff] [blame] | 238 | Completions completions; |
| 239 | try { |
| 240 | completions = parser.completeCommand(input, stream); |
| 241 | } catch (std::exception& ex) { |
| 242 | std::cerr << "Error while completing: " << ex.what() << "\n"; |
| 243 | } |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 244 | |
Jan Kundrát | 8d8efe8 | 2019-10-18 10:21:36 +0200 | [diff] [blame] | 245 | std::vector<replxx::Replxx::Completion> res; |
Václav Kubernát | 1ed4aa3 | 2020-01-23 13:13:28 +0100 | [diff] [blame] | 246 | std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res)); |
| 247 | context = completions.m_contextLength; |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 248 | return res; |
| 249 | }); |
Václav Kubernát | 2b68461 | 2018-08-09 18:55:24 +0200 | [diff] [blame] | 250 | |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 251 | std::optional<std::string> historyFile; |
| 252 | if (auto xdgHome = getenv("XDG_DATA_HOME")) { |
| 253 | historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME; |
| 254 | } else if (auto home = getenv("HOME")) { |
| 255 | historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME; |
| 256 | } |
| 257 | |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 258 | if (historyFile) { |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 259 | lineEditor.history_load(historyFile.value()); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 260 | } |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 261 | |
Václav Kubernát | fd26116 | 2020-11-12 02:36:29 +0100 | [diff] [blame] | 262 | while (backendReturnCode == 0) { |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 263 | auto fullContextPath = parser.currentNode(); |
| 264 | std::string prompt; |
| 265 | if (auto activeRpcPath = proxyDatastore.inputDatastorePath()) { |
| 266 | auto rpcPrefixLength = activeRpcPath->size(); |
| 267 | prompt = "(prepare: " + *activeRpcPath + ") " + fullContextPath.substr(rpcPrefixLength); |
| 268 | } else { |
| 269 | prompt = fullContextPath; |
| 270 | } |
| 271 | |
| 272 | prompt += "> "; |
| 273 | |
| 274 | auto line = lineEditor.input(prompt); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 275 | if (!line) { |
Václav Kubernát | 82bf131 | 2019-11-05 11:19:26 +0100 | [diff] [blame] | 276 | // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN. |
| 277 | // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program. |
| 278 | // I have no idea why replxx uses errno for this. |
| 279 | if (errno == EAGAIN) { |
| 280 | continue; |
| 281 | } else { |
| 282 | break; |
| 283 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 284 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 285 | |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 286 | std::locale C_locale("C"); |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 287 | std::string_view view{line}; |
| 288 | if (std::all_of(view.begin(), view.end(), |
Jan Kundrát | e387702 | 2018-09-05 15:32:09 +0200 | [diff] [blame] | 289 | [C_locale](const auto c) { return std::isspace(c, C_locale);})) { |
| 290 | continue; |
| 291 | } |
| 292 | |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 293 | try { |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 294 | command_ cmd = parser.parseCommand(line, std::cout); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 295 | boost::apply_visitor(Interpreter(parser, proxyDatastore), cmd); |
Petr Gotthard | 1c76dea | 2022-04-13 17:56:58 +0200 | [diff] [blame] | 296 | if (cmd.type() == typeid(quit_)) { |
| 297 | break; |
| 298 | } |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 299 | } catch (InvalidCommandException& ex) { |
| 300 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | c58e4aa | 2019-04-03 18:37:32 +0200 | [diff] [blame] | 301 | } catch (DatastoreException& ex) { |
| 302 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 303 | } catch (std::runtime_error& ex) { |
| 304 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | 160e5a2 | 2020-12-01 01:11:28 +0100 | [diff] [blame] | 305 | } catch (std::logic_error& ex) { |
| 306 | std::cerr << ex.what() << std::endl; |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 307 | } |
Václav Kubernát | 5b80e52 | 2019-01-25 12:17:03 +0100 | [diff] [blame] | 308 | |
Václav Kubernát | a395d33 | 2019-02-13 16:49:20 +0100 | [diff] [blame] | 309 | lineEditor.history_add(line); |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 312 | if (historyFile) { |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 313 | lineEditor.history_save(historyFile.value()); |
Václav Kubernát | 3a43323 | 2020-07-08 17:52:50 +0200 | [diff] [blame] | 314 | } |
Václav Kubernát | 435706e | 2019-02-20 18:05:59 +0100 | [diff] [blame] | 315 | |
Václav Kubernát | fd26116 | 2020-11-12 02:36:29 +0100 | [diff] [blame] | 316 | return backendReturnCode; |
Jan Kundrát | dc2b072 | 2018-03-02 14:13:37 +0100 | [diff] [blame] | 317 | } |