Add support for disassembling WebAssembly opcodes.
[deliverable/binutils-gdb.git] / gdb / completer.c
index 59374393e1d03cdddf9b32e7b48c28ba16575c0b..7e46416b1c7eef243a5b894be46e9befcae63074 100644 (file)
@@ -1,5 +1,5 @@
 /* Line completion stuff for GDB, the GNU debugger.
-   Copyright (C) 2000-2015 Free Software Foundation, Inc.
+   Copyright (C) 2000-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -26,6 +26,8 @@
 #include "target.h"
 #include "reggroups.h"
 #include "user-regs.h"
+#include "arch-utils.h"
+#include "location.h"
 
 #include "cli/cli-decode.h"
 
 
 #include "completer.h"
 
+/* An enumeration of the various things a user might
+   attempt to complete for a location.  */
+
+enum explicit_location_match_type
+{
+    /* The filename of a source file.  */
+    MATCH_SOURCE,
+
+    /* The name of a function or method.  */
+    MATCH_FUNCTION,
+
+    /* The name of a label.  */
+    MATCH_LABEL
+};
+
 /* Prototypes for local functions.  */
 static
 char *line_completion_function (const char *text, int matches, 
@@ -67,29 +84,30 @@ char *line_completion_function (const char *text, int matches,
    readline library sees one in any of the current completion strings,
    it thinks that the string needs to be quoted and automatically
    supplies a leading quote.  */
-static char *gdb_completer_command_word_break_characters =
+static const char gdb_completer_command_word_break_characters[] =
 " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
 
 /* When completing on file names, we remove from the list of word
    break characters any characters that are commonly used in file
    names, such as '-', '+', '~', etc.  Otherwise, readline displays
    incorrect completion candidates.  */
-#ifdef HAVE_DOS_BASED_FILE_SYSTEM
 /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
    programs support @foo style response files.  */
-static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
+static const char gdb_completer_file_name_break_characters[] =
+#ifdef HAVE_DOS_BASED_FILE_SYSTEM
+  " \t\n*|\"';?><@";
 #else
-static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
+  " \t\n*|\"';:?><";
 #endif
 
 /* Characters that can be used to quote completion strings.  Note that
    we can't include '"' because the gdb C parser treats such quoted
    sequences as strings.  */
-static char *gdb_completer_quote_characters = "'";
+static const char gdb_completer_quote_characters[] = "'";
 \f
 /* Accessor for some completer data that may interest other files.  */
 
-char *
+const char *
 get_gdb_completer_quote_characters (void)
 {
   return gdb_completer_quote_characters;
@@ -148,14 +166,14 @@ filename_completer (struct cmd_list_element *ignore,
       else if (word > text)
        {
          /* Return some portion of p.  */
-         q = xmalloc (strlen (p) + 5);
+         q = (char *) xmalloc (strlen (p) + 5);
          strcpy (q, p + (word - text));
          xfree (p);
        }
       else
        {
          /* Return some of TEXT plus p.  */
-         q = xmalloc (strlen (p) + (text - word) + 5);
+         q = (char *) xmalloc (strlen (p) + (text - word) + 5);
          strncpy (q, word, text - word);
          q[text - word] = '\0';
          strcat (q, p);
@@ -174,7 +192,7 @@ filename_completer (struct cmd_list_element *ignore,
   return return_val;
 }
 
-/* Complete on locations, which might be of two possible forms:
+/* Complete on linespecs, which might be of two possible forms:
 
        file:line
    or
@@ -183,9 +201,9 @@ filename_completer (struct cmd_list_element *ignore,
    This is intended to be used in commands that set breakpoints
    etc.  */
 
-VEC (char_ptr) *
-location_completer (struct cmd_list_element *ignore, 
-                   const char *text, const char *word)
+static VEC (char_ptr) *
+linespec_location_completer (struct cmd_list_element *ignore,
+                            const char *text, const char *word)
 {
   int n_syms, n_files, ix;
   VEC (char_ptr) *fn_list = NULL;
@@ -247,7 +265,8 @@ location_completer (struct cmd_list_element *ignore,
       char *s;
 
       file_to_match = (char *) xmalloc (colon - text + 1);
-      strncpy (file_to_match, text, colon - text + 1);
+      strncpy (file_to_match, text, colon - text);
+      file_to_match[colon - text] = '\0';
       /* Remove trailing colons and quotes from the file name.  */
       for (s = file_to_match + (colon - text);
           s > file_to_match;
@@ -332,6 +351,184 @@ location_completer (struct cmd_list_element *ignore,
   return list;
 }
 
+/* A helper function to collect explicit location matches for the given
+   LOCATION, which is attempting to match on WORD.  */
+
+static VEC (char_ptr) *
+collect_explicit_location_matches (struct event_location *location,
+                                  enum explicit_location_match_type what,
+                                  const char *word)
+{
+  VEC (char_ptr) *matches = NULL;
+  const struct explicit_location *explicit_loc
+    = get_explicit_location (location);
+
+  switch (what)
+    {
+    case MATCH_SOURCE:
+      {
+       const char *text = (explicit_loc->source_filename == NULL
+                           ? "" : explicit_loc->source_filename);
+
+       matches = make_source_files_completion_list (text, word);
+      }
+      break;
+
+    case MATCH_FUNCTION:
+      {
+       const char *text = (explicit_loc->function_name == NULL
+                           ? "" : explicit_loc->function_name);
+
+       if (explicit_loc->source_filename != NULL)
+         {
+           const char *filename = explicit_loc->source_filename;
+
+           matches = make_file_symbol_completion_list (text, word, filename);
+         }
+       else
+         matches = make_symbol_completion_list (text, word);
+      }
+      break;
+
+    case MATCH_LABEL:
+      /* Not supported.  */
+      break;
+
+    default:
+      gdb_assert_not_reached ("unhandled explicit_location_match_type");
+    }
+
+  return matches;
+}
+
+/* A convenience macro to (safely) back up P to the previous word.  */
+
+static const char *
+backup_text_ptr (const char *p, const char *text)
+{
+  while (p > text && isspace (*p))
+    --p;
+  for (; p > text && !isspace (p[-1]); --p)
+    ;
+
+  return p;
+}
+
+/* A completer function for explicit locations.  This function
+   completes both options ("-source", "-line", etc) and values.  */
+
+static VEC (char_ptr) *
+explicit_location_completer (struct cmd_list_element *ignore,
+                            struct event_location *location,
+                            const char *text, const char *word)
+{
+  const char *p;
+  VEC (char_ptr) *matches = NULL;
+
+  /* Find the beginning of the word.  This is necessary because
+     we need to know if we are completing an option name or value.  We
+     don't get the leading '-' from the completer.  */
+  p = backup_text_ptr (word, text);
+
+  if (*p == '-')
+    {
+      /* Completing on option name.  */
+      static const char *const keywords[] =
+       {
+         "source",
+         "function",
+         "line",
+         "label",
+         NULL
+       };
+
+      /* Skip over the '-'.  */
+      ++p;
+
+      return complete_on_enum (keywords, p, p);
+    }
+  else
+    {
+      /* Completing on value (or unknown).  Get the previous word to see what
+        the user is completing on.  */
+      size_t len, offset;
+      const char *new_word, *end;
+      enum explicit_location_match_type what;
+      struct explicit_location *explicit_loc
+       = get_explicit_location (location);
+
+      /* Backup P to the previous word, which should be the option
+        the user is attempting to complete.  */
+      offset = word - p;
+      end = --p;
+      p = backup_text_ptr (p, text);
+      len = end - p;
+
+      if (strncmp (p, "-source", len) == 0)
+       {
+         what = MATCH_SOURCE;
+         new_word = explicit_loc->source_filename + offset;
+       }
+      else if (strncmp (p, "-function", len) == 0)
+       {
+         what = MATCH_FUNCTION;
+         new_word = explicit_loc->function_name + offset;
+       }
+      else if (strncmp (p, "-label", len) == 0)
+       {
+         what = MATCH_LABEL;
+         new_word = explicit_loc->label_name + offset;
+       }
+      else
+       {
+         /* The user isn't completing on any valid option name,
+            e.g., "break -source foo.c [tab]".  */
+         return NULL;
+       }
+
+      /* If the user hasn't entered a search expression, e.g.,
+        "break -function <TAB><TAB>", new_word will be NULL, but
+        search routines require non-NULL search words.  */
+      if (new_word == NULL)
+       new_word = "";
+
+      /* Now gather matches  */
+      matches = collect_explicit_location_matches (location, what, new_word);
+    }
+
+  return matches;
+}
+
+/* A completer for locations.  */
+
+VEC (char_ptr) *
+location_completer (struct cmd_list_element *ignore,
+                   const char *text, const char *word)
+{
+  VEC (char_ptr) *matches = NULL;
+  const char *copy = text;
+  struct event_location *location;
+
+  location = string_to_explicit_location (&copy, current_language, 1);
+  if (location != NULL)
+    {
+      struct cleanup *cleanup;
+
+      cleanup = make_cleanup_delete_event_location (location);
+      matches = explicit_location_completer (ignore, location, text, word);
+      do_cleanups (cleanup);
+    }
+  else
+    {
+      /* This is an address or linespec location.
+        Right now both of these are handled by the (old) linespec
+        completer.  */
+      matches = linespec_location_completer (ignore, text, word);
+    }
+
+  return matches;
+}
+
 /* Helper for expression_completer which recursively adds field and
    method names from TYPE, a struct or union type, to the array
    OUTPUT.  */
@@ -343,7 +540,7 @@ add_struct_fields (struct type *type, VEC (char_ptr) **output,
   int computed_type_name = 0;
   const char *type_name = NULL;
 
-  CHECK_TYPEDEF (type);
+  type = check_typedef (type);
   for (i = 0; i < TYPE_NFIELDS (type); ++i)
     {
       if (i < TYPE_N_BASECLASSES (type))
@@ -395,25 +592,27 @@ expression_completer (struct cmd_list_element *ignore,
   struct type *type = NULL;
   char *fieldname;
   const char *p;
-  volatile struct gdb_exception except;
   enum type_code code = TYPE_CODE_UNDEF;
 
   /* Perform a tentative parse of the expression, to see whether a
      field completion is required.  */
   fieldname = NULL;
-  TRY_CATCH (except, RETURN_MASK_ERROR)
+  TRY
     {
       type = parse_expression_for_completion (text, &fieldname, &code);
     }
-  if (except.reason < 0)
-    return NULL;
+  CATCH (except, RETURN_MASK_ERROR)
+    {
+      return NULL;
+    }
+  END_CATCH
+
   if (fieldname && type)
     {
       for (;;)
        {
-         CHECK_TYPEDEF (type);
-         if (TYPE_CODE (type) != TYPE_CODE_PTR
-             && TYPE_CODE (type) != TYPE_CODE_REF)
+         type = check_typedef (type);
+         if (TYPE_CODE (type) != TYPE_CODE_PTR && !TYPE_IS_REFERENCE (type))
            break;
          type = TYPE_TARGET_TYPE (type);
        }
@@ -453,17 +652,27 @@ expression_completer (struct cmd_list_element *ignore,
 
 /* See definition in completer.h.  */
 
+void
+set_rl_completer_word_break_characters (const char *break_chars)
+{
+  rl_completer_word_break_characters = (char *) break_chars;
+}
+
+/* See definition in completer.h.  */
+
 void
 set_gdb_completion_word_break_characters (completer_ftype *fn)
 {
+  const char *break_chars;
+
   /* So far we are only interested in differentiating filename
      completers from everything else.  */
   if (fn == filename_completer)
-    rl_completer_word_break_characters
-      = gdb_completer_file_name_break_characters;
+    break_chars = gdb_completer_file_name_break_characters;
   else
-    rl_completer_word_break_characters
-      = gdb_completer_command_word_break_characters;
+    break_chars = gdb_completer_command_word_break_characters;
+
+  set_rl_completer_word_break_characters (break_chars);
 }
 
 /* Here are some useful test cases for completion.  FIXME: These
@@ -545,8 +754,8 @@ complete_line_internal (const char *text,
      then we will switch to the special word break set for command
      strings, which leaves out the '-' character used in some
      commands.  */
-  rl_completer_word_break_characters =
-    current_language->la_word_break_characters();
+  set_rl_completer_word_break_characters
+    (current_language->la_word_break_characters());
 
   /* Decide whether to complete on a list of gdb commands or on
      symbols.  */
@@ -623,8 +832,8 @@ complete_line_internal (const char *text,
            }
          /* Ensure that readline does the right thing with respect to
             inserting quotes.  */
-         rl_completer_word_break_characters =
-           gdb_completer_command_word_break_characters;
+         set_rl_completer_word_break_characters
+           (gdb_completer_command_word_break_characters);
        }
     }
   else
@@ -650,8 +859,8 @@ complete_line_internal (const char *text,
 
                  /* Ensure that readline does the right thing
                     with respect to inserting quotes.  */
-                 rl_completer_word_break_characters =
-                   gdb_completer_command_word_break_characters;
+                 set_rl_completer_word_break_characters
+                   (gdb_completer_command_word_break_characters);
                }
              else if (reason == handle_help)
                list = NULL;
@@ -659,8 +868,8 @@ complete_line_internal (const char *text,
                {
                  if (reason != handle_brkchars)
                    list = complete_on_enum (c->enums, p, word);
-                 rl_completer_word_break_characters =
-                   gdb_completer_command_word_break_characters;
+                 set_rl_completer_word_break_characters
+                   (gdb_completer_command_word_break_characters);
                }
              else
                {
@@ -681,18 +890,8 @@ complete_line_internal (const char *text,
                             && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
                           p--)
                        ;
-                     rl_completer_word_break_characters =
-                       gdb_completer_file_name_break_characters;
-                   }
-                 else if (c->completer == location_completer)
-                   {
-                     /* Commands which complete on locations want to
-                        see the entire argument.  */
-                     for (p = word;
-                          p > tmp_command
-                            && p[-1] != ' ' && p[-1] != '\t';
-                          p--)
-                       ;
+                     set_rl_completer_word_break_characters
+                       (gdb_completer_file_name_break_characters);
                    }
                  if (reason == handle_brkchars
                      && c->completer_handle_brkchars != NULL)
@@ -725,8 +924,8 @@ complete_line_internal (const char *text,
 
              /* Ensure that readline does the right thing
                 with respect to inserting quotes.  */
-             rl_completer_word_break_characters =
-               gdb_completer_command_word_break_characters;
+             set_rl_completer_word_break_characters
+               (gdb_completer_command_word_break_characters);
            }
        }
       else if (reason == handle_help)
@@ -759,16 +958,8 @@ complete_line_internal (const char *text,
                                    p[-1]) == NULL;
                       p--)
                    ;
-                 rl_completer_word_break_characters =
-                   gdb_completer_file_name_break_characters;
-               }
-             else if (c->completer == location_completer)
-               {
-                 for (p = word;
-                      p > tmp_command
-                        && p[-1] != ' ' && p[-1] != '\t';
-                      p--)
-                   ;
+                 set_rl_completer_word_break_characters
+                   (gdb_completer_file_name_break_characters);
                }
              if (reason == handle_brkchars
                  && c->completer_handle_brkchars != NULL)
@@ -781,9 +972,86 @@ complete_line_internal (const char *text,
 
   return list;
 }
-/* Generate completions all at once.  Returns a vector of strings.
-   Each element is allocated with xmalloc.  It can also return NULL if
-   there are no completions.
+
+/* See completer.h.  */
+
+int max_completions = 200;
+
+/* See completer.h.  */
+
+completion_tracker_t
+new_completion_tracker (void)
+{
+  if (max_completions <= 0)
+    return NULL;
+
+  return htab_create_alloc (max_completions,
+                           htab_hash_string, (htab_eq) streq,
+                           NULL, xcalloc, xfree);
+}
+
+/* Cleanup routine to free a completion tracker and reset the pointer
+   to NULL.  */
+
+static void
+free_completion_tracker (void *p)
+{
+  completion_tracker_t *tracker_ptr = (completion_tracker_t *) p;
+
+  htab_delete (*tracker_ptr);
+  *tracker_ptr = NULL;
+}
+
+/* See completer.h.  */
+
+struct cleanup *
+make_cleanup_free_completion_tracker (completion_tracker_t *tracker_ptr)
+{
+  if (*tracker_ptr == NULL)
+    return make_cleanup (null_cleanup, NULL);
+
+  return make_cleanup (free_completion_tracker, tracker_ptr);
+}
+
+/* See completer.h.  */
+
+enum maybe_add_completion_enum
+maybe_add_completion (completion_tracker_t tracker, char *name)
+{
+  void **slot;
+
+  if (max_completions < 0)
+    return MAYBE_ADD_COMPLETION_OK;
+  if (max_completions == 0)
+    return MAYBE_ADD_COMPLETION_MAX_REACHED;
+
+  gdb_assert (tracker != NULL);
+
+  if (htab_elements (tracker) >= max_completions)
+    return MAYBE_ADD_COMPLETION_MAX_REACHED;
+
+  slot = htab_find_slot (tracker, name, INSERT);
+
+  if (*slot != HTAB_EMPTY_ENTRY)
+    return MAYBE_ADD_COMPLETION_DUPLICATE;
+
+  *slot = name;
+
+  return (htab_elements (tracker) < max_completions
+         ? MAYBE_ADD_COMPLETION_OK
+         : MAYBE_ADD_COMPLETION_OK_MAX_REACHED);
+}
+
+void
+throw_max_completions_reached_error (void)
+{
+  throw_error (MAX_COMPLETIONS_REACHED_ERROR, _("Max completions reached."));
+}
+
+/* Generate completions all at once.  Returns a vector of unique strings
+   allocated with xmalloc.  Returns NULL if there are no completions
+   or if max_completions is 0.  If max_completions is non-negative, this will
+   return at most max_completions strings.
 
    TEXT is the caller's idea of the "word" we are looking at.
 
@@ -796,8 +1064,58 @@ complete_line_internal (const char *text,
 VEC (char_ptr) *
 complete_line (const char *text, const char *line_buffer, int point)
 {
-  return complete_line_internal (text, line_buffer, 
-                                point, handle_completions);
+  VEC (char_ptr) *list;
+  VEC (char_ptr) *result = NULL;
+  struct cleanup *cleanups;
+  completion_tracker_t tracker;
+  char *candidate;
+  int ix, max_reached;
+
+  if (max_completions == 0)
+    return NULL;
+  list = complete_line_internal (text, line_buffer, point,
+                                handle_completions);
+  if (max_completions < 0)
+    return list;
+
+  tracker = new_completion_tracker ();
+  cleanups = make_cleanup_free_completion_tracker (&tracker);
+  make_cleanup_free_char_ptr_vec (list);
+
+  /* Do a final test for too many completions.  Individual completers may
+     do some of this, but are not required to.  Duplicates are also removed
+     here.  Otherwise the user is left scratching his/her head: readline and
+     complete_command will remove duplicates, and if removal of duplicates
+     there brings the total under max_completions the user may think gdb quit
+     searching too early.  */
+
+  for (ix = 0, max_reached = 0;
+       !max_reached && VEC_iterate (char_ptr, list, ix, candidate);
+       ++ix)
+    {
+      enum maybe_add_completion_enum add_status;
+
+      add_status = maybe_add_completion (tracker, candidate);
+
+      switch (add_status)
+       {
+         case MAYBE_ADD_COMPLETION_OK:
+           VEC_safe_push (char_ptr, result, xstrdup (candidate));
+           break;
+         case MAYBE_ADD_COMPLETION_OK_MAX_REACHED:
+           VEC_safe_push (char_ptr, result, xstrdup (candidate));
+           max_reached = 1;
+           break;
+         case MAYBE_ADD_COMPLETION_MAX_REACHED:
+           gdb_assert_not_reached ("more than max completions reached");
+         case MAYBE_ADD_COMPLETION_DUPLICATE:
+           break;
+       }
+    }
+
+  do_cleanups (cleanups);
+
+  return result;
 }
 
 /* Complete on command names.  Used by "help".  */
@@ -817,7 +1135,7 @@ signal_completer (struct cmd_list_element *ignore,
 {
   VEC (char_ptr) *return_val = NULL;
   size_t len = strlen (word);
-  enum gdb_signal signum;
+  int signum;
   const char *signame;
 
   for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
@@ -826,7 +1144,7 @@ signal_completer (struct cmd_list_element *ignore,
       if (signum == GDB_SIGNAL_0)
        continue;
 
-      signame = gdb_signal_to_name (signum);
+      signame = gdb_signal_to_name ((enum gdb_signal) signum);
 
       /* Ignore the unknown signal case.  */
       if (!signame || strcmp (signame, "?") == 0)
@@ -839,44 +1157,83 @@ signal_completer (struct cmd_list_element *ignore,
   return return_val;
 }
 
-/* Complete on a register or reggroup.  */
+/* Bit-flags for selecting what the register and/or register-group
+   completer should complete on.  */
 
-VEC (char_ptr) *
-reg_or_group_completer (struct cmd_list_element *ignore,
-                       const char *text, const char *word)
+enum reg_completer_target
+  {
+    complete_register_names = 0x1,
+    complete_reggroup_names = 0x2
+  };
+DEF_ENUM_FLAGS_TYPE (enum reg_completer_target, reg_completer_targets);
+
+/* Complete register names and/or reggroup names based on the value passed
+   in TARGETS.  At least one bit in TARGETS must be set.  */
+
+static VEC (char_ptr) *
+reg_or_group_completer_1 (struct cmd_list_element *ignore,
+                         const char *text, const char *word,
+                         reg_completer_targets targets)
 {
   VEC (char_ptr) *result = NULL;
   size_t len = strlen (word);
   struct gdbarch *gdbarch;
-  struct reggroup *group;
   const char *name;
-  int i;
 
-  if (!target_has_registers)
-    return result;
+  gdb_assert ((targets & (complete_register_names
+                         | complete_reggroup_names)) != 0);
+  gdbarch = get_current_arch ();
 
-  gdbarch = get_frame_arch (get_selected_frame (NULL));
-
-  for (i = 0;
-       (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL;
-       i++)
+  if ((targets & complete_register_names) != 0)
     {
-      if (*name != '\0' && strncmp (word, name, len) == 0)
-       VEC_safe_push (char_ptr, result, xstrdup (name));
+      int i;
+
+      for (i = 0;
+          (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL;
+          i++)
+       {
+         if (*name != '\0' && strncmp (word, name, len) == 0)
+           VEC_safe_push (char_ptr, result, xstrdup (name));
+       }
     }
 
-  for (group = reggroup_next (gdbarch, NULL);
-       group != NULL;
-       group = reggroup_next (gdbarch, group))
+  if ((targets & complete_reggroup_names) != 0)
     {
-      name = reggroup_name (group);
-      if (strncmp (word, name, len) == 0)
-       VEC_safe_push (char_ptr, result, xstrdup (name));
+      struct reggroup *group;
+
+      for (group = reggroup_next (gdbarch, NULL);
+          group != NULL;
+          group = reggroup_next (gdbarch, group))
+       {
+         name = reggroup_name (group);
+         if (strncmp (word, name, len) == 0)
+           VEC_safe_push (char_ptr, result, xstrdup (name));
+       }
     }
 
   return result;
 }
 
+/* Perform completion on register and reggroup names.  */
+
+VEC (char_ptr) *
+reg_or_group_completer (struct cmd_list_element *ignore,
+                       const char *text, const char *word)
+{
+  return reg_or_group_completer_1 (ignore, text, word,
+                                  (complete_register_names
+                                   | complete_reggroup_names));
+}
+
+/* Perform completion on reggroup names.  */
+
+VEC (char_ptr) *
+reggroup_completer (struct cmd_list_element *ignore,
+                   const char *text, const char *word)
+{
+  return reg_or_group_completer_1 (ignore, text, word,
+                                  complete_reggroup_names);
+}
 
 /* Get the list of chars that are considered as word breaks
    for the current command.  */
@@ -1020,6 +1377,15 @@ 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).
@@ -1318,7 +1684,8 @@ 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, *dn;
+  char *s, c, *new_full_pathname;
+  const char *dn;
   extern int _rl_complete_mark_directories;
 
   extension_char = 0;
@@ -1412,10 +1779,17 @@ gdb_complete_get_screenwidth (const struct match_list_displayer *displayer)
   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.  */
+   See gdb_display_match_list for a description of MATCHES, LEN, MAX.
+   Returns non-zero if all matches are displayed.  */
 
-static void
+static int
 gdb_display_match_list_1 (char **matches, int len, int max,
                          const struct match_list_displayer *displayer)
 {
@@ -1423,10 +1797,6 @@ gdb_display_match_list_1 (char **matches, int len, int max,
   int i, j, k, l, common_length, sind;
   char *temp, *t;
   int page_completions = displayer->height != INT_MAX && pagination_enabled;
-  extern int _rl_completion_prefix_display_length;
-  extern int _rl_qsort_string_compare (const void *, const void *);
-  extern int _rl_print_completions_horizontally;
-  typedef int QSFUNC (const void *, const void *);
 
   /* 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) */
@@ -1501,7 +1871,7 @@ gdb_display_match_list_1 (char **matches, int len, int max,
            {
              lines = gdb_display_match_list_pager (lines, displayer);
              if (lines < 0)
-               return;
+               return 0;
            }
        }
     }
@@ -1523,7 +1893,7 @@ gdb_display_match_list_1 (char **matches, int len, int max,
                    {
                      lines = gdb_display_match_list_pager (lines, displayer);
                      if (lines < 0)
-                       return;
+                       return 0;
                    }
                }
              else
@@ -1533,6 +1903,8 @@ gdb_display_match_list_1 (char **matches, int len, int max,
        }
       displayer->crlf (displayer);
     }
+
+  return 1;
 }
 
 /* Utility for displaying completion list matches, used by both CLI and TUI.
@@ -1545,6 +1917,13 @@ 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];
@@ -1567,5 +1946,33 @@ gdb_display_match_list (char **matches, int len, int max,
        }
     }
 
-  gdb_display_match_list_1 (matches, len, max, displayer);
+  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);
+       }
+    }
+}
+\f
+extern initialize_file_ftype _initialize_completer; /* -Wmissing-prototypes */
+
+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);
 }
This page took 0.048417 seconds and 4 git commands to generate.