* Re: [PATCH 1/2] Exporting variables to upper levels
[not found] <mailman.5258.1748282307.3741.grub-devel@gnu.org>
@ 2025-05-27 11:46 ` Avnish Chouhan
0 siblings, 0 replies; 4+ messages in thread
From: Avnish Chouhan @ 2025-05-27 11:46 UTC (permalink / raw)
To: projects; +Cc: grub-devel, grub-devel-request, daniel.kiper
> Message: 4
> Date: Mon, 26 May 2025 19:22:38 +0200
> From: Jiří 'bindiff' Wolker <projects@jwo.cz>
> To: grub-devel@gnu.org
> Subject: [PATCH 1/2] Exporting variables to upper levels
> Message-ID: <853421b8-f534-45a9-9c21-0769148fc4a5@jwo.cz>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> This adds new options ‘-g’ and ‘-u’ to the ‘export’ command.
>
> By default, the ‘export’ command exports the variables to
> lower environment context nesting levels, but not to the upper levels.
> This behavior is left unchanged from the original version.
>
> Using of the ‘-g’ option allows exporting of the given variable(s)
> even to the upper levels.
>
> The ‘-u’ option complements the default behavior of ‘export’ command:
> It causes the variable to become local, i.e., un-exported.
> ---
> grub-core/kern/env.c | 11 ++++--
> grub-core/normal/context.c | 70
> ++++++++++++++++++++++++++++++--------
> include/grub/env.h | 11 +++++-
> 3 files changed, 75 insertions(+), 17 deletions(-)
>
> diff --git a/grub-core/kern/env.c b/grub-core/kern/env.c
> index 764068896..6b60c71de 100644
> --- a/grub-core/kern/env.c
> +++ b/grub-core/kern/env.c
> @@ -231,7 +231,8 @@ grub_register_variable_hook (const char *name,
> }
> grub_err_t
> -grub_env_export (const char *name)
> +grub_env_set_export_mode (const char *name,
> + enum grub_env_var_export mode)
Hi Jiří,
This above statement would be better in one line!
grub_env_set_export_mode (const char *name, enum grub_env_var_export
mode)
> {
> struct grub_env_var *var;
> @@ -245,7 +246,13 @@ grub_env_export (const char *name)
> return err;
> var = grub_env_find (name);
> }
> - var->global = 1;
> + var->global = mode;
> return GRUB_ERR_NONE;
> }
> +
> +grub_err_t
> +grub_env_export (const char *name)
> +{
> + return grub_env_set_export_mode (name, grub_env_var_exported);
> +}
> diff --git a/grub-core/normal/context.c b/grub-core/normal/context.c
> index ba185e915..8abe5065c 100644
> --- a/grub-core/normal/context.c
> +++ b/grub-core/normal/context.c
> @@ -22,6 +22,7 @@
> #include <grub/misc.h>
> #include <grub/mm.h>
> #include <grub/command.h>
> +#include <grub/extcmd.h>
> #include <grub/normal.h>
> #include <grub/i18n.h>
> @@ -88,7 +89,7 @@ grub_env_new_context (int export_all)
> grub_env_context_close ();
> return grub_errno;
> }
> - grub_env_export (var->name);
> + grub_env_set_export_mode (var->name, var->global);
> grub_register_variable_hook (var->name, var->read_hook,
> var->write_hook);
> }
> }
> @@ -115,31 +116,52 @@ grub_err_t
> grub_env_context_close (void)
> {
> struct grub_env_context *context;
> + struct grub_env_context *original_context;
> int i;
> struct menu_pointer *menu;
> + grub_err_t last_err = GRUB_ERR_NONE;
> if (! grub_current_context->prev)
> return grub_error (GRUB_ERR_BAD_ARGUMENT,
> "cannot close the initial context");
> - /* Free the variables associated with this context. */
> + /* Restore the previous context. */
> + original_context = grub_current_context;
> + context = grub_current_context->prev;
> + grub_current_context = context;
> +
> + /* Re-export global variables to the upper context. */
> +
> for (i = 0; i < HASHSZ; i++)
> {
> struct grub_env_var *p, *q;
> - for (p = grub_current_context->vars[i]; p; p = q)
> + for (p = original_context->vars[i]; p; p = q)
> {
> q = p->next;
> +
> + /* Copy global variables to the upper context. */
> + if (p->global == grub_env_var_global) {
Please move '{' to next line!
> + if (grub_env_set (p->name, p->value) != GRUB_ERR_NONE)
> + {
> + last_err = grub_errno;
> + }
Brackets are not required here!
> + else
> + {
> + grub_env_set_export_mode (p->name, p->global);
> + grub_register_variable_hook (p->name, p->read_hook, p->write_hook);
> + }
> + }
> +
> + /* Free the variables associated with this context. */
> grub_free (p->name);
> grub_free (p->value);
> grub_free (p);
> }
> }
> - /* Restore the previous context. */
> - context = grub_current_context->prev;
> - grub_free (grub_current_context);
> - grub_current_context = context;
> + /* Free the previous context. */
> + grub_free (original_context);
> menu = current_menu->prev;
> if (current_menu->menu)
> @@ -181,34 +203,54 @@ grub_env_extractor_close (int source)
> return err;
> }
> -static grub_command_t export_cmd;
> +static grub_extcmd_t export_cmd;
> static grub_err_t
> -grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
> +grub_cmd_export (grub_extcmd_context_t ctxt,
> int argc, char **args)
> {
> + struct grub_arg_list *state = ctxt->state;
> int i;
> + enum grub_env_var_export mode = grub_env_var_exported;
> + if (state[0].set)
> + mode = grub_env_var_local;
> + if (state[1].set)
> + mode = grub_env_var_global;
> + if (state[0].set && state[1].set)
> + return grub_error (GRUB_ERR_BAD_ARGUMENT,
> + N_("canot use both -u and -a at once"));
Indention seems off here!
return grub_error (GRUB_ERR_BAD_ARGUMENT,
N_("canot use both -u and -a at once"));
> +
> if (argc < 1)
> return grub_error (GRUB_ERR_BAD_ARGUMENT,
> N_("one argument expected"));
> for (i = 0; i < argc; i++)
> - grub_env_export (args[i]);
> + grub_env_set_export_mode (args[i], mode);
> return 0;
> }
> +static const struct grub_arg_option export_options[] =
> + {
> + {"unexport", 'u', 0, N_("Undo exporting the variable."), 0, 0},
> + {"global", 'g', 0, N_("Export also to parent contexts."), 0, 0},
> + {0, 0, 0, 0, 0, 0}
> + };
> +
> void
> grub_context_init (void)
> {
> - export_cmd = grub_register_command ("export", grub_cmd_export,
> - N_("ENVVAR [ENVVAR] ..."),
> - N_("Export variables."));
> + export_cmd = grub_register_extcmd ("export", grub_cmd_export,
> + GRUB_COMMAND_ACCEPT_DASH
> + | GRUB_COMMAND_OPTIONS_AT_START,
> + N_("[-u|-g] ENVVAR [ENVVAR] ..."),
> + N_("Export variables."),
> + export_options);
Same as mentioned above!
> }
> void
> grub_context_fini (void)
> {
> - grub_unregister_command (export_cmd);
> + grub_unregister_extcmd (export_cmd);
> }
> diff --git a/include/grub/env.h b/include/grub/env.h
> index 6b9379a30..4634c09d3 100644
> --- a/include/grub/env.h
> +++ b/include/grub/env.h
> @@ -31,6 +31,13 @@ typedef const char *(*grub_env_read_hook_t) (struct
> grub_env_var *var,
> typedef char *(*grub_env_write_hook_t) (struct grub_env_var *var,
> const char *val);
> +enum grub_env_var_export
> +{
> + grub_env_var_local = 0,
> + grub_env_var_exported = 1,
> + grub_env_var_global = 2,
> +};
> +
> struct grub_env_var
> {
> char *name;
> @@ -40,7 +47,7 @@ struct grub_env_var
> struct grub_env_var *next;
> struct grub_env_var **prevp;
> struct grub_env_var *sorted_next;
> - int global;
> + enum grub_env_var_export global;
> };
> grub_err_t EXPORT_FUNC(grub_env_set) (const char *name, const char
> *val);
> @@ -58,6 +65,8 @@ grub_err_t EXPORT_FUNC(grub_register_variable_hook)
> (const char *name,
> grub_err_t grub_env_context_open (void);
> grub_err_t grub_env_context_close (void);
> grub_err_t EXPORT_FUNC(grub_env_export) (const char *name);
> +grub_err_t EXPORT_FUNC(grub_env_set_export_mode) (const char *name,
> + enum grub_env_var_export mode);
Same as mentioned above!
Thank you!
Regards,
Avnish Chouhan
> void grub_env_unset_menu (void);
> grub_menu_t grub_env_get_menu (void);
> --
> 2.45.3
>
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] Exporting variables to upper levels
@ 2025-05-26 17:22 Jiří 'bindiff' Wolker via Grub-devel
2025-05-26 19:53 ` Vladimir 'phcoder' Serbinenko
0 siblings, 1 reply; 4+ messages in thread
From: Jiří 'bindiff' Wolker via Grub-devel @ 2025-05-26 17:22 UTC (permalink / raw)
To: grub-devel; +Cc: Jiří 'bindiff' Wolker
This adds new options ‘-g’ and ‘-u’ to the ‘export’ command.
By default, the ‘export’ command exports the variables to
lower environment context nesting levels, but not to the upper levels.
This behavior is left unchanged from the original version.
Using of the ‘-g’ option allows exporting of the given variable(s)
even to the upper levels.
The ‘-u’ option complements the default behavior of ‘export’ command:
It causes the variable to become local, i.e., un-exported.
---
grub-core/kern/env.c | 11 ++++--
grub-core/normal/context.c | 70 ++++++++++++++++++++++++++++++--------
include/grub/env.h | 11 +++++-
3 files changed, 75 insertions(+), 17 deletions(-)
diff --git a/grub-core/kern/env.c b/grub-core/kern/env.c
index 764068896..6b60c71de 100644
--- a/grub-core/kern/env.c
+++ b/grub-core/kern/env.c
@@ -231,7 +231,8 @@ grub_register_variable_hook (const char *name,
}
grub_err_t
-grub_env_export (const char *name)
+grub_env_set_export_mode (const char *name,
+ enum grub_env_var_export mode)
{
struct grub_env_var *var;
@@ -245,7 +246,13 @@ grub_env_export (const char *name)
return err;
var = grub_env_find (name);
}
- var->global = 1;
+ var->global = mode;
return GRUB_ERR_NONE;
}
+
+grub_err_t
+grub_env_export (const char *name)
+{
+ return grub_env_set_export_mode (name, grub_env_var_exported);
+}
diff --git a/grub-core/normal/context.c b/grub-core/normal/context.c
index ba185e915..8abe5065c 100644
--- a/grub-core/normal/context.c
+++ b/grub-core/normal/context.c
@@ -22,6 +22,7 @@
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/command.h>
+#include <grub/extcmd.h>
#include <grub/normal.h>
#include <grub/i18n.h>
@@ -88,7 +89,7 @@ grub_env_new_context (int export_all)
grub_env_context_close ();
return grub_errno;
}
- grub_env_export (var->name);
+ grub_env_set_export_mode (var->name, var->global);
grub_register_variable_hook (var->name, var->read_hook,
var->write_hook);
}
}
@@ -115,31 +116,52 @@ grub_err_t
grub_env_context_close (void)
{
struct grub_env_context *context;
+ struct grub_env_context *original_context;
int i;
struct menu_pointer *menu;
+ grub_err_t last_err = GRUB_ERR_NONE;
if (! grub_current_context->prev)
return grub_error (GRUB_ERR_BAD_ARGUMENT,
"cannot close the initial context");
- /* Free the variables associated with this context. */
+ /* Restore the previous context. */
+ original_context = grub_current_context;
+ context = grub_current_context->prev;
+ grub_current_context = context;
+
+ /* Re-export global variables to the upper context. */
+
for (i = 0; i < HASHSZ; i++)
{
struct grub_env_var *p, *q;
- for (p = grub_current_context->vars[i]; p; p = q)
+ for (p = original_context->vars[i]; p; p = q)
{
q = p->next;
+
+ /* Copy global variables to the upper context. */
+ if (p->global == grub_env_var_global) {
+ if (grub_env_set (p->name, p->value) != GRUB_ERR_NONE)
+ {
+ last_err = grub_errno;
+ }
+ else
+ {
+ grub_env_set_export_mode (p->name, p->global);
+ grub_register_variable_hook (p->name, p->read_hook, p->write_hook);
+ }
+ }
+
+ /* Free the variables associated with this context. */
grub_free (p->name);
grub_free (p->value);
grub_free (p);
}
}
- /* Restore the previous context. */
- context = grub_current_context->prev;
- grub_free (grub_current_context);
- grub_current_context = context;
+ /* Free the previous context. */
+ grub_free (original_context);
menu = current_menu->prev;
if (current_menu->menu)
@@ -181,34 +203,54 @@ grub_env_extractor_close (int source)
return err;
}
-static grub_command_t export_cmd;
+static grub_extcmd_t export_cmd;
static grub_err_t
-grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
+grub_cmd_export (grub_extcmd_context_t ctxt,
int argc, char **args)
{
+ struct grub_arg_list *state = ctxt->state;
int i;
+ enum grub_env_var_export mode = grub_env_var_exported;
+ if (state[0].set)
+ mode = grub_env_var_local;
+ if (state[1].set)
+ mode = grub_env_var_global;
+ if (state[0].set && state[1].set)
+ return grub_error (GRUB_ERR_BAD_ARGUMENT,
+ N_("canot use both -u and -a at once"));
+
if (argc < 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT,
N_("one argument expected"));
for (i = 0; i < argc; i++)
- grub_env_export (args[i]);
+ grub_env_set_export_mode (args[i], mode);
return 0;
}
+static const struct grub_arg_option export_options[] =
+ {
+ {"unexport", 'u', 0, N_("Undo exporting the variable."), 0, 0},
+ {"global", 'g', 0, N_("Export also to parent contexts."), 0, 0},
+ {0, 0, 0, 0, 0, 0}
+ };
+
void
grub_context_init (void)
{
- export_cmd = grub_register_command ("export", grub_cmd_export,
- N_("ENVVAR [ENVVAR] ..."),
- N_("Export variables."));
+ export_cmd = grub_register_extcmd ("export", grub_cmd_export,
+ GRUB_COMMAND_ACCEPT_DASH
+ | GRUB_COMMAND_OPTIONS_AT_START,
+ N_("[-u|-g] ENVVAR [ENVVAR] ..."),
+ N_("Export variables."),
+ export_options);
}
void
grub_context_fini (void)
{
- grub_unregister_command (export_cmd);
+ grub_unregister_extcmd (export_cmd);
}
diff --git a/include/grub/env.h b/include/grub/env.h
index 6b9379a30..4634c09d3 100644
--- a/include/grub/env.h
+++ b/include/grub/env.h
@@ -31,6 +31,13 @@ typedef const char *(*grub_env_read_hook_t) (struct
grub_env_var *var,
typedef char *(*grub_env_write_hook_t) (struct grub_env_var *var,
const char *val);
+enum grub_env_var_export
+{
+ grub_env_var_local = 0,
+ grub_env_var_exported = 1,
+ grub_env_var_global = 2,
+};
+
struct grub_env_var
{
char *name;
@@ -40,7 +47,7 @@ struct grub_env_var
struct grub_env_var *next;
struct grub_env_var **prevp;
struct grub_env_var *sorted_next;
- int global;
+ enum grub_env_var_export global;
};
grub_err_t EXPORT_FUNC(grub_env_set) (const char *name, const char *val);
@@ -58,6 +65,8 @@ grub_err_t EXPORT_FUNC(grub_register_variable_hook)
(const char *name,
grub_err_t grub_env_context_open (void);
grub_err_t grub_env_context_close (void);
grub_err_t EXPORT_FUNC(grub_env_export) (const char *name);
+grub_err_t EXPORT_FUNC(grub_env_set_export_mode) (const char *name,
+ enum grub_env_var_export mode);
void grub_env_unset_menu (void);
grub_menu_t grub_env_get_menu (void);
--
2.45.3
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH 1/2] Exporting variables to upper levels
2025-05-26 17:22 Jiří 'bindiff' Wolker via Grub-devel
@ 2025-05-26 19:53 ` Vladimir 'phcoder' Serbinenko
2025-05-26 20:26 ` Jiří 'bindiff' Wolker via Grub-devel
0 siblings, 1 reply; 4+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2025-05-26 19:53 UTC (permalink / raw)
To: The development of GNU GRUB; +Cc: Jiří 'bindiff' Wolker
[-- Attachment #1.1: Type: text/plain, Size: 8233 bytes --]
Please explain what problem you try to solve. Bash doesn't do anything of
the kind. Why should we? Looks like a solution without a problem
Regards
Vladimir 'phcoder' Serbinenko
Le lun. 26 mai 2025, 20:34, Jiří 'bindiff' Wolker via Grub-devel <
grub-devel@gnu.org> a écrit :
> This adds new options ‘-g’ and ‘-u’ to the ‘export’ command.
>
> By default, the ‘export’ command exports the variables to
> lower environment context nesting levels, but not to the upper levels.
> This behavior is left unchanged from the original version.
>
> Using of the ‘-g’ option allows exporting of the given variable(s)
> even to the upper levels.
>
> The ‘-u’ option complements the default behavior of ‘export’ command:
> It causes the variable to become local, i.e., un-exported.
> ---
> grub-core/kern/env.c | 11 ++++--
> grub-core/normal/context.c | 70 ++++++++++++++++++++++++++++++--------
> include/grub/env.h | 11 +++++-
> 3 files changed, 75 insertions(+), 17 deletions(-)
>
> diff --git a/grub-core/kern/env.c b/grub-core/kern/env.c
> index 764068896..6b60c71de 100644
> --- a/grub-core/kern/env.c
> +++ b/grub-core/kern/env.c
> @@ -231,7 +231,8 @@ grub_register_variable_hook (const char *name,
> }
> grub_err_t
> -grub_env_export (const char *name)
> +grub_env_set_export_mode (const char *name,
> + enum grub_env_var_export mode)
> {
> struct grub_env_var *var;
> @@ -245,7 +246,13 @@ grub_env_export (const char *name)
> return err;
> var = grub_env_find (name);
> }
> - var->global = 1;
> + var->global = mode;
> return GRUB_ERR_NONE;
> }
> +
> +grub_err_t
> +grub_env_export (const char *name)
> +{
> + return grub_env_set_export_mode (name, grub_env_var_exported);
> +}
> diff --git a/grub-core/normal/context.c b/grub-core/normal/context.c
> index ba185e915..8abe5065c 100644
> --- a/grub-core/normal/context.c
> +++ b/grub-core/normal/context.c
> @@ -22,6 +22,7 @@
> #include <grub/misc.h>
> #include <grub/mm.h>
> #include <grub/command.h>
> +#include <grub/extcmd.h>
> #include <grub/normal.h>
> #include <grub/i18n.h>
> @@ -88,7 +89,7 @@ grub_env_new_context (int export_all)
> grub_env_context_close ();
> return grub_errno;
> }
> - grub_env_export (var->name);
> + grub_env_set_export_mode (var->name, var->global);
> grub_register_variable_hook (var->name, var->read_hook,
> var->write_hook);
> }
> }
> @@ -115,31 +116,52 @@ grub_err_t
> grub_env_context_close (void)
> {
> struct grub_env_context *context;
> + struct grub_env_context *original_context;
> int i;
> struct menu_pointer *menu;
> + grub_err_t last_err = GRUB_ERR_NONE;
> if (! grub_current_context->prev)
> return grub_error (GRUB_ERR_BAD_ARGUMENT,
> "cannot close the initial context");
> - /* Free the variables associated with this context. */
> + /* Restore the previous context. */
> + original_context = grub_current_context;
> + context = grub_current_context->prev;
> + grub_current_context = context;
> +
> + /* Re-export global variables to the upper context. */
> +
> for (i = 0; i < HASHSZ; i++)
> {
> struct grub_env_var *p, *q;
> - for (p = grub_current_context->vars[i]; p; p = q)
> + for (p = original_context->vars[i]; p; p = q)
> {
> q = p->next;
> +
> + /* Copy global variables to the upper context. */
> + if (p->global == grub_env_var_global) {
> + if (grub_env_set (p->name, p->value) != GRUB_ERR_NONE)
> + {
> + last_err = grub_errno;
> + }
> + else
> + {
> + grub_env_set_export_mode (p->name, p->global);
> + grub_register_variable_hook (p->name, p->read_hook,
> p->write_hook);
> + }
> + }
> +
> + /* Free the variables associated with this context. */
> grub_free (p->name);
> grub_free (p->value);
> grub_free (p);
> }
> }
> - /* Restore the previous context. */
> - context = grub_current_context->prev;
> - grub_free (grub_current_context);
> - grub_current_context = context;
> + /* Free the previous context. */
> + grub_free (original_context);
> menu = current_menu->prev;
> if (current_menu->menu)
> @@ -181,34 +203,54 @@ grub_env_extractor_close (int source)
> return err;
> }
> -static grub_command_t export_cmd;
> +static grub_extcmd_t export_cmd;
> static grub_err_t
> -grub_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
> +grub_cmd_export (grub_extcmd_context_t ctxt,
> int argc, char **args)
> {
> + struct grub_arg_list *state = ctxt->state;
> int i;
> + enum grub_env_var_export mode = grub_env_var_exported;
> + if (state[0].set)
> + mode = grub_env_var_local;
> + if (state[1].set)
> + mode = grub_env_var_global;
> + if (state[0].set && state[1].set)
> + return grub_error (GRUB_ERR_BAD_ARGUMENT,
> + N_("canot use both -u and -a at once"));
> +
> if (argc < 1)
> return grub_error (GRUB_ERR_BAD_ARGUMENT,
> N_("one argument expected"));
> for (i = 0; i < argc; i++)
> - grub_env_export (args[i]);
> + grub_env_set_export_mode (args[i], mode);
> return 0;
> }
> +static const struct grub_arg_option export_options[] =
> + {
> + {"unexport", 'u', 0, N_("Undo exporting the variable."), 0, 0},
> + {"global", 'g', 0, N_("Export also to parent contexts."), 0, 0},
> + {0, 0, 0, 0, 0, 0}
> + };
> +
> void
> grub_context_init (void)
> {
> - export_cmd = grub_register_command ("export", grub_cmd_export,
> - N_("ENVVAR [ENVVAR] ..."),
> - N_("Export variables."));
> + export_cmd = grub_register_extcmd ("export", grub_cmd_export,
> + GRUB_COMMAND_ACCEPT_DASH
> + | GRUB_COMMAND_OPTIONS_AT_START,
> + N_("[-u|-g] ENVVAR [ENVVAR] ..."),
> + N_("Export variables."),
> + export_options);
> }
> void
> grub_context_fini (void)
> {
> - grub_unregister_command (export_cmd);
> + grub_unregister_extcmd (export_cmd);
> }
> diff --git a/include/grub/env.h b/include/grub/env.h
> index 6b9379a30..4634c09d3 100644
> --- a/include/grub/env.h
> +++ b/include/grub/env.h
> @@ -31,6 +31,13 @@ typedef const char *(*grub_env_read_hook_t) (struct
> grub_env_var *var,
> typedef char *(*grub_env_write_hook_t) (struct grub_env_var *var,
> const char *val);
> +enum grub_env_var_export
> +{
> + grub_env_var_local = 0,
> + grub_env_var_exported = 1,
> + grub_env_var_global = 2,
> +};
> +
> struct grub_env_var
> {
> char *name;
> @@ -40,7 +47,7 @@ struct grub_env_var
> struct grub_env_var *next;
> struct grub_env_var **prevp;
> struct grub_env_var *sorted_next;
> - int global;
> + enum grub_env_var_export global;
> };
> grub_err_t EXPORT_FUNC(grub_env_set) (const char *name, const char
> *val);
> @@ -58,6 +65,8 @@ grub_err_t EXPORT_FUNC(grub_register_variable_hook)
> (const char *name,
> grub_err_t grub_env_context_open (void);
> grub_err_t grub_env_context_close (void);
> grub_err_t EXPORT_FUNC(grub_env_export) (const char *name);
> +grub_err_t EXPORT_FUNC(grub_env_set_export_mode) (const char *name,
> + enum grub_env_var_export
> mode);
> void grub_env_unset_menu (void);
> grub_menu_t grub_env_get_menu (void);
> --
> 2.45.3
>
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>
[-- Attachment #1.2: Type: text/html, Size: 10075 bytes --]
[-- Attachment #2: Type: text/plain, Size: 141 bytes --]
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH 1/2] Exporting variables to upper levels
2025-05-26 19:53 ` Vladimir 'phcoder' Serbinenko
@ 2025-05-26 20:26 ` Jiří 'bindiff' Wolker via Grub-devel
0 siblings, 0 replies; 4+ messages in thread
From: Jiří 'bindiff' Wolker via Grub-devel @ 2025-05-26 20:26 UTC (permalink / raw)
To: Vladimir 'phcoder' Serbinenko
Cc: Jiří 'bindiff' Wolker, grub-devel
> Please explain what problem you try to solve. Bash doesn't do anything
> of the kind. Why should we? Looks like a solution without a problem
I'll provide you an example script similar to one which I use, heavily
simplified to present only the specific issue:
set kern_tty=tty
menuentry "Start system" {
linux /kernel tty=$kern_tty […]
}
submenu "Kernel console configuration options" {
menuentry "Use serial console @ 115200 bd" {
set kern_tty=ttyS0,115200
export -g kern_tty
}
menuentry "Use screen" {
set kern_tty=tty
export -g kern_tty
}
}
submenu "System distribution" {
# […]
}
This is my primary use-case. I want to change environment variables
used from scopes above the scopes in which the submenus are executed.
This allows me to create an user-friendly bootable USB/CD image allowing
to perform configuration of the system before starting the system. Since
I have multiple options to configure, using deeply nested submenus would
be tiring for the user (since they would have to go through all the
menus on every boot) and it would not allow to save the settings for the
next boot using save_env.
Bash does have this, but it is the default behavior, unlike the behavior
in GRUB for the ‘submenu’ command. Consider this bash script:
$ cat test.sh
foo=bar
function uwu {
foo=baz
}
uwu
echo $foo
$ bash test.sh
baz
Assignemnt performed in a function takes effect in the parent scope.
If you run equivalent script in the GRUB (just prepend ‘set’ to the
assignment lines), the behavior is still exactly the same. However,
this is not true for when used from the ‘submenu’ command. Try this
in the GRUB:
set foo=bar
submenu "foo configuration" {
menuentry "set foo" {
set foo=baz
}
}
menuentry "display foo" {
echo $foo
read
}
When the latter menu entry is executed, it will still display “bar”,
because the ‘submenu’ creates a new environment variable scope.
The only two alternatives to this would be either creating a completely
custom interface using ‘echo’ and ‘read’, or creating a single flat
menu. The first option would make the pre-boot user interface harder to
use. The latter option will cause that the menu is a incomprehensible
mess, when there is a lot of configuration options.
And because I don't want to break compatibility with existing scripts,
I decided to make the exporting variables up from submenus explicit.
Thanks.
bindiff
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-05-27 11:47 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <mailman.5258.1748282307.3741.grub-devel@gnu.org>
2025-05-27 11:46 ` [PATCH 1/2] Exporting variables to upper levels Avnish Chouhan
2025-05-26 17:22 Jiří 'bindiff' Wolker via Grub-devel
2025-05-26 19:53 ` Vladimir 'phcoder' Serbinenko
2025-05-26 20:26 ` Jiří 'bindiff' Wolker via Grub-devel
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.