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"
39 * Like loader.perform, except args are passed already parsed
43 lua_command(lua_State *L)
47 int argc = lua_gettop(L);
50 argv = malloc(sizeof(char *) * (argc + 1));
53 for (i = 0; i < argc; i++)
54 argv[i] = (char *)(intptr_t)luaL_checkstring(L, i + 1);
56 res = interp_builtin_cmd(argc, argv);
58 lua_pushinteger(L, res);
64 lua_has_command(lua_State *L)
68 if (lua_gettop(L) != 1) {
72 cmd = luaL_checkstring(L, 1);
73 lua_pushinteger(L, interp_has_builtin_cmd(cmd));
79 lua_perform(lua_State *L)
85 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
86 res = interp_builtin_cmd(argc, argv);
89 lua_pushinteger(L, res);
95 lua_command_error(lua_State *L)
98 lua_pushstring(L, command_errbuf);
103 * Accepts a space-delimited loader command and runs it through the standard
104 * loader parsing, as if it were executed at the loader prompt by the user.
107 lua_interpret(lua_State *L)
109 const char *interp_string;
111 if (lua_gettop(L) != 1) {
116 interp_string = luaL_checkstring(L, 1);
117 lua_pushinteger(L, interp_run(interp_string));
122 lua_parse(lua_State *L)
127 if (parse(&argc, &argv, luaL_checkstring(L, 1)) == 0) {
128 for (nargc = 0; nargc < argc; ++nargc) {
129 lua_pushstring(L, argv[nargc]);
140 lua_getchar(lua_State *L)
143 lua_pushinteger(L, getchar());
148 lua_ischar(lua_State *L)
151 lua_pushboolean(L, ischar());
156 lua_gets(lua_State *L)
161 lua_pushstring(L, buf);
166 lua_time(lua_State *L)
169 lua_pushinteger(L, time(NULL));
174 lua_delay(lua_State *L)
177 delay((int)luaL_checknumber(L, 1));
182 lua_getenv(lua_State *L)
184 lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
190 lua_setenv(lua_State *L)
192 const char *key, *val;
194 key = luaL_checkstring(L, 1);
195 val = luaL_checkstring(L, 2);
196 lua_pushinteger(L, setenv(key, val, 1));
202 lua_unsetenv(lua_State *L)
206 ev = luaL_checkstring(L, 1);
207 lua_pushinteger(L, unsetenv(ev));
213 lua_printc(lua_State *L)
216 const char *s = luaL_checklstring(L, 1, &l);
218 for (cur = 0; cur < l; ++cur)
219 putchar((unsigned char)*(s++));
225 lua_openfile(lua_State *L)
227 const char *mode, *str;
230 nargs = lua_gettop(L);
231 if (nargs < 1 || nargs > 2) {
235 str = lua_tostring(L, 1);
238 mode = lua_tostring(L, 2);
244 FILE * f = fopen(str, mode);
246 FILE ** ptr = (FILE**)lua_newuserdata(L, sizeof(FILE**));
254 lua_closefile(lua_State *L)
257 if (lua_gettop(L) != 1) {
258 lua_pushboolean(L, 0);
262 f = (FILE**)lua_touserdata(L, 1);
263 if (f != NULL && *f != NULL) {
264 lua_pushboolean(L, fclose(*f) == 0 ? 1 : 0);
267 lua_pushboolean(L, 0);
273 lua_readfile(lua_State *L)
279 if (lua_gettop(L) < 1 || lua_gettop(L) > 2) {
281 lua_pushinteger(L, 0);
285 f = (FILE**)lua_touserdata(L, 1);
287 if (f == NULL || *f == NULL) {
289 lua_pushinteger(L, 0);
293 if (lua_gettop(L) == 2)
294 size = (size_t)lua_tonumber(L, 2);
299 buf = (char*)malloc(size);
300 r = fread(buf, 1, size, *f);
301 lua_pushlstring(L, buf, r);
303 lua_pushinteger(L, r);
309 * Implements io.write(file, ...)
310 * Any number of string and number arguments may be passed to it,
311 * and it will return the number of bytes written, or nil, an error string, and
315 lua_writefile(lua_State *L)
320 size_t bufsz, w, wrsz;
326 nargs = lua_gettop(L);
329 return luaL_fileresult(L, 0, NULL);
332 f = (FILE**)lua_touserdata(L, 1);
334 if (f == NULL || *f == NULL) {
336 return luaL_fileresult(L, 0, NULL);
339 /* Do a validation pass first */
340 for (i = 0; i < nargs - 1; i++) {
342 * With Lua's API, lua_isstring really checks if the argument
343 * is a string or a number. The latter will be implicitly
344 * converted to a string by our later call to lua_tolstring.
346 if (!lua_isstring(L, i + 2)) {
348 return luaL_fileresult(L, 0, NULL);
351 for (i = 0; i < nargs - 1; i++) {
352 /* We've already validated; there's no chance of failure */
353 buf = lua_tolstring(L, i + 2, &bufsz);
354 wrsz = fwrite(buf, 1, bufsz, *f);
356 return luaL_fileresult(L, 0, NULL);
359 lua_pushinteger(L, w);
364 * put image using terminal coordinates.
367 lua_term_putimage(lua_State *L)
371 uint32_t x1, y1, x2, y2, f;
372 int nargs, ret = 0, error;
374 nargs = lua_gettop(L);
376 lua_pushboolean(L, 0);
380 name = luaL_checkstring(L, 1);
381 x1 = luaL_checknumber(L, 2);
382 y1 = luaL_checknumber(L, 3);
383 x2 = luaL_checknumber(L, 4);
384 y2 = luaL_checknumber(L, 5);
385 f = luaL_checknumber(L, 6);
387 x1 = gfx_state.tg_origin.tp_col + x1 * gfx_state.tg_font.vf_width;
388 y1 = gfx_state.tg_origin.tp_row + y1 * gfx_state.tg_font.vf_height;
390 x2 = gfx_state.tg_origin.tp_col +
391 x2 * gfx_state.tg_font.vf_width;
394 y2 = gfx_state.tg_origin.tp_row +
395 y2 * gfx_state.tg_font.vf_height;
398 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
399 if (f & FL_PUTIMAGE_DEBUG)
400 printf("%s\n", png_error_string(error));
402 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
404 (void) png_close(&png);
406 lua_pushboolean(L, ret);
411 lua_fb_putimage(lua_State *L)
415 uint32_t x1, y1, x2, y2, f;
416 int nargs, ret = 0, error;
418 nargs = lua_gettop(L);
420 lua_pushboolean(L, 0);
424 name = luaL_checkstring(L, 1);
425 x1 = luaL_checknumber(L, 2);
426 y1 = luaL_checknumber(L, 3);
427 x2 = luaL_checknumber(L, 4);
428 y2 = luaL_checknumber(L, 5);
429 f = luaL_checknumber(L, 6);
431 if ((error = png_open(&png, name)) != PNG_NO_ERROR) {
432 if (f & FL_PUTIMAGE_DEBUG)
433 printf("%s\n", png_error_string(error));
435 if (gfx_fb_putimage(&png, x1, y1, x2, y2, f) == 0)
437 (void) png_close(&png);
439 lua_pushboolean(L, ret);
444 lua_fb_setpixel(lua_State *L)
449 nargs = lua_gettop(L);
455 x = luaL_checknumber(L, 1);
456 y = luaL_checknumber(L, 2);
457 gfx_fb_setpixel(x, y);
462 lua_fb_line(lua_State *L)
464 uint32_t x0, y0, x1, y1, wd;
467 nargs = lua_gettop(L);
473 x0 = luaL_checknumber(L, 1);
474 y0 = luaL_checknumber(L, 2);
475 x1 = luaL_checknumber(L, 3);
476 y1 = luaL_checknumber(L, 4);
477 wd = luaL_checknumber(L, 5);
478 gfx_fb_line(x0, y0, x1, y1, wd);
483 lua_fb_bezier(lua_State *L)
485 uint32_t x0, y0, x1, y1, x2, y2, width;
488 nargs = lua_gettop(L);
494 x0 = luaL_checknumber(L, 1);
495 y0 = luaL_checknumber(L, 2);
496 x1 = luaL_checknumber(L, 3);
497 y1 = luaL_checknumber(L, 4);
498 x2 = luaL_checknumber(L, 5);
499 y2 = luaL_checknumber(L, 6);
500 width = luaL_checknumber(L, 7);
501 gfx_fb_bezier(x0, y0, x1, y1, x2, y2, width);
506 lua_fb_drawrect(lua_State *L)
508 uint32_t x0, y0, x1, y1, fill;
511 nargs = lua_gettop(L);
517 x0 = luaL_checknumber(L, 1);
518 y0 = luaL_checknumber(L, 2);
519 x1 = luaL_checknumber(L, 3);
520 y1 = luaL_checknumber(L, 4);
521 fill = luaL_checknumber(L, 5);
522 gfx_fb_drawrect(x0, y0, x1, y1, fill);
527 lua_term_drawrect(lua_State *L)
529 uint32_t x0, y0, x1, y1;
532 nargs = lua_gettop(L);
538 x0 = luaL_checknumber(L, 1);
539 y0 = luaL_checknumber(L, 2);
540 x1 = luaL_checknumber(L, 3);
541 y1 = luaL_checknumber(L, 4);
542 gfx_term_drawrect(x0, y0, x1, y1);
546 #define REG_SIMPLE(n) { #n, lua_ ## n }
547 static const struct luaL_Reg loaderlib[] = {
549 REG_SIMPLE(command_error),
551 REG_SIMPLE(interpret),
554 REG_SIMPLE(has_command),
556 REG_SIMPLE(printc), /* Also registered as the global 'printc' */
559 REG_SIMPLE(unsetenv),
560 REG_SIMPLE(fb_bezier),
561 REG_SIMPLE(fb_drawrect),
563 REG_SIMPLE(fb_putimage),
564 REG_SIMPLE(fb_setpixel),
565 REG_SIMPLE(term_drawrect),
566 REG_SIMPLE(term_putimage),
570 static const struct luaL_Reg iolib[] = {
571 { "close", lua_closefile },
575 { "open", lua_openfile },
576 { "read", lua_readfile },
577 { "write", lua_writefile },
583 luaopen_loader(lua_State *L)
585 luaL_newlib(L, loaderlib);
586 /* Add loader.machine and loader.machine_arch properties */
587 lua_pushstring(L, MACHINE);
588 lua_setfield(L, -2, "machine");
589 lua_pushstring(L, MACHINE_ARCH);
590 lua_setfield(L, -2, "machine_arch");
591 lua_pushstring(L, LUA_PATH);
592 lua_setfield(L, -2, "lua_path");
593 lua_pushinteger(L, bootprog_rev);
594 lua_setfield(L, -2, "version");
595 /* Set global printc to loader.printc */
596 lua_register(L, "printc", lua_printc);
601 luaopen_io(lua_State *L)
603 luaL_newlib(L, iolib);