[comp.lang.c] Incorporating Users' Functions

larocque@jupiter.crd.ge.com (David M. LaRocque) (09/18/90)

I'm looking for suggestions on the best technique to incorporate
user-written functions into a software package I'm developing.  
Specifically, I have helped write a large system in C that is
used by researchers to reason about events.  It is a system
that is used to develop other systems.  As such, there needs to
be a way to allow users to develop their own functions which can
in turn be accessed by the system.  The system was originally
written in Lisp.  In the Lisp version users can simply write their own
functions and add them to the "Lisp world".  The Lisp version can 
then "eval" the function name with the arguements.  I wish it
were that simple in C.

As background, the user creates english-like rules such as:

if (is-value-predicate X classification mammal)
then (set-value X respiration-type air-breathing).

The user creates a file of rules such as the one above.  The file
of rules is parsed by the yacc/lex and appropriate data structures
created.  For the above rule all objects that have a classification
of mammals would have their respiration-type set to air-breathing.
Simple functions like "is-value-predicate" and "set-value" are 
provided for the user.  However, the user might want to do something
more complicated like:

if (hairy-user-calculation X air-speed)
then (set-value X passed-hairy-test yes).

The problem is how do I allow the user to write the C function
"hairy-user-calculation" and have the system call it with the 
appropriate arguements.

Since the file of rules is parsed using yacc/lex, what I think I
need is a way for yacc/lex to grab a function pointer to
the function.  Another problem is that one doesn't know how
many arguements a given function might use, but perhaps that
could be dealt with with the varargs facility.

I suppose what might be ideal is to have the user create all his
functions in a separate file that is compiled into the main
program.  Then I think the problem becomes finding out what a
function's pointer is when one only knows the name of the function.

Thanks, Dave

/**************************************************
 * larocque@crd.ge.com        (518) 387-5805
 * ...!crdgw1!cetus.crd.ge.com!larocque
 **************************************************/

dpd@mullian.ee.mu.oz.au (Denis Dowling) (09/18/90)

In article <11954@crdgw1.crd.ge.com> larocque@jupiter.crd.ge.com (David M. LaRocque) writes:
>I'm looking for suggestions on the best technique to incorporate
>user-written functions into a software package I'm developing.  
>could be dealt with with the varargs facility.

[Description Deleted]

>I suppose what might be ideal is to have the user create all his
>functions in a separate file that is compiled into the main
>program.  Then I think the problem becomes finding out what a
>function's pointer is when one only knows the name of the function.

Have you had a look at the nlist(3) call? The synopsis is given below.

NAME
     nlist - get entries from symbol table

SYNOPSIS
     #include <nlist.h>

     int nlist(filename, nl)
     char *filename;
     struct nlist *nl;

This will return the symbol table entry for the specified symbol.
The nlist structure has a value field that can be cast to a function pointer.
I have used this for a system similar to what you describe.

I do not know of any clean way to deal with function arguments though. What
is needed is a reverse form of varargs that allows the construction of an
argument list for passing to a function. Anyone have any ideas?

It might pay you to have a look at the recent postings in alt.sources on
dynamic loading of proceedures in unix. This can allow functions to be
loaded on the fly as needed and not have to be explicitly linked with the
executable program.

>
>Thanks, Dave
>
>/**************************************************
> * larocque@crd.ge.com        (518) 387-5805
> * ...!crdgw1!cetus.crd.ge.com!larocque
> **************************************************/

______________________________________________________________________________
Denis Dowling 			 	ACSnet   :   dpd@mullian.mu.oz
SITEE - (Elec Eng)		 	uunet    :  uunet!munnari!mullian!dpd 
University of Melbourne 	 	internet :  dpd@mullian.mu.oz.au