| 1 |
/* lcallbacklib.c |
| 2 |
|
| 3 |
Copyright 2006-2008 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "ptexlib.h" |
| 21 |
#include "lua/luatex-api.h" |
| 22 |
|
| 23 |
int callback_count = 0; |
| 24 |
int saved_callback_count = 0; |
| 25 |
int direct_callback_count = 0; |
| 26 |
int late_callback_count = 0; |
| 27 |
int function_callback_count = 0; |
| 28 |
|
| 29 |
int callback_set[total_callbacks] = { 0 }; |
| 30 |
|
| 31 |
/* See also callback_callback_type in luatexcallbackids.h: they must have the same order ! */ |
| 32 |
|
| 33 |
static const char *const callbacknames[] = { |
| 34 |
"", /* empty on purpose */ |
| 35 |
"find_write_file", |
| 36 |
"find_output_file", |
| 37 |
"find_image_file", |
| 38 |
"find_format_file", |
| 39 |
"find_read_file", "open_read_file", |
| 40 |
"find_vf_file", "read_vf_file", |
| 41 |
"find_data_file", "read_data_file", |
| 42 |
"find_font_file", "read_font_file", |
| 43 |
"find_map_file", "read_map_file", |
| 44 |
"find_enc_file", "read_enc_file", |
| 45 |
"find_type1_file", "read_type1_file", |
| 46 |
"find_truetype_file", "read_truetype_file", |
| 47 |
"find_opentype_file", "read_opentype_file", |
| 48 |
"find_cidmap_file", "read_cidmap_file", |
| 49 |
"find_pk_file", "read_pk_file", |
| 50 |
"show_error_hook", |
| 51 |
"process_input_buffer", "process_output_buffer", |
| 52 |
"process_jobname", |
| 53 |
"start_page_number", "stop_page_number", |
| 54 |
"start_run", "stop_run", |
| 55 |
"define_font", |
| 56 |
"pre_output_filter", |
| 57 |
"buildpage_filter", |
| 58 |
"hpack_filter", "vpack_filter", |
| 59 |
"glyph_not_found", "glyph_info", |
| 60 |
"hyphenate", |
| 61 |
"ligaturing", |
| 62 |
"kerning", |
| 63 |
"pre_linebreak_filter", |
| 64 |
"linebreak_filter", |
| 65 |
"post_linebreak_filter", |
| 66 |
"append_to_vlist_filter", |
| 67 |
"mlist_to_hlist", |
| 68 |
"finish_pdffile", |
| 69 |
"finish_pdfpage", |
| 70 |
"pre_dump","start_file", "stop_file", |
| 71 |
"show_error_message","show_lua_error_hook","show_ignored_error_message", |
| 72 |
"show_warning_message", |
| 73 |
"hpack_quality", "vpack_quality", |
| 74 |
"process_rule", |
| 75 |
"insert_local_par", |
| 76 |
"contribute_filter", |
| 77 |
"call_edit", |
| 78 |
"build_page_insert", |
| 79 |
"glyph_stream_provider", |
| 80 |
"font_descriptor_objnum_provider", |
| 81 |
"finish_synctex", |
| 82 |
"wrapup_run", |
| 83 |
"new_graf", |
| 84 |
"page_order_index", |
| 85 |
"make_extensible", |
| 86 |
"process_pdf_image_content", |
| 87 |
"provide_charproc_data", |
| 88 |
"input_level_string", |
| 89 |
NULL |
| 90 |
}; |
| 91 |
|
| 92 |
int callback_callbacks_id = 0; |
| 93 |
|
| 94 |
int debug_callback_defined(int i) |
| 95 |
{ |
| 96 |
printf ("callback_defined(%s)\n", callbacknames[i]); |
| 97 |
return callback_set[i]; |
| 98 |
} |
| 99 |
|
| 100 |
void get_lua_boolean(const char *table, const char *name, boolean * target) |
| 101 |
{ |
| 102 |
int stacktop = lua_gettop(Luas); |
| 103 |
luaL_checkstack(Luas, 2, "out of stack space"); |
| 104 |
lua_getglobal(Luas, table); |
| 105 |
if (lua_istable(Luas, -1)) { |
| 106 |
int t; |
| 107 |
lua_getfield(Luas, -1, name); |
| 108 |
t = lua_type(Luas, -1); |
| 109 |
if (t == LUA_TBOOLEAN) { |
| 110 |
*target = (boolean) (lua_toboolean(Luas, -1)); |
| 111 |
} else if (t == LUA_TNUMBER) { |
| 112 |
*target = (boolean) (lua_tointeger(Luas, -1) == 0 ? 0 : 1); |
| 113 |
} |
| 114 |
} |
| 115 |
lua_settop(Luas, stacktop); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
void get_saved_lua_boolean(int r, const char *name, boolean * target) |
| 120 |
{ |
| 121 |
int stacktop = lua_gettop(Luas); |
| 122 |
luaL_checkstack(Luas, 2, "out of stack space"); |
| 123 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, r); |
| 124 |
if (lua_istable(Luas, -1)) { |
| 125 |
int t; |
| 126 |
lua_getfield(Luas, -1, name); |
| 127 |
t = lua_type(Luas, -1); |
| 128 |
if (t == LUA_TBOOLEAN) { |
| 129 |
*target = (boolean) lua_toboolean(Luas, -1); |
| 130 |
} else if (t == LUA_TNUMBER) { |
| 131 |
*target = (boolean) (lua_tointeger(Luas, -1) == 0 ? 0 : 1); |
| 132 |
} |
| 133 |
} |
| 134 |
lua_settop(Luas, stacktop); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
void get_lua_number(const char *table, const char *name, int *target) |
| 139 |
{ |
| 140 |
int stacktop = lua_gettop(Luas); |
| 141 |
luaL_checkstack(Luas, 2, "out of stack space"); |
| 142 |
lua_getglobal(Luas, table); |
| 143 |
if (lua_istable(Luas, -1)) { |
| 144 |
lua_getfield(Luas, -1, name); |
| 145 |
if (lua_type(Luas, -1) == LUA_TNUMBER) { |
| 146 |
*target = (int) lua_roundnumber(Luas, -1); /* was lua_tointeger */ |
| 147 |
} |
| 148 |
} |
| 149 |
lua_settop(Luas, stacktop); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
void get_saved_lua_number(int r, const char *name, int *target) |
| 154 |
{ |
| 155 |
int stacktop = lua_gettop(Luas); |
| 156 |
luaL_checkstack(Luas, 2, "out of stack space"); |
| 157 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, r); |
| 158 |
if (lua_istable(Luas, -1)) { |
| 159 |
lua_getfield(Luas, -1, name); |
| 160 |
if (lua_type(Luas, -1) == LUA_TNUMBER) { |
| 161 |
*target = (int) lua_tointeger(Luas, -1); |
| 162 |
} |
| 163 |
} |
| 164 |
lua_settop(Luas, stacktop); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
void get_lua_string(const char *table, const char *name, char **target) |
| 169 |
{ |
| 170 |
int stacktop = lua_gettop(Luas); |
| 171 |
luaL_checkstack(Luas, 2, "out of stack space"); |
| 172 |
lua_getglobal(Luas, table); |
| 173 |
if (lua_type(Luas, -1) == LUA_TTABLE) { |
| 174 |
lua_getfield(Luas, -1, name); |
| 175 |
if (lua_type(Luas, -1) == LUA_TSTRING) { |
| 176 |
*target = xstrdup(lua_tostring(Luas, -1)); |
| 177 |
} |
| 178 |
} |
| 179 |
lua_settop(Luas, stacktop); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
void get_saved_lua_string(int r, const char *name, char **target) |
| 184 |
{ |
| 185 |
int stacktop = lua_gettop(Luas); |
| 186 |
luaL_checkstack(Luas, 2, "out of stack space"); |
| 187 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, r); |
| 188 |
if (lua_type(Luas, -1) == LUA_TTABLE) { |
| 189 |
lua_getfield(Luas, -1, name); |
| 190 |
if (lua_type(Luas, -1) == LUA_TSTRING) { |
| 191 |
*target = xstrdup(lua_tostring(Luas, -1)); |
| 192 |
} |
| 193 |
} |
| 194 |
lua_settop(Luas, stacktop); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
#define CALLBACK_BOOLEAN 'b' |
| 199 |
#define CALLBACK_INTEGER 'd' |
| 200 |
#define CALLBACK_FLOAT 'f' |
| 201 |
#define CALLBACK_LINE 'l' |
| 202 |
#define CALLBACK_STRNUMBER 's' |
| 203 |
#define CALLBACK_STRING 'S' |
| 204 |
#define CALLBACK_RESULT 'R' /* a string but nil is also ok */ |
| 205 |
#define CALLBACK_CHARNUM 'c' |
| 206 |
#define CALLBACK_LSTRING 'L' |
| 207 |
#define CALLBACK_NODE 'N' |
| 208 |
#define CALLBACK_DIR 'D' |
| 209 |
|
| 210 |
int run_saved_callback(int r, const char *name, const char *values, ...) |
| 211 |
{ |
| 212 |
va_list args; |
| 213 |
int ret = 0; |
| 214 |
int stacktop = lua_gettop(Luas); |
| 215 |
va_start(args, values); |
| 216 |
luaL_checkstack(Luas, 2, "out of stack space"); |
| 217 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, r); |
| 218 |
lua_pushstring(Luas, name); |
| 219 |
lua_rawget(Luas, -2); |
| 220 |
if (lua_isfunction(Luas, -1)) { |
| 221 |
saved_callback_count++; |
| 222 |
ret = do_run_callback(2, values, args); |
| 223 |
} |
| 224 |
va_end(args); |
| 225 |
lua_settop(Luas, stacktop); |
| 226 |
return ret; |
| 227 |
} |
| 228 |
|
| 229 |
boolean get_callback(lua_State * L, int i) |
| 230 |
{ |
| 231 |
luaL_checkstack(L, 2, "out of stack space"); |
| 232 |
lua_rawgeti(L, LUA_REGISTRYINDEX, callback_callbacks_id); |
| 233 |
lua_rawgeti(L, -1, i); |
| 234 |
if (lua_isfunction(L, -1)) { |
| 235 |
callback_count++; |
| 236 |
return true; |
| 237 |
} else { |
| 238 |
return false; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
int run_and_save_callback(int i, const char *values, ...) |
| 243 |
{ |
| 244 |
va_list args; |
| 245 |
int ret = 0; |
| 246 |
int stacktop = lua_gettop(Luas); |
| 247 |
va_start(args, values); |
| 248 |
if (get_callback(Luas, i)) { |
| 249 |
ret = do_run_callback(1, values, args); |
| 250 |
} |
| 251 |
va_end(args); |
| 252 |
if (ret > 0) { |
| 253 |
ret = luaL_ref(Luas, LUA_REGISTRYINDEX); |
| 254 |
} |
| 255 |
lua_settop(Luas, stacktop); |
| 256 |
return ret; |
| 257 |
} |
| 258 |
|
| 259 |
int run_callback(int i, const char *values, ...) |
| 260 |
{ |
| 261 |
va_list args; |
| 262 |
int ret = 0; |
| 263 |
int stacktop = lua_gettop(Luas); |
| 264 |
va_start(args, values); |
| 265 |
if (get_callback(Luas, i)) { |
| 266 |
ret = do_run_callback(0, values, args); |
| 267 |
} |
| 268 |
va_end(args); |
| 269 |
lua_settop(Luas, stacktop); |
| 270 |
return ret; |
| 271 |
} |
| 272 |
|
| 273 |
int do_run_callback(int special, const char *values, va_list vl) |
| 274 |
{ |
| 275 |
int ret; |
| 276 |
size_t len; |
| 277 |
int narg, nres; |
| 278 |
const char *s; |
| 279 |
lstring *lstr; |
| 280 |
char cs; |
| 281 |
int *bufloc; |
| 282 |
char *ss = NULL; |
| 283 |
int retval = 0; |
| 284 |
if (special == 2) { /* copy the enclosing table */ |
| 285 |
luaL_checkstack(Luas, 1, "out of stack space"); |
| 286 |
lua_pushvalue(Luas, -2); |
| 287 |
} |
| 288 |
ss = strchr(values, '>'); |
| 289 |
luaL_checkstack(Luas, (int) (ss - values + 1), "out of stack space"); |
| 290 |
ss = NULL; |
| 291 |
for (narg = 0; *values; narg++) { |
| 292 |
switch (*values++) { |
| 293 |
case CALLBACK_CHARNUM: /* an ascii char! */ |
| 294 |
cs = (char) va_arg(vl, int); |
| 295 |
lua_pushlstring(Luas, &cs, 1); |
| 296 |
break; |
| 297 |
case CALLBACK_STRING: /* C string */ |
| 298 |
s = va_arg(vl, char *); |
| 299 |
lua_pushstring(Luas, s); |
| 300 |
break; |
| 301 |
case CALLBACK_LSTRING: /* 'lstring' */ |
| 302 |
lstr = va_arg(vl, lstring *); |
| 303 |
lua_pushlstring(Luas, (const char *)lstr->s, lstr->l); |
| 304 |
break; |
| 305 |
case CALLBACK_INTEGER: /* int */ |
| 306 |
lua_pushinteger(Luas, va_arg(vl, int)); |
| 307 |
break; |
| 308 |
case CALLBACK_FLOAT: /* double */ |
| 309 |
lua_pushnumber(Luas, va_arg(vl, double)); |
| 310 |
break; |
| 311 |
case CALLBACK_STRNUMBER: /* TeX string */ |
| 312 |
s = makeclstring(va_arg(vl, int), &len); |
| 313 |
lua_pushlstring(Luas, s, len); |
| 314 |
break; |
| 315 |
case CALLBACK_BOOLEAN: /* boolean */ |
| 316 |
lua_pushboolean(Luas, va_arg(vl, int)); |
| 317 |
break; |
| 318 |
case CALLBACK_LINE: /* a buffer section, with implied start */ |
| 319 |
lua_pushlstring(Luas, (char *) (buffer + first), (size_t) va_arg(vl, int)); |
| 320 |
break; |
| 321 |
case CALLBACK_NODE: |
| 322 |
lua_nodelib_push_fast(Luas, va_arg(vl, int)); |
| 323 |
break; |
| 324 |
case CALLBACK_DIR: |
| 325 |
lua_push_dir_par(Luas, va_arg(vl, int)); |
| 326 |
break; |
| 327 |
case '-': |
| 328 |
narg--; |
| 329 |
break; |
| 330 |
case '>': |
| 331 |
goto ENDARGS; |
| 332 |
default: |
| 333 |
; |
| 334 |
} |
| 335 |
} |
| 336 |
ENDARGS: |
| 337 |
nres = (int) strlen(values); |
| 338 |
if (special == 1) { |
| 339 |
nres++; |
| 340 |
} |
| 341 |
if (special == 2) { |
| 342 |
narg++; |
| 343 |
} |
| 344 |
{ |
| 345 |
int i; |
| 346 |
lua_active++; |
| 347 |
i = lua_pcall(Luas, narg, nres, 0); |
| 348 |
lua_active--; |
| 349 |
/* lua_remove(L, base); *//* remove traceback function */ |
| 350 |
if (i != 0) { |
| 351 |
/* Can't be more precise here, could be called before |
| 352 |
* TeX initialization is complete |
| 353 |
*/ |
| 354 |
if (!log_opened_global) { |
| 355 |
fprintf(stderr, "error in callback: %s\n", lua_tostring(Luas, -1)); |
| 356 |
error(); |
| 357 |
} else { |
| 358 |
lua_gc(Luas, LUA_GCCOLLECT, 0); |
| 359 |
luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1)); |
| 360 |
} |
| 361 |
return 0; |
| 362 |
} |
| 363 |
} |
| 364 |
if (nres == 0) { |
| 365 |
return 1; |
| 366 |
} |
| 367 |
nres = -nres; |
| 368 |
while (*values) { |
| 369 |
int b, t; |
| 370 |
double d; |
| 371 |
halfword p; |
| 372 |
t = lua_type(Luas, nres); |
| 373 |
switch (*values++) { |
| 374 |
case CALLBACK_BOOLEAN: |
| 375 |
if (t == LUA_TNIL) { |
| 376 |
b = 0; |
| 377 |
} else if (t != LUA_TBOOLEAN) { |
| 378 |
fprintf(stderr, "callback should return a boolean, not: %s\n", lua_typename(Luas, t)); |
| 379 |
goto EXIT; |
| 380 |
} else { |
| 381 |
b = lua_toboolean(Luas, nres); |
| 382 |
} |
| 383 |
*va_arg(vl, boolean *) = (boolean) b; |
| 384 |
break; |
| 385 |
case CALLBACK_INTEGER: |
| 386 |
if (t != LUA_TNUMBER) { |
| 387 |
fprintf(stderr, "callback should return a number, not: %s\n", lua_typename(Luas, t)); |
| 388 |
goto EXIT; |
| 389 |
} |
| 390 |
b = lua_tointeger(Luas, nres); |
| 391 |
*va_arg(vl, int *) = b; |
| 392 |
break; |
| 393 |
case CALLBACK_FLOAT: |
| 394 |
if (t != LUA_TNUMBER) { |
| 395 |
fprintf(stderr, "callback should return a number, not: %s\n", lua_typename(Luas, t)); |
| 396 |
goto EXIT; |
| 397 |
} |
| 398 |
d = lua_tonumber(Luas, nres); |
| 399 |
*va_arg(vl, double *) = d; |
| 400 |
break; |
| 401 |
case CALLBACK_LINE: /* TeX line ... happens frequently when we have a plug-in */ |
| 402 |
if (t == LUA_TNIL) { |
| 403 |
bufloc = 0; |
| 404 |
goto EXIT; |
| 405 |
} else if (t == LUA_TSTRING) { |
| 406 |
s = lua_tolstring(Luas, nres, &len); |
| 407 |
if (s == NULL) { /* |len| can be zero */ |
| 408 |
bufloc = 0; |
| 409 |
} else if (len == 0) { |
| 410 |
bufloc = 0; |
| 411 |
} else { |
| 412 |
bufloc = va_arg(vl, int *); |
| 413 |
ret = *bufloc; |
| 414 |
check_buffer_overflow(ret + (int) len); |
| 415 |
strncpy((char *) (buffer + ret), s, len); |
| 416 |
*bufloc += (int) len; |
| 417 |
/* while (len--) { buffer[(*bufloc)++] = *s++; } */ |
| 418 |
while ((*bufloc) - 1 > ret && buffer[(*bufloc) - 1] == ' ') |
| 419 |
(*bufloc)--; |
| 420 |
} |
| 421 |
} else { |
| 422 |
fprintf(stderr, "callback should return a string, not: %s\n", lua_typename(Luas, t)); |
| 423 |
goto EXIT; |
| 424 |
} |
| 425 |
break; |
| 426 |
case CALLBACK_STRNUMBER: /* TeX string */ |
| 427 |
if (t != LUA_TSTRING) { |
| 428 |
fprintf(stderr, "callback should return a string, not: %s\n", lua_typename(Luas, t)); |
| 429 |
goto EXIT; |
| 430 |
} |
| 431 |
s = lua_tolstring(Luas, nres, &len); |
| 432 |
if (s == NULL) /* |len| can be zero */ |
| 433 |
*va_arg(vl, int *) = 0; |
| 434 |
else { |
| 435 |
*va_arg(vl, int *) = maketexlstring(s, len); |
| 436 |
} |
| 437 |
break; |
| 438 |
case CALLBACK_STRING: /* C string aka buffer */ |
| 439 |
if (t != LUA_TSTRING) { |
| 440 |
fprintf(stderr, "callback should return a string, not: %s\n", lua_typename(Luas, t)); |
| 441 |
goto EXIT; |
| 442 |
} |
| 443 |
s = lua_tolstring(Luas, nres, &len); |
| 444 |
if (s == NULL) /* |len| can be zero */ |
| 445 |
*va_arg(vl, int *) = 0; |
| 446 |
else { |
| 447 |
ss = xmalloc((unsigned) (len + 1)); |
| 448 |
(void) memcpy(ss, s, (len + 1)); |
| 449 |
*va_arg(vl, char **) = ss; |
| 450 |
} |
| 451 |
break; |
| 452 |
case CALLBACK_RESULT: /* C string aka buffer */ |
| 453 |
if (t == LUA_TNIL) { |
| 454 |
*va_arg(vl, int *) = 0; |
| 455 |
} else if (t == LUA_TBOOLEAN) { |
| 456 |
b = lua_toboolean(Luas, nres); |
| 457 |
if (b == 0) { |
| 458 |
*va_arg(vl, int *) = 0; |
| 459 |
} else { |
| 460 |
fprintf(stderr, "callback should return a string, false or nil, not: %s\n", lua_typename(Luas, t)); |
| 461 |
goto EXIT; |
| 462 |
} |
| 463 |
} else if (t != LUA_TSTRING) { |
| 464 |
fprintf(stderr, "callback should return a string, false or nil, not: %s\n", lua_typename(Luas, t)); |
| 465 |
goto EXIT; |
| 466 |
} else { |
| 467 |
s = lua_tolstring(Luas, nres, &len); |
| 468 |
if (s == NULL) /* |len| can be zero */ |
| 469 |
*va_arg(vl, int *) = 0; |
| 470 |
else { |
| 471 |
ss = xmalloc((unsigned) (len + 1)); |
| 472 |
(void) memcpy(ss, s, (len + 1)); |
| 473 |
*va_arg(vl, char **) = ss; |
| 474 |
} |
| 475 |
} |
| 476 |
break; |
| 477 |
case CALLBACK_LSTRING: /* lstring */ |
| 478 |
if (t != LUA_TSTRING) { |
| 479 |
fprintf(stderr, "callback should return a string, not: %s\n", lua_typename(Luas, t)); |
| 480 |
goto EXIT; |
| 481 |
} |
| 482 |
s = lua_tolstring(Luas, nres, &len); |
| 483 |
if (s == NULL) /* |len| can be zero */ |
| 484 |
*va_arg(vl, int *) = 0; |
| 485 |
else { |
| 486 |
lstring *ret = xmalloc(sizeof(lstring)); |
| 487 |
ret->s = xmalloc((unsigned) (len + 1)); |
| 488 |
(void) memcpy(ret->s, s, (len + 1)); |
| 489 |
ret->l = len; |
| 490 |
*va_arg(vl, lstring **) = ret; |
| 491 |
} |
| 492 |
break; |
| 493 |
case CALLBACK_NODE: |
| 494 |
if (t == LUA_TNIL) { |
| 495 |
p = null; |
| 496 |
} else { |
| 497 |
p = *check_isnode(Luas,nres); |
| 498 |
} |
| 499 |
*va_arg(vl, int *) = p; |
| 500 |
break; |
| 501 |
default: |
| 502 |
fprintf(stdout, "callback returned an invalid value type"); |
| 503 |
goto EXIT; |
| 504 |
} |
| 505 |
nres++; |
| 506 |
} |
| 507 |
retval = 1; |
| 508 |
EXIT: |
| 509 |
return retval; |
| 510 |
} |
| 511 |
|
| 512 |
void destroy_saved_callback(int i) |
| 513 |
{ |
| 514 |
luaL_unref(Luas, LUA_REGISTRYINDEX, i); |
| 515 |
} |
| 516 |
|
| 517 |
static int callback_register(lua_State * L) |
| 518 |
{ |
| 519 |
int cb; |
| 520 |
const char *s; |
| 521 |
int t1 = lua_type(L,1); |
| 522 |
int t2 = lua_type(L,2); |
| 523 |
if (t1 != LUA_TSTRING) { |
| 524 |
lua_pushnil(L); |
| 525 |
lua_pushstring(L, "Invalid arguments to callback.register, first argument must be string."); |
| 526 |
return 2; |
| 527 |
} |
| 528 |
if ((t2 != LUA_TFUNCTION) && (t2 != LUA_TNIL) && ((t2 != LUA_TBOOLEAN) && (lua_toboolean(L, 2) == 0))) { |
| 529 |
lua_pushnil(L); |
| 530 |
lua_pushstring(L, "Invalid arguments to callback.register."); |
| 531 |
return 2; |
| 532 |
} |
| 533 |
s = lua_tostring(L, 1); |
| 534 |
for (cb = 0; cb < total_callbacks; cb++) { |
| 535 |
if (strcmp(callbacknames[cb], s) == 0) |
| 536 |
break; |
| 537 |
} |
| 538 |
if (cb == total_callbacks) { |
| 539 |
lua_pushnil(L); |
| 540 |
lua_pushstring(L, "No such callback exists."); |
| 541 |
return 2; |
| 542 |
} |
| 543 |
if (t2 == LUA_TFUNCTION) { |
| 544 |
callback_set[cb] = cb; |
| 545 |
} else if (t2 == LUA_TBOOLEAN) { |
| 546 |
callback_set[cb] = -1; |
| 547 |
} else { |
| 548 |
callback_set[cb] = 0; |
| 549 |
} |
| 550 |
luaL_checkstack(L, 2, "out of stack space"); |
| 551 |
lua_rawgeti(L, LUA_REGISTRYINDEX, callback_callbacks_id); /* push the table */ |
| 552 |
lua_pushvalue(L, 2); /* the function or nil */ |
| 553 |
lua_rawseti(L, -2, cb); |
| 554 |
lua_rawseti(L, LUA_REGISTRYINDEX, callback_callbacks_id); |
| 555 |
lua_pushinteger(L, cb); |
| 556 |
return 1; |
| 557 |
} |
| 558 |
|
| 559 |
static int callback_find(lua_State * L) |
| 560 |
{ |
| 561 |
int cb; |
| 562 |
const char *s; |
| 563 |
if (lua_type(L, 1) != LUA_TSTRING) { |
| 564 |
lua_pushnil(L); |
| 565 |
lua_pushstring(L, "Invalid arguments to callback.find."); |
| 566 |
return 2; |
| 567 |
} |
| 568 |
s = lua_tostring(L, 1); |
| 569 |
for (cb = 0; cb < total_callbacks; cb++) { |
| 570 |
if (strcmp(callbacknames[cb], s) == 0) |
| 571 |
break; |
| 572 |
} |
| 573 |
if (cb == total_callbacks) { |
| 574 |
lua_pushnil(L); |
| 575 |
lua_pushstring(L, "No such callback exists."); |
| 576 |
return 2; |
| 577 |
} |
| 578 |
luaL_checkstack(L, 2, "out of stack space"); |
| 579 |
lua_rawgeti(L, LUA_REGISTRYINDEX, callback_callbacks_id); /* push the table */ |
| 580 |
lua_rawgeti(L, -1, cb); |
| 581 |
return 1; |
| 582 |
} |
| 583 |
|
| 584 |
static int callback_listf(lua_State * L) |
| 585 |
{ |
| 586 |
int i; |
| 587 |
luaL_checkstack(L, 3, "out of stack space"); |
| 588 |
lua_newtable(L); |
| 589 |
for (i = 1; callbacknames[i]; i++) { |
| 590 |
lua_pushstring(L, callbacknames[i]); |
| 591 |
if (callback_defined(i)) { |
| 592 |
lua_pushboolean(L, 1); |
| 593 |
} else { |
| 594 |
lua_pushboolean(L, 0); |
| 595 |
} |
| 596 |
lua_rawset(L, -3); |
| 597 |
} |
| 598 |
return 1; |
| 599 |
} |
| 600 |
|
| 601 |
static const struct luaL_Reg callbacklib[] = { |
| 602 |
{"find", callback_find}, |
| 603 |
{"register", callback_register}, |
| 604 |
{"list", callback_listf}, |
| 605 |
{NULL, NULL} /* sentinel */ |
| 606 |
}; |
| 607 |
|
| 608 |
int luaopen_callback(lua_State * L) |
| 609 |
{ |
| 610 |
luaL_register(L, "callback", callbacklib); |
| 611 |
luaL_checkstack(L, 1, "out of stack space"); |
| 612 |
lua_newtable(L); |
| 613 |
callback_callbacks_id = luaL_ref(L, LUA_REGISTRYINDEX); |
| 614 |
return 1; |
| 615 |
} |