Skip to contentSkip to conversation

Allow user-defined functions to override builtins. [take 2]

PATCH In review Glenn Washburn 2 participants 4 messages May 22, 2014 – May 27, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Allow user-defined functions to override builtins. [take 2]

Thanks for suggestions from Michel and Ben, I've added a pseudo-command called "builtin" which will allow access to builtins that have been overridden by functions.

Any help in getting this functionality committed would be much appreciated.

---
 grub-core/script/execute.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
grub-core/script/execute.c +9 -8
index afd5513..857f8c6 100644
@@ -941,14 +941,15 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
       args = argv.args + 2;
       cmdname = argv.args[1];
     }
-  grubcmd = grub_command_find (cmdname);
-  if (! grubcmd)
+  /* Allow user functions to override built in commands. */
+  func = grub_script_function_find (cmdname);
+  if (! func)
     {
       grub_errno = GRUB_ERR_NONE;
 
-      /* It's not a GRUB command, try all functions.  */
-      func = grub_script_function_find (cmdname);
-      if (! func)
+      /* It's not a function, check if GRUB command.  */
+      grubcmd = grub_command_find (cmdname);
+      if (! grubcmd)
 	{
 	  /* As a last resort, try if it is an assignment.  */
 	  char *assign = grub_strdup (cmdname);
@@ -977,7 +978,9 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
     }
 
   /* Execute the GRUB command or function.  */
-  if (grubcmd)
+  if (func)
+    ret = grub_script_function_call (func, argc, args);
+  else
     {
       if (grub_extractor_level && !(grubcmd->flags
 				    & GRUB_COMMAND_FLAG_EXTRACTOR))
@@ -990,8 +993,6 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
       else
 	ret = (grubcmd->func) (grubcmd, argc, args);
     }
-  else
-    ret = grub_script_function_call (func, argc, args);
 
   if (invert)
     {
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 2/2 Allow user-defined functions to override builtins. [take 2]

This is implements the builtin pseudo-command.

---
 grub-core/script/execute.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
grub-core/script/execute.c +0 -0
index 857f8c6..2dbc17f 100644
@@ -917,6 +917,7 @@ grub_script_execute_cmdline (struct grub_script_cmd

*cmd) int argc;
char **args;
int invert;
+ int builtin;
struct grub_script_argv argv = { 0, 0, 0 };

/* Lookup the command. */
@@ -924,6 +925,7 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) return grub_errno;

invert = 0;
+ builtin = 0;
argc = argv.argc - 1;
args = argv.args + 1;
cmdname = argv.args[0];
@@ -937,12 +939,27 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd) }

       invert = 1;
-      argc = argv.argc - 2;
-      args = argv.args + 2;
-      cmdname = argv.args[1];
+      cmdname = args[0];
+      argc--;
+      args++;

+ }
+ if (grub_strcmp (cmdname, "builtin") == 0)
+ {
+ if (argv.argc < 2 || ! argv.args[1])
+ {
+ grub_script_argv_free (&argv);
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
+ N_("no command is specified"));
+ }
+

+      builtin = 1;
+      cmdname = args[0];
+      argc--;
+      args++;
     }

/* Allow user functions to override built in commands. */
- func = grub_script_function_find (cmdname);
+ if (! builtin)
+ func = grub_script_function_find (cmdname);
if (! func)

     {
       grub_errno = GRUB_ERR_NONE;
Signature · 1 line
1.8.3.2
#2 lore raw

I don't know the code much, but i think your change looks like a hack. I fell missing an internal command registration in main.c, a small function to handle the command, and an extra parameter in grub_script_execute_cmdline to allow/exclude user defined commands so that it can be called from builtin, and a change in the documentation to advertise about builtin command.
Cheers
Le 22 mai 2014 08:46, "Glenn Washburn" <[email protected]> a écrit :

Quoted 67 lines

This is implements the builtin pseudo-command.

---
 grub-core/script/execute.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
grub-core/script/execute.c +21 -5
index 857f8c6..2dbc17f 100644
@@ -917,6 +917,7 @@ grub_script_execute_cmdline (struct grub_script_cmd
 *cmd) int argc;
   char **args;
   int invert;
+  int builtin;
   struct grub_script_argv argv = { 0, 0, 0 };
 
   /* Lookup the command.  */
@@ -924,6 +925,7 @@ grub_script_execute_cmdline (struct grub_script_cmd
 *cmd) return grub_errno;
 
   invert = 0;
+  builtin = 0;
   argc = argv.argc - 1;
   args = argv.args + 1;
   cmdname = argv.args[0];
@@ -937,12 +939,27 @@ grub_script_execute_cmdline (struct
 grub_script_cmd *cmd) }
 
       invert = 1;
-      argc = argv.argc - 2;
-      args = argv.args + 2;
-      cmdname = argv.args[1];
+      cmdname = args[0];
+      argc--;
+      args++;
+    }
+  if (grub_strcmp (cmdname, "builtin") == 0)
+    {
+      if (argv.argc < 2 || ! argv.args[1])
+       {
+         grub_script_argv_free (&argv);
+         return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                            N_("no command is specified"));
+       }
+
+      builtin = 1;
+      cmdname = args[0];
+      argc--;
+      args++;
     }
   /* Allow user functions to override built in commands. */
-  func = grub_script_function_find (cmdname);
+  if (! builtin)
+    func = grub_script_function_find (cmdname);
   if (! func)
     {
       grub_errno = GRUB_ERR_NONE;
--

1.8.3.2

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

#3 lore raw

I agree that a documentation change would make this patch better. However, I don't see it as a hack. Essentially I've made "builtin" a contruct of the language, rather than a true command. I don't see the need to make it a true command. I see two reasons why you might want to do that.

1. You want to hook/override the builtin command. Why should this be allowed when it should just be running the builtin and nothing else?

2. You want to make builtin a module that you can choose not to load, so as not to have the builtin functionality. What's the point?

Quoted 77 lines · On Thu, 22 May 2014 15:59:58 +0200 Michel Hermier <[email protected]>…

I don't know the code much, but i think your change looks like a hack. I fell missing an internal command registration in main.c, a small function to handle the command, and an extra parameter in grub_script_execute_cmdline to allow/exclude user defined commands so that it can be called from builtin, and a change in the documentation to advertise about builtin command.
Cheers
Le 22 mai 2014 08:46, "Glenn Washburn" <[email protected]> a écrit :

Quoted 67 lines

This is implements the builtin pseudo-command.

---
 grub-core/script/execute.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
grub-core/script/execute.c +21 -5
index 857f8c6..2dbc17f 100644
@@ -917,6 +917,7 @@ grub_script_execute_cmdline (struct
 grub_script_cmd *cmd) int argc;
   char **args;
   int invert;
+  int builtin;
   struct grub_script_argv argv = { 0, 0, 0 };
 
   /* Lookup the command.  */
@@ -924,6 +925,7 @@ grub_script_execute_cmdline (struct
 grub_script_cmd *cmd) return grub_errno;
 
   invert = 0;
+  builtin = 0;
   argc = argv.argc - 1;
   args = argv.args + 1;
   cmdname = argv.args[0];
@@ -937,12 +939,27 @@ grub_script_execute_cmdline (struct
 grub_script_cmd *cmd) }
 
       invert = 1;
-      argc = argv.argc - 2;
-      args = argv.args + 2;
-      cmdname = argv.args[1];
+      cmdname = args[0];
+      argc--;
+      args++;
+    }
+  if (grub_strcmp (cmdname, "builtin") == 0)
+    {
+      if (argv.argc < 2 || ! argv.args[1])
+       {
+         grub_script_argv_free (&argv);
+         return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                            N_("no command is specified"));
+       }
+
+      builtin = 1;
+      cmdname = args[0];
+      argc--;
+      args++;
     }
   /* Allow user functions to override built in commands. */
-  func = grub_script_function_find (cmdname);
+  if (! builtin)
+    func = grub_script_function_find (cmdname);
   if (! func)
     {
       grub_errno = GRUB_ERR_NONE;
--

1.8.3.2

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