All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] grub-core/commands/basic_math.c: New commands to perform basic math operations on variables
@ 2019-07-16 16:44 glenn tanner
  2019-07-16 19:47 ` Vladimir 'phcoder' Serbinenko
  2019-07-16 19:48 ` Vladimir 'phcoder' Serbinenko
  0 siblings, 2 replies; 3+ messages in thread
From: glenn tanner @ 2019-07-16 16:44 UTC (permalink / raw)
  To: grub-devel

[-- Attachment #1: Type: text/plain, Size: 4648 bytes --]

Added new commands to add and subtract values from grub variables, was
desirable for automated testing. Compiled and tested under x86_64-efi.

diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 474a63e68..c383f0157 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -2503,3 +2503,8 @@ module = {
  common = commands/i386/wrmsr.c;
  enable = x86;
};
+
+module = {
+  name = basic_math;
+  common = commands/basic_math.c;
+};
diff --git a/grub-core/commands/basic_math.c
b/grub-core/commands/basic_math.c
new file mode 100644
index 000000000..9337cd5b1
--- /dev/null
+++ b/grub-core/commands/basic_math.c
@@ -0,0 +1,134 @@
+/* basic_math.c - basic math functions.  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2008,2009,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/>.
+ */
+
+/* what any module needs */
+#include <grub/dl.h>
+#include <grub/misc.h>
+#include <grub/extcmd.h>
+#include <grub/i18n.h>
+#include <grub/mm.h>
+/* what my module needs */
+#include <grub/env.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_err_t
+grub_cmd_add (grub_extcmd_context_t ctxt __attribute__ ((unused)), int
argc, char **args)
+{
+  unsigned int i;
+  int ret, value_int=0, arg_int=0, digit;
+  const char *value;
+  char buf[512];
+
+  if (argc != 2)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable and value
required"));
+
+  value = grub_env_get (args[0]);
+  if (!value)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable must be
defined"));
+
+  for (i=0;i<grub_strlen(value);i++) {
+    if (grub_isdigit((int)value[i])) {
+      digit = value[i] - '0';
+      value_int = (10*value_int) + digit;
+    }
+  }
+
+  for (i=0;i<grub_strlen(args[1]);i++) {
+    if (grub_isdigit((int)args[1][i])) {
+      digit = args[1][i] - '0';
+      arg_int = (10*arg_int) + digit;
+    } else {
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Value must be a
positive interger"));
+    }
+  }
+
+  ret=value_int+arg_int;
+  /* grub_printf ("%d\n", ret); */
+
+  grub_snprintf (buf, sizeof (buf), "%d", ret);
+  /* grub_printf ("%s\n", buf); */
+  grub_env_set (args[0], buf);
+  return 0;
+}
+
+static grub_err_t
+grub_cmd_sub (grub_extcmd_context_t ctxt __attribute__ ((unused)), int
argc, char **args)
+{
+  unsigned int i;
+  int ret, value_int=0, arg_int=0, digit;
+  const char *value;
+  char buf[512];
+
+  if (argc != 2)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable and value
required"));
+
+  value = grub_env_get (args[0]);
+  if (!value)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable must be
defined"));
+
+  for (i=0;i<grub_strlen(value);i++) {
+    if (grub_isdigit((int)value[i])) {
+      digit = value[i] - '0';
+      value_int = (10*value_int) + digit;
+    }
+  }
+
+  for (i=0;i<grub_strlen(args[1]);i++) {
+    if (grub_isdigit((int)args[1][i])) {
+      digit = args[1][i] - '0';
+      arg_int = (10*arg_int) + digit;
+    } else {
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Value must be a
positive interger"));
+    }
+  }
+
+  if (arg_int > value_int)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Result may not be
negitive"));
+
+  ret=value_int-arg_int;
+  /* grub_printf ("%d\n", ret); */
+
+  grub_snprintf (buf, sizeof (buf), "%d", ret);
+  /* grub_printf ("%s\n", buf); */
+  grub_env_set (args[0], buf);
+  return 0;
+}
+
+static grub_extcmd_t cmd_add, cmd_sub;
+

+GRUB_MOD_INIT(basic_math)
+{
+  cmd_add =
+    grub_register_extcmd ("add", grub_cmd_add, 0,
+                         N_("<variable_name> <value>"),
+                         N_("Add value to variable."),
+                         0);
+  cmd_sub =
+    grub_register_extcmd ("sub", grub_cmd_sub, 0,
+                         N_("<variable_name> <value>"),
+                         N_("Subtract value from variable."),
+                         0);
+}
+
+GRUB_MOD_FINI(basic_math)
+{
+  grub_unregister_extcmd (cmd_add);
+  grub_unregister_extcmd (cmd_sub);
+}

--
Regards,
Glenn Tanner

[-- Attachment #2: Type: text/html, Size: 6514 bytes --]

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] grub-core/commands/basic_math.c: New commands to perform basic math operations on variables
  2019-07-16 16:44 [PATCH] grub-core/commands/basic_math.c: New commands to perform basic math operations on variables glenn tanner
@ 2019-07-16 19:47 ` Vladimir 'phcoder' Serbinenko
  2019-07-16 19:48 ` Vladimir 'phcoder' Serbinenko
  1 sibling, 0 replies; 3+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2019-07-16 19:47 UTC (permalink / raw)
  To: The development of GRUB 2

[-- Attachment #1: Type: text/plain, Size: 5292 bytes --]

We should rather have $[...] syntax in parser rather than ad-hoc math
commands

On Tue, 16 Jul 2019, 19:15 glenn tanner, <glenntanner3@gmail.com> wrote:

> Added new commands to add and subtract values from grub variables, was
> desirable for automated testing. Compiled and tested under x86_64-efi.
>
> diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
> index 474a63e68..c383f0157 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -2503,3 +2503,8 @@ module = {
>   common = commands/i386/wrmsr.c;
>   enable = x86;
> };
> +
> +module = {
> +  name = basic_math;
> +  common = commands/basic_math.c;
> +};
> diff --git a/grub-core/commands/basic_math.c
> b/grub-core/commands/basic_math.c
> new file mode 100644
> index 000000000..9337cd5b1
> --- /dev/null
> +++ b/grub-core/commands/basic_math.c
> @@ -0,0 +1,134 @@
> +/* basic_math.c - basic math functions.  */
> +/*
> + *  GRUB  --  GRand Unified Bootloader
> + *  Copyright (C) 2008,2009,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/>.
> + */
> +
> +/* what any module needs */
> +#include <grub/dl.h>
> +#include <grub/misc.h>
> +#include <grub/extcmd.h>
> +#include <grub/i18n.h>
> +#include <grub/mm.h>
> +/* what my module needs */
> +#include <grub/env.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +static grub_err_t
> +grub_cmd_add (grub_extcmd_context_t ctxt __attribute__ ((unused)), int
> argc, char **args)
> +{
> +  unsigned int i;
> +  int ret, value_int=0, arg_int=0, digit;
> +  const char *value;
> +  char buf[512];
> +
> +  if (argc != 2)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable and value
> required"));
> +
> +  value = grub_env_get (args[0]);
> +  if (!value)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable must be
> defined"));
> +
> +  for (i=0;i<grub_strlen(value);i++) {
> +    if (grub_isdigit((int)value[i])) {
> +      digit = value[i] - '0';
> +      value_int = (10*value_int) + digit;
> +    }
> +  }
> +
> +  for (i=0;i<grub_strlen(args[1]);i++) {
> +    if (grub_isdigit((int)args[1][i])) {
> +      digit = args[1][i] - '0';
> +      arg_int = (10*arg_int) + digit;
> +    } else {
> +      return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Value must be a
> positive interger"));
> +    }
> +  }
> +
> +  ret=value_int+arg_int;
> +  /* grub_printf ("%d\n", ret); */
> +
> +  grub_snprintf (buf, sizeof (buf), "%d", ret);
> +  /* grub_printf ("%s\n", buf); */
> +  grub_env_set (args[0], buf);
> +  return 0;
> +}
> +
> +static grub_err_t
> +grub_cmd_sub (grub_extcmd_context_t ctxt __attribute__ ((unused)), int
> argc, char **args)
> +{
> +  unsigned int i;
> +  int ret, value_int=0, arg_int=0, digit;
> +  const char *value;
> +  char buf[512];
> +
> +  if (argc != 2)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable and value
> required"));
> +
> +  value = grub_env_get (args[0]);
> +  if (!value)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable must be
> defined"));
> +
> +  for (i=0;i<grub_strlen(value);i++) {
> +    if (grub_isdigit((int)value[i])) {
> +      digit = value[i] - '0';
> +      value_int = (10*value_int) + digit;
> +    }
> +  }
> +
> +  for (i=0;i<grub_strlen(args[1]);i++) {
> +    if (grub_isdigit((int)args[1][i])) {
> +      digit = args[1][i] - '0';
> +      arg_int = (10*arg_int) + digit;
> +    } else {
> +      return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Value must be a
> positive interger"));
> +    }
> +  }
> +
> +  if (arg_int > value_int)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Result may not be
> negitive"));
> +
> +  ret=value_int-arg_int;
> +  /* grub_printf ("%d\n", ret); */
> +
> +  grub_snprintf (buf, sizeof (buf), "%d", ret);
> +  /* grub_printf ("%s\n", buf); */
> +  grub_env_set (args[0], buf);
> +  return 0;
> +}
> +
> +static grub_extcmd_t cmd_add, cmd_sub;
> +
>
> +GRUB_MOD_INIT(basic_math)
> +{
> +  cmd_add =
> +    grub_register_extcmd ("add", grub_cmd_add, 0,
> +                         N_("<variable_name> <value>"),
> +                         N_("Add value to variable."),
> +                         0);
> +  cmd_sub =
> +    grub_register_extcmd ("sub", grub_cmd_sub, 0,
> +                         N_("<variable_name> <value>"),
> +                         N_("Subtract value from variable."),
> +                         0);
> +}
> +
> +GRUB_MOD_FINI(basic_math)
> +{
> +  grub_unregister_extcmd (cmd_add);
> +  grub_unregister_extcmd (cmd_sub);
> +}
>
> --
> Regards,
> Glenn Tanner
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 7328 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] grub-core/commands/basic_math.c: New commands to perform basic math operations on variables
  2019-07-16 16:44 [PATCH] grub-core/commands/basic_math.c: New commands to perform basic math operations on variables glenn tanner
  2019-07-16 19:47 ` Vladimir 'phcoder' Serbinenko
@ 2019-07-16 19:48 ` Vladimir 'phcoder' Serbinenko
  1 sibling, 0 replies; 3+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2019-07-16 19:48 UTC (permalink / raw)
  To: The development of GRUB 2

[-- Attachment #1: Type: text/plain, Size: 5262 bytes --]

On Tue, 16 Jul 2019, 19:15 glenn tanner, <glenntanner3@gmail.com> wrote:

> Added new commands to add and subtract values from grub variables, was
> desirable for automated testing. Compiled and tested under x86_64-efi.
>
> diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
> index 474a63e68..c383f0157 100644
> --- a/grub-core/Makefile.core.def
> +++ b/grub-core/Makefile.core.def
> @@ -2503,3 +2503,8 @@ module = {
>   common = commands/i386/wrmsr.c;
>   enable = x86;
> };
> +
> +module = {
> +  name = basic_math;
> +  common = commands/basic_math.c;
> +};
> diff --git a/grub-core/commands/basic_math.c
> b/grub-core/commands/basic_math.c
> new file mode 100644
> index 000000000..9337cd5b1
> --- /dev/null
> +++ b/grub-core/commands/basic_math.c
> @@ -0,0 +1,134 @@
> +/* basic_math.c - basic math functions.  */
> +/*
> + *  GRUB  --  GRand Unified Bootloader
> + *  Copyright (C) 2008,2009,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/>.
> + */
> +
> +/* what any module needs */
> +#include <grub/dl.h>
> +#include <grub/misc.h>
> +#include <grub/extcmd.h>
> +#include <grub/i18n.h>
> +#include <grub/mm.h>
> +/* what my module needs */
> +#include <grub/env.h>
> +
> +GRUB_MOD_LICENSE ("GPLv3+");
> +
> +static grub_err_t
> +grub_cmd_add (grub_extcmd_context_t ctxt __attribute__ ((unused)), int
> argc, char **args)
> +{
> +  unsigned int i;
> +  int ret, value_int=0, arg_int=0, digit;
> +  const char *value;
> +  char buf[512];
> +
> +  if (argc != 2)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable and value
> required"));
> +
> +  value = grub_env_get (args[0]);
> +  if (!value)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable must be
> defined"));
> +
> +  for (i=0;i<grub_strlen(value);i++) {
> +    if (grub_isdigit((int)value[i])) {
> +      digit = value[i] - '0';
> +      value_int = (10*value_int) + digit;
>
This should be grub_strtol and not custom loop

> +    }
> +  }
> +
> +  for (i=0;i<grub_strlen(args[1]);i++) {
> +    if (grub_isdigit((int)args[1][i])) {
> +      digit = args[1][i] - '0';
> +      arg_int = (10*arg_int) + digit;
> +    } else {
> +      return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Value must be a
> positive interger"));
> +    }
> +  }
> +
> +  ret=value_int+arg_int;
> +  /* grub_printf ("%d\n", ret); */
> +
> +  grub_snprintf (buf, sizeof (buf), "%d", ret);
> +  /* grub_printf ("%s\n", buf); */
> +  grub_env_set (args[0], buf);
> +  return 0;
> +}
> +
> +static grub_err_t
> +grub_cmd_sub (grub_extcmd_context_t ctxt __attribute__ ((unused)), int
> argc, char **args)
> +{
> +  unsigned int i;
> +  int ret, value_int=0, arg_int=0, digit;
> +  const char *value;
> +  char buf[512];
> +
> +  if (argc != 2)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable and value
> required"));
> +
> +  value = grub_env_get (args[0]);
> +  if (!value)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Variable must be
> defined"));
> +
> +  for (i=0;i<grub_strlen(value);i++) {
> +    if (grub_isdigit((int)value[i])) {
> +      digit = value[i] - '0';
> +      value_int = (10*value_int) + digit;
> +    }
> +  }
> +
> +  for (i=0;i<grub_strlen(args[1]);i++) {
> +    if (grub_isdigit((int)args[1][i])) {
> +      digit = args[1][i] - '0';
> +      arg_int = (10*arg_int) + digit;
> +    } else {
> +      return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Value must be a
> positive interger"));
> +    }
> +  }
> +
> +  if (arg_int > value_int)
> +    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Result may not be
> negitive"));
> +
> +  ret=value_int-arg_int;
> +  /* grub_printf ("%d\n", ret); */
> +
> +  grub_snprintf (buf, sizeof (buf), "%d", ret);
> +  /* grub_printf ("%s\n", buf); */
> +  grub_env_set (args[0], buf);
> +  return 0;
> +}
> +
> +static grub_extcmd_t cmd_add, cmd_sub;
> +
>
> +GRUB_MOD_INIT(basic_math)
> +{
> +  cmd_add =
> +    grub_register_extcmd ("add", grub_cmd_add, 0,
> +                         N_("<variable_name> <value>"),
> +                         N_("Add value to variable."),
> +                         0);
> +  cmd_sub =
> +    grub_register_extcmd ("sub", grub_cmd_sub, 0,
> +                         N_("<variable_name> <value>"),
> +                         N_("Subtract value from variable."),
> +                         0);
> +}
> +
> +GRUB_MOD_FINI(basic_math)
> +{
> +  grub_unregister_extcmd (cmd_add);
> +  grub_unregister_extcmd (cmd_sub);
> +}
>
> --
> Regards,
> Glenn Tanner
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>

[-- Attachment #2: Type: text/html, Size: 7572 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-07-16 19:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-16 16:44 [PATCH] grub-core/commands/basic_math.c: New commands to perform basic math operations on variables glenn tanner
2019-07-16 19:47 ` Vladimir 'phcoder' Serbinenko
2019-07-16 19:48 ` Vladimir 'phcoder' Serbinenko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.