From 7cafeaa1fde93dbefa40971343bf9a3bb92f2ede Mon Sep 17 00:00:00 2001 From: Warner Losh Date: Mon, 12 Feb 2018 15:31:53 +0000 Subject: [PATCH] Add Lua as a scripting langauge to /boot/loader liblua glues the lua run time into the boot loader. It implements all the runtime routines that lua expects. In addition, it has a few standard 'C' headers that nueter various aspects of the LUA build that are too specific to lua to be in libsa. Many refinements from the original code to improve implementation and the number of included lua libraries. Use int64_t for lua_Number. Have "/boot/lua" be the default module path. Numerous cleanups from the original GSoC project, including hacking libsa to allow lua to be built with only one change outside luaconf.h. Add the final bit of lua glue to bring in liblua and plug into the multiple interpreter framework, previously committed. Add LOADER_LUA option, currently off by default. Presently, this is an experimental option. One must opt-in to using this by defining WITH_LOADER_LUA and WITHOUT_FORTH. It's been lightly tested, so keep a backup copy of your old loader handy. The menu code, coming in the next commit, hasn't been exhaustively tested. A LUA boot loader is 60k larger than a FORTH one, which is 80k larger than a no-interpreter one. Subtle changes in size may tip things past some subtle limit (the binary is ~430k now when built with LUA). A future version may offer coexistance. Bump FreeBSD version to 1200058 to mark the milestone. Pedro Souza's 2014 Summer of Code project. Rui Paulo, Pedro Arthur, Zakary Nafziger and Wojciech A. Koszek also contributed. Warner Losh reworked it extensively into its current form. Obtained from: https://wiki.freebsd.org/SummerOfCode2014/LuaLoader Sponsored by: Google Summer of Code Relnotes: Yes MFC After: 1 month Differential Review: https://reviews.freebsd.org/D14295 --- UPDATING | 8 + share/mk/src.opts.mk | 1 + stand/Makefile | 3 + stand/Makefile.amd64 | 3 + stand/common/interp_lua.c | 164 ++++++ stand/defs.mk | 2 + stand/liblua/Makefile | 38 ++ stand/liblua/float.h | 32 ++ stand/liblua/locale.h | 27 + stand/liblua/lstd.c | 182 +++++++ stand/liblua/lstd.h | 74 +++ stand/liblua/luaconf.h | 811 ++++++++++++++++++++++++++++ stand/liblua/lutils.c | 268 +++++++++ stand/liblua/lutils.h | 32 ++ stand/liblua/math.h | 38 ++ stand/liblua32/Makefile | 5 + stand/loader.mk | 18 +- stand/lua.mk | 12 + sys/sys/param.h | 2 +- tools/build/options/WITH_LOADER_LUA | 2 + 20 files changed, 1717 insertions(+), 5 deletions(-) create mode 100644 stand/common/interp_lua.c create mode 100644 stand/liblua/Makefile create mode 100644 stand/liblua/float.h create mode 100644 stand/liblua/locale.h create mode 100644 stand/liblua/lstd.c create mode 100644 stand/liblua/lstd.h create mode 100644 stand/liblua/luaconf.h create mode 100644 stand/liblua/lutils.c create mode 100644 stand/liblua/lutils.h create mode 100644 stand/liblua/math.h create mode 100644 stand/liblua32/Makefile create mode 100644 stand/lua.mk create mode 100644 tools/build/options/WITH_LOADER_LUA diff --git a/UPDATING b/UPDATING index 4e4da15417ba..56da3c70a638 100644 --- a/UPDATING +++ b/UPDATING @@ -51,6 +51,14 @@ NOTE TO PEOPLE WHO THINK THAT FreeBSD 12.x IS SLOW: ****************************** SPECIAL WARNING: ****************************** +20180212: + FreeBSD boot loader enhanced with Lua scripting. It's purely opt-in for + now by building WITH_LOADER_LUA and WITHOUT_FORTH in /etc/src.conf. + Co-existance for the transition period will come shortly. Booting is a + complex environment and test coverage for Lua-enabled loaders has been + thin, so it would be prudent to assume it might not work and make + provisions for backup boot methods. + 20180211: devmatch functionality has been turned on in devd. It will automatically load drivers for unattached devices. This may cause unexpected drivers to diff --git a/share/mk/src.opts.mk b/share/mk/src.opts.mk index 425157b031ee..e4f558525ddb 100644 --- a/share/mk/src.opts.mk +++ b/share/mk/src.opts.mk @@ -184,6 +184,7 @@ __DEFAULT_NO_OPTIONS = \ LIBSOFT \ LOADER_FIREWIRE \ LOADER_FORCE_LE \ + LOADER_LUA \ NAND \ OFED \ OPENLDAP \ diff --git a/stand/Makefile b/stand/Makefile index 523418965871..ede7dd5d51e6 100644 --- a/stand/Makefile +++ b/stand/Makefile @@ -8,6 +8,9 @@ SUBDIR+= libsa SUBDIR+= ficl SUBDIR+= forth .endif +.if ${MK_LOADER_LUA} != "no" +SUBDIR+= liblua +.endif SUBDIR+= man diff --git a/stand/Makefile.amd64 b/stand/Makefile.amd64 index 6e8c967378cb..2b742143fb33 100644 --- a/stand/Makefile.amd64 +++ b/stand/Makefile.amd64 @@ -7,6 +7,9 @@ SUBDIR+= zfs zfs32 .if ${MK_FORTH} != "no" SUBDIR+= ficl32 .endif +.if ${MK_LOADER_LUA} != "no" +SUBDIR+= liblua32 +.endif SUBDIR+= efi SUBDIR+= userboot diff --git a/stand/common/interp_lua.c b/stand/common/interp_lua.c new file mode 100644 index 000000000000..383d96ab09d9 --- /dev/null +++ b/stand/common/interp_lua.c @@ -0,0 +1,164 @@ +/*- + * Copyright (c) 2011 Wojciech A. Koszek + * Copyright (c) 2014 Pedro Souza + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include "bootstrap.h" + +#define lua_c + +#include "lstd.h" + +#include +#include +#include +#include +#include + +struct interp_lua_softc { + lua_State *luap; +}; + +static struct interp_lua_softc lua_softc; + +#ifdef LUA_DEBUG +#define LDBG(...) do { \ + printf("%s(%d): ", __func__, __LINE__); \ + printf(__VA_ARGS__); \ + printf("\n"); \ +} while (0) +#else +#define LDBG(...) +#endif + + +static void * +interp_lua_realloc(void *ud __unused, void *ptr, size_t osize __unused, size_t nsize) +{ + + if (nsize == 0) { + free(ptr); + return NULL; + } + return realloc(ptr, nsize); +} + +/* + * The libraries commented out below either lack the proper + * support from libsa, or they are unlikely to be useful + * in the bootloader, so have been commented out. + */ +static const luaL_Reg loadedlibs[] = { + {"_G", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, +// {LUA_COLIBNAME, luaopen_coroutine}, +// {LUA_TABLIBNAME, luaopen_table}, + {LUA_STRLIBNAME, luaopen_string}, +// {LUA_IOLIBNAME, luaopen_io}, +// {LUA_OSLIBNAME, luaopen_os}, +// {LUA_MATHLIBNAME, luaopen_math}, +// {LUA_UTF8LIBNAME, luaopen_utf8}, +// {LUA_DBLIBNAME, luaopen_debug}, + {NULL, NULL} +}; + +void +interp_init(void) +{ + lua_State *luap; + struct interp_lua_softc *softc = &lua_softc; + const char *filename; + const luaL_Reg *lib; + + setenv("script.lang", "lua", 1); + LDBG("creating context"); + + luap = lua_newstate(interp_lua_realloc, NULL); + if (luap == NULL) { + printf("problem initializing the Lua interpreter\n"); + abort(); + } + softc->luap = luap; + register_utils(luap); + + /* "require" functions from 'loadedlibs' and set results to global table */ + for (lib = loadedlibs; lib->func; lib++) { + luaL_requiref(luap, lib->name, lib->func, 1); + lua_pop(luap, 1); /* remove lib */ + } + + filename = "/boot/lua/loader.lua"; + if (interp_include(filename) != 0) { + const char *errstr = lua_tostring(luap, -1); + errstr = errstr == NULL ? "unknown" : errstr; + printf("Startup errorr in %s:\nLUA ERROR: %s.\n", filename, errstr); + lua_pop(luap, 1); + } +} + +int +interp_run(const char *line) +{ + int argc; + char **argv; + lua_State *luap; + struct interp_lua_softc *softc = &lua_softc; + int status; + + luap = softc->luap; + LDBG("executing line..."); + if ((status = luaL_dostring(luap, line)) != 0) { + /* + * If we could not parse the line as Lua syntax, + * try parsing it as a loader command. + */ + lua_pop(luap, 1); + if (parse(&argc, &argv, line) == 0) { + status = interp_builtin_cmd(argc, argv); + if (status != CMD_OK) + printf("Command failed\n"); + free(argv); + } else { + printf("Failed to parse \'%s\'\n", line); + status = -1; + } + } + + return (status == 0 ? CMD_OK : CMD_ERROR); +} + +int +interp_include(const char *filename) +{ + struct interp_lua_softc *softc = &lua_softc; + + LDBG("loading file %s", filename); + + return (luaL_dofile(softc->luap, filename)); +} diff --git a/stand/defs.mk b/stand/defs.mk index 3b646c61d9b9..eefc764e0ea6 100644 --- a/stand/defs.mk +++ b/stand/defs.mk @@ -23,6 +23,8 @@ EFIINCMD= ${EFIINC}/${MACHINE} FDTSRC= ${BOOTSRC}/fdt FICLSRC= ${BOOTSRC}/ficl LDRSRC= ${BOOTSRC}/common +LIBLUASRC= ${BOOTSRC}/liblua +LUASRC= ${SRCTOP}/contrib/lua/src SASRC= ${BOOTSRC}/libsa SYSDIR= ${SRCTOP}/sys UBOOTSRC= ${BOOTSRC}/uboot diff --git a/stand/liblua/Makefile b/stand/liblua/Makefile new file mode 100644 index 000000000000..2b9db13f5c95 --- /dev/null +++ b/stand/liblua/Makefile @@ -0,0 +1,38 @@ +# $FreeBSD$ + +.include + +.PATH: ${LUASRC} +.PATH: ${LIBLUASRC} + +LIB= lua +INTERNALLIB= + +# Core Lua. +SRCS= lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c \ + lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c \ + ltm.c lundump.c lvm.c lzio.c +SRCS+= lauxlib.c lbaselib.c lstrlib.c loadlib.c + +# These aren't yet included, but link now, omitting them saves 15k +#SRCS+= lcorolib.c ldblib.c lutf8lib.c + +# These aren't yet compatible with the boot environment, and some may never be +#SRCS+= lbitlib.c liolib.c lmathlib.c loslib.c ltablib.c + +# Our utilities. +SRCS+= lstd.c lutils.c + +WARNS= 3 + +CFLAGS+= -DLUA_FLOAT_TYPE=LUA_FLOAT_INT64 +CFLAGS+= -DLUA_PATH_DEFAULT=\"/boot/lua/\?.lua\" +CFLAGS+= -ffreestanding -nostdlib -DBOOT_LUA -DLUA_USE_POSIX +CFLAGS+= -fno-stack-protector -D__BSD_VISIBLE +CFLAGS+= -I${BOOTSRC}/include -I${LIBLUASRC} -I${LUASRC} -I${LDRSRC} + +.if ${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 0 +CFLAGS+= -fPIC +.endif + +.include diff --git a/stand/liblua/float.h b/stand/liblua/float.h new file mode 100644 index 000000000000..fa6995113807 --- /dev/null +++ b/stand/liblua/float.h @@ -0,0 +1,32 @@ +/*- + * Copyright (c) 2018 M Warner Losh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * Empty file to keep lua build happy. Lua expects to have to include float.h + * but the int64_t number implementation doesn't need it. + */ diff --git a/stand/liblua/locale.h b/stand/liblua/locale.h new file mode 100644 index 000000000000..787dbd73c352 --- /dev/null +++ b/stand/liblua/locale.h @@ -0,0 +1,27 @@ +/*- + * Copyright (c) 2018 M Warner Losh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ diff --git a/stand/liblua/lstd.c b/stand/liblua/lstd.c new file mode 100644 index 000000000000..59f3f9746668 --- /dev/null +++ b/stand/liblua/lstd.c @@ -0,0 +1,182 @@ +/*- + * Copyright (c) 2014 Pedro Souza + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "lstd.h" +#include "math.h" + +FILE * +fopen(const char *filename, const char *mode) +{ + struct stat st; + int fd; + FILE *f; + + if (mode == NULL || mode[0] != 'r') + return NULL; + + fd = open(filename, O_RDONLY); + if (fd < 0) + return NULL; + + f = malloc(sizeof(FILE)); + if (f == NULL) { + close(fd); + return NULL; + } + + if (fstat(fd, &st) != 0) { + free(f); + close(fd); + return (NULL); + } + + f->fd = fd; + f->offset = 0; + f->size = st.st_size; + + return (f); +} + + +FILE * +freopen(const char *filename, const char *mode, FILE *stream) +{ + fclose(stream); + return (fopen(filename, mode)); +} + +size_t +fread(void *ptr, size_t size, size_t count, FILE *stream) +{ + size_t r; + + if (stream == NULL) + return 0; + r = (size_t)read(stream->fd, ptr, size * count); + stream->offset += r; + + return (r); +} + +int +fclose(FILE *stream) +{ + if (stream == NULL) + return EOF; + close(stream->fd); + free(stream); + + return (0); +} + +int +ferror(FILE *stream) +{ + + return (stream == NULL || stream->fd < 0); +} + +int +feof(FILE *stream) +{ + + if (stream == NULL) + return 1; + + return (stream->offset >= stream->size); +} + +int +getc(FILE *stream) +{ + char ch; + size_t r; + + if (stream == NULL) + return EOF; + r = read(stream->fd, &ch, 1); + if (r == 1) + return ch; + return EOF; +} + +void +luai_writestring(const char *s, int i) +{ + + while (i-- > 0) + putchar(*s++); +} + +/* + * These routines from here on down are to implement the lua math + * library, but that's not presently included by default. They are + * little more than placeholders to allow compilation due to linkage + * issues with upstream Lua. + */ + +int64_t +lstd_pow(int64_t x, int64_t y) +{ + int64_t rv = 1; + + if (y < 0) + return 0; + rv = x; + while (--y) + rv *= x; + + return rv; +} + +int64_t +lstd_floor(int64_t x) +{ + + return (x); +} + +int64_t +lstd_fmod(int64_t a, int64_t b) +{ + + return (a % b); +} + +/* + * This can't be implemented, so maybe it should just abort. + */ +int64_t +lstd_frexp(int64_t a, int *y) +{ + *y = 0; + + return 0; +} diff --git a/stand/liblua/lstd.h b/stand/liblua/lstd.h new file mode 100644 index 000000000000..09e330a7aff9 --- /dev/null +++ b/stand/liblua/lstd.h @@ -0,0 +1,74 @@ +/*- + * Copyright (c) 2014 Pedro Souza + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef LSTD_H +#define LSTD_H + +#include +#include +#include +#include +#include +#include + +typedef struct FILE +{ + int fd; + size_t offset; + size_t size; +} FILE; + +FILE *fopen(const char *filename, const char *mode); +FILE *freopen( const char *filename, const char *mode, FILE *stream); +size_t fread(void *ptr, size_t size, size_t count, FILE *stream); +int fclose(FILE *stream); +int ferror(FILE *stream); +int feof(FILE *stream); +int getc(FILE * stream); + +#ifndef EOF +#define EOF (-1) +#endif + +#define stdin ((FILE*)NULL) +#define stdout 1 + +#ifndef BUFSIZ +#define BUFSIZ 512 +#endif + +#define lua_writestringerror(s, p) do { printf((s), (p)); } while (0) + +void luai_writestring(const char *, int); + +#define lua_writestring(s,l) luai_writestring(s,l) + +#define fflush /* */ +#define fgets(b, l, s) fgetstr((b), (l), 0) + +#endif /* LSTD_H */ diff --git a/stand/liblua/luaconf.h b/stand/liblua/luaconf.h new file mode 100644 index 000000000000..4cbc14f5e9f4 --- /dev/null +++ b/stand/liblua/luaconf.h @@ -0,0 +1,811 @@ +/* $FreeBSD$ */ +/* +** $Id: luaconf.h,v 1.259 2016/12/22 13:08:50 roberto Exp $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef luaconf_h +#define luaconf_h + +#include +#include + + +/* +** =================================================================== +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +** {==================================================================== +** System Configuration: macros to adapt (if needed) Lua to some +** particular platform, for instance compiling it with 32-bit numbers or +** restricting it to C89. +** ===================================================================== +*/ + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You +** can also define LUA_32BITS in the make file, but changing here you +** ensure that all software connected to Lua will be compiled with the +** same configuration. +*/ +/* #define LUA_32BITS */ + + +/* +@@ LUA_USE_C89 controls the use of non-ISO-C89 features. +** Define it if you want Lua to avoid the use of a few C99 features +** or Windows-specific features on Windows. +*/ +/* #define LUA_USE_C89 */ + + +/* +** By default, Lua on Windows use (some) specific Windows features +*/ +#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ +#endif + + +#if defined(LUA_USE_WINDOWS) +#define LUA_DL_DLL /* enable support for DLL */ +#define LUA_USE_C89 /* broadly, Windows is C89 */ +#endif + + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_USE_READLINE /* needs some extra libraries */ +#endif + + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#define LUA_USE_READLINE /* needs an extra library: -lreadline */ +#endif + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS +#endif + + + +/* +@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. +*/ +/* avoid undefined shifts */ +#if ((INT_MAX >> 15) >> 15) >= 1 +#define LUAI_BITSINT 32 +#else +/* 'int' always must have at least 16 bits */ +#define LUAI_BITSINT 16 +#endif + + +/* +@@ LUA_INT_TYPE defines the type for Lua integers. +@@ LUA_FLOAT_TYPE defines the type for Lua floats. +** Lua should work fine with any mix of these options (if supported +** by your C compiler). The usual configurations are 64-bit integers +** and 'double' (the default), 32-bit integers and 'float' (for +** restricted platforms), and 'long'/'double' (for C compilers not +** compliant with C99, which may not have support for 'long long'). +*/ + +/* predefined options for LUA_INT_TYPE */ +#define LUA_INT_INT 1 +#define LUA_INT_LONG 2 +#define LUA_INT_LONGLONG 3 + +/* predefined options for LUA_FLOAT_TYPE */ +#define LUA_FLOAT_FLOAT 1 +#define LUA_FLOAT_DOUBLE 2 +#define LUA_FLOAT_LONGDOUBLE 3 +#define LUA_FLOAT_INT64 4 + +#if defined(LUA_32BITS) /* { */ +/* +** 32-bit integers and 'float' +*/ +#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ +#define LUA_INT_TYPE LUA_INT_INT +#else /* otherwise use 'long' */ +#define LUA_INT_TYPE LUA_INT_LONG +#endif +#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT + +#elif defined(LUA_C89_NUMBERS) /* }{ */ +/* +** largest types available for C89 ('long' and 'double') +*/ +#define LUA_INT_TYPE LUA_INT_LONG +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE + +#endif /* } */ + + +/* +** default configuration for 64-bit Lua ('long long' and 'double') +*/ +#if !defined(LUA_INT_TYPE) +#define LUA_INT_TYPE LUA_INT_LONGLONG +#endif + +#if !defined(LUA_FLOAT_TYPE) +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE +#endif + +/* }================================================================== */ + + + + +/* +** {================================================================== +** Configuration for Paths. +** =================================================================== +*/ + +/* +** LUA_PATH_SEP is the character that separates templates in a path. +** LUA_PATH_MARK is the string that marks the substitution points in a +** template. +** LUA_EXEC_DIR in a Windows path is replaced by the executable's +** directory. +*/ +#define LUA_PATH_SEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXEC_DIR "!" + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +** Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +** C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#if defined(_WIN32) /* { */ +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + ".\\?.lua;" ".\\?\\init.lua" +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" + +#else /* }{ */ + +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" +#ifndef LUA_PATH_DEFAULT +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + "./?.lua;" "./?/init.lua" +#endif +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif /* } */ + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Marks for exported symbols in the C code +** =================================================================== +*/ + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all auxiliary library functions. +@@ LUAMOD_API is a mark for all standard library opening functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) /* { */ + +#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ +#define LUA_API __declspec(dllexport) +#else /* }{ */ +#define LUA_API __declspec(dllimport) +#endif /* } */ + +#else /* }{ */ + +#define LUA_API extern + +#endif /* } */ + + +/* more often than not the libs go together with the core */ +#define LUALIB_API LUA_API +#define LUAMOD_API LUALIB_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +** exported to outside modules. +@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables +** that are not to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. Not all elf targets support +** this attribute. Unfortunately, gcc does not offer a way to check +** whether the target offers that support, and those without support +** give a warning about it. To avoid these warnings, change to the +** default definition. +*/ +#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) /* { */ +#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + +#define LUAI_DDEC LUAI_FUNC +#define LUAI_DDEF /* empty */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Compatibility with previous versions +** =================================================================== +*/ + +/* +@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. +@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. +** You can define it to get all options, or change specific options +** to fit your specific needs. +*/ +#if defined(LUA_COMPAT_5_2) /* { */ + +/* +@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated +** functions in the mathematical library. +*/ +#define LUA_COMPAT_MATHLIB + +/* +@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. +*/ +#define LUA_COMPAT_BITLIB + +/* +@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. +*/ +#define LUA_COMPAT_IPAIRS + +/* +@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for +** manipulating other integer types (lua_pushunsigned, lua_tounsigned, +** luaL_checkint, luaL_checklong, etc.) +*/ +#define LUA_COMPAT_APIINTCASTS + +#endif /* } */ + + +#if defined(LUA_COMPAT_5_1) /* { */ + +/* Incompatibilities from 5.2 -> 5.3 */ +#define LUA_COMPAT_MATHLIB +#define LUA_COMPAT_APIINTCASTS + +/* +@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. +** You can replace it with 'table.unpack'. +*/ +#define LUA_COMPAT_UNPACK + +/* +@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. +** You can replace it with 'package.searchers'. +*/ +#define LUA_COMPAT_LOADERS + +/* +@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. +** You can call your C function directly (with light C functions). +*/ +#define lua_cpcall(L,f,u) \ + (lua_pushcfunction(L, (f)), \ + lua_pushlightuserdata(L,(u)), \ + lua_pcall(L,1,0,0)) + + +/* +@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. +** You can rewrite 'log10(x)' as 'log(x, 10)'. +*/ +#define LUA_COMPAT_LOG10 + +/* +@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base +** library. You can rewrite 'loadstring(s)' as 'load(s)'. +*/ +#define LUA_COMPAT_LOADSTRING + +/* +@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. +*/ +#define LUA_COMPAT_MAXN + +/* +@@ The following macros supply trivial compatibility for some +** changes in the API. The macros themselves document how to +** change your code to avoid using them. +*/ +#define lua_strlen(L,i) lua_rawlen(L, (i)) + +#define lua_objlen(L,i) lua_rawlen(L, (i)) + +#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) + +/* +@@ LUA_COMPAT_MODULE controls compatibility with previous +** module functions 'module' (Lua) and 'luaL_register' (C). +*/ +#define LUA_COMPAT_MODULE + +#endif /* } */ + + +/* +@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a +@@ a float mark ('.0'). +** This macro is not on by default even in compatibility mode, +** because this is not really an incompatibility. +*/ +/* #define LUA_COMPAT_FLOATSTRING */ + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Numbers. +** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* +** satisfy your needs. +** =================================================================== +*/ + +/* +@@ LUA_NUMBER is the floating-point type used by Lua. +@@ LUAI_UACNUMBER is the result of a 'default argument promotion' +@@ over a floating number. +@@ l_mathlim(x) corrects limit name 'x' to the proper float type +** by prefixing it with one of FLT/DBL/LDBL. +@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. +@@ LUA_NUMBER_FMT is the format for writing floats. +@@ lua_number2str converts a float to a string. +@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. +@@ l_floor takes the floor of a float. +@@ lua_str2number converts a decimal numeric string to a number. +*/ + + +/* The following definitions are good for most cases here */ + +#define l_floor(x) (l_mathop(floor)(x)) + +#define lua_number2str(s,sz,n) \ + l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) + +/* +@@ lua_numbertointeger converts a float number to an integer, or +** returns 0 if float is not within the range of a lua_Integer. +** (The range comparisons are tricky because of rounding. The tests +** here assume a two-complement representation, where MININTEGER always +** has an exact representation as a float; MAXINTEGER may not have one, +** and therefore its conversion to float may have an ill-defined value.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + +/* now the variable definitions */ + +#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ + +#define LUA_NUMBER float + +#define l_mathlim(n) (FLT_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.7g" + +#define l_mathop(op) op##f + +#define lua_str2number(s,p) strtof((s), (p)) + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ + +#define LUA_NUMBER long double + +#define l_mathlim(n) (LDBL_##n) + +#define LUAI_UACNUMBER long double + +#define LUA_NUMBER_FRMLEN "L" +#define LUA_NUMBER_FMT "%.19Lg" + +#define l_mathop(op) op##l + +#define lua_str2number(s,p) strtold((s), (p)) + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ + +#define LUA_NUMBER double + +#define l_mathlim(n) (DBL_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.14g" + +#define l_mathop(op) op + +#define lua_str2number(s,p) strtod((s), (p)) + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_INT64 /* }{ int64 */ + +#include "lstd.h" + +#include + +#define panic lua_panic +/* Hack to use int64 as the LUA_NUMBER from ZFS code, kinda */ + +#define LUA_NUMBER int64_t + +#define l_mathlim(n) (LUA_FLOAT_INT_HACK_##n) +#define LUA_FLOAT_INT_HACK_MANT_DIG 32 +#define LUA_FLOAT_INT_HACK_MAX_10_EXP 32 + +#define LUAI_UACNUMBER int64_t + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%" PRId64 + +#define l_mathop(x) (lstd_ ## x) + +#define lua_str2number(s,p) strtoll((s), (p), 0) + +#define lua_getlocaledecpoint() '.' + +#else /* }{ */ + +#error "numeric float type not defined" + +#endif /* } */ + + + +/* +@@ LUA_INTEGER is the integer type used by Lua. +** +@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. +** +@@ LUAI_UACINT is the result of a 'default argument promotion' +@@ over a lUA_INTEGER. +@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. +@@ LUA_INTEGER_FMT is the format for writing integers. +@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. +@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. +@@ lua_integer2str converts an integer to a string. +*/ + + +/* The following definitions are good for most cases here */ + +#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" + +#define LUAI_UACINT LUA_INTEGER + +#define lua_integer2str(s,sz,n) \ + l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) + +/* +** use LUAI_UACINT here to avoid problems with promotions (which +** can turn a comparison between unsigneds into a signed comparison) +*/ +#define LUA_UNSIGNED unsigned LUAI_UACINT + + +/* now the variable definitions */ + +#if LUA_INT_TYPE == LUA_INT_INT /* { int */ + +#define LUA_INTEGER int +#define LUA_INTEGER_FRMLEN "" + +#define LUA_MAXINTEGER INT_MAX +#define LUA_MININTEGER INT_MIN + +#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ + +#define LUA_INTEGER long +#define LUA_INTEGER_FRMLEN "l" + +#define LUA_MAXINTEGER LONG_MAX +#define LUA_MININTEGER LONG_MIN + +#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ + +/* use presence of macro LLONG_MAX as proxy for C99 compliance */ +#if defined(LLONG_MAX) /* { */ +/* use ISO C99 stuff */ + +#define LUA_INTEGER long long +#define LUA_INTEGER_FRMLEN "ll" + +#define LUA_MAXINTEGER LLONG_MAX +#define LUA_MININTEGER LLONG_MIN + +#elif defined(LUA_USE_WINDOWS) /* }{ */ +/* in Windows, can use specific Windows types */ + +#define LUA_INTEGER __int64 +#define LUA_INTEGER_FRMLEN "I64" + +#define LUA_MAXINTEGER _I64_MAX +#define LUA_MININTEGER _I64_MIN + +#else /* }{ */ + +#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ + or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" + +#endif /* } */ + +#else /* }{ */ + +#error "numeric integer type not defined" + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Dependencies with C99 and other C details +** =================================================================== +*/ + +/* +@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. +** (All uses in Lua have only one format item.) +*/ +#if !defined(LUA_USE_C89) +#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) +#else +#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) +#endif + + +/* +@@ lua_strx2number converts an hexadecimal numeric string to a number. +** In C99, 'strtod' does that conversion. Otherwise, you can +** leave 'lua_strx2number' undefined and Lua will provide its own +** implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_strx2number(s,p) lua_str2number(s,p) +#endif + + +/* +@@ lua_number2strx converts a float to an hexadecimal numeric string. +** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. +** Otherwise, you can leave 'lua_number2strx' undefined and Lua will +** provide its own implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_number2strx(L,b,sz,f,n) \ + ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) +#endif + + +/* +** 'strtof' and 'opf' variants for math functions are not valid in +** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the +** availability of these variants. ('math.h' is already included in +** all files that use these macros.) +*/ +#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) +#undef l_mathop /* variants not available */ +#undef lua_str2number +#define l_mathop(op) (lua_Number)op /* no variant */ +#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) +#endif + + +/* +@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation +** functions. It must be a numerical type; Lua will use 'intptr_t' if +** available, otherwise it will use 'ptrdiff_t' (the nearest thing to +** 'intptr_t' in C89) +*/ +#define LUA_KCONTEXT ptrdiff_t + +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(INTPTR_MAX) /* even in C99 this type is optional */ +#undef LUA_KCONTEXT +#define LUA_KCONTEXT intptr_t +#endif +#endif + + +/* +@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). +** Change that if you do not want to use C locales. (Code using this +** macro must include header 'locale.h'.) +*/ +#if !defined(lua_getlocaledecpoint) +#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Language Variations +** ===================================================================== +*/ + +/* +@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some +** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from +** numbers to strings. Define LUA_NOCVTS2N to turn off automatic +** coercion from strings to numbers. +*/ +/* #define LUA_NOCVTN2S */ +/* #define LUA_NOCVTS2N */ + + +/* +@@ LUA_USE_APICHECK turns on several consistency checks on the C API. +** Define it as a help when debugging C code. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(l,e) assert(e) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Macros that affect the API and must be stable (that is, must be the +** same when you compile Lua and when you compile code that links to +** Lua). You probably do not want/need to change them. +** ===================================================================== +*/ + +/* +@@ LUAI_MAXSTACK limits the size of the Lua stack. +** CHANGE it if you need a different limit. This limit is arbitrary; +** its only purpose is to stop Lua from consuming unlimited stack +** space (and to reserve some numbers for pseudo-indices). +*/ +#if LUAI_BITSINT >= 32 +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK 15000 +#endif + + +/* +@@ LUA_EXTRASPACE defines the size of a raw memory area associated with +** a Lua state with very fast access. +** CHANGE it if you need a different size. +*/ +#define LUA_EXTRASPACE (sizeof(void *)) + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@@ of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +** CHANGE it if it uses too much C-stack space. (For long double, +** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a +** smaller buffer would force a memory allocation for each call to +** 'string.format'.) +*/ +#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE +#define LUAL_BUFFERSIZE 8192 +#else +#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) +#endif + +/* }================================================================== */ + + +/* +@@ LUA_QL describes how error messages quote program elements. +** Lua does not use these macros anymore; they are here for +** compatibility only. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + + + +#endif diff --git a/stand/liblua/lutils.c b/stand/liblua/lutils.c new file mode 100644 index 000000000000..3f85dd155787 --- /dev/null +++ b/stand/liblua/lutils.c @@ -0,0 +1,268 @@ +/*- + * Copyright (c) 2014 Pedro Souza + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "lua.h" +#include "lauxlib.h" +#include "lstd.h" +#include "lutils.h" +#include "bootstrap.h" + +static int +lua_perform(lua_State *L) +{ + int argc; + char **argv; + int res = 1; + + if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) { + res = interp_builtin_cmd(argc, argv); + free(argv); + } + lua_pushinteger(L, res); + + return 1; +} + +static int +lua_getchar(lua_State *L) +{ + + lua_pushinteger(L, getchar()); + return 1; +} + +static int +lua_ischar(lua_State *L) +{ + + lua_pushboolean(L, ischar()); + return 1; +} + +static int +lua_gets(lua_State *L) +{ + char buf[129]; + + ngets(buf, 128); + lua_pushstring(L, buf); + return 1; +} + +static int +lua_time(lua_State *L) +{ + + lua_pushinteger(L, time(NULL)); + return 1; +} + +static int +lua_delay(lua_State *L) +{ + + delay((int)luaL_checknumber(L, 1)); + return 0; +} + +static int +lua_getenv(lua_State *L) +{ + lua_pushstring(L, getenv(luaL_checkstring(L, 1))); + + return 1; +} + +static int +lua_setenv(lua_State *L) +{ + const char *key, *val; + + key = luaL_checkstring(L, 1); + val = luaL_checkstring(L, 2); + lua_pushinteger(L, setenv(key, val, 1)); + + return 1; +} + +static int +lua_unsetenv(lua_State *L) +{ + const char *ev; + + ev = luaL_checkstring(L, 1); + lua_pushinteger(L, unsetenv(ev)); + + return 1; +} + +static int +lua_printc(lua_State *L) +{ + int status; + ssize_t l; + const char *s = luaL_checklstring(L, 1, &l); + + status = (printf("%s", s) == l); + + return status; +} + +static int +lua_openfile(lua_State *L) +{ + const char *str; + + if (lua_gettop(L) != 1) { + lua_pushnil(L); + return 1; + } + str = lua_tostring(L, 1); + + FILE * f = fopen(str, "r"); + if (f != NULL) { + FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**)); + *ptr = f; + } else + lua_pushnil(L); + return 1; +} + +static int +lua_closefile(lua_State *L) +{ + FILE ** f; + if (lua_gettop(L) != 1) { + lua_pushboolean(L, 0); + return 1; + } + + f = (FILE**)lua_touserdata(L, 1); + if (f != NULL && *f != NULL) { + lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0); + *f = NULL; + } else + lua_pushboolean(L, 0); + + return 1; +} + +static int +lua_readfile(lua_State *L) +{ + FILE **f; + size_t size, r; + char * buf; + + if (lua_gettop(L) < 1 || lua_gettop(L) > 2) { + lua_pushnil(L); + lua_pushinteger(L, 0); + return 2; + } + + f = (FILE**)lua_touserdata(L, 1); + + if (f == NULL || *f == NULL) { + lua_pushnil(L); + lua_pushinteger(L, 0); + return 2; + } + + if (lua_gettop(L) == 2) + size = (size_t)lua_tonumber(L, 2); + else + size = (*f)->size; + + + buf = (char*)malloc(size); + r = fread(buf, 1, size, *f); + lua_pushlstring(L, buf, r); + free(buf); + lua_pushinteger(L, r); + + return 2; +} + +void +lregister(lua_State *L, const char *tableName, const char *funcName, int (*funcPointer)(lua_State *)) +{ + lua_getglobal(L, tableName); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + lua_newtable(L); + lua_setglobal(L, tableName); + lua_getglobal(L, tableName); + } + + lua_pushcfunction(L, funcPointer); + lua_setfield(L, -2, funcName); + lua_pop(L, 1); +} + + +typedef struct utils_func +{ + int (*func)(lua_State *); + const char *table; + const char *name; +} utils_func; + +static utils_func reg_funcs[] = { + {lua_delay, "loader", "delay"}, + {lua_getenv, "loader", "getenv"}, + {lua_perform, "loader", "perform"}, + {lua_printc, "loader", "printc"}, + {lua_setenv, "loader", "setenv"}, + {lua_time, "loader", "time"}, + {lua_unsetenv, "loader", "unsetenv"}, + + {lua_closefile, "io", "close"}, + {lua_getchar, "io", "getchar"}, + {lua_gets, "io", "gets"}, + {lua_ischar, "io", "ischar"}, + {lua_openfile, "io", "open"}, + {lua_readfile, "io", "read"}, + + {NULL, NULL, NULL}, + }; + +void +register_utils(lua_State *L) +{ + utils_func *f = reg_funcs; + + while (f->func != NULL && f->name != NULL) { + if (f->table != NULL) + lregister(L, f->table, f->name, f->func); + else + lua_register(L, f->name, f->func); + ++f; + } +} diff --git a/stand/liblua/lutils.h b/stand/liblua/lutils.h new file mode 100644 index 000000000000..81f4fa169280 --- /dev/null +++ b/stand/liblua/lutils.h @@ -0,0 +1,32 @@ +/*- + * Copyright (c) 2014 Pedro Souza + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include + +void lregister(lua_State *, const char *, const char *, int (*fptr)(lua_State *)); +void register_utils(lua_State *); diff --git a/stand/liblua/math.h b/stand/liblua/math.h new file mode 100644 index 000000000000..1e6d2dd1db8b --- /dev/null +++ b/stand/liblua/math.h @@ -0,0 +1,38 @@ +/*- + * Copyright (c) 2018 M Warner Losh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +/* + * A replacement for math.h that's sufficient to pretend that we + * actually have one to keep the un-modified lua happy. + */ +#include + +int64_t lstd_pow(int64_t x, int64_t y); +int64_t lstd_floor(int64_t); +int64_t lstd_fmod(int64_t a, int64_t b); +int64_t lstd_frexp(int64_t a, int *); diff --git a/stand/liblua32/Makefile b/stand/liblua32/Makefile new file mode 100644 index 000000000000..4e09f1b05178 --- /dev/null +++ b/stand/liblua32/Makefile @@ -0,0 +1,5 @@ +# $FreeBSD$ + +DO32=1 + +.include "${.CURDIR}/../liblua/Makefile" diff --git a/stand/loader.mk b/stand/loader.mk index 78aedddfbf49..fdb0b6f51830 100644 --- a/stand/loader.mk +++ b/stand/loader.mk @@ -56,9 +56,16 @@ SRCS+= pnp.c .endif # Forth interpreter -.if ${MK_FORTH} != "no" +.if ${MK_LOADER_LUA} != "no" +SRCS+= interp_lua.c +.include "${BOOTSRC}/lua.mk" +LDR_INTERP= ${LIBLUA} +LDR_INTERP32= ${LIBLUA32} +.elif ${MK_FORTH} != "no" SRCS+= interp_forth.c .include "${BOOTSRC}/ficl.mk" +LDR_INTERP= ${LIBFICL} +LDR_INTERP32= ${LIBFICL32} .else SRCS+= interp_simple.c .endif @@ -134,9 +141,12 @@ LIBFICL32= ${LIBFICL} .else LIBFICL32= ${BOOTOBJ}/ficl32/libficl.a .endif -.if ${MK_FORTH} != no -LDR_INTERP= ${LIBFICL} -LDR_INTERP32= ${LIBFICL32} + +LIBLUA= ${BOOTOBJ}/liblua/liblua.a +.if ${MACHINE} == "i386" +LIBLUA32= ${LIBFICL} +.else +LIBLUA32= ${BOOTOBJ}/liblua32/liblua.a .endif CLEANFILES+= vers.c diff --git a/stand/lua.mk b/stand/lua.mk new file mode 100644 index 000000000000..8f5788d4d893 --- /dev/null +++ b/stand/lua.mk @@ -0,0 +1,12 @@ +# $FreeBSD$ + +# Common flags to build lua related files + +.include "defs.mk" + +.if ${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 0 +CFLAGS+= -fPIC +.endif + +CFLAGS+= -I${LUASRC} -I${LDRSRC} -I${LIBLUASRC} +CFLAGS+= -DBOOT_LUA diff --git a/sys/sys/param.h b/sys/sys/param.h index d2d0a4d5243e..ab43437cbd26 100644 --- a/sys/sys/param.h +++ b/sys/sys/param.h @@ -60,7 +60,7 @@ * in the range 5 to 9. */ #undef __FreeBSD_version -#define __FreeBSD_version 1200057 /* Master, propagated to newvers */ +#define __FreeBSD_version 1200058 /* Master, propagated to newvers */ /* * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD, diff --git a/tools/build/options/WITH_LOADER_LUA b/tools/build/options/WITH_LOADER_LUA new file mode 100644 index 000000000000..91867d15ef63 --- /dev/null +++ b/tools/build/options/WITH_LOADER_LUA @@ -0,0 +1,2 @@ +.\" $FreeBSD$ +Set to build LUA bindings for the boot loader.