Modifications and simplifications for luametalatex

This commit is contained in:
Marcel Krüger 2020-07-05 20:37:09 +02:00
parent 23a2d7c706
commit e1db9313e1
1 changed files with 52 additions and 97 deletions

View File

@ -1,8 +1,9 @@
/* lkpselib.c
Copyright 2020 Marcel Krüger <tex@2krueger.de
Copyright 2006-2008 Taco Hoekwater <taco@luatex.org>
This file is part of LuaTeX.
This file is based on a file from LuaTeX.
LuaTeX 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
@ -17,27 +18,14 @@
You should have received a copy of the GNU General Public License along
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
#ifdef MF_LUA
#define EXTERN extern
#include <string.h>
#include <stdlib.h>
#include <kpathsea/c-pathch.h>
#include <kpathsea/lib.h>
#include <kpathsea/proginit.h>
#include <kpathsea/progname.h>
#include <lua.h>
#include <lauxlib.h>
/* #if defined(JIT) */
/* #include "mfluajitd.h" */
/* #else */
/* #include "mfluad.h" */
/* #endif */
#include <kpathsea/version.h>
#define xfree(p) do { if (p != NULL) free(p); p = NULL; } while (0)
#else
#include "ptexlib.h"
#include "lua/luatex-api.h"
#endif
#include <kpathsea/expand.h>
#include <kpathsea/variable.h>
#include <kpathsea/tex-glyph.h>
@ -47,66 +35,8 @@
#include <kpathsea/tex-file.h>
#include <kpathsea/paths.h>
static const unsigned filetypes[] = {
kpse_gf_format,
kpse_pk_format,
kpse_any_glyph_format,
kpse_tfm_format,
kpse_afm_format,
kpse_base_format,
kpse_bib_format,
kpse_bst_format,
kpse_cnf_format,
kpse_db_format,
kpse_fmt_format,
kpse_fontmap_format,
kpse_mem_format,
kpse_mf_format,
kpse_mfpool_format,
kpse_mft_format,
kpse_mp_format,
kpse_mppool_format,
kpse_mpsupport_format,
kpse_ocp_format,
kpse_ofm_format,
kpse_opl_format,
kpse_otp_format,
kpse_ovf_format,
kpse_ovp_format,
kpse_pict_format,
kpse_tex_format,
kpse_texdoc_format,
kpse_texpool_format,
kpse_texsource_format,
kpse_tex_ps_header_format,
kpse_troff_font_format,
kpse_type1_format,
kpse_vf_format,
kpse_dvips_config_format,
kpse_ist_format,
kpse_truetype_format,
kpse_type42_format,
kpse_web2c_format,
kpse_program_text_format,
kpse_program_binary_format,
kpse_miscfonts_format,
kpse_web_format,
kpse_cweb_format,
kpse_enc_format,
kpse_cmap_format,
kpse_sfd_format,
kpse_opentype_format,
kpse_pdftex_config_format,
kpse_lig_format,
kpse_texmfscripts_format,
kpse_lua_format,
kpse_fea_format,
kpse_cid_format,
kpse_mlbib_format,
kpse_mlbst_format,
kpse_clua_format
};
#include <lua.h>
#include <lauxlib.h>
static const char *const filetypenames[] = {
"gf",
@ -166,23 +96,33 @@ static const char *const filetypenames[] = {
"mlbib",
"mlbst",
"clua",
"ris",
"bltxml",
NULL
};
_Static_assert(kpse_last_format == sizeof(filetypenames)/sizeof(*filetypenames) - 1, "Mismatch in format types");
#ifdef MF
#define KPATHSEA_METATABLE "mflua.kpathsea"
#else
#define KPATHSEA_METATABLE "luatex.kpathsea"
#endif
static const char *const srcnames[] = {
"implicit", /* C initialization to zero */
"compile", /* configure/compile-time default */
"texmf_cnf", /* texmf.cnf, the kpathsea config file */
"client_cnf", /* application config file, e.g., config.ps */
"env", /* environment variable */
"x", /* X Window System resource */
"cmdline", /* command-line option */
NULL
};
_Static_assert(kpse_src_cmdline == sizeof(srcnames)/sizeof(*srcnames) - 2, "Mismatch in configuration sources");
#define KPATHSEA_METATABLE "luametalatex.kpathsea"
/* set to 1 by the |program_name| function */
#ifdef MF
int program_name_set = 1;
#else
int program_name_set = 0;
#endif
#define TEST_PROGRAM_NAME_SET do { \
if (! program_name_set) { \
@ -209,8 +149,7 @@ static int find_file(lua_State * L)
} else if (t == LUA_TNUMBER) {
mexist = (int) lua_tointeger(L, i);
} else if (t == LUA_TSTRING) {
int op = luaL_checkoption(L, i, NULL, filetypenames);
ftype = filetypes[op];
ftype = luaL_checkoption(L, i, NULL, filetypenames);
} else {
/* ignore */
}
@ -247,8 +186,7 @@ static int lua_kpathsea_find_file(lua_State * L)
} else if (t == LUA_TNUMBER) {
mexist = (int) lua_tointeger(L, i);
} else if (t == LUA_TSTRING) {
int op = luaL_checkoption(L, i, NULL, filetypenames);
ftype = filetypes[op];
ftype = luaL_checkoption(L, i, NULL, filetypenames);
} else {
/* ignore */
}
@ -277,8 +215,7 @@ static int show_texmfcnf(lua_State * L)
static int show_path(lua_State * L)
{
int op = luaL_checkoption(L, -1, "tex", filetypenames);
unsigned user_format = filetypes[op];
unsigned user_format = luaL_checkoption(L, -1, "tex", filetypenames);
TEST_PROGRAM_NAME_SET;
if (!kpse_format_info[user_format].type) /* needed if arg was numeric */
kpse_init_format(user_format);
@ -289,8 +226,7 @@ static int show_path(lua_State * L)
static int lua_kpathsea_show_path(lua_State * L)
{
kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
int op = luaL_checkoption(L, -1, "tex", filetypenames);
unsigned user_format = filetypes[op];
unsigned user_format = luaL_checkoption(L, -1, "tex", filetypenames);
if (!(*kp)->format_info[user_format].type) /* needed if arg was numeric */
kpathsea_init_format(*kp, user_format);
lua_pushstring(L, (*kp)->format_info[user_format].path);
@ -541,8 +477,7 @@ static int do_lua_kpathsea_lookup(lua_State * L, kpathsea kpse, int idx)
lua_pushstring(L, "format");
lua_gettable(L, idx + 1);
if (lua_type(L, -1) == LUA_TSTRING) {
int op = luaL_checkoption(L, -1, NULL, filetypenames);
user_format = filetypes[op];
user_format = luaL_checkoption(L, -1, NULL, filetypenames);
}
lua_pop(L, 1);
lua_pushstring(L, "dpi");
@ -615,14 +550,14 @@ static int do_lua_kpathsea_lookup(lua_State * L, kpathsea kpse, int idx)
if (lua_type(L, -1) == LUA_TSTRING) {
char *s = xstrdup(lua_tostring(L, -1));
str_list_add(&subdir_paths, s);
xfree(s);
free(s);
}
lua_pop(L, 1);
}
} else if (lua_type(L, -1) == LUA_TSTRING) {
char *s = xstrdup(lua_tostring(L, -1));
str_list_add(&subdir_paths, s);
xfree(s);
free(s);
}
lua_pop(L, 1);
if (STR_LIST_LENGTH(subdir_paths) > 0) {
@ -786,6 +721,25 @@ static int lua_kpathsea_init_prog(lua_State * L)
return 0;
}
static int lua_set_maketex(lua_State * L)
{
int ftype = luaL_checkoption(L, 1, NULL, filetypenames);
boolean enable = lua_toboolean(L, 2);
boolean src = luaL_checkoption(L, 3, "cmdline", srcnames);
kpse_set_program_enabled(ftype, enable, src);
return 0;
}
static int lua_kpathsea_set_maketex(lua_State * L)
{
kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
int ftype = luaL_checkoption(L, 2, NULL, filetypenames);
boolean enable = lua_toboolean(L, 3);
boolean src = luaL_checkoption(L, 4, "cmdline", srcnames);
kpathsea_set_program_enabled(*kp, ftype, enable, src);
return 0;
}
static int lua_kpse_version(lua_State * L)
{
lua_pushstring(L, kpathsea_version_string);
@ -862,6 +816,7 @@ static const struct luaL_Reg kpselib_m[] = {
{"var_value", lua_kpathsea_var_value},
{"show_path", lua_kpathsea_show_path},
{"lookup", lua_kpathsea_lookup},
{"set_maketex", lua_kpathsea_set_maketex},
{"version", lua_kpse_version},
{"default_texmfcnf", show_texmfcnf},
{"record_input_file", lua_record_input_file},
@ -881,6 +836,7 @@ static const struct luaL_Reg kpselib_l[] = {
{"var_value", var_value},
{"show_path", show_path},
{"lookup", lua_kpse_lookup},
{"set_maketex", lua_set_maketex},
{"version", lua_kpse_version},
{"default_texmfcnf", show_texmfcnf},
{NULL, NULL} /* sentinel */
@ -894,6 +850,5 @@ int luaopen_kpse(lua_State * L)
luaL_setfuncs(L, kpselib_m, 0);
lua_newtable(L);
luaL_setfuncs(L, kpselib_l, 0);
lua_setglobal(L, "kpse");
return 1;
}