For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > param_cnt









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author param_cnt
Clemens Meier

2004-03-19, 8:23 pm


Hello!

I'm trying to extend gawk to the libpnm functions, but I've hit
a snag: According to Effective Awk Programming (3rd Ed.),
node->param_cnt is the number of parameters actually passed in
a function call at runtime. Yet on my Linux box (Debian Woody),
node->param_cnt seems just to report the maximum number of
parameters the function is defined for with make_builtin().

This is my environment. Gawk 3.1.0 comes with the Linux
distribution, I've tried gawk 3.1.3, which exhibits the same
behaviour with respect to node->param_cnt.

lilihome$ uname -a
Linux lilihome 2.2.20 #1 Mon Dec 31 07:05:08 EST 2001 i586 unknown
lilihome$ gawk --version
GNU Awk 3.1.0
lilihome$ gcc --version
2.95.4
lilihome$ ld --version
GNU ld version 2.12.90.0.1 20020307 Debian/GNU Linux

Here is a small test suite, it assumes there is a configured
gawk source in ./gawk-3.1.3 together with a freshly made gawk
executable.

--- test.skel --- skeleton C source ---------------------------------->8---

#include "awk.h"

#define NPARAMS 0

static NODE *get_param_cnt (NODE *t)
{
set_value(make_number((AWKNUM)(t->param_cnt)));
}

NODE *dlload (NODE *t, void *dl)
{
make_builtin("get_param_cnt", get_param_cnt, NPARAMS);
return tmp_number((AWKNUM)0);
}
--- test.skel --- skeleton C source ---------------------------------->8---

--- test.awk --- code to test C function ----------------------------->8---

BEGIN {
extension("./test.so", "dlload");
print 0, get_param_cnt();
print 1, get_param_cnt(1);
print 2, get_param_cnt(1, 2);
print 3, get_param_cnt(1, 2, 3);
print 4, get_param_cnt(1, 2, 3, 4);
print 5, get_param_cnt(1, 2, 3, 4, 5);
print 6, get_param_cnt(1, 2, 3, 4, 5, 6);
print 7, get_param_cnt(1, 2, 3, 4, 5, 6, 7);
print 8, get_param_cnt(1, 2, 3, 4, 5, 6, 7, 8);
print 9, get_param_cnt(1, 2, 3, 4, 5, 6, 7, 8, 9);
print 10, get_param_cnt(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}
--- test.awk --- code to test C function ----------------------------->8---

--- test-param_cnt.awk --- test scaffold ----------------------------->8---

BEGIN {
if (INC == "") INC = "./gawk-3.1.3";
if (AWK == "") AWK = INC "/gawk";

printf("called with: ");
for (i = 0; i <= 10; i++) {
printf(" %2d ", i);
}
printf("\n");

for (i = 0; i <= 10; i++) {
create(i);
compile();
test(i);
}
}

function create (nparams, s) {
while (getline < "test.skel") {
if ($0 ~ /^\#define NPARAMS/) {
$3 = nparams;
}
print > "test.c";
}
close("test.skel");
close("test.c");
}

function compile () {
system("gcc -shared -DHAVE_CONFIG_H -c -O -I" INC " test.c");
system("ld -o test.so -shared test.o");
}

function test (nparams, cmd, warn) {
printf("defined %2d, got", nparams);
cmd = AWK " -f test.awk 2>&1";
warn = 0;
while (cmd | getline) {
if ($0 ~ /called with more arguments than declared$/) {
warn++;
} else if ($0 ~ /^[0-9]+ [0-9]+$/) {
printf(" %2d%s%s", $2, warn ? "w" : "_", $2 == $1 + 1 ? " " : "!");
warn = 0;
} else {
printf("\ndon't grok \"%s\"\n", $0) >> "/dev/stderr";
}
}
printf("\n");
close(cmd);
}

--- test-param_cnt.awk --- test scaffold ----------------------------->8---

Calling gawk -f test-param_cnt.awk produces the following
output. For each line a shared object file is created, with
get_param_cnt defined (by make_builtin()) as having the number
of parameters given in the table. Then (across), get_param_cnt
is called (via test.awk) with 0 to 10 parameters. The data is
the value returned by get_param_cnt, with a ``w'' suffix if
gawk issued a warning (about too many parameters) and a ``!''
if the returned value is wrong, i.e. not the number of
arguments given minus 1 (zero-based!).

called with: 0 1 2 3 4 5 6 7 8 9 10
defined 0, got 0_! 0w! 0w! 0w! 0w! 0w! 0w! 0w! 0w! 0w! 0w!
defined 1, got 1_ 1_! 1w! 1w! 1w! 1w! 1w! 1w! 1w! 1w! 1w!
defined 2, got 2_! 2_ 2_! 2w! 2w! 2w! 2w! 2w! 2w! 2w! 2w!
defined 3, got 3_! 3_! 3_ 3_! 3w! 3w! 3w! 3w! 3w! 3w! 3w!
defined 4, got 4_! 4_! 4_! 4_ 4_! 4w! 4w! 4w! 4w! 4w! 4w!
defined 5, got 5_! 5_! 5_! 5_! 5_ 5_! 5w! 5w! 5w! 5w! 5w!
defined 6, got 6_! 6_! 6_! 6_! 6_! 6_ 6_! 6w! 6w! 6w! 6w!
defined 7, got 7_! 7_! 7_! 7_! 7_! 7_! 7_ 7_! 7w! 7w! 7w!
defined 8, got 8_! 8_! 8_! 8_! 8_! 8_! 8_! 8_ 8_! 8w! 8w!
defined 9, got 9_! 9_! 9_! 9_! 9_! 9_! 9_! 9_! 9_ 9_! 9w!
defined 10, got 10_! 10_! 10_! 10_! 10_! 10_! 10_! 10_! 10_! 10_ 10_!

As you can see, gawk issues warnings correctly, but
node->param_cnt always returns the number that has been fed to
make_builtin() instead of the actual number of parameters in
the function call.

This clearly is an undocumented feature, but is it truly gawk's
or my program's?

CU

Clemens

-- ------------------------------------------------------------------ --
Der Ehrliche ist der Dumme, clmeier@lili.uni-bielefeld.de
der Aufrechte ist der Kranke, Clemens Meier
der Hilfsbereite ist der in den A*sch getretene
und ich bin der Messias. ----------------- ``Ulrich Wickert'' bei Switch
Aharon Robbins

2004-03-19, 8:23 pm

In article <87brnmj2xl.fsf@lili.uni-bielefeld.de>,
Clemens Meier <clmeier@lili.uni-bielefeld.de> wrote:
>
>Hello!
>
>I'm trying to extend gawk to the libpnm functions, but I've hit
>a snag: According to Effective Awk Programming (3rd Ed.),
>node->param_cnt is the number of parameters actually passed in
>a function call at runtime. Yet on my Linux box (Debian Woody),
>node->param_cnt seems just to report the maximum number of
>parameters the function is defined for with make_builtin().


You're not the first one to report this.

There's something hosed somewhere. Either it's my understanding of
the code, in which case the doc is wrong, or something in the code
broke somewhere at sometime and I wasn't paying attention. This is
on my list of things to get fixed, one way or the other, for 3.1.4.

Thanks,

Arnold
--
Aharon (Arnold) Robbins --- Pioneer Consulting Ltd. arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381 Fax: +1 530 688 5518
Nof Ayalon Cell Phone: +972 51 297-545
D.N. Shimshon 99785 ISRAEL
john@nowhere.com

2004-03-19, 8:23 pm

In article <87brnmj2xl.fsf@lili.uni-bielefeld.de>, Clemens Meier wrote:
>
> Hello!
>
> I'm trying to extend gawk to the libpnm functions, but I've hit
> a snag: According to Effective Awk Programming (3rd Ed.),
> node->param_cnt is the number of parameters actually passed in
> a function call at runtime. Yet on my Linux box (Debian Woody),
> node->param_cnt seems just to report the maximum number of
> parameters the function is defined for with make_builtin().


Try this with gawk-3.1.3 (untested):

/* count the number of arguments in a function call */
static int
count_args(NODE *tree)
{
int i, count = 0;
extern NODE **stack_ptr;

for (i = 0; i < tree->param_cnt; i++) {
if (stack_ptr[i]->type == Node_var_new) /* local variable */
break;
count++;
}
return count;
}

This assumes you don't call the function with more arguments than declared
in make_builtin(..). In the later case, it will always report param_cnt
(the number passed to make_builtin) as the number of arguments supplied.
If you are only interested in whether it has been called with more args
than declared, simply pass (N + 1) to make_builtin, where N is the
maximum number of allowed args, and check if count_args() returns (N + 1).

Hope this helps.

John
Clemens Meier

2004-03-19, 8:23 pm


Hello!

arnold@skeeve.com (Aharon Robbins) writes:

> In article <87brnmj2xl.fsf@lili.uni-bielefeld.de>,
> Clemens Meier <clmeier@lili.uni-bielefeld.de> wrote:


>
> You're not the first one to report this.


Oops, I'm sorry. I googled for it but not *thoroughly*, it seems.

> There's something hosed somewhere. Either it's my understanding of
> the code, in which case the doc is wrong, or something in the code
> broke somewhere at sometime and I wasn't paying attention. This is
> on my list of things to get fixed, one way or the other, for 3.1.4.


Thanks. I'll use John's workaround (thanks, John) in the mean
time, and since my application isn't very time-critical, it'll
do. Meaning, don't let me bother you. ;-)

CU

Clemens

-- ------------------------------------------------------------------ --
Der Ehrliche ist der Dumme, clmeier@lili.uni-bielefeld.de
der Aufrechte ist der Kranke, Clemens Meier
der Hilfsbereite ist der in den A*sch getretene
und ich bin der Messias. ----------------- ``Ulrich Wickert'' bei Switch
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com