This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC] GDB interpreters


On Tue, 21 May 2002, Keith Seitz wrote:

> Eli Zaretskii wrote:
>
> > Can we have completion on known interpreter names in these commands?  The
> > default completer function completes on symbols from the inferior, which
> > is really not a good idea in this case ;-)
>
> I can certainly look into that...

Ok, I admit, I know very little about the completer. However, I can copy
code as well as the next guy! :-)

How does this look? (This would be a patch for the file I posted
yesterday.)

Keith

*** interpreter.c.orig	Tue May 21 11:35:42 2002
--- interpreter.c	Tue May 21 11:35:46 2002
***************
*** 44,49 ****
--- 44,51 ----
  #include "event-loop.h"
  #include "event-top.h"
  #include "interpreter.h"
+ #include "completer.h"
+ #include "gdb_string.h"

  /* Functions local to this file. */
  static void initialize_interps (void);
*************** static void set_interpreter_cmd (char *a
*** 52,57 ****
--- 54,60 ----
  				 struct cmd_list_element *c);
  static void list_interpreter_cmd (char *args, int from_tty);
  static void do_set_interpreter (int not_an_fd);
+ static char **interpreter_completer (char *text, char *word);

  /* The magic initialization routine for this module. */

*************** interpreter_exec_cmd (char *args, int fr
*** 555,560 ****
--- 558,620 ----
    gdb_interpreter_set_quiet (interp_to_use, old_quiet);
  }

+ /* List the possible interpreters which could complete the given text. */
+
+ static char **
+ interpreter_completer (char *text, char *word)
+ {
+   int alloced, textlen;
+   int num_matches;
+   char **matches;
+   struct gdb_interpreter *interp;
+
+   /* We expect only a very limited number of interpreters, so just
+      allocate room for all of them. */
+   for (interp = interp_list; interp != NULL; interp = interp->next)
+     ++alloced;
+   matches = (char **) xmalloc (alloced * sizeof (char *));
+
+   num_matches = 0;
+   textlen = strlen (text);
+   for (interp = interp_list; interp != NULL; interp = interp->next)
+     {
+       if (strncmp (interp->name, text, textlen) == 0)
+ 	{
+ 	  matches[num_matches] =
+ 	    (char *) xmalloc (strlen (word) + strlen (interp->name) + 1);
+ 	  if (word == text)
+ 	    strcpy (matches[num_matches], interp->name);
+ 	  else if (word > text)
+ 	    {
+ 	      /* Return some portion of interp->name */
+ 	      strcpy (matches[num_matches], interp->name + (word - text));
+ 	    }
+ 	  else
+ 	    {
+ 	      /* Return some of text plus interp->name */
+ 	      strncpy (matches[num_matches], word, text - word);
+ 	      matches[num_matches][text - word] = '\0';
+ 	      strcat (matches[num_matches], interp->name);
+ 	    }
+ 	  ++num_matches;
+ 	}
+     }
+
+   if (num_matches == 0)
+     {
+       xfree (matches);
+       matches = NULL;
+     }
+   else if (num_matches < alloced)
+     {
+       matches = (char **) xrealloc ((char *) matches, ((num_matches + 1)
+ 						       * sizeof (char *)));
+       matches[num_matches] = NULL;
+     }
+
+   return matches;
+ }
+
  /* _initialize_interpreter - This just adds the "set interpreter" and
   * "info interpreters" commands.
   */
*************** _initialize_interpreter (void)
*** 574,582 ****
  	   list_interpreter_cmd,
  	   "List the interpreters currently available in gdb.", &infolist);

!   add_cmd ("interpreter-exec", class_support,
  	   interpreter_exec_cmd,
  	   "Execute a command in an interpreter.  It takes two arguments:\n\
  The first argument is the name of the interpreter to use.\n\
  The second argument is the command to execute.\n", &cmdlist);
  }
--- 634,643 ----
  	   list_interpreter_cmd,
  	   "List the interpreters currently available in gdb.", &infolist);

!   c = add_cmd ("interpreter-exec", class_support,
  	       interpreter_exec_cmd,
  	       "Execute a command in an interpreter.  It takes two arguments:\n\
  The first argument is the name of the interpreter to use.\n\
  The second argument is the command to execute.\n", &cmdlist);
+   set_cmd_completer (c, interpreter_completer);
  }


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]