test -e patch
Attached you will find the patch adding test -e support for grub2.
This is my first patch. I have compiled it without no errors.
However as long as the grub2.tar.gz that Marco gave me did not have any documentation about how to create a floppy (or at least I did not manage to find it) I have not tested it.
Could you please test it?
I have applied the GNU Coding Standards Style on the patch although the copy-pasted code that you will find in the mail body has not the GCS applied.
Here there are some doubts:
Quoted 5 lines
static const struct
grub_arg_optionoptions[] ={ {"file", 'e', 0, "test if a file exists", 0, 0}, {0, 0, 0, 0, 0, 0}};
Is this correct? What's that last line for? Is it compulsory ?
Should I write "Test if a file exists" instead of "test if a file exists" or "FILE exists"?
Quoted 3 lines
static void
test_file_exists(const char *key)
Do you prefer another name like: file_exists_test ?
Quoted 7 lines
{
grub_file_t file;
file = grub_file_open (key);
if (file)
{
grub_printf (" %s", key);Should I prompt the existent file or not?
Should I prompt %s exists maybe ?
Should I prompt an \n also?
Quoted 6 lines
grub_file_close (file);
grub_errno = GRUB_ERR_NONE;
}
else
{
grub_error (GRUB_ERR_FILE_NOT_FOUND, "File does not exists.");How the hell should I treat grub errors. Maybe the test_file_exists has to return a static grub_err_t and then from grub_cmd_test I should call it like this: return (test_file_exists (args[0])) so that the error propagates ?
What are the error polices ?
Quoted 8 lines
} }static
grub_err_tgrub_cmd_test(structgrub_arg_list*state__attribute__((unused)), int argc,
char **args)
Quoted 6 lines
{
char *eq;
char *eqis;if (state[0].set)
I suppose this refers to the first line of static const struct grub_arg_option options and thus as -e is the first option and if its set we should run the correspondent function.
One question. Should we put the test-e function into a separate file or not ?
Quoted 32 lines
test_file_exists (args[0]); else { /* 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'. */ if (grub_strcmp (eq, eqis)) grub_error (GRUB_ERR_TEST_FAILURE, "false"); grub_free (eq);}
return
grub_errno;}
GRUB_MOD_INIT(test)
{
(void)mod; /* To stop warning. */
grub_register_command("[",grub_cmd_test,GRUB_COMMAND_FLAG_CMDLINE,
Quoted 4 lines
"[ EXPRESSION ]", "Evaluate an expression", 0);
grub_register_command ("test", grub_cmd_test, GRUB_COMMAND_FLAG_CMDLINE,
"test EXPRESSION", "Evaluate an expression", 0);
}I understand this register commands. I suppose this information is read from the help command or it isn't ?
Or maybe it also reads from:
static const struct grub_arg_option options ?
The question is if the user will see the -e, -f or other options when querying the test command help or not ?
Quoted 6 lines
GRUB_MOD_FINI(test)
{
grub_unregister_command("[");
grub_unregister_command("test");
}
That's all for my first compiled patch.
I hope that Marco_g is happy :).
But you know Marco_g I will continue my non-sense grub legacy development too. ;)
adrian15
commands/test.c +47 -15
@@ -24,32 +24,64 @@#include <grub/misc.h> #include <grub/mm.h> #include <grub/env.h> +#include <grub/file.h> + +static const struct grub_arg_option options[] = + { + {"file" ,'e' ,0 ,"Test if a file exists" ,0 ,0 }, + {0 ,0 ,0 ,0 ,0 ,0 } + }; + +static void +test_file_exists (const char *key) +{ +grub_file_t file; + + file =grub_file_open (key); +if (file) + { +grub_printf (" %s" , key); +grub_file_close (file); + grub_errno =GRUB_ERR_NONE ; + } +else + { +grub_error (GRUB_ERR_FILE_NOT_FOUND ,"File does not exists." ); + } +} +static grub_err_t grub_cmd_test (struct grub_arg_list *state __attribute__ ((unused)),int argc,char **args) { +char *eq;char *eqis; -/* XXX: No fancy expression evaluation yet. */ +if (state[0 ].set) +test_file_exists (args[0 ]); +else + { +/* XXX: No fancy expression evaluation yet. */ -if (argc ==0 ) -return 0 ; +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'. */ +if (grub_strcmp (eq, eqis)) +grub_error (GRUB_ERR_TEST_FAILURE ,"false" ); +grub_free (eq); + } - eq =grub_strdup (args[0 ]); - eqis =grub_strchr (eq,'=' ); -if (! eqis) -return 0 ; - - *eqis ='\0' ; - eqis++; -/* Check an expression in the form `A=B'. */ -if (grub_strcmp (eq, eqis)) -grub_error (GRUB_ERR_TEST_FAILURE ,"false" ); -grub_free (eq); -return grub_errno; }
Quoted 7 lines · On Wed, Jun 06, 2007 at 01:42:48AM +0200, adrian15 wrote:
Attached you will find the patch adding test -e support for grub2.
This is my first patch. I have compiled it without no errors.
However as long as the grub2.tar.gz that Marco gave me did not have any documentation about how to create a floppy (or at least I did not manage to find it)
This should work:
cat boot.img core.img | dd of=foo.img seek=0 conv=notrunc
Quoted 7 lines
Quoted 5 lines
static const struct
grub_arg_optionoptions[] =
{
{"file", 'e', 0, "test if a file exists", 0, 0}, {0, 0, 0, 0, 0, 0}
};Is this correct? What's that last line for? Is it compulsory ?
It's common practice for arrays to make them null-terminated to be able to determine their limits without making assumptions about their size. Doing it with structs is akin to null-terminating a char array.
Quoted 4 lines
Quoted 4 lines
{ grub_file_t file; file = grub_file_open (key);
What happens if file is a device node, or a directory? Does grub_file_open work with these? Perhaps grub_file_stat would make more sense (after all, if you want to implement -f, -H, -d, etc we'll need to stat it).
Quoted 6 lines
How the hell should I treat grub errors. Maybe the
test_file_existshas to return a staticgrub_err_tand then fromgrub_cmd_testI should call it like this: return (test_file_exists(args[0])) so that the error propagates ?What are the error polices ?
I'm not sure what the policy is, but this seems to not be in sync with the cpuid module I wrote a while ago.
IMHO, an error would be if existance of the file cannot be determined. I don't think non-existance should be considered an error too. However, existing code in the test plugin already does this, which puzzles me.
Can someone compare them and tell which is the right one?
Quoted 11 lines
Quoted 8 lines
GRUB_MOD_INIT(test)
{
(void)mod; /* To stop warning. */
grub_register_command("[",grub_cmd_test,GRUB_COMMAND_FLAG_CMDLINE,"[ EXPRESSION ]", "Evaluate an expression", 0); grub_register_command ("test", grub_cmd_test, GRUB_COMMAND_FLAG_CMDLINE, "test EXPRESSION", "Evaluate an expression", 0); }I understand this register commands. I suppose this information is read from the help command or it isn't ?
Yes. Also with "test --help".
Quoted 2 lines
Or maybe it also reads from:
static const structgrub_arg_optionoptions ?
That too.
Quoted 2 lines
The question is if the user will see the -e, -f or other options when querying the test command help or not ?
Yes, based on options[]. But you should really test that and not blindly believe me :-)
commands/test.c line 31
Quoted 12 lines
diff -urN
grub2_2007_05_31_original/commands/test.cgrub2_2007_05_31/commands/test.ccommands/test.c +5
-0@@ -24,32 +24,64 @@#include <grub/misc.h> #include <grub/mm.h> #include <grub/env.h> +#include <grub/file.h> + +static const struct grub_arg_option options[] = + { + {"file" ,'e' ,0 ,"Test if a file exists" ,0 ,0 },
Do we want a long option here? bash/coreutils don't have any.
Signature · 4 lines
Robert Millan My spam trap is [email protected]. Note: this address is only intended for spam harvesters. Writing to it will get you added to my black list.
Quoted 4 lines · On Wed, Jun 06, 2007 at 01:42:48AM +0200, adrian15 wrote:
I have applied the GNU Coding Standards Style on the patch although the copy-pasted code that you will find in the mail body has not the GCS applied.
Btw, you can also test it with grub-emu.
Signature · 4 lines
Robert Millan My spam trap is [email protected]. Note: this address is only intended for spam harvesters. Writing to it will get you added to my black list.
Quoted 3 lines · adrian15 <[email protected]> writes:
Attached you will find the patch adding test -e support for grub2.
This is my first patch. I have compiled it without no errors.
Urgh... I thought/hoped I told you I had a test.c rewrite sitting on my harddisk? Or did I tell Robert to poke me until next weekend so I will work on it? It includes everything you expect from test.c, expect the cleanup and testing. ;-)
Quoted 3 lines
However as long as the grub2.tar.gz that Marco gave me did not have any documentation about how to create a floppy (or at least I did not manage to find it) I have not tested it.
Please have a look at the wiki. It has quite some information about GRUB 2.
Quoted 6 lines
I have applied the GNU Coding Standards Style on the patch although the copy-pasted code that you will find in the mail body has not the GCS applied.
Here there are some doubts:
Quoted 7 lines
Quoted 5 lines
static const struct
grub_arg_optionoptions[] ={ {"file", 'e', 0, "test if a file exists", 0, 0}, {0, 0, 0, 0, 0, 0}};
Is this correct? What's that last line for? Is it compulsory ?
It means that this is the last option. There is no other way to tell if this is the last option without this.
Quoted 2 lines
Should I write "Test if a file exists" instead of "test if a file exists" or "FILE exists"?
FILE
Quoted 23 lines
Quoted 3 lines
static void
test_file_exists(const char *key)Do you prefer another name like:
file_exists_test?Quoted 7 lines
{ grub_file_t file; file = grub_file_open (key); if (file) { grub_printf (" %s", key);Should I prompt the existent file or not?
Should I prompt %s exists maybe ?
Should I prompt an \n also?Quoted 6 lines
grub_file_close (file); grub_errno = GRUB_ERR_NONE; } else { grub_error (GRUB_ERR_FILE_NOT_FOUND, "File does not exists.");
Quoted 4 lines
How the hell should I treat grub errors. Maybe the
test_file_existshas to return a staticgrub_err_tand then fromgrub_cmd_testI should call it like this: return (test_file_exists(args[0])) so that the error propagates ?
Right. Otherwise the error will be lost.
Quoted 1 line
What are the error polices ?
Returning the err_t that grub_error returns when possible.
Quoted 10 lines
Quoted 10 lines
} }static
grub_err_tgrub_cmd_test(structgrub_arg_list*state__attribute__((unused)), int argc,
char **args){
Quoted 8 lines
Quoted 4 lines
char *eq;
char *eqis;if (state[0].set)
I suppose this refers to the first line of static const struct
grub_arg_optionoptions and thus as -e is the first option and if its set we should run the correspondent function.
Yes.
Quoted 2 lines
One question. Should we put the test-e function into a separate file or not ?
No, the problem is that the design of test.c (which is just a placeholder) is wrong. It needs a proper parser for the arguments and a way to deal with this...
Quoted 34 lines
Quoted 29 lines
test_file_exists (args[0]); else { /* 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'. */ if (grub_strcmp (eq, eqis)) grub_error (GRUB_ERR_TEST_FAILURE, "false"); grub_free (eq); } return grub_errno;}
GRUB_MOD_INIT(test)
{
(void)mod; /* To stop warning. */
grub_register_command("[",grub_cmd_test,GRUB_COMMAND_FLAG_CMDLINE,Quoted 4 lines
"[ EXPRESSION ]", "Evaluate an expression", 0); grub_register_command ("test", grub_cmd_test, GRUB_COMMAND_FLAG_CMDLINE, "test EXPRESSION", "Evaluate an expression", 0); }
Quoted 2 lines
I understand this register commands. I suppose this information is read from the help command or it isn't ?
Yes, the help command uses this information.
Quoted 2 lines
Or maybe it also reads from:
static const structgrub_arg_optionoptions ?
Help only does when you use "help test". So it can use both.
Quoted 2 lines
The question is if the user will see the -e, -f or other options when querying the test command help or not ?
They should. But I am not sure if the final version will support this. Especially because of the nested syntax of the test arguments.
Quoted 11 lines
Quoted 6 lines
GRUB_MOD_FINI(test)
{
grub_unregister_command("[");
grub_unregister_command("test");
}That's all for my first compiled patch.
I hope thatMarco_gis happy :).
But you knowMarco_gI will continue my non-sense grub legacy development too. ;)
:-))
Quoted 9 lines
adrian15
diff -urN
grub2_2007_05_31_original/commands/test.cgrub2_2007_05_31/commands/test.ccommands/test.c +0
-0@@ -24,32 +24,64 @@#include <grub/misc.h> #include <grub/mm.h> #include <grub/env.h>
commands/test.c line 36
Quoted 10 lines
commands/test.c +10
-0+#include <grub/file.h> + +static const struct grub_arg_option options[] = + { + {"file" ,'e' ,0 ,"Test if a file exists" ,0 ,0 }, + {0 ,0 ,0 ,0 ,0 ,0 } + }; + +static void +test_file_exists (const char *key)
Why not filename?
Quoted 13 lines
commands/test.c +13
-0+{ +grub_file_t file; + + file =grub_file_open (key); +if (file) + { +grub_printf (" %s" , key); +grub_file_close (file); + grub_errno =GRUB_ERR_NONE ; + } +else + { +grub_error (GRUB_ERR_FILE_NOT_FOUND ,"File does not exists." );
commands/test.c line 59
Quoted 10 lines
commands/test.c +4
-0+ } +} +static grub_err_t grub_cmd_test (struct grub_arg_list *state __attribute__ ((unused)),int argc,char **args) { +
You accidently introduced a whiteline.
commands/test.c line 66
Quoted 8 lines
commands/test.c +4
-1char *eq;char *eqis; -/* XXX: No fancy expression evaluation yet. */ +if (state[0 ].set) +test_file_exists (args[0 ]); +else + {
This means that this check is run for any other expression. This is quite error sensitive.
Quoted 24 lines
commands/test.c +16
-6+/* XXX: No fancy expression evaluation yet. */ -if (argc ==0 ) -return 0 ; +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'. */ +if (grub_strcmp (eq, eqis)) +grub_error (GRUB_ERR_TEST_FAILURE ,"false" ); +grub_free (eq); + } - eq =grub_strdup (args[0 ]); - eqis =grub_strchr (eq,'=' ); -if (! eqis) -return 0 ;
commands/test.c line 86
Quoted 10 lines
commands/test.c +0
-8- - *eqis ='\0' ; - eqis++; -/* Check an expression in the form `A=B'. */ -if (grub_strcmp (eq, eqis)) -grub_error (GRUB_ERR_TEST_FAILURE ,"false" ); -grub_free (eq); -return grub_errno; }
--
Marco