]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
shift command support
authorBVK Chaitanya <bvk.groups@gmail.com>
Fri, 7 May 2010 04:38:09 +0000 (10:08 +0530)
committerBVK Chaitanya <bvk.groups@gmail.com>
Fri, 7 May 2010 04:38:09 +0000 (10:08 +0530)
conf/common.rmk
conf/tests.rmk
include/grub/script_sh.h
script/execute.c
script/main.c
tests/grub_script_shift.in [new file with mode: 0644]
util/grub-script-check.c

index 4b39e9b71a1247e49a0598a8f287c7bb979e9282..01f15dc59dec473d07e0c9e97b8fc252f41865fa 100644 (file)
@@ -106,7 +106,7 @@ util/grub-script-check.c_DEPENDENCIES = grub_script_check_init.h
 grub_script_check_SOURCES = gnulib/progname.c gnulib/getdelim.c gnulib/getline.c \
        util/grub-script-check.c util/misc.c util/mm.c \
        script/main.c script/script.c script/function.c script/lexer.c \
-       kern/handler.c kern/err.c kern/parser.c kern/list.c \
+       kern/handler.c kern/err.c kern/parser.c kern/list.c kern/command.c \
        kern/misc.c kern/env.c grub_script_check_init.c grub_script.tab.c \
        grub_script.yy.c
 grub_script_check_CFLAGS = $(GNULIB_UTIL_CFLAGS)
index 9144e552845b3a517dc7ff959b226bbfcc722e58..9cda007db49a2069d95600e3a11599a485d68bbf 100644 (file)
@@ -74,6 +74,9 @@ grub_script_comments_SOURCES = tests/grub_script_comments.in
 check_SCRIPTS += grub_script_functions
 grub_script_functions_SOURCES = tests/grub_script_functions.in
 
+check_SCRIPTS += grub_script_shift
+grub_script_shift_SOURCES = tests/grub_script_shift.in
+
 # List of tests to execute on "make check"
 # SCRIPTED_TESTS    = example_scripted_test
 # SCRIPTED_TESTS   += example_grub_script_test
@@ -91,6 +94,7 @@ SCRIPTED_TESTS += grub_script_final_semicolon
 SCRIPTED_TESTS += grub_script_dollar
 SCRIPTED_TESTS += grub_script_comments
 SCRIPTED_TESTS += grub_script_functions
+SCRIPTED_TESTS += grub_script_shift
 
 # dependencies between tests and testing-tools
 $(SCRIPTED_TESTS): grub-shell grub-shell-tester
index 730aa30050e85670d054d70cbcfa559e58f09109..8610348f6ba9bf678a967f0c878aca5bf5e13c3b 100644 (file)
@@ -23,6 +23,7 @@
 #include <grub/types.h>
 #include <grub/err.h>
 #include <grub/parser.h>
+#include <grub/command.h>
 
 struct grub_script_mem;
 
@@ -80,6 +81,7 @@ struct grub_script_scope
 
   char **args;
   unsigned int argc;
+  unsigned int shift;
 };
 
 /* A single command line.  */
@@ -308,6 +310,9 @@ grub_err_t grub_script_execute_menuentry (struct grub_script_cmd *cmd);
 /* Execute any GRUB pre-parsed command or script.  */
 grub_err_t grub_script_execute (struct grub_script *script);
 
+/* SHIFT command for GRUB script.  */
+grub_err_t grub_script_cmd_shift (grub_command_t cmd, int argc, char *argv[]);
+
 /* This variable points to the parsed command.  This is used to
    communicate with the bison code.  */
 extern struct grub_script_cmd *grub_script_parsed;
index 571b6785bfc13de1c49f4555463b515bb8f95eca..d3f8e9b258eacc1b14bc49eaf00e288148845043 100644 (file)
 
 static struct grub_script_scope *scope = 0;
 
+grub_err_t
+grub_script_cmd_shift (grub_command_t cmd __attribute__((unused)),
+                      int argc, char *argv[])
+{
+  char *p = 0;
+  unsigned long n = 0;
+
+  if (! scope)
+    return GRUB_ERR_NONE;
+
+  if (argc == 0)
+    n = 1;
+  else if (argc > 1)
+    return GRUB_ERR_BAD_ARGUMENT;
+  else
+    {
+      n = grub_strtoul (argv[0], &p, 10);
+      if (*p != '\0')
+       return GRUB_ERR_BAD_ARGUMENT;
+    }
+
+  scope->shift = (unsigned int)(n > scope->argc ? 0 : n);
+  return GRUB_ERR_NONE;
+}
+
 static char *
 grub_script_env_get (const char *name)
 {
@@ -49,7 +74,10 @@ grub_script_env_get (const char *name)
          if (num == 0)
            return 0; /* XXX no file name, for now.  */
 
-         return (num > scope->argc ? 0 : scope->args[num - 1]);
+         if (num > scope->argc - scope->shift)
+           return 0;
+         else
+           return scope->args[num - 1 + scope->shift];
        }
       else
        {
@@ -60,7 +88,7 @@ grub_script_env_get (const char *name)
   else if (grub_strcmp (name, "#") == 0)
     {
       static char buf[32]; /* Rewritten everytime.  */
-      grub_snprintf (buf, sizeof (buf), "%u", scope->argc);
+      grub_snprintf (buf, sizeof (buf), "%u", scope->argc - scope->shift);
       return buf;
     }
   else
@@ -244,6 +272,7 @@ grub_script_function_call (grub_script_function_t func, int argc, char **args)
   grub_err_t ret = 0;
   struct grub_script_scope new_scope;
 
+  new_scope.shift = 0;
   new_scope.argc = argc;
   new_scope.args = args;
   grub_list_push (GRUB_AS_LIST_P (&scope), GRUB_AS_LIST (&new_scope));
index b5159dc7ddcd71a653f389c947f9938465ad754e..ef810611c09e2401fdab092011ba0fdde5773f20 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <grub/dl.h>
+#include <grub/i18n.h>
 #include <grub/parser.h>
 #include <grub/script_sh.h>
 
@@ -49,6 +50,8 @@ static struct grub_parser grub_sh_parser =
 GRUB_MOD_INIT(sh)
 {
   grub_parser_register ("grub", &grub_sh_parser);
+  grub_register_command ("shift", grub_script_cmd_shift,
+                        N_("[n]"), N_("Shift positional parameters."));
 }
 
 GRUB_MOD_FINI(sh)
diff --git a/tests/grub_script_shift.in b/tests/grub_script_shift.in
new file mode 100644 (file)
index 0000000..542e729
--- /dev/null
@@ -0,0 +1,69 @@
+#! @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/>.
+
+function f1 {
+  echo f1 $# $1 $2 $3
+  shift
+  echo f1 $# $1 $2 $3
+}
+
+function f2 {
+  echo f2 $# $1 $2 $3
+  shift 1
+  echo f2 $# $1 $2 $3
+}
+
+function f3 {
+  echo f3 $# $1 $2 $3
+  shift 3
+  echo f3 $# $1 $2 $3
+}
+
+function f4 {
+  echo f4 $# $1 $2 $3
+  shift 100
+  echo f4 $# $1 $2 $3
+}
+
+f1
+f1 a
+f1 a b
+f1 a b c
+f1 a b c d
+f1 a b c d e
+
+f2
+f2 a
+f2 a b
+f2 a b c
+f2 a b c d
+f2 a b c d e
+
+f3
+f3 a
+f3 a b
+f3 a b c
+f3 a b c d
+f3 a b c d e
+
+f4
+f4 a
+f4 a b
+f4 a b c
+f4 a b c d
+f4 a b c d e
index 3b7ab295d9e9feb8f9642fc7e71c8091f9929cc0..1d9ebd57fd5bcd3b80eff56537610b7ca3e1421a 100644 (file)
@@ -57,6 +57,14 @@ grub_refresh (void)
   fflush (stdout);
 }
 
+grub_err_t
+grub_script_cmd_shift (grub_command_t cmd __attribute__((unused)),
+                      int argc __attribute__((unused)),
+                      char *argv[] __attribute__((unused)))
+{
+  return 0;
+}
+
 char *
 grub_script_execute_argument_to_string (struct grub_script_arg *arg __attribute__ ((unused)))
 {