Scripting support (PATCH)
Hi,
Here is the patch I promised last week. It is far from perfect and will most likely break one or two things, but I think it is worth the trouble to add it for the following reasons:
1) I don't want to have a big diff between my work on HEAD.
2) It needs to be tested.
3) People can start hacking on it.
First I will start with some design issues and a feature description. Later in this email I will describe the important internals. I hope every regular contributor is willing to read the first part and give some comments, this patch changes a lot of stuff, too much to just check it in I think (So I will at least wait for Okuji's opinion). I hope someone is capable of extending genmk.rb so .y files are accepted, otherwise I can not cleanly add it to the rmk. Perhaps we can talk things over on IRC, if it is too hard to do this on the list.
Basic description:
There are not many features supported. Until now the following things are supported:
- A single command line.
Example: ls -l
- Multiple commands, separated with a `;'.
Example: ls -l ; help
- Functions.
Example: function lsls { ls ; ls }
Run it with: grub> lsls
Argument are not yet supported, but I took care of passing things around in the code.
- An if statement.
set a=foo
if [ ${a}=foo ]; then ls else help fi
(This executes ls, because a is set to foo)
- The [ commands
It's just a dummy, only capable of checking if two strings are equal. It should be extended so it can do more tests.
Things that don't work, are broken, etc:
- The parser does not do good error checking of the script yet.
- Only single lines of code are supported.
- No real scripts are parsed, only the command line.
- Not integrated with the menu yet.
- foo=bar' was removed for now. I will restore it, but for now you have to use set foo=bar'.
- The syntax it supports is not loose enough yet. For example, this does not work:
if [ $a=foo ]; then ls else help fi
Of course, when it is in CVS I can work on some things, if no one else picks them up. Some issues like menu integration have to be discussed in detail to do it right and clean.
Now the technical issues:
The scanner:
It's in normal/lexer.c. It's quite a simple scanner. I haven't used flex because it was not possible to use it outside of a POSIX environment. I yet have to make sure all memory is freed when it should...
The parser:
For the parser I have used bison. You will see I had to support spaces instead of removing them. This is because spaces have a functional meaning in the syntax. :(
I think the parser is quite clear. parser.y parses the tokens and the functions in script.c generate a binary representation of the script. The form I am using is some kind of OO approach. I have described this approach in an earlier email, it is quite similar to how bash works and easy to maintain. I stopped using a type field, but I am using function pointers to execute and free the code now. The advantage of this approach is that someone can extend GRUB with a new syntax without any cost. It also makes it easier to extend GRUB with new language constructs.
The result of this execution is a `struct grub_script_cmd *'. This can be stored in a function, later in a menu entry and it can be executed immediately.
Functions:
Functions are just names linked to a `struct grub_script_cmd *'. You can easily execute them, overwrite them, remove them, etc. They do not show up in the command list yet. I think I want to keep it that way and add another command to add the command to the list of commands. By doing that helper functions won't be shown to the user, only real functions.
Conditions:
This is only used by the if command in combination to `[' so far. It works as follows:
if' accepts any command. I showed the example of using the [' command. It can be made similar to the [' command of coreutils. This command sets the environment variable RESULT' to either 0 or 1. After execution if' checks RESULT' and either executes the if' or the else' branch. It would also be possible to use a return value instead of `RESULT', but that was too much trouble for me at first. But of course this can be changed if someone can convince me of that.
I hope this is clear to everyone, otherwise please ask me. After this patch is committed I work on the problems I described in this email. I don't think I will work on lsb.c or on the language constructs itself, unless no comes up with something I like. :-)
Thanks,
Marco
2005-10-29 Marco Gerards <[email protected]>
Add initial scripting support.
* commands/lsb.c: New file.
* conf/i386-pc.rmk (grub_emu_SOURCES): Add commands/lsb.c', normal/execute.c', normal/lexer.c', normal/parser.tab.c', normal/function.c' and normal/script.c'. (normal_mod_SOURCES): normal/execute.c', normal/lexer.c', normal/parser.tab.c', normal/function.c' and normal/script.c'. (lsb_mod_SOURCES, lsb_mod_CFLAGS, lsb_mod_LDFLAGS): New variables. (pkgdata_MODULES): Add lsb.mod'.
* include/grub/parser.h: New file.
* include/grub/script.h: Likewise.
* kern/parser.c: Likewise.
* normal/execute.c: Likewise.
* normal/function.c: Likewise.
* normal/lexer.c: Likewise.
* normal/parser.y: Likewise.
* normal/script.c: Likewise.
* normal/command.c: Include <grub/script.h>. (grub_command_execute): Rewritten.
* util/grub-emu.c (main): Call grub_lsb_init' and grub_lsb_fini'.
Index: commands/lsb.c
===================================================================
RCS file: commands/lsb.c
lsb.c new file +83 -0
@@ -0,0 +1,83 @@ +/* lsb.c - The Left Square Bracket (`[') command. */ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2005 Free Software Foundation, Inc. +* +* GRUB is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/arg.h> +#include <grub/misc.h> + + + +static grub_err_t +grub_cmd_lsb (struct grub_arg_list *state __attribute__ ((unused)),int argc, +char **args) + +{ +char *eq; +char *eqis; +int true; + +/* XXX: No fancy expression evaluation yet. */ + +if (argc ==0 ) +return 0 ; + + eq =grub_strdup (args[0 ]); + eqis =grub_strchr (eq,'=' ); +if (! eqis) +return 0 ; + + *eqis ='\0' ; + eqis++; +/* Check an expression in the form `A=B'. */ +grub_env_set ("RESULT" ,grub_strcmp (eq, eqis) ?"0" :"1" ); +grub_free (eq); + +return 0 ; +} + + + +#ifdef GRUB_UTIL +void +grub_lsb_init (void ) +{ +grub_register_command ("[" , grub_cmd_lsb,GRUB_COMMAND_FLAG_CMDLINE , +"[ EXPRESSION ]" ,"Evaluate an expression" ,0 ); +} + +void +grub_lsb_fini (void ) +{ +grub_unregister_command ("[" ); +} +#else /* ! GRUB_UTIL */ +GRUB_MOD_INIT +{ + (void )mod;/* To stop warning. */ +grub_register_command ("[" , grub_cmd_lsb,GRUB_COMMAND_FLAG_CMDLINE , +"[ EXPRESSION ]" ,"Evaluate an expression" ,0 ); +} + +GRUB_MOD_FINI +{ +grub_unregister_command ("[" ); +} +#endif /* ! GRUB_UTIL */
Index: conf/i386-pc.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.50
i386-pc.rmk +17 -10
@@ -80,19 +80,20 @@ grub_probefs_SOURCES = util/i386/pc/grub # For grub-emu. grub_emu_SOURCES = commands/boot.c commands/cat.c commands/cmp.c \ commands/configfile.c commands/default.c commands/help.c \ - commands/terminal.c commands/ls.c commands/search.c \ - commands/timeout.c \ + commands/terminal.c commands/ls.c commands/lsb.c \ + commands/search.c commands/timeout.c \ commands/i386/pc/halt.c commands/i386/pc/reboot.c \ disk/loopback.c \ fs/affs.c fs/ext2.c fs/fat.c fs/fshelp.c fs/hfs.c fs/iso9660.c \ fs/jfs.c fs/minix.c fs/sfs.c fs/ufs.c fs/xfs.c \ io/gzio.c \ kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c \ - kern/file.c kern/fs.c kern/loader.c kern/main.c kern/misc.c \ - kern/parser.c kern/partition.c kern/rescue.c kern/term.c \ - normal/arg.c normal/cmdline.c normal/command.c \ + normal/execute.c kern/file.c kern/fs.c normal/lexer.c \ + kern/loader.c kern/main.c kern/misc.c kern/parser.c \ + normal/parser.tab.c kern/partition.c kern/rescue.c kern/term.c \ + normal/arg.c normal/cmdline.c normal/command.c normal/function.c\ normal/completion.c normal/context.c normal/main.c \ - normal/menu.c normal/menu_entry.c normal/misc.c \ + normal/menu.c normal/menu_entry.c normal/misc.c normal/script.c \ partmap/amiga.c partmap/apple.c partmap/pc.c partmap/sun.c \ util/console.c util/grub-emu.c util/misc.c \ util/i386/pc/biosdisk.c util/i386/pc/getroot.c \
@@ -117,7 +118,7 @@ pkgdata_MODULES = _chain.mod _linux.mod apple.mod pc.mod sun.mod loopback.mod reboot.mod halt.mod \ help.mod default.mod timeout.mod configfile.mod vbe.mod \ vesafb.mod vbetest.mod vbeinfo.mod search.mod gzio.mod \ - terminfo.mod serial.mod xfs.mod affs.mod sfs.mod + terminfo.mod serial.mod xfs.mod affs.mod sfs.mod lsb.mod # For _chain.mod. _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -196,9 +197,10 @@ linux_mod_LDFLAGS = $(COMMON_LDFLAGS) # For normal.mod. normal_mod_SOURCES = normal/arg.c normal/cmdline.c normal/command.c \ - normal/completion.c normal/context.c normal/main.c \ - normal/menu.c normal/menu_entry.c normal/misc.c \ - normal/i386/setjmp.S + normal/completion.c normal/context.c normal/execute.c \ + normal/function.c normal/lexer.c normal/main.c normal/menu.c \ + normal/menu_entry.c normal/misc.c normal/parser.tab.c \ + normal/script.c normal/i386/setjmp.S normal_mod_CFLAGS = $(COMMON_CFLAGS) normal_mod_ASFLAGS = $(COMMON_ASFLAGS) -m32 normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
@@ -347,3 +349,8 @@ search_mod_LDFLAGS = $(COMMON_LDFLAGS) gzio_mod_SOURCES = io/gzio.c gzio_mod_CFLAGS = $(COMMON_CFLAGS) gzio_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For lsb.mod. +lsb_mod_SOURCES = commands/lsb.c +lsb_mod_CFLAGS = $(COMMON_CFLAGS) +lsb_mod_LDFLAGS = $(COMMON_LDFLAGS)
Index: include/grub/parser.h
===================================================================
RCS file: include/grub/parser.h
grub/parser.h new file +68 -0
68 lines not on this page · raw message
Index: include/grub/script.h
===================================================================
RCS file: include/grub/script.h
grub/script.h new file +164 -0
164 lines not on this page · raw message
Index: kern/parser.c
===================================================================
RCS file: kern/parser.c
parser.c new file +230 -0
230 lines not on this page · raw message
Index: normal/command.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/command.c,v
retrieving revision 1.13
command.c +13 -48
88 lines not on this page · raw message
Index: normal/execute.c
===================================================================
RCS file: normal/execute.c
execute.c new file +200 -0
200 lines not on this page · raw message
Index: normal/function.c
===================================================================
RCS file: normal/function.c
function.c new file +126 -0
126 lines not on this page · raw message
Index: normal/lexer.c
===================================================================
RCS file: normal/lexer.c
lexer.c new file +204 -0
204 lines not on this page · raw message
Index: normal/parser.y
===================================================================
RCS file: normal/parser.y
parser.y new file +157 -0
157 lines not on this page · raw message
Index: normal/script.c
===================================================================
RCS file: normal/script.c
script.c new file +221 -0
221 lines not on this page · raw message
Index: util/grub-emu.c
===================================================================
RCS file: /cvsroot/grub/grub2/util/grub-emu.c,v
retrieving revision 1.26
grub-emu.c +2 -0
@@ -219,6 +219,7 @@ main (int argc, char *argv[])grub_timeout_init ();grub_configfile_init ();grub_search_init (); +grub_lsb_init ();/* XXX: Should normal mode be started by default? */ grub_normal_init ();
@@ -227,6 +228,7 @@ main (int argc, char *argv[])if (setjmp (main_env) ==0 )grub_main (); +grub_lsb_fini ();grub_search_fini ();grub_configfile_fini ();grub_timeout_fini ();
Now, both Marco and Vladimir work on the scripting support separately, and I'm glad to see the development going well. However, we must choose either of them (or even none of them, if both are not good ;) to integrate a result into the official code.
I understand that there are two implementations because they do not agree on one or more technical issues. As the maintainer, I need to listen to opinions (not mental ones, but technical ones), and determine a direction. So I ask you to speak pros and cons in both implementations (especially, Vladimir and Marco). Even if you are not Vladimir or Marco, if you have any opinion, feel free to describe it.
- What are advantages in Vladimir's implementation?
- What are disadvantages in Vladimir's implementation?
- What are advantages in Marco's implementation?
- What are disadvantages in Marco's implementation?
Here are my own:
- What are advantages in Vladimir's implementation?
It has already implemented most features we need.
- What are disadvantages in Vladimir's implementation?
Poorly documented. The execution model is a bit complicated.
- What are advantages in Marco's implementation?
Well documented. A bit simpler.
- What are disadvantages in Marco's implementation?
Fewer features are implemented. The kernel is bloated.
Okuji
Quoted 11 lines · Yoshinori K. Okuji
Now, both Marco and Vladimir work on the scripting support separately, and I'm glad to see the development going well. However, we must choose either of them (or even none of them, if both are not good ;) to integrate a result into the official code.
I understand that there are two implementations because they do not agree on one or more technical issues. As the maintainer, I need to listen to opinions (not mental ones, but technical ones), and determine a direction. So I ask you to speak pros and cons in both implementations (especially, Vladimir and Marco). Even if you are not Vladimir or Marco, if you have any opinion, feel free to describe it.
Right, I felt it hard to explain the direction I'd like to take on this list. So I have implemented the core functionality because that definitely has to be right.
Quoted 1 line
- What are advantages in Vladimir's implementation?
More features.
Quoted 1 line
- What are disadvantages in Vladimir's implementation?
In my opinion it is not clean enough and does not integrate with GRUB like I would like it to. And Vladimir's patch was way too hard to maintain when I started working on this a week ago.
Quoted 1 line
- What are advantages in Marco's implementation?
It's more flexible, cleaner and easier to maintain.
Quoted 1 line
- What are disadvantages in Marco's implementation?
It's work in progress.
Quoted 5 lines
Here are my own:
- What are advantages in Vladimir's implementation?
It has already implemented most features we need.
Right, but I have asked Vladimir several times for a discussion on this mailinglist about what the features are. That discussion did not properly take place yet.
Quoted 3 lines
- What are disadvantages in Marco's implementation?
Fewer features are implemented. The kernel is bloated.
What do you mean with the kernel is bloated? My patch moves a lot of responsibility to commands. I would say it is less bloated.
Thanks,
Marco
Quoted 4 lines · Yoshinori K. Okuji
Now, both Marco and Vladimir work on the scripting support separately, and I'm glad to see the development going well. However, we must choose either of them (or even none of them, if both are not good ;) to integrate a result into the official code.
There is one important thing I forgot to mention in my last email. Vladimir's newest patch is heavily based on my last week's patch. My new patch does only clean up some things I did wrong last week and it adds an if statement and is better integrated with GRUB.
--
Marco
Quoted 3 lines · Marco Gerards
Right, I felt it hard to explain the direction I'd like to take on this list. So I have implemented the core functionality because that definitely has to be right.
I don't know what direction you would like yet. It is hard to figure out all of your ideas from the implementation. I only know that it is differently implemented.
Quoted 5 lines
Quoted 1 line
- What are disadvantages in Vladimir's implementation?
In my opinion it is not clean enough and does not integrate with GRUB like I would like it to. And Vladimir's patch was way too hard to maintain when I started working on this a week ago.
Can you elaborate on those points? I still don't understand.
Quoted 6 lines
Quoted 3 lines
- What are disadvantages in Marco's implementation?
Fewer features are implemented. The kernel is bloated.
What do you mean with the kernel is bloated? My patch moves a lot of responsibility to commands. I would say it is less bloated.
I mentioned this, because your parser is built into the kernel. This is unnecessary, as the rescue mode does not need fancy features.
Okuji
Quoted 8 lines · Yoshinori K. Okuji
Quoted 3 lines · Marco Gerards
Right, I felt it hard to explain the direction I'd like to take on this list. So I have implemented the core functionality because that definitely has to be right.
I don't know what direction you would like yet. It is hard to figure out all of your ideas from the implementation. I only know that it is differently implemented.
Sure, but I have explained it in detail in the emails I sent to the list a week ago. My most important point was that I did not want to use pcode.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
- What are disadvantages in Vladimir's implementation?
In my opinion it is not clean enough and does not integrate with GRUB like I would like it to. And Vladimir's patch was way too hard to maintain when I started working on this a week ago.
Can you elaborate on those points? I still don't understand.
That was about the previous patch Vladimir wrote. His current patch is based on my code, AFAICS.
I had a review of his previous ( > 1 week ago) code, and had some problems with it:
- It was very hard to understand.
- It was all handwritten (later bison was used, which is a good thing).
- Poorly documented.
- Little reuse or adoption of existing GRUB code.
- No well known design and execution model. I just based my design on the model bash uses.
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
- What are disadvantages in Marco's implementation?
Fewer features are implemented. The kernel is bloated.
What do you mean with the kernel is bloated? My patch moves a lot of responsibility to commands. I would say it is less bloated.
I mentioned this, because your parser is built into the kernel. This is unnecessary, as the rescue mode does not need fancy features.
It is build into normal mode. And even much is not put in normal mode, but in commands like `['.
The thing you see in the kernel is a replacement for the command to split lines into arguments. The old code for this was removed. So some code was removed and some was added, the end result is about the same, but more reusable code.
--
Marco
After discussion with Marco on IRC we decided that his code is better for the core features and other things are fast to implement. But the next questions are menu entries. I propose the following syntax:
menu [arguments] name
{
Commands
}
arguments can be --default, --fallback perhaps some more in the future.
But the main question is about realization. We need to store the source code to be able to edit it. Marco and me propose the following solution:
lexer keeps the buffer of current parsed script/block and it stores begin and end of tokens in this buffer in yylloc's new fields: buf_beg and buf_end. Then when parser founds a menu entry it copies a part of buffer using position of command. E.g (schematically):
"menu" string '{' commands '}'
{
char was = buf[@4.buf_end];
buf[@4.buf_end] = 0;
create_menu_command ($2, grub_strdup (&buf[@4.buf_beg]));
}
And menu is always stored unparsed and parsed only just before execution. Than editing is no problem.
Other solution proposed by Marco was to rerun the script file or part of it.
We would like to know what other people think about it
Vladimir 'phcoder' Serbinenko
Quoted 16 lines · Vladimir Serbinenko
But the next questions are menu entries. I propose the following syntax:
menu [arguments] name
{
Commands
}arguments can be --default, --fallback perhaps some more in the future.
But the main question is about realization. We need to store the source code to be able to edit it. Marco and me propose the following solution:
lexer keeps the buffer of current parsed script/block and it stores begin and end of tokens in this buffer in yylloc's new fields:buf_begandbuf_end. Then when parser founds a menu entry it copies a part of buffer using position of command. E.g (schematically):
Quoted 1 line
"menu" string '{' commands '}'
I would even propose something like:
menuargs: /* Empty */
menuargs ARG
...
;
menuentry: "menu" menuargs '{' commands '}'
;
Or something like that. Arg is something the scanner can recognize or so. I am not sure how that should work :)
Quoted 5 lines
{
char was = buf[@4.buf_end];
buf[@4.buf_end] = 0;
create_menu_command ($2, grub_strdup (&buf[@4.buf_beg]));
}I didn't know that bison had such buffer. I think it is cleaner to use grub_strndup so you don't have to modify the buffer. You also have to pass the result of `commands' ($4 in your example) to create_menu_command.
Quoted 2 lines
And menu is always stored unparsed and parsed only just before execution. Than editing is no problem.
I think you have to parse it immediately and both store parsed and the unparsed code. After editing you do the same again.
Quoted 2 lines
Other solution proposed by Marco was to rerun the script file or part of it.
This solution is required if you want to be able to edit the complete script instead of just the separate menu entries. I think this is not interesting, but if someone wants to know I can say more about this.
Thanks,
Marco
Quoted 4 lines · Marco Gerards
I didn't know that bison had such buffer.
It doesn't. This buffer is created by lexer in my example. And menu is always stored unparsed and parsed only just before
Quoted 9 lines
Quoted 3 lines
execution. Than editing is no problem.
I think you have to parse it immediately and both store parsed and the unparsed code. After editing you do the same again.
I see 2 approaches:
1) Parse menu commands right before executing
2) Parse them directly and reparse after editing.
I think 1st one is cleaner because the parser will be at the same place after editing or before
Quoted 5 lines
This solution is required if you want to be able to edit the complete script instead of just the separate menu entries. I think this is not interesting, but if someone wants to know I can say more about this.
I think also. Because theese modifications are lost after boot they are generally modified only to boot one time an OS with specifical parameters and you don't need scripts for this.
Vladimir 'phcoder' Serbinenko
Quoted 8 lines · Vladimir Serbinenko
Quoted 3 lines
I think you have to parse it immediately and both store parsed and the unparsed code. After editing you do the same again.
I see 2 approaches:
1) Parse menu commands right before executing
2) Parse them directly and reparse after editing.
I think 1st one is cleaner because the parser will be at the same place after editing or before
Same for #2. And #2 matches the rest of the code. Otherwise you are parsing the code twice. One when reading it and once when executing it. The code is already parsed by bison. If you ignore the result of this parsing you are causing a memory leak and wasting processor time. It is done already so why not use it? :)
So the problem with approach #1 is doing double work, less obvious integration with the rest of the parser and memory leakage.
Quoted 8 lines
Quoted 5 lines
This solution is required if you want to be able to edit the complete script instead of just the separate menu entries. I think this is not interesting, but if someone wants to know I can say more about this.
I think also. Because theese modifications are lost after boot they are generally modified only to boot one time an OS with specifical parameters and you don't need scripts for this.
Right. The only reason I see for editing the script in general is to edit some menu generation routine. But that is a very rare case I think. If it turns out not to be a rare case, we can provide this feature later on.
--
Marco
Quoted 6 lines · Marco Gerards
Vladimir Serbinenko <[email protected]> writes:
So the problem with approach #1 is doing double work, less obvious integration with the rest of the parser and memory leakage.
Of course in real code it would be freed. It was just schemathical
Quoted 7 lines
Right. The only reason I see for editing the script in general is to edit some menu generation routine. But that is a very rare case I think. If it turns out not to be a rare case, we can provide this feature later on.
Even if it's not generating is used to create many menus or to create menus from templates and anyway you boot only 1 menu entry and only interesting thing is generated code, not the template or generating method.
Vladimir 'phcoder' Serbinenko
Quoted 9 lines · Vladimir Serbinenko
Quoted 6 lines · Marco Gerards
Vladimir Serbinenko <[email protected]> writes:
So the problem with approach #1 is doing double work, less obvious integration with the rest of the parser and memory leakage.
Of course in real code it would be freed. It was just schemathical
Right, but the other disadvantages remain. So I really want approach #2 to be used unless it has a clear disadvantage.
--
Marco
I don't have many comments, since I'm not familiar with lexing and parsing. Just a couple small comments:
Quoted 8 lines · Marco Gerards
if' accepts any command. I showed the example of using the[' command. It can be made similar to the[' command of coreutils. This command sets the environment variableRESULT' to either 0 or 1. After executionif' checksRESULT' and either executes theif' or theelse' branch. It would also be possible to use a return value instead of `RESULT', but that was too much trouble for me at first. But of course this can be changed if someone can convince me of that.
You want it to be "$RESULT" instead of "$?" ?
I really don't like that each command has to explicitly set RESULT. As you note, it would be better if the return code from the command were automatically placed into the status environment variable.
Quoted 16 lines
lsb.c new file +16
-0+#ifdef GRUB_UTIL +void +grub_lsb_init (void ) +{ +grub_register_command ("[" , grub_cmd_lsb,GRUB_COMMAND_FLAG_CMDLINE , +"[ EXPRESSION ]" ,"Evaluate an expression" ,0 ); +} + +void +grub_lsb_fini (void ) +{ +grub_unregister_command ("[" ); +} +#else /* ! GRUB_UTIL */ +GRUB_MOD_INIT +{
lsb.c line 83
Quoted 10 lines
lsb.c new file +10
-0+ (void )mod;/* To stop warning. */ +grub_register_command ("[" , grub_cmd_lsb,GRUB_COMMAND_FLAG_CMDLINE , +"[ EXPRESSION ]" ,"Evaluate an expression" ,0 ); +} + +GRUB_MOD_FINI +{ +grub_unregister_command ("[" ); +} +#endif /* ! GRUB_UTIL */
We *really* need to redefine GRUB_MOD_INIT/FINI to remove all this duplicated code. I guess I will add that to my list.
grub/script.h line 26
Quoted 6 lines
grub/script.h new file +6
-0+/* A part of an argument. */ +struct grub_script_arg +{ +/* If this is 0, STR is a string. If it is one, STR is a variable +name. */ +int type;
This should probably be an enum.
Since this code won't break any existing behavior (simple "ls (hd,0)/" will still work, right?), I guess it can be committed as soon as serious issues like the memory leak have been fixed.
-Hollis
Quoted 13 lines · Hollis Blanchard
I don't have many comments, since I'm not familiar with lexing and parsing. Just a couple small comments:
Quoted 8 lines · Marco Gerards
if' accepts any command. I showed the example of using the[' command. It can be made similar to the[' command of coreutils. This command sets the environment variableRESULT' to either 0 or 1. After executionif' checksRESULT' and either executes theif' or theelse' branch. It would also be possible to use a return value instead of `RESULT', but that was too much trouble for me at first. But of course this can be changed if someone can convince me of that.
Quoted 1 line
You want it to be "$RESULT" instead of "$?" ?
You are right.
Quoted 3 lines
I really don't like that each command has to explicitly set RESULT. As you note, it would be better if the return code from the command were automatically placed into the status environment variable.
Most command return grub_err_t. The only commands that matter for us are commands like `['. Would you propose every commands returns an int and that on function return grub_errno is checked?
Quoted 21 lines
Quoted 21 lines
lsb.c new file +21
-0+#ifdef GRUB_UTIL +void +grub_lsb_init (void ) +{ +grub_register_command ("[" , grub_cmd_lsb,GRUB_COMMAND_FLAG_CMDLINE , +"[ EXPRESSION ]" ,"Evaluate an expression" ,0 ); +} + +void +grub_lsb_fini (void ) +{ +grub_unregister_command ("[" ); +} +#else /* ! GRUB_UTIL */ +GRUB_MOD_INIT +{ + (void )mod;/* To stop warning. */ +grub_register_command ("[" , grub_cmd_lsb,GRUB_COMMAND_FLAG_CMDLINE , +"[ EXPRESSION ]" ,"Evaluate an expression" ,0 ); +} +
Quoted 8 lines
lsb.c line 83
Quoted 5 lines
lsb.c new file +5
-0+GRUB_MOD_FINI +{ +grub_unregister_command ("[" ); +} +#endif /* ! GRUB_UTIL */ We *really* need to redefine GRUB_MOD_INIT/FINI to remove all this duplicated code. I guess I will add that to my list.
Cool :-)
Quoted 8 lines
grub/script.h line 26
Quoted 6 lines
grub/script.h new file +6
-0+/* A part of an argument. */ +struct grub_script_arg +{ +/* If this is 0, STR is a string. If it is one, STR is a variable +name. */ +int type;This should probably be an enum.
Yes, good catch.
Quoted 3 lines
Since this code won't break any existing behavior (simple "ls (hd,0)/" will still work, right?), I guess it can be committed as soon as serious issues like the memory leak have been fixed.
That is right. All normal code will continue to function, otherwise it would be a bug or have been discussed on the list. So far there is nothing that would break the current behavior except multiple lines (which I should fix before committing).
--
Marco
Quoted 2 lines · Vladimir Serbinenko
After discussion with Marco on IRC we decided that his code is better for the core features and other things are fast to implement.
I feel very happy with that both of you have agreed.
Okuji
Quoted 7 lines · Marco Gerards
Quoted 3 lines
I really don't like that each command has to explicitly set RESULT. As you note, it would be better if the return code from the command were automatically placed into the status environment variable.
Most command return
grub_err_t. The only commands that matter for us are commands like `['. Would you propose every commands returns an int and that on function returngrub_errnois checked?
I agree with Hollis. It should be automatic. What is wrong with setting $? to grub_errno? Isn't it enough to see if it is GRUB_ERR_NONE or not?
BTW, you added lsb.c, but I don't like this very much. On Unix, the testing command is "test", and "[" is just an aliases, isn't it? I prefer to use commands/test.c, and register "[" as another name of test.
Okuji
Quoted 11 lines · Yoshinori K. Okuji
Quoted 7 lines · Marco Gerards
Quoted 3 lines
I really don't like that each command has to explicitly set RESULT. As you note, it would be better if the return code from the command were automatically placed into the status environment variable.
Most command return
grub_err_t. The only commands that matter for us are commands like `['. Would you propose every commands returns an int and that on function returngrub_errnois checked?I agree with Hollis. It should be automatic. What is wrong with setting $? to
grub_errno? Isn't it enough to see if it isGRUB_ERR_NONEor not?
Perhaps there is no problem with that. I just assumed it should be an int. I will make this change.
Quoted 3 lines
BTW, you added
lsb.c, but I don't like this very much. On Unix, the testing command is "test", and "[" is just an aliases, isn't it? I prefer to usecommands/test.c, and register "[" as another name of test.
You are right. I am not that familiar with shell scripting, so there might be more stupid mistakes you could all catch. Please tell me about them.
Thanks,
Marco
Quoted 11 lines · Marco Gerards
Quoted 9 lines · Yoshinori K. Okuji
Quoted 7 lines · Marco Gerards
Quoted 3 lines
I really don't like that each command has to explicitly set RESULT. As you note, it would be better if the return code from the command were automatically placed into the status environment variable.
Most command return
grub_err_t. The only commands that matter for us are commands like `['. Would you propose every commands returns an int and that on function returngrub_errnois checked?
Quoted 5 lines
Quoted 2 lines
I agree with Hollis. It should be automatic. What is wrong with setting $? to
grub_errno? Isn't it enough to see if it isGRUB_ERR_NONEor not?Perhaps there is no problem with that. I just assumed it should be an int. I will make this change.
To which value should grub_err_t be set on function return in the case of the test command? I could add a new error `GRUB_ERR_TEST_RESULT' and make it 1. I would prefer some better suggestion though :).
--
Marco
Quoted 3 lines · Marco Gerards
To which value should
grub_err_tbe set on function return in the case of the test command? I could add a new error `GRUB_ERR_TEST_RESULT' and make it 1. I would prefer some better suggestion though :).
Vladimir used GRUB_ERR_TEST_FAILURE. Not bad.
Okuji
Marco Gerards <[email protected]> writes:
Hi,
Quoted 3 lines
Here is the patch I promised last week. It is far from perfect and will most likely break one or two things, but I think it is worth the trouble to add it for the following reasons:
Here is a new version. If you have some time, please have a look at it. If I don't hear about any important problems I will commit this patch at the end of the weekend.
Quoted 3 lines
Things that don't work, are broken, etc:
- The parser does not do good error checking of the script yet.
The error message is still cryptic, but at least things don't go completely wrong is some situations. No memory leaks! :-)
Quoted 1 line
- Only single lines of code are supported.
This is fixed.
Quoted 1 line
- No real scripts are parsed, only the command line.
It should be easy to add.
Quoted 9 lines
- Not integrated with the menu yet.
-
foo=bar' was removed for now. I will restore it, but for now you have to useset foo=bar'.- The syntax it supports is not loose enough yet. For example, this does not work:
if [ $a=foo ]; then ls else help fi
This is somewhat improved.
Thanks,
Marco
2005-11-03 Marco Gerards <[email protected]>
Add initial scripting support.
* commands/test.c: New file.
* include/grub/script.h: Likewise.
* normal/execute.c: Likewise.
* normal/function.c: Likewise.
* normal/lexer.c: Likewise.
* normal/parser.y: Likewise.
* normal/script.c: Likewise.
* configure.ac: Add `AC_PROG_YACC' test.
* conf/i386-pc.rmk (grub_emu_SOURCES): Add commands/test.c', normal/execute.c', normal/lexer.c', grub_script.tab.c', normal/function.c' and normal/script.c'. (normal_mod_SOURCES): normal/execute.c', normal/lexer.c', grub_script.tab.c', normal/function.c' and normal/script.c'. (test_mod_SOURCES, test_mod_CFLAGS, test_mod_LDFLAGS): New variables. (pkgdata_MODULES): Add test.mod'.
(grub_script.tab.c): New rule.
(grub_script.tab.h): Likewise.
* include/grub/err.h (grub_err_t): Add `GRUB_ERR_TEST_FAILURE'.
* include/grub/normal.h (grub_test_init): New prototype. (grub_test_fini): Likewise.
* normal/command.c: Include <grub/script.h>. (grub_command_execute): Rewritten.
* util/grub-emu.c (main): Call grub_test_init' and grub_test_fini'.
Index: configure.ac
===================================================================
RCS file: /cvsroot/grub/grub2/configure.ac,v
retrieving revision 1.15
configure.ac +1 -0
@@ -44,6 +44,7 @@ if test "x$CFLAGS" = x; then fi AC_PROG_CC +AC_PROG_YACC AC_SYS_LARGEFILE # Must be GCC.
Index: commands/test.c
===================================================================
RCS file: commands/test.c
test.c new file +89 -0
89 lines not on this page · raw message
Index: conf/i386-pc.rmk
===================================================================
RCS file: /cvsroot/grub/grub2/conf/i386-pc.rmk,v
retrieving revision 1.50
i386-pc.rmk +24 -10
@@ -46,6 +46,13 @@ DEFSYMFILES += kernel_syms.lst symlist.c: $(addprefix include/grub/,$(kernel_img_HEADERS)) gensymlist.sh sh $(srcdir)/gensymlist.sh $(filter %.h,$^) > $@ +# For the parser. +grub_script.tab.c: normal/parser.y + $(YACC) -d -p grub_script_yy -b grub_script $(srcdir)/normal/parser.y +grub_script.tab.h: normal/parser.y + $(YACC) -d -p grub_script_yy -b grub_script $(srcdir)/normal/parser.y + + kernel_syms.lst: $(addprefix include/grub/,$(kernel_img_HEADERS)) genkernsyms.sh sh $(srcdir)/genkernsyms.sh $(filter %h,$^) > $@
@@ -80,19 +87,20 @@ grub_probefs_SOURCES = util/i386/pc/grub # For grub-emu. grub_emu_SOURCES = commands/boot.c commands/cat.c commands/cmp.c \ commands/configfile.c commands/default.c commands/help.c \ - commands/terminal.c commands/ls.c commands/search.c \ - commands/timeout.c \ + commands/terminal.c commands/ls.c commands/test.c \ + commands/search.c commands/timeout.c \ commands/i386/pc/halt.c commands/i386/pc/reboot.c \ disk/loopback.c \ fs/affs.c fs/ext2.c fs/fat.c fs/fshelp.c fs/hfs.c fs/iso9660.c \ fs/jfs.c fs/minix.c fs/sfs.c fs/ufs.c fs/xfs.c \ io/gzio.c \ kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c \ - kern/file.c kern/fs.c kern/loader.c kern/main.c kern/misc.c \ - kern/parser.c kern/partition.c kern/rescue.c kern/term.c \ - normal/arg.c normal/cmdline.c normal/command.c \ + normal/execute.c kern/file.c kern/fs.c normal/lexer.c \ + kern/loader.c kern/main.c kern/misc.c kern/parser.c \ + grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c \ + normal/arg.c normal/cmdline.c normal/command.c normal/function.c\ normal/completion.c normal/context.c normal/main.c \ - normal/menu.c normal/menu_entry.c normal/misc.c \ + normal/menu.c normal/menu_entry.c normal/misc.c normal/script.c \ partmap/amiga.c partmap/apple.c partmap/pc.c partmap/sun.c \ util/console.c util/grub-emu.c util/misc.c \ util/i386/pc/biosdisk.c util/i386/pc/getroot.c \
@@ -117,7 +125,7 @@ pkgdata_MODULES = _chain.mod _linux.mod apple.mod pc.mod sun.mod loopback.mod reboot.mod halt.mod \ help.mod default.mod timeout.mod configfile.mod vbe.mod \ vesafb.mod vbetest.mod vbeinfo.mod search.mod gzio.mod \ - terminfo.mod serial.mod xfs.mod affs.mod sfs.mod + terminfo.mod serial.mod xfs.mod affs.mod sfs.mod test.mod # For _chain.mod. _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -196,9 +204,10 @@ linux_mod_LDFLAGS = $(COMMON_LDFLAGS) # For normal.mod. normal_mod_SOURCES = normal/arg.c normal/cmdline.c normal/command.c \ - normal/completion.c normal/context.c normal/main.c \ - normal/menu.c normal/menu_entry.c normal/misc.c \ - normal/i386/setjmp.S + normal/completion.c normal/context.c normal/execute.c \ + normal/function.c normal/lexer.c normal/main.c normal/menu.c \ + normal/menu_entry.c normal/misc.c grub_script.tab.c \ + normal/script.c normal/i386/setjmp.S normal_mod_CFLAGS = $(COMMON_CFLAGS) normal_mod_ASFLAGS = $(COMMON_ASFLAGS) -m32 normal_mod_LDFLAGS = $(COMMON_LDFLAGS)
@@ -347,3 +356,8 @@ search_mod_LDFLAGS = $(COMMON_LDFLAGS) gzio_mod_SOURCES = io/gzio.c gzio_mod_CFLAGS = $(COMMON_CFLAGS) gzio_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For test.mod. +test_mod_SOURCES = commands/test.c +test_mod_CFLAGS = $(COMMON_CFLAGS) +test_mod_LDFLAGS = $(COMMON_LDFLAGS)
Index: include/grub/err.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/err.h,v retrieving revision 1.9
grub/err.h +1 -0
@@ -26,6 +26,7 @@ typedef enum { GRUB_ERR_NONE = 0, + GRUB_ERR_TEST_FAILURE, GRUB_ERR_BAD_MODULE, GRUB_ERR_OUT_OF_MEMORY, GRUB_ERR_BAD_FILE_TYPE,
Index: include/grub/normal.h
===================================================================
RCS file: /cvsroot/grub/grub2/include/grub/normal.h,v retrieving revision 1.22
grub/normal.h +2 -0
@@ -224,6 +224,8 @@ void grub_configfile_init (void);void grub_configfile_fini (void );void grub_search_init (void );void grub_search_fini (void ); +void grub_test_init (void ); +void grub_test_fini (void );#endif #endif /* ! GRUB_NORMAL_HEADER */
Index: include/grub/script.h
===================================================================
RCS file: include/grub/script.h
grub/script.h new file +189 -0
189 lines not on this page · raw message
Index: normal/command.c
===================================================================
RCS file: /cvsroot/grub/grub2/normal/command.c,v
retrieving revision 1.13
command.c +13 -50
88 lines not on this page · raw message
Index: normal/execute.c
===================================================================
RCS file: normal/execute.c
execute.c new file +204 -0
204 lines not on this page · raw message
Index: normal/function.c
===================================================================
RCS file: normal/function.c
function.c new file +127 -0
127 lines not on this page · raw message
Index: normal/lexer.c
===================================================================
RCS file: normal/lexer.c
lexer.c new file +274 -0
274 lines not on this page · raw message
Index: normal/parser.y
===================================================================
RCS file: normal/parser.y
parser.y new file +191 -0
191 lines not on this page · raw message
Index: normal/script.c
===================================================================
RCS file: normal/script.c
script.c new file +289 -0
289 lines not on this page · raw message
Index: util/grub-emu.c
===================================================================
RCS file: /cvsroot/grub/grub2/util/grub-emu.c,v
retrieving revision 1.26
grub-emu.c +2 -0
@@ -219,6 +219,7 @@ main (int argc, char *argv[])grub_timeout_init ();grub_configfile_init ();grub_search_init (); +grub_test_init ();/* XXX: Should normal mode be started by default? */ grub_normal_init ();
@@ -227,6 +228,7 @@ main (int argc, char *argv[])if (setjmp (main_env) ==0 )grub_main (); +grub_test_fini ();grub_search_fini ();grub_configfile_fini ();grub_timeout_fini ();
Quoted 11 lines · Marco Gerards
Marco Gerards <[email protected]> writes:
Hi,
Quoted 3 lines
Here is the patch I promised last week. It is far from perfect and will most likely break one or two things, but I think it is worth the trouble to add it for the following reasons:
Here is a new version. If you have some time, please have a look at it. If I don't hear about any important problems I will commit this patch at the end of the weekend.
Committed!
It does not work on the PPC yet, but I'll take care of that tomorrow.
Please test if everything still works. Of course I took great care in testing and implementing, but a lot changed and I am not perfect. This week I will have some time, so if there are any problems reported, they should be fixed in no time.
Thanks,
Marco
Quoted 5 lines · Marco Gerards
Please test if everything still works. Of course I took great care in testing and implementing, but a lot changed and I am not perfect. This week I will have some time, so if there are any problems reported, they should be fixed in no time.
Could you add a configure check for yacc? I didn't have it installed on my system, and got a compile error.
-Hollis
Quoted 9 lines · Hollis Blanchard
Quoted 5 lines · Marco Gerards
Please test if everything still works. Of course I took great care in testing and implementing, but a lot changed and I am not perfect. This week I will have some time, so if there are any problems reported, they should be fixed in no time.
Could you add a configure check for yacc? I didn't have it installed on my system, and got a compile error.
That is really weird, I have added such test:
* configure.ac: Add `AC_PROG_YACC' test.
When configuring I see:
checking for bison... bison -y
Did you see that test and what did it output?
--
Marco
Quoted 14 lines · Marco Gerards
Quoted 9 lines · Hollis Blanchard
Quoted 5 lines · Marco Gerards
Please test if everything still works. Of course I took great care in testing and implementing, but a lot changed and I am not perfect. This week I will have some time, so if there are any problems reported, they should be fixed in no time.
Could you add a configure check for yacc? I didn't have it installed on my system, and got a compile error.
That is really weird, I have added such test:
Quoted 7 lines
* configure.ac: Add `AC_PROG_YACC' test.
When configuring I see:
checking for bison... bison -y
Did you see that test and what did it output?
checking for bison... no
checking for byacc... no
And then it happily continued to configure and make, with the eventual output:
yacc -d -p grub_script_yy -b grub_script ../normal/parser.y
make: yacc: Command not found
It's a little late for me, otherwise I'd have a look at configure.ac myself.
-Hollis