2 * Copyright (c) 2014 Pedro Souza <pedrosouza@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
35 #include "bootstrap.h"
38 lua_perform(lua_State *L)
44 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
45 res = interp_builtin_cmd(argc, argv);
48 lua_pushinteger(L, res);
54 lua_getchar(lua_State *L)
57 lua_pushinteger(L, getchar());
62 lua_ischar(lua_State *L)
65 lua_pushboolean(L, ischar());
70 lua_gets(lua_State *L)
75 lua_pushstring(L, buf);
80 lua_time(lua_State *L)
83 lua_pushinteger(L, time(NULL));
88 lua_delay(lua_State *L)
91 delay((int)luaL_checknumber(L, 1));
96 lua_getenv(lua_State *L)
98 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
104 lua_setenv(lua_State *L)
106 const char *key, *val;
108 key = luaL_checkstring(L, 1);
109 val = luaL_checkstring(L, 2);
110 lua_pushinteger(L, setenv(key, val, 1));
116 lua_unsetenv(lua_State *L)
120 ev = luaL_checkstring(L, 1);
121 lua_pushinteger(L, unsetenv(ev));
127 lua_printc(lua_State *L)
131 const char *s = luaL_checklstring(L, 1, &l);
133 status = (printf("%s", s) == l);
139 lua_openfile(lua_State *L)
143 if (lua_gettop(L) != 1) {
147 str = lua_tostring(L, 1);
149 FILE * f = fopen(str, "r");
151 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
159 lua_closefile(lua_State *L)
162 if (lua_gettop(L) != 1) {
163 lua_pushboolean(L, 0);
167 f = (FILE**)lua_touserdata(L, 1);
168 if (f != NULL && *f != NULL) {
169 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
172 lua_pushboolean(L, 0);
178 lua_readfile(lua_State *L)
184 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
186 lua_pushinteger(L, 0);
190 f = (FILE**)lua_touserdata(L, 1);
192 if (f == NULL || *f == NULL) {
194 lua_pushinteger(L, 0);
198 if (lua_gettop(L) == 2)
199 size = (size_t)lua_tonumber(L, 2);
204 buf = (char*)malloc(size);
205 r = fread(buf, 1, size, *f);
206 lua_pushlstring(L, buf, r);
208 lua_pushinteger(L, r);
213 #define REG_SIMPLE(n) { #n, lua_ ## n }
214 static const struct luaL_Reg loaderlib[] = {
221 REG_SIMPLE(unsetenv),
225 static const struct luaL_Reg iolib[] = {
226 { "close", lua_closefile },
230 { "open", lua_openfile },
231 { "read", lua_readfile },
237 luaopen_loader(lua_State *L)
239 luaL_newlib(L, loaderlib);
244 luaopen_io(lua_State *L)
246 luaL_newlib(L, iolib);