Indent every scope and put opening braces on the signature line

Namespace and extern "C" bodies are indented a level, so nesting is
visible from the indentation rather than only from the braces. Opening
braces stay on the line that opens the scope, including function
definitions, which previously carried theirs on a line of their own.
This commit is contained in:
2026-08-15 23:47:43 -04:00
parent 9fd7d2f433
commit 12a4f68025
13 changed files with 634 additions and 709 deletions

View File

@@ -63,7 +63,6 @@ struct dpm_ctx {
};
namespace dpm_core {
/**
* @brief Records a failure reason on the context
*
@@ -81,5 +80,4 @@ void set_error(dpm_ctx* ctx, const std::string& msg);
* @param ctx The libdpm-core context
*/
void load_config_dir(dpm_ctx* ctx);
} // namespace dpm_core

View File

@@ -65,7 +65,6 @@ struct dpm_cursor {
void dpm_internal_unload(void* handle);
namespace dpm_core {
/**
* @brief Runs the full load-time validation sequence against a module
*
@@ -80,5 +79,4 @@ namespace dpm_core {
std::unique_ptr<dpm_module> validate_and_load(dpm_ctx* ctx,
const std::string& name,
std::string& reason);
} // namespace dpm_core

View File

@@ -23,7 +23,6 @@
#pragma once
namespace dpm_core {
/**
* @brief Parses a strict X.Y.Z version string
*
@@ -32,5 +31,4 @@ namespace dpm_core {
* @return true on success; false on any malformation
*/
bool parse_version(const char* s, long out[3]);
} // namespace dpm_core

View File

@@ -36,16 +36,14 @@
/**
* @brief Returns the module's own version
*/
extern "C" const char* dpm_module_version(void)
{
extern "C" const char* dpm_module_version(void) {
return INFO_MODULE_VERSION;
}
/**
* @brief Returns the module's one-line description
*/
extern "C" const char* dpm_module_description(void)
{
extern "C" const char* dpm_module_description(void) {
return "Reports and tests libdpm-core functionality.";
}
@@ -62,8 +60,7 @@ extern "C" const char* dpm_module_description(void)
* @return 0 on success, non-zero on failure
*/
extern "C" int dpm_module_execute(dpm_ctx* ctx, const char* command,
int argc, char** argv)
{
int argc, char** argv) {
(void)argc;
(void)argv;

View File

@@ -33,9 +33,7 @@
#define INFO_VERSION "0.1.0"
namespace {
std::string detect_architecture()
{
std::string detect_architecture() {
struct utsname system_info;
if (uname(&system_info) == -1) {
return "Unknown";
@@ -43,8 +41,7 @@ std::string detect_architecture()
return system_info.machine;
}
std::string detect_os()
{
std::string detect_os() {
struct utsname system_info;
if (uname(&system_info) == -1) {
return "Unknown";
@@ -93,11 +90,9 @@ std::string detect_os()
return os;
}
} // namespace
Command parse_command(const char* cmd_str)
{
Command parse_command(const char* cmd_str) {
if (cmd_str == nullptr || std::strlen(cmd_str) == 0) {
return CMD_HELP;
}
@@ -116,8 +111,7 @@ Command parse_command(const char* cmd_str)
return CMD_UNKNOWN;
}
int cmd_help(dpm_ctx* ctx)
{
int cmd_help(dpm_ctx* ctx) {
dpm_log(ctx, DPM_LOG_INFO, "DPM Info Module - Reports and tests libdpm-core functionality.");
dpm_log(ctx, DPM_LOG_INFO, "");
dpm_log(ctx, DPM_LOG_INFO, "Available commands:");
@@ -130,8 +124,7 @@ int cmd_help(dpm_ctx* ctx)
return 0;
}
int cmd_version(dpm_ctx* ctx)
{
int cmd_version(dpm_ctx* ctx) {
std::string core_msg = "libdpm-core Version: ";
core_msg += dpm_core_version();
dpm_log(ctx, DPM_LOG_INFO, core_msg.c_str());
@@ -143,8 +136,7 @@ int cmd_version(dpm_ctx* ctx)
return 0;
}
int cmd_system(dpm_ctx* ctx)
{
int cmd_system(dpm_ctx* ctx) {
dpm_log(ctx, DPM_LOG_INFO, "System Information:");
std::string os_msg = " OS: ";
@@ -158,8 +150,7 @@ int cmd_system(dpm_ctx* ctx)
return 0;
}
int cmd_config(dpm_ctx* ctx)
{
int cmd_config(dpm_ctx* ctx) {
dpm_log(ctx, DPM_LOG_INFO, "Configuration Information:");
const char* configured_path = dpm_config_get(ctx, "core", "modules", "path");
@@ -175,8 +166,7 @@ int cmd_config(dpm_ctx* ctx)
return 0;
}
int cmd_unknown(dpm_ctx* ctx, const char* command)
{
int cmd_unknown(dpm_ctx* ctx, const char* command) {
std::string msg = "Unknown command: ";
msg += (command ? command : "");
dpm_log(ctx, DPM_LOG_WARN, msg.c_str());

View File

@@ -33,12 +33,10 @@
#include <vector>
namespace {
/**
* @brief Prints the CLI usage message
*/
void show_help()
{
void show_help() {
std::printf(
"Usage: dpm [options] [module] [module args...]\n"
"\n"
@@ -59,8 +57,7 @@ void show_help()
* @param name The level name (case-insensitive)
* @return The DPM_LOG_* level, or -1 if unrecognized
*/
int level_from_name(const char* name)
{
int level_from_name(const char* name) {
if (strcasecmp(name, "FATAL") == 0) { return DPM_LOG_FATAL; }
if (strcasecmp(name, "ERROR") == 0) { return DPM_LOG_ERROR; }
if (strcasecmp(name, "WARN") == 0) { return DPM_LOG_WARN; }
@@ -83,8 +80,7 @@ int level_from_name(const char* name)
* @return true if the argument is this option
*/
bool option_matches(const char* arg, const char* short_form,
const char* long_form, const char** inline_value)
{
const char* long_form, const char** inline_value) {
if (std::strcmp(arg, short_form) == 0 ||
std::strcmp(arg, long_form) == 0) {
*inline_value = nullptr;
@@ -106,8 +102,7 @@ bool option_matches(const char* arg, const char* short_form,
* @param ctx The libdpm-core context
* @return 0 on success, 1 on failure
*/
int list_modules(dpm_ctx* ctx)
{
int list_modules(dpm_ctx* ctx) {
dpm_cursor* cur = dpm_list_modules(ctx);
if (!cur) {
const char* err = dpm_last_error(ctx);
@@ -149,11 +144,9 @@ int list_modules(dpm_ctx* ctx)
return 0;
}
} // namespace
int main(int argc, char** argv)
{
int main(int argc, char** argv) {
dpm_open_overrides overrides = {nullptr, nullptr, nullptr, -1};
bool list = false;

View File

@@ -33,13 +33,11 @@
namespace fs = std::filesystem;
namespace {
const char* DEFAULT_CONFIG_DIR = "/etc/dpm/conf.d/";
const char* DEFAULT_MODULE_PATH = "/usr/lib/dpm/modules/";
const char* DEFAULT_LOG_FILE = "/var/log/dpm/dpm.log";
std::string trim(const std::string& s)
{
std::string trim(const std::string& s) {
const char* ws = " \t\r\n\f\v";
size_t start = s.find_first_not_of(ws);
if (start == std::string::npos) {
@@ -49,16 +47,14 @@ std::string trim(const std::string& s)
return s.substr(start, end - start + 1);
}
std::string with_trailing_slash(std::string s)
{
std::string with_trailing_slash(std::string s) {
if (!s.empty() && s.back() != '/') {
s += '/';
}
return s;
}
bool parse_bool(const std::string& v, bool fallback)
{
bool parse_bool(const std::string& v, bool fallback) {
std::string lower;
for (char c : v) {
lower += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
@@ -72,8 +68,7 @@ bool parse_bool(const std::string& v, bool fallback)
return fallback;
}
int level_from_string(const std::string& v, int fallback)
{
int level_from_string(const std::string& v, int fallback) {
if (v == "FATAL") { return DPM_LOG_FATAL; }
if (v == "ERROR") { return DPM_LOG_ERROR; }
if (v == "WARN") { return DPM_LOG_WARN; }
@@ -82,8 +77,7 @@ int level_from_string(const std::string& v, int fallback)
return fallback;
}
const char* level_name(int level)
{
const char* level_name(int level) {
switch (level) {
case DPM_LOG_FATAL: return "FATAL";
case DPM_LOG_ERROR: return "ERROR";
@@ -95,8 +89,7 @@ const char* level_name(int level)
}
void parse_config_file(dpm_ctx* ctx, const fs::path& file,
const std::string& module_name)
{
const std::string& module_name) {
std::ifstream in(file);
if (!in.is_open()) {
return;
@@ -132,20 +125,16 @@ void parse_config_file(dpm_ctx* ctx, const fs::path& file,
ctx->config[module_name][section][key] = value;
}
}
} // namespace
namespace dpm_core {
void set_error(dpm_ctx* ctx, const std::string& msg)
{
void set_error(dpm_ctx* ctx, const std::string& msg) {
if (ctx) {
ctx->last_error = msg;
}
}
void load_config_dir(dpm_ctx* ctx)
{
void load_config_dir(dpm_ctx* ctx) {
std::error_code ec;
if (!fs::is_directory(ctx->config_dir, ec)) {
return;
@@ -164,13 +153,10 @@ void load_config_dir(dpm_ctx* ctx)
parse_config_file(ctx, entry.path(), entry.path().stem().string());
}
}
} // namespace dpm_core
extern "C" {
dpm_ctx* dpm_open(const dpm_open_overrides* overrides)
{
dpm_ctx* dpm_open(const dpm_open_overrides* overrides) {
/* Validate explicit overrides before allocating anything. */
if (overrides) {
if (overrides->config_dir && *overrides->config_dir) {
@@ -233,8 +219,7 @@ dpm_ctx* dpm_open(const dpm_open_overrides* overrides)
return ctx;
}
void dpm_close(dpm_ctx* ctx)
{
void dpm_close(dpm_ctx* ctx) {
if (!ctx) {
return;
}
@@ -249,8 +234,7 @@ void dpm_close(dpm_ctx* ctx)
}
const char* dpm_config_get(dpm_ctx* ctx, const char* module,
const char* section, const char* key)
{
const char* section, const char* key) {
if (!ctx || !module || !section || !key) {
return nullptr;
}
@@ -273,8 +257,7 @@ const char* dpm_config_get(dpm_ctx* ctx, const char* module,
return key_it->second.c_str();
}
void dpm_log(dpm_ctx* ctx, int level, const char* message)
{
void dpm_log(dpm_ctx* ctx, int level, const char* message) {
if (!ctx || !message) {
return;
}
@@ -308,20 +291,17 @@ void dpm_log(dpm_ctx* ctx, int level, const char* message)
}
}
const char* dpm_module_path(dpm_ctx* ctx)
{
const char* dpm_module_path(dpm_ctx* ctx) {
if (!ctx) {
return nullptr;
}
return ctx->module_path.c_str();
}
const char* dpm_last_error(dpm_ctx* ctx)
{
const char* dpm_last_error(dpm_ctx* ctx) {
if (!ctx || ctx->last_error.empty()) {
return nullptr;
}
return ctx->last_error.c_str();
}
} /* extern "C" */

View File

@@ -41,21 +41,18 @@ namespace fs = std::filesystem;
/* dlclose wrapper referenced from context.cpp so handle release stays
in one translation unit with the loader. */
void dpm_internal_unload(void* handle)
{
void dpm_internal_unload(void* handle) {
if (handle) {
dlclose(handle);
}
}
namespace {
using execute_fn = int (*)(dpm_ctx*, const char*, int, char**);
using string_fn = const char* (*)(void);
/** dlsym with dlerror() discipline; returns nullptr on any error. */
void* resolve(void* handle, const char* symbol)
{
void* resolve(void* handle, const char* symbol) {
dlerror();
void* addr = dlsym(handle, symbol);
if (dlerror() != nullptr) {
@@ -63,15 +60,12 @@ void* resolve(void* handle, const char* symbol)
}
return addr;
}
} // namespace
namespace dpm_core {
std::unique_ptr<dpm_module> validate_and_load(dpm_ctx* ctx,
const std::string& name,
std::string& reason)
{
std::string& reason) {
std::string so_path = ctx->module_path + name + ".so";
std::error_code ec;
@@ -136,13 +130,10 @@ std::unique_ptr<dpm_module> validate_and_load(dpm_ctx* ctx,
mod->execute = exec_f;
return mod;
}
} // namespace dpm_core
extern "C" {
dpm_module* dpm_require(dpm_ctx* ctx, const char* name)
{
dpm_module* dpm_require(dpm_ctx* ctx, const char* name) {
if (!ctx || !name || !*name) {
return nullptr;
}
@@ -165,8 +156,7 @@ dpm_module* dpm_require(dpm_ctx* ctx, const char* name)
return mod;
}
int dpm_module_info_of(dpm_ctx* ctx, dpm_module* mod, dpm_module_info* out)
{
int dpm_module_info_of(dpm_ctx* ctx, dpm_module* mod, dpm_module_info* out) {
if (!ctx || !mod || !out) {
return 1;
}
@@ -178,16 +168,14 @@ int dpm_module_info_of(dpm_ctx* ctx, dpm_module* mod, dpm_module_info* out)
}
int dpm_execute(dpm_ctx* ctx, dpm_module* mod, const char* command,
int argc, char** argv)
{
int argc, char** argv) {
if (!ctx || !mod || !mod->execute) {
return 1;
}
return mod->execute(ctx, command, argc, argv);
}
dpm_cursor* dpm_list_modules(dpm_ctx* ctx)
{
dpm_cursor* dpm_list_modules(dpm_ctx* ctx) {
if (!ctx) {
return nullptr;
}
@@ -247,8 +235,7 @@ dpm_cursor* dpm_list_modules(dpm_ctx* ctx)
return cur;
}
int dpm_cursor_next(dpm_cursor* cur, dpm_module_info* out)
{
int dpm_cursor_next(dpm_cursor* cur, dpm_module_info* out) {
if (!cur || !out || cur->idx >= cur->infos.size()) {
return 1;
}
@@ -257,9 +244,7 @@ int dpm_cursor_next(dpm_cursor* cur, dpm_module_info* out)
return 0;
}
void dpm_cursor_free(dpm_cursor* cur)
{
void dpm_cursor_free(dpm_cursor* cur) {
delete cur;
}
} /* extern "C" */

View File

@@ -30,15 +30,12 @@
/** libdpm-core.so's own version. */
#define DPM_CORE_VERSION_STR "1.0.0"
extern "C" const char* dpm_core_version(void)
{
extern "C" const char* dpm_core_version(void) {
return DPM_CORE_VERSION_STR;
}
namespace dpm_core {
bool parse_version(const char* s, long out[3])
{
bool parse_version(const char* s, long out[3]) {
if (!s || !*s) {
return false;
}
@@ -69,5 +66,4 @@ bool parse_version(const char* s, long out[3])
return true;
}
} // namespace dpm_core

View File

@@ -20,19 +20,16 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
extern "C" const char* dpm_module_version(void)
{
extern "C" const char* dpm_module_version(void) {
return "banana";
}
extern "C" const char* dpm_module_description(void)
{
extern "C" const char* dpm_module_description(void) {
return "Fixture with a malformed version.";
}
extern "C" int dpm_module_execute(void* ctx, const char* command,
int argc, char** argv)
{
int argc, char** argv) {
(void)ctx;
(void)command;
(void)argc;

View File

@@ -24,21 +24,18 @@
*/
#include <cstring>
extern "C" const char* dpm_module_version(void)
{
extern "C" const char* dpm_module_version(void) {
return "1.2.3";
}
extern "C" const char* dpm_module_description(void)
{
extern "C" const char* dpm_module_description(void) {
return "Known-good stub module.";
}
/* Answers "ping" with 42 so a dispatch round trip is observable, and
0 for anything else. */
extern "C" int dpm_module_execute(void* ctx, const char* command,
int argc, char** argv)
{
int argc, char** argv) {
(void)ctx;
(void)argc;
(void)argv;

View File

@@ -21,12 +21,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
extern "C" const char* dpm_module_version(void)
{
extern "C" const char* dpm_module_version(void) {
return "1.0.0";
}
extern "C" const char* dpm_module_description(void)
{
extern "C" const char* dpm_module_description(void) {
return "Fixture missing most of the contract.";
}

View File

@@ -50,14 +50,12 @@ static int g_checks = 0;
} \
} while (0)
static bool error_contains(dpm_ctx* ctx, const char* needle)
{
static bool error_contains(dpm_ctx* ctx, const char* needle) {
const char* err = dpm_last_error(ctx);
return err != nullptr && std::strstr(err, needle) != nullptr;
}
int main(void)
{
int main(void) {
/* ---- dpm_open: invalid explicit override refuses ---- */
{
dpm_open_overrides bad = {"/does/not/exist/conf", nullptr, nullptr, -1};