Skip to contentSkip to conversation

LUA script engine for grub2

PATCH In review Bean 7 participants 12 messages Apr 7, 2009 – May 16, 2009

lore
Share & tools
lore

Bean

author
#0 lore raw

PATCH 1/1 LUA script engine for grub2

Hi,

This patch integrate the LUA script engine to grub2. Before applying this patch, you should apply the split module patch split_3.diff first.

BTW, I forget to add Makefile.in the previous split_3.diff, so that handler.lst will not be generated, I include it in this one.

To try the LUA engine, you can enter command line, and use:

parser.lua

Inside lua, you can use grub.run to execute grub commands, such as:

grub.run("ls", "-l")

To switch back to sh, you can run this command:

grub.run("parser.sh")

Here is a small lua program to solve the hanoi tower:

function hanoi(n,a,b,c)
if (n == 1) then
print ("move from", a, "to", c)
else
hanoi (n-1, a, c, b)
hanoi (1, a, b, c)
hanoi (n-1, b, a, c)
end
end
hanoi (3, 1, 2, 3)

Signature · 1 line
Bean
#1 lore raw

Is it all from scratch? Wau. Very nice job. Unfortunately I don't know any lua but I'll test this as time permits

Quoted 44 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2. Before applying this patch, you should apply the split module patch split_3.diff first.

BTW, I forget to add Makefile.in the previous split_3.diff, so that handler.lst will not be generated, I include it in this one.

To try the LUA engine, you can enter command line, and use:

parser.lua

Inside lua, you can use grub.run to execute grub commands, such as:

grub.run("ls", "-l")

To switch back to sh, you can run this command:

grub.run("parser.sh")

Here is a small lua program to solve the hanoi tower:

function hanoi(n,a,b,c)
if (n == 1) then
print ("move from", a, "to", c)
else
hanoi (n-1, a, c, b)
hanoi (1, a, b, c)
hanoi (n-1, b, a, c)
end
end
hanoi (3, 1, 2, 3)

------------------------------------------------------------------------

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Regards
Vladimir 'phcoder' Serbinenko
#2 lore raw
In reply to Bean
Quoted 13 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2. Before applying this patch, you should apply the split module patch split_3.diff first.

BTW, I forget to add Makefile.in the previous split_3.diff, so that handler.lst will not be generated, I include it in this one.

To try the LUA engine, you can enter command line, and use:

parser.lua

Quoted 7 lines

Inside lua, you can use grub.run to execute grub commands, such as:

grub.run("ls", "-l")

To switch back to sh, you can run this command:

grub.run("parser.sh")

I would prefer something like grub.return or grub.exit, or something like that if really needed.

Here is something that I would like that it can do:

In GUI definition:

action="lua:grub.run(\"ls\", \"-l\")" and action="my_lua_action.lua"

or just

action="lua:my_lua_action.lua"

and

action="ls -l" or action="(ba)sh:ls -l"

This way one could use both grub bash scripting and lua scripting.

Now if something is being executed in my_lua_action.lua and execution comes to the end it should return to calling code.

grub_lua_execute_file("<file name to lua script>"); grub_lua_execute("<lua commands>");

author
#3 lore raw

2009/4/8 Vesa Jääskeläinen <[email protected]>:

Quoted 16 lines
Quoted 15 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2. Before applying this patch, you should apply the split module patch split_3.diff first.

BTW, I forget to add Makefile.in the previous split_3.diff, so that handler.lst will not be generated, I include it in this one.

To try the LUA engine, you can enter command line, and use:

parser.lua

Inside lua, you can use grub.run to execute grub commands, such as:

Quoted 8 lines
Quoted 5 lines

grub.run("ls", "-l")

To switch back to sh, you can run this command:

grub.run("parser.sh")

I would prefer something like grub.return or grub.exit, or something like that if really needed.

Hi,

I add a grub library to lua, now it only contain grub.run, but it would be easy to add more function. I think it would be useful to add function to interface with the video subsystem directly, so that lua can be used to control the dynamic drawing of ui components.

Quoted 14 lines

Here is something that I would like that it can do:

In GUI definition:

action="lua:grub.run(\"ls\", \"-l\")" and action="my_lua_action.lua"

or just

action="lua:my_lua_action.lua"

and

action="ls -l" or action="(ba)sh:ls -l"

Quoted 7 lines

This way one could use both grub bash scripting and lua scripting.

Now if something is being executed in my_lua_action.lua and execution comes to the end it should return to calling code.

grub_lua_execute_file("<file name to lua script>"); grub_lua_execute("<lua commands>");

In fact, I have extended the configfile command to accept parser parameter:

configfile /new_script.lua lua

or

source /script.lua lua

It would change to lua and parse script.lua, then switch back to the old parser.

Signature · 1 line
Bean
#4 lore raw
Quoted 5 lines · Bean

2009/4/8 Vesa Jääskeläinen <[email protected]>:

Quoted 4 lines
Quoted 3 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2

Cool!

Quoted 4 lines

I add a grub library to lua, now it only contain grub.run, but it would be easy to add more function. I think it would be useful to add function to interface with the video subsystem directly, so that lua can be used to control the dynamic drawing of ui components.

I actually did some preliminary work to make this possible. I made a demo where text is painted by a Lua script and it animates “flying by” on the
screen. (http://grub.gibibit.com/files/gfxmenu-lua-1.iso.gz for a demo ISO.)

Besides making the GRUB video API accessible (partially, so far) to Lua, I also made Lua able to implement GUI components in the graphical menu interface. This is limited at this point and only paint() and destroy() methods can be implemented in Lua. However, I think being able to implement new GUI components in Lua would be really great.

If my code would be any help to you, I can send it to you.

Regards,
Colin

author
#5 lore raw
Quoted 18 lines · Colin D Bennett
Quoted 5 lines · Bean

2009/4/8 Vesa Jääskeläinen <[email protected]>:

Quoted 4 lines
Quoted 3 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2

Cool!

Quoted 4 lines

I add a grub library to lua, now it only contain grub.run, but it would be easy to add more function. I think it would be useful to add function to interface with the video subsystem directly, so that lua can be used to control the dynamic drawing of ui components.

I actually did some preliminary work to make this possible.  I made a demo where text is painted by a Lua script and it animates “flying by” on the screen.   (http://grub.gibibit.com/files/gfxmenu-lua-1.iso.gz for a demo ISO.)

Quoted 7 lines

Besides making the GRUB video API accessible (partially, so far) to Lua, I also made Lua able to implement GUI components in the graphical menu interface.  This is limited at this point and only paint() and destroy() methods can be implemented in Lua.  However, I think being able to implement new GUI components in Lua would be really great.

If my code would be any help to you, I can send it to you.

Hi,

Oh, that looks great, just hope the new graphic interface can be merged soon.

For gui components, here is some of my thoughts. Perhaps we can add api to register ui components, something like this:

grub_ui_register (&grub_menu_ui);

The structure may look like this:

struct grub_ui
{
next
name
create
destroy
invoke
}

Then, we use an xml document to specify the screen layout and actions:

<grub ontimer="script1.draw_image()">

    <script id=script1 lang=lua>
       x = 100, y = 100
       function draw_image()
         x = x + 10
         grub.document.image1.move (x, y)
       end
    </script>
    <header id=header1 x= y= width= height= class= text=>
    </header>
    <menu id=menu1 x= y= width= height= class=>
    </menu>
    <image id=image1 src= x= y=>
    </image>

<grub>

In create function, we pass the attributes so that ui knows how to draw itself. The ui can also implement extra named method, for example, image can have move method. To call them, we can use the invoke function, for example:

invoke ("move", argc, argv);

A similar interface can be implemented for lua ui components.

Signature · 1 line
Bean
#6 lore raw
In reply to Bean
Quoted 3 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2.

Hi,

I don't have any opinion for or against using LUA, but note that we need approval from Marco or Okuji before we can integrate external code.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#7 lore raw
Quoted 9 lines · On Monday 13 April 2009 23:27:32 Robert Millan wrote:
Quoted 3 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2.

Hi,

I don't have any opinion for or against using LUA, but note that we need approval from Marco or Okuji before we can integrate external code.

As long as LUA does not become the first citizen in GRUB (i.e. not essential in using GRUB), no problem.

Regards,
Okuji

author
#8 lore raw

Hi,

Oh nice, can you confirm that there is no license conflict in porting code from lua ?

Quoted 23 lines · Yoshinori K. Okuji
Quoted 9 lines · On Monday 13 April 2009 23:27:32 Robert Millan wrote:
Quoted 3 lines · Bean

Hi,

This patch integrate the LUA script engine to grub2.

Hi,

I don't have any opinion for or against using LUA, but note that we need approval from Marco or Okuji before we can integrate external code.

As long as LUA does not become the first citizen in GRUB (i.e. not essential in using GRUB), no problem.

Regards,
Okuji

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 1 line
Bean
author
#9 lore raw

Hi,

This is an update for lua script engine. Now there are three commands in the grub library:

grub.run
Execute a grub command, the command is now passed as a single string, this allows users to construct it dynamically. Return grub_errno, the error message is returned as second value if grub_errno is not 0.

Example:
err_no,err_msg=grub.run("ls /")
print(err_no,err_msg)

grub.getenv
Get grub environment variable. Allow multiple input.

Example:
prefix,root=grub.getenv("prefix", "root")

grub.setenv
Set grub environment variable.

Example:
grub.setenv ("aa", "Hello")
grub.run("set")

The grub code is now completely separated from lua code, it always starts with grub_ prefix: grub_main.c, grub_lua.h, grub_lib.c and grub_lib.h.

If there is no objection, I'd commit this in a few days.

2009-05-03 Bean <[email protected]>

* conf/common.rmk (pkglib_MODULES): Add lua.mod. (lua_mod_SOURCES): New variable. (lua_mod_CFLAGS): Likewise. (lua_mod_LDFLAGS): Likewise.

* conf/i386.rmk (pkglib_MODULES): Add setjmp.mod. (setjmp_mod_SOURCES): New variable. (setjmp_mod_CFLAGS): Likewise. (setjmp_LDFLAGS): Likewise.

* include/grub/i386/setjmp.h (grub_setjmp): Don't use attribute returns_twice in mingw.

* script/lua/grub_lib.c: New file.
* script/lua/grub_lib.h: Likewise.
* script/lua/grub_lua.h: Likewise.
* script/lua/grub_main.c: Likewise.
* script/lua/lapi.c: Likewise.
* script/lua/lapi.h: Likewise.
* script/lua/lauxlib.c: Likewise.
* script/lua/lauxlib.h: Likewise.
* script/lua/lbaselib.c: Likewise.
* script/lua/lcode.c: Likewise.
* script/lua/lcode.h: Likewise.
* script/lua/ldblib.c: Likewise.
* script/lua/ldebug.c: Likewise.
* script/lua/ldebug.h: Likewise.
* script/lua/ldo.c: Likewise.
* script/lua/ldo.h: Likewise.
* script/lua/ldump.c: Likewise.
* script/lua/lfunc.c: Likewise.
* script/lua/lfunc.h: Likewise.
* script/lua/lgc.c: Likewise.
* script/lua/lgc.h: Likewise.
* script/lua/linit.c: Likewise.
* script/lua/liolib.c: Likewise.
* script/lua/llex.c: Likewise.
* script/lua/llex.h: Likewise.
* script/lua/llimits.h: Likewise.
* script/lua/lmathlib.c: Likewise.
* script/lua/lmem.c: Likewise.
* script/lua/lmem.h: Likewise.
* script/lua/loadlib.c: Likewise.
* script/lua/lobject.c: Likewise.
* script/lua/lobject.h: Likewise.
* script/lua/lopcodes.c: Likewise.
* script/lua/lopcodes.h: Likewise.
* script/lua/loslib.c: Likewise.
* script/lua/lparser.c: Likewise.
* script/lua/lparser.h: Likewise.
* script/lua/lstate.c: Likewise.
* script/lua/lstate.h: Likewise.
* script/lua/lstring.c: Likewise.
* script/lua/lstring.h: Likewise.
* script/lua/lstrlib.c: Likewise.
* script/lua/ltable.c: Likewise.
* script/lua/ltable.h: Likewise.
* script/lua/ltablib.c: Likewise.
* script/lua/ltm.c: Likewise.
* script/lua/ltm.h: Likewise.
* script/lua/lua.h: Likewise.
* script/lua/luaconf.h: Likewise.
* script/lua/lualib.h: Likewise.
* script/lua/lundump.c: Likewise.
* script/lua/lundump.h: Likewise.
* script/lua/lvm.c: Likewise.
* script/lua/lvm.h: Likewise.
* script/lua/lzio.c: Likewise.
* script/lua/lzio.h: Likewise.

Signature · 1 line
Bean
author
#10 lore raw

Hi,

Committed.

Signature · 1 line
Bean
#11 lore raw

Am Samstag, den 16.05.2009, 20:37 +0800 schrieb Bean:

Quoted 3 lines

Hi,

Committed.

Hi Bean,

I think it would be good if you could document this in the Wiki.

Signature · 1 line
Felix Zielcke