Date: Mon, 16 Jun 2014 02:06:37 GMT From: pedrosouza@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r269601 - in soc2014/pedrosouza/lua_loader/head/sys/boot: common lua Message-ID: <201406160206.s5G26bx0045646@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: pedrosouza Date: Mon Jun 16 02:06:36 2014 New Revision: 269601 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=269601 Log: Added lutils.c which contains lua helper functions Added: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.h Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp.h soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c soc2014/pedrosouza/lua_loader/head/sys/boot/lua/Makefile Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp.h ============================================================================== --- soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp.h Sun Jun 15 21:55:22 2014 (r269600) +++ soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp.h Mon Jun 16 02:06:36 2014 (r269601) @@ -23,6 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * $FreeBSD$ */ typedef void interp_init_t(void *ctx); Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c ============================================================================== --- soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c Sun Jun 15 21:55:22 2014 (r269600) +++ soc2014/pedrosouza/lua_loader/head/sys/boot/common/interp_lua.c Mon Jun 16 02:06:36 2014 (r269601) @@ -40,6 +40,8 @@ #include "../../lua/src/lua.h" #include "../../lua/src/ldebug.h" +#include "../lua/lutils.h" + struct interp_lua_softc { lua_State *luap; }; @@ -52,76 +54,6 @@ } while (0) -int -lua_print(lua_State *L) -{ - int i; - int n = lua_gettop(L); - for (i = 1; i <= n; ++i) - printf("%s", lua_tostring(L, i)); - return 0; -} - -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; -} - -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 -do_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; -} - - void interp_lua_init(void *ctx) { @@ -133,13 +65,12 @@ char buf[16]; softc = ctx; - luap = lua_newstate(lua_realloc, NULL); + luap = lua_create(); if (luap == NULL) { LDBG("problem with initializing Lua interpreter\n"); } softc->luap = luap; - lua_register(luap, "print", lua_print); - lua_register(luap, "perform", lua_perform); + register_utils(luap); } Modified: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/Makefile ============================================================================== --- soc2014/pedrosouza/lua_loader/head/sys/boot/lua/Makefile Sun Jun 15 21:55:22 2014 (r269600) +++ soc2014/pedrosouza/lua_loader/head/sys/boot/lua/Makefile Mon Jun 16 02:06:36 2014 (r269601) @@ -2,9 +2,11 @@ LUA_PATH= ../../lua +SRCS+= lutils.c + .PATH: ${LUA_PATH}/src -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 lstd.c +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 lstd.c CFLAGS+= -I. CFLAGS+= -DBOOT_LUA -ffreestanding -nostdlib -fno-stack-protector Added: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.c Mon Jun 16 02:06:36 2014 (r269601) @@ -0,0 +1,221 @@ +/*- + * 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 "../../lua/src/lua.h" +#include "../common/interp.h" + + +int +lua_print(lua_State *L) +{ + int i; + int n = lua_gettop(L); + for (i = 1; i <= n; ++i) + printf("%s", lua_tostring(L, i)); + return 0; +} + +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_gets(lua_State *L) +{ + char buf[129]; + ngets(buf, 128); + lua_pushstring(L, buf); + return 1; +} + +int +lua_strchar(lua_State *L) +{ + const char * str; + int i, j; + int n = lua_gettop(L); + if (n != 2) return 0; + str = lua_tostring(L, 1); + if (str == NULL) return 0; + i = strlen(str); + j = (int)lua_tonumber(L, 2); + if ( j >= 0 && j < i) + { + lua_pushnumber(L, str[j]); + return 1; + } + return 0; +} + +int +lua_charstr(lua_State *L) +{ + int n = lua_gettop(L); + char buf[129]; + if (n < 1) return 0; + n = n > 128 ? 128 : n; + + buf[n] = 0; + while (n-- > 0) + { + buf[n] = ((int)lua_tonumber(L, n)) & 0xFF; + } + lua_pushstring(L, buf); + return 1; +} + +int +lua_delay(lua_State * L) +{ + int n = lua_gettop(L); + if (n == 1) + { + delay((int)lua_tonumber(L, 1)); + } + return 0; +} + +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 +do_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; +} + + + +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_print, NULL, "print"}, + {lua_perform, "loader", "perform"}, + {lua_delay, "loader", "delay"}, + {lua_strchar, "string", "byte"}, + {lua_charstr, "string", "char"}, + {lua_getchar, "io", "getchar"}, + {lua_gets, "io", "gets"}, + {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; + } +} \ No newline at end of file Added: soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ soc2014/pedrosouza/lua_loader/head/sys/boot/lua/lutils.h Mon Jun 16 02:06:36 2014 (r269601) @@ -0,0 +1,44 @@ +/*- + * 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/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 do_string(lua_State *L, const char * str, size_t size); + +void lregister(const char * tname, const char * fname, int (*fptr)(lua_State *)); + +void register_utils(lua_State * L); +
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201406160206.s5G26bx0045646>