diff --git a/source/texk/web2c/luatexdir/lua/liolibext.c b/source/texk/web2c/luatexdir/lua/liolibext.c index 43f1ea64871be14adcde77447dfaaaddee6df756..76284ba335b7d5786f204795f74b3218209de3d4 100644 --- a/source/texk/web2c/luatexdir/lua/liolibext.c +++ b/source/texk/web2c/luatexdir/lua/liolibext.c @@ -181,12 +181,10 @@ static int read2dot14(lua_State *L) { FILE *f = tofile(L); int a = getc(f); int b = getc(f); - int c = getc(f); - int d = getc(f); - if (d == EOF) { + if (b == EOF) { lua_pushnil(L); } else { - int n = (0x1000000 * a + 0x10000 * b + 0x100 * c + d); + int n = 0x100 * a + b ; lua_pushnumber(L, (n >> 14) + ((n & 0x3fff) / 16384.0)); } return 1; diff --git a/source/texk/web2c/luatexdir/lua/liolibext.c.orig b/source/texk/web2c/luatexdir/lua/liolibext.c.orig deleted file mode 100644 index 193110799b1bfbdc23f32f437bcc29dbaa5124a8..0000000000000000000000000000000000000000 --- a/source/texk/web2c/luatexdir/lua/liolibext.c.orig +++ /dev/null @@ -1,963 +0,0 @@ -/* liolibext.c - - Copyright 2014 Taco Hoekwater <taco@luatex.org> - - This file is part of 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 - Software Foundation; either version 2 of the License, or (at your - option) any later version. - - LuaTeX 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 Lesser General Public - License for more details. - - You should have received a copy of the GNU General Public License along - with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */ - -/* This extension only replaces one function: io.popen() and two - metatable entries: f:read() and f:lines() but unfortunately - it has to copy quite a bunch of lines from the original liolib.c - to do so. -*/ - -#include "ptexlib.h" -#include "lua/luatex-api.h" - - -#ifdef LuajitTeX -#include "lua/lauxlib_bridge.h" -#else -#include "lauxlib.h" -#endif -#include "lualib.h" - - - -/* -** {====================================================== -** lua_popen spawns a new process connected to the current -** one through the file streams. -** ======================================================= -*/ - -#if defined(_WIN32) - -#ifdef _MSC_VER -#define lua_popen(L,c,m) ((void)L, win32_popen(c,m)) -#define lua_pclose(L,file) ((void)L, win32_pclose(file)) -#else -#define lua_popen(L,c,m) ((void)L, _popen(c,m)) -#define lua_pclose(L,file) ((void)L, _pclose(file)) -#endif - -#else - -#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) -#define lua_pclose(L,file) ((void)L, pclose(file)) - -#endif - -/* }====================================================== */ - - -#if defined(LUA_USE_POSIX) - -#define l_fseek(f,o,w) fseeko(f,o,w) -#define l_ftell(f) ftello(f) -#define l_seeknum off_t - -#elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \ - && defined(_MSC_VER) && (_MSC_VER >= 1400) -/* Windows (but not DDK) and Visual C++ 2005 or higher */ - -#define l_fseek(f,o,w) _fseeki64(f,o,w) -#define l_ftell(f) _ftelli64(f) -#define l_seeknum __int64 - -#elif defined(__MINGW32__) - -#define l_fseek(f,o,w) fseeko64(f,o,w) -#define l_ftell(f) ftello64(f) -#define l_seeknum int64_t - -#else - -#define l_fseek(f,o,w) fseek(f,o,w) -#define l_ftell(f) ftell(f) -#define l_seeknum long - -#endif - -#define IO_PREFIX "_IO_" -#define IO_INPUT (IO_PREFIX "input") -#define IO_OUTPUT (IO_PREFIX "output") - -typedef luaL_Stream LStream; - -#define tolstream(L) ((LStream *)luaL_checkudata(L, 1, LUA_FILEHANDLE)) - -#define isclosed(p) ((p)->closef == NULL) - -static int io_type (lua_State *L) { - LStream *p; - luaL_checkany(L, 1); - p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); - if (p == NULL) - lua_pushnil(L); /* not a file */ - else if (isclosed(p)) - lua_pushliteral(L, "closed file"); - else - lua_pushliteral(L, "file"); - return 1; -} - - -static int f_tostring (lua_State *L) { - LStream *p = tolstream(L); - if (isclosed(p)) - lua_pushliteral(L, "file (closed)"); - else - lua_pushfstring(L, "file (%p)", p->f); - return 1; -} - -static FILE *tofile (lua_State *L) { -#ifdef LuajitTeX - /* see host/minilua.c */ - FILE**f=luaL_checkudata(L,1,LUA_FILEHANDLE); - if(*f==NULL) - luaL_error(L,"attempt to use a closed file"); - return*f; -#else - LStream *p = tolstream(L); - if (isclosed(p)) - luaL_error(L, "attempt to use a closed file"); - lua_assert(p->f); - return p->f; -#endif -} -/* -** When creating file handles, always creates a `closed' file handle -** before opening the actual file; so, if there is a memory error, the -** file is not left opened. -*/ -static LStream *newprefile (lua_State *L) { - LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); - p->closef = NULL; /* mark file handle as 'closed' */ - luaL_setmetatable(L, LUA_FILEHANDLE); - return p; -} - - -static int aux_close (lua_State *L) { - LStream *p = tolstream(L); - lua_CFunction cf = p->closef; - p->closef = NULL; /* mark stream as closed */ - return (*cf)(L); /* close it */ -} - - -static int io_close (lua_State *L) { - if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ - tofile(L); /* make sure argument is an open stream */ - return aux_close(L); -} - -static int f_gc (lua_State *L) { - LStream *p = tolstream(L); - if (!isclosed(p) && p->f != NULL) - aux_close(L); /* ignore closed and incompletely open files */ - return 0; -} - - -/* -** function to close regular files -*/ -static int io_fclose (lua_State *L) { - LStream *p = tolstream(L); - int res = fclose(p->f); - return luaL_fileresult(L, (res == 0), NULL); -} - - -static LStream *newfile (lua_State *L) { - LStream *p = newprefile(L); - p->f = NULL; - p->closef = &io_fclose; - return p; -} - - -static void opencheck (lua_State *L, const char *fname, const char *mode) { - LStream *p = newfile(L); - p->f = fopen(fname, mode); - if (p->f == NULL) { - luaL_error(L, "cannot open file " LUA_QS " (%s)", fname, strerror(errno)); - } else { -#ifdef WIN32 - _setmode (fileno (p->f), _O_BINARY); -#endif - if (mode[0]=='r') - recorder_record_input(fname); - else - recorder_record_output(fname); - } -} - - -static int io_open (lua_State *L) { - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newfile(L); - int i = 0; - /* check whether 'mode' matches '[rwa]%+?b?' */ - if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL && - (mode[i] != '+' || ++i) && /* skip if char is '+' */ - (mode[i] != 'b' || ++i) && /* skip if char is 'b' */ - (mode[i] == '\0'))) - return luaL_error(L, "invalid mode " LUA_QS - " (should match " LUA_QL("[rwa]%%+?b?") ")", mode); - p->f = fopen(filename, mode); - if (p->f == NULL) { - return luaL_fileresult(L, 0, filename) ; - } else { -#ifdef WIN32 - _setmode (fileno (p->f), _O_BINARY); -#endif - if (mode[0]=='r') - recorder_record_input(filename); - else - recorder_record_output(filename); - return 1; - } -} - -/* -** function to close 'popen' files -*/ -static int io_pclose (lua_State *L) { - LStream *p = tolstream(L); - return luaL_execresult(L, lua_pclose(L, p->f)); -} - -static int io_popen(lua_State * L) -{ - int ret = 1; - char *safecmd = NULL; - char *cmdname = NULL; - int allow = 0; - const char *filename = luaL_checkstring(L, 1); - const char *mode = luaL_optstring(L, 2, "r"); - LStream *p = newprefile(L); - - if (shellenabledp <= 0) { - lua_pushnil(L); - lua_pushliteral(L, "All command execution is disabled"); - return 2; - } - /* If restrictedshell == 0, any command is allowed. */ - if (restrictedshell == 0) - allow = 1; - else - allow = shell_cmd_is_allowed(filename, &safecmd, &cmdname); - - if (allow == 1) { - p->f = lua_popen(L, filename, mode); - p->closef = &io_pclose; - ret = (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; - } else if (allow == 2) { - p->f = lua_popen(L, safecmd, mode); - p->closef = &io_pclose; - ret = (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; - } else if (allow == 0) { - lua_pushnil(L); - lua_pushliteral(L, "Command execution disabled via shell_escape='p'"); - ret = 2; - } else { - lua_pushnil(L); - lua_pushliteral(L, "Bad command line quoting"); - ret = 2; - } - return ret; -} - - -static int io_tmpfile (lua_State *L) { - LStream *p = newfile(L); - p->f = tmpfile(); - return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; -} - - -static FILE *getiofile (lua_State *L, const char *findex) { - LStream *p; - lua_getfield(L, LUA_REGISTRYINDEX, findex); - p = (LStream *)lua_touserdata(L, -1); - if (isclosed(p)) - luaL_error(L, "standard %s file is closed", findex + strlen(IO_PREFIX)); - return p->f; -} - - -static int g_iofile (lua_State *L, const char *f, const char *mode) { - if (!lua_isnoneornil(L, 1)) { - const char *filename = lua_tostring(L, 1); - if (filename) - opencheck(L, filename, mode); - else { - tofile(L); /* check that it's a valid file handle */ - lua_pushvalue(L, 1); - } - lua_setfield(L, LUA_REGISTRYINDEX, f); - } - /* return current value */ - lua_getfield(L, LUA_REGISTRYINDEX, f); - return 1; -} - - -static int io_input (lua_State *L) { - return g_iofile(L, IO_INPUT, "r"); -} - - -static int io_output (lua_State *L) { - return g_iofile(L, IO_OUTPUT, "w"); -} - - -static int io_readline (lua_State *L); - - -static void aux_lines (lua_State *L, int toclose) { - int i; - int n = lua_gettop(L) - 1; /* number of arguments to read */ - /* ensure that arguments will fit here and into 'io_readline' stack */ - luaL_argcheck(L, n <= LUA_MINSTACK - 3, LUA_MINSTACK - 3, "too many options"); - lua_pushvalue(L, 1); /* file handle */ - lua_pushinteger(L, n); /* number of arguments to read */ - lua_pushboolean(L, toclose); /* close/not close file when finished */ - for (i = 1; i <= n; i++) lua_pushvalue(L, i + 1); /* copy arguments */ - lua_pushcclosure(L, io_readline, 3 + n); -} - - -static int f_lines (lua_State *L) { - tofile(L); /* check that it's a valid file handle */ - aux_lines(L, 0); - return 1; -} - - -static int io_lines (lua_State *L) { - int toclose; - if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ - if (lua_isnil(L, 1)) { /* no file name? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_INPUT); /* get default input */ - lua_replace(L, 1); /* put it at index 1 */ - tofile(L); /* check that it's a valid file handle */ - toclose = 0; /* do not close it after iteration */ - } - else { /* open a new file */ - const char *filename = luaL_checkstring(L, 1); - opencheck(L, filename, "r"); - lua_replace(L, 1); /* put file at index 1 */ - toclose = 1; /* close it after iteration */ - } - aux_lines(L, toclose); - return 1; -} - -/* -** {====================================================== -** READ -** ======================================================= -*/ - - -static int read_number (lua_State *L, FILE *f) { - lua_Number d; - if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { - lua_pushnumber(L, d); /* integer or float */ - return 1; - } - else { - lua_pushnil(L); /* "result" to be removed */ - return 0; /* read fails */ - } -} - - -static int test_eof (lua_State *L, FILE *f) { - int c = getc(f); - ungetc(c, f); - lua_pushlstring(L, NULL, 0); - return (c != EOF); -} - -/* this version does not care wether the file has - line endings using an 'alien' convention */ - -static int read_line(lua_State * L, FILE * f, int chop) -{ - luaL_Buffer buf; - int c, d; - luaL_buffinit(L, &buf); - while (1) { - c = fgetc(f); - if (c == EOF) { - luaL_pushresult(&buf); /* close buffer */ - return (lua_rawlen(L, -1) > 0); /* check whether read something */ - }; - if (c == '\n') { - if (!chop) - luaL_addchar(&buf, c); - break; - } else if (c == '\r') { - if (!chop) - luaL_addchar(&buf, c); - d = fgetc(f); - if (d != EOF && d != '\n') { - ungetc(d, f); - } else if (!chop) { - luaL_addchar(&buf, d); - } - break; - } else { - luaL_addchar(&buf, c); - } - } - luaL_pushresult(&buf); /* close buffer */ - return 1; -} - -#define MAX_SIZE_T (~(size_t)0) - -static void read_all (lua_State *L, FILE *f) { - size_t rlen = LUAL_BUFFERSIZE; /* how much to read in each cycle */ - l_seeknum old, nrlen = 0; /* for testing file size */ - luaL_Buffer b; - luaL_buffinit(L, &b); - /* speed up loading of not too large files: */ - old = l_ftell(f); - if ((l_fseek(f, 0, SEEK_END) >= 0) && - ((nrlen = l_ftell(f)) > 0) && nrlen < 1000 * 1000 * 100) { - rlen = nrlen; - } - l_fseek(f, old, SEEK_SET); - for (;;) { - char *p = luaL_prepbuffsize(&b, rlen); - size_t nr = fread(p, sizeof(char), rlen, f); - luaL_addsize(&b, nr); - if (nr < rlen) break; /* eof? */ - else if (rlen <= (MAX_SIZE_T / 4)) /* avoid buffers too large */ - rlen *= 2; /* double buffer size at each iteration */ - } - luaL_pushresult(&b); /* close buffer */ -} - - -static int read_chars (lua_State *L, FILE *f, size_t n) { - size_t nr; /* number of chars actually read */ - char *p; - luaL_Buffer b; - luaL_buffinit(L, &b); - p = luaL_prepbuffsize(&b, n); /* prepare buffer to read whole block */ - nr = fread(p, sizeof(char), n, f); /* try to read 'n' chars */ - luaL_addsize(&b, nr); - luaL_pushresult(&b); /* close buffer */ - return (nr > 0); /* true iff read something */ -} - - -static int g_read (lua_State *L, FILE *f, int first) { - int nargs = lua_gettop(L) - 1; - int success; - int n; - clearerr(f); - if (nargs == 0) { /* no arguments? */ - success = read_line(L, f, 1); - n = first+1; /* to return 1 result */ - } - else { /* ensure stack space for all results and for auxlib's buffer */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); - success = 1; - for (n = first; nargs-- && success; n++) { - if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)lua_tointeger(L, n); - success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); - } - else { - const char *p = lua_tostring(L, n); - luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); - switch (p[1]) { - case 'n': /* number */ - success = read_number(L, f); - break; - case 'l': /* line */ - success = read_line(L, f, 1); - break; - case 'L': /* line with end-of-line */ - success = read_line(L, f, 0); - break; - case 'a': /* file */ - read_all(L, f); /* read entire file */ - success = 1; /* always success */ - break; - default: - return luaL_argerror(L, n, "invalid format"); - } - } - } - } - if (ferror(f)) - return luaL_fileresult(L, 0, NULL); - if (!success) { - lua_pop(L, 1); /* remove last result */ - lua_pushnil(L); /* push nil instead */ - } - return n - first; -} - - -static int io_read (lua_State *L) { - return g_read(L, getiofile(L, IO_INPUT), 1); -} - -static int f_read (lua_State *L) { - return g_read(L, tofile(L), 2); -} - - -static int io_readline (lua_State *L) { - LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); - int i; - int n = (int)lua_tointeger(L, lua_upvalueindex(2)); - if (isclosed(p)) /* file is already closed? */ - return luaL_error(L, "file is already closed"); - lua_settop(L , 1); - for (i = 1; i <= n; i++) /* push arguments to 'g_read' */ - lua_pushvalue(L, lua_upvalueindex(3 + i)); - n = g_read(L, p->f, 2); /* 'n' is number of results */ - lua_assert(n > 0); /* should return at least a nil */ - if (!lua_isnil(L, -n)) /* read at least one value? */ - return n; /* return them */ - else { /* first result is nil: EOF or error */ - if (n > 1) { /* is there error information? */ - /* 2nd result is error message */ - return luaL_error(L, "%s", lua_tostring(L, -n + 1)); - } - if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ - lua_settop(L, 0); - lua_pushvalue(L, lua_upvalueindex(1)); - aux_close(L); /* close it */ - } - return 0; - } -} - -/* }====================================================== */ - - -static int g_write (lua_State *L, FILE *f, int arg) { - int nargs = lua_gettop(L) - arg; - int status = 1; - for (; nargs--; arg++) { - if (lua_type(L, arg) == LUA_TNUMBER) { - /* optimization: could be done exactly as for strings */ - status = status && - fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; - } - else { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - status = status && (fwrite(s, sizeof(char), l, f) == l); - } - } - if (status) return 1; /* file handle already on stack top */ - else return luaL_fileresult(L, status, NULL); -} - - -static int io_write (lua_State *L) { - return g_write(L, getiofile(L, IO_OUTPUT), 1); -} - - -static int f_write (lua_State *L) { - FILE *f = tofile(L); - lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */ - return g_write(L, f, 2); -} - - -static int f_seek (lua_State *L) { - static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; - static const char *const modenames[] = {"set", "cur", "end", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, "cur", modenames); - lua_Number p3 = luaL_optnumber(L, 3, 0); - l_seeknum offset = (l_seeknum)p3; - luaL_argcheck(L, (lua_Number)offset == p3, 3, - "not an integer in proper range"); - op = l_fseek(f, offset, mode[op]); - if (op) - return luaL_fileresult(L, 0, NULL); /* error */ - else { - lua_pushinteger(L, (lua_Number)l_ftell(f)); - return 1; - } -} - - -static int f_setvbuf (lua_State *L) { - int res; - static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; - static const char *const modenames[] = {"no", "full", "line", NULL}; - FILE *f = tofile(L); - int op = luaL_checkoption(L, 2, NULL, modenames); - lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - if(sz == 0) - res = setvbuf(f, NULL, _IONBF, sz); - else - res = setvbuf(f, NULL, mode[op], sz); - return luaL_fileresult(L, res == 0, NULL); -} - - - -static int io_flush (lua_State *L) { - return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); -} - - -static int f_flush (lua_State *L) { - return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); -} - -/* a few helpers to avoid reading numbers as strings */ - -static int readcardinal1(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - if (a == EOF) - lua_pushnil(L); - else - lua_pushinteger(L, a); - return 1; -} - -static int readcardinal2(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - if (b == EOF) - lua_pushnil(L); - else - lua_pushinteger(L, 0x100 * a + b); - return 1; -} - -static int readcardinal3(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - int c = getc(f); - if (c == EOF) - lua_pushnil(L); - else - lua_pushinteger(L, 0x10000 * a + 0x100 * b + c); - return 1; -} - -static int readcardinal4(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - int c = getc(f); - int d = getc(f); - if (d == EOF) - lua_pushnil(L); - else - lua_pushinteger(L,0x1000000 * a + 0x10000 * b + 0x100 * c + d); - return 1; -} - -static int readinteger1(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - if (a == EOF) - lua_pushnil(L); - else if (a >= 0x80) - lua_pushinteger(L, a - 0xFF - 1); - else - lua_pushinteger(L, a); - return 1; -} - -static int readinteger2(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - if (b == EOF) - lua_pushnil(L); - else if (a >= 0x80) - lua_pushinteger(L, 0x100 * a + b - 0x10000); - else - lua_pushinteger(L, 0x100 * a + b); - return 1; -} - -static int readinteger3(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - int c = getc(f); - if (c == EOF) - lua_pushnil(L); - else if (a >= 0x80) - lua_pushinteger(L, 0x10000 * a + 0x100 * b + c - 0x1000000); - else - lua_pushinteger(L, 0x10000 * a + 0x100 * b + c); - return 1; -} - -static int readinteger4(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - int c = getc(f); - int d = getc(f); - if (d == EOF) - lua_pushnil(L); - else if (a >= 0x80) - lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000); - else - lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d); - return 1; -} - -static int readfixed2(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - if (b == EOF) - lua_pushnil(L); - else if (a >= 0x80) - lua_pushinteger(L, a + b/0xFFFF - 0x100); - else - lua_pushinteger(L, a + b/0xFFFF); - return 1; -} - -static int readfixed4(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - int c = getc(f); - int d = getc(f); - if (d == EOF) - lua_pushnil(L); - else if (a >= 0x80) - lua_pushnumber(L, (0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000)/65536.0); - else - lua_pushnumber(L, (0x1000000 * a + 0x10000 * b + 0x100 * c + d)/65536.0); - return 1; -} - -static int read2dot14(lua_State *L) { - FILE *f = tofile(L); - int a = getc(f); - int b = getc(f); - int c = getc(f); - int d = getc(f); - if (d == EOF) { - lua_pushnil(L); - } else { - int n = (0x1000000 * a + 0x10000 * b + 0x100 * c + d); - lua_pushnumber(L, (n >> 14) + ((n & 0x3fff) / 16384.0)); - } - return 1; -} - -static int getposition(lua_State *L) { - FILE *f = tofile(L); - long p = ftell(f); - if (p<0) - lua_pushnil(L); - else - lua_pushinteger(L, p); - return 1; -} - -static int setposition(lua_State *L) { - FILE *f = tofile(L); - long p = lua_tointeger(L,2); - p = fseek(f,p,SEEK_SET); - if (p<0) - lua_pushnil(L); - else - lua_pushinteger(L, p); - return 1; -} - -static int skipposition(lua_State *L) { - FILE *f = tofile(L); - long p = lua_tointeger(L,2); - p = fseek(f,ftell(f)+p,SEEK_SET); - if (p<0) - lua_pushnil(L); - else - lua_pushinteger(L, p); - return 1; -} - -static int readbytetable(lua_State *L) { - FILE *f = tofile(L); - int n = lua_tointeger(L,2); - int i ; - lua_createtable(L, n, 0); - for (i=1;i<=n;i++) { - int a = getc(f); - if (a == EOF) { - break; - } else { - /* - lua_pushinteger(L, i); - lua_pushinteger(L, a); - lua_rawset(L, -3); - */ - lua_pushinteger(L, a); - lua_rawseti(L,-2,i); - } - } - return 1; -} - -static int readbytes(lua_State *L) { - FILE *f = tofile(L); - int n = lua_tointeger(L,2); - int i = 0; - for (i=1;i<=n;i++) { - int a = getc(f); - if (a == EOF) { - return i-1; - } else { - lua_pushinteger(L, a); - } - } - return n; -} - -static const luaL_Reg fiolib[] = { - /* helpers */ - {"readcardinal1",readcardinal1}, - {"readcardinal2",readcardinal2}, - {"readcardinal3",readcardinal3}, - {"readcardinal4",readcardinal4}, - {"readinteger1",readinteger1}, - {"readinteger2",readinteger2}, - {"readinteger3",readinteger3}, - {"readinteger4",readinteger4}, - {"readfixed2",readfixed2}, - {"readfixed4",readfixed4}, - {"read2dot14",read2dot14}, - {"setposition",setposition}, - {"getposition",getposition}, - {"skipposition",skipposition}, - {"readbytes",readbytes}, - {"readbytetable",readbytetable}, - /* done */ - {NULL, NULL} -}; - -/* functions for 'io' library */ - -static const luaL_Reg iolib[] = { - {"close", io_close}, - {"flush", io_flush}, - {"input", io_input}, - {"lines", io_lines}, - {"open", io_open}, - {"output", io_output}, - {"popen", io_popen}, - {"read", io_read}, - {"tmpfile", io_tmpfile}, - {"type", io_type}, - {"write", io_write}, - /* done */ - {NULL, NULL} -}; - - -/* - Methods for file handles. -*/ - -static const luaL_Reg flib[] = { - {"close", io_close}, - {"flush", f_flush}, - {"lines", f_lines}, - {"read", f_read}, - {"seek", f_seek}, - {"setvbuf", f_setvbuf}, - {"write", f_write}, - {"__gc", f_gc}, - {"__tostring", f_tostring}, - {NULL, NULL} -}; - - -#ifndef LuajitTeX -static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ - lua_pushvalue(L, -1); /* push metatable */ - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ - lua_pop(L, 1); /* pop new metatable */ -} - -/* - a function to (not) close the standard files stdin, stdout, and stderr -*/ - -static int io_noclose (lua_State *L) { - LStream *p = tolstream(L); - p->closef = &io_noclose; /* keep file opened */ - lua_pushnil(L); - lua_pushliteral(L, "cannot close standard file"); - return 2; -} - -static void createstdfile (lua_State *L, FILE *f, const char *k, - const char *fname) { - LStream *p = newprefile(L); - p->f = f; - p->closef = &io_noclose; - if (k != NULL) { - lua_pushvalue(L, -1); - lua_setfield(L, LUA_REGISTRYINDEX, k); /* add file to registry */ - } - lua_setfield(L, -2, fname); /* add file to module */ -} -#endif - -int open_iolibext (lua_State *L) { - luaL_register(L, "fio", fiolib); -#ifdef LuajitTeX - return luaopen_io(L); -#else - luaL_newlib(L, iolib); /* new module */ - createmeta(L); - /* create (and set) default files */ - createstdfile(L, stdin, IO_INPUT, "stdin"); - createstdfile(L, stdout, IO_OUTPUT, "stdout"); - createstdfile(L, stderr, NULL, "stderr"); - return 1; -#endif -} diff --git a/source/texk/web2c/luatexdir/lua/lstrlibext.c b/source/texk/web2c/luatexdir/lua/lstrlibext.c index 8a474fd09c96f2626a6b723d0904cbfa198222ac..f9bd2177d9910da253634e43230b9a5344c2eaeb 100644 --- a/source/texk/web2c/luatexdir/lua/lstrlibext.c +++ b/source/texk/web2c/luatexdir/lua/lstrlibext.c @@ -1,5 +1,5 @@ /* lstrlibext.c - + Copyright 2012 Taco Hoekwater <taco@luatex.org> This file is part of LuaTeX. @@ -92,7 +92,7 @@ static int str_split (lua_State *L) { } free(orig); return 1; -} +} static int characters_aux (lua_State *L) { size_t ls; @@ -152,7 +152,7 @@ static int utfcharacters_aux (lua_State *L) { } } return utf_failed(L,ind+1); /* we found a follow byte! */ -} +} static int str_utfcharacters (lua_State *L) { @@ -180,7 +180,7 @@ static int utfvalues_aux (lua_State *L) { if (i<0x80) { v = i; } else if (i>=0xF0) { - if ((ind+3)<(int)ls && ((unsigned)*(s+ind+1))>=0x80 + if ((ind+3)<(int)ls && ((unsigned)*(s+ind+1))>=0x80 && ((unsigned)*(s+ind+2))>=0x80 && ((unsigned)*(s+ind+3))>=0x80) { numbytes = 4; j = ((unsigned)*(s+ind+1))-128; @@ -237,7 +237,7 @@ static int characterpairs_aux (lua_State *L) { b[0] = *(s+ind); b[1] = 0; lua_pushlstring(L, b, 1); if (ind+1<(int)ls) { - b[0] = *(s+ind+1); + b[0] = *(s+ind+1); lua_pushlstring(L, b, 1); } else { lua_pushlstring(L, b+1, 0); @@ -350,7 +350,17 @@ static int str_dump (lua_State *L) { return 1; } - +static int str_bytetable (lua_State *L) { + size_t l; + int i; + const char *s = luaL_checklstring(L, 1, &l); + lua_createtable(L,l,0); + for (i=0;i<l;i++) { + lua_pushinteger(L,(unsigned char)*(s+i)); + lua_rawseti(L,-2,i+1); + } + return 1; +} static const luaL_Reg strlibext[] = { {"utfvalues", str_utfvalues}, @@ -359,6 +369,7 @@ static const luaL_Reg strlibext[] = { {"characterpairs", str_characterpairs}, {"bytes", str_bytes}, {"bytepairs", str_bytepairs}, + {"bytetable", str_bytetable}, {"explode", str_split}, {"dump", str_dump}, {NULL, NULL} @@ -374,4 +385,3 @@ void open_strlibext(lua_State * L) } lua_pop(L,1); } - diff --git a/source/texk/web2c/luatexdir/lua/ltokenlib.c.old b/source/texk/web2c/luatexdir/lua/ltokenlib.c.old deleted file mode 100644 index 89fd1d87aa647806464433ab234583d9dcc27fb8..0000000000000000000000000000000000000000 --- a/source/texk/web2c/luatexdir/lua/ltokenlib.c.old +++ /dev/null @@ -1,290 +0,0 @@ -/* ltokenlib.c - - Copyright 2006-2012 Taco Hoekwater <taco@luatex.org> - - This file is part of 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 - Software Foundation; either version 2 of the License, or (at your - option) any later version. - - LuaTeX 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 Lesser General Public - License for more details. - - You should have received a copy of the GNU General Public License along - with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */ - -/* THIS IS THE OLD ONE, ONLY AS REFERENCE. WILL BE REMOVED */ - -#include "ptexlib.h" -#include "lua/luatex-api.h" - - -#define is_valid_token(L,i) (lua_istable(L,i) && lua_rawlen(L,i)==3) -#define get_token_cmd(L,i) lua_rawgeti(L,i,1) -#define get_token_chr(L,i) lua_rawgeti(L,i,2) -#define get_token_cs(L,i) lua_rawgeti(L,i,3) -#define is_active_string(s) (strlen((char *)s)>3 && *s==0xEF && *(s+1)==0xBF && *(s+2)==0xBF) - - -static unsigned char *get_cs_text(int cs) -{ - if (cs == null_cs) - return (unsigned char *) xstrdup("\\csname\\endcsname"); - else if ((cs_text(cs) < 0) || (cs_text(cs) >= str_ptr)) - return (unsigned char *) xstrdup(""); - else - return (unsigned char *) makecstring(cs_text(cs)); -} - - -static int test_expandable(lua_State * L) -{ - int cmd = -1; - int t; - if (is_valid_token(L, -1)) { - get_token_cmd(L, -1); - t = lua_type(L, -1); - if (t == LUA_TNUMBER) { - cmd = (int) lua_tointeger(L, -1); - } else if (t == LUA_TSTRING) { - cmd = get_command_id(lua_tostring(L, -1)); - } - if (cmd > max_command_cmd) { - lua_pushboolean(L, 1); - } else { - lua_pushboolean(L, 0); - } - } else { - lua_pushnil(L); - } - return 1; -} - - -static int test_protected(lua_State * L) -{ - int chr = -1; - int t; - if (is_valid_token(L, -1)) { - get_token_chr(L, -1); - t = lua_type(L, -1); - if (t == LUA_TNUMBER) { - chr = (int) lua_tointeger(L, -1); - } else if (t == LUA_TSTRING) { - chr = get_command_id(lua_tostring(L, -1)); - } - if (token_info(token_link(chr)) == protected_token) { - lua_pushboolean(L, 1); - } else { - lua_pushboolean(L, 0); - } - } else { - lua_pushnil(L); - } - return 1; -} - -static int test_activechar(lua_State * L) -{ - if (is_valid_token(L, -1)) { - unsigned char *s; - int cs = 0; - get_token_cs(L, -1); - if (lua_type(L, -1) == LUA_TNUMBER) { - cs = (int) lua_tointeger(L, -1); - } - lua_pop(L, 1); - if (cs != 0 && ((s = get_cs_text(cs)) != (unsigned char *) NULL)) { - if (is_active_string(s)) { - free(s); - lua_pushboolean(L, 1); - return 1; - } - free(s); - } - } - lua_pushboolean(L, 0); - return 1; -} - - -static int run_get_command_name(lua_State * L) -{ - int cs; - if (is_valid_token(L, -1)) { - get_token_cmd(L, -1); - if (lua_type(L, -1) == LUA_TNUMBER) { - cs = (int) lua_tointeger(L, -1); - lua_pushstring(L, command_names[cs].cmd_name); - } else { - lua_pushstring(L, ""); - } - } else { - lua_pushnil(L); - } - return 1; -} - - -static int run_get_csname_name(lua_State * L) -{ - int cs /*, cmd*/; - unsigned char *s; - if (is_valid_token(L, -1)) { - get_token_cmd(L, -1); - /* - if (lua_type(L, -1) == LUA_TNUMBER) { - cmd = (int) lua_tointeger(L, -1); - } - */ - lua_pop(L, 1); - cs = 0; - get_token_cs(L, -1); - if (lua_type(L, -1) == LUA_TNUMBER) { - cs = (int) lua_tointeger(L, -1); - } - lua_pop(L, 1); - - if (cs != 0 && ((s = get_cs_text(cs)) != (unsigned char *) NULL)) { - if (is_active_string(s)) - lua_pushstring(L, (char *) (s + 3)); - else - lua_pushstring(L, (char *) s); - } else { - lua_pushstring(L, ""); - } - } else { - lua_pushnil(L); - } - return 1; -} - -static int run_get_command_id(lua_State * L) -{ - int cs = -1; - if (lua_type(L, -1) == LUA_TSTRING) { - cs = get_command_id(lua_tostring(L, -1)); - } - lua_pushnumber(L, cs); - return 1; -} - - -static int run_get_csname_id(lua_State * L) -{ - const char *s; - size_t k, cs = 0; - if (lua_type(L, -1) == LUA_TSTRING) { - s = lua_tolstring(L, -1, &k); - cs = (size_t) string_lookup(s, k); - } - lua_pushnumber(L, (lua_Number) cs); - return 1; -} - - -void make_token_table(lua_State * L, int cmd, int chr, int cs) -{ - lua_createtable(L, 3, 0); - lua_pushnumber(L, cmd); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, chr); - lua_rawseti(L, -2, 2); - lua_pushnumber(L, cs); - lua_rawseti(L, -2, 3); -} - -static int run_get_next(lua_State * L) -{ - int save_nncs; - save_nncs = no_new_control_sequence; - no_new_control_sequence = 0; - get_next(); - no_new_control_sequence = save_nncs; - make_token_table(L, cur_cmd, cur_chr, cur_cs); - return 1; -} - -static int run_expand(lua_State * L) -{ - (void) L; - expand(); - return 0; -} - - -static int run_lookup(lua_State * L) -{ - const char *s; - size_t l; - int cs, cmd, chr; - int save_nncs; - if (lua_type(L, -1) == LUA_TSTRING) { - s = lua_tolstring(L, -1, &l); - if (l > 0) { - save_nncs = no_new_control_sequence; - no_new_control_sequence = true; - cs = id_lookup((last + 1), (int) l); /* cleans up the lookup buffer */ - cs = string_lookup(s, l); - cmd = eq_type(cs); - chr = equiv(cs); - make_token_table(L, cmd, chr, cs); - no_new_control_sequence = save_nncs; - return 1; - } - } - lua_newtable(L); - return 1; -} - -static int run_build(lua_State * L) -{ - int cmd, chr, cs; - if (lua_type(L, 1) == LUA_TNUMBER) { - cs = 0; - chr = (int) lua_tointeger(L, 1); - cmd = (int) luaL_optinteger(L, 2, get_char_cat_code(chr)); - if (cmd == 0 || cmd == 9 || cmd == 14 || cmd == 15) { - fprintf(stdout, - "\n\nluatex error: not a good token.\nCatcode %i can not be returned, so I replaced it by 12 (other)", - (int) cmd); - error(); - cmd = 12; - } - if (cmd == 13) { - cs = active_to_cs(chr, false); - cmd = eq_type(cs); - chr = equiv(cs); - } - make_token_table(L, cmd, chr, cs); - return 1; - } else { - return run_lookup(L); - } -} - - -static const struct luaL_Reg tokenlib[] = { - {"get_next", run_get_next}, - {"expand", run_expand}, - {"lookup", run_lookup}, - {"create", run_build}, - {"is_expandable", test_expandable}, - {"is_activechar", test_activechar}, - {"is_protected", test_protected}, - {"csname_id", run_get_csname_id}, - {"csname_name", run_get_csname_name}, - {"command_name", run_get_command_name}, - {"command_id", run_get_command_id}, - {NULL, NULL} /* sentinel */ -}; - -int luaopen_oldtoken(lua_State * L) -{ - luaL_register(L, "oldtoken", tokenlib); - return 1; -} diff --git a/source/texk/web2c/luatexdir/lua/luajitstuff.w.old b/source/texk/web2c/luatexdir/lua/luajitstuff.w.old deleted file mode 100644 index d8165b723258c47b5700e7ad915259be7f759336..0000000000000000000000000000000000000000 --- a/source/texk/web2c/luatexdir/lua/luajitstuff.w.old +++ /dev/null @@ -1,660 +0,0 @@ -% luastuff.w -% -% Copyright 2006-2013 Taco Hoekwater <taco@@luatex.org> -% -% This file is part of 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 -% Software Foundation; either version 2 of the License, or (at your -% option) any later version. -% -% LuaTeX 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 Lesser General Public -% License for more details. -% -% You should have received a copy of the GNU General Public License along -% with LuaTeX; if not, see <http://www.gnu.org/licenses/>. - -@ @c -#include "ptexlib.h" -#include "lua/luatex-api.h" -#include "lua/lauxlib_bridge.h" - - -@ @c -lua_State *Luas = NULL; - -int luastate_bytes = 0; -int lua_active = 0; - -@ @c -void make_table(lua_State * L, const char *tab, const char *mttab, const char *getfunc, - const char *setfunc) -{ - /* make the table *//* |[{<tex>}]| */ - lua_pushstring(L, tab); /* |[{<tex>},"dimen"]| */ - lua_newtable(L); /* |[{<tex>},"dimen",{}]| */ - lua_settable(L, -3); /* |[{<tex>}]| */ - /* fetch it back */ - lua_pushstring(L, tab); /* |[{<tex>},"dimen"]| */ - lua_gettable(L, -2); /* |[{<tex>},{<dimen>}]| */ - /* make the meta entries */ - luaL_newmetatable(L, mttab); /* |[{<tex>},{<dimen>},{<dimen_m>}]| */ - lua_pushstring(L, "__index"); /* |[{<tex>},{<dimen>},{<dimen_m>},"__index"]| */ - lua_pushstring(L, getfunc); /* |[{<tex>},{<dimen>},{<dimen_m>},"__index","getdimen"]| */ - lua_gettable(L, -5); /* |[{<tex>},{<dimen>},{<dimen_m>},"__index",<tex.getdimen>]| */ - lua_settable(L, -3); /* |[{<tex>},{<dimen>},{<dimen_m>}]| */ - lua_pushstring(L, "__newindex"); /* |[{<tex>},{<dimen>},{<dimen_m>},"__newindex"]| */ - lua_pushstring(L, setfunc); /* |[{<tex>},{<dimen>},{<dimen_m>},"__newindex","setdimen"]| */ - lua_gettable(L, -5); /* |[{<tex>},{<dimen>},{<dimen_m>},"__newindex",<tex.setdimen>]| */ - lua_settable(L, -3); /* |[{<tex>},{<dimen>},{<dimen_m>}]| */ - lua_setmetatable(L, -2); /* |[{<tex>},{<dimen>}]| : assign the metatable */ - lua_pop(L, 1); /* |[{<tex>}]| : clean the stack */ -} - -@ @c -static const char *getS(lua_State * L, void *ud, size_t * size) -{ - LoadS *ls = (LoadS *) ud; - (void) L; - if (ls->size == 0) - return NULL; - *size = ls->size; - ls->size = 0; - return ls->s; -} - -@ @c -#if 0 /* currently unused */ -static void *my_luaalloc(void *ud, void *ptr, size_t osize, size_t nsize) -{ - void *ret = NULL; - (void) ud; /* for -Wunused */ - if (nsize == 0) - free(ptr); - else - ret = realloc(ptr, nsize); - luastate_bytes += (int) (nsize - osize); - return ret; -} -#endif - -@ @c -static int my_luapanic(lua_State * L) -{ - (void) L; /* to avoid warnings */ - fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); - return 0; -} - -@ @c -void luafunctioncall(int slot) -{ - int i ; - int stacktop = lua_gettop(Luas); - lua_active++; - lua_rawgeti(Luas, LUA_REGISTRYINDEX, lua_key_index(lua_functions)); - lua_gettable(Luas, LUA_REGISTRYINDEX); - lua_rawgeti(Luas, -1,slot); - if (lua_isfunction(Luas,-1)) { - int base = lua_gettop(Luas); /* function index */ - lua_pushinteger(Luas, slot); - lua_pushcfunction(Luas, lua_traceback); /* push traceback function */ - lua_insert(Luas, base); /* put it under chunk */ - i = lua_pcall(Luas, 1, 0, base); - lua_remove(Luas, base); /* remove traceback function */ - if (i != 0) { - lua_gc(Luas, LUA_GCCOLLECT, 0); - Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1)); - } - } - lua_settop(Luas,stacktop); - lua_active--; -} - -@ @c -static const luaL_Reg lualibs[] = { - {"", luaopen_base}, - {"package", luaopen_package}, - /*{"coroutine", luaopen_coroutine},*/ - {"table", luaopen_table}, - {"io", luaopen_io}, - {"fio", open_iolibext}, - {"os", luaopen_os}, - {"string", luaopen_string}, - {"math", luaopen_math}, - {"debug", luaopen_debug}, - {"unicode", luaopen_unicode}, - {"zip", luaopen_zip}, - {"bit32", luaopen_bit32}, - {"md5", luaopen_md5}, - {"lfs", luaopen_lfs}, - {"profiler", luaopen_profiler}, - {"jit", luaopen_jit}, - {"ffi", luaopen_ffi}, - {"bit", luaopen_bit }, - {NULL, NULL} -}; - -static const luaL_Reg lualibs_nofenv[] = { - {"lpeg", luaopen_lpeg}, - {NULL, NULL} -}; - -@ @c -static void do_openlibs(lua_State * L) -{ - const luaL_Reg *lib = lualibs; - for (; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); - } - lib = lualibs_nofenv; - for (; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_newtable(L); - lua_setfenv(L,-2); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); - } -} - - -@ @c -static int luatex_loadfile (lua_State *L) { - int status = 0; - const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); - /*int env = !lua_isnone(L, 3);*/ /* 'env' parameter? */ - if (!lua_only && !fname && interaction == batch_mode) { - lua_pushnil(L); - lua_pushstring(L, "reading from stdin is disabled in batch mode"); - return 2; /* return nil plus error message */ - } - status = luaL_loadfilex(L, fname, mode); - if (status == LUA_OK) { - recorder_record_input(fname); - /* if (env) { *//* 'env' parameter? */ - /* lua_pushvalue(L, 3); */ - /* lua_setupvalue(L, -2, 1); *//* set it as 1st upvalue of loaded chunk */ - /*} */ - } - return RESERVED_load_aux_JIT(L, status,3); -} - -@ @c -static int luatex_dofile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - int n = lua_gettop(L); - if (!lua_only && !fname) { - if (interaction == batch_mode) { - lua_pushnil(L); - lua_pushstring(L, "reading from stdin is disabled in batch mode"); - return 2; /* return nil plus error message */ - } else { - tprint_nl("lua> "); - } - } - if (luaL_loadfile(L, fname) != 0) lua_error(L); - recorder_record_input(fname); - lua_call(L, 0, LUA_MULTRET); - return lua_gettop(L) - n; -} - -@ @c -void luainterpreter(void) -{ - lua_State *L; - - if (jithash_hashname==NULL){ - /* default lua51 */ - luajittex_choose_hash_function = 0; - jithash_hashname = (char *) xmalloc(strlen("lua51")+1); - jithash_hashname = strcpy ( jithash_hashname, "lua51"); - } else { - if (strcmp((const char*)jithash_hashname,"lua51")==0){ - luajittex_choose_hash_function = 0; - }else if (strcmp((const char*)jithash_hashname,"luajit20")==0){ - luajittex_choose_hash_function = 1; - } else { - /* default lua51 */ - luajittex_choose_hash_function = 0; - jithash_hashname = strcpy ( jithash_hashname, "lua51"); - } - } - - L = luaL_newstate() ; - /*L = lua_newstate(my_luaalloc, NULL);*/ - if (L == NULL) { - fprintf(stderr, "Can't create the Lua state.\n"); - return; - } - lua_atpanic(L, &my_luapanic); - - do_openlibs(L); /* does all the 'simple' libraries */ - - if (luajiton){ - luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_ON); - } - else { - luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_OFF); - } - - lua_pushcfunction(L,luatex_dofile); - lua_setglobal(L, "dofile"); - lua_pushcfunction(L,luatex_loadfile); - lua_setglobal(L, "loadfile"); - - luatex_md5_lua_open(L); - - open_oslibext(L); - /* open_iolibext(L); */ - open_strlibext(L); - open_lfslibext(L); - - /* - The socket and mime libraries are a bit tricky to open because they use a load-time - dependency that has to be worked around for luatex, where the C module is loaded - way before the lua module. - */ - - if (!nosocket_option) { - - /* todo: move this to common */ - - lua_getglobal(L, "package"); - lua_getfield(L, -1, "loaded"); - if (!lua_istable(L, -1)) { - lua_newtable(L); - lua_setfield(L, -2, "loaded"); - lua_getfield(L, -1, "loaded"); - } - luaopen_socket_core(L); - lua_setfield(L, -2, "socket.core"); - lua_pushnil(L); - lua_setfield(L, -2, "socket"); /* package.loaded.socket = nil */ - - luaopen_mime_core(L); - lua_setfield(L, -2, "mime.core"); - lua_pushnil(L); - lua_setfield(L, -2, "mime"); /* package.loaded.mime = nil */ - lua_pop(L, 2); /* pop the tables */ - - luatex_socketlua_open(L); /* preload the pure lua modules */ - } - /* zlib. slightly odd calling convention */ - luaopen_zlib(L); - lua_setglobal(L, "zlib"); - luaopen_gzip(L); - - /* our own libraries */ - luaopen_ff(L); - luaopen_tex(L); - luaopen_token(L); - luaopen_node(L); - luaopen_texio(L); - luaopen_kpse(L); - luaopen_callback(L); - luaopen_lua(L, startup_filename); - luaopen_stats(L); - luaopen_font(L); - luaopen_lang(L); - luaopen_mplib(L); - luaopen_vf(L); - - /* |luaopen_pdf(L);| */ - /* environment table at |LUA_ENVIRONINDEX| needs to load this way: */ - lua_pushcfunction(L, luaopen_pdf); - lua_pushstring(L, "pdf"); - lua_call(L, 1, 0); - - if (!lua_only) { - /* |luaopen_img(L);| */ - lua_pushcfunction(L, luaopen_img); - lua_pushstring(L, "img"); - lua_call(L, 1, 0); - } - - /* |luaopen_epdf(L);| */ - lua_pushcfunction(L, luaopen_epdf); - lua_pushstring(L, "epdf"); - lua_call(L, 1, 0); - - /* |luaopen_pdfscanner(L);| */ - lua_pushcfunction(L, luaopen_pdfscanner); - lua_pushstring(L, "pdfscanner"); - lua_call(L, 1, 0); - - lua_createtable(L, 0, 0); - lua_setglobal(L, "texconfig"); - - Luas = L; -} - -@ @c -int hide_lua_table(lua_State * L, const char *name) -{ - int r = 0; - lua_getglobal(L, name); - if (lua_istable(L, -1)) { - r = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushnil(L); - lua_setglobal(L, name); - } - return r; -} - -@ @c -void unhide_lua_table(lua_State * L, const char *name, int r) -{ - lua_rawgeti(L, LUA_REGISTRYINDEX, r); - lua_setglobal(L, name); - luaL_unref(L, LUA_REGISTRYINDEX, r); -} - -@ @c -int hide_lua_value(lua_State * L, const char *name, const char *item) -{ - int r = 0; - lua_getglobal(L, name); - if (lua_istable(L, -1)) { - lua_getfield(L, -1, item); - r = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushnil(L); - lua_setfield(L, -2, item); - } - return r; -} - -@ @c -void unhide_lua_value(lua_State * L, const char *name, const char *item, int r) -{ - lua_getglobal(L, name); - if (lua_istable(L, -1)) { - lua_rawgeti(L, LUA_REGISTRYINDEX, r); - lua_setfield(L, -2, item); - luaL_unref(L, LUA_REGISTRYINDEX, r); - } -} - -@ @c -int lua_traceback(lua_State * L) -{ - lua_getglobal(L, "debug"); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - return 1; - } - lua_getfield(L, -1, "traceback"); - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); - return 1; - } - lua_pushvalue(L, 1); /* pass error message */ - lua_pushinteger(L, 2); /* skip this function and traceback */ - lua_call(L, 2, 1); /* call debug.traceback */ - return 1; -} - -@ @c -static void luacall(int p, int nameptr, boolean is_string) /* hh-ls: optimized lua_id resolving */ -{ - LoadS ls; - int i; - size_t ll = 0; - char *lua_id; - char *s = NULL; - - if (Luas == NULL) { - luainterpreter(); - } - lua_active++; - if (is_string) { - const char *ss = NULL; - lua_rawgeti(Luas, LUA_REGISTRYINDEX, p); - if (lua_isfunction(Luas,-1)) { - int base = lua_gettop(Luas); /* function index */ - lua_checkstack(Luas, 1); - lua_pushcfunction(Luas, lua_traceback); /* push traceback function */ - lua_insert(Luas, base); /* put it under chunk */ - i = lua_pcall(Luas, 0, 0, base); - lua_remove(Luas, base); /* remove traceback function */ - if (i != 0) { - lua_gc(Luas, LUA_GCCOLLECT, 0); - Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1)); - } - lua_active--; - return ; - } - ss = lua_tolstring(Luas, -1, &ll); - s = xmalloc(ll+1); - memcpy(s,ss,ll+1); - lua_pop(Luas,1); - } else { - int l = 0; - s = tokenlist_to_cstring(p, 1, &l); - ll = (size_t)l; - } - ls.s = s; - ls.size = ll; - if (ls.size > 0) { - if (nameptr > 0) { - int l = 0; /* not used */ - lua_id = tokenlist_to_cstring(nameptr, 1, &l); - i = lua_load(Luas, getS, &ls, lua_id); - xfree(lua_id); - } else if (nameptr < 0) { - lua_id = get_lua_name((nameptr + 65536)); - if (lua_id != NULL) { - i = lua_load(Luas, getS, &ls, lua_id); - } else { - i = lua_load(Luas, getS, &ls, "=[\\latelua]"); - } - } else { - i = lua_load(Luas, getS, &ls, "=[\\latelua]"); - } - if (i != 0) { - Luas = luatex_error(Luas, (i == LUA_ERRSYNTAX ? 0 : 1)); - } else { - int base = lua_gettop(Luas); /* function index */ - lua_checkstack(Luas, 1); - lua_pushcfunction(Luas, lua_traceback); /* push traceback function */ - lua_insert(Luas, base); /* put it under chunk */ - i = lua_pcall(Luas, 0, 0, base); - lua_remove(Luas, base); /* remove traceback function */ - if (i != 0) { - lua_gc(Luas, LUA_GCCOLLECT, 0); - Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1)); - } - } - xfree(ls.s); - } - lua_active--; -} - -@ @c -void late_lua(PDF pdf, halfword p) -{ - (void) pdf; - if (late_lua_type(p)==normal) { - expand_macros_in_tokenlist(p); /* sets |def_ref| */ - luacall(def_ref, late_lua_name(p), false); - flush_list(def_ref); - } else { - luacall(late_lua_data(p), late_lua_name(p), true); - } -} - -@ @c -void luatokencall(int p, int nameptr) /* hh-ls: optimized lua_id resolving */ -{ - LoadS ls; - int i, l; - char *s = NULL; - char *lua_id; - assert(Luas); - l = 0; - lua_active++; - s = tokenlist_to_cstring(p, 1, &l); - ls.s = s; - ls.size = (size_t) l; - if (ls.size > 0) { - if (nameptr > 0) { - lua_id = tokenlist_to_cstring(nameptr, 1, &l); - i = lua_load(Luas, getS, &ls, lua_id); - xfree(lua_id); - } else if (nameptr < 0) { - lua_id = get_lua_name((nameptr + 65536)); - if (lua_id != NULL) { - i = lua_load(Luas, getS, &ls, lua_id); - } else { - i = lua_load(Luas, getS, &ls, "=[\\directlua]"); - } - } else { - i = lua_load(Luas, getS, &ls, "=[\\directlua]"); - } - xfree(s); - if (i != 0) { - Luas = luatex_error(Luas, (i == LUA_ERRSYNTAX ? 0 : 1)); - } else { - int base = lua_gettop(Luas); /* function index */ - lua_checkstack(Luas, 1); - lua_pushcfunction(Luas, lua_traceback); /* push traceback function */ - lua_insert(Luas, base); /* put it under chunk */ - i = lua_pcall(Luas, 0, 0, base); - lua_remove(Luas, base); /* remove traceback function */ - if (i != 0) { - lua_gc(Luas, LUA_GCCOLLECT, 0); - Luas = luatex_error(Luas, (i == LUA_ERRRUN ? 0 : 1)); - } - } - } - lua_active--; -} - -@ @c -lua_State *luatex_error(lua_State * L, int is_fatal) -{ - - const_lstring luaerr; - char *err = NULL; - if (lua_type(L, -1) == LUA_TSTRING) { - luaerr.s = lua_tolstring(L, -1, &luaerr.l); - /* free last one ? */ - err = (char *) xmalloc((unsigned) (luaerr.l + 1)); - snprintf(err, (luaerr.l + 1), "%s", luaerr.s); - last_lua_error = err; /* hm, what if we have several .. not freed */ - } - if (is_fatal > 0) { - /* Normally a memory error from lua. - The pool may overflow during the |maketexlstring()|, but we - are crashing anyway so we may as well abort on the pool size */ - normal_error("lua",err); - /* never reached */ - lua_close(L); - return (lua_State *) NULL; - } else { - normal_warning("lua",err); - return L; - } -} - -@ @c -void preset_environment(lua_State * L, const parm_struct * p, const char *s) -{ - int i; - assert(L != NULL); - /* double call with same s gives assert(0) */ - lua_pushstring(L, s); /* s */ - lua_gettable(L, LUA_REGISTRYINDEX); /* t */ - assert(lua_isnil(L, -1)); - lua_pop(L, 1); /* - */ - lua_pushstring(L, s); /* s */ - lua_newtable(L); /* t s */ - for (i = 1, ++p; p->name != NULL; i++, p++) { - assert(i == p->idx); - lua_pushstring(L, p->name); /* k t s */ - lua_pushinteger(L, p->idx); /* v k t s */ - lua_settable(L, -3); /* t s */ - } - lua_settable(L, LUA_REGISTRYINDEX); /* - */ -} - -@ @c -/* - int luaL_typerror (void *LL, int narg, const char *tname) - { - lua_State *L = (lua_State *)LL; - const char *msg = lua_pushfstring(L, "%s expected, got %s", - tname, luaL_typename(L, narg)); - return luaL_argerror(L, narg, msg); - } -*/ - -/* - Compatibility layer for luatex lua5.2 -*/ - -@ @c -LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { - void *p = lua_touserdata(L, ud); - if (p != NULL) { /* value is a userdata? */ - if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ - luaL_getmetatable(L, tname); /* get correct metatable */ - if (!lua_rawequal(L, -1, -2)) /* not the same? */ - p = NULL; /* value is a userdata with wrong metatable */ - lua_pop(L, 2); /* remove both metatables */ - return p; - } - } - return NULL; /* value is not a userdata with a metatable */ -} - -@ @c - -/* It's not ok. See lua-users.org/wiki/CompatibilityWithLuaFive for another solution */ - -LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { - /*luaL_checkversion(L);*/ - luaL_checkstack(L, nup, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_setfield(L, -(nup + 2), l->name); - } - lua_pop(L, nup); /* remove upvalues */ -} - -@ @c -LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { - lua_State *L = B->L; - if (sz > LUAL_BUFFERSIZE ) - luaL_error(L, "buffer too large"); - return luaL_prepbuffer(B) ; -} - -@ @c -LUA_API int lua_compare (lua_State *L, int o1, int o2, int op) { - /*StkId o1, o2;*/ - int i = 0; - lua_lock(L); /* may call tag method */ - /* o1 = index2addr(L, index1); */ - /* o2 = index2addr(L, index2); */ - /*if (isvalid(o1) && isvalid(o2)) {*/ - switch (op) { - case LUA_OPEQ: i = lua_equal(L, o1, o2); break; - case LUA_OPLT: i = lua_lessthan(L, o1, o2); break; - case LUA_OPLE: i = (lua_lessthan(L, o1, o2) || lua_equal(L, o1, o2)) ; break; - default: luaL_error(L, "invalid option"); - } - /*}*/ - lua_unlock(L); - return i; -} -