+/* Print the prefix of C followed by name of C in title style. */
+
+static void
+fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream)
+{
+ std::string prefixname
+ = c.prefix == nullptr ? "" : c.prefix->prefixname ();
+
+ fprintf_styled (stream, title_style.style (), "%s%s",
+ prefixname.c_str (), c.name);
+}
+
+/* True if ALIAS has a user-defined documentation. */
+
+static bool
+user_documented_alias (const cmd_list_element &alias)
+{
+ gdb_assert (alias.is_alias ());
+ /* Alias is user documented if it has an allocated documentation
+ that differs from the aliased command. */
+ return (alias.doc_allocated
+ && strcmp (alias.doc, alias.alias_target->doc) != 0);
+}
+
+/* Print the definition of alias C using title style for alias
+ and aliased command. */
+
+static void
+fput_alias_definition_styled (const cmd_list_element &c,
+ struct ui_file *stream)
+{
+ gdb_assert (c.is_alias ());
+ gdb_puts (" alias ", stream);
+ fput_command_name_styled (c, stream);
+ gdb_printf (stream, " = ");
+ fput_command_name_styled (*c.alias_target, stream);
+ gdb_printf (stream, " %s\n", c.default_args.c_str ());
+}
+
+/* Print the definition of CMD aliases not deprecated and having default args
+ and not specifically documented by the user. */
+
+static void
+fput_aliases_definition_styled (const cmd_list_element &cmd,
+ struct ui_file *stream)
+{
+ for (const cmd_list_element &alias : cmd.aliases)
+ if (!alias.cmd_deprecated
+ && !user_documented_alias (alias)
+ && !alias.default_args.empty ())
+ fput_alias_definition_styled (alias, stream);
+}
+
+/* If C has one or more aliases, style print the name of C and the name of its
+ aliases not documented specifically by the user, separated by commas.
+ If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
+ If one or more names are printed, POSTFIX is printed after the last name.
+*/
+
+static void
+fput_command_names_styled (const cmd_list_element &c,
+ bool always_fput_c_name, const char *postfix,
+ struct ui_file *stream)
+{
+ /* First, check if we are going to print something. That is, either if
+ ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated
+ alias not documented specifically by the user. */
+
+ auto print_alias = [] (const cmd_list_element &alias)
+ {
+ return !alias.cmd_deprecated && !user_documented_alias (alias);
+ };
+
+ bool print_something = always_fput_c_name;
+ if (!print_something)
+ for (const cmd_list_element &alias : c.aliases)
+ {
+ if (!print_alias (alias))
+ continue;
+
+ print_something = true;
+ break;
+ }
+
+ if (print_something)
+ fput_command_name_styled (c, stream);
+
+ for (const cmd_list_element &alias : c.aliases)
+ {
+ if (!print_alias (alias))
+ continue;
+
+ gdb_puts (", ", stream);
+ stream->wrap_here (3);
+ fput_command_name_styled (alias, stream);
+ }
+
+ if (print_something)
+ gdb_puts (postfix, stream);
+}
+
+/* If VERBOSE, print the full help for command C and highlight the
+ documentation parts matching HIGHLIGHT,
+ otherwise print only one-line help for command C. */
+
+static void
+print_doc_of_command (const cmd_list_element &c, const char *prefix,
+ bool verbose, compiled_regex &highlight,
+ struct ui_file *stream)
+{
+ /* When printing the full documentation, add a line to separate
+ this documentation from the previous command help, in the likely
+ case that apropos finds several commands. */
+ if (verbose)
+ gdb_puts ("\n", stream);
+
+ fput_command_names_styled (c, true,
+ verbose ? "" : " -- ", stream);
+ if (verbose)
+ {
+ gdb_puts ("\n", stream);
+ fput_aliases_definition_styled (c, stream);
+ fputs_highlighted (c.doc, highlight, stream);
+ gdb_puts ("\n", stream);
+ }
+ else
+ {
+ print_doc_line (stream, c.doc, false);
+ gdb_puts ("\n", stream);
+ fput_aliases_definition_styled (c, stream);
+ }
+}
+