471
/* lua require patch */
473
/* The lua search function.
474
* When kpathsea is not initialized, then it runs the
475
* normal lua function that is saved in the registry, otherwise
479
/* two registry ref variables are needed: one for the actual lua
480
* function, the other for its environment .
483
static int lua_loader_function = 0;
484
static int lua_loader_env = 0;
486
static int luatex_kpse_lua_find(lua_State * L)
488
const char *filename;
490
name = luaL_checkstring(L, 1);
491
if (program_name_set == 0) {
492
lua_CFunction orig_func;
493
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_loader_function);
494
lua_rawgeti(L, LUA_REGISTRYINDEX, lua_loader_env);
495
lua_replace(L, LUA_ENVIRONINDEX);
496
orig_func = lua_tocfunction(L, -1);
498
return (orig_func) (L);
500
filename = kpse_find_file(name, kpse_lua_format, false);
501
if (filename == NULL)
502
return 1; /* library not found in this path */
503
if (luaL_loadfile(L, filename) != 0) {
504
luaL_error(L, "error loading module %s from file %s:\n\t%s",
505
lua_tostring(L, 1), filename, lua_tostring(L, -1));
507
return 1; /* library loaded successfully */
510
/* The lua lib search function.
511
* When kpathsea is not initialized, then it runs the
512
* normal lua function that is saved in the registry, otherwise
516
/* two registry ref variables are needed: one for the actual lua
517
* function, the other for its environment .
520
static int clua_loader_function = 0;
521
static int clua_loader_env = 0;
523
extern int loader_C_luatex(lua_State * L, const char *name,
524
const char *filename);
526
static int luatex_kpse_clua_find(lua_State * L)
528
const char *filename;
531
return 1; /* library not found in this path */
532
name = luaL_checkstring(L, 1);
533
if (program_name_set == 0) {
534
lua_CFunction orig_func;
535
lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_function);
536
lua_rawgeti(L, LUA_REGISTRYINDEX, clua_loader_env);
537
lua_replace(L, LUA_ENVIRONINDEX);
538
orig_func = lua_tocfunction(L, -1);
540
return (orig_func) (L);
542
filename = kpse_find_file(name, kpse_clua_format, false);
543
if (filename == NULL)
544
return 1; /* library not found in this path */
545
return loader_C_luatex(L, name, filename);
548
/* Setting up the new search functions.
549
* This replaces package.loaders[2] with the function defined above.
552
static void setup_lua_path(lua_State * L)
554
lua_getglobal(L, "package");
555
lua_getfield(L, -1, "loaders");
556
lua_rawgeti(L, -1, 2); /* package.loaders[2] */
558
lua_loader_env = luaL_ref(L, LUA_REGISTRYINDEX);
559
lua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
560
lua_pushcfunction(L, luatex_kpse_lua_find);
561
lua_rawseti(L, -2, 2); /* replace the normal lua loader */
563
lua_rawgeti(L, -1, 3); /* package.loaders[3] */
565
clua_loader_env = luaL_ref(L, LUA_REGISTRYINDEX);
566
clua_loader_function = luaL_ref(L, LUA_REGISTRYINDEX);
567
lua_pushcfunction(L, luatex_kpse_clua_find);
568
lua_rawseti(L, -2, 3); /* replace the normal lua lib loader */
569
lua_pop(L, 2); /* pop the array and table */
572
/* helper variables for the safe keeping of table ids */
578
extern void init_tex_table(lua_State * L);
580
#if defined(WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
583
# define EXE_SUFFIXES ".com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.ws;.tcl;.py;.pyw"
585
static void mk_suffixlist(void)
591
# if defined(__CYGWIN__)
592
v = xstrdup(EXE_SUFFIXES);
594
v = (char *) getenv("PATHEXT");
595
if (v) /* strlwr() exists also in MingW */
596
v = (char *) strlwr(xstrdup(v));
598
v = xstrdup(EXE_SUFFIXES);
604
while ((r = strchr(q, ';')) != NULL) {
611
suffixlist = (char **) xmalloc((n + 2) * sizeof(char *));
613
*p = xstrdup(".dll");
616
while ((r = strchr(q, ';')) != NULL) {
630
#endif /* WIN32 || __MIBGW32__ || __CYGWIN__ */
474
632
void lua_initialize(int ac, char **av)
477
635
char *given_file = NULL;
483
637
/* Save to pass along to topenin. */
641
ptexbanner = malloc(256);
642
snprintf(ptexbanner, 256, "This is LuaTeX, Version %s-%d",
643
luatex_version_string, luatex_date_info);
489
645
program_invocation_name = cleaned_invocation_name(argv[0]);