Use language neutral description rather than lua.
[screen-lua.git] / src / script.c
blob9f0e6d5ba5a3efe0eaa7be5208447c6e9d141a60
1 /* Copyright (c) 2008 Sadrul Habib Chowdhury (sadrul@users.sf.net)
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 3, or (at your option)
6 * any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program (see the file COPYING); if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
18 ****************************************************************
21 #include "config.h"
22 #include "screen.h"
24 struct scripts
26 struct scripts *s_next;
27 struct ScriptFuncs *fns;
30 struct scripts *scripts;
32 static void
33 AddScript(struct ScriptFuncs *sf)
35 struct scripts *ns = (struct scripts *)calloc(1, sizeof(*ns));
36 if (!ns)
37 return;
38 ns->fns = sf;
39 ns->s_next = scripts;
40 scripts = ns;
43 #define ALL_SCRIPTS(fn, params, stop) do { \
44 struct scripts *iter; \
45 for (iter = scripts; iter; iter = iter->s_next) \
46 { \
47 if (iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
48 break; \
49 } \
50 } while (0)
52 void ScriptInit(void)
54 int ret;
55 ALL_SCRIPTS(sf_Init, (), 0);
58 void ScriptFinit(void)
60 int ret;
61 ALL_SCRIPTS(sf_Finit, (), 0);
64 void ScriptForeWindowChanged(void)
66 int ret;
67 ALL_SCRIPTS(sf_ForeWindowChanged, (), 0);
70 void ScriptSource(int argc, const char **argv)
72 int ret;
73 int async = 0;
74 const char *binding = 0, *path;
76 while (*argv && **argv == '-') {
77 // check for (-a | -async)
78 if ((*argv[1] == 'a' && !*argv[2])
79 || strcmp(*argv, "-async") == 0)
80 async = 1;
81 // check for (-b | -binding)
82 else if ((*argv[1] == 'b' && !*argv[2])
83 || strcmp(*argv, "-binding") == 0) {
84 argv++;
85 binding = *argv;
87 argv++;
90 path = *argv;
91 if (!binding) {
92 /* If one script loader accepts the file, we don't send it to any other loader */
93 ALL_SCRIPTS(sf_Source, (path), 1);
94 } else {
95 //TODO: select the specified engine.
99 int ScriptProcessCaption(const char *str, struct win *win, int len)
101 int ret = 0;
102 ALL_SCRIPTS(sf_ProcessCaption, (str, win, len), 1);
103 return ret;
106 int ScriptCommandExecuted(const char *command, const char **args, int argc)
108 int ret = 0;
109 ALL_SCRIPTS(sf_CommandExecuted, (command, args, argc), 0);
110 return ret;
113 #define HAVE_LUA 1 /* XXX: Remove */
114 #if HAVE_LUA
115 extern struct ScriptFuncs LuaFuncs;
116 #endif
118 void LoadScripts(void)
120 /* XXX: We could load the script loaders dynamically */
121 #if HAVE_LUA
122 AddScript(&LuaFuncs);
123 #endif