diff options
Diffstat (limited to 'stand/liblua')
-rw-r--r-- | stand/liblua/Makefile | 47 | ||||
-rw-r--r-- | stand/liblua/float.h | 31 | ||||
-rw-r--r-- | stand/liblua/lerrno.c | 181 | ||||
-rw-r--r-- | stand/liblua/lerrno.h | 33 | ||||
-rw-r--r-- | stand/liblua/locale.h | 26 | ||||
-rw-r--r-- | stand/liblua/lpager.c | 89 | ||||
-rw-r--r-- | stand/liblua/lstd.c | 267 | ||||
-rw-r--r-- | stand/liblua/lstd.h | 83 | ||||
-rw-r--r-- | stand/liblua/luaconf.h | 785 | ||||
-rw-r--r-- | stand/liblua/lutils.c | 591 | ||||
-rw-r--r-- | stand/liblua/lutils.h | 33 | ||||
-rw-r--r-- | stand/liblua/math.h | 37 |
12 files changed, 2203 insertions, 0 deletions
diff --git a/stand/liblua/Makefile b/stand/liblua/Makefile new file mode 100644 index 000000000000..68fa2da64fdb --- /dev/null +++ b/stand/liblua/Makefile @@ -0,0 +1,47 @@ +# $FreeBSD$ + +.include <bsd.init.mk> + +.PATH: ${LUASRC} +.PATH: ${LIBLUASRC} + +.include "${BOOTSRC}/lua.mk" + +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+= lerrno.c lpager.c lstd.c lutils.c + +.PATH: ${FLUASRC}/modules +SRCS+= lfs.c + +WARNS?= 3 + +CFLAGS+= -DLUA_PATH=\"${LUAPATH}\" -DLUA_PATH_DEFAULT=\"${LUAPATH}/\?.lua\" +CFLAGS+= -ffreestanding -nostdlib -DLUA_USE_POSIX +CFLAGS+= -fno-stack-protector -D__BSD_VISIBLE +CFLAGS+= -I${BOOTSRC}/include -I${LIBLUASRC} -I${LUASRC} -I${LDRSRC} + +CFLAGS.lutils.c+= -I${SRCTOP}/sys/teken -I${SRCTOP}/contrib/pnglite + +.if ${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 0 +CFLAGS+= -fPIC +.endif +.if ${MK_LOADER_VERIEXEC} == "yes" +CFLAGS+= -I${SRCTOP}/lib/libsecureboot/h -DLOADER_VERIEXEC +.endif + +.include <bsd.lib.mk> diff --git a/stand/liblua/float.h b/stand/liblua/float.h new file mode 100644 index 000000000000..fa958597bb55 --- /dev/null +++ b/stand/liblua/float.h @@ -0,0 +1,31 @@ +/*- + * Copyright (c) 2018 M. Warner Losh <imp@FreeBSD.org> + * + * 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/lerrno.c b/stand/liblua/lerrno.c new file mode 100644 index 000000000000..defa3c73baad --- /dev/null +++ b/stand/liblua/lerrno.c @@ -0,0 +1,181 @@ +/*- + * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org> + * 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 <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#define _WANT_KERNEL_ERRNO 1 +#include <errno.h> + +#include <lua.h> +#include "lauxlib.h" +#include "lerrno.h" + +#ifndef nitems +#define nitems(x) (sizeof((x)) / sizeof((x)[0])) +#endif + +/* + * Populated with: + * $ egrep "^#define.E" sys/sys/errno.h | \ + * awk '{ print "\tENTRY(" $2 ")," }' >> lerrno.c + */ +#define ENTRY(name) { #name, name } +static const struct err_name_number { + const char *err_name; + int err_num; +} errnoconstants[] = { + ENTRY(EPERM), + ENTRY(ENOENT), + ENTRY(ESRCH), + ENTRY(EINTR), + ENTRY(EIO), + ENTRY(ENXIO), + ENTRY(E2BIG), + ENTRY(ENOEXEC), + ENTRY(EBADF), + ENTRY(ECHILD), + ENTRY(EDEADLK), + ENTRY(ENOMEM), + ENTRY(EACCES), + ENTRY(EFAULT), + ENTRY(ENOTBLK), + ENTRY(EBUSY), + ENTRY(EEXIST), + ENTRY(EXDEV), + ENTRY(ENODEV), + ENTRY(ENOTDIR), + ENTRY(EISDIR), + ENTRY(EINVAL), + ENTRY(ENFILE), + ENTRY(EMFILE), + ENTRY(ENOTTY), + ENTRY(ETXTBSY), + ENTRY(EFBIG), + ENTRY(ENOSPC), + ENTRY(ESPIPE), + ENTRY(EROFS), + ENTRY(EMLINK), + ENTRY(EPIPE), + ENTRY(EDOM), + ENTRY(ERANGE), + ENTRY(EAGAIN), + ENTRY(EWOULDBLOCK), + ENTRY(EINPROGRESS), + ENTRY(EALREADY), + ENTRY(ENOTSOCK), + ENTRY(EDESTADDRREQ), + ENTRY(EMSGSIZE), + ENTRY(EPROTOTYPE), + ENTRY(ENOPROTOOPT), + ENTRY(EPROTONOSUPPORT), + ENTRY(ESOCKTNOSUPPORT), + ENTRY(EOPNOTSUPP), + ENTRY(ENOTSUP), + ENTRY(EPFNOSUPPORT), + ENTRY(EAFNOSUPPORT), + ENTRY(EADDRINUSE), + ENTRY(EADDRNOTAVAIL), + ENTRY(ENETDOWN), + ENTRY(ENETUNREACH), + ENTRY(ENETRESET), + ENTRY(ECONNABORTED), + ENTRY(ECONNRESET), + ENTRY(ENOBUFS), + ENTRY(EISCONN), + ENTRY(ENOTCONN), + ENTRY(ESHUTDOWN), + ENTRY(ETOOMANYREFS), + ENTRY(ETIMEDOUT), + ENTRY(ECONNREFUSED), + ENTRY(ELOOP), + ENTRY(ENAMETOOLONG), + ENTRY(EHOSTDOWN), + ENTRY(EHOSTUNREACH), + ENTRY(ENOTEMPTY), + ENTRY(EPROCLIM), + ENTRY(EUSERS), + ENTRY(EDQUOT), + ENTRY(ESTALE), + ENTRY(EREMOTE), + ENTRY(EBADRPC), + ENTRY(ERPCMISMATCH), + ENTRY(EPROGUNAVAIL), + ENTRY(EPROGMISMATCH), + ENTRY(EPROCUNAVAIL), + ENTRY(ENOLCK), + ENTRY(ENOSYS), + ENTRY(EFTYPE), + ENTRY(EAUTH), + ENTRY(ENEEDAUTH), + ENTRY(EIDRM), + ENTRY(ENOMSG), + ENTRY(EOVERFLOW), + ENTRY(ECANCELED), + ENTRY(EILSEQ), + ENTRY(ENOATTR), + ENTRY(EDOOFUS), + ENTRY(EBADMSG), + ENTRY(EMULTIHOP), + ENTRY(ENOLINK), + ENTRY(EPROTO), + ENTRY(ENOTCAPABLE), + ENTRY(ECAPMODE), + ENTRY(ENOTRECOVERABLE), + ENTRY(EOWNERDEAD), + ENTRY(EINTEGRITY), + ENTRY(ELAST), + ENTRY(ERESTART), + ENTRY(EJUSTRETURN), + ENTRY(ENOIOCTL), + ENTRY(EDIRIOCTL), + ENTRY(ERELOOKUP), +}; +#undef ENTRY + +static void +lerrno_register(lua_State *L) +{ + size_t i; + + for (i = 0; i < nitems(errnoconstants); i++) { + lua_pushinteger(L, errnoconstants[i].err_num); + lua_setfield(L, -2, errnoconstants[i].err_name); + } +} + +static const struct luaL_Reg errnolib[] = { + /* Extra bogus entry required by luaL_newlib API */ + { NULL, NULL }, +}; + +int +luaopen_errno(lua_State *L) +{ + luaL_newlib(L, errnolib); + lerrno_register(L); + return 1; +} diff --git a/stand/liblua/lerrno.h b/stand/liblua/lerrno.h new file mode 100644 index 000000000000..a6ad428facee --- /dev/null +++ b/stand/liblua/lerrno.h @@ -0,0 +1,33 @@ +/*- + * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org> + * 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$ + */ + +#pragma once + +#include <lua.h> + +int luaopen_errno(lua_State *L); diff --git a/stand/liblua/locale.h b/stand/liblua/locale.h new file mode 100644 index 000000000000..b6420935d83f --- /dev/null +++ b/stand/liblua/locale.h @@ -0,0 +1,26 @@ +/*- + * Copyright (c) 2018 M. Warner Losh <imp@FreeBSD.org> + * + * 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/lpager.c b/stand/liblua/lpager.c new file mode 100644 index 000000000000..910931e11d58 --- /dev/null +++ b/stand/liblua/lpager.c @@ -0,0 +1,89 @@ +/*- + * Copyright (c) 2020 Kyle Evans <kevans@FreeBSD.org> + * + * 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 <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <lua.h> +#include "lauxlib.h" + +/* Open the pager. No arguments, no return value. */ +static int +lpager_open(lua_State *L) +{ + + pager_open(); + return (0); +} + +/* + * Output to the pager. All arguments are interpreted as strings and passed to + * pager_output(). No return value. + */ +static int +lpager_output(lua_State *L) +{ + const char *outstr; + int i; + + for (i = 1; i <= lua_gettop(L); i++) { + outstr = luaL_tolstring(L, i, NULL); + pager_output(outstr); + lua_pop(L, -1); + } + + return (0); +} + +/* Output to the pager from a file. Takes a filename, no return value. */ +static int +lpager_file(lua_State *L) +{ + + return (pager_file(luaL_checkstring(L, 1))); +} + +static int +lpager_close(lua_State *L) +{ + + pager_close(); + return (0); +} + +static const struct luaL_Reg pagerlib[] = { + { "open", lpager_open }, + { "output", lpager_output }, + { "file", lpager_file }, + { "close", lpager_close }, + { NULL, NULL }, +}; + +int +luaopen_pager(lua_State *L) +{ + luaL_newlib(L, pagerlib); + return 1; +} diff --git a/stand/liblua/lstd.c b/stand/liblua/lstd.c new file mode 100644 index 000000000000..12675842f65d --- /dev/null +++ b/stand/liblua/lstd.c @@ -0,0 +1,267 @@ +/*- + * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org> + * 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 <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include "lstd.h" +#include "math.h" + +#ifdef LOADER_VERIEXEC +#include <verify_file.h> +#endif + +FILE * +fopen(const char *filename, const char *mode) +{ + struct stat st; + int fd, m, o; + FILE *f; + + if (mode == NULL) + return NULL; + + switch (*mode++) { + case 'r': /* open for reading */ + m = O_RDONLY; + o = 0; + break; + + case 'w': /* open for writing */ + m = O_WRONLY; + /* These are not actually implemented yet */ + o = O_CREAT | O_TRUNC; + break; + + default: /* illegal mode */ + return (NULL); + } + + if (*mode == '+') + m = O_RDWR; + + fd = open(filename, m | o); + 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); + } + +#ifdef LOADER_VERIEXEC + /* only regular files and only reading makes sense */ + if (S_ISREG(st.st_mode) && !(m & O_WRONLY)) { + if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) { + free(f); + close(fd); + return (NULL); + } + } +#endif + + 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); +} + +size_t +fwrite(const void *ptr, size_t size, size_t count, FILE *stream) +{ + ssize_t w; + + if (stream == NULL || ptr == NULL) + return (0); + w = write(stream->fd, ptr, size * count); + if (w == -1) + return (0); + + stream->offset += w; + return ((size_t)w); +} + +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; +} + +DIR * +opendir(const char *name) +{ + DIR *dp; + int fd; + + fd = open(name, O_RDONLY); + if (fd < 0) + return NULL; + dp = fdopendir(fd); + if (dp == NULL) + close(fd); + return dp; +} + +DIR * +fdopendir(int fd) +{ + DIR *dp; + + dp = malloc(sizeof(*dp)); + if (dp == NULL) + return NULL; + dp->fd = fd; + return dp; +} + +int +closedir(DIR *dp) +{ + close(dp->fd); + dp->fd = -1; + free(dp); + return 0; +} + +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..6f9eec2b40b9 --- /dev/null +++ b/stand/liblua/lstd.h @@ -0,0 +1,83 @@ +/*- + * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org> + * 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 <stand.h> +#include <sys/types.h> +#include <sys/stdint.h> +#include <limits.h> +#include <string.h> +#include <machine/stdarg.h> + +typedef struct FILE +{ + int fd; + size_t offset; + size_t size; +} FILE; + +typedef struct DIR +{ + int fd; +} DIR; + +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); +size_t fwrite(const 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); +DIR *opendir(const char *name); +DIR *fdopendir(int fd); +int closedir(DIR *); + +#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..8a0ad782e5e1 --- /dev/null +++ b/stand/liblua/luaconf.h @@ -0,0 +1,785 @@ +/* $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 <limits.h> +#include <stddef.h> + + +/* +** =================================================================== +** General Configuration File for Lua +** +** Some definitions here can be changed externally, through the +** compiler (e.g., with '-D' options). Those are protected by +** '#if !defined' guards. However, several other definitions should +** be changed directly here, either because they affect the Lua +** ABI (by making the changes here, you ensure that all software +** connected to Lua, such as C libraries, will be compiled with the +** same configuration); or because they are seldom changed. +** +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +** {==================================================================== +** System Configuration: macros to adapt (if needed) Lua to some +** particular platform, for instance restricting it to C89. +** ===================================================================== +*/ + +/* +@@ 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 */ +#endif + + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#endif + + +/* +@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. +*/ +#define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Number types. +** =================================================================== +*/ + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +/* #define LUA_32BITS */ + + +/* +@@ 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 + + +/* +@@ 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 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_IS32INT /* 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 "\\" + +#if !defined(LUA_PATH_DEFAULT) +#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" +#endif + +#if !defined(LUA_CPATH_DEFAULT) +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" +#endif + +#else /* }{ */ + +#define LUA_ROOT LUA_PATH "/" LUA_VDIR "/" +#define LUA_LDIR LUA_ROOT "share/" +#define LUA_CDIR LUA_ROOT "lib/" + +#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 + +#if !defined(LUA_CPATH_DEFAULT) +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif + +#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(LUA_DIRSEP) + +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + +#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 LUA_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, +** none of which 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("internal"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + +#define LUAI_DDEC(dec) LUAI_FUNC dec +#define LUAI_DDEF /* empty */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Compatibility with previous versions +** =================================================================== +*/ + +/* +@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. +** You can define it to get all options, or change specific options +** to fit your specific needs. +*/ +#if defined(LUA_COMPAT_5_3) /* { */ + +/* +@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated +** functions in the mathematical library. +** (These functions were already officially removed in 5.3; +** nevertheless they are still available here.) +*/ +#define LUA_COMPAT_MATHLIB + +/* +@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for +** manipulating other integer types (lua_pushunsigned, lua_tounsigned, +** luaL_checkint, luaL_checklong, etc.) +** (These macros were also officially removed in 5.3, but they are still +** available here.) +*/ +#define LUA_COMPAT_APIINTCASTS + + +/* +@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod +** using '__lt'. +*/ +#define LUA_COMPAT_LT_LE + + +/* +@@ 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. +** (Once more, these macros were officially removed in 5.3, but they are +** still available here.) +*/ +#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) + +#endif /* } */ + +/* }================================================================== */ + + + +/* +** {================================================================== +** 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_floatatt(x) corrects float attribute '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 numeral 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 with an integral value +** 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) \ + (*(p) = (LUA_INTEGER)(n), 1) + + +/* now the variable definitions */ + +#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ + +#define LUA_NUMBER float + +#define l_floatatt(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_floatatt(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_floatatt(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 <machine/_inttypes.h> + +#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_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. +@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. +@@ 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 + + +#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) + + +/* 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 + +#define LUA_MAXUNSIGNED UINT_MAX + +#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 + +#define LUA_MAXUNSIGNED ULONG_MAX + +#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 + +#define LUA_MAXUNSIGNED ULLONG_MAX + +#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 + +#define LUA_MAXUNSIGNED _UI64_MAX + +#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 a hexadecimal numeral 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_pointer2str converts a pointer to a readable string in a +** non-specified way. +*/ +#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) + + +/* +@@ lua_number2strx converts a float to a hexadecimal numeral. +** 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 <stdint.h> +#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 the 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 <assert.h> +#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). +** ===================================================================== +*/ + +/* +@@ 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). +** (It must fit into max(size_t)/32.) +*/ +#if LUAI_IS32INT +#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. +*/ +#define LUAL_BUFFERSIZE 128 + + +/* +@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure +** maximum alignment for the other items in that union. +*/ +#define LUAI_MAXALIGN lua_Number n; void *s; lua_Integer i; long l + +/* }================================================================== */ + + + + + +/* =================================================================== */ + +/* +** 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..9243edd07e8f --- /dev/null +++ b/stand/liblua/lutils.c @@ -0,0 +1,591 @@ +/*- + * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org> + * 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 <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> + +#include "lua.h" +#include "lauxlib.h" +#include "lstd.h" +#include "lutils.h" +#include "bootstrap.h" +#include <gfx_fb.h> +#include <pnglite.h> + +/* + * Like loader.perform, except args are passed already parsed + * on the stack. + */ +static int +lua_command(lua_State *L) +{ + int i; + int res = 1; + int argc = lua_gettop(L); + char **argv; + + argv = malloc(sizeof(char *) * (argc + 1)); + if (argv == NULL) + return 0; + for (i = 0; i < argc; i++) + argv[i] = (char *)(intptr_t)luaL_checkstring(L, i + 1); + argv[argc] = NULL; + res = interp_builtin_cmd(argc, argv); + free(argv); + lua_pushinteger(L, res); + + return 1; +} + +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_command_error(lua_State *L) +{ + + lua_pushstring(L, command_errbuf); + return 1; +} + +/* + * Accepts a space-delimited loader command and runs it through the standard + * loader parsing, as if it were executed at the loader prompt by the user. + */ +static int +lua_interpret(lua_State *L) +{ + const char *interp_string; + + if (lua_gettop(L) != 1) { + lua_pushnil(L); + return 1; + } + + interp_string = luaL_checkstring(L, 1); + lua_pushinteger(L, interp_run(interp_string)); + return 1; +} + +static int +lua_parse(lua_State *L) +{ + int argc, nargc; + char **argv; + + if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) { + for (nargc = 0; nargc < argc; ++nargc) { + lua_pushstring(L, argv[nargc]); + } + free(argv); + return nargc; + } + + lua_pushnil(L); + 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) +{ + ssize_t cur, l; + const char *s = luaL_checklstring(L, 1, &l); + + for (cur = 0; cur < l; ++cur) + putchar((unsigned char)*(s++)); + + return 1; +} + +static int +lua_openfile(lua_State *L) +{ + const char *mode, *str; + int nargs; + + nargs = lua_gettop(L); + if (nargs < 1 || nargs > 2) { + lua_pushnil(L); + return 1; + } + str = lua_tostring(L, 1); + mode = "r"; + if (nargs > 1) { + mode = lua_tostring(L, 2); + if (mode == NULL) { + lua_pushnil(L); + return 1; + } + } + FILE * f = fopen(str, mode); + 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; +} + +/* + * Implements io.write(file, ...) + * Any number of string and number arguments may be passed to it, + * and it will return the number of bytes written, or nil, an error string, and + * the errno. + */ +static int +lua_writefile(lua_State *L) +{ + FILE **f; + const char *buf; + int i, nargs; + size_t bufsz, w, wrsz; + + buf = NULL; + bufsz = 0; + w = 0; + wrsz = 0; + nargs = lua_gettop(L); + if (nargs < 2) { + errno = EINVAL; + return luaL_fileresult(L, 0, NULL); + } + + f = (FILE**)lua_touserdata(L, 1); + + if (f == NULL || *f == NULL) { + errno = EINVAL; + return luaL_fileresult(L, 0, NULL); + } + + /* Do a validation pass first */ + for (i = 0; i < nargs - 1; i++) { + /* + * With Lua's API, lua_isstring really checks if the argument + * is a string or a number. The latter will be implicitly + * converted to a string by our later call to lua_tolstring. + */ + if (!lua_isstring(L, i + 2)) { + errno = EINVAL; + return luaL_fileresult(L, 0, NULL); + } + } + for (i = 0; i < nargs - 1; i++) { + /* We've already validated; there's no chance of failure */ + buf = lua_tolstring(L, i + 2, &bufsz); + wrsz = fwrite(buf, 1, bufsz, *f); + if (wrsz < bufsz) + return luaL_fileresult(L, 0, NULL); + w += wrsz; + } + lua_pushinteger(L, w); + return 1; +} + +/* + * put image using terminal coordinates. + */ +static int +lua_term_putimage(lua_State *L) +{ + const char *name; + png_t png; + uint32_t x1, y1, x2, y2, f; + int nargs, ret = 0, error; + + nargs = lua_gettop(L); + if (nargs != 6) { + lua_pushboolean(L, 0); + return 1; + } + + name = luaL_checkstring(L, 1); + x1 = luaL_checknumber(L, 2); + y1 = luaL_checknumber(L, 3); + x2 = luaL_checknumber(L, 4); + y2 = luaL_checknumber(L, 5); + f = luaL_checknumber(L, 6); + + x1 = gfx_state.tg_origin.tp_col + x1 * gfx_state.tg_font.vf_width; + y1 = gfx_state.tg_origin.tp_row + y1 * gfx_state.tg_font.vf_height; + if (x2 != 0) { + x2 = gfx_state.tg_origin.tp_col + + x2 * gfx_state.tg_font.vf_width; + } + if (y2 != 0) { + y2 = gfx_state.tg_origin.tp_row + + y2 * gfx_state.tg_font.vf_height; + } + + if ((error = png_open(&png, name)) != PNG_NO_ERROR) { + if (f & FL_PUTIMAGE_DEBUG) + printf("%s\n", png_error_string(error)); + } else { + if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0) + ret = 1; + (void) png_close(&png); + } + lua_pushboolean(L, ret); + return 1; +} + +static int +lua_fb_putimage(lua_State *L) +{ + const char *name; + png_t png; + uint32_t x1, y1, x2, y2, f; + int nargs, ret = 0, error; + + nargs = lua_gettop(L); + if (nargs != 6) { + lua_pushboolean(L, 0); + return 1; + } + + name = luaL_checkstring(L, 1); + x1 = luaL_checknumber(L, 2); + y1 = luaL_checknumber(L, 3); + x2 = luaL_checknumber(L, 4); + y2 = luaL_checknumber(L, 5); + f = luaL_checknumber(L, 6); + + if ((error = png_open(&png, name)) != PNG_NO_ERROR) { + if (f & FL_PUTIMAGE_DEBUG) + printf("%s\n", png_error_string(error)); + } else { + if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0) + ret = 1; + (void) png_close(&png); + } + lua_pushboolean(L, ret); + return 1; +} + +static int +lua_fb_setpixel(lua_State *L) +{ + uint32_t x, y; + int nargs; + + nargs = lua_gettop(L); + if (nargs != 2) { + lua_pushnil(L); + return 1; + } + + x = luaL_checknumber(L, 1); + y = luaL_checknumber(L, 2); + gfx_fb_setpixel(x, y); + return 0; +} + +static int +lua_fb_line(lua_State *L) +{ + uint32_t x0, y0, x1, y1, wd; + int nargs; + + nargs = lua_gettop(L); + if (nargs != 5) { + lua_pushnil(L); + return 1; + } + + x0 = luaL_checknumber(L, 1); + y0 = luaL_checknumber(L, 2); + x1 = luaL_checknumber(L, 3); + y1 = luaL_checknumber(L, 4); + wd = luaL_checknumber(L, 5); + gfx_fb_line(x0, y0, x1, y1, wd); + return 0; +} + +static int +lua_fb_bezier(lua_State *L) +{ + uint32_t x0, y0, x1, y1, x2, y2, width; + int nargs; + + nargs = lua_gettop(L); + if (nargs != 7) { + lua_pushnil(L); + return 1; + } + + x0 = luaL_checknumber(L, 1); + y0 = luaL_checknumber(L, 2); + x1 = luaL_checknumber(L, 3); + y1 = luaL_checknumber(L, 4); + x2 = luaL_checknumber(L, 5); + y2 = luaL_checknumber(L, 6); + width = luaL_checknumber(L, 7); + gfx_fb_bezier(x0, y0, x1, y1, x2, y2, width); + return 0; +} + +static int +lua_fb_drawrect(lua_State *L) +{ + uint32_t x0, y0, x1, y1, fill; + int nargs; + + nargs = lua_gettop(L); + if (nargs != 5) { + lua_pushnil(L); + return 1; + } + + x0 = luaL_checknumber(L, 1); + y0 = luaL_checknumber(L, 2); + x1 = luaL_checknumber(L, 3); + y1 = luaL_checknumber(L, 4); + fill = luaL_checknumber(L, 5); + gfx_fb_drawrect(x0, y0, x1, y1, fill); + return 0; +} + +static int +lua_term_drawrect(lua_State *L) +{ + uint32_t x0, y0, x1, y1; + int nargs; + + nargs = lua_gettop(L); + if (nargs != 4) { + lua_pushnil(L); + return 1; + } + + x0 = luaL_checknumber(L, 1); + y0 = luaL_checknumber(L, 2); + x1 = luaL_checknumber(L, 3); + y1 = luaL_checknumber(L, 4); + gfx_term_drawrect(x0, y0, x1, y1); + return 0; +} + +#define REG_SIMPLE(n) { #n, lua_ ## n } +static const struct luaL_Reg loaderlib[] = { + REG_SIMPLE(delay), + REG_SIMPLE(command_error), + REG_SIMPLE(command), + REG_SIMPLE(interpret), + REG_SIMPLE(parse), + REG_SIMPLE(getenv), + REG_SIMPLE(perform), + /* Also registered as the global 'printc' */ + REG_SIMPLE(printc), + REG_SIMPLE(setenv), + REG_SIMPLE(time), + REG_SIMPLE(unsetenv), + REG_SIMPLE(fb_bezier), + REG_SIMPLE(fb_drawrect), + REG_SIMPLE(fb_line), + REG_SIMPLE(fb_putimage), + REG_SIMPLE(fb_setpixel), + REG_SIMPLE(term_drawrect), + REG_SIMPLE(term_putimage), + { NULL, NULL }, +}; + +static const struct luaL_Reg iolib[] = { + { "close", lua_closefile }, + REG_SIMPLE(getchar), + REG_SIMPLE(gets), + REG_SIMPLE(ischar), + { "open", lua_openfile }, + { "read", lua_readfile }, + { "write", lua_writefile }, + { NULL, NULL }, +}; +#undef REG_SIMPLE + +int +luaopen_loader(lua_State *L) +{ + luaL_newlib(L, loaderlib); + /* Add loader.machine and loader.machine_arch properties */ + lua_pushstring(L, MACHINE); + lua_setfield(L, -2, "machine"); + lua_pushstring(L, MACHINE_ARCH); + lua_setfield(L, -2, "machine_arch"); + lua_pushstring(L, LUA_PATH); + lua_setfield(L, -2, "lua_path"); + /* Set global printc to loader.printc */ + lua_register(L, "printc", lua_printc); + return 1; +} + +int +luaopen_io(lua_State *L) +{ + luaL_newlib(L, iolib); + return 1; +} diff --git a/stand/liblua/lutils.h b/stand/liblua/lutils.h new file mode 100644 index 000000000000..d7d968b705bb --- /dev/null +++ b/stand/liblua/lutils.h @@ -0,0 +1,33 @@ +/*- + * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org> + * 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 <lua.h> + +int luaopen_loader(lua_State *); +int luaopen_io(lua_State *); +int luaopen_pager(lua_State *); diff --git a/stand/liblua/math.h b/stand/liblua/math.h new file mode 100644 index 000000000000..c29466f1af49 --- /dev/null +++ b/stand/liblua/math.h @@ -0,0 +1,37 @@ +/*- + * Copyright (c) 2018 M. Warner Losh <imp@FreeBSD.org> + * + * 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 <stdint.h> + +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 *); |