diff --git a/source/cli/plugins/cli_core_plugin/source/cli_core_plugin.cpp b/source/cli/plugins/cli_core_plugin/source/cli_core_plugin.cpp index 00e7f665a..a308fce68 100644 --- a/source/cli/plugins/cli_core_plugin/source/cli_core_plugin.cpp +++ b/source/cli/plugins/cli_core_plugin/source/cli_core_plugin.cpp @@ -20,8 +20,7 @@ #include -#include -#include +#include #include @@ -320,59 +319,12 @@ int cli_core_plugin(void *loader, void *handle, void *context) { (void)handle; - { - enum metacall_value_id *arg_types = NULL; - if (metacall_register_loaderv(loader, context, "inspect", inspect, METACALL_STRING, 0, arg_types) != 0) - { - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: inspect"); - return 1; - } - } - - { - enum metacall_value_id arg_types[] = { METACALL_STRING, METACALL_STRING }; - if (metacall_register_loaderv(loader, context, "clear", clear, METACALL_INT, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0) - { - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: clear"); - return 1; - } - } - - { - enum metacall_value_id arg_types[] = { METACALL_STRING }; - if (metacall_register_loaderv(loader, context, "call", call, METACALL_PTR, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0) - { - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: call"); - return 1; - } - } - - { - enum metacall_value_id arg_types[] = { METACALL_STRING }; - if (metacall_register_loaderv(loader, context, "await", await, METACALL_PTR, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0) - { - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: await"); - return 1; - } - } - - { - enum metacall_value_id arg_types[] = { METACALL_STRING, METACALL_STRING }; - if (metacall_register_loaderv(loader, context, "eval", eval, METACALL_INT, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0) - { - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: eval"); - return 1; - } - } - - { - enum metacall_value_id arg_types[] = { METACALL_STRING, METACALL_ARRAY }; - if (metacall_register_loaderv(loader, context, "load", load, METACALL_INT, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0) - { - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: load"); - return 1; - } - } + EXTENSION_FUNCTION(METACALL_STRING, inspect); + EXTENSION_FUNCTION(METACALL_INT, clear, METACALL_STRING, METACALL_STRING); + EXTENSION_FUNCTION(METACALL_PTR, call, METACALL_STRING); + EXTENSION_FUNCTION(METACALL_PTR, await, METACALL_STRING); + EXTENSION_FUNCTION(METACALL_INT, eval, METACALL_STRING, METACALL_STRING); + EXTENSION_FUNCTION(METACALL_INT, load, METACALL_STRING, METACALL_ARRAY); return 0; } diff --git a/source/plugin/include/plugin/plugin_interface.h b/source/plugin/include/plugin/plugin_interface.h new file mode 100644 index 000000000..0775d7265 --- /dev/null +++ b/source/plugin/include/plugin/plugin_interface.h @@ -0,0 +1,58 @@ +/* + * Plugin Library by Parra Studios + * A library for plugins at run-time into a process. + * + * Copyright (C) 2016 - 2022 Vicente Eduardo Ferrer Garcia + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef PLUGIN_INTERFACE_H +#define PLUGIN_INTERFACE_H 1 + +/* Private interface for using it inside any loader, extension or plugin for registering functions */ + +#include + +#include + +#include + +#define EXTENSION_FUNCTION_IMPL_VOID(ret, name) \ + do \ + { \ + if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, 0, NULL) != 0) \ + { \ + log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \ + return 1; \ + } \ + } while (0) + +#define EXTENSION_FUNCTION_IMPL(ret, name, ...) \ + do \ + { \ + enum metacall_value_id arg_types[] = { __VA_ARGS__ }; \ + if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, PREPROCESSOR_ARGS_COUNT(__VA_ARGS__), arg_types) != 0) \ + { \ + log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \ + return 1; \ + } \ + } while (0) + +#define EXTENSION_FUNCTION(ret, name, ...) \ + PREPROCESSOR_IF(PREPROCESSOR_ARGS_EMPTY(__VA_ARGS__), \ + EXTENSION_FUNCTION_IMPL_VOID(ret, name), \ + EXTENSION_FUNCTION_IMPL(ret, name, __VA_ARGS__)) + +#endif /* PLUGIN_INTERFACE_H */ diff --git a/source/plugins/sandbox_plugin/source/sandbox_plugin.cpp b/source/plugins/sandbox_plugin/source/sandbox_plugin.cpp index 3251935ea..fa70a89d4 100644 --- a/source/plugins/sandbox_plugin/source/sandbox_plugin.cpp +++ b/source/plugins/sandbox_plugin/source/sandbox_plugin.cpp @@ -20,13 +20,9 @@ #include -#include - -#include +#include -#include - -#include +#include /* TODO: Use SCMP_ACT_KILL_PROCESS for catching the signal and showing the stack trace? */ #define SANDBOX_DEFAULT_ACTION SCMP_ACT_ALLOW // SCMP_ACT_KILL @@ -148,39 +144,9 @@ int sandbox_plugin(void *loader, void *handle, void *context) { (void)handle; -#define SANDBOX_FUNCTION_IMPL_0(ret, name) \ - do \ - { \ - if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, 0, NULL) != 0) \ - { \ - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \ - return 1; \ - } \ - } while (0) - -#define SANDBOX_FUNCTION_IMPL(ret, name, ...) \ - do \ - { \ - enum metacall_value_id arg_types[] = { __VA_ARGS__ }; \ - if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, PREPROCESSOR_ARGS_COUNT(__VA_ARGS__), arg_types) != 0) \ - { \ - log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \ - return 1; \ - } \ - } while (0) - -#define SANDBOX_FUNCTION(ret, name, ...) \ - PREPROCESSOR_IF(PREPROCESSOR_ARGS_EMPTY(__VA_ARGS__), \ - SANDBOX_FUNCTION_IMPL_0(ret, PREPROCESSOR_CONCAT(sandbox_, name)), \ - SANDBOX_FUNCTION_IMPL(ret, PREPROCESSOR_CONCAT(sandbox_, name), __VA_ARGS__)) - - SANDBOX_FUNCTION(METACALL_PTR, initialize); - SANDBOX_FUNCTION_IMPL(METACALL_INT, sandbox_uname, METACALL_PTR, METACALL_BOOL); - SANDBOX_FUNCTION_IMPL(METACALL_INT, sandbox_destroy, METACALL_PTR); - -#undef SANDBOX_FUNCTION_IMPL_0 -#undef SANDBOX_FUNCTION_IMPL -#undef SANDBOX_FUNCTION + EXTENSION_FUNCTION(METACALL_PTR, sandbox_initialize); + EXTENSION_FUNCTION(METACALL_INT, sandbox_uname, METACALL_PTR, METACALL_BOOL); + EXTENSION_FUNCTION(METACALL_INT, sandbox_destroy, METACALL_PTR); #if 0 /* TODO: Fork safety */ #ifdef METACALL_FORK_SAFE diff --git a/source/preprocessor/include/preprocessor/preprocessor_if.h b/source/preprocessor/include/preprocessor/preprocessor_if.h index df0cf3759..8e31e054c 100644 --- a/source/preprocessor/include/preprocessor/preprocessor_if.h +++ b/source/preprocessor/include/preprocessor/preprocessor_if.h @@ -21,8 +21,12 @@ extern "C" { /* -- Macros -- */ -#define PREPROCESSOR_IIF_IMPL_0(true_expr, false_expr) false_expr -#define PREPROCESSOR_IIF_IMPL_1(true_expr, false_expr) true_expr +#define PREPROCESSOR_IIF_IMPL_EXPAND_ARGS(...) __VA_ARGS__ +#define PREPROCESSOR_IIF_IMPL_EXPAND(expr) expr +#define PREPROCESSOR_IIF_IMPL_EXPAND_REMOVE_PARENTHESIS(expr) PREPROCESSOR_IIF_IMPL_EXPAND(PREPROCESSOR_IIF_IMPL_EXPAND_ARGS expr) + +#define PREPROCESSOR_IIF_IMPL_0(true_expr, false_expr) PREPROCESSOR_IIF_IMPL_EXPAND_REMOVE_PARENTHESIS(false_expr) +#define PREPROCESSOR_IIF_IMPL_1(true_expr, false_expr) PREPROCESSOR_IIF_IMPL_EXPAND_REMOVE_PARENTHESIS(true_expr) #if defined(_MSC_VER) && !defined(__clang__) #define PREPROCESSOR_IIF_IMPL_I(expr) expr @@ -40,9 +44,9 @@ extern "C" { #if defined(__EDG__) || defined(__EDG_VERSION__) #define PREPROCESSOR_IF_IMPL(condition, true_expr, false_expr) PREPROCESSOR_IIF(PREPROCESSOR_BOOL(condition), true_expr, false_expr) - #define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IF_IMPL(condition, true_expr, false_expr) + #define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IF_IMPL(condition, (true_expr), (false_expr)) #else - #define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IIF(PREPROCESSOR_BOOL(condition), true_expr, false_expr) + #define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IIF(PREPROCESSOR_BOOL(condition), (true_expr), (false_expr)) #endif #ifdef __cplusplus