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$");
31 #include <sys/param.h>
37 #include "bootstrap.h"
40 * Like loader.perform, except args are passed already parsed
44 lua_command(lua_State *L)
48 int argc = lua_gettop(L);
51 argv = malloc(sizeof(char *) * (argc + 1));
54 for (i = 0; i < argc; i++)
55 argv[i] = (char *)(intptr_t)luaL_checkstring(L, i + 1);
57 res = interp_builtin_cmd(argc, argv);
59 lua_pushinteger(L, res);
65 lua_perform(lua_State *L)
71 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
72 res = interp_builtin_cmd(argc, argv);
75 lua_pushinteger(L, res);
81 lua_getchar(lua_State *L)
84 lua_pushinteger(L, getchar());
89 lua_ischar(lua_State *L)
92 lua_pushboolean(L, ischar());
97 lua_gets(lua_State *L)
102 lua_pushstring(L, buf);
107 lua_time(lua_State *L)
110 lua_pushinteger(L, time(NULL));
115 lua_delay(lua_State *L)
118 delay((int)luaL_checknumber(L, 1));
123 lua_getenv(lua_State *L)
125 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
131 lua_setenv(lua_State *L)
133 const char *key, *val;
135 key = luaL_checkstring(L, 1);
136 val = luaL_checkstring(L, 2);
137 lua_pushinteger(L, setenv(key, val, 1));
143 lua_unsetenv(lua_State *L)
147 ev = luaL_checkstring(L, 1);
148 lua_pushinteger(L, unsetenv(ev));
154 lua_printc(lua_State *L)
158 const char *s = luaL_checklstring(L, 1, &l);
160 status = (printf("%s", s) == l);
166 lua_openfile(lua_State *L)
170 if (lua_gettop(L) != 1) {
174 str = lua_tostring(L, 1);
176 FILE * f = fopen(str, "r");
178 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
186 lua_closefile(lua_State *L)
189 if (lua_gettop(L) != 1) {
190 lua_pushboolean(L, 0);
194 f = (FILE**)lua_touserdata(L, 1);
195 if (f != NULL && *f != NULL) {
196 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
199 lua_pushboolean(L, 0);
205 lua_readfile(lua_State *L)
211 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
213 lua_pushinteger(L, 0);
217 f = (FILE**)lua_touserdata(L, 1);
219 if (f == NULL || *f == NULL) {
221 lua_pushinteger(L, 0);
225 if (lua_gettop(L) == 2)
226 size = (size_t)lua_tonumber(L, 2);
231 buf = (char*)malloc(size);
232 r = fread(buf, 1, size, *f);
233 lua_pushlstring(L, buf, r);
235 lua_pushinteger(L, r);
240 #define REG_SIMPLE(n) { #n, lua_ ## n }
241 static const struct luaL_Reg loaderlib[] = {
249 REG_SIMPLE(unsetenv),
253 static const struct luaL_Reg iolib[] = {
254 { "close", lua_closefile },
258 { "open", lua_openfile },
259 { "read", lua_readfile },
265 luaopen_loader(lua_State *L)
267 luaL_newlib(L, loaderlib);
268 /* Add loader.machine and loader.machine_arch properties */
269 lua_pushstring(L, MACHINE);
270 lua_setfield(L, -2, "machine");
271 lua_pushstring(L, MACHINE_ARCH);
272 lua_setfield(L, -2, "machine_arch");
277 luaopen_io(lua_State *L)
279 luaL_newlib(L, iolib);