Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1lOCYb-0002em-4r for mharc-grub-devel@gnu.org; Mon, 22 Mar 2021 00:52:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54722) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lOCYZ-0002dc-JE for grub-devel@gnu.org; Mon, 22 Mar 2021 00:52:47 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]:38758) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lOCYW-0001cz-Lf for grub-devel@gnu.org; Mon, 22 Mar 2021 00:52:47 -0400 Received: by mail-pj1-x1032.google.com with SMTP id k23-20020a17090a5917b02901043e35ad4aso9747164pji.3 for ; Sun, 21 Mar 2021 21:52:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oOsgkSXSvjPXFpIYrt6atXARpSy82QzBQc9rjzQQLvQ=; b=LQqe/0rdY6vVpDcpLZJZDrU+NJf0QpqVFuXcGlenShgXxwjIoR7zDKMu0IPBtkeJDN Eh2g5jn7rWdmi3xobrR9f3wv3RboijzHR2yMwXlGtLPkpvUbDNvF9uLt3Q/mzBwlO5FC 6GyyWIzvJ0PcdIQ5eKd1OAh66M4b66ICPqOVqJshrwZpv8gp0DeTOkBapoi9h0AUZAC8 06I3waATSCeP5zMRb0sihiqoAcSSwaYmAkzi9n5a2xHXuecKK81qkBfq7BkNuBHrX858 CaVi6p5kgPW4vL20X/5rRmNfJASwya/ZUnNv6J6cjEV4xMOGt/p4W5NL5hB0XCizRkje GcnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oOsgkSXSvjPXFpIYrt6atXARpSy82QzBQc9rjzQQLvQ=; b=dUqXUbuEgkZGq32YvAie3uX1lJwgE6AEY8NLGGl5jPW6YVM9V08G+DZwYkvFFkKobN yfkPYa7URIwCBqYG/phX4tofVPL0nWN+LGopoE0GFQlFFjdlGEu6+HSJf8CTnjkaPx24 UZGieHYZ07N2zsPmP4v50/eL+WS4EjewgDMV0qHTVJQ1aM8T1Is//y3b6Szt7MywADlf I5UggRaamTEJvRCcOxRHANWrPtwnlx7av6fubvYSxJZ3b9z7ZWbed6K/8fBfY82VPTAh Y00qjdfhsEwTPXgMVXEgfb2cnA+JxUUPZf8zLpki+1P0t1NgxilOkBNE7s/ZqCyu7zCy CXNQ== X-Gm-Message-State: AOAM533DMwwD5hZrK8EpvKnqa/9RHfvv9v5eacpGHmXTRgTUymx/ENNJ 7a0iG5Fo/bX5kXbJnMmhY5gO371Cbq6rNg== X-Google-Smtp-Source: ABdhPJyluvBo5jj9B+5uiC7oWfDaBv6TlPXHGewQ4HHE/nsmvp158zpy+ADw7RNj+KoWyzfN9X3wGg== X-Received: by 2002:a17:903:208a:b029:e6:20c3:2300 with SMTP id d10-20020a170903208ab02900e620c32300mr24980490plc.47.1616388762889; Sun, 21 Mar 2021 21:52:42 -0700 (PDT) Received: from localhost.localdomain ([2605:a601:ab16:db00:59d7:d891:def4:a439]) by smtp.gmail.com with ESMTPSA id q16sm7038913pfs.16.2021.03.21.21.52.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Mar 2021 21:52:42 -0700 (PDT) From: Glenn Washburn To: grub-devel@gnu.org, Daniel Kiper Cc: Glenn Washburn Subject: [PATCH 1/2] script: Allow user-defined functions to override builtins Date: Sun, 21 Mar 2021 23:52:21 -0500 Message-Id: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=development@efficientek.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Mar 2021 04:52:47 -0000 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 --- 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 . + +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