From a71096fb0a327baf9400ec6df964bfb696b18cfb Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Sun, 4 Jul 2021 23:12:07 +0800 Subject: lib: Update Script command example to call function Signed-off-by: Donald Lee --- lib/command.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'lib/command.c') diff --git a/lib/command.c b/lib/command.c index 9dac60599c..b2b80daddd 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2428,18 +2428,20 @@ DEFUN(script, struct prefix p; (void)str2prefix("1.2.3.4/24", &p); - struct frrscript *fs = frrscript_load(argv[1]->arg, NULL); + struct frrscript *fs = frrscript_new(argv[1]->arg); if (fs == NULL) { vty_out(vty, "Script '/etc/frr/scripts/%s.lua' not found\n", argv[1]->arg); - } else { - int ret = frrscript_call(fs, ("p", &p)); - char buf[40]; - prefix2str(&p, buf, sizeof(buf)); - vty_out(vty, "p: %s\n", buf); - vty_out(vty, "Script result: %d\n", ret); } + if (frrscript_load(fs, argv[2]->arg, NULL)) + vty_out(vty, "Script function '/%s' not found\n", argv[2]->arg); + + int ret = frrscript_call(fs, argv[2]->arg, ("p", &p)); + char buf[40]; + prefix2str(&p, buf, sizeof(buf)); + vty_out(vty, "p: %s\n", buf); + vty_out(vty, "Script result: %d\n", ret); return CMD_SUCCESS; } -- cgit v1.2.3 From ad6e9b854d64aa91eef2d965b7778188d2d7811a Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Thu, 8 Jul 2021 01:32:30 +0800 Subject: test: Use frrscript_unload Signed-off-by: Donald Lee --- lib/command.c | 2 ++ tests/lib/test_frrscript.c | 2 ++ 2 files changed, 4 insertions(+) (limited to 'lib/command.c') diff --git a/lib/command.c b/lib/command.c index b2b80daddd..7808f57594 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2443,6 +2443,8 @@ DEFUN(script, vty_out(vty, "p: %s\n", buf); vty_out(vty, "Script result: %d\n", ret); + frrscript_unload(fs); + return CMD_SUCCESS; } #endif diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index e8b7927e15..399b950799 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -72,5 +72,7 @@ int main(int argc, char **argv) result = frrscript_call(fs, "bad_return3"); assert(result == 1); + frrscript_unload(fs); + return 0; } -- cgit v1.2.3 From 64d457d7ac22d643afb5638bacb0b9713274689e Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Thu, 8 Jul 2021 17:51:14 +0800 Subject: lib: Rename frrscript_unload to delete frrscript_load now loads a function instead of a file, so frrscript_unload should be renamed since it does not unload a function. Signed-off-by: Donald Lee --- bgpd/bgp_routemap.c | 2 +- lib/command.c | 2 +- lib/frrscript.c | 2 +- lib/frrscript.h | 2 +- tests/lib/test_frrscript.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/command.c') diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 0b22caaee7..c2062a5f60 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -434,7 +434,7 @@ route_match_script(void *rule, const struct prefix *prefix, void *object) break; } - frrscript_unload(fs); + frrscript_delete(fs); return status; } diff --git a/lib/command.c b/lib/command.c index 7808f57594..ceea186a9d 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2443,7 +2443,7 @@ DEFUN(script, vty_out(vty, "p: %s\n", buf); vty_out(vty, "Script result: %d\n", ret); - frrscript_unload(fs); + frrscript_delete(fs); return CMD_SUCCESS; } diff --git a/lib/frrscript.c b/lib/frrscript.c index 8252455fd9..b3f8a9f6f5 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -319,7 +319,7 @@ fail: return 1; } -void frrscript_unload(struct frrscript *fs) +void frrscript_delete(struct frrscript *fs) { hash_iterate(fs->lua_function_hash, lua_function_free, NULL); XFREE(MTYPE_SCRIPT, fs->name); diff --git a/lib/frrscript.h b/lib/frrscript.h index 1f234a38a9..14a922a741 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -89,7 +89,7 @@ int frrscript_load(struct frrscript *fs, const char *function_name, /* * Destroy FRR script. */ -void frrscript_unload(struct frrscript *fs); +void frrscript_delete(struct frrscript *fs); /* * Register a Lua codec for a type. diff --git a/tests/lib/test_frrscript.c b/tests/lib/test_frrscript.c index e01807f193..2d5746b587 100644 --- a/tests/lib/test_frrscript.c +++ b/tests/lib/test_frrscript.c @@ -98,7 +98,7 @@ int main(int argc, char **argv) result = frrscript_call(fs, "bad_return4"); assert(result == 1); - frrscript_unload(fs); + frrscript_delete(fs); return 0; } -- cgit v1.2.3 From 596b44af8a51c79a7e23447c8c45baf0d6638a63 Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Sun, 18 Jul 2021 06:25:15 +0800 Subject: lib: Remove warning about script not found Signed-off-by: Donald Lee --- lib/command.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'lib/command.c') diff --git a/lib/command.c b/lib/command.c index ceea186a9d..4da59c9dd6 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2430,12 +2430,11 @@ DEFUN(script, (void)str2prefix("1.2.3.4/24", &p); struct frrscript *fs = frrscript_new(argv[1]->arg); - if (fs == NULL) { - vty_out(vty, "Script '/etc/frr/scripts/%s.lua' not found\n", - argv[1]->arg); + if (frrscript_load(fs, argv[2]->arg, NULL)) { + vty_out(vty, + "/etc/frr/scripts%s.lua or function '/%s' not found\n", + argv[1]->arg, argv[2]->arg); } - if (frrscript_load(fs, argv[2]->arg, NULL)) - vty_out(vty, "Script function '/%s' not found\n", argv[2]->arg); int ret = frrscript_call(fs, argv[2]->arg, ("p", &p)); char buf[40]; -- cgit v1.2.3 From 8be973f5c29fdadd1a156fd60bcb66a0d58d695c Mon Sep 17 00:00:00 2001 From: Donald Lee Date: Sat, 24 Jul 2021 00:29:21 +0800 Subject: lib: Add function name to script command Signed-off-by: Donald Lee --- lib/command.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'lib/command.c') diff --git a/lib/command.c b/lib/command.c index 4da59c9dd6..122e189342 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2419,11 +2419,10 @@ DEFUN(find, } #if defined(DEV_BUILD) && defined(HAVE_SCRIPTING) -DEFUN(script, - script_cmd, - "script SCRIPT", - "Test command - execute a script\n" - "Script name (same as filename in /etc/frr/scripts/\n") +DEFUN(script, script_cmd, "script SCRIPT FUNCTION", + "Test command - execute a function in a script\n" + "Script name (same as filename in /etc/frr/scripts/)\n" + "Function name (in the script)\n") { struct prefix p; @@ -2432,7 +2431,7 @@ DEFUN(script, if (frrscript_load(fs, argv[2]->arg, NULL)) { vty_out(vty, - "/etc/frr/scripts%s.lua or function '/%s' not found\n", + "/etc/frr/scripts/%s.lua or function '%s' not found\n", argv[1]->arg, argv[2]->arg); } -- cgit v1.2.3