From 12cb4d20ecd9aa80c19299f0b8daaf10da7c9ccc Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Fri, 12 Jun 2009 22:46:04 +0800 Subject: [PATCH] Implement a robust ScriptCall and format the code. --- src/script.c | 74 +++++++++++++++++++++++++++++++++++++++++++++--------------- src/script.h | 2 +- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/script.c b/src/script.c index 663eddb..f567115 100644 --- a/src/script.c +++ b/src/script.c @@ -21,6 +21,7 @@ #include "config.h" #include "screen.h" +#include "extern.h" #include /*Binding structure & functions*/ @@ -70,7 +71,7 @@ ScriptSource(int argc, const char **argv) struct binding *binding = bindings; /* Parse the commandline options - * sourcescript [-async|-a] [-binding|-b ] script + * script source [-async|-a] [-binding|-b ] script */ while (*argv && **argv == '-') { @@ -81,38 +82,75 @@ ScriptSource(int argc, const char **argv) async = 1; /* check for (-b | -binding) */ else if ((arg[1] == 'b' && !arg[2]) - || strcmp(arg, "-binding") == 0) { + || strcmp(arg, "-binding") == 0) + { argv++; bd_select = *argv; - } + } argv++; - } + } script = *argv; - while (binding) { - if (!bd_select || strcmp(bd_select, binding->name) == 0) { + while (binding) + { + if (!bd_select || strcmp(bd_select, binding->name) == 0) + { /*dynamically initialize the binding*/ if (!binding->inited) - { - binding->bd_Init(); - binding->inited = 1; - } + { + binding->bd_Init(); + binding->inited = 1; + } /*and source the script*/ - if (ret = binding->bd_Source(script, async)) + ret = binding->bd_Source(script, async); + if (ret) break; - } + } binding = binding->b_next; - } + } if (!ret) - LMsg(1, "Could not source specified script %s", script); + LMsg(0, "Could not source specified script %s", script); } int -ScriptCall(const char *func, const char **argv) +ScriptCall(int argc, const char **argv) { - /*TODO*/ - return LuaCall(func, argv); + int ret = 0; + struct binding *binding = bindings; + const char *bd_select = 0, *func; + /* Parse the commandline options + * script call [-binding|-b ] function + */ + while (*argv && **argv == '-') + { + const char *arg = *argv; + /* check for (-b | -binding) */ + if ((arg[1] == 'b' && !arg[2]) + || strcmp(arg, "-binding") == 0) + { + argv++; + bd_select = *argv; + } + argv++; + } + func = *argv; + argv++; + + while (binding) + { + if (!bd_select || strcmp(bd_select, binding->name) == 0) + { + ret = binding->bd_call(func, argv); + if (ret) + break; + } + binding = binding->b_next; + } + + if (!ret) + LMsg(0, "Failed to run specified script coummand '%s'", func); + return ret; } void @@ -121,7 +159,7 @@ ScriptCmd(int argc, const char **argv) const char * sub = *argv; argv++;argc--; if (!strcmp(sub, "call")) - ScriptCall(*argv, argv+1); + ScriptCall(argc, argv); else if (!strcmp(sub, "source")) ScriptSource(argc, argv); } diff --git a/src/script.h b/src/script.h index 36e347c..6870d31 100644 --- a/src/script.h +++ b/src/script.h @@ -37,7 +37,7 @@ struct binding int registered; int (*bd_Init) __P((void)); int (*bd_Finit) __P((void)); - int (*bd_call) __P((char *func, char **argv)); + int (*bd_call) __P((const char *func, const char **argv)); /*Returns zero on failure, non zero on success*/ int (*bd_Source) __P((const char *, int)); /* The return value is significant: -- 2.11.4.GIT