Scripting (IMPORTANT!)
Hi,
As you noticed I put important in the topic. I personally consider scripting one of the most important features that has to be implemented before GRUB 2 can be used in distributions.
It's also one of the features that we all have to talk about before we determine it will not be changed. After GRUB 2 is being used by everyone it will be hard, if not impossible, to make changes that make different GRUB 2 versions incompatible.
So it is important that most active developers read my ideas about scripting and what I currently implemented. As you might have noticed, I wrote this in little time and thus it will be a bit chaotic and it is not well written. But I hope I can start this important discussion this way.
Another thing I want to discuss soon is using "cd" and "pwd" commands to change the current directory instead of what we have now. I would like to make this change as well.
I'm looking forwards to your ideas, questions, suggestions, criticism and bug reports. :-)
--
Marco
=========
Variables
=========
In GRUB 2 there are two types of variables. The difference is like in
bash: the scope. Variables either have a local or global (exported) scope. Initially all new added variables have the local scope. By using the export command they can be made global.
Some variables have a special purpose and will be created by GRUB. These are:
- root: The active disk/partition (rw, global)
- prefix: The pathname used to load grub.cfg and the modules (rw, global)
- platform: Set to the platform, eg. EFI, IEEE1275, PCBIOS, etc. (ro, global)
- processor: Processor architecture GRUB was compiled for, eg. PPC, x86 (ro, global).
- debug: Print debugging messages for certain parts of the code, or
all messages when set to all. (rw, global?)
- pager: When set to `1', wait for a key when the bottom of the screen was reached.
(more will be added, like variables to read time/date, random, etc)
To include a file two commands can be used. The first is source, which does not change the scope. The second is `configfile' which opens a new scope.
Variables can be used like in bash. You can use set to set or list variables. Explicit assignments (foo=bar' instead of set foo=bar') is possible. You can include variables in command lines by using the $' prefix, for example $foo. To use it to concatenate variable contents and text, you can use the ${foo} syntax. Sometimes you want to use the $' in commands (for example, multiboot lines for the Hurd) without meaning a variable, in that case you can use single quotes for escaping (for example, '$foo').
==> Currently unimplemented: The export command, special variables
other than root, prefix, debug and pager. Concatenating with variables is currently broken.
=========
Functions
=========
Functions can be created as follows:
function funcname { code }
After that they can be called just like any other command.
Arguments can be passed to functions. In the function body they can be accessed as in bash ($1, $2, etc).
==> Currently unimplemented: Function arguments.
==> Idea: Exporting functions to be full featured commands that can be
tab completed, documented, etc. This way functions can be implemented by scripts instead of C code.
====
If
====
The if command is also quite similar to the if command in bash. It has the following syntax:
if command ; then commands fi
OR:
if command ; then commands else commands fi
The command that follows the if contains the condition that is tested for. You can use any command and check the return status like this. The most useful command to use here is the test' (or [') command. It is similar to the test command in bash, although it can not check for certain filesystem bits like file protection and certain filetypes.
==> Currently unimplemented: The test command (although it is almost
finished).
============
Menu entries
============
Menu entries are added with menuentry' (or its alias @'). It's important to notice this is not a command. Because it's part of the scripting syntax, it can have unique features. A menuentry is like a function, especially because it can have arguments!
Syntax: menuentry "Entry name" [arguments...] { body }
Make sure the entry name is properly escaped, otherwise spaces can not be included and will be seen as separator. The arguments will become semi-variables like #1, #2, etc. These can be used so one menuentry can be used as a template for a lot of menuentries. This can best be explained with an example:
(inside loop that sets i = 1...x)
@ "Partition $i" $i { linux /vmlinuz root=/dev/hda#{1} }
This will set #1 to $i (it's the first argument). The menuentry command is executed for i=1...9 and will generate 9 different menu entries. This is useful for automatic generation of menu entries.
==> Currently unimplemented: Arguments for automatic generation of
menuentries.
==> Discussion: Perhaps I will change the #1 into $1, although this is
not really a variable.
=====
for
=====
The for command can be used to iterate over a set of data. I don't like the idea of implementing this *exactly* like in bash. Personally I am thinking of the following syntax:
Iterating over files:
for x in (hd0,3)/foo/* ; do commands ; done
The commands will be executed for each file in the directory.
Iterating over disks:
for x in (*) ; do commands ; done
Iterating over partitions:
for x in (*,*); do commands ; done
An alternative syntax which I think is more natural could be:
foreach x in files ; do commands ; done foreach x in disks ; do commands ; done foreach x in partitions (hd0) ; do commands ; done foreach x in modules ; do commands ; done
The foreach syntax makes it possible to add named iterators with arguments. In that case modules can be written that add new iterators. Perhaps even scrips can be used for this. I don't like the idea of using the standard output of files for this, because I am afraid this will get messy and tricky.
==> Currently unimplemented: All of this.
===========================
Other language constructs
===========================
Other language constructs can and will be implemented. Like: elif,
while, case, select, etc.
==> Currently unimplemented: All these constructs.
===============================
Commands useful for scripting
===============================
- echo: Prints text.
- cmp: Compares files.
- configfile/source: Including other files.
- search: Search for files, labels, etc. and set a variable.
- test: Test certain conditions.
==> Currently unimplemented: Echo can't "beep".
================
Error handling
================
Every command can return an error value. This error value will be available as $?. This variable will be checked by if, while, etc. In scripts all parsing errors will be non fatal so the system is not left unbootable.
Unparsable menu entries will be added, but will not be executable, only the editor is available.
One special purpose variable $ERROR will be added. It will contain the error strings when $? is set to non-zero. In that case you can do error handling in scripts.
After every command an error is printed by default. You don't want this to be silent, otherwise users can not detect errors. But it should be able to set error handling to silent because a certain script or function does its own error handling. This can be done by setting $ERRORS=0 (default it is set to 1).
==> Currently unimplemented: $ERROR, $ERRORS, a lot of this logic is
far from perfect.
Quoted 2 lines · Marco Gerards
Iterating over files:
for x in (hd0,3)/foo/* ; do commands ; done
I must say I would prefer this over the other ("foreach") suggestion, since I'm more used to it.
Anyhow, is your plan to make expansion available through out the whole shell, or only for "for"? It would be nice to be able to do;
grub> ls (*,*)/boot/vmlinuz*
Quoted 3 lines
One special purpose variable $ERROR will be added. It will contain the error strings when $? is set to non-zero. In that case you can do error handling in scripts.
I'm afraid there might be situations where the user do not realize that $ERROR has been overwritten. Bogus example;
if cat (hd0,1)/foo/bar; then
ls (hd0,1)/foo
echo "*** 'bar' is missing: $ERROR"fi
The execution of 'ls' will overwrite $ERROR.
~j
Quoted 6 lines · Marco Gerards
=========
Variables
=========- root: The active disk/partition (rw, global)
...
What about a 'prompt' variable?
~j
Quoting Marco Gerards <[email protected]>:
Hi,
a few remarks:
[...]
Quoted 10 lines
============
Menu entries
============Menu entries are added with
menuentry' (or its alias@'). It's important to notice this is not a command. Because it's part of the scripting syntax, it can have unique features. A menuentry is like a function, especially because it can have arguments!Syntax: menuentry "Entry name" [arguments...] { body }
It would be nice to be able to add properties to a menuentry. My first idea is a logo property.
I think this could be done through a new command such as:
logo "Entry name" filename
Quoted 8 lines
Make sure the entry name is properly escaped, otherwise spaces can not be included and will be seen as separator. The arguments will become semi-variables like #1, #2, etc. These can be used so one menuentry can be used as a template for a lot of menuentries. This can best be explained with an example:
(inside loop that sets i = 1...x)
@ "Partition $i" $i { linux /vmlinuz root=/dev/hda#{1} }
Why not substituing variable at creation (unsubstitued one can be escaped) ?
Eg: @ "Partition $i" { linux /vmlinux root=/dev/hda$1 prefix=$$prefix } Humm maybe too complex ?
Tristan.
Quoted 2 lines · Marco Gerards
I'm looking forwards to your ideas, questions, suggestions, criticism and bug reports. :-)
How hard do you think it will be to implement all these features (and handle the bugs)?
I wonder if we wouldn't be better served by a simpler scripting language, rather than trying to emulate bash.
-Hollis
Quoted 6 lines · Hollis Blanchard
Quoted 2 lines · Marco Gerards
I'm looking forwards to your ideas, questions, suggestions, criticism and bug reports. :-)
How hard do you think it will be to implement all these features (and handle the bugs)?
Most of it is implemented already. Other features will be implemented.
Some things are easy to implement. Like the test and expr commands. I hope I can move the iterators to modules as well. Modules are easier to maintain, because of its interfaces.
But of course there are things that I do not like, when talking about maintainability:
- The lexer is handcoded, I prefer a generated scanner.
- Preventing memory leaks is annoying.
- Perhaps more thoughts about error handling is required.
Quoted 2 lines
I wonder if we wouldn't be better served by a simpler scripting language, rather than trying to emulate bash.
I think it is simple already. But suggestions to make the code easier to read, increase maintainability and flexibility are always welcome.
--
Marco
Quoted 4 lines · Marco Gerards
It's also one of the features that we all have to talk about before we determine it will not be changed. After GRUB 2 is being used by everyone it will be hard, if not impossible, to make changes that make different GRUB 2 versions incompatible.
I agree. And I believe that GRUB should follow BASH whenever reasonable, so that the user does not have to learn new things only for GRUB. Inventing a new syntax is something I dislike. Also, I believe that one should write a new module if she wants another language in GRUB, and execute it by a command, as in GNU.
Quoted 3 lines
Another thing I want to discuss soon is using "cd" and "pwd" commands to change the current directory instead of what we have now. I would like to make this change as well.
I thought the same thing before, but I didn't, because the effect of setting the root device has a different meaning, that is, to set a boot device for the chainloader. Besides this, the root variable is very similar to the concept of "current working directory" in Unix. So I wouldn't object strongly, even if you change it this way.
Quoted 12 lines
=========
Variables
=========In GRUB 2 there are two types of variables. The difference is like in
bash: the scope. Variables either have a local or global (exported) scope. Initially all new added variables have the local scope. By using the export command they can be made global.Some variables have a special purpose and will be created by GRUB. These are:
Quoted 8 lines
- root: The active disk/partition (rw, global)
- prefix: The pathname used to load grub.cfg and the modules (rw, global)
- platform: Set to the platform, eg. EFI, IEEE1275, PCBIOS, etc. (ro, global) - processor: Processor architecture GRUB was compiled for, eg. PPC, x86 (ro, global). - debug: Print debugging messages for certain parts of the code, or all messages when set to all. (rw, global?)
- pager: When set to `1', wait for a key when the bottom of the screen was reached.
BTW the Intel's EFI Shell uses an option to a command to enable a pager. This way might be more convenient than a variable. Or a pipe? I think a pipe sounds overkill, though.
Quoted 20 lines
============
Menu entries
============Menu entries are added with
menuentry' (or its alias@'). It's important to notice this is not a command. Because it's part of the scripting syntax, it can have unique features. A menuentry is like a function, especially because it can have arguments!Syntax: menuentry "Entry name" [arguments...] { body }
Make sure the entry name is properly escaped, otherwise spaces can not be included and will be seen as separator. The arguments will become semi-variables like #1, #2, etc. These can be used so one menuentry can be used as a template for a lot of menuentries. This can best be explained with an example:
(inside loop that sets i = 1...x)
@ "Partition $i" $i { linux /vmlinuz root=/dev/hda#{1} }
Quoted 8 lines
This will set #1 to $i (it's the first argument). The menuentry command is executed for i=1...9 and will generate 9 different menu entries. This is useful for automatic generation of menu entries.
==> Currently unimplemented: Arguments for automatic generation of
menuentries.
==> Discussion: Perhaps I will change the #1 into $1, although this is
not really a variable.
As I said before, menu entries are a special type of functions. So the arguments must be passed as $1, $2, etc.
Quoted 10 lines
=====
for
=====The for command can be used to iterate over a set of data. I don't like the idea of implementing this *exactly* like in bash. Personally I am thinking of the following syntax:
Iterating over files:
for x in (hd0,3)/foo/* ; do commands ; done
How is this different from BASH? The asterisk is interpreted as a wildcard, and this is not a part of "for" in BASH.
Quoted 16 lines
================
Error handling
================Every command can return an error value. This error value will be available as $?. This variable will be checked by if, while, etc. In scripts all parsing errors will be non fatal so the system is not left unbootable.
Unparsable menu entries will be added, but will not be executable, only the editor is available.
One special purpose variable $ERROR will be added. It will contain the error strings when $? is set to non-zero. In that case you can do error handling in scripts.
Quoted 8 lines
After every command an error is printed by default. You don't want this to be silent, otherwise users can not detect errors. But it should be able to set error handling to silent because a certain script or function does its own error handling. This can be done by setting $ERRORS=0 (default it is set to 1).
==> Currently unimplemented: $ERROR, $ERRORS, a lot of this logic is
far from perfect.
This sounds too much for me. How about supporting a subset of "set" in BASH? For example, set -d and set +d. The default can be set -d.
Okuji
Quoted 11 lines · Yoshinori K. Okuji
Quoted 4 lines · Marco Gerards
It's also one of the features that we all have to talk about before we determine it will not be changed. After GRUB 2 is being used by everyone it will be hard, if not impossible, to make changes that make different GRUB 2 versions incompatible.
I agree. And I believe that GRUB should follow BASH whenever reasonable, so that the user does not have to learn new things only for GRUB. Inventing a new syntax is something I dislike. Also, I believe that one should write a new module if she wants another language in GRUB, and execute it by a command, as in GNU.
Great. It is easy to add a new parser and bits and pieces of the AST. So adding another language should be easy by design.
Quoted 9 lines
Quoted 3 lines
Another thing I want to discuss soon is using "cd" and "pwd" commands to change the current directory instead of what we have now. I would like to make this change as well.
I thought the same thing before, but I didn't, because the effect of setting the root device has a different meaning, that is, to set a boot device for the chainloader. Besides this, the root variable is very similar to the concept of "current working directory" in Unix. So I wouldn't object strongly, even if you change it this way.
I think it makes more sense when scripting. I also think it is easier for users to deal with.
[...]
Quoted 6 lines
Quoted 2 lines
- pager: When set to `1', wait for a key when the bottom of the screen was reached.
BTW the Intel's EFI Shell uses an option to a command to enable a pager. This way might be more convenient than a variable. Or a pipe? I think a pipe sounds overkill, though.
Well, I made this a variable because we wanted variables instead of commands when possible.
Quoted 24 lines
Quoted 24 lines
============
Menu entries
============Menu entries are added with
menuentry' (or its alias@'). It's important to notice this is not a command. Because it's part of the scripting syntax, it can have unique features. A menuentry is like a function, especially because it can have arguments!Syntax: menuentry "Entry name" [arguments...] { body }
Make sure the entry name is properly escaped, otherwise spaces can not be included and will be seen as separator. The arguments will become semi-variables like #1, #2, etc. These can be used so one menuentry can be used as a template for a lot of menuentries. This can best be explained with an example:
(inside loop that sets i = 1...x)
@ "Partition $i" $i { linux /vmlinuz root=/dev/hda#{1} }This will set #1 to $i (it's the first argument). The menuentry command is executed for i=1...9 and will generate 9 different menu entries. This is useful for automatic generation of menu entries.
Quoted 7 lines
Quoted 4 lines
==> Currently unimplemented: Arguments for automatic generation of
menuentries.
==> Discussion: Perhaps I will change the #1 into $1, although this is
not really a variable.As I said before, menu entries are a special type of functions. So the arguments must be passed as $1, $2, etc.
Well, $1, $2, etc makes more sense to me as well. But it doesn't describe what really happens, although the user might not be concerned with this. I will start a discussion on this if it causes problems.
Quoted 8 lines
Quoted 8 lines
=====
for
=====The for command can be used to iterate over a set of data. I don't like the idea of implementing this *exactly* like in bash. Personally I am thinking of the following syntax:
Quoted 5 lines
Quoted 2 lines
Iterating over files:
for x in (hd0,3)/foo/* ; do commands ; doneHow is this different from BASH? The asterisk is interpreted as a wildcard, and this is not a part of "for" in BASH.
You removed the relevant context. Right, GRUB has no wildcard. I don't really understand what you mean.
Quoted 22 lines
Quoted 22 lines
================
Error handling
================Every command can return an error value. This error value will be available as $?. This variable will be checked by if, while, etc. In scripts all parsing errors will be non fatal so the system is not left unbootable.
Unparsable menu entries will be added, but will not be executable, only the editor is available.
One special purpose variable $ERROR will be added. It will contain the error strings when $? is set to non-zero. In that case you can do error handling in scripts.
After every command an error is printed by default. You don't want this to be silent, otherwise users can not detect errors. But it should be able to set error handling to silent because a certain script or function does its own error handling. This can be done by setting $ERRORS=0 (default it is set to 1).
Quoted 5 lines
Quoted 2 lines
==> Currently unimplemented: $ERROR, $ERRORS, a lot of this logic is
far from perfect.This sounds too much for me. How about supporting a subset of "set" in BASH? For example, set -d and set +d. The default can be set -d.
What do you mean by "this"? I assume you mean -e instead of -d? I hope you can check what you really mean, a -d does not exist.
--
Marco
Quoted 8 lines · Marco Gerards
Quoted 5 lines
I thought the same thing before, but I didn't, because the effect of setting the root device has a different meaning, that is, to set a boot device for the chainloader. Besides this, the root variable is very similar to the concept of "current working directory" in Unix. So I wouldn't object strongly, even if you change it this way.
I think it makes more sense when scripting. I also think it is easier for users to deal with.
I don't know. I leave the decision to you.
Quoted 6 lines
Quoted 3 lines
BTW the Intel's EFI Shell uses an option to a command to enable a pager. This way might be more convenient than a variable. Or a pipe? I think a pipe sounds overkill, though.
Well, I made this a variable because we wanted variables instead of commands when possible.
Exactly.
Quoted 3 lines
Well, $1, $2, etc makes more sense to me as well. But it doesn't describe what really happens, although the user might not be concerned with this. I will start a discussion on this if it causes problems.
I guess this wouldn't be a big problem, since most users wouldn't use this feature.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
=====
for
=====The for command can be used to iterate over a set of data. I don't like the idea of implementing this *exactly* like in bash. Personally I am thinking of the following syntax:
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Iterating over files:
for x in (hd0,3)/foo/* ; do commands ; doneHow is this different from BASH? The asterisk is interpreted as a wildcard, and this is not a part of "for" in BASH.
You removed the relevant context. Right, GRUB has no wildcard. I don't really understand what you mean.
I meant that the asterisk is not a part of a description for "for".
Quoted 5 lines
Quoted 2 lines
This sounds too much for me. How about supporting a subset of "set" in BASH? For example, set -d and set +d. The default can be set -d.
What do you mean by "this"? I assume you mean -e instead of -d? I hope you can check what you really mean, a -d does not exist.
Oops. Yes, I meant -e instead.
Okuji
Quoted 11 lines · Yoshinori K. Okuji
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
=====
for
=====The for command can be used to iterate over a set of data. I don't like the idea of implementing this *exactly* like in bash. Personally I am thinking of the following syntax:
Iterating over files:
for x in (hd0,3)/foo/* ; do commands ; done
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
How is this different from BASH? The asterisk is interpreted as a wildcard, and this is not a part of "for" in BASH.
You removed the relevant context. Right, GRUB has no wildcard. I don't really understand what you mean.
I meant that the asterisk is not a part of a description for "for".
It's good you mention this, because it's exactly the discussion I want to start. The question here is: How do we want to deal with the `for'?
In bash it iterates over all arguments. The wildcards are expanded by the shell and thus it just has a look at its arguments. The question here is, do we want to deal with wildcards? It makes the code more complex and I think there is little gain.
For GRUB I think some kind of iterators are more useful. In that case you can write a module to iterate over certain data. For example over files, disks, partitions, terminals or whatever. It leaves the bash syntax, but it is more useful in our case and modular.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
This sounds too much for me. How about supporting a subset of "set" in BASH? For example, set -d and set +d. The default can be set -d.
What do you mean by "this"? I assume you mean -e instead of -d? I hope you can check what you really mean, a -d does not exist.
Oops. Yes, I meant -e instead.
Great! Thanks. I didn't know about "set -e", so I am happy you mentioned it. I will add this feature to our set.
--
Marco
Quoted 9 lines · Marco Gerards
It's good you mention this, because it's exactly the discussion I want to start. The question here is: How do we want to deal with the `for'?
In bash it iterates over all arguments. The wildcards are expanded by the shell and thus it just has a look at its arguments. The question here is, do we want to deal with wildcards? It makes the code more complex and I think there is little gain.
Quoted 4 lines
For GRUB I think some kind of iterators are more useful. In that case you can write a module to iterate over certain data. For example over files, disks, partitions, terminals or whatever. It leaves the bash syntax, but it is more useful in our case and modular.
If you make this feature only for "for", the behavior would be not intuitive for the user. For instance, if "echo" does not interpret wildcards, we would hear the same question, "Why doesn't this work?", again and again. Also, It sounds really ugly to interpret wildcards in each command.
Okuji
Quoted 15 lines · Yoshinori K. Okuji
Quoted 9 lines · Marco Gerards
It's good you mention this, because it's exactly the discussion I want to start. The question here is: How do we want to deal with the `for'?
In bash it iterates over all arguments. The wildcards are expanded by the shell and thus it just has a look at its arguments. The question here is, do we want to deal with wildcards? It makes the code more complex and I think there is little gain.
Quoted 4 lines
For GRUB I think some kind of iterators are more useful. In that case you can write a module to iterate over certain data. For example over files, disks, partitions, terminals or whatever. It leaves the bash syntax, but it is more useful in our case and modular.
Quoted 4 lines
If you make this feature only for "for", the behavior would be not intuitive for the user. For instance, if "echo" does not interpret wildcards, we would hear the same question, "Why doesn't this work?", again and again. Also, It sounds really ugly to interpret wildcards in each command.
Ok, that sounds sane. But it does not address my problem. How would one iterate over disks and partitions. And perhaps other things like loaded modules, etc. Personally I do not care a lot about syntax, but I want the language to be full features so most end users can do what they want.
--
Marco
Quoted 5 lines · Marco Gerards
Ok, that sounds sane. But it does not address my problem. How would one iterate over disks and partitions. And perhaps other things like loaded modules, etc. Personally I do not care a lot about syntax, but I want the language to be full features so most end users can do what they want.
A naive way is "(*)" and "(*,*)". But I guess you had this in mind.
Okuji