summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRui Guo <firemeteor.guo@gmail.com>2009-06-12 22:46:04 +0800
committerSadrul Habib Chowdhury <sadrul@users.sourceforge.net>2009-06-22 01:59:19 -0400
commit6ca474d789d8b791f3e6164412948cdaefa8b3c3 (patch)
tree5d0ee7b619102ec6f106b5a5727b7b145965e05f
parent9faed3a12789f225f614059035b202cc1006de61 (diff)
downloadscreen-6ca474d789d8b791f3e6164412948cdaefa8b3c3.tar.gz
Implement a robust ScriptCall and format the code.
-rw-r--r--src/script.c74
-rw-r--r--src/script.h2
2 files changed, 57 insertions, 19 deletions
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 <stddef.h>
/*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 <binding>] script
+ * script source [-async|-a] [-binding|-b <binding>] 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 <binding>] 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;