SNode.C
Loading...
Searching...
No Matches
utils Namespace Reference

Namespaces

namespace  config
namespace  CallForCommandline
namespace  system

Classes

class  ConfigRoot
class  Config
class  AppWithPtr
class  SubCommand
class  Uuid
class  Timeval
class  DaemonFailure
class  DaemonError
class  DaemonSignaled
class  Daemon
class  PreserveErrno
class  Random
struct  fixed_string
class  AttributeProxy
class  SingleAttributeInjector
class  MultibleAttributeInjector
class  SHA1

Concepts

concept  InjectableAttribute

Functions

static std::string bash_backslash_escape_no_whitespace (std::string_view s)
static void createCommandLineOptions (std::stringstream &out, CLI::App *app, utils::CallForCommandline::Mode mode)
static std::string createCommandLineOptions (CLI::App *app, utils::CallForCommandline::Mode mode)
static void createCommandLineTemplate (std::stringstream &out, CLI::App *app, utils::CallForCommandline::Mode mode)
static std::string createCommandLineSubcommands (CLI::App *app, utils::CallForCommandline::Mode mode)
static std::string createCommandLineTemplate (CLI::App *app, utils::CallForCommandline::Mode mode)
static std::string getCommandLine (CLI::App *commandlineTriggerApp)
static std::string getConfig (CLI::App *configTriggeredApp)
static std::string doWriteConfig (utils::SubCommand *subCommand)
static std::string getHelp (utils::SubCommand *subCommand, CLI::App *helpTriggerApp)
std::string hexDump (const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
std::string hexDump (const std::string &string, int prefixLength, bool prefixAtFirstLine)
std::string hexDump (const char *bytes, uint64_t length, int prefixLength, bool prefixAtFirstLine)
static std::vector< unsigned char > transform_to_binary (const std::string &string)
std::vector< unsigned char > sha1 (const std::string &string)
static std::shared_ptr< CLI::HelpFormattermakeSectionFormatter ()
std::ostream & operator<< (std::ostream &ostream, const utils::Timeval &timeVal)
Timeval operator* (double mul, const utils::Timeval &timeVal)
template<unsigned N>
 fixed_string (char const (&)[N]) -> fixed_string< N - 1 >

Function Documentation

◆ bash_backslash_escape_no_whitespace()

std::string utils::bash_backslash_escape_no_whitespace ( std::string_view s)
static

Definition at line 81 of file Config.cpp.

81 {
82 static const std::unordered_set<char> special{
83 '\\', '\'', // quoting/escape
84 '`', '$', // substitution
85 '|', '&', ';', '<', '>', '(', ')', '{', '}', // operators
86 '*', '?', '[', ']', '~', '!', '#', '=' // globbing/history/others
87 };
88
89 std::string out;
90 out.reserve(s.size() * 3);
91
92 for (const char c : s) {
93 if (special.contains(c)) {
94 out.push_back('\\');
95 }
96 out.push_back(c);
97 }
98 return out;
99 }

Referenced by createCommandLineOptions().

Here is the caller graph for this function:

◆ createCommandLineOptions() [1/2]

std::string utils::createCommandLineOptions ( CLI::App * app,
utils::CallForCommandline::Mode mode )
static

Definition at line 181 of file Config.cpp.

181 {
182 std::stringstream out;
183
184 createCommandLineOptions(out, app, mode);
185
186 std::string optionString = out.str();
187 if (!optionString.empty() && optionString.back() == ' ') {
188 optionString.pop_back();
189 }
190
191 return optionString;
192 }
static void createCommandLineOptions(std::stringstream &out, CLI::App *app, utils::CallForCommandline::Mode mode)
Definition Config.cpp:101

References createCommandLineOptions().

Referenced by createCommandLineTemplate(), and createCommandLineTemplate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineOptions() [2/2]

void utils::createCommandLineOptions ( std::stringstream & out,
CLI::App * app,
utils::CallForCommandline::Mode mode )
static

Definition at line 101 of file Config.cpp.

101 {
102 const CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
103 const bool disabled = disabledOpt != nullptr ? disabledOpt->as<bool>() : false;
104 if (!disabled || mode == utils::CallForCommandline::Mode::COMPLETE) {
105 for (const CLI::Option* option : app->get_options()) {
106 if (option->get_configurable()) {
107 std::string value;
108
109 switch (mode) {
111 if (option->count() > 0) {
112 try {
113 for (const auto& result : option->reduced_results()) {
114 value += (!result.empty() ? result : "\"\"") + " ";
115 }
116 value.pop_back();
117 } catch (CLI::ParseError& e) {
118 value = std::string{"<["} + Color::Code::FG_RED + e.get_name() + Color::Code::FG_DEFAULT + "] " +
119 e.what() + ">";
120 }
121 } else if (option->get_required()) {
122 value = "<REQUIRED>";
123 }
124 break;
126 if (option->get_required()) {
127 if (option->count() > 0) {
128 try {
129 for (const auto& result : option->reduced_results()) {
130 value += (!result.empty() ? result : "\"\"") + " ";
131 }
132 value.pop_back();
133 } catch (CLI::ParseError& e) {
134 value = std::string{"<["} + Color::Code::FG_RED + e.get_name() + Color::Code::FG_DEFAULT + "] " +
135 e.what() + ">";
136 }
137 } else {
138 value = "<REQUIRED>";
139 }
140 }
141 break;
144 if (option->count() > 0) {
145 try {
146 for (const auto& result : option->reduced_results()) {
147 value += (!result.empty() ? result : "\"\"") + " ";
148 }
149 value.pop_back();
150 } catch (CLI::ParseError& e) {
151 value = std::string{"<["} + Color::Code::FG_RED + e.get_name() + Color::Code::FG_DEFAULT + "] " +
152 e.what() + ">";
153 }
154 } else if (!option->get_default_str().empty()) {
155 value = option->get_default_str();
156 } else if (!option->get_required()) {
157 value = "\"\"";
158 } else {
159 value = "<REQUIRED>";
160 }
161 break;
162 }
163
164 if (!value.empty()) {
165 if (value.starts_with(std::string{"["}) && value.ends_with("]")) {
166 value = value.substr(1, value.size() - 2);
167 }
168
169 if (value != "<REQUIRED>" && value != "\"\"" && !value.starts_with("<[")) {
171 }
172 out << "--" << option->get_single_name() << ((option->get_items_expected_max() == 0) ? "=" : " ") << value << " ";
173 }
174 }
175 }
176 } else if (disabledOpt->get_default_str() == "false") {
177 out << "--disabled=true ";
178 }
179 }
@ FG_DEFAULT
Definition Logger.h:69
static std::string bash_backslash_escape_no_whitespace(std::string_view s)
Definition Config.cpp:81

References utils::CallForCommandline::ACTIVE, bash_backslash_escape_no_whitespace(), utils::CallForCommandline::COMPLETE, Color::FG_DEFAULT, Color::FG_RED, Color::operator+(), utils::CallForCommandline::REQUIRED, and utils::CallForCommandline::STANDARD.

Referenced by createCommandLineOptions().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineSubcommands()

std::string utils::createCommandLineSubcommands ( CLI::App * app,
utils::CallForCommandline::Mode mode )
static

Definition at line 196 of file Config.cpp.

196 {
197 std::stringstream out;
198
199 const CLI::Option* disabledOpt = app->get_option_no_throw("--disabled");
200 if (disabledOpt == nullptr || !disabledOpt->as<bool>() || mode == utils::CallForCommandline::Mode::COMPLETE) {
201 for (CLI::App* subcommand : app->get_subcommands({})) {
202 if (!subcommand->get_name().empty()) {
204 }
205 }
206 }
207
208 return out.str();
209 }
static void createCommandLineTemplate(std::stringstream &out, CLI::App *app, utils::CallForCommandline::Mode mode)
Definition Config.cpp:211

References utils::CallForCommandline::COMPLETE, and createCommandLineTemplate().

Referenced by createCommandLineTemplate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineTemplate() [1/2]

std::string utils::createCommandLineTemplate ( CLI::App * app,
utils::CallForCommandline::Mode mode )
static

Definition at line 226 of file Config.cpp.

226 {
227 std::stringstream out;
228
229 createCommandLineTemplate(out, app, mode);
230
231 std::string outString = out.str();
232 while (app->get_parent() != nullptr) {
233 app = app->get_parent();
234 std::string parentOptions = createCommandLineOptions(app, mode);
235 outString =
236 std::string(app->get_name()).append(" ").append(!parentOptions.empty() ? parentOptions.append(" ") : "").append(outString);
237 }
238
239 if (outString.empty()) {
240 outString = Config::getApplicationName();
241 }
242
243 return outString;
244 }
static const std::string & getApplicationName()
Definition Config.cpp:786

References createCommandLineOptions(), createCommandLineTemplate(), and utils::Config::getApplicationName().

Referenced by getCommandLine().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ createCommandLineTemplate() [2/2]

void utils::createCommandLineTemplate ( std::stringstream & out,
CLI::App * app,
utils::CallForCommandline::Mode mode )
static

Definition at line 211 of file Config.cpp.

211 {
212 std::string outString;
213
214 outString = createCommandLineOptions(app, mode);
215 if (!outString.empty()) {
216 outString += " ";
217 }
218
219 outString += createCommandLineSubcommands(app, mode);
220
221 if (!outString.empty()) {
222 out << app->get_name() << " " << outString;
223 }
224 }
static std::string createCommandLineSubcommands(CLI::App *app, utils::CallForCommandline::Mode mode)
Definition Config.cpp:196

References createCommandLineOptions(), and createCommandLineSubcommands().

Referenced by createCommandLineSubcommands(), and createCommandLineTemplate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ doWriteConfig()

std::string utils::doWriteConfig ( utils::SubCommand * subCommand)
static

Definition at line 304 of file Config.cpp.

304 {
305 std::stringstream out;
306
307 VLOG(0) << "1: subCommand->getOption(--write-config)->as<std::string>()";
308 std::ofstream confFile(subCommand->getOption("--write-config")->as<std::string>());
309 if (confFile.is_open()) {
310 VLOG(0) << "2: subCommand->getOption(--write-config)->as<std::string>()";
311 VLOG(0) << subCommand->configToStr();
312
313 try {
314 confFile << subCommand->configToStr();
315 confFile.close();
316 out << std::string{"["} << Color::Code::FG_GREEN << "SUCCESS" << Color::Code::FG_DEFAULT
317 << "] Writing config file: " + subCommand->getOption("--write-config")->as<std::string>() << std::endl
318 << std::endl;
319 } catch (const CLI::ParseError& e) {
320 confFile.close();
321 out << std::string{"["} << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT
322 << "] Writing config file: " << e.get_name() << " " << e.what() << std::endl;
323 }
324 confFile.close();
325 } else {
326 out << std::string{"["} << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT
327 << "] Writing config file: " << std::strerror(errno) << std::endl;
328 }
329
330 return out.str();
331 }
CLI::Option * getOption(const std::string &name) const
std::string configToStr() const

References utils::SubCommand::configToStr(), Color::FG_DEFAULT, Color::FG_GREEN, Color::FG_RED, and utils::SubCommand::getOption().

Referenced by utils::ConfigRoot::parse2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fixed_string()

template<unsigned N>
utils::fixed_string ( char const(&)[N]) ->fixed_string< N-1 >

◆ getCommandLine()

std::string utils::getCommandLine ( CLI::App * commandlineTriggerApp)
static

Definition at line 246 of file Config.cpp.

246 {
247 const std::string modeString = commandlineTriggerApp->get_option("--command-line")->as<std::string>();
248
250 std::ostringstream out;
251
252 if (modeString == "standard") {
253 out << "Below is a command line viewing all non-default and required options:\n"
254 "* Options show their configured value\n"
255 "* Required but not yet configured options show <REQUIRED> as value\n"
256 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap"
257 << std::endl;
259
260 } else if (modeString == "active") {
261 out << "Below is a command line viewing the active set of options with their default or configured values:\n"
262 "* Options show either their configured or default value\n"
263 "* Required but not yet configured options show <REQUIRED> as value\n"
264 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap"
265 << std::endl;
267 } else if (modeString == "complete") {
268 out << "Below is a command line viewing the complete set of options with their default values\n"
269 "* Options show their default value\n"
270 "* Required but not yet configured options show <REQUIRED> as value\n"
271 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap"
272 << std::endl;
274 } else if (modeString == "required") {
275 out << "Below is a command line viewing required options only:\n"
276 "* Options show either their configured or default value\n"
277 "* Required but not yet configured options show <REQUIRED> as value\n"
278 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap"
279 << std::endl;
281 }
282
283 out << std::endl
284 << Color::Code::FG_GREEN << "command@line" << Color::Code::FG_DEFAULT << ":" << Color::Code::FG_BLUE << "~/> "
285 << Color::Code::FG_DEFAULT << createCommandLineTemplate(commandlineTriggerApp, mode) << std::endl
286 << std::endl;
287
288 return out.str();
289 }

References utils::CallForCommandline::ACTIVE, utils::CallForCommandline::COMPLETE, createCommandLineTemplate(), Color::FG_BLUE, Color::FG_DEFAULT, Color::FG_GREEN, utils::CallForCommandline::REQUIRED, and utils::CallForCommandline::STANDARD.

Referenced by utils::ConfigRoot::parse2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getConfig()

std::string utils::getConfig ( CLI::App * configTriggeredApp)
static

Definition at line 291 of file Config.cpp.

291 {
292 std::stringstream out;
293
294 try {
295 out << configTriggeredApp->config_to_str(true, true);
296 } catch (const CLI::ParseError& e) {
297 out << std::string{"["} << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT
298 << "] Showing current config: " << configTriggeredApp->get_name() << " " << e.get_name() << " " << e.what();
299 }
300
301 return out.str();
302 }

References Color::FG_DEFAULT, and Color::FG_RED.

Referenced by utils::ConfigRoot::parse2().

Here is the caller graph for this function:

◆ getHelp()

std::string utils::getHelp ( utils::SubCommand * subCommand,
CLI::App * helpTriggerApp )
static

Definition at line 333 of file Config.cpp.

333 {
334 std::stringstream out;
335
336 const std::string helpMode =
337 (helpTriggerApp != nullptr ? helpTriggerApp->get_option("--help") : subCommand->getOption("--help"))->as<std::string>();
338
339 const CLI::App* helpApp = nullptr;
340 CLI::AppFormatMode mode = CLI::AppFormatMode::Normal;
341 if (helpMode == "exact") {
342 helpApp = helpTriggerApp;
343 } else if (helpMode == "expanded") {
344 helpApp = helpTriggerApp;
345 mode = CLI::AppFormatMode::All;
346 }
347 try {
348 out << subCommand->help(helpApp, mode);
349 } catch (CLI::ParseError& e) {
350 out << std::string{"["} << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT << "] Show help: " << e.get_name() << " "
351 << e.what();
352 }
353
354 return out.str();
355 }
std::string help(const CLI::App *helpApp, const CLI::AppFormatMode &mode) const

References Color::FG_DEFAULT, Color::FG_RED, utils::SubCommand::getOption(), and utils::SubCommand::help().

Referenced by utils::ConfigRoot::parse2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hexDump() [1/3]

std::string utils::hexDump ( const char * bytes,
uint64_t length,
int prefixLength = 0,
bool prefixAtFirstLine = false )

Definition at line 66 of file hexdump.cpp.

66 {
67 std::stringstream hexStream;
68
69 if (length > 0) {
70 uint8_t buff[17];
71 size_t i = 0;
72
73 hexStream << std::hex;
74
75 int currentPrefixLength = prefixAtFirstLine ? prefixLength : 0;
76
77 // Process every byte in the data.
78 for (i = 0; i < length; i++) {
79 // Multiple of 16 means new line (with line offset).
80
81 if ((i % 16) == 0) {
82 // Just don't print ASCII for the zeroth line.
83 if (i != 0) {
84 hexStream << " " << buff << std::endl;
85 }
86
87 // Output the offset.
88 hexStream << Color::Code::FG_BLUE;
89 hexStream << std::setw(currentPrefixLength) << std::setfill(' ') << ""
90 << ": " << std::setw(8) << std::setfill('0') << static_cast<unsigned int>(i);
91 hexStream << Color::Code::FG_DEFAULT << " ";
92 }
93
94 // Now the hex code for the specific character.
95 hexStream << Color::Code::FG_GREEN;
96 hexStream << " " << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(static_cast<unsigned char>(bytes[i]));
97 hexStream << Color::Code::FG_DEFAULT;
98
99 // And store a printable ASCII character for later.
100 if ((bytes[i] < 0x20) || (bytes[i] > 0x7e)) {
101 buff[i % 16] = '.';
102 } else {
103 buff[i % 16] = static_cast<uint8_t>(bytes[i]);
104 }
105 buff[(i % 16) + 1] = '\0';
106
107 currentPrefixLength = prefixLength;
108 }
109
110 hexStream << std::dec;
111
112 // Pad out last line if not exactly 16 characters.
113 while ((i % 16) != 0) {
114 hexStream << " ";
115 i++;
116 }
117
118 // And print the final ASCII bit.
119 hexStream << " " << buff;
120 }
121
122 return hexStream.str();
123 }

References Color::FG_BLUE, Color::FG_DEFAULT, and Color::FG_GREEN.

Referenced by hexDump(), hexDump(), web::websocket::Receiver::readPayload(), and web::websocket::Transmitter::sendFrame().

Here is the caller graph for this function:

◆ hexDump() [2/3]

std::string utils::hexDump ( const std::string & string,
int prefixLength = 0,
bool prefixAtFirstLine = false )

Definition at line 62 of file hexdump.cpp.

62 {
63 return hexDump(string.data(), string.length(), prefixLength, prefixAtFirstLine);
64 }
std::string hexDump(const std::vector< char > &bytes, int prefixLength, bool prefixAtFirstLine)
Definition hexdump.cpp:58

References hexDump().

Here is the call graph for this function:

◆ hexDump() [3/3]

std::string utils::hexDump ( const std::vector< char > & bytes,
int prefixLength = 0,
bool prefixAtFirstLine = false )

Definition at line 58 of file hexdump.cpp.

58 {
59 return hexDump(bytes.data(), bytes.size(), prefixLength, prefixAtFirstLine);
60 }

References hexDump().

Referenced by iot::mqtt::SubProtocol< WSSubProtocolRoleT >::onMessageData(), iot::mqtt::Mqtt::toHexString(), httputils::toString(), and httputils::toString().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ makeSectionFormatter()

std::shared_ptr< CLI::HelpFormatter > utils::makeSectionFormatter ( )
static

Definition at line 350 of file SubCommand.cpp.

350 {
351 const std::shared_ptr<CLI::HelpFormatter> sectionFormatter = std::make_shared<CLI::HelpFormatter>();
352
353 sectionFormatter->label("SUBCOMMAND", "SECTION");
354 sectionFormatter->label("SUBCOMMANDS", "SECTIONS");
355 sectionFormatter->label("PERSISTENT", "");
356 sectionFormatter->label("Persistent Options", "Options (persistent)");
357 sectionFormatter->label("Nonpersistent Options", "Options (nonpersistent)");
358 sectionFormatter->label("Usage", "\nUsage");
359 sectionFormatter->label("bool:{true,false}", "{true,false}");
360 sectionFormatter->label(":{standard,active,complete,required}", "{standard,active,complete,required}");
361 sectionFormatter->label(":{standard,exact,expanded}", "{standard,exact,expanded}");
362 sectionFormatter->column_width(7);
363
364 return sectionFormatter;
365 }

◆ operator*()

Timeval utils::operator* ( double mul,
const utils::Timeval & timeVal )

Definition at line 223 of file Timeval.cpp.

223 {
224 utils::Timeval help;
225
226 help.timeVal.tv_sec = static_cast<time_t>(static_cast<double>(timeVal.timeVal.tv_sec) * mul);
227 help.timeVal.tv_usec = static_cast<suseconds_t>(static_cast<double>(timeVal.timeVal.tv_usec) * mul);
228
229 return help.normalize();
230 }
timeval timeVal
Definition Timeval.h:99
const Timeval & normalize()
Definition Timeval.cpp:204

◆ operator<<()

std::ostream & utils::operator<< ( std::ostream & ostream,
const utils::Timeval & timeVal )

Definition at line 218 of file Timeval.cpp.

218 {
219 return ostream << std::string("{") + std::to_string(timeVal.timeVal.tv_sec) + std::string(":") +
220 std::to_string(timeVal.timeVal.tv_usec) + std::string("}");
221 }

◆ sha1()

std::vector< unsigned char > utils::sha1 ( const std::string & string)

Definition at line 267 of file sha1.cpp.

267 {
268 SHA1 checksum;
269 checksum.update(string);
270 return transform_to_binary(checksum.final());
271 }
std::string final()
Definition sha1.cpp:79
void update(const std::string &s)
Definition sha1.cpp:57
static std::vector< unsigned char > transform_to_binary(const std::string &string)
Definition sha1.cpp:251

References utils::SHA1::final(), transform_to_binary(), and utils::SHA1::update().

Referenced by base64::serverWebSocketKey().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ transform_to_binary()

std::vector< unsigned char > utils::transform_to_binary ( const std::string & string)
static

Definition at line 251 of file sha1.cpp.

251 {
252 std::vector<unsigned char> buf;
253
254 char hex_byte[3];
255 hex_byte[2] = 0;
256
257 for (std::size_t i = 0, j = 0; i < string.size(); i += 2, j += 1) {
258 hex_byte[0] = string.at(i);
259 hex_byte[1] = string.at(i + 1);
260 char* end_ptr = nullptr;
261 buf.push_back(static_cast<unsigned char>(strtoul(hex_byte, &end_ptr, 16)));
262 }
263
264 return buf;
265 }

Referenced by sha1().

Here is the caller graph for this function: