In my previous report I suggesting building one of the example modules from the 
ksh source distribution (open.c) - that is still a worthwhile test
case but I have another that may be simpler to work with:

This example, when run, takes two arguments, a variable name, and a value, and 
assigns the value to the variable. It's useful as a test case because
it's simple, more so than open.c. It's a bit limited as a test case because it 
doesn't include any stdio calls - which is currently where I'm stuck in
terms of getting things working.

Interestingly enough, I discovered that some of the build files in the ksh 
source distribution do appear to conditionally add -Wl,--export-dynamic to
the linker flags, but for whatever reason that doesn't seem to be taking effect 
in the actual build.
/* setvar:
 *
 * A loadable Korn Shell "builtin" that provides a simple test of the process of creating builtins
 */

#include <ast.h>
#include <error.h>
#include <defs.h>
#include <variables.h>
#include <builtins.h>
#include <nval.h>
#include "stdio.h"

/* b_setvar() is ksh's entry point to the module
   Shbltin_t is a starting point for accessing ksh data structures.
   It is defined in src/lib/libast/include/shcmd.h in the ksh source tree. */
int b_setvar(int argc, char *argv[], Shbltin_t *context)
{
    Shell_t* shp = context->shp;
    Namval_t* var;

    char* var_name = 0;
    char* var_value = 0;

    if (argc == 3)
    {
        var_name = argv[1];
        var_value = argv[2];
    }
    else
    {
        return -1;
    }

    var = nv_open(var_name, shp->var_tree, (NV_ASSIGN | NV_IDENT | NV_VARNAME));

    /* I think nv_open will actually print our error and exit out for us if we get a bad var name
       But just in case... */
    if (!var)
    {
        return -2;
    }

    nv_putval(var, var_value, 0);
    nv_close(var);

    return 0;
}


SHLIB(setvar);

INCLUDES=-I /home/tetsujin/src/ksh-93u+20120801/arch/linux.i386-64/include/ast/ -I /home/tetsujin/src/ksh-93u+20120801/src/cmd/ksh93/include/ -I /home/tetsujin/src/ksh-93u+20120801/arch/linux.i386-64/src/cmd/ksh93/
CCFLAGS=-fPIC -g $(INCLUDES)
LDFLAGS=-shared -L/home/tetsujin/src/ksh-93u+20120801/arch/linux.i386-64/lib -last

all: setvar.so

.c.o:
	gcc -c $(CCFLAGS) -c -o $@ $<

setvar.so: setvar.o
	gcc $(LDFLAGS) -Wl,-soname,setvar.so -o setvar.so setvar.o

Reply via email to