From: Rui Guo Date: Fri, 12 Jun 2009 14:46:04 +0000 (+0800) Subject: Implement a robust ScriptCall and format the code. X-Git-Url: https://repo.or.cz/w/screen-lua.git/commitdiff_plain/6ca474d789d8b791f3e6164412948cdaefa8b3c3 Implement a robust ScriptCall and format the code. --- diff --git a/src/script.c b/src/script.c index 28e8afb..6f20bc4 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*/ @@ -78,7 +79,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 == '-') { @@ -89,38 +90,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 @@ -129,7 +167,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 c268eb5..dd37899 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)); struct binding *b_next;