4293be476d28e6f69cd03fc96f1dba57900ced8d
[screen-lua.git] / src / script.c
blob4293be476d28e6f69cd03fc96f1dba57900ced8d
1 /* Copyright (c) 2008 Sadrul Habib Chowdhury (sadrul@users.sf.net)
2 * 2009 Rui Guo (firemeteor.guo@gmail.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3, or (at your option)
7 * any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program (see the file COPYING); if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 ****************************************************************
22 #include "config.h"
23 #include "screen.h"
25 struct binding *bindings = NULL;
27 static void
28 register_binding (struct binding *new_binding)
30 if (!new_binding->registered)
32 new_binding->b_next = bindings;
33 bindings = new_binding;
34 new_binding->registered = 1;
38 #ifdef LUA_BINDING
39 extern struct binding lua_binding;
40 #endif
42 void LoadBindings(void)
44 #ifdef LUA_BINDING
45 register_binding(&lua_binding);
46 #endif
49 void
50 FinalizeBindings (void)
52 struct binding *binding=bindings;
53 while(binding)
55 if (binding->inited)
56 binding->bd_Finit();
57 binding = binding->b_next;
61 void
62 ScriptSource(int argc, const char **argv)
64 int ret = 0;
65 int async = 0;
66 const char *bd_select = 0, *script;
67 struct binding *binding = bindings;
69 /* Parse the commandline options
70 * sourcescript [-async|-a] [-binding|-b <binding>] script
72 while (*argv && **argv == '-') {
73 // check for (-a | -async)
74 if ((*argv[1] == 'a' && !*argv[2])
75 || strcmp(*argv, "-async") == 0)
76 async = 1;
77 // check for (-b | -binding)
78 else if ((*argv[1] == 'b' && !*argv[2])
79 || strcmp(*argv, "-binding") == 0) {
80 argv++;
81 bd_select = *argv;
83 argv++;
85 script = *argv;
87 while (binding) {
88 if (!bd_select || strcmp(bd_select, binding->name) == 0) {
89 //dynamically initialize the binding
90 if (!binding->inited)
91 binding->bd_Init();
93 //and source the script
94 if (ret = binding->bd_Source(script, async))
95 break;
97 binding = binding->b_next;
99 if (!ret)
100 LMsg(1, "Could not source specified script %s", script);
103 #define ALL_SCRIPTS(fn, params, stop) do { \
104 struct binding *iter; \
105 for (iter = bindings; iter; iter = iter->b_next) \
107 if (iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
108 break; \
110 } while (0)
112 void ScriptForeWindowChanged(void)
114 int ret;
115 ALL_SCRIPTS(sf_ForeWindowChanged, (), 0);
118 int ScriptProcessCaption(const char *str, struct win *win, int len)
120 int ret = 0;
121 ALL_SCRIPTS(sf_ProcessCaption, (str, win, len), 1);
122 return ret;
125 int ScriptCommandExecuted(const char *command, const char **args, int argc)
127 int ret = 0;
128 ALL_SCRIPTS(sf_CommandExecuted, (command, args, argc), 0);
129 return ret;