[comp.lang.c] Executing C functions w/ names passed as strings

ballarte@qucis.queensu.CA (Sandra Ballarte) (03/03/91)

	This might be a naive question, but I am having a problem 
trying to implement a query language that calls compiled C 
code.  This compiled C code should exist in a library of
functions (file.h).  The user enters the name of the so called
function and this is stored as a string.   

	How does one execute the named function within 
the query language (C) source code?  

	Is there a function that can evaluate and execute 
strings *and* return the results?

	I know about a function called SYSTEM which
executes compiled external functions whose names
are passed as strings.  *BUT* the results are not
returned!

	I would really appreciate any help from
anyone who can help.  I am sure there is a simple
solution, but I just can't find it.

	Please respond by e-mail.

	Thanks,

	Sandra Ballarte
	Queen's University, Canada

	email:  ballarte@qucis.queensu.ca

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (03/03/91)

In article <1103@qusung.queensu.CA>, ballarte@qucis.queensu.CA (Sandra Ballarte) writes:
> 	This might be a naive question, but I am having a problem 
> trying to implement a query language that calls compiled C 
> code.  This compiled C code should exist in a library of
> functions (file.h).  The user enters the name of the so called
> function and this is stored as a string.   

Is there any reason at all why the names the user sees should be the
same as the names that are written in the program?  There are several
excellent reasons why they might *not* be:
     -- externally visible identifiers should be unique in the first
	six characters, ignoring case, if the program is to be portable.
	(This is not something invented by the ANSI committee, it is a
	reflection of The Way Things Are.)  So the identifiers used in
	the program may not be as clear as they might be.
     -- most programmers are atrociously bad at naming things, functions
        not excepted.  There is no reason to inflict a programmers' idea
        of "cute" names on customers.
     -- You might want to allow customers to install names in their own
	language, either instead of the existing names, or in addition
	to them.  (A command "alias oldname newname" is easy to provide.)
     -- it might be useful to have several names for one function anyway;
	"PR" and "PRINT", perhaps, or "COMMENT" and "NOTE" and "REMARK".
     -- alphabetic case is significant in C identifiers, but many users
	are confused by this.  You will probably want to convert the user's
	choice of case to lower case.  Given that the function name and
	what the user types are not EXACTLY the same, why force the linkage
	to be one to one in any other sense?

> 	How does one execute the named function within 
> the query language (C) source code?  

Build a table (what kind?  any kind: simple array, linked list, AVL tree,
hash table, B-tree, whatever takes your fancy) that has strings as keys
and C function pointers as values.  Look the string up in the table, and
call via the function pointer you get back.  For details on function
pointers and how to call functions through them, see any introductory C text.
For dictionary data structures, see any introductory data structures text.
(See if your C library has hsearch().  You may be able to use that.)

> 	I know about a function called SYSTEM which
> executes compiled external functions whose names
> are passed as strings.  *BUT* the results are not
> returned!

There is no function called "SYSTEM" in C.  There is a function called
"system".  (See what I said about alphabetic case being significant?)
Its effect depends on the operating system.  In UNIX, VMS, MS-DOS, VM/CMS,
and others that I have come across, it does *NOT* execute "compiled
external functions".  It calls "programs", which need not be "compiled".
(They might be Bourne shell, Korn shell, C shell, DCL, ".BAT", or REXX
scripts.)  In most of these systems, the "result" of a program is
defined to be some sort of success/failure status, and that _is_ returned.

> I am sure there is a simple solution, but I just can't find it.

If the worst came to the worst, what was stopping you doing
	if (strcmp(Name, "fred")     == 0) fred(); else
	...
which is what you would have to do in Pascal or Ada?

-- 
The purpose of advertising is to destroy the freedom of the market.