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_perform(lua_State *L)
73 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
74 res = interp_builtin_cmd(argc, argv);
77 lua_pushinteger(L, res);
83 lua_command_error(lua_State *L)
86 lua_pushstring(L, command_errbuf);
91 * Accepts a space-delimited loader command and runs it through the standard
92 * loader parsing, as if it were executed at the loader prompt by the user.
95 lua_interpret(lua_State *L)
97 const char *interp_string;
99 if (lua_gettop(L) != 1) {
104 interp_string = luaL_checkstring(L, 1);
105 lua_pushinteger(L, interp_run(interp_string));
110 lua_parse(lua_State *L)
115 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
116 for (nargc = 0; nargc < argc; ++nargc) {
117 lua_pushstring(L, argv[nargc]);
128 lua_getchar(lua_State *L)
131 lua_pushinteger(L, getchar());
136 lua_ischar(lua_State *L)
139 lua_pushboolean(L, ischar());
144 lua_gets(lua_State *L)
149 lua_pushstring(L, buf);
154 lua_time(lua_State *L)
157 lua_pushinteger(L, time(NULL));
162 lua_delay(lua_State *L)
165 delay((int)luaL_checknumber(L, 1));
170 lua_getenv(lua_State *L)
172 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
178 lua_setenv(lua_State *L)
180 const char *key, *val;
182 key = luaL_checkstring(L, 1);
183 val = luaL_checkstring(L, 2);
184 lua_pushinteger(L, setenv(key, val, 1));
190 lua_unsetenv(lua_State *L)
194 ev = luaL_checkstring(L, 1);
195 lua_pushinteger(L, unsetenv(ev));
201 lua_printc(lua_State *L)
204 const char *s = luaL_checklstring(L, 1, &l);
206 for (cur = 0; cur < l; ++cur)
207 putchar((unsigned char)*(s++));
213 lua_openfile(lua_State *L)
215 const char *mode, *str;
218 nargs = lua_gettop(L);
219 if (nargs < 1 || nargs > 2) {
223 str = lua_tostring(L, 1);
226 mode = lua_tostring(L, 2);
232 FILE * f = fopen(str, mode);
234 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
242 lua_closefile(lua_State *L)
245 if (lua_gettop(L) != 1) {
246 lua_pushboolean(L, 0);
250 f = (FILE**)lua_touserdata(L, 1);
251 if (f != NULL && *f != NULL) {
252 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
255 lua_pushboolean(L, 0);
261 lua_readfile(lua_State *L)
267 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
269 lua_pushinteger(L, 0);
273 f = (FILE**)lua_touserdata(L, 1);
275 if (f == NULL || *f == NULL) {
277 lua_pushinteger(L, 0);
281 if (lua_gettop(L) == 2)
282 size = (size_t)lua_tonumber(L, 2);
287 buf = (char*)malloc(size);
288 r = fread(buf, 1, size, *f);
289 lua_pushlstring(L, buf, r);
291 lua_pushinteger(L, r);
297 * Implements io.write(file, ...)
298 * Any number of string and number arguments may be passed to it,
299 * and it will return the number of bytes written, or nil, an error string, and
303 lua_writefile(lua_State *L)
308 size_t bufsz, w, wrsz;
314 nargs = lua_gettop(L);
317 return luaL_fileresult(L, 0, NULL);
320 f = (FILE**)lua_touserdata(L, 1);
322 if (f == NULL || *f == NULL) {
324 return luaL_fileresult(L, 0, NULL);
327 /* Do a validation pass first */
328 for (i = 0; i < nargs - 1; i++) {
330 * With Lua's API, lua_isstring really checks if the argument
331 * is a string or a number. The latter will be implicitly
332 * converted to a string by our later call to lua_tolstring.
334 if (!lua_isstring(L, i + 2)) {
336 return luaL_fileresult(L, 0, NULL);
339 for (i = 0; i < nargs - 1; i++) {
340 /* We've already validated; there's no chance of failure */
341 buf = lua_tolstring(L, i + 2, &bufsz);
342 wrsz = fwrite(buf, 1, bufsz, *f);
344 return luaL_fileresult(L, 0, NULL);
347 lua_pushinteger(L, w);
352 * put image using terminal coordinates.
355 lua_term_putimage(lua_State *L)
359 uint32_t x1, y1, x2, y2, f;
360 int nargs, ret = 0, error;
362 nargs = lua_gettop(L);
364 lua_pushboolean(L, 0);
368 name = luaL_checkstring(L, 1);
369 x1 = luaL_checknumber(L, 2);
370 y1 = luaL_checknumber(L, 3);
371 x2 = luaL_checknumber(L, 4);
372 y2 = luaL_checknumber(L, 5);
373 f = luaL_checknumber(L, 6);
375 x1 = gfx_state.tg_origin.tp_col + x1 * gfx_state.tg_font.vf_width;
376 y1 = gfx_state.tg_origin.tp_row + y1 * gfx_state.tg_font.vf_height;
378 x2 = gfx_state.tg_origin.tp_col +
379 x2 * gfx_state.tg_font.vf_width;
382 y2 = gfx_state.tg_origin.tp_row +
383 y2 * gfx_state.tg_font.vf_height;
386 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
387 if (f & FL_PUTIMAGE_DEBUG)
388 printf("%s\n", png_error_string(error));
390 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
392 (void) png_close(&png);
394 lua_pushboolean(L, ret);
399 lua_fb_putimage(lua_State *L)
403 uint32_t x1, y1, x2, y2, f;
404 int nargs, ret = 0, error;
406 nargs = lua_gettop(L);
408 lua_pushboolean(L, 0);
412 name = luaL_checkstring(L, 1);
413 x1 = luaL_checknumber(L, 2);
414 y1 = luaL_checknumber(L, 3);
415 x2 = luaL_checknumber(L, 4);
416 y2 = luaL_checknumber(L, 5);
417 f = luaL_checknumber(L, 6);
419 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
420 if (f & FL_PUTIMAGE_DEBUG)
421 printf("%s\n", png_error_string(error));
423 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
425 (void) png_close(&png);
427 lua_pushboolean(L, ret);
432 lua_fb_setpixel(lua_State *L)
437 nargs = lua_gettop(L);
443 x = luaL_checknumber(L, 1);
444 y = luaL_checknumber(L, 2);
445 gfx_fb_setpixel(x, y);
450 lua_fb_line(lua_State *L)
452 uint32_t x0, y0, x1, y1, wd;
455 nargs = lua_gettop(L);
461 x0 = luaL_checknumber(L, 1);
462 y0 = luaL_checknumber(L, 2);
463 x1 = luaL_checknumber(L, 3);
464 y1 = luaL_checknumber(L, 4);
465 wd = luaL_checknumber(L, 5);
466 gfx_fb_line(x0, y0, x1, y1, wd);
471 lua_fb_bezier(lua_State *L)
473 uint32_t x0, y0, x1, y1, x2, y2, width;
476 nargs = lua_gettop(L);
482 x0 = luaL_checknumber(L, 1);
483 y0 = luaL_checknumber(L, 2);
484 x1 = luaL_checknumber(L, 3);
485 y1 = luaL_checknumber(L, 4);
486 x2 = luaL_checknumber(L, 5);
487 y2 = luaL_checknumber(L, 6);
488 width = luaL_checknumber(L, 7);
489 gfx_fb_bezier(x0, y0, x1, y1, x2, y2, width);
494 lua_fb_drawrect(lua_State *L)
496 uint32_t x0, y0, x1, y1, fill;
499 nargs = lua_gettop(L);
505 x0 = luaL_checknumber(L, 1);
506 y0 = luaL_checknumber(L, 2);
507 x1 = luaL_checknumber(L, 3);
508 y1 = luaL_checknumber(L, 4);
509 fill = luaL_checknumber(L, 5);
510 gfx_fb_drawrect(x0, y0, x1, y1, fill);
515 lua_term_drawrect(lua_State *L)
517 uint32_t x0, y0, x1, y1;
520 nargs = lua_gettop(L);
526 x0 = luaL_checknumber(L, 1);
527 y0 = luaL_checknumber(L, 2);
528 x1 = luaL_checknumber(L, 3);
529 y1 = luaL_checknumber(L, 4);
530 gfx_term_drawrect(x0, y0, x1, y1);
534 #define REG_SIMPLE(n) { #n, lua_ ## n }
535 static const struct luaL_Reg loaderlib[] = {
537 REG_SIMPLE(command_error),
539 REG_SIMPLE(interpret),
543 /* Also registered as the global 'printc' */
547 REG_SIMPLE(unsetenv),
548 REG_SIMPLE(fb_bezier),
549 REG_SIMPLE(fb_drawrect),
551 REG_SIMPLE(fb_putimage),
552 REG_SIMPLE(fb_setpixel),
553 REG_SIMPLE(term_drawrect),
554 REG_SIMPLE(term_putimage),
558 static const struct luaL_Reg iolib[] = {
559 { "close", lua_closefile },
563 { "open", lua_openfile },
564 { "read", lua_readfile },
565 { "write", lua_writefile },
571 luaopen_loader(lua_State *L)
573 luaL_newlib(L, loaderlib);
574 /* Add loader.machine and loader.machine_arch properties */
575 lua_pushstring(L, MACHINE);
576 lua_setfield(L, -2, "machine");
577 lua_pushstring(L, MACHINE_ARCH);
578 lua_setfield(L, -2, "machine_arch");
579 lua_pushstring(L, LUA_PATH);
580 lua_setfield(L, -2, "lua_path");
581 /* Set global printc to loader.printc */
582 lua_register(L, "printc", lua_printc);
587 luaopen_io(lua_State *L)
589 luaL_newlib(L, iolib);