-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrext.c
346 lines (288 loc) · 8.7 KB
/
scrext.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*************************************************************************************************
* Scripting language extension of Tokyo Promenade
* Copyright (C) 2008-2010 Mikio Hirabayashi
* This file is part of Tokyo Promenade.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#include "scrext.h"
/*************************************************************************************************
* by default
*************************************************************************************************/
#if defined(TTNOEXT)
/* Initialize the scripting language extension. */
void *scrextnew(void){
return NULL;
}
/* Destroy the scripting language extension. */
void scrextdel(void *scr){
assert(scr);
}
/* Load a script file of the scripting language extension. */
bool scrextload(void *scr, const char *path){
assert(scr && path);
return false;
}
/* Check a function is implemented by the scripting language extension. */
bool scrextcheckfunc(void *scr, const char *name){
assert(scr && name);
return false;
}
/* Call a function of the scripting language extension. */
char *scrextcallfunc(void *scr, const char *name, const char *param){
assert(scr && name && param);
return NULL;
}
/* Get the error message of the last operation of scripting language extension. */
const char *screxterrmsg(void *scr){
assert(scr);
return NULL;
}
/* Set a table variable of scripting language extension. */
void scrextsetmapvar(void *scr, const char *name, const TCMAP *params){
assert(scr && name && params);
}
#endif
/*************************************************************************************************
* for Lua
*************************************************************************************************/
#if defined(TTLUAEXT)
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
typedef struct { // type of structure of the script extension
lua_State *lua; // Lua environment
TCXSTR *emsg; // error message
} SCREXT;
/* private function prototypes */
static void scrextseterrmsg(SCREXT *scr);
static int scrextstrstr(lua_State *lua);
static int scrextregex(lua_State *lua);
/* Initialize the scripting language extension. */
void *scrextnew(void){
lua_State *lua = luaL_newstate();
if(!lua) return NULL;
luaL_openlibs(lua);
lua_pushcfunction(lua, scrextstrstr);
lua_setglobal(lua, "_strstr");
lua_pushcfunction(lua, scrextregex);
lua_setglobal(lua, "_regex");
SCREXT *scr = tcmalloc(sizeof(*scr));
scr->lua = lua;
scr->emsg = tcxstrnew();
return scr;
}
/* Destroy the scripting language extension. */
void scrextdel(void *scr){
assert(scr);
SCREXT *s = scr;
tcxstrdel(s->emsg);
lua_close(s->lua);
tcfree(s);
}
/* Load a script file of the scripting language extension. */
bool scrextload(void *scr, const char *path){
assert(scr && path);
SCREXT *s = scr;
lua_State *lua = s->lua;
tcxstrclear(s->emsg);
char *ibuf;
int isiz;
if(*path == '@'){
ibuf = tcstrdup(path + 1);
isiz = strlen(ibuf);
} else if(*path != '\0'){
ibuf = tcreadfile(path, 0, &isiz);
} else {
ibuf = tcmemdup("", 0);
isiz = 0;
}
bool err = false;
if(ibuf){
lua_settop(lua, 0);
if(luaL_loadstring(lua, ibuf) == 0){
if(lua_pcall(lua, 0, 0, 0) != 0){
scrextseterrmsg(s);
err = true;
}
} else {
scrextseterrmsg(s);
err = true;
}
tcfree(ibuf);
} else {
tcxstrprintf(s->emsg, "%s: could not open", path);
err = true;
}
return !err;
}
/* Check a function is implemented by the scripting language extension. */
bool scrextcheckfunc(void *scr, const char *name){
assert(scr && name);
SCREXT *s = scr;
lua_State *lua = s->lua;
lua_settop(lua, 0);
lua_getglobal(lua, name);
return lua_gettop(lua) == 1 && lua_isfunction(lua, 1);
}
/* Get the error message of the last operation of scripting language extension. */
const char *screxterrmsg(void *scr){
assert(scr);
SCREXT *s = scr;
const char *emsg = tcxstrptr(s->emsg);
return *emsg != '\0' ? emsg : NULL;
}
/* Call a function of the scripting language extension. */
char *scrextcallfunc(void *scr, const char *name, const char *param){
assert(scr && name && param);
SCREXT *s = scr;
lua_State *lua = s->lua;
tcxstrclear(s->emsg);
lua_settop(lua, 0);
lua_getglobal(lua, name);
if(lua_gettop(lua) != 1 || !lua_isfunction(lua, 1)){
tcxstrprintf(s->emsg, "%s: no such function", name);
return NULL;
}
lua_pushstring(lua, param);
if(lua_pcall(lua, 1, 1, 0) != 0){
scrextseterrmsg(s);
return NULL;
}
if(lua_gettop(lua) < 1){
tcxstrprintf(s->emsg, "%s: no return value", name);
return NULL;
}
const char *res = NULL;
switch(lua_type(lua, 1)){
case LUA_TNUMBER:
case LUA_TSTRING:
res = lua_tostring(lua, 1);
break;
case LUA_TBOOLEAN:
res = lua_toboolean(lua, 1) ? "[true]" : "[false]";
break;
case LUA_TTABLE:
res = "[table]";
break;
case LUA_TNIL:
res = "";
break;
}
if(!res) res = "[unknown]";
return tcstrdup(res);
}
/* Set a table variable of scripting language extension. */
void scrextsetmapvar(void *scr, const char *name, const TCMAP *params){
assert(scr && name && params);
SCREXT *s = scr;
lua_State *lua = s->lua;
lua_settop(lua, 0);
TCLIST *keys = tcmapkeys(params);
int knum = tclistnum(keys);
lua_createtable(lua, 0, knum);
for(int i = 0; i < knum; i++){
const char *key = tclistval2(keys, i);
const char *val = tcmapget2(params, key);
if(val){
lua_pushstring(lua, key);
lua_pushstring(lua, val);
lua_settable(lua, 1);
}
}
tclistdel(keys);
lua_setglobal(lua, name);
}
/* Set the error message the last operation.
`scr' specifies the scripting object. */
static void scrextseterrmsg(SCREXT *scr){
assert(scr);
lua_State *lua = scr->lua;
int argc = lua_gettop(lua);
tcxstrcat2(scr->emsg, argc > 0 ? lua_tostring(lua, argc) : "unknown");
}
/* Built-in function of strstr.
`lua' specifies the lua processor object
The return value is the number of the return values. */
static int scrextstrstr(lua_State *lua){
int argc = lua_gettop(lua);
if(argc < 2){
lua_pushstring(lua, "strstr: invalid arguments");
lua_error(lua);
}
const char *str = lua_tostring(lua, 1);
const char *pat = lua_tostring(lua, 2);
if(!str || !pat){
lua_pushstring(lua, "strstr: invalid arguments");
lua_error(lua);
}
const char *alt = argc > 2 ? lua_tostring(lua, 3) : NULL;
if(alt){
TCXSTR *xstr = tcxstrnew();
int plen = strlen(pat);
int alen = strlen(alt);
if(plen > 0){
char *pv;
while((pv = strstr(str, pat)) != NULL){
tcxstrcat(xstr, str, pv - str);
tcxstrcat(xstr, alt, alen);
str = pv + plen;
}
}
tcxstrcat2(xstr, str);
lua_settop(lua, 0);
lua_pushstring(lua, tcxstrptr(xstr));
tcxstrdel(xstr);
} else {
char *pv = strstr(str, pat);
if(pv){
int idx = pv - str + 1;
lua_settop(lua, 0);
lua_pushinteger(lua, idx);
} else {
lua_settop(lua, 0);
lua_pushinteger(lua, 0);
}
}
return 1;
}
/* Built-in function of regex.
`lua' specifies the lua processor object
The return value is the number of the return values. */
static int scrextregex(lua_State *lua){
int argc = lua_gettop(lua);
if(argc < 2){
lua_pushstring(lua, "regex: invalid arguments");
lua_error(lua);
}
const char *str = lua_tostring(lua, 1);
const char *regex = lua_tostring(lua, 2);
if(!str || !regex){
lua_pushstring(lua, "regex: invalid arguments");
lua_error(lua);
}
const char *alt = argc > 2 ? lua_tostring(lua, 3) : NULL;
if(alt){
char *res = tcregexreplace(str, regex, alt);
lua_settop(lua, 0);
lua_pushstring(lua, res);
tcfree(res);
} else {
if(tcregexmatch(str, regex)){
lua_settop(lua, 0);
lua_pushboolean(lua, true);
} else {
lua_settop(lua, 0);
lua_pushboolean(lua, false);
}
}
return 1;
}
#endif
// END OF FILE