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)
157 const char *s = luaL_checklstring(L, 1, &l);
159 for (cur = 0; cur < l; ++cur)
160 putchar((unsigned char)*(s++));
166 lua_openfile(lua_State *L)
168 const char *mode, *str;
171 nargs = lua_gettop(L);
172 if (nargs < 1 || nargs > 2) {
176 str = lua_tostring(L, 1);
179 mode = lua_tostring(L, 2);
185 FILE * f = fopen(str, mode);
187 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
195 lua_closefile(lua_State *L)
198 if (lua_gettop(L) != 1) {
199 lua_pushboolean(L, 0);
203 f = (FILE**)lua_touserdata(L, 1);
204 if (f != NULL && *f != NULL) {
205 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
208 lua_pushboolean(L, 0);
214 lua_readfile(lua_State *L)
220 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
222 lua_pushinteger(L, 0);
226 f = (FILE**)lua_touserdata(L, 1);
228 if (f == NULL || *f == NULL) {
230 lua_pushinteger(L, 0);
234 if (lua_gettop(L) == 2)
235 size = (size_t)lua_tonumber(L, 2);
240 buf = (char*)malloc(size);
241 r = fread(buf, 1, size, *f);
242 lua_pushlstring(L, buf, r);
244 lua_pushinteger(L, r);
250 * Implements io.write(file, ...)
251 * Any number of string and number arguments may be passed to it,
252 * and it will return the number of bytes written, or nil, an error string, and
256 lua_writefile(lua_State *L)
261 size_t bufsz, w, wrsz;
267 nargs = lua_gettop(L);
270 return luaL_fileresult(L, 0, NULL);
273 f = (FILE**)lua_touserdata(L, 1);
275 if (f == NULL || *f == NULL) {
277 return luaL_fileresult(L, 0, NULL);
280 /* Do a validation pass first */
281 for (i = 0; i < nargs - 1; i++) {
283 * With Lua's API, lua_isstring really checks if the argument
284 * is a string or a number. The latter will be implicitly
285 * converted to a string by our later call to lua_tolstring.
287 if (!lua_isstring(L, i + 2)) {
289 return luaL_fileresult(L, 0, NULL);
292 for (i = 0; i < nargs - 1; i++) {
293 /* We've already validated; there's no chance of failure */
294 buf = lua_tolstring(L, i + 2, &bufsz);
295 wrsz = fwrite(buf, 1, bufsz, *f);
297 return luaL_fileresult(L, 0, NULL);
300 lua_pushinteger(L, w);
304 #define REG_SIMPLE(n) { #n, lua_ ## n }
305 static const struct luaL_Reg loaderlib[] = {
310 /* Also registered as the global 'printc' */
314 REG_SIMPLE(unsetenv),
318 static const struct luaL_Reg iolib[] = {
319 { "close", lua_closefile },
323 { "open", lua_openfile },
324 { "read", lua_readfile },
325 { "write", lua_writefile },
331 luaopen_loader(lua_State *L)
333 luaL_newlib(L, loaderlib);
334 /* Add loader.machine and loader.machine_arch properties */
335 lua_pushstring(L, MACHINE);
336 lua_setfield(L, -2, "machine");
337 lua_pushstring(L, MACHINE_ARCH);
338 lua_setfield(L, -2, "machine_arch");
339 /* Set global printc to loader.printc */
340 lua_register(L, "printc", lua_printc);
345 luaopen_io(lua_State *L)
347 luaL_newlib(L, iolib);