All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/3] Customization of GRUB menu title
  2025-05-27 11:54 [PATCH v2 0/3] Allow script-configurable GRUB menu title Jiří 'bindiff' Wolker via Grub-devel
@ 2025-05-25 19:55 ` Jiří Wolker via Grub-devel
  2025-05-26 16:15 ` [PATCH v2 2/3] Option to set title of submenus Jiří Wolker via Grub-devel
  2025-05-26 16:21 ` [PATCH v2 3/3] Documentation for the --menutitle option Jiří ‘bindiff’ Wolker via Grub-devel
  2 siblings, 0 replies; 6+ messages in thread
From: Jiří Wolker via Grub-devel @ 2025-05-25 19:55 UTC (permalink / raw)
  To: grub-devel; +Cc: Jiří Wolker


Since these changes, the module ‘normal’ initializes environment variable
‘grub_menu_title’ with the previously hardcoded text. The variable is
automatically exported, so it applies also to submenus.

Scripts may change this variable to change the title of the menu.
---
 grub-core/normal/main.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
index 96abfda2f..b341cebe0 100644
--- a/grub-core/normal/main.c
+++ b/grub-core/normal/main.c
@@ -203,19 +203,18 @@ grub_normal_init_page (struct grub_term_output *term,
 {
   grub_ssize_t msg_len;
   int posx;
-  char *msg_formatted;
+  const char *msg_title;
   grub_uint32_t *unicode_msg;
   grub_uint32_t *last_position;
 
   grub_term_cls (term);
 
-  msg_formatted = grub_xasprintf (_("GNU GRUB  version %s"), PACKAGE_VERSION);
-  if (!msg_formatted)
+  msg_title = grub_env_get ("grub_menu_title");
+  if (! msg_title)
     return;
 
-  msg_len = grub_utf8_to_ucs4_alloc (msg_formatted,
-  				     &unicode_msg, &last_position);
-  grub_free (msg_formatted);
+  msg_len = grub_utf8_to_ucs4_alloc (msg_title,
+				     &unicode_msg, &last_position);
 
   if (msg_len < 0)
     {
@@ -315,6 +314,16 @@ static grub_err_t
 grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
 		 int argc, char *argv[])
 {
+  if (grub_env_get ("grub_menu_title") == NULL)
+    {
+      /* Initialize the menu title with a GRUB version information. */
+      char *title;
+      title = grub_xasprintf (_("GNU GRUB  version %s"), PACKAGE_VERSION);
+      grub_env_set ("grub_menu_title", title);
+      grub_free (title);
+      grub_env_export ("grub_menu_title");
+    }
+
   if (argc == 0)
     {
       /* Guess the config filename. It is necessary to make CONFIG static,
-- 
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] 6+ messages in thread

* [PATCH v2 2/3] Option to set title of submenus
  2025-05-27 11:54 [PATCH v2 0/3] Allow script-configurable GRUB menu title Jiří 'bindiff' Wolker via Grub-devel
  2025-05-25 19:55 ` [PATCH v2 1/3] Customization of " Jiří Wolker via Grub-devel
@ 2025-05-26 16:15 ` Jiří Wolker via Grub-devel
  2025-05-27 13:14   ` Vladimir 'phcoder' Serbinenko
  2025-05-26 16:21 ` [PATCH v2 3/3] Documentation for the --menutitle option Jiří ‘bindiff’ Wolker via Grub-devel
  2 siblings, 1 reply; 6+ messages in thread
From: Jiří Wolker via Grub-devel @ 2025-05-26 16:15 UTC (permalink / raw)
  To: grub-devel; +Cc: Jiří Wolker

---
 grub-core/commands/menuentry.c | 47 ++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/grub-core/commands/menuentry.c b/grub-core/commands/menuentry.c
index 720e6d8ea..c68afae38 100644
--- a/grub-core/commands/menuentry.c
+++ b/grub-core/commands/menuentry.c
@@ -43,6 +43,9 @@ static const struct grub_arg_option options[] =
        anyone can boot it.  */
     {"unrestricted", 0, 0, N_("This entry can be booted by any user."),
      0, ARG_TYPE_NONE},
+    {"menutitle", 0, 0,
+     N_("Use STRING as menu title. When omitted, use the entry label."),
+     N_("TITLE"), ARG_TYPE_STRING},
     {0, 0, 0, 0, 0, 0}
   };
 
@@ -252,12 +255,44 @@ setparams_prefix (int argc, char **args)
   return result;
 }
 
+static char *
+menutitle_prefix (char *old_prefix, const char *title)
+{
+  const char *t;
+  char *p;
+  char *result;
+  grub_size_t len = 22;
+  grub_size_t old_len = grub_strlen (old_prefix);
+
+  /* Count resulting string length. */
+  for (t = title; *t; t++)
+    len += (*t++ == '\'' ? 4 : 1);
+
+  result = grub_malloc (old_len + len + 2);
+  if (! result)
+    return 0;
+
+  p = result;
+  grub_strcpy (p, old_prefix);
+  p = result + old_len;
+  grub_strcpy (p, "set grub_menu_title='");
+  p += 21;
+
+  p = grub_strchrsub (p, title, '\'', "'\\''");
+  *p++ = '\'';
+  *p++ = '\n';
+  *p = '\0';
+
+  return result;
+}
+
 static grub_err_t
 grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
 {
   char ch;
   char *src;
   char *prefix;
+  char *old_prefix;
   unsigned len;
   grub_err_t r;
   const char *users;
@@ -299,6 +334,18 @@ grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
   if (! prefix)
     return grub_errno;
 
+  /* Prepend the menu title line. */
+  if (ctxt->state[6].arg)
+    {
+      old_prefix = prefix;
+      prefix = menutitle_prefix (old_prefix, (ctxt->state[6].arg
+				 && grub_strlen (ctxt->state[6].arg) > 0)
+				 ? ctxt->state[6].arg : args[0]);
+      if (! prefix)
+	return grub_errno;
+      grub_free (old_prefix);
+    }
+
   r = grub_normal_add_menu_entry (argc - 1, (const char **) args,
 				  ctxt->state[0].args, ctxt->state[4].arg,
 				  users,
-- 
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] 6+ messages in thread

* [PATCH v2 3/3] Documentation for the --menutitle option
  2025-05-27 11:54 [PATCH v2 0/3] Allow script-configurable GRUB menu title Jiří 'bindiff' Wolker via Grub-devel
  2025-05-25 19:55 ` [PATCH v2 1/3] Customization of " Jiří Wolker via Grub-devel
  2025-05-26 16:15 ` [PATCH v2 2/3] Option to set title of submenus Jiří Wolker via Grub-devel
@ 2025-05-26 16:21 ` Jiří ‘bindiff’ Wolker via Grub-devel
  2 siblings, 0 replies; 6+ messages in thread
From: Jiří ‘bindiff’ Wolker via Grub-devel @ 2025-05-26 16:21 UTC (permalink / raw)
  To: grub-devel; +Cc: Jiří ‘bindiff’ Wolker, projects

---
 docs/grub.texi | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/docs/grub.texi b/docs/grub.texi
index 34b3484dc..5b5e40c49 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -1845,7 +1845,7 @@ definitions do not affect the exit status in @code{$?}.  When executed, the
 exit status of a function is the exit status of the last command executed in
 the body.
 
-@item menuentry @var{title} [@option{--class=class} @dots{}] [@option{--users=users}] [@option{--unrestricted}] [@option{--hotkey=key}] [@option{--id=id}] @{ @var{command}; @dots{} @}
+@item menuentry @var{title} [@option{--class=class} @dots{}] [@option{--users=users}] [@option{--unrestricted}] [@option{--hotkey=key}] [@option{--id=id}] [@option{--menutitle=text}] @{ @var{command}; @dots{} @}
 @xref{menuentry}.
 @end table
 
@@ -6034,7 +6034,7 @@ These commands can only be used in the menu:
 
 @deffn Command menuentry @var{title} @
  [@option{--class=class} @dots{}] [@option{--users=users}] @
- [@option{--unrestricted}] [@option{--hotkey=key}] [@option{--id=id}] @
+ [@option{--unrestricted}] [@option{--hotkey=key}] [@option{--id=id}] [@option{--menutitle=text}] @
  [@var{arg} @dots{}] @{ @var{command}; @dots{} @}
 This defines a GRUB menu entry named @var{title}.  When this entry is
 selected from the menu, GRUB will set the @var{chosen} environment variable
@@ -6060,6 +6060,13 @@ The @option{--id} may be used to associate unique identifier with a menu entry.
 @var{id} is string of ASCII aphanumeric characters, underscore and hyphen
 and should not start with a digit.
 
+The @option{--menutitle} can override the text displayed above the
+frame in which the menu is shown. This is useful especially for the
+@command{submenu}, which shares options with
+@command{menuentry}. Nested menus will inherit the menu title, unless
+explicitly replaces. Using an empty string causes the @var{title} of
+the option to be used as the menu title.
+
 All other arguments including @var{title} are passed as positional parameters
 when list of commands is executed with @var{title} always assigned to @code{$1}.
 @end deffn
@@ -6071,7 +6078,7 @@ when list of commands is executed with @var{title} always assigned to @code{$1}.
 @deffn Command submenu @var{title} @
  [@option{--class=class} @dots{}] [@option{--users=users}] @
  [@option{--unrestricted}] [@option{--hotkey=key}] [@option{--id=id}] @
- @{ @var{menu entries} @dots{} @}
+ [@option{--menutitle=text}] @{ @var{menu entries} @dots{} @}
 This defines a submenu.  An entry called @var{title} will be added to the
 menu; when that entry is selected, a new menu will be displayed showing all
 the entries within this submenu.
-- 
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] 6+ messages in thread

* [PATCH v2 0/3] Allow script-configurable GRUB menu title
@ 2025-05-27 11:54 Jiří 'bindiff' Wolker via Grub-devel
  2025-05-25 19:55 ` [PATCH v2 1/3] Customization of " Jiří Wolker via Grub-devel
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jiří 'bindiff' Wolker via Grub-devel @ 2025-05-27 11:54 UTC (permalink / raw)
  To: grub-devel; +Cc: Jiří 'bindiff' Wolker


This patches allow setting custom title of menus in the GRUB menu-based
interface.

This introduces new environment variable ‘grub_menu_title’ which can be
set by the script to override the title shown in the text-based GRUB
menu. This replaces the “GNU GRUB  <version>” message, which was set to
this fixed value in the source code. Now, the module ‘normal’ uses only
this variable as the title text. As part of the module initialisation,
the value which was previously hard-coded, is set to this variable.

The ‘grub_menu_title’ variable is automatically exported to provide the
same title also in the submenus.

I have chosen the ‘grub_’ prefix to reduce possibility of clashes with
user-defined variables in existing GRUB scripts.

Any scripts using this variable to override the title will fail safely,
when a version of GNU GRUB before these changes is used to run them. The
GRUB will display the hard-coded title, not causing the script to fail
and only degrading the user-experience a bit.

Additionally, a new option ‘--menutitle’ is accepted by the ‘menuentry’
and ‘submenu’ commands. This option allows overriding the title for a
submenu. This leverages existing ‘prefix’ mechanism which was previously
used only to prepend the ‘setparams’ line to the source code of menu
entries and submenus.

If newly created scripts require to be compatible even with previous
versions of GRUB, the script authors can use one of these approaches:

* Use ‘set grub_menu_title="Some title"’ variable assignment
  explicitly. This will cause no compatibility issues.
* Use the ‘--menutitle’ option of the ‘submenu’ command _after_ the menu
  entry title. This will cause the option to be silently ignored by
  versions of GRUB not supporting this option, However, the title will
  be passed as a part of ‘setparams’ line of the menu entry or submenu.

I also decided to rename the ‘msg_formatted’ variable in
‘grub_normal_init_page()’ function to ‘msg_title’, which is a more
descriptive title.

My motivation for these changes was to make it easier to navigate in
more complex nested menus, such as on live disks containing a
multiplicity of operating systems.

Jiří Wolker (3):
  Customization of GRUB menu title
  Option to set title of submenus
  Documentation for the --menutitle option

 docs/grub.texi                 | 13 ++++++---
 grub-core/commands/menuentry.c | 49 ++++++++++++++++++++++++++++++++++
 grub-core/normal/main.c        | 21 ++++++++++-----
 3 files changed, 74 insertions(+), 9 deletions(-)

-- 
2.45.3


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

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

* Re: [PATCH v2 2/3] Option to set title of submenus
  2025-05-26 16:15 ` [PATCH v2 2/3] Option to set title of submenus Jiří Wolker via Grub-devel
@ 2025-05-27 13:14   ` Vladimir 'phcoder' Serbinenko
  2025-05-27 13:18     ` Jiří 'bindiff' Wolker via Grub-devel
  0 siblings, 1 reply; 6+ messages in thread
From: Vladimir 'phcoder' Serbinenko @ 2025-05-27 13:14 UTC (permalink / raw)
  To: The development of GNU GRUB; +Cc: Jiří Wolker


[-- Attachment #1.1: Type: text/plain, Size: 2970 bytes --]

Why specifying the text as direct argument to menu entry not enough?
menuentry "Title" {
....
}

Regards
Vladimir 'phcoder' Serbinenko

Le mar. 27 mai 2025, 15:57, Jiří Wolker via Grub-devel <grub-devel@gnu.org>
a écrit :

> ---
>  grub-core/commands/menuentry.c | 47 ++++++++++++++++++++++++++++++++++
>  1 file changed, 47 insertions(+)
>
> diff --git a/grub-core/commands/menuentry.c
> b/grub-core/commands/menuentry.c
> index 720e6d8ea..c68afae38 100644
> --- a/grub-core/commands/menuentry.c
> +++ b/grub-core/commands/menuentry.c
> @@ -43,6 +43,9 @@ static const struct grub_arg_option options[] =
>         anyone can boot it.  */
>      {"unrestricted", 0, 0, N_("This entry can be booted by any user."),
>       0, ARG_TYPE_NONE},
> +    {"menutitle", 0, 0,
> +     N_("Use STRING as menu title. When omitted, use the entry label."),
> +     N_("TITLE"), ARG_TYPE_STRING},
>      {0, 0, 0, 0, 0, 0}
>    };
>
> @@ -252,12 +255,44 @@ setparams_prefix (int argc, char **args)
>    return result;
>  }
>
> +static char *
> +menutitle_prefix (char *old_prefix, const char *title)
> +{
> +  const char *t;
> +  char *p;
> +  char *result;
> +  grub_size_t len = 22;
> +  grub_size_t old_len = grub_strlen (old_prefix);
> +
> +  /* Count resulting string length. */
> +  for (t = title; *t; t++)
> +    len += (*t++ == '\'' ? 4 : 1);
> +
> +  result = grub_malloc (old_len + len + 2);
> +  if (! result)
> +    return 0;
> +
> +  p = result;
> +  grub_strcpy (p, old_prefix);
> +  p = result + old_len;
> +  grub_strcpy (p, "set grub_menu_title='");
> +  p += 21;
> +
> +  p = grub_strchrsub (p, title, '\'', "'\\''");
> +  *p++ = '\'';
> +  *p++ = '\n';
> +  *p = '\0';
> +
> +  return result;
> +}
> +
>  static grub_err_t
>  grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
>  {
>    char ch;
>    char *src;
>    char *prefix;
> +  char *old_prefix;
>    unsigned len;
>    grub_err_t r;
>    const char *users;
> @@ -299,6 +334,18 @@ grub_cmd_menuentry (grub_extcmd_context_t ctxt, int
> argc, char **args)
>    if (! prefix)
>      return grub_errno;
>
> +  /* Prepend the menu title line. */
> +  if (ctxt->state[6].arg)
> +    {
> +      old_prefix = prefix;
> +      prefix = menutitle_prefix (old_prefix, (ctxt->state[6].arg
> +                                && grub_strlen (ctxt->state[6].arg) > 0)
> +                                ? ctxt->state[6].arg : args[0]);
> +      if (! prefix)
> +       return grub_errno;
> +      grub_free (old_prefix);
> +    }
> +
>    r = grub_normal_add_menu_entry (argc - 1, (const char **) args,
>                                   ctxt->state[0].args, ctxt->state[4].arg,
>                                   users,
> --
> 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: 4090 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] 6+ messages in thread

* Re: [PATCH v2 2/3] Option to set title of submenus
  2025-05-27 13:14   ` Vladimir 'phcoder' Serbinenko
@ 2025-05-27 13:18     ` Jiří 'bindiff' Wolker via Grub-devel
  0 siblings, 0 replies; 6+ messages in thread
From: Jiří 'bindiff' Wolker via Grub-devel @ 2025-05-27 13:18 UTC (permalink / raw)
  To: Vladimir 'phcoder' Serbinenko,
	The development of GNU GRUB
  Cc: Jiří 'bindiff' Wolker

> Why specifying the text as direct argument to menu entry not enough?
> menuentry "Title" {
> ....
> }

Sometimes, you can want to have some additional information, such as
menu entry hotkey, in the submenu title (i.e., its label in the parent
menu). This would result in cluttered menu titles.

Consider this situation:

submenu "<d> Debian Linux"       --hotkey=d { … }
submenu "<f> Fedora Workstation" --hotkey=d { … }

Main menu:               GNU GRUB  v2.xx
  ┌────────────────────────────────────────────────────────────┐

The first submenu:      <d> Debian Linux
  ┌────────────────────────────────────────────────────────────┐

That would result in “<d> Debian Linux” displayed in the menu title,
instead of only “Debian Linux”, when you can set the title manually
to precisely what you need.

Even worse situation happens when the script author tries to make some
parts of the menu entry label right-aligned etc. Example:



submenu "Debian Linux        <d>" --hotkey=d { … }
submenu "Fedora Workstation  <f>" --hotkey=d { … }

Main menu:               GNU GRUB  v2.xx
  ┌────────────────────────────────────────────────────────────┐
  ┆
The first submenu:   Debian Linux        <d>
  ┌────────────────────────────────────────────────────────────┐

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

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

end of thread, other threads:[~2025-05-27 14:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-27 11:54 [PATCH v2 0/3] Allow script-configurable GRUB menu title Jiří 'bindiff' Wolker via Grub-devel
2025-05-25 19:55 ` [PATCH v2 1/3] Customization of " Jiří Wolker via Grub-devel
2025-05-26 16:15 ` [PATCH v2 2/3] Option to set title of submenus Jiří Wolker via Grub-devel
2025-05-27 13:14   ` Vladimir 'phcoder' Serbinenko
2025-05-27 13:18     ` Jiří 'bindiff' Wolker via Grub-devel
2025-05-26 16:21 ` [PATCH v2 3/3] Documentation for the --menutitle option 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.