+ /* New completion starting. */
+ current_completion.aborted = false;
+
+ try
+ {
+ return gdb_completion_word_break_characters_throw ();
+ }
+ catch (const gdb_exception &ex)
+ {
+ /* Set this to that gdb_rl_attempted_completion_function knows
+ to abort early. */
+ current_completion.aborted = true;
+ }
+
+ return NULL;
+}
+
+/* See completer.h. */
+
+const char *
+completion_find_completion_word (completion_tracker &tracker, const char *text,
+ int *quote_char)
+{
+ size_t point = strlen (text);
+
+ complete_line_internal (tracker, NULL, text, point, handle_brkchars);
+
+ if (tracker.use_custom_word_point ())
+ {
+ gdb_assert (tracker.custom_word_point () > 0);
+ *quote_char = tracker.quote_char ();
+ return text + tracker.custom_word_point ();
+ }
+
+ gdb_rl_completion_word_info info;
+
+ info.word_break_characters = rl_completer_word_break_characters;
+ info.quote_characters = gdb_completer_quote_characters;
+ info.basic_quote_characters = rl_basic_quote_characters;
+
+ return gdb_rl_find_completion_word (&info, quote_char, NULL, text);
+}
+
+/* See completer.h. */
+
+void
+completion_tracker::recompute_lowest_common_denominator
+ (gdb::unique_xmalloc_ptr<char> &&new_match_up)
+{
+ if (m_lowest_common_denominator == NULL)
+ {
+ /* We don't have a lowest common denominator yet, so simply take
+ the whole NEW_MATCH_UP as being it. */
+ m_lowest_common_denominator = new_match_up.release ();
+ m_lowest_common_denominator_unique = true;
+ }
+ else
+ {
+ /* Find the common denominator between the currently-known
+ lowest common denominator and NEW_MATCH_UP. That becomes the
+ new lowest common denominator. */
+ size_t i;
+ const char *new_match = new_match_up.get ();
+
+ for (i = 0;
+ (new_match[i] != '\0'
+ && new_match[i] == m_lowest_common_denominator[i]);
+ i++)
+ ;
+ if (m_lowest_common_denominator[i] != new_match[i])
+ {
+ m_lowest_common_denominator[i] = '\0';
+ m_lowest_common_denominator_unique = false;
+ }
+ }
+}
+
+/* See completer.h. */
+
+void
+completion_tracker::advance_custom_word_point_by (int len)
+{
+ m_custom_word_point += len;
+}
+
+/* Build a new C string that is a copy of LCD with the whitespace of
+ ORIG/ORIG_LEN preserved.
+
+ Say the user is completing a symbol name, with spaces, like:
+
+ "foo ( i"
+
+ and the resulting completion match is:
+
+ "foo(int)"
+
+ we want to end up with an input line like:
+
+ "foo ( int)"
+ ^^^^^^^ => text from LCD [1], whitespace from ORIG preserved.
+ ^^ => new text from LCD
+
+ [1] - We must take characters from the LCD instead of the original
+ text, since some completions want to change upper/lowercase. E.g.:
+
+ "handle sig<>"
+
+ completes to:
+
+ "handle SIG[QUIT|etc.]"
+*/
+
+static char *
+expand_preserving_ws (const char *orig, size_t orig_len,
+ const char *lcd)
+{
+ const char *p_orig = orig;
+ const char *orig_end = orig + orig_len;
+ const char *p_lcd = lcd;
+ std::string res;
+
+ while (p_orig < orig_end)
+ {
+ if (*p_orig == ' ')
+ {
+ while (p_orig < orig_end && *p_orig == ' ')
+ res += *p_orig++;
+ p_lcd = skip_spaces (p_lcd);
+ }
+ else
+ {
+ /* Take characters from the LCD instead of the original
+ text, since some completions change upper/lowercase.
+ E.g.:
+ "handle sig<>"
+ completes to:
+ "handle SIG[QUIT|etc.]"
+ */
+ res += *p_lcd;
+ p_orig++;
+ p_lcd++;
+ }
+ }
+
+ while (*p_lcd != '\0')
+ res += *p_lcd++;
+
+ return xstrdup (res.c_str ());
+}
+
+/* See completer.h. */
+
+completion_result
+completion_tracker::build_completion_result (const char *text,
+ int start, int end)
+{
+ completion_list &list = m_entries_vec; /* The completions. */
+
+ if (list.empty ())
+ return {};
+
+ /* +1 for the LCD, and +1 for NULL termination. */
+ char **match_list = XNEWVEC (char *, 1 + list.size () + 1);
+
+ /* Build replacement word, based on the LCD. */
+
+ match_list[0]
+ = expand_preserving_ws (text, end - start,
+ m_lowest_common_denominator);
+
+ if (m_lowest_common_denominator_unique)
+ {
+ /* We don't rely on readline appending the quote char as
+ delimiter as then readline wouldn't append the ' ' after the
+ completion. */
+ char buf[2] = { (char) quote_char () };
+
+ match_list[0] = reconcat (match_list[0], match_list[0],
+ buf, (char *) NULL);
+ match_list[1] = NULL;
+
+ /* If the tracker wants to, or we already have a space at the
+ end of the match, tell readline to skip appending
+ another. */
+ bool completion_suppress_append
+ = (suppress_append_ws ()
+ || match_list[0][strlen (match_list[0]) - 1] == ' ');
+
+ return completion_result (match_list, 1, completion_suppress_append);
+ }
+ else
+ {
+ int ix;
+
+ for (ix = 0; ix < list.size (); ++ix)
+ match_list[ix + 1] = list[ix].release ();
+ match_list[ix + 1] = NULL;
+
+ return completion_result (match_list, list.size (), false);
+ }
+}
+
+/* See completer.h */
+
+completion_result::completion_result ()
+ : match_list (NULL), number_matches (0),
+ completion_suppress_append (false)
+{}
+
+/* See completer.h */
+
+completion_result::completion_result (char **match_list_,
+ size_t number_matches_,
+ bool completion_suppress_append_)
+ : match_list (match_list_),
+ number_matches (number_matches_),
+ completion_suppress_append (completion_suppress_append_)
+{}
+
+/* See completer.h */
+
+completion_result::~completion_result ()
+{
+ reset_match_list ();
+}
+
+/* See completer.h */
+
+completion_result::completion_result (completion_result &&rhs)
+{
+ if (this == &rhs)
+ return;
+
+ reset_match_list ();
+ match_list = rhs.match_list;
+ rhs.match_list = NULL;
+ number_matches = rhs.number_matches;
+ rhs.number_matches = 0;
+}
+
+/* See completer.h */
+
+char **
+completion_result::release_match_list ()
+{
+ char **ret = match_list;
+ match_list = NULL;
+ return ret;
+}
+
+/* See completer.h */
+
+void
+completion_result::sort_match_list ()
+{
+ if (number_matches > 1)
+ {
+ /* Element 0 is special (it's the common prefix), leave it
+ be. */
+ std::sort (&match_list[1],
+ &match_list[number_matches + 1],
+ compare_cstrings);
+ }
+}
+
+/* See completer.h */
+
+void
+completion_result::reset_match_list ()
+{
+ if (match_list != NULL)
+ {
+ for (char **p = match_list; *p != NULL; p++)
+ xfree (*p);
+ xfree (match_list);
+ match_list = NULL;
+ }
+}
+
+/* Helper for gdb_rl_attempted_completion_function, which does most of
+ the work. This is called by readline to build the match list array
+ and to determine the lowest common denominator. The real matches
+ list starts at match[1], while match[0] is the slot holding
+ readline's idea of the lowest common denominator of all matches,
+ which is what readline replaces the completion "word" with.
+
+ TEXT is the caller's idea of the "word" we are looking at, as
+ computed in the handle_brkchars phase.
+
+ START is the offset from RL_LINE_BUFFER where TEXT starts. END is
+ the offset from RL_LINE_BUFFER where TEXT ends (i.e., where
+ rl_point is).
+
+ You should thus pretend that the line ends at END (relative to
+ RL_LINE_BUFFER).
+
+ RL_LINE_BUFFER contains the entire text of the line. RL_POINT is
+ the offset in that line of the cursor. You should pretend that the
+ line ends at POINT.
+
+ Returns NULL if there are no completions. */
+
+static char **
+gdb_rl_attempted_completion_function_throw (const char *text, int start, int end)
+{
+ /* Completers that provide a custom word point in the
+ handle_brkchars phase also compute their completions then.
+ Completers that leave the completion word handling to readline
+ must be called twice. If rl_point (i.e., END) is at column 0,
+ then readline skips the handle_brkchars phase, and so we create a
+ tracker now in that case too. */
+ if (end == 0 || !current_completion.tracker->use_custom_word_point ())
+ {
+ delete current_completion.tracker;
+ current_completion.tracker = new completion_tracker ();
+
+ complete_line (*current_completion.tracker, text,
+ rl_line_buffer, rl_point);
+ }
+
+ completion_tracker &tracker = *current_completion.tracker;
+
+ completion_result result
+ = tracker.build_completion_result (text, start, end);
+
+ rl_completion_suppress_append = result.completion_suppress_append;
+ return result.release_match_list ();
+}
+
+/* Function installed as "rl_attempted_completion_function" readline
+ hook. Wrapper around gdb_rl_attempted_completion_function_throw
+ that catches C++ exceptions, which can't cross readline. */
+
+char **
+gdb_rl_attempted_completion_function (const char *text, int start, int end)
+{
+ /* Restore globals that might have been tweaked in
+ gdb_completion_word_break_characters. */
+ rl_basic_quote_characters = gdb_org_rl_basic_quote_characters;
+
+ /* If we end up returning NULL, either on error, or simple because
+ there are no matches, inhibit readline's default filename
+ completer. */
+ rl_attempted_completion_over = 1;
+
+ /* If the handle_brkchars phase was aborted, don't try
+ completing. */
+ if (current_completion.aborted)
+ return NULL;
+
+ try
+ {
+ return gdb_rl_attempted_completion_function_throw (text, start, end);
+ }
+ catch (const gdb_exception &ex)
+ {
+ }
+
+ return NULL;
+}
+
+/* Skip over the possibly quoted word STR (as defined by the quote
+ characters QUOTECHARS and the word break characters BREAKCHARS).
+ Returns pointer to the location after the "word". If either
+ QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
+ completer. */
+
+const char *
+skip_quoted_chars (const char *str, const char *quotechars,
+ const char *breakchars)
+{
+ char quote_char = '\0';
+ const char *scan;
+
+ if (quotechars == NULL)
+ quotechars = gdb_completer_quote_characters;
+
+ if (breakchars == NULL)
+ breakchars = current_language->la_word_break_characters();
+
+ for (scan = str; *scan != '\0'; scan++)
+ {
+ if (quote_char != '\0')
+ {
+ /* Ignore everything until the matching close quote char. */
+ if (*scan == quote_char)
+ {
+ /* Found matching close quote. */
+ scan++;
+ break;
+ }
+ }
+ else if (strchr (quotechars, *scan))
+ {
+ /* Found start of a quoted string. */
+ quote_char = *scan;
+ }
+ else if (strchr (breakchars, *scan))
+ {
+ break;
+ }
+ }
+
+ return (scan);
+}
+
+/* Skip over the possibly quoted word STR (as defined by the quote
+ characters and word break characters used by the completer).
+ Returns pointer to the location after the "word". */
+
+const char *
+skip_quoted (const char *str)
+{
+ return skip_quoted_chars (str, NULL, NULL);
+}
+
+/* Return a message indicating that the maximum number of completions
+ has been reached and that there may be more. */
+
+const char *
+get_max_completions_reached_message (void)
+{
+ return _("*** List may be truncated, max-completions reached. ***");
+}
+\f
+/* GDB replacement for rl_display_match_list.
+ Readline doesn't provide a clean interface for TUI(curses).
+ A hack previously used was to send readline's rl_outstream through a pipe
+ and read it from the event loop. Bleah. IWBN if readline abstracted
+ away all the necessary bits, and this is what this code does. It
+ replicates the parts of readline we need and then adds an abstraction
+ layer, currently implemented as struct match_list_displayer, so that both
+ CLI and TUI can use it. We copy all this readline code to minimize
+ GDB-specific mods to readline. Once this code performs as desired then
+ we can submit it to the readline maintainers.
+
+ N.B. A lot of the code is the way it is in order to minimize differences
+ from readline's copy. */
+
+/* Not supported here. */
+#undef VISIBLE_STATS
+
+#if defined (HANDLE_MULTIBYTE)
+#define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
+#define MB_NULLWCH(x) ((x) == 0)
+#endif
+
+#define ELLIPSIS_LEN 3
+
+/* gdb version of readline/complete.c:get_y_or_n.
+ 'y' -> returns 1, and 'n' -> returns 0.
+ Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
+ If FOR_PAGER is non-zero, then also supported are:
+ NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
+
+static int
+gdb_get_y_or_n (int for_pager, const struct match_list_displayer *displayer)
+{
+ int c;
+
+ for (;;)
+ {
+ RL_SETSTATE (RL_STATE_MOREINPUT);
+ c = displayer->read_key (displayer);
+ RL_UNSETSTATE (RL_STATE_MOREINPUT);
+
+ if (c == 'y' || c == 'Y' || c == ' ')
+ return 1;
+ if (c == 'n' || c == 'N' || c == RUBOUT)
+ return 0;
+ if (c == ABORT_CHAR || c < 0)
+ {
+ /* Readline doesn't erase_entire_line here, but without it the
+ --More-- prompt isn't erased and neither is the text entered
+ thus far redisplayed. */
+ displayer->erase_entire_line (displayer);
+ /* Note: The arguments to rl_abort are ignored. */
+ rl_abort (0, 0);
+ }
+ if (for_pager && (c == NEWLINE || c == RETURN))
+ return 2;
+ if (for_pager && (c == 'q' || c == 'Q'))
+ return 0;
+ displayer->beep (displayer);
+ }
+}
+
+/* Pager function for tab-completion.
+ This is based on readline/complete.c:_rl_internal_pager.
+ LINES is the number of lines of output displayed thus far.
+ Returns:
+ -1 -> user pressed 'n' or equivalent,
+ 0 -> user pressed 'y' or equivalent,
+ N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
+
+static int
+gdb_display_match_list_pager (int lines,
+ const struct match_list_displayer *displayer)
+{
+ int i;
+
+ displayer->puts (displayer, "--More--");
+ displayer->flush (displayer);
+ i = gdb_get_y_or_n (1, displayer);
+ displayer->erase_entire_line (displayer);
+ if (i == 0)
+ return -1;
+ else if (i == 2)
+ return (lines - 1);
+ else
+ return 0;
+}
+
+/* Return non-zero if FILENAME is a directory.
+ Based on readline/complete.c:path_isdir. */
+
+static int
+gdb_path_isdir (const char *filename)
+{
+ struct stat finfo;
+
+ return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
+}
+
+/* Return the portion of PATHNAME that should be output when listing
+ possible completions. If we are hacking filename completion, we
+ are only interested in the basename, the portion following the
+ final slash. Otherwise, we return what we were passed. Since
+ printing empty strings is not very informative, if we're doing
+ filename completion, and the basename is the empty string, we look
+ for the previous slash and return the portion following that. If
+ there's no previous slash, we just return what we were passed.
+
+ Based on readline/complete.c:printable_part. */
+
+static char *
+gdb_printable_part (char *pathname)
+{
+ char *temp, *x;
+
+ if (rl_filename_completion_desired == 0) /* don't need to do anything */
+ return (pathname);
+
+ temp = strrchr (pathname, '/');
+#if defined (__MSDOS__)
+ if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
+ temp = pathname + 1;
+#endif
+
+ if (temp == 0 || *temp == '\0')
+ return (pathname);
+ /* If the basename is NULL, we might have a pathname like '/usr/src/'.
+ Look for a previous slash and, if one is found, return the portion
+ following that slash. If there's no previous slash, just return the
+ pathname we were passed. */
+ else if (temp[1] == '\0')
+ {
+ for (x = temp - 1; x > pathname; x--)
+ if (*x == '/')
+ break;
+ return ((*x == '/') ? x + 1 : pathname);
+ }
+ else
+ return ++temp;
+}
+
+/* Compute width of STRING when displayed on screen by print_filename.
+ Based on readline/complete.c:fnwidth. */
+
+static int
+gdb_fnwidth (const char *string)
+{
+ int width, pos;
+#if defined (HANDLE_MULTIBYTE)
+ mbstate_t ps;
+ int left, w;
+ size_t clen;
+ wchar_t wc;
+
+ left = strlen (string) + 1;
+ memset (&ps, 0, sizeof (mbstate_t));
+#endif
+
+ width = pos = 0;
+ while (string[pos])
+ {
+ if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
+ {
+ width += 2;
+ pos++;
+ }
+ else
+ {
+#if defined (HANDLE_MULTIBYTE)
+ clen = mbrtowc (&wc, string + pos, left - pos, &ps);
+ if (MB_INVALIDCH (clen))
+ {
+ width++;
+ pos++;
+ memset (&ps, 0, sizeof (mbstate_t));
+ }
+ else if (MB_NULLWCH (clen))
+ break;
+ else
+ {
+ pos += clen;
+ w = wcwidth (wc);
+ width += (w >= 0) ? w : 1;
+ }
+#else
+ width++;
+ pos++;
+#endif
+ }
+ }
+
+ return width;
+}
+
+/* Print TO_PRINT, one matching completion.
+ PREFIX_BYTES is number of common prefix bytes.
+ Based on readline/complete.c:fnprint. */
+
+static int
+gdb_fnprint (const char *to_print, int prefix_bytes,
+ const struct match_list_displayer *displayer)
+{
+ int printed_len, w;
+ const char *s;
+#if defined (HANDLE_MULTIBYTE)
+ mbstate_t ps;
+ const char *end;
+ size_t tlen;
+ int width;
+ wchar_t wc;
+
+ end = to_print + strlen (to_print) + 1;
+ memset (&ps, 0, sizeof (mbstate_t));
+#endif
+
+ printed_len = 0;
+
+ /* Don't print only the ellipsis if the common prefix is one of the
+ possible completions */
+ if (to_print[prefix_bytes] == '\0')
+ prefix_bytes = 0;
+
+ if (prefix_bytes)
+ {
+ char ellipsis;
+
+ ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
+ for (w = 0; w < ELLIPSIS_LEN; w++)
+ displayer->putch (displayer, ellipsis);
+ printed_len = ELLIPSIS_LEN;
+ }
+
+ s = to_print + prefix_bytes;
+ while (*s)
+ {
+ if (CTRL_CHAR (*s))
+ {
+ displayer->putch (displayer, '^');
+ displayer->putch (displayer, UNCTRL (*s));
+ printed_len += 2;
+ s++;
+#if defined (HANDLE_MULTIBYTE)
+ memset (&ps, 0, sizeof (mbstate_t));
+#endif
+ }
+ else if (*s == RUBOUT)
+ {
+ displayer->putch (displayer, '^');
+ displayer->putch (displayer, '?');
+ printed_len += 2;
+ s++;
+#if defined (HANDLE_MULTIBYTE)
+ memset (&ps, 0, sizeof (mbstate_t));
+#endif
+ }
+ else
+ {
+#if defined (HANDLE_MULTIBYTE)
+ tlen = mbrtowc (&wc, s, end - s, &ps);
+ if (MB_INVALIDCH (tlen))
+ {
+ tlen = 1;
+ width = 1;
+ memset (&ps, 0, sizeof (mbstate_t));
+ }
+ else if (MB_NULLWCH (tlen))
+ break;
+ else
+ {
+ w = wcwidth (wc);
+ width = (w >= 0) ? w : 1;
+ }
+ for (w = 0; w < tlen; ++w)
+ displayer->putch (displayer, s[w]);
+ s += tlen;
+ printed_len += width;
+#else
+ displayer->putch (displayer, *s);
+ s++;
+ printed_len++;
+#endif
+ }
+ }
+
+ return printed_len;
+}
+
+/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
+ are using it, check for and output a single character for `special'
+ filenames. Return the number of characters we output.
+ Based on readline/complete.c:print_filename. */
+
+static int
+gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
+ const struct match_list_displayer *displayer)
+{
+ int printed_len, extension_char, slen, tlen;
+ char *s, c, *new_full_pathname;
+ const char *dn;
+ extern int _rl_complete_mark_directories;
+
+ extension_char = 0;
+ printed_len = gdb_fnprint (to_print, prefix_bytes, displayer);
+
+#if defined (VISIBLE_STATS)
+ if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
+#else
+ if (rl_filename_completion_desired && _rl_complete_mark_directories)
+#endif
+ {
+ /* If to_print != full_pathname, to_print is the basename of the
+ path passed. In this case, we try to expand the directory
+ name before checking for the stat character. */
+ if (to_print != full_pathname)
+ {
+ /* Terminate the directory name. */
+ c = to_print[-1];
+ to_print[-1] = '\0';
+
+ /* If setting the last slash in full_pathname to a NUL results in
+ full_pathname being the empty string, we are trying to complete
+ files in the root directory. If we pass a null string to the
+ bash directory completion hook, for example, it will expand it
+ to the current directory. We just want the `/'. */
+ if (full_pathname == 0 || *full_pathname == 0)
+ dn = "/";
+ else if (full_pathname[0] != '/')
+ dn = full_pathname;
+ else if (full_pathname[1] == 0)
+ dn = "//"; /* restore trailing slash to `//' */
+ else if (full_pathname[1] == '/' && full_pathname[2] == 0)
+ dn = "/"; /* don't turn /// into // */
+ else
+ dn = full_pathname;
+ s = tilde_expand (dn);
+ if (rl_directory_completion_hook)
+ (*rl_directory_completion_hook) (&s);
+
+ slen = strlen (s);
+ tlen = strlen (to_print);
+ new_full_pathname = (char *)xmalloc (slen + tlen + 2);
+ strcpy (new_full_pathname, s);
+ if (s[slen - 1] == '/')
+ slen--;
+ else
+ new_full_pathname[slen] = '/';
+ new_full_pathname[slen] = '/';
+ strcpy (new_full_pathname + slen + 1, to_print);
+
+#if defined (VISIBLE_STATS)
+ if (rl_visible_stats)
+ extension_char = stat_char (new_full_pathname);
+ else
+#endif
+ if (gdb_path_isdir (new_full_pathname))
+ extension_char = '/';
+
+ xfree (new_full_pathname);
+ to_print[-1] = c;
+ }
+ else
+ {
+ s = tilde_expand (full_pathname);
+#if defined (VISIBLE_STATS)
+ if (rl_visible_stats)
+ extension_char = stat_char (s);
+ else
+#endif
+ if (gdb_path_isdir (s))
+ extension_char = '/';
+ }
+
+ xfree (s);
+ if (extension_char)
+ {
+ displayer->putch (displayer, extension_char);
+ printed_len++;
+ }
+ }
+
+ return printed_len;
+}
+
+/* GDB version of readline/complete.c:complete_get_screenwidth. */
+
+static int
+gdb_complete_get_screenwidth (const struct match_list_displayer *displayer)
+{
+ /* Readline has other stuff here which it's not clear we need. */
+ return displayer->width;
+}
+
+extern int _rl_completion_prefix_display_length;
+extern int _rl_print_completions_horizontally;
+
+EXTERN_C int _rl_qsort_string_compare (const void *, const void *);
+typedef int QSFUNC (const void *, const void *);
+
+/* GDB version of readline/complete.c:rl_display_match_list.
+ See gdb_display_match_list for a description of MATCHES, LEN, MAX.
+ Returns non-zero if all matches are displayed. */
+
+static int
+gdb_display_match_list_1 (char **matches, int len, int max,
+ const struct match_list_displayer *displayer)
+{
+ int count, limit, printed_len, lines, cols;
+ int i, j, k, l, common_length, sind;
+ char *temp, *t;
+ int page_completions = displayer->height != INT_MAX && pagination_enabled;
+
+ /* Find the length of the prefix common to all items: length as displayed
+ characters (common_length) and as a byte index into the matches (sind) */
+ common_length = sind = 0;
+ if (_rl_completion_prefix_display_length > 0)
+ {
+ t = gdb_printable_part (matches[0]);
+ temp = strrchr (t, '/');
+ common_length = temp ? gdb_fnwidth (temp) : gdb_fnwidth (t);
+ sind = temp ? strlen (temp) : strlen (t);
+
+ if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
+ max -= common_length - ELLIPSIS_LEN;
+ else
+ common_length = sind = 0;
+ }
+
+ /* How many items of MAX length can we fit in the screen window? */
+ cols = gdb_complete_get_screenwidth (displayer);
+ max += 2;
+ limit = cols / max;
+ if (limit != 1 && (limit * max == cols))
+ limit--;
+
+ /* If cols == 0, limit will end up -1 */
+ if (cols < displayer->width && limit < 0)
+ limit = 1;
+
+ /* Avoid a possible floating exception. If max > cols,
+ limit will be 0 and a divide-by-zero fault will result. */
+ if (limit == 0)
+ limit = 1;
+
+ /* How many iterations of the printing loop? */
+ count = (len + (limit - 1)) / limit;
+
+ /* Watch out for special case. If LEN is less than LIMIT, then
+ just do the inner printing loop.
+ 0 < len <= limit implies count = 1. */
+
+ /* Sort the items if they are not already sorted. */
+ if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
+ qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
+
+ displayer->crlf (displayer);
+
+ lines = 0;
+ if (_rl_print_completions_horizontally == 0)
+ {
+ /* Print the sorted items, up-and-down alphabetically, like ls. */
+ for (i = 1; i <= count; i++)
+ {
+ for (j = 0, l = i; j < limit; j++)
+ {
+ if (l > len || matches[l] == 0)
+ break;
+ else
+ {
+ temp = gdb_printable_part (matches[l]);
+ printed_len = gdb_print_filename (temp, matches[l], sind,
+ displayer);
+
+ if (j + 1 < limit)
+ for (k = 0; k < max - printed_len; k++)
+ displayer->putch (displayer, ' ');
+ }
+ l += count;
+ }
+ displayer->crlf (displayer);
+ lines++;
+ if (page_completions && lines >= (displayer->height - 1) && i < count)
+ {
+ lines = gdb_display_match_list_pager (lines, displayer);
+ if (lines < 0)
+ return 0;
+ }
+ }
+ }
+ else
+ {
+ /* Print the sorted items, across alphabetically, like ls -x. */
+ for (i = 1; matches[i]; i++)
+ {
+ temp = gdb_printable_part (matches[i]);
+ printed_len = gdb_print_filename (temp, matches[i], sind, displayer);
+ /* Have we reached the end of this line? */
+ if (matches[i+1])
+ {
+ if (i && (limit > 1) && (i % limit) == 0)
+ {
+ displayer->crlf (displayer);
+ lines++;
+ if (page_completions && lines >= displayer->height - 1)
+ {
+ lines = gdb_display_match_list_pager (lines, displayer);
+ if (lines < 0)
+ return 0;
+ }
+ }
+ else
+ for (k = 0; k < max - printed_len; k++)
+ displayer->putch (displayer, ' ');
+ }
+ }
+ displayer->crlf (displayer);
+ }
+
+ return 1;
+}
+
+/* Utility for displaying completion list matches, used by both CLI and TUI.
+
+ MATCHES is the list of strings, in argv format, LEN is the number of
+ strings in MATCHES, and MAX is the length of the longest string in
+ MATCHES. */
+
+void
+gdb_display_match_list (char **matches, int len, int max,
+ const struct match_list_displayer *displayer)
+{
+ /* Readline will never call this if complete_line returned NULL. */
+ gdb_assert (max_completions != 0);
+
+ /* complete_line will never return more than this. */
+ if (max_completions > 0)
+ gdb_assert (len <= max_completions);
+
+ if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
+ {
+ char msg[100];
+
+ /* We can't use *query here because they wait for <RET> which is
+ wrong here. This follows the readline version as closely as possible
+ for compatibility's sake. See readline/complete.c. */
+
+ displayer->crlf (displayer);
+
+ xsnprintf (msg, sizeof (msg),
+ "Display all %d possibilities? (y or n)", len);
+ displayer->puts (displayer, msg);
+ displayer->flush (displayer);
+
+ if (gdb_get_y_or_n (0, displayer) == 0)
+ {
+ displayer->crlf (displayer);
+ return;
+ }
+ }
+
+ if (gdb_display_match_list_1 (matches, len, max, displayer))
+ {
+ /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
+ if (len == max_completions)
+ {
+ /* The maximum number of completions has been reached. Warn the user
+ that there may be more. */
+ const char *message = get_max_completions_reached_message ();
+
+ displayer->puts (displayer, message);
+ displayer->crlf (displayer);
+ }
+ }
+}
+
+void
+_initialize_completer (void)
+{
+ add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class,
+ &max_completions, _("\
+Set maximum number of completion candidates."), _("\
+Show maximum number of completion candidates."), _("\
+Use this to limit the number of candidates considered\n\
+during completion. Specifying \"unlimited\" or -1\n\
+disables limiting. Note that setting either no limit or\n\
+a very large limit can make completion slow."),
+ NULL, NULL, &setlist, &showlist);