public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
From: Andrew STUBBS <andrew.stubbs@st.com>
To: Daniel Jacobowitz <drow@false.org>
Cc: GDB List <gdb@sources.redhat.com>
Subject: Re: $argc variable
Date: Tue, 08 Nov 2005 13:05:00 -0000	[thread overview]
Message-ID: <43709E94.4070004@st.com> (raw)
In-Reply-To: <20051107133538.GA2331@nevyn.them.org>

[-- Attachment #1: Type: text/plain, Size: 668 bytes --]

Daniel Jacobowitz wrote:
> Mostly, the coding style was very un-GNU.  I also got the feeling that
> there were better ways to do a lot of the string parsing, but I really
> didn't spend much time looking at it (since I figured reimplementing
> would be simpler in this case than FSF assignment).

I'm not sure exactly what I have to do to avoid being "un-GNU", but 
here's my attempt at the same problem. I don't think the string parsing 
can be made any simpler.

In the documentation I changed a '@var' to '@code' because @var makes it 
upper case in the info and I thought that misleading. I also added a 
missing 'end' to the existing example.

OK?

Andrew Stubbs

[-- Attachment #2: argc.patch --]
[-- Type: text/plain, Size: 4212 bytes --]

2005-11-08  Andrew Stubbs  <andrew.stubbs@st.com>

	* cli/cli-script.c: Include gdb_assert.h.
	(locate_arg): Detect $argc.
	(insert_args): Substitute $argc.
	* Makefile.in (cli-script.o): Add dependency on gdb_assert.h.

doc/
	* gdb.texinfo (User-defined commands): Add $argc. Add missing 'end'.
	Change @var{$arg0 to @code{$arg0 to keep case right.

Index: src/gdb/cli/cli-script.c
===================================================================
--- src.orig/gdb/cli/cli-script.c	2005-11-08 12:39:37.000000000 +0000
+++ src/gdb/cli/cli-script.c	2005-11-08 12:40:10.000000000 +0000
@@ -33,6 +33,7 @@
 #include "cli/cli-cmds.h"
 #include "cli/cli-decode.h"
 #include "cli/cli-script.h"
+#include "gdb_assert.h"
 
 /* Prototypes for local functions */
 
@@ -572,7 +573,8 @@ locate_arg (char *p)
 {
   while ((p = strchr (p, '$')))
     {
-      if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
+      if (strncmp (p, "$arg", 4) == 0
+	  && (isdigit (p[4]) || p[4] == 'c'))
 	return p;
       p++;
     }
@@ -596,12 +598,20 @@ insert_args (char *line)
       len += p - line;
       i = p[4] - '0';
 
-      if (i >= user_args->count)
+      if (p[4] == 'c')
+	{
+	  /* $argc.  Number will be <=10.  */
+	  len += user_args->count == 10 ? 2 : 1;
+	}
+      else if (i >= user_args->count)
 	{
 	  error (_("Missing argument %d in user function."), i);
 	  return NULL;
 	}
-      len += user_args->a[i].len;
+      else
+	{
+	  len += user_args->a[i].len;
+	}
       line = p + 5;
     }
 
@@ -625,13 +635,27 @@ insert_args (char *line)
 
       memcpy (new_line, line, p - line);
       new_line += p - line;
-      i = p[4] - '0';
 
-      len = user_args->a[i].len;
-      if (len)
+      if (p[4] == 'c')
+	{
+	  gdb_assert (user_args->count >= 0 && user_args->count <= 10);
+	  if (user_args->count == 10)
+	    {
+	      *(new_line++) = '1';
+	      *(new_line++) = '0';
+	    }
+	  else
+	    *(new_line++) = user_args->count + '0';
+	}
+      else
 	{
-	  memcpy (new_line, user_args->a[i].arg, len);
-	  new_line += len;
+	  i = p[4] - '0';
+	  len = user_args->a[i].len;
+	  if (len)
+	  {
+	    memcpy (new_line, user_args->a[i].arg, len);
+	    new_line += len;
+	  }
 	}
       line = p + 5;
     }
Index: src/gdb/doc/gdb.texinfo
===================================================================
--- src.orig/gdb/doc/gdb.texinfo	2005-11-08 12:39:40.000000000 +0000
+++ src/gdb/doc/gdb.texinfo	2005-11-08 12:40:11.000000000 +0000
@@ -15660,11 +15660,12 @@ A @dfn{user-defined command} is a sequen
 which you assign a new name as a command.  This is done with the
 @code{define} command.  User commands may accept up to 10 arguments
 separated by whitespace.  Arguments are accessed within the user command
-via @var{$arg0@dots{}$arg9}.  A trivial example:
+via @code{$arg0@dots{}$arg9}.  A trivial example:
 
 @smallexample
 define adder
   print $arg0 + $arg1 + $arg2
+end
 @end smallexample
 
 @noindent
@@ -15680,6 +15681,21 @@ its three arguments.  Note the arguments
 reference variables, use complex expressions, or even perform inferior
 functions calls.
 
+@kindex $argc
+In addition, @code{$argc} may be used to find out how many arguments have
+been passed.  This expands to a number in the range 0@dots{}10.
+
+@smallexample
+define adder
+  if $argc == 2
+    print $arg0 + $arg1
+  end
+  if $argc == 3
+    print $arg0 + $arg1 + $arg2
+  end
+end
+@end smallexample
+
 @table @code
 
 @kindex define
Index: src/gdb/Makefile.in
===================================================================
--- src.orig/gdb/Makefile.in	2005-11-08 12:39:40.000000000 +0000
+++ src/gdb/Makefile.in	2005-11-08 12:40:11.000000000 +0000
@@ -2807,7 +2807,7 @@ cli-logging.o: $(srcdir)/cli/cli-logging
 	$(CC) -c $(INTERNAL_CFLAGS) $(srcdir)/cli/cli-logging.c
 cli-script.o: $(srcdir)/cli/cli-script.c $(defs_h) $(value_h) $(language_h) \
 	$(ui_out_h) $(gdb_string_h) $(exceptions_h) $(top_h) $(cli_cmds_h) \
-	$(cli_decode_h) $(cli_script_h)
+	$(cli_decode_h) $(cli_script_h) $(gdb_assert_h)
 	$(CC) -c $(INTERNAL_CFLAGS) $(srcdir)/cli/cli-script.c
 cli-setshow.o: $(srcdir)/cli/cli-setshow.c $(defs_h) $(readline_tilde_h) \
 	$(value_h) $(gdb_string_h) $(ui_out_h) $(cli_decode_h) $(cli_cmds_h) \

  reply	other threads:[~2005-11-08 13:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-03 13:11 Andrew STUBBS
2005-11-03 19:51 ` Dan Nicolaescu
2005-11-07  0:18 ` Daniel Jacobowitz
2005-11-07 11:12   ` Andrew STUBBS
2005-11-07 13:35     ` Daniel Jacobowitz
2005-11-08 13:05       ` Andrew STUBBS [this message]
2005-11-08 23:19         ` Eli Zaretskii
2005-11-09 11:15           ` Andrew STUBBS
2005-11-09 19:19             ` Eli Zaretskii
2005-11-10 10:11               ` Andrew STUBBS
2005-11-13 17:35                 ` Daniel Jacobowitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43709E94.4070004@st.com \
    --to=andrew.stubbs@st.com \
    --cc=drow@false.org \
    --cc=gdb@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).