This is the mail archive of the
systemtap@sourceware.org
mailing list for the systemtap project.
Patch proposal 1/2: number of arg identifier
- From: Pierre Peiffer <pierre dot peiffer at bull dot net>
- To: systemtap at sourceware dot org
- Date: Tue, 03 Apr 2007 16:37:38 +0200
- Subject: Patch proposal 1/2: number of arg identifier
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
---
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.