Permalink
Browse files

Macros: Add new macro function `ELEKTRA_STRINGIFY`

  • Loading branch information...
sanssecours committed Oct 2, 2017
1 parent 76274ec commit 4adbe7ede9286fd4147e3fba5af4842455781a2a
View
@@ -7,14 +7,12 @@
*/
#include <kdb.h>
#include <kdbmacros.h> // Declares `ELEKTRA_STRINGIFY`
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define XSTR(s) STR (s)
#define STR(s) #s
void printError (Key * key);
void printWarnings (Key * key);
void removeMetaData (Key * key, const char * searchfor);
@@ -93,7 +91,7 @@ void printWarnings (Key * key)
}
size_t warn_iter = 0;
char buffer[sizeof ("warnings/#00/description") + sizeof (XSTR (SIZE_MAX))];
char buffer[sizeof ("warnings/#00/description") + sizeof (ELEKTRA_STRINGIFY (SIZE_MAX))];
do
{
View
@@ -33,13 +33,9 @@ ostream & operator<< (ostream & os, parse_t & p)
<< "#include <kdb.h>" << endl
<< "#include <kdbhelper.h>" << endl
<< "#include <kdblogger.h>" << endl
<< "#include <kdbmacros.h>" << endl
<< "#include <string.h>" << endl
<< endl
<< "#ifndef STRINGIFY" << endl
<< "#define STRINGIFY(x) STRINGIFY2 (x)" << endl
<< "#define STRINGIFY2(x) #x" << endl
<< "#endif" << endl
<< endl
<< "#define ELEKTRA_SET_ERROR(number, key, text) ELEKTRA_SET_ERROR_HELPER\\" << endl
<< " (number, key, text, __FILE__, __LINE__)" << endl
<< endl
@@ -71,7 +67,7 @@ ostream & operator<< (ostream & os, parse_t & p)
<< " (number, key, text, file, line, __VA_ARGS__)" << endl
<< endl
<< "#define ELEKTRA_SET_ERRORF_HELPER_HELPER(number, key, text, file, line, ...) do {ELEKTRA_LOG (\"Add Error \" "
"STRINGIFY(number) \" : \" text, __VA_ARGS__); elektraSetErrorf ## number\\"
"ELEKTRA_STRINGIFY(number) \" : \" text, __VA_ARGS__); elektraSetErrorf ## number\\"
<< endl
<< " (key, text, file, #line, __VA_ARGS__); } while (0)" << endl
<< endl
@@ -83,7 +79,7 @@ ostream & operator<< (ostream & os, parse_t & p)
<< " (number, key, text, file, line, __VA_ARGS__)" << endl
<< "" << endl
<< "#define ELEKTRA_ADD_WARNINGF_HELPER_HELPER(number, key, text, file, line, ...) do {ELEKTRA_LOG (\"Add Warning \" "
"STRINGIFY(number) \" : \" text, __VA_ARGS__); elektraAddWarningf ## number\\"
"ELEKTRA_STRINGIFY(number) \" : \" text, __VA_ARGS__); elektraAddWarningf ## number\\"
<< endl
<< " (key, text, file, #line, __VA_ARGS__); } while (0)" << endl
<< endl
View
@@ -7,6 +7,7 @@
*/
#include <kdbconfig.h>
#include <kdbmacros.h>
#ifdef __cplusplus
extern "C" {
@@ -28,18 +29,13 @@ void elektraAbort (const char * expression, const char * function, const char *
}
#endif
#ifndef STRINGIFY
#define STRINGIFY(x) STRINGIFY2 (x)
#define STRINGIFY2(x) #x
#endif
#ifdef ELEKTRA_BMC
#undef NDEBUG
#include <assert.h>
#define ELEKTRA_ASSERT(EXPR, ...) assert (EXPR)
#else
#if DEBUG
#define ELEKTRA_ASSERT(EXPR, ...) ((EXPR)) ? (void)(0) : elektraAbort (STRINGIFY (EXPR), __func__, __FILE__, __LINE__, __VA_ARGS__)
#define ELEKTRA_ASSERT(EXPR, ...) ((EXPR)) ? (void)(0) : elektraAbort (ELEKTRA_STRINGIFY (EXPR), __func__, __FILE__, __LINE__, __VA_ARGS__)
#else
#define ELEKTRA_ASSERT(EXPR, ...)
#endif
View
@@ -13,6 +13,7 @@
#define KDBMACROS_H
#define ELEKTRA_QUOTE(x) #x
#define ELEKTRA_STRINGIFY(x) ELEKTRA_QUOTE (x)
#if defined(__APPLE__)
#define ELEKTRA_STAT_SECONDS(status) status.st_mtime
@@ -15,9 +15,6 @@
#include <kdberrors.h>
#define STRINGIFY(x) STRINGIFY2 (x)
#define STRINGIFY2(x) #x
int elektraConstantsGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED);
int elektraConstantsSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED);
@@ -259,31 +256,31 @@ static void elektraAddConstants (KeySet * ks, Key * parentKey)
key = keyDup (parentKey);
keyAddName (key, "macros/KDB_MAX_PATH_LENGTH");
keySetString (key, STRINGIFY (KDB_MAX_PATH_LENGTH));
keySetString (key, ELEKTRA_STRINGIFY (KDB_MAX_PATH_LENGTH));
ksAppendKey (ks, key);
key = keyDup (parentKey);
keyAddName (key, "macros/KDB_FILE_MODE");
keySetString (key, STRINGIFY (KDB_FILE_MODE));
keySetString (key, ELEKTRA_STRINGIFY (KDB_FILE_MODE));
ksAppendKey (ks, key);
key = keyDup (parentKey);
keyAddName (key, "macros/KDB_DIR_MODE");
keySetString (key, STRINGIFY (KDB_DIR_MODE));
keySetString (key, ELEKTRA_STRINGIFY (KDB_DIR_MODE));
ksAppendKey (ks, key);
key = keyDup (parentKey);
keyAddName (key, "macros/KDB_PATH_SEPARATOR");
keySetString (key, STRINGIFY (KDB_PATH_SEPARATOR));
keySetString (key, ELEKTRA_STRINGIFY (KDB_PATH_SEPARATOR));
ksAppendKey (ks, key);
key = keyDup (parentKey);
keyAddName (key, "macros/KDB_PATH_ESCAPE");
keySetString (key, STRINGIFY (KDB_PATH_ESCAPE));
keySetString (key, ELEKTRA_STRINGIFY (KDB_PATH_ESCAPE));
ksAppendKey (ks, key);
@@ -6,9 +6,6 @@
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#define ELEKTRA_CRYPTO_CONTRACT_XSTR_VAL(s) #s
#define ELEKTRA_CRYPTO_CONTRACT_STR_VAL(s) ELEKTRA_CRYPTO_CONTRACT_XSTR_VAL (s)
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME, KEY_VALUE, "crypto plugin waits for your orders", KEY_END),
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports", KEY_END),
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/exports/open", KEY_FUNC, CRYPTO_PLUGIN_FUNCTION (open), KEY_END),
@@ -21,10 +18,10 @@ keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME, KEY_VALUE, "crypto plugin
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/constants/PAYLOAD_VERSION", KEY_VALUE, ELEKTRA_CRYPTO_PAYLOAD_VERSION,
KEY_END),
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/constants/masterpasswordlength", KEY_VALUE,
ELEKTRA_CRYPTO_CONTRACT_STR_VAL (ELEKTRA_CRYPTO_DEFAULT_MASTER_PWD_LENGTH), KEY_END),
ELEKTRA_STRINGIFY (ELEKTRA_CRYPTO_DEFAULT_MASTER_PWD_LENGTH), KEY_END),
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/constants/iterations", KEY_VALUE,
ELEKTRA_CRYPTO_CONTRACT_STR_VAL (ELEKTRA_CRYPTO_DEFAULT_ITERATION_COUNT), KEY_END),
ELEKTRA_STRINGIFY (ELEKTRA_CRYPTO_DEFAULT_ITERATION_COUNT), KEY_END),
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/constants/saltlength", KEY_VALUE,
ELEKTRA_CRYPTO_CONTRACT_STR_VAL (ELEKTRA_CRYPTO_DEFAULT_SALT_LEN), KEY_END),
ELEKTRA_STRINGIFY (ELEKTRA_CRYPTO_DEFAULT_SALT_LEN), KEY_END),
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/constants/algorithm", KEY_VALUE, "AES256", KEY_END),
keyNew ("system/elektra/modules/" ELEKTRA_PLUGIN_NAME "/constants/mode", KEY_VALUE, "CBC", KEY_END),
@@ -24,8 +24,6 @@
#define MIN_VALID_STACK 3
#define EPSILON 0.00001
#define str(s) #s
#define xstr(s) str (s)
typedef enum {
ERROR = 0,
@@ -65,7 +63,9 @@ int elektraMathcheckGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKT
#include ELEKTRA_README (mathcheck)
keyNew ("system/elektra/modules/mathcheck/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END),
keyNew ("system/elektra/modules/mathcheck/export/constants", KEY_END),
keyNew ("system/elektra/modules/mathcheck/export/constants/EPSILON", KEY_VALUE, xstr (EPSILON), KEY_END), KS_END);
keyNew ("system/elektra/modules/mathcheck/export/constants/EPSILON", KEY_VALUE, ELEKTRA_STRINGIFY (EPSILON),
KEY_END),
KS_END);
ksAppend (returned, contract);
ksDel (contract);
@@ -6,15 +6,13 @@
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <kdbmacros.h>
#include <stdlib.h>
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#define STRINGIFY(x) STRINGIFY2 (x)
#define STRINGIFY2(x) #x
#include <tests_plugin.h>
static void init_env (void)
@@ -30,7 +28,7 @@ static char * python_file (const char * filename)
if (!srcdir_rewrite)
{
/* no rewrite. just append our plugin name */
strcpy (filebuf, STRINGIFY (PYTHON_PLUGIN_NAME));
strcpy (filebuf, ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
strcat (strcat (filebuf, "/"), filename);
return srcdir_file (filebuf);
}
@@ -39,7 +37,7 @@ static char * python_file (const char * filename)
*srcdir_rewrite = '\0';
/* append plugin name and delete last character */
strcat (strcat (filebuf, "/"), STRINGIFY (PYTHON_PLUGIN_NAME));
strcat (strcat (filebuf, "/"), ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
*(filebuf + strlen (filebuf) - 1) = '\0';
strcat (strcat (filebuf, "/"), filename);
@@ -53,7 +51,7 @@ static void test_variable_passing (void)
KeySet * conf = ksNew (1, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin.py"), KEY_END),
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/print", KEY_END), KS_END);
PLUGIN_OPEN (STRINGIFY (PYTHON_PLUGIN_NAME));
PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
Key * parentKey = keyNew ("user/from_c", KEY_END);
KeySet * ks = ksNew (0, KS_END);
@@ -82,14 +80,14 @@ static void test_two_scripts (void)
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/print", KEY_END), KS_END);
Key * errorKey = keyNew ("", KEY_END);
Plugin * plugin = elektraPluginOpen (STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
succeed_if (output_warnings (errorKey), "warnings in kdbOpen");
succeed_if (output_error (errorKey), "errors in kdbOpen");
exit_if_fail (plugin != NULL, "unable to load python plugin");
keyDel (errorKey);
Key * errorKey2 = keyNew ("", KEY_END);
Plugin * plugin2 = elektraPluginOpen (STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf2, errorKey2);
Plugin * plugin2 = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf2, errorKey2);
succeed_if (output_warnings (errorKey2), "warnings in kdbOpen");
succeed_if (output_error (errorKey2), "errors in kdbOpen");
exit_if_fail (plugin2 != NULL, "unable to load python plugin again");
@@ -108,7 +106,7 @@ static void test_fail (void)
KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin_fail.py"), KEY_END),
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/print", KEY_END), KS_END);
PLUGIN_OPEN (STRINGIFY (PYTHON_PLUGIN_NAME));
PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
Key * parentKey = keyNew ("user/tests/from_c", KEY_END);
KeySet * ks = ksNew (0, KS_END);
@@ -135,7 +133,7 @@ static void test_wrong (void)
keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/print", KEY_END), KS_END);
Key * errorKey = keyNew ("", KEY_END);
Plugin * plugin = elektraPluginOpen (STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
succeed_if (!output_warnings (errorKey), "we expect some warnings");
succeed_if (!output_error (errorKey), "we expect some errors");
succeed_if (plugin == NULL, "python plugin shouldn't be loadable");

0 comments on commit 4adbe7e

Please sign in to comment.