[PATCH v5 3/4] [gdb] Add defaulted_query_auto_answers_p

Tom de Vries
2026-03-14T17:40:17Z

Factor out "static bool defaulted_query_auto_answers_p (bool)" out of
defaulted_query, and use it to implement
"extern tribool defaulted_query_auto_answers_p (void)".
---
gdb/utils.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-----
gdb/utils.h | 4 ++++
2 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/gdb/utils.c b/gdb/utils.c
index ef8ef87bc5b..0de356eac80 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -742,6 +742,54 @@ class scoped_input_handler
};

\f
+/* Return true if defaulted queries should be answered:
+ - if VERBOSELY, automatically and verbosely
+ - otherwise, automatically and quietly. */
+
+static bool
+defaulted_query_auto_answers_p (bool verbosely)
+{
+ if (verbosely)
+ {
+ /* Automatically answer verbosely if input isn't coming from the user
+ directly. */
+ return (current_ui->instream != current_ui->stdin_stream
+ || !current_ui->input_interactive_p ()
+ /* Restrict queries to the main UI. */
+ || current_ui != main_ui);
+ }
+ else
+ {
+ /* Automatically answer quietly if:
+ - the user did not want prompts, or
+ - the command was issued with the server prefix. */
+ return !confirm || server_command;
+ }
+}
+
+/* See utils.h. */
+
+tribool
+defaulted_query_auto_answers_p ()
+{
+ if (defaulted_query_auto_answers_p (false)
+ || defaulted_query_auto_answers_p (true))
+ return TRIBOOL_TRUE;
+
+ if (deprecated_query_hook)
+ {
+ /* The only user of deprecated_query_hook in core gdb seems to be
+ mi_interp_query_hook, for which we could return TRIBOOL_TRUE here.
+ If that turns out to be required, we could accomplish this by
+ changing the hook interface to say:
+ int (*deprecated_query_hook) (const char *, va_list,
+ bool auto_answers_p = false)
+ and calling it here with auto_answers_p == true. */
+ return TRIBOOL_UNKNOWN;
+ }
+
+ return TRIBOOL_FALSE;
+}

/* This function supports the query, nquery, and yquery functions.
Ask user a y-or-n question and return 0 if answer is no, 1 if
@@ -789,17 +837,14 @@ defaulted_query (const char *ctlstr, const char defchar, va_list args)

/* Automatically answer the default value if the user did not want
prompts or the command was issued with the server prefix. */
- if (!confirm || server_command)
+ if (defaulted_query_auto_answers_p (false))
return def_value;

/* If input isn't coming from the user directly, just say what
question we're asking, and then answer the default automatically. This
way, important error messages don't get lost when talking to GDB
over a pipe. */
- if (current_ui->instream != current_ui->stdin_stream
- || !current_ui->input_interactive_p ()
- /* Restrict queries to the main UI. */
- || current_ui != main_ui)
+ if (defaulted_query_auto_answers_p (true))
{
target_terminal::scoped_restore_terminal_state term_state;
target_terminal::ours_for_output ();
diff --git a/gdb/utils.h b/gdb/utils.h
index 7cab021e7a9..6ef2049b2a4 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -515,4 +515,8 @@ struct deferred_warnings final : public warning_hook_handler_type
std::vector<string_file> m_warnings;
};

+/* Whether defaulted queries are answered automatically. */
+
+extern tribool defaulted_query_auto_answers_p ();
+
#endif /* GDB_UTILS_H */
--
2.51.0