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_command_error(lua_State *L)
84 lua_pushstring(L, command_errbuf);
89 * Accepts a space-delimited loader command and runs it through the standard
90 * loader parsing, as if it were executed at the loader prompt by the user.
93 lua_interpret(lua_State *L)
95 const char *interp_string;
97 if (lua_gettop(L) != 1) {
102 interp_string = luaL_checkstring(L, 1);
103 lua_pushinteger(L, interp_run(interp_string));
108 lua_parse(lua_State *L)
113 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
114 for (nargc = 0; nargc < argc; ++nargc) {
115 lua_pushstring(L, argv[nargc]);
126 lua_getchar(lua_State *L)
129 lua_pushinteger(L, getchar());
134 lua_ischar(lua_State *L)
137 lua_pushboolean(L, ischar());
142 lua_gets(lua_State *L)
147 lua_pushstring(L, buf);
152 lua_time(lua_State *L)
155 lua_pushinteger(L, time(NULL));
160 lua_delay(lua_State *L)
163 delay((int)luaL_checknumber(L, 1));
168 lua_getenv(lua_State *L)
170 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
176 lua_setenv(lua_State *L)
178 const char *key, *val;
180 key = luaL_checkstring(L, 1);
181 val = luaL_checkstring(L, 2);
182 lua_pushinteger(L, setenv(key, val, 1));
188 lua_unsetenv(lua_State *L)
192 ev = luaL_checkstring(L, 1);
193 lua_pushinteger(L, unsetenv(ev));
199 lua_printc(lua_State *L)
202 const char *s = luaL_checklstring(L, 1, &l);
204 for (cur = 0; cur < l; ++cur)
205 putchar((unsigned char)*(s++));
211 lua_openfile(lua_State *L)
213 const char *mode, *str;
216 nargs = lua_gettop(L);
217 if (nargs < 1 || nargs > 2) {
221 str = lua_tostring(L, 1);
224 mode = lua_tostring(L, 2);
230 FILE * f = fopen(str, mode);
232 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
240 lua_closefile(lua_State *L)
243 if (lua_gettop(L) != 1) {
244 lua_pushboolean(L, 0);
248 f = (FILE**)lua_touserdata(L, 1);
249 if (f != NULL && *f != NULL) {
250 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
253 lua_pushboolean(L, 0);
259 lua_readfile(lua_State *L)
265 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
267 lua_pushinteger(L, 0);
271 f = (FILE**)lua_touserdata(L, 1);
273 if (f == NULL || *f == NULL) {
275 lua_pushinteger(L, 0);
279 if (lua_gettop(L) == 2)
280 size = (size_t)lua_tonumber(L, 2);
285 buf = (char*)malloc(size);
286 r = fread(buf, 1, size, *f);
287 lua_pushlstring(L, buf, r);
289 lua_pushinteger(L, r);
295 * Implements io.write(file, ...)
296 * Any number of string and number arguments may be passed to it,
297 * and it will return the number of bytes written, or nil, an error string, and
301 lua_writefile(lua_State *L)
306 size_t bufsz, w, wrsz;
312 nargs = lua_gettop(L);
315 return luaL_fileresult(L, 0, NULL);
318 f = (FILE**)lua_touserdata(L, 1);
320 if (f == NULL || *f == NULL) {
322 return luaL_fileresult(L, 0, NULL);
325 /* Do a validation pass first */
326 for (i = 0; i < nargs - 1; i++) {
328 * With Lua's API, lua_isstring really checks if the argument
329 * is a string or a number. The latter will be implicitly
330 * converted to a string by our later call to lua_tolstring.
332 if (!lua_isstring(L, i + 2)) {
334 return luaL_fileresult(L, 0, NULL);
337 for (i = 0; i < nargs - 1; i++) {
338 /* We've already validated; there's no chance of failure */
339 buf = lua_tolstring(L, i + 2, &bufsz);
340 wrsz = fwrite(buf, 1, bufsz, *f);
342 return luaL_fileresult(L, 0, NULL);
345 lua_pushinteger(L, w);
349 #define REG_SIMPLE(n) { #n, lua_ ## n }
350 static const struct luaL_Reg loaderlib[] = {
352 REG_SIMPLE(command_error),
354 REG_SIMPLE(interpret),
358 /* Also registered as the global 'printc' */
362 REG_SIMPLE(unsetenv),
366 static const struct luaL_Reg iolib[] = {
367 { "close", lua_closefile },
371 { "open", lua_openfile },
372 { "read", lua_readfile },
373 { "write", lua_writefile },
379 luaopen_loader(lua_State *L)
381 luaL_newlib(L, loaderlib);
382 /* Add loader.machine and loader.machine_arch properties */
383 lua_pushstring(L, MACHINE);
384 lua_setfield(L, -2, "machine");
385 lua_pushstring(L, MACHINE_ARCH);
386 lua_setfield(L, -2, "machine_arch");
387 lua_pushstring(L, LUA_PATH);
388 lua_setfield(L, -2, "lua_path");
389 /* Set global printc to loader.printc */
390 lua_register(L, "printc", lua_printc);
395 luaopen_io(lua_State *L)
397 luaL_newlib(L, iolib);