The first example simply takes two numbers as arguments on the command line, multiplies the numbers and prints out the result on stderr.
#include <shell.h>
#include <nval.h>
int main(int argc, char *argv[])
{
Namval_t *np;
Sfdouble_t res;
Shell_t *shp = sh_init(argc, argv, 0);
sh_trap("((xresult=$0*$1))", 0);
np = nv_open("xresult", shp->var_tree, 0);
res = nv_getnum(np);
sfprintf(sfstderr,"%Lg\n", res);
nv_close(np);
return(0);
}
gcc -o example1 -I/work/ksh93/arch/linux.i386-64/include/ast example1.c -L/work/ksh93/arch/linux.i386-64/lib -lshell -lm -ldll -lcmd -ldl -last
$ ./example1 3 4
12
$
#include <shell.h>
#include <stdio.h>
#include <nval.h>
int
main(int argc, char *argv[])
{
char tmp[512];
char string[] ="hello world 'my name is Finnbarr' foo\\ bar";
sprintf(tmp, "set - %s && for i in \"$@\";\n do\n echo \"$i\"\ndone", string);
Shell_t *shp = sh_init(argc, argv, 0);
sh_trap(tmp, 0);
return(0);
}
./example2
hello
world
my name is Finnbarr
foo bar
#include <shell.h>
#include <stdio.h>
#include <nval.h>
int
shell(int argc, char* argv[], char *str)
{
Namval_t *np, *np_sub;
char tmp[512];
sprintf(tmp, "set - %s && for i in \"$@\";\n do\n aname+=(\"$i\")\ndone", str);
Shell_t *shp = sh_init(argc, argv, 0);
sh_trap(tmp, 0);
np = nv_open("aname", shp->var_tree, 0);
nv_putsub(np, NULL, ARRAY_SCAN);
np_sub = np;
do {
// copy out the arguments to wherever here.
fprintf(stderr, "%d: subscript='%s' value='%s'\n", np_sub, nv_getsub(np_sub), nv_getval(np_sub));
} while (np_sub && nv_nextsub(np_sub));
nv_close(np);
return(0);
}
int
main(int argc, char *argv[])
{
char string[] ="hello world 'my name is simon' foo\\ bar";
shell(argc, argv, string);
}
$ ./example1
37212336: subscript='0' value='hello'
37212336: subscript='1' value='world'
37212336: subscript='2' value='my name is simon'
37212336: subscript='3' value='foo bar'
$