svn commit: r280485 - projects/lua-bootloader/sys/boot/liblua
Rui Paulo
rpaulo at FreeBSD.org
Wed Mar 25 06:03:37 UTC 2015
Author: rpaulo
Date: Wed Mar 25 06:03:34 2015
New Revision: 280485
URL: https://svnweb.freebsd.org/changeset/base/280485
Log:
Import Pedro Souza's customized lua code that integrates with the boot
loader.
Added:
projects/lua-bootloader/sys/boot/liblua/
projects/lua-bootloader/sys/boot/liblua/Makefile (contents, props changed)
projects/lua-bootloader/sys/boot/liblua/lstd.c (contents, props changed)
projects/lua-bootloader/sys/boot/liblua/lstd.h (contents, props changed)
projects/lua-bootloader/sys/boot/liblua/lutils.c (contents, props changed)
projects/lua-bootloader/sys/boot/liblua/lutils.h (contents, props changed)
Added: projects/lua-bootloader/sys/boot/liblua/Makefile
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ projects/lua-bootloader/sys/boot/liblua/Makefile Wed Mar 25 06:03:34 2015 (r280485)
@@ -0,0 +1,20 @@
+# $FreeBSD$
+
+LUA_PATH= ${.CURDIR}/../../contrib/lua
+.PATH: ${LUA_PATH}/src
+
+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
+
+# Our utilities.
+SRCS+= lstd.c lutils.c
+
+CFLAGS+= -I${.CURDIR} -DBOOT_LUA -ffreestanding -nostdlib -fno-stack-protector
+CFLAGS+= -I${LUA_PATH}
+
+.include <bsd.lib.mk>
Added: projects/lua-bootloader/sys/boot/liblua/lstd.c
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ projects/lua-bootloader/sys/boot/liblua/lstd.c Wed Mar 25 06:03:34 2015 (r280485)
@@ -0,0 +1,429 @@
+/*-
+ * Copyright (c) 2014 Pedro Souza <pedrosouza at 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"
+
+#ifdef BOOT_LUA
+
+int
+abs(int v)
+{
+ return v < 0 ? -v : v;
+}
+
+double
+floor(double v)
+{
+ long long int a = (long long int)v;
+
+ return ((double)a);
+}
+
+char *
+strpbrk (const char *str1, const char *str2)
+{
+ while (*str1)
+ {
+ const char *tmp = str2;
+ while (*tmp)
+ if (*str1 == *tmp++)
+ return (str1);
+ ++str1;
+ }
+
+ return (0);
+}
+
+double
+ldexp (double x, int exp)
+{
+ if (exp >= 0)
+ return x * ((long long)1 << exp);
+ else
+ return x / ((long long)1 << (-exp));
+}
+
+
+double
+pow(double a, double b)
+{
+ printf("pow not implemented!\n");
+ return 1.;
+}
+
+double
+strtod(const char *string, char **endPtr)
+{
+ int sign = 0;
+ int exp_sign = 0;
+ int has_num = 0;
+ int has_frac = 0;
+ int has_exp = 0;
+ unsigned long long num = 0;
+ unsigned long long exp = 0;
+
+ double frac = 0;
+ double fm = 0.1;
+ double exp_m = 1;
+ double ret = 0;
+
+ const char *ptr = string;
+
+ while (isspace(*ptr)) ++ptr;
+
+ if (*ptr == '-')
+ {
+ sign = 1;
+ ++ptr;
+ } else if (*ptr == '+')
+ ++ptr;
+
+ while (isdigit(*ptr))
+ {
+ num *= 10;
+ num += *ptr - '0';
+ ++ptr;
+ ++has_num;
+ }
+
+ if (*ptr == '.')
+ {
+ ++ptr;
+ while (isdigit(*ptr))
+ {
+ frac += (double)(*ptr - '0') * fm;
+ fm *= 0.1;
+ ++ptr;
+ ++has_frac;
+ }
+ }
+
+ if (has_frac == 0 && has_num == 0)
+ {
+ if (endPtr)
+ *endPtr = (char*)string;
+ return 0.;
+ }
+
+ ret = (double)num;
+ ret += frac;
+
+ if (*ptr == 'e' || *ptr == 'E')
+ {
+ if (endPtr)
+ *endPtr = (char*)ptr;
+ ++ptr;
+ if (*ptr == '-')
+ {
+ exp_sign = 1;
+ ++ptr;
+ } else if (*ptr == '+')
+ ++ptr;
+
+ while (isdigit(*ptr))
+ {
+ exp *= 10;
+ exp += *ptr - '0';
+ ++ptr;
+ ++has_exp;
+ }
+ if (has_exp == 0)
+ return ret;
+ }
+
+ if (endPtr)
+ *endPtr = (char*)ptr;
+
+ if (has_exp)
+ {
+ while (exp--)
+ exp_m *= 10;
+ if (exp_sign)
+ exp_m = 1./exp_m;
+
+ }
+ if (sign)
+ ret = -ret;
+
+ return ret * exp_m;
+}
+
+int
+dtostr(double v, char *str)
+{
+ int exp = 0;
+ int i;
+ long long n;
+ double e = 1;
+ char *ptr;
+ char tmp[20];
+ char *buf = str;
+
+ if (v == 0)
+ {
+ str[0] = '0';
+ str[1] = 0;
+ return 1;
+ }
+
+ if (v < 0)
+ {
+ *buf++ = '-';
+ v = -v;
+ }
+
+ if (v <= e)
+ {
+ while (v < e)
+ {
+ --exp;
+ e *= 0.1;
+ }
+ } else {
+ while (v > e)
+ {
+ ++exp;
+ e *= 10;
+ }
+ --exp;
+ e /= 10;
+ }
+ if (exp > 9 || exp < -9)
+ {
+ v /= e;
+ } else {
+ exp = 0;
+ }
+
+ n = (long long)v;
+ v -= n;
+ ptr = &tmp[19];
+ *ptr = 0;
+
+ do
+ {
+ i = n % 10;
+ n /= 10;
+ *(--ptr) = i + '0';
+ } while (n > 0);
+
+ while (*ptr != 0) *buf++ = *ptr++;
+
+ if (v != 0)
+ {
+ ptr = buf;
+ *buf++ = '.';
+
+ for (i = 0; i < 17; ++i)
+ {
+ v *= 10;
+ n = (long long)v;
+ *buf++ = '0' + n;
+ ptr = n > 0 ? buf : ptr;
+ v -= n;
+ }
+ buf = ptr;
+ }
+
+ if (exp != 0)
+ {
+ *buf++ = 'e';
+ if (exp < 0)
+ {
+ *buf++ = '-';
+ exp = -exp;
+ }
+ ptr = &tmp[19];
+ *ptr = 0;
+ while (exp > 0)
+ {
+ i = exp % 10;
+ exp /= 10;
+ *(--ptr) = '0' + i;
+ }
+ while (*ptr != 0) *buf++ = *ptr++;
+ }
+ *buf = 0;
+ return buf - str;
+}
+
+FILE *
+fopen(const char *filename, const char *mode)
+{
+ struct stat st;
+ int fd, r;
+ 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;
+ }
+
+ r = fstat(fd, &st);
+ if (r == 0)
+ {
+ f->fd = fd;
+ f->offset = 0;
+ f->size = st.st_size;
+ } else {
+ free(f);
+ close(fd);
+ f = NULL;
+ }
+ 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;
+}
+
+char *
+strstr(const char *str1, const char *str2)
+{
+ const char *s1 = str1;
+ const char *s2 = str2;
+ int eq = 0;
+
+ while (*s1)
+ {
+ while (*s2 && *s1)
+ {
+ if (*s2 != *s1)
+ break;
+ s1++; s2++;
+ }
+ if (*s2 == NULL)
+ return (char*)str1;
+ if (*s1 == NULL)
+ return NULL;
+ s1 = ++str1;
+ s2 = str2;
+ }
+ return NULL;
+}
+
+void
+luai_writestring(const char *s, int i)
+{
+ while (i-- > 0)
+ putchar(*s++);
+}
+
+int
+iscntrl(int c)
+{
+ return (c >= 0x00 && c <= 0x1F) || c == 0x7F;
+}
+
+int
+isgraph(int c)
+{
+ return (c >= 0x21 && c <= 0x7E);
+}
+
+int
+ispunct(int c)
+{
+ return (c >= 0x21 && c <= 0x2F) || (c >= 0x3A && c <= 0x40) ||
+ (c >= 0x5B && c <= 0x60) || (c >= 0x7B && c <= 0x7E);
+}
+
+void *
+memchr(const void *ptr, int value, size_t num)
+{
+ const unsigned char * str = (const unsigned char*)ptr;
+ const unsigned char * end = (const unsigned char*)ptr + num;
+ while (str < end)
+ {
+ if (*str == (unsigned char)value) return str;
+ ++str;
+ }
+ return NULL;
+}
+
+#endif /* BOOT_LUA */
Added: projects/lua-bootloader/sys/boot/liblua/lstd.h
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ projects/lua-bootloader/sys/boot/liblua/lstd.h Wed Mar 25 06:03:34 2015 (r280485)
@@ -0,0 +1,105 @@
+/*-
+ * Copyright (c) 2014 Pedro Souza <pedrosouza at 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
+
+#ifdef BOOT_LUA
+
+#include <stand.h>
+#include <sys/types.h>
+#include <sys/stddef.h>
+#include <sys/stdint.h>
+#include <limits.h>
+#include <string.h>
+#include <machine/stdarg.h>
+
+
+typedef __ptrdiff_t ptrdiff_t;
+
+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)
+
+#ifndef BUFSIZ
+#define BUFSIZ 512
+#endif
+
+#define getlocaledecpoint() ('.')
+
+#define strcoll strcmp
+
+int abs(int v);
+
+double floor(double v);
+
+char * strpbrk (const char *str1, const char *str2);
+
+double ldexp (double x, int exp);
+
+double pow(double a, double b);
+
+double strtod(const char *string, char **endPtr);
+
+int dtostr(double v, char *str);
+
+char * strstr(const char *str1, const char *str2);
+
+int iscntrl(int c);
+
+int isgraph(int c);
+
+int ispunct(int c);
+
+void * memchr(const void *ptr, int value, size_t num);
+
+#endif
+#endif //LSTD_H
Added: projects/lua-bootloader/sys/boot/liblua/lutils.c
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ projects/lua-bootloader/sys/boot/liblua/lutils.c Wed Mar 25 06:03:34 2015 (r280485)
@@ -0,0 +1,361 @@
+/*-
+ * Copyright (c) 2014 Pedro Souza <pedrosouza at 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 <src/lua.h>
+#include <lstd.h>
+
+int
+lua_perform(lua_State *L)
+{
+ int argc, ret;
+ char **argv;
+ int res = -1;
+ int n = lua_gettop(L);
+
+ if (n >= 1)
+ {
+ parse(&argc, &argv, lua_tostring(L, 1));
+ res = perform(argc, argv);
+ }
+ lua_pushnumber(L, res);
+
+ return 1;
+}
+
+int
+lua_getchar(lua_State *L)
+{
+ lua_pushnumber(L, getchar());
+ return 1;
+}
+
+int lua_ischar(lua_State *L)
+{
+ lua_pushboolean(L, ischar());
+ return 1;
+}
+
+int
+lua_gets(lua_State *L)
+{
+ char buf[129];
+ ngets(buf, 128);
+ lua_pushstring(L, buf);
+ return 1;
+}
+
+int
+lua_time(lua_State *L)
+{
+ lua_pushnumber(L, time(NULL));
+ return 1;
+}
+
+int
+lua_delay(lua_State *L)
+{
+ int n = lua_gettop(L);
+
+ if (n == 1)
+ {
+ delay((int)lua_tonumber(L, 1));
+ }
+ return 0;
+}
+
+int
+lua_getenv(lua_State *L)
+{
+ char *ev;
+ int n = lua_gettop(L);
+
+ if (n == 1)
+ {
+ ev = getenv(lua_tostring(L, 1));
+ if (ev != NULL)
+ lua_pushstring(L, ev);
+ else
+ lua_pushnil(L);
+ } else
+ lua_pushnil(L);
+ return 1;
+}
+
+void *
+lua_realloc(void *ud, void *ptr, size_t osize, size_t nsize)
+{
+ (void)ud; (void)osize; /* not used */
+ if (nsize == 0)
+ {
+ free(ptr);
+ return NULL;
+ }
+ else
+ return realloc(ptr, nsize);
+}
+
+typedef struct data_chunk
+{
+ void * data;
+ size_t size;
+} data_chunk;
+
+const char *
+read_chunk(lua_State *L, void *chunk, size_t *sz)
+{
+ data_chunk * ds = (data_chunk *)chunk;
+ if (ds->size == 0) return NULL;
+ *sz = ds->size;
+ ds->size = 0;
+ return (const char*)ds->data;
+}
+
+
+int
+ldo_string(lua_State *L, const char *str, size_t size)
+{
+ int res;
+ data_chunk ds;
+
+ ds.data = (void*)str;
+ ds.size = size;
+ res = lua_load(L, read_chunk, &ds, "do_string", 0);
+ res = lua_pcall(L, 0, LUA_MULTRET, 0);
+ return res;
+}
+
+int
+ldo_file(lua_State *L, const char *filename)
+{
+ struct stat st;
+ int fd, r;
+ char *buf;
+ const char *errstr;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0) {
+ printf("Failed to open file %s\n", filename);
+ return 1;
+ }
+
+ r = fstat(fd, &st);
+
+ if (r != 0) {
+ printf("Failed to retrieve file stat!\n");
+ close(fd);
+ return 1;
+ }
+
+ buf = malloc(st.st_size);
+ if (buf == NULL) {
+ printf("Failed to alloc buf!\n");
+ close(fd);
+ return 1;
+ }
+
+ r = read(fd, buf, st.st_size);
+ if (r != st.st_size) {
+ printf("Failed to read file (%d/%d)!\n", r, (unsigned int)st.st_size);
+ free(buf);
+ close(fd);
+ return 1;
+ }
+
+ if (ldo_string(L, buf, st.st_size) != 0) {
+ errstr = lua_tostring(L, -1);
+ errstr = errstr == NULL ? "unknown" : errstr;
+ printf("Failed to run %s file with error: %s.\n", filename, errstr);
+ lua_pop(L, 1);
+ }
+
+ free(buf);
+ close(fd);
+
+ return 0;
+}
+
+int
+lua_include(lua_State *L)
+{
+ const char *str;
+
+ if (lua_gettop(L) != 1)
+ {
+ lua_pushboolean(L, 0);
+ return 1;
+ }
+ str = lua_tostring(L, 1);
+ lua_pushboolean(L, (ldo_file(L, str) == 0));
+ return 1;
+}
+
+int
+lua_openfile(lua_State *L)
+{
+ const char *str;
+ int fd;
+ int r;
+ struct stat st;
+
+ 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;
+}
+
+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;
+}
+
+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_pushnumber(L, 0);
+ return 2;
+ }
+
+ f = (FILE**)lua_touserdata(L, 1);
+
+ if (f == NULL || *f == NULL)
+ {
+ lua_pushnil(L);
+ lua_pushnumber(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_pushnumber(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;
+
+utils_func reg_funcs[] = {
+ {lua_perform, "loader", "perform"},
+ {lua_delay, "loader", "delay"},
+ {lua_time, "loader", "time"},
+ {lua_include, "loader", "include"},
+ {lua_getenv, "loader", "getenv"},
+ {lua_getchar, "io", "getchar"},
+ {lua_ischar, "io", "ischar"},
+ {lua_gets, "io", "gets"},
+ {lua_openfile, "io", "open"},
+ {lua_closefile, "io", "close"},
+ {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;
+ }
+}
Added: projects/lua-bootloader/sys/boot/liblua/lutils.h
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ projects/lua-bootloader/sys/boot/liblua/lutils.h Wed Mar 25 06:03:34 2015 (r280485)
@@ -0,0 +1,45 @@
+/*-
+ * Copyright (c) 2014 Pedro Souza <pedrosouza at 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 <src/lua.h>
+
+#define lua_create() lua_newstate(lua_realloc, NULL)
+
+int lua_print(lua_State *L);
+
+int lua_perform(lua_State *L);
+
+void * lua_realloc(void *ud, void *ptr, size_t osize, size_t nsize);
+
+int ldo_string(lua_State *L, const char *str, size_t size);
+
+int ldo_file(lua_State *L, const char *filename);
+
+void lregister(const char *tname, const char *fname, int (*fptr)(lua_State *));
+
+void register_utils(lua_State *L);
More information about the svn-src-projects
mailing list