public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
* Patch proposal 1/2: number of arg identifier
@ 2007-04-03 14:39 Pierre Peiffer
  2007-04-03 14:45 ` Patch proposal 2/2: allow use of $x and @x identifiers during preprocessing Pierre Peiffer
  0 siblings, 1 reply; 8+ messages in thread
From: Pierre Peiffer @ 2007-04-03 14:39 UTC (permalink / raw)
  To: systemtap

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

Hi,

	Here is a proposal of patch which allows the access to the number of arguments 
passed to the script through the bash-like identifier $# and @# (the identifier 
is interpreted as tok_number or tok_string accordingly).

	In fact, I've done this for a colleague and found this useful. That's why I 
send it to this list, but just forget this mail if you don't like it ;-)

	I'll send a second proposal, anyway...

-- 
Pierre

[-- Attachment #2: allow-number-of-args-pattern.patch --]
[-- Type: text/x-patch, Size: 2839 bytes --]

---
 ChangeLog |    5 +++++
 parse.cxx |   48 +++++++++++++++++++++++++++++-------------------
 2 files changed, 34 insertions(+), 19 deletions(-)

Index: b/parse.cxx
===================================================================
--- a/parse.cxx
+++ b/parse.cxx
@@ -501,7 +501,7 @@ lexer::scan ()
 	  int c2 = input_peek ();
 	  if (! input)
 	    break;
-	  if ((isalnum(c2) || c2 == '_' || c2 == '$'))
+	  if ((isalnum(c2) || c2 == '_' || c2 == '$' || c2 == '#' ))
 	    {
 	      n->content.push_back(c2);
 	      input_get ();
@@ -514,24 +514,34 @@ lexer::scan ()
       // numbers and @1 .. @999 as strings.
       if (n->content[0] == '@' || n->content[0] == '$')
         {
-          string idxstr = n->content.substr(1);
-          const char* startp = idxstr.c_str();
-          char *endp;
-          errno = 0;
-          unsigned long idx = strtoul (startp, &endp, 10);
-          if (endp == startp)
-            ; // no numbers at all - leave alone as identifier 
-          else
-            {
-              // Use @1/$1 as the base, not @0/$0.  Thus the idx-1.
-              if (errno == ERANGE || errno == EINVAL || *endp != '\0' ||
-                  idx == 0 || idx-1 >= session.args.size ())
-                throw parse_error ("command line argument index invalid or out of range", n);
-              
-              string arg = session.args[idx-1];
-              n->type = (n->content[0] == '@') ? tok_string : tok_number;
-              n->content = arg;
-            }
+	  if (n->content[1] == '#')
+	    {
+	      stringstream converter;
+	      converter << session.args.size ();
+	      n->type = (n->content[0] == '@') ? tok_string : tok_number;
+	      n->content = converter.str();
+	    }
+	  else
+	    {
+	      string idxstr = n->content.substr(1);
+	      const char* startp = idxstr.c_str();
+	      char *endp;
+	      errno = 0;
+	      unsigned long idx = strtoul (startp, &endp, 10);
+	      if (endp == startp)
+		; // no numbers at all - leave alone as identifier
+	      else
+		{
+		  // Use @1/$1 as the base, not @0/$0.  Thus the idx-1.
+		  if (errno == ERANGE || errno == EINVAL || *endp != '\0' ||
+		      idx == 0 || idx-1 >= session.args.size ())
+		    throw parse_error ("command line argument index invalid or out of range", n);
+
+		  string arg = session.args[idx-1];
+		  n->type = (n->content[0] == '@') ? tok_string : tok_number;
+		  n->content = arg;
+		}
+	    }
         }
       else
         {
Index: b/ChangeLog
===================================================================
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2007-04-03  Pierre Peiffer  <pierre.peiffer@bull.net>
+
+	* parse.cxx: Add $# and @# identifiers to access the number
+	of arguments passed as 'number' or as 'string'.
+
 2007-03-22  Frank Ch. Eigler  <fche@elastic.org>
 
 	PR 4224.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2007-04-04 21:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-03 14:39 Patch proposal 1/2: number of arg identifier Pierre Peiffer
2007-04-03 14:45 ` Patch proposal 2/2: allow use of $x and @x identifiers during preprocessing Pierre Peiffer
2007-04-03 15:09   ` Benjamin Thery
2007-04-03 15:39     ` Frank Ch. Eigler
2007-04-03 16:10       ` Benjamin Thery
2007-04-04  7:16       ` Pierre Peiffer
2007-04-04 14:48         ` Patch: allow use of $x and @x Pierre Peiffer
2007-04-04 21:18           ` Frank Ch. Eigler

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).