grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] script: Allow user-defined functions to override builtins


From: Glenn Washburn
Subject: [PATCH 1/2] script: Allow user-defined functions to override builtins
Date: Sun, 21 Mar 2021 23:52:21 -0500

When sourcing a grub script file that you do not control, it can be useful
to be able to hook in to certain built in commands to modify behavior of
those built in commands.

This is a preparatory patch in that it does not provide access to overridden
built in commands. So it is of limited value by itself.

Signed-off-by: Glenn Washburn <development@efficientek.com>
---
 Makefile.util.def             |  6 ++++++
 grub-core/script/execute.c    | 22 ++++++++++++++--------
 tests/grub_script_override.in | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 8 deletions(-)
 create mode 100644 tests/grub_script_override.in

diff --git a/Makefile.util.def b/Makefile.util.def
index f8b356cc1..756158ffd 100644
--- a/Makefile.util.def
+++ b/Makefile.util.def
@@ -1031,6 +1031,12 @@ script = {
   common = tests/grub_script_setparams.in;
 };
 
+script = {
+  testcase;
+  name = grub_script_override;
+  common = tests/grub_script_override.in;
+};
+
 script = {
   testcase;
   name = grub_script_return;
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
index 25158407d..6ce8850ff 100644
--- a/grub-core/script/execute.c
+++ b/grub-core/script/execute.c
@@ -984,14 +984,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);
@@ -1007,6 +1008,11 @@ grub_script_execute_cmdline (struct grub_script_cmd *cmd)
              eq++;
              grub_script_env_set (assign, eq);
            }
+         else
+           {
+             /* Not an assignment, notify the user that the command was not 
found */
+             grub_error(GRUB_ERR_UNKNOWN_COMMAND, N_("can't find command 
`%s'"), cmdname);
+           }
          grub_free (assign);
 
          grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
@@ -1020,7 +1026,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))
@@ -1033,8 +1041,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)
     {
diff --git a/tests/grub_script_override.in b/tests/grub_script_override.in
new file mode 100644
index 000000000..4f9428753
--- /dev/null
+++ b/tests/grub_script_override.in
@@ -0,0 +1,33 @@
+#! @builddir@/grub-shell-tester
+
+# Run GRUB script in a Qemu instance
+# Copyright (C) 2010  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 3 of the License, or
+# (at your option) any later version.
+#
+# GRUB 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, see <http://www.gnu.org/licenses/>.
+
+echo override builtin function
+function help {
+  echo "This is not the help you're looking for... $@"
+}
+
+help
+help module
+help -module
+help module1 module2
+
+function insmod {
+  echo "Ignoring all module insertions: $@"
+}
+
+insmod module1 module2
-- 
2.27.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]