0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-05-23 13:23:46 +02:00
construct/construct/console.cc

516 lines
9.3 KiB
C++
Raw Normal View History

2018-02-04 03:22:01 +01:00
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
2017-03-21 03:24:41 +01:00
#include <ircd/ircd.h>
2018-02-22 22:33:53 +01:00
#include "construct.h"
2019-02-10 01:06:59 +01:00
#include "console.h"
2017-03-21 03:24:41 +01:00
using namespace ircd;
2018-02-22 22:28:34 +01:00
IRCD_EXCEPTION_HIDENAME(ircd::error, bad_command)
decltype(construct::console)
construct::console;
2017-03-21 03:24:41 +01:00
decltype(construct::console::stack_sz)
construct::console::stack_sz
{
{ "name", "construct.console.stack.size" },
{ "default", long(2_MiB) },
};
decltype(construct::console::input_max)
construct::console::input_max
{
{ "name", "construct.console.input.max" },
{ "default", long(64_KiB) },
};
decltype(construct::console::ratelimit_sleep)
construct::console::ratelimit_sleep
{
{ "name", "construct.console.ratelimit.sleep" },
{ "default", 75L },
};
decltype(construct::console::ratelimit_bytes)
construct::console::ratelimit_bytes
{
{ "name", "construct.console.ratelimit.bytes" },
{ "default", long(2_KiB) },
};
decltype(construct::console::generic_message)
construct::console::generic_message
{R"(
*** - To end the console session: type exit, or ctrl-d -> EOF
*** - To shutdown cleanly: type die, or ctrl-\ -> SIGQUIT
*** - To generate a coredump for developers, type ABORT -> abort()
***)"_sv
};
decltype(construct::console::console_message)
construct::console::console_message
2017-03-21 03:24:41 +01:00
{R"(
***
*** The server is still running in the background. This is the
*** terminal console also available in your !control room.
***)"_sv
};
2017-03-21 03:24:41 +01:00
decltype(construct::console::seen_message)
construct::console::seen_message;
decltype(construct::console::queue)
construct::console::queue;
bool
construct::console::spawn()
2017-03-21 03:24:41 +01:00
{
if(active())
return false;
construct::console = new console;
return true;
}
2017-03-21 03:24:41 +01:00
bool
construct::console::execute(std::string cmd)
{
console::queue.emplace_back(std::move(cmd));
console::spawn();
return true;
}
2017-03-21 03:24:41 +01:00
bool
construct::console::interrupt()
{
if(active())
{
construct::console->context.interrupt();
return true;
}
return false;
}
bool
construct::console::terminate()
{
if(active())
2017-03-21 03:24:41 +01:00
{
construct::console->context.terminate();
return true;
}
return false;
}
2017-08-23 23:11:40 +02:00
bool
construct::console::active()
{
return construct::console != nullptr;
}
//
// console::console
//
construct::console::console()
:context
{
"console",
stack_sz,
std::bind(&console::main, this),
ircd::context::POST
}
,runlevel_changed
{
std::bind(&console::on_runlevel, this, std::placeholders::_1)
2017-03-21 03:24:41 +01:00
}
{
}
void
construct::console::main()
try
{
const unwind destruct{[]
{
construct::console->context.detach();
delete construct::console;
construct::console = nullptr;
}};
if(!wait_running())
return;
ircd::module module{"console"};
this->module = &module;
if(next_command())
{
while(handle_line())
if(!next_command())
break;
return;
}
show_message(); do
{
ctx::interruption_point();
wait_input();
}
while(handle_line());
}
catch(const std::exception &e)
{
std::cout
<< "\n***"
<< "\n*** The console session has ended: " << e.what()
<< "\n***"
<< std::endl;
log::debug
{
"The console session has ended: %s", e.what()
};
}
2017-08-23 23:11:40 +02:00
bool
construct::console::handle_line()
2017-03-21 03:24:41 +01:00
try
{
if(line == "ABORT")
abort();
if(line == "TERMINATE")
std::terminate();
if(line == "terminate")
ircd::terminate();
2017-03-21 03:24:41 +01:00
if(line == "EXIT")
exit(0);
if(startswith(line, "record"))
return cmd__record();
2018-12-24 03:39:09 +01:00
if(startswith(line, "watch"))
return cmd__watch();
int ret{-1};
if(module) switch((ret = handle_line_bymodule()))
2017-03-21 03:24:41 +01:00
{
default: break;
case 0: return false;
case 1: return true;
2018-02-22 22:28:34 +01:00
}
2017-09-25 03:05:42 +02:00
throw bad_command
{
"%s", line
};
2018-02-22 22:28:34 +01:00
}
catch(const std::out_of_range &e)
{
std::cerr << "missing required arguments. " << std::endl;
return true;
}
catch(const bad_command &e)
{
std::cerr << "Bad command or file name: " << e.what() << std::endl;
return true;
}
catch(const http::error &e)
2018-02-22 22:28:34 +01:00
{
log::error
{
"%s %s", e.what(), e.content
};
2018-02-22 22:28:34 +01:00
return true;
}
catch(const std::exception &e)
{
log::error
{
"%s", e.what()
};
2018-02-22 22:28:34 +01:00
return true;
}
2017-10-17 09:49:33 +02:00
int
construct::console::handle_line_bymodule()
{
using prototype = int (std::ostream &, const string_view &, const string_view &);
const mods::import<prototype> command
{
*module, "console_command"
};
std::ostringstream out;
out.exceptions(out.badbit | out.failbit | out.eofbit);
int ret;
static const string_view opts;
switch((ret = command(out, line, opts)))
{
case 0:
case 1:
{
// For this scope we have to suppress again because there's some
// yielding business going on below where log messages can break
// into the command output.
const log::console_quiet quiet{false};
const auto str
{
out.str()
};
// If this string is set, the user wants to log a copy of the output
// to the file at this path.
if(!empty(record_path))
{
const fs::fd fd
{
record_path, std::ios::out | std::ios::app
};
// Generate a copy of the command line to give some context
// to the output following it.
const std::string cmdline
{
"\n> "s + line + "\n\n"
};
append(fd, string_view(cmdline));
append(fd, string_view(str));
}
// The string is iterated for rate-limiting. After a configured
// number of bytes sent to stdout we sleep the ircd::ctx for a
// configured number of milliseconds. If these settings are too
// aggressive then the output heading to stdout won't appear in
// the terminal after the buffers are filled.
for(size_t off(0); off < str.size(); off += size_t(ratelimit_bytes))
{
const string_view substr
{
str.data() + off, std::min(str.size() - off, size_t(ratelimit_bytes))
};
std::cout << substr << std::flush;
ctx::sleep(milliseconds(ratelimit_sleep));
}
if(!endswith(str, '\n'))
std::cout << std::endl;
return ret;
}
// The command was handled but the arguments were bad_command.
// The module has its own class for a bad_command exception which
// is a local and separate symbol from the bad_command here so
// we use this code to translate it.
case -2: throw bad_command
{
"%s", out.str()
};
// Command isn't handled by the module; continue handling here
default:
break;
}
return ret;
}
bool
construct::console::cmd__record()
{
const string_view &args
{
tokens_after(line, ' ', 0)
};
if(empty(args) && empty(record_path))
{
std::cout << "Console not currently recorded to any file." << std::endl;
return true;
}
if(empty(args) && !empty(record_path))
{
std::cout << "Stopped recording to file `" << record_path << "'" << std::endl;
record_path = {};
return true;
}
const auto path
{
token(args, ' ', 0)
};
std::cout << "Recording console to file `" << path << "'" << std::endl;
record_path = path;
return true;
}
2018-12-24 03:39:09 +01:00
bool
construct::console::cmd__watch()
{
const auto delay
{
lex_cast<seconds>(token(this->line, ' ', 1))
};
const string_view &line
{
tokens_after(this->line, ' ', 1)
};
this->line = line; do
2018-12-24 03:39:09 +01:00
{
std::cout << '\n';
handle_line(); try
{
const log::console_quiet quiet(false);
2018-12-24 03:39:09 +01:00
ctx::sleep(delay);
}
catch(const ctx::interrupted &)
{
break;
}
}
while(1);
return true;
}
2018-02-22 22:28:34 +01:00
void
construct::console::wait_input()
2018-02-22 22:28:34 +01:00
{
line = {}; do
{
// Suppression scope ends after the command is entered
// so the output of the command (if log messages) can be seen.
const log::console_quiet quiet(false);
std::cout << "\n> " << std::flush;
line.resize(size_t(input_max));
const mutable_buffer buffer
{
const_cast<char *>(line.data()), line.size()
};
2017-10-17 09:49:33 +02:00
const string_view read
{
fs::stdin::readline(buffer)
};
2017-10-17 09:49:33 +02:00
line.resize(size(read));
if(startswith(line, "\x1B"_sv))
esc_handle();
}
while(line.empty());
history.emplace_back(line);
}
bool
construct::console::esc_handle()
{
if(startswith(line, "\x1B\x5B"_sv) && size(line) >= 3)
return esc_handle_bra();
line = {};
return true;
}
bool
construct::console::esc_handle_bra()
{
switch(line[2])
{
case 'A': // up-arrow
{
if(history.empty())
{
line = {};
return false;
}
line = history.front();
history.pop_front();
return true;
}
}
line = {};
return true;
}
2017-10-17 09:49:33 +02:00
bool
construct::console::next_command()
{
line = {};
while(!queue.empty() && line.empty())
2018-02-22 22:28:34 +01:00
{
line = std::move(queue.front());
queue.pop_front();
2018-02-22 22:28:34 +01:00
}
2017-09-25 03:05:42 +02:00
return !line.empty();
2018-02-22 22:28:34 +01:00
}
void
construct::console::on_runlevel(const enum ircd::run::level &runlevel)
2018-02-22 22:28:34 +01:00
{
switch(runlevel)
{
case ircd::run::level::QUIT:
case ircd::run::level::HALT:
console::terminate();
break;
default:
break;
}
2018-02-22 22:28:34 +01:00
}
2017-10-17 09:49:33 +02:00
bool
construct::console::wait_running()
const
2018-02-22 22:28:34 +01:00
{
ircd::run::changed::dock.wait([]
{
return ircd::run::level == ircd::run::level::RUN ||
ircd::run::level == ircd::run::level::QUIT ||
ircd::run::level == ircd::run::level::HALT;
});
2017-09-25 03:05:42 +02:00
return ircd::run::level == ircd::run::level::RUN;
2018-02-22 22:28:34 +01:00
}
void
construct::console::show_message()
const
2018-02-22 22:28:34 +01:00
{
std::call_once(seen_message, []
{
std::cout << console_message << generic_message;
});
2018-02-22 22:28:34 +01:00
}