mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-08-15 13:27:53 +00:00
lib: make frrscript_call encode args based on type
Signed-off-by: Donald Lee <dlqs@gmx.com>
This commit is contained in:
parent
249cd5daa7
commit
43a5106d42
@ -104,24 +104,8 @@ static void codec_free(struct codec *c)
|
|||||||
|
|
||||||
/* Generic script APIs */
|
/* Generic script APIs */
|
||||||
|
|
||||||
int frrscript_call(struct frrscript *fs, struct frrscript_env *env)
|
int _frrscript_call(struct frrscript *fs)
|
||||||
{
|
{
|
||||||
struct frrscript_codec c = {};
|
|
||||||
const void *arg;
|
|
||||||
const char *bindname;
|
|
||||||
|
|
||||||
/* Encode script arguments */
|
|
||||||
for (int i = 0; env && env[i].val != NULL; i++) {
|
|
||||||
bindname = env[i].name;
|
|
||||||
c.typename = env[i].typename;
|
|
||||||
arg = env[i].val;
|
|
||||||
|
|
||||||
struct frrscript_codec *codec = hash_lookup(codec_hash, &c);
|
|
||||||
assert(codec && "No encoder for type");
|
|
||||||
codec->encoder(fs->L, arg);
|
|
||||||
|
|
||||||
lua_setglobal(fs->L, bindname);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ret = lua_pcall(fs->L, 0, 0, 0);
|
int ret = lua_pcall(fs->L, 0, 0, 0);
|
||||||
|
|
||||||
|
@ -96,6 +96,21 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs);
|
|||||||
*/
|
*/
|
||||||
void frrscript_init(const char *scriptdir);
|
void frrscript_init(const char *scriptdir);
|
||||||
|
|
||||||
|
#define ENCODE_ARGS(name, value) \
|
||||||
|
do { \
|
||||||
|
ENCODE_ARGS_WITH_STATE(L, value) \
|
||||||
|
lua_setglobal(L, name); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define DECODE_ARGS(name, value) \
|
||||||
|
do { \
|
||||||
|
lua_getglobal(L, name); \
|
||||||
|
DECODE_ARGS_WITH_STATE(L, value) \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define ENCODE_ARGS_WITH_STATE(L, value) _Generic((value), )(L, value);
|
||||||
|
|
||||||
|
#define DECODE_ARGS_WITH_STATE(L, value) _Generic((value), )(L, value);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Call script.
|
* Call script.
|
||||||
@ -109,8 +124,18 @@ void frrscript_init(const char *scriptdir);
|
|||||||
* Returns:
|
* Returns:
|
||||||
* 0 if the script ran successfully, nonzero otherwise.
|
* 0 if the script ran successfully, nonzero otherwise.
|
||||||
*/
|
*/
|
||||||
int frrscript_call(struct frrscript *fs, struct frrscript_env *env);
|
int _frrscript_call(struct frrscript *fs);
|
||||||
|
|
||||||
|
#define frrscript_call(fs, ...) \
|
||||||
|
({ \
|
||||||
|
lua_State *L = fs->L; \
|
||||||
|
MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \
|
||||||
|
int ret = _frrscript_call(fs); \
|
||||||
|
if (ret == 0) { \
|
||||||
|
MAP_LISTS(DECODE_ARGS, ##__VA_ARGS__); \
|
||||||
|
} \
|
||||||
|
ret; \
|
||||||
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get result from finished script.
|
* Get result from finished script.
|
||||||
|
Loading…
Reference in New Issue
Block a user