+/* Complete on locations, which might be of two possible forms:
+
+ file:line
+ or
+ symbol+offset
+
+ This is intended to be used in commands that set breakpoints etc. */
+char **
+location_completer (struct cmd_list_element *ignore, char *text, char *word)
+{
+ int n_syms = 0, n_files = 0;
+ char ** fn_list = NULL;
+ char ** list = NULL;
+ char *p;
+ int quote_found = 0;
+ int quoted = *text == '\'' || *text == '"';
+ int quote_char = '\0';
+ char *colon = NULL;
+ char *file_to_match = NULL;
+ char *symbol_start = text;
+ char *orig_text = text;
+ size_t text_len;
+
+ /* Do we have an unquoted colon, as in "break foo.c::bar"? */
+ for (p = text; *p != '\0'; ++p)
+ {
+ if (*p == '\\' && p[1] == '\'')
+ p++;
+ else if (*p == '\'' || *p == '"')
+ {
+ quote_found = *p;
+ quote_char = *p++;
+ while (*p != '\0' && *p != quote_found)
+ {
+ if (*p == '\\' && p[1] == quote_found)
+ p++;
+ p++;
+ }
+
+ if (*p == quote_found)
+ quote_found = 0;
+ else
+ break; /* Hit the end of text. */
+ }
+#if HAVE_DOS_BASED_FILE_SYSTEM
+ /* If we have a DOS-style absolute file name at the beginning of
+ TEXT, and the colon after the drive letter is the only colon
+ we found, pretend the colon is not there. */
+ else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
+ ;
+#endif
+ else if (*p == ':' && !colon)
+ {
+ colon = p;
+ symbol_start = p + 1;
+ }
+ else if (strchr (current_language->la_word_break_characters(), *p))
+ symbol_start = p + 1;
+ }
+
+ if (quoted)
+ text++;
+ text_len = strlen (text);
+
+ /* Where is the file name? */
+ if (colon)
+ {
+ char *s;
+
+ file_to_match = (char *) xmalloc (colon - text + 1);
+ strncpy (file_to_match, text, colon - text + 1);
+ /* Remove trailing colons and quotes from the file name. */
+ for (s = file_to_match + (colon - text);
+ s > file_to_match;
+ s--)
+ if (*s == ':' || *s == quote_char)
+ *s = '\0';
+ }
+ /* If the text includes a colon, they want completion only on a
+ symbol name after the colon. Otherwise, we need to complete on
+ symbols as well as on files. */
+ if (colon)
+ {
+ list = make_file_symbol_completion_list (symbol_start, word,
+ file_to_match);
+ xfree (file_to_match);
+ }
+ else
+ {
+ list = make_symbol_completion_list (symbol_start, word);
+ /* If text includes characters which cannot appear in a file
+ name, they cannot be asking for completion on files. */
+ if (strcspn (text,
+ gdb_completer_file_name_break_characters) == text_len)
+ fn_list = make_source_files_completion_list (text, text);
+ }
+
+ /* How many completions do we have in both lists? */
+ if (fn_list)
+ for ( ; fn_list[n_files]; n_files++)
+ ;
+ if (list)
+ for ( ; list[n_syms]; n_syms++)
+ ;
+
+ /* Make list[] large enough to hold both lists, then catenate
+ fn_list[] onto the end of list[]. */
+ if (n_syms && n_files)
+ {
+ list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
+ memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
+ xfree (fn_list);
+ }
+ else if (n_files)
+ {
+ /* If we only have file names as possible completion, we should
+ bring them in sync with what rl_complete expects. The
+ problem is that if the user types "break /foo/b TAB", and the
+ possible completions are "/foo/bar" and "/foo/baz"
+ rl_complete expects us to return "bar" and "baz", without the
+ leading directories, as possible completions, because `word'
+ starts at the "b". But we ignore the value of `word' when we
+ call make_source_files_completion_list above (because that
+ would not DTRT when the completion results in both symbols
+ and file names), so make_source_files_completion_list returns
+ the full "/foo/bar" and "/foo/baz" strings. This produces
+ wrong results when, e.g., there's only one possible
+ completion, because rl_complete will prepend "/foo/" to each
+ candidate completion. The loop below removes that leading
+ part. */
+ for (n_files = 0; fn_list[n_files]; n_files++)
+ {
+ memmove (fn_list[n_files], fn_list[n_files] + (word - text),
+ strlen (fn_list[n_files]) + 1 - (word - text));
+ }
+ /* Return just the file-name list as the result. */
+ list = fn_list;
+ }
+ else if (!n_syms)
+ {
+ /* No completions at all. As the final resort, try completing
+ on the entire text as a symbol. */
+ list = make_symbol_completion_list (orig_text, word);
+ xfree (fn_list);
+ }
+ else
+ xfree (fn_list);
+
+ return list;
+}
+
+/* Helper for expression_completer which recursively counts the number
+ of named fields and methods in a structure or union type. */
+static int
+count_struct_fields (struct type *type)
+{
+ int i, result = 0;
+
+ CHECK_TYPEDEF (type);
+ for (i = 0; i < TYPE_NFIELDS (type); ++i)
+ {
+ if (i < TYPE_N_BASECLASSES (type))
+ result += count_struct_fields (TYPE_BASECLASS (type, i));
+ else if (TYPE_FIELD_NAME (type, i))
+ ++result;
+ }
+
+ for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
+ {
+ if (TYPE_FN_FIELDLIST_NAME (type, i))
+ ++result;
+ }
+
+ return result;
+}
+
+/* Helper for expression_completer which recursively adds field and
+ method names from TYPE, a struct or union type, to the array
+ OUTPUT. This function assumes that OUTPUT is correctly-sized. */
+static void
+add_struct_fields (struct type *type, int *nextp, char **output,
+ char *fieldname, int namelen)
+{
+ int i;
+ int computed_type_name = 0;
+ char *type_name = NULL;
+
+ CHECK_TYPEDEF (type);
+ for (i = 0; i < TYPE_NFIELDS (type); ++i)
+ {
+ if (i < TYPE_N_BASECLASSES (type))
+ add_struct_fields (TYPE_BASECLASS (type, i), nextp, output,
+ fieldname, namelen);
+ else if (TYPE_FIELD_NAME (type, i)
+ && ! strncmp (TYPE_FIELD_NAME (type, i), fieldname, namelen))
+ {
+ output[*nextp] = xstrdup (TYPE_FIELD_NAME (type, i));
+ ++*nextp;
+ }
+ }
+
+ for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
+ {
+ char *name = TYPE_FN_FIELDLIST_NAME (type, i);
+
+ if (name && ! strncmp (name, fieldname, namelen))
+ {
+ if (!computed_type_name)
+ {
+ type_name = type_name_no_tag (type);
+ computed_type_name = 1;
+ }
+ /* Omit constructors from the completion list. */
+ if (!type_name || strcmp (type_name, name))
+ {
+ output[*nextp] = xstrdup (name);
+ ++*nextp;
+ }
+ }
+ }
+}
+
+/* Complete on expressions. Often this means completing on symbol
+ names, but some language parsers also have support for completing
+ field names. */
+char **
+expression_completer (struct cmd_list_element *ignore, char *text, char *word)
+{
+ struct type *type;
+ char *fieldname, *p;
+
+ /* Perform a tentative parse of the expression, to see whether a
+ field completion is required. */
+ fieldname = NULL;
+ type = parse_field_expression (text, &fieldname);
+ if (fieldname && type)
+ {
+ for (;;)
+ {
+ CHECK_TYPEDEF (type);
+ if (TYPE_CODE (type) != TYPE_CODE_PTR
+ && TYPE_CODE (type) != TYPE_CODE_REF)
+ break;
+ type = TYPE_TARGET_TYPE (type);
+ }
+
+ if (TYPE_CODE (type) == TYPE_CODE_UNION
+ || TYPE_CODE (type) == TYPE_CODE_STRUCT)
+ {
+ int alloc = count_struct_fields (type);
+ int flen = strlen (fieldname);
+ int out = 0;
+ char **result = (char **) xmalloc ((alloc + 1) * sizeof (char *));
+
+ add_struct_fields (type, &out, result, fieldname, flen);
+ result[out] = NULL;
+ xfree (fieldname);
+ return result;
+ }
+ }
+ xfree (fieldname);
+
+ /* Commands which complete on locations want to see the entire
+ argument. */
+ for (p = word;
+ p > text && p[-1] != ' ' && p[-1] != '\t';
+ p--)
+ ;
+
+ /* Not ideal but it is what we used to do before... */
+ return location_completer (ignore, p, word);
+}
+