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"
42 * Like loader.perform, except args are passed already parsed
46 lua_command(lua_State *L)
50 int argc = lua_gettop(L);
53 argv = malloc(sizeof(char *) * (argc + 1));
56 for (i = 0; i < argc; i++)
57 argv[i] = (char *)(intptr_t)luaL_checkstring(L, i + 1);
59 res = interp_builtin_cmd(argc, argv);
61 lua_pushinteger(L, res);
67 lua_has_command(lua_State *L)
71 if (lua_gettop(L) != 1) {
75 cmd = luaL_checkstring(L, 1);
76 lua_pushinteger(L, interp_has_builtin_cmd(cmd));
82 lua_perform(lua_State *L)
88 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
89 res = interp_builtin_cmd(argc, argv);
92 lua_pushinteger(L, res);
98 lua_command_error(lua_State *L)
101 lua_pushstring(L, command_errbuf);
106 * Accepts a space-delimited loader command and runs it through the standard
107 * loader parsing, as if it were executed at the loader prompt by the user.
110 lua_interpret(lua_State *L)
112 const char *interp_string;
114 if (lua_gettop(L) != 1) {
119 interp_string = luaL_checkstring(L, 1);
120 lua_pushinteger(L, interp_run(interp_string));
125 lua_parse(lua_State *L)
130 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
131 for (nargc = 0; nargc < argc; ++nargc) {
132 lua_pushstring(L, argv[nargc]);
143 lua_getchar(lua_State *L)
146 lua_pushinteger(L, getchar());
151 lua_ischar(lua_State *L)
154 lua_pushboolean(L, ischar());
159 lua_gets(lua_State *L)
164 lua_pushstring(L, buf);
169 lua_time(lua_State *L)
172 lua_pushinteger(L, time(NULL));
177 lua_delay(lua_State *L)
180 delay((int)luaL_checknumber(L, 1));
185 lua_getenv(lua_State *L)
187 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
193 lua_setenv(lua_State *L)
195 const char *key, *val;
197 key = luaL_checkstring(L, 1);
198 val = luaL_checkstring(L, 2);
199 lua_pushinteger(L, setenv(key, val, 1));
205 lua_unsetenv(lua_State *L)
209 ev = luaL_checkstring(L, 1);
210 lua_pushinteger(L, unsetenv(ev));
216 lua_printc(lua_State *L)
219 const char *s = luaL_checklstring(L, 1, &l);
221 for (cur = 0; cur < l; ++cur)
222 putchar((unsigned char)*(s++));
228 lua_openfile(lua_State *L)
230 const char *mode, *str;
233 nargs = lua_gettop(L);
234 if (nargs < 1 || nargs > 2) {
238 str = lua_tostring(L, 1);
241 mode = lua_tostring(L, 2);
247 FILE * f = fopen(str, mode);
249 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
257 lua_closefile(lua_State *L)
260 if (lua_gettop(L) != 1) {
261 lua_pushboolean(L, 0);
265 f = (FILE**)lua_touserdata(L, 1);
266 if (f != NULL && *f != NULL) {
267 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
270 lua_pushboolean(L, 0);
276 lua_readfile(lua_State *L)
282 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
284 lua_pushinteger(L, 0);
288 f = (FILE**)lua_touserdata(L, 1);
290 if (f == NULL || *f == NULL) {
292 lua_pushinteger(L, 0);
296 if (lua_gettop(L) == 2)
297 size = (size_t)lua_tonumber(L, 2);
302 buf = (char*)malloc(size);
303 r = fread(buf, 1, size, *f);
304 lua_pushlstring(L, buf, r);
306 lua_pushinteger(L, r);
312 * Implements io.write(file, ...)
313 * Any number of string and number arguments may be passed to it,
314 * and it will return the number of bytes written, or nil, an error string, and
318 lua_writefile(lua_State *L)
323 size_t bufsz, w, wrsz;
329 nargs = lua_gettop(L);
332 return luaL_fileresult(L, 0, NULL);
335 f = (FILE**)lua_touserdata(L, 1);
337 if (f == NULL || *f == NULL) {
339 return luaL_fileresult(L, 0, NULL);
342 /* Do a validation pass first */
343 for (i = 0; i < nargs - 1; i++) {
345 * With Lua's API, lua_isstring really checks if the argument
346 * is a string or a number. The latter will be implicitly
347 * converted to a string by our later call to lua_tolstring.
349 if (!lua_isstring(L, i + 2)) {
351 return luaL_fileresult(L, 0, NULL);
354 for (i = 0; i < nargs - 1; i++) {
355 /* We've already validated; there's no chance of failure */
356 buf = lua_tolstring(L, i + 2, &bufsz);
357 wrsz = fwrite(buf, 1, bufsz, *f);
359 return luaL_fileresult(L, 0, NULL);
362 lua_pushinteger(L, w);
367 * put image using terminal coordinates.
370 lua_term_putimage(lua_State *L)
374 uint32_t x1, y1, x2, y2, f;
375 int nargs, ret = 0, error;
377 nargs = lua_gettop(L);
379 lua_pushboolean(L, 0);
383 name = luaL_checkstring(L, 1);
384 x1 = luaL_checknumber(L, 2);
385 y1 = luaL_checknumber(L, 3);
386 x2 = luaL_checknumber(L, 4);
387 y2 = luaL_checknumber(L, 5);
388 f = luaL_checknumber(L, 6);
390 x1 = gfx_state.tg_origin.tp_col + x1 * gfx_state.tg_font.vf_width;
391 y1 = gfx_state.tg_origin.tp_row + y1 * gfx_state.tg_font.vf_height;
393 x2 = gfx_state.tg_origin.tp_col +
394 x2 * gfx_state.tg_font.vf_width;
397 y2 = gfx_state.tg_origin.tp_row +
398 y2 * gfx_state.tg_font.vf_height;
401 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
402 if (f & FL_PUTIMAGE_DEBUG)
403 printf("%s\n", png_error_string(error));
405 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
407 (void) png_close(&png);
409 lua_pushboolean(L, ret);
414 lua_fb_putimage(lua_State *L)
418 uint32_t x1, y1, x2, y2, f;
419 int nargs, ret = 0, error;
421 nargs = lua_gettop(L);
423 lua_pushboolean(L, 0);
427 name = luaL_checkstring(L, 1);
428 x1 = luaL_checknumber(L, 2);
429 y1 = luaL_checknumber(L, 3);
430 x2 = luaL_checknumber(L, 4);
431 y2 = luaL_checknumber(L, 5);
432 f = luaL_checknumber(L, 6);
434 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
435 if (f & FL_PUTIMAGE_DEBUG)
436 printf("%s\n", png_error_string(error));
438 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
440 (void) png_close(&png);
442 lua_pushboolean(L, ret);
447 lua_fb_setpixel(lua_State *L)
452 nargs = lua_gettop(L);
458 x = luaL_checknumber(L, 1);
459 y = luaL_checknumber(L, 2);
460 gfx_fb_setpixel(x, y);
465 lua_fb_line(lua_State *L)
467 uint32_t x0, y0, x1, y1, wd;
470 nargs = lua_gettop(L);
476 x0 = luaL_checknumber(L, 1);
477 y0 = luaL_checknumber(L, 2);
478 x1 = luaL_checknumber(L, 3);
479 y1 = luaL_checknumber(L, 4);
480 wd = luaL_checknumber(L, 5);
481 gfx_fb_line(x0, y0, x1, y1, wd);
486 lua_fb_bezier(lua_State *L)
488 uint32_t x0, y0, x1, y1, x2, y2, width;
491 nargs = lua_gettop(L);
497 x0 = luaL_checknumber(L, 1);
498 y0 = luaL_checknumber(L, 2);
499 x1 = luaL_checknumber(L, 3);
500 y1 = luaL_checknumber(L, 4);
501 x2 = luaL_checknumber(L, 5);
502 y2 = luaL_checknumber(L, 6);
503 width = luaL_checknumber(L, 7);
504 gfx_fb_bezier(x0, y0, x1, y1, x2, y2, width);
509 lua_fb_drawrect(lua_State *L)
511 uint32_t x0, y0, x1, y1, fill;
514 nargs = lua_gettop(L);
520 x0 = luaL_checknumber(L, 1);
521 y0 = luaL_checknumber(L, 2);
522 x1 = luaL_checknumber(L, 3);
523 y1 = luaL_checknumber(L, 4);
524 fill = luaL_checknumber(L, 5);
525 gfx_fb_drawrect(x0, y0, x1, y1, fill);
530 lua_term_drawrect(lua_State *L)
532 uint32_t x0, y0, x1, y1;
535 nargs = lua_gettop(L);
541 x0 = luaL_checknumber(L, 1);
542 y0 = luaL_checknumber(L, 2);
543 x1 = luaL_checknumber(L, 3);
544 y1 = luaL_checknumber(L, 4);
545 gfx_term_drawrect(x0, y0, x1, y1);
549 #define REG_SIMPLE(n) { #n, lua_ ## n }
550 static const struct luaL_Reg loaderlib[] = {
552 REG_SIMPLE(command_error),
554 REG_SIMPLE(interpret),
557 REG_SIMPLE(has_command),
559 REG_SIMPLE(printc), /* Also registered as the global 'printc' */
562 REG_SIMPLE(unsetenv),
563 REG_SIMPLE(fb_bezier),
564 REG_SIMPLE(fb_drawrect),
566 REG_SIMPLE(fb_putimage),
567 REG_SIMPLE(fb_setpixel),
568 REG_SIMPLE(term_drawrect),
569 REG_SIMPLE(term_putimage),
573 static const struct luaL_Reg iolib[] = {
574 { "close", lua_closefile },
578 { "open", lua_openfile },
579 { "read", lua_readfile },
580 { "write", lua_writefile },
586 luaopen_loader(lua_State *L)
588 luaL_newlib(L, loaderlib);
589 /* Add loader.machine and loader.machine_arch properties */
590 lua_pushstring(L, MACHINE);
591 lua_setfield(L, -2, "machine");
592 lua_pushstring(L, MACHINE_ARCH);
593 lua_setfield(L, -2, "machine_arch");
594 lua_pushstring(L, LUA_PATH);
595 lua_setfield(L, -2, "lua_path");
596 /* Set global printc to loader.printc */
597 lua_register(L, "printc", lua_printc);
602 luaopen_io(lua_State *L)
604 luaL_newlib(L, iolib);