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/param.h>
34 #include "bootstrap.h"
37 * Like loader.perform, except args are passed already parsed
41 lua_command(lua_State *L)
45 int argc = lua_gettop(L);
48 argv = malloc(sizeof(char *) * (argc + 1));
51 for (i = 0; i < argc; i++)
52 argv[i] = (char *)(intptr_t)luaL_checkstring(L, i + 1);
54 res = interp_builtin_cmd(argc, argv);
56 lua_pushinteger(L, res);
62 lua_has_command(lua_State *L)
66 cmd = luaL_checkstring(L, 1);
67 if (interp_has_builtin_cmd(cmd)) {
68 lua_pushboolean(L, 1);
73 lua_pushstring(L, "Builtin command not found");
78 lua_has_feature(lua_State *L)
83 feature = luaL_checkstring(L, 1);
85 if (feature_name_is_enabled(feature)) {
86 lua_pushboolean(L, 1);
91 lua_pushstring(L, "Feature not enabled");
97 lua_perform(lua_State *L)
103 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
104 res = interp_builtin_cmd(argc, argv);
107 lua_pushinteger(L, res);
113 lua_exit(lua_State *L)
115 exit(luaL_checkinteger(L, 1));
120 lua_command_error(lua_State *L)
123 lua_pushstring(L, command_errbuf);
128 * Accepts a space-delimited loader command and runs it through the standard
129 * loader parsing, as if it were executed at the loader prompt by the user.
132 lua_interpret(lua_State *L)
134 const char *interp_string;
136 if (lua_gettop(L) != 1) {
141 interp_string = luaL_checkstring(L, 1);
142 lua_pushinteger(L, interp_run(interp_string));
147 lua_parse(lua_State *L)
152 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
153 for (nargc = 0; nargc < argc; ++nargc) {
154 lua_pushstring(L, argv[nargc]);
165 lua_getchar(lua_State *L)
168 lua_pushinteger(L, getchar());
173 lua_ischar(lua_State *L)
176 lua_pushboolean(L, ischar());
181 lua_gets(lua_State *L)
186 lua_pushstring(L, buf);
191 lua_time(lua_State *L)
194 lua_pushinteger(L, time(NULL));
199 lua_delay(lua_State *L)
202 delay((int)luaL_checknumber(L, 1));
207 lua_getenv(lua_State *L)
209 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
215 lua_setenv(lua_State *L)
217 const char *key, *val;
219 key = luaL_checkstring(L, 1);
220 val = luaL_checkstring(L, 2);
221 lua_pushinteger(L, setenv(key, val, 1));
227 lua_unsetenv(lua_State *L)
231 ev = luaL_checkstring(L, 1);
232 lua_pushinteger(L, unsetenv(ev));
238 lua_printc(lua_State *L)
241 const char *s = luaL_checklstring(L, 1, &l);
243 for (cur = 0; cur < l; ++cur)
244 putchar((unsigned char)*(s++));
250 lua_openfile(lua_State *L)
252 const char *mode, *str;
255 nargs = lua_gettop(L);
256 if (nargs < 1 || nargs > 2) {
260 str = lua_tostring(L, 1);
263 mode = lua_tostring(L, 2);
269 FILE * f = fopen(str, mode);
271 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
279 lua_closefile(lua_State *L)
282 if (lua_gettop(L) != 1) {
283 lua_pushboolean(L, 0);
287 f = (FILE**)lua_touserdata(L, 1);
288 if (f != NULL && *f != NULL) {
289 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
292 lua_pushboolean(L, 0);
298 lua_readfile(lua_State *L)
304 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
306 lua_pushinteger(L, 0);
310 f = (FILE**)lua_touserdata(L, 1);
312 if (f == NULL || *f == NULL) {
314 lua_pushinteger(L, 0);
318 if (lua_gettop(L) == 2)
319 size = (size_t)lua_tonumber(L, 2);
324 buf = (char*)malloc(size);
325 r = fread(buf, 1, size, *f);
326 lua_pushlstring(L, buf, r);
328 lua_pushinteger(L, r);
334 * Implements io.write(file, ...)
335 * Any number of string and number arguments may be passed to it,
336 * and it will return the number of bytes written, or nil, an error string, and
340 lua_writefile(lua_State *L)
345 size_t bufsz, w, wrsz;
351 nargs = lua_gettop(L);
354 return luaL_fileresult(L, 0, NULL);
357 f = (FILE**)lua_touserdata(L, 1);
359 if (f == NULL || *f == NULL) {
361 return luaL_fileresult(L, 0, NULL);
364 /* Do a validation pass first */
365 for (i = 0; i < nargs - 1; i++) {
367 * With Lua's API, lua_isstring really checks if the argument
368 * is a string or a number. The latter will be implicitly
369 * converted to a string by our later call to lua_tolstring.
371 if (!lua_isstring(L, i + 2)) {
373 return luaL_fileresult(L, 0, NULL);
376 for (i = 0; i < nargs - 1; i++) {
377 /* We've already validated; there's no chance of failure */
378 buf = lua_tolstring(L, i + 2, &bufsz);
379 wrsz = fwrite(buf, 1, bufsz, *f);
381 return luaL_fileresult(L, 0, NULL);
384 lua_pushinteger(L, w);
388 #define REG_SIMPLE(n) { #n, lua_ ## n }
389 static const struct luaL_Reg loaderlib[] = {
391 REG_SIMPLE(command_error),
395 REG_SIMPLE(has_command),
396 REG_SIMPLE(has_feature),
397 REG_SIMPLE(interpret),
400 REG_SIMPLE(printc), /* Also registered as the global 'printc' */
403 REG_SIMPLE(unsetenv),
407 static const struct luaL_Reg iolib[] = {
408 { "close", lua_closefile },
412 { "open", lua_openfile },
413 { "read", lua_readfile },
414 { "write", lua_writefile },
420 lua_add_feature(void *cookie, const char *name, const char *desc, bool enabled)
422 lua_State *L = cookie;
425 * The feature table consists solely of features that are enabled, and
426 * their associated descriptions for debugging purposes.
428 lua_pushstring(L, desc);
429 lua_setfield(L, -2, name);
433 lua_add_features(lua_State *L)
437 feature_iter(&lua_add_feature, L);
440 * We should still have just the table on the stack after we're done
443 lua_setfield(L, -2, "features");
447 luaopen_loader(lua_State *L)
449 luaL_newlib(L, loaderlib);
450 /* Add loader.machine and loader.machine_arch properties */
451 lua_pushstring(L, MACHINE);
452 lua_setfield(L, -2, "machine");
453 lua_pushstring(L, MACHINE_ARCH);
454 lua_setfield(L, -2, "machine_arch");
455 lua_pushstring(L, LUA_PATH);
456 lua_setfield(L, -2, "lua_path");
457 lua_pushinteger(L, bootprog_rev);
458 lua_setfield(L, -2, "version");
459 lua_pushinteger(L, CMD_OK);
460 lua_setfield(L, -2, "CMD_OK");
461 lua_pushinteger(L, CMD_WARN);
462 lua_setfield(L, -2, "CMD_WARN");
463 lua_pushinteger(L, CMD_ERROR);
464 lua_setfield(L, -2, "CMD_ERROR");
465 lua_pushinteger(L, CMD_CRIT);
466 lua_setfield(L, -2, "CMD_CRIT");
467 lua_pushinteger(L, CMD_FATAL);
468 lua_setfield(L, -2, "CMD_FATAL");
470 /* Set global printc to loader.printc */
471 lua_register(L, "printc", lua_printc);
476 luaopen_io(lua_State *L)
478 luaL_newlib(L, iolib);