mirror of
https://github.com/postgres/postgres.git
synced 2025-11-12 05:01:15 +03:00
Postgres95 1.01 Distribution - Virgin Sources
This commit is contained in:
15
src/backend/utils/fmgr/Makefile.inc
Normal file
15
src/backend/utils/fmgr/Makefile.inc
Normal file
@@ -0,0 +1,15 @@
|
||||
#-------------------------------------------------------------------------
|
||||
#
|
||||
# Makefile.inc--
|
||||
# Makefile for utils/fmgr
|
||||
#
|
||||
# Copyright (c) 1994, Regents of the University of California
|
||||
#
|
||||
#
|
||||
# IDENTIFICATION
|
||||
# $Header: /cvsroot/pgsql/src/backend/utils/fmgr/Attic/Makefile.inc,v 1.1.1.1 1996/07/09 06:22:07 scrappy Exp $
|
||||
#
|
||||
#-------------------------------------------------------------------------
|
||||
|
||||
SUBSRCS+= dfmgr.c fmgr.c
|
||||
|
||||
269
src/backend/utils/fmgr/dfmgr.c
Normal file
269
src/backend/utils/fmgr/dfmgr.c
Normal file
@@ -0,0 +1,269 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* dfmgr.c--
|
||||
* Dynamic function manager code.
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.1.1.1 1996/07/09 06:22:07 scrappy Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "fmgr.h" /* generated by Gen_fmgrtab.sh */
|
||||
#include "utils/dynamic_loader.h"
|
||||
#include "utils/elog.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "access/heapam.h"
|
||||
#include "nodes/pg_list.h"
|
||||
|
||||
#include "port-protos.h" /* system specific function prototypes */
|
||||
|
||||
#include "catalog/catname.h"
|
||||
#include "utils/syscache.h"
|
||||
#include "catalog/pg_proc.h"
|
||||
|
||||
static DynamicFileList *file_list = (DynamicFileList *) NULL;
|
||||
static DynamicFileList *file_tail = (DynamicFileList *) NULL;
|
||||
|
||||
#define NOT_EQUAL(A, B) (((A).st_ino != (B).inode) \
|
||||
|| ((A).st_dev != (B).device))
|
||||
|
||||
static Oid procedureId_save = -1;
|
||||
static int pronargs_save;
|
||||
static func_ptr user_fn_save = (func_ptr) NULL;
|
||||
static func_ptr handle_load(char *filename, char *funcname);
|
||||
|
||||
func_ptr
|
||||
fmgr_dynamic(Oid procedureId, int *pronargs)
|
||||
{
|
||||
HeapTuple procedureTuple;
|
||||
Form_pg_proc procedureStruct;
|
||||
char *proname;
|
||||
char *probinattr, *probinstring;
|
||||
func_ptr user_fn, handle_load();
|
||||
Relation rdesc;
|
||||
bool isnull;
|
||||
|
||||
if (procedureId == procedureId_save) {
|
||||
*pronargs = pronargs_save;
|
||||
return(user_fn_save);
|
||||
}
|
||||
|
||||
/*
|
||||
* The procedure isn't a builtin, so we'll have to do a catalog
|
||||
* lookup to find its pg_proc entry.
|
||||
*/
|
||||
procedureTuple = SearchSysCacheTuple(PROOID, ObjectIdGetDatum(procedureId),
|
||||
0,0,0);
|
||||
if (!HeapTupleIsValid(procedureTuple)) {
|
||||
elog(WARN, "fmgr: Cache lookup failed for procedure %d\n",
|
||||
procedureId);
|
||||
return((func_ptr) NULL);
|
||||
}
|
||||
|
||||
procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
|
||||
proname = procedureStruct->proname.data;
|
||||
pronargs_save = *pronargs = procedureStruct->pronargs;
|
||||
|
||||
/*
|
||||
* Extract the procedure info from the pg_proc tuple.
|
||||
* Since probin is varlena, do a amgetattr() on the procedure
|
||||
* tuple. To do that, we need the rdesc for the procedure
|
||||
* relation, so...
|
||||
*/
|
||||
|
||||
/* open pg_procedure */
|
||||
|
||||
rdesc = heap_openr(ProcedureRelationName);
|
||||
if (!RelationIsValid(rdesc)) {
|
||||
elog(WARN, "fmgr: Could not open relation %s",
|
||||
ProcedureRelationName);
|
||||
return((func_ptr) NULL);
|
||||
}
|
||||
probinattr = heap_getattr(procedureTuple, (Buffer) 0,
|
||||
Anum_pg_proc_probin,
|
||||
RelationGetTupleDescriptor(rdesc), &isnull);
|
||||
if (!PointerIsValid(probinattr) /*|| isnull*/) {
|
||||
heap_close(rdesc);
|
||||
elog(WARN, "fmgr: Could not extract probin for %d from %s",
|
||||
procedureId, ProcedureRelationName);
|
||||
return((func_ptr) NULL);
|
||||
}
|
||||
probinstring = textout((struct varlena *) probinattr);
|
||||
|
||||
user_fn = handle_load(probinstring, proname);
|
||||
|
||||
procedureId_save = procedureId;
|
||||
user_fn_save = user_fn;
|
||||
|
||||
return(user_fn);
|
||||
}
|
||||
|
||||
static func_ptr
|
||||
handle_load(char *filename, char *funcname)
|
||||
{
|
||||
DynamicFileList *file_scanner = (DynamicFileList *) NULL;
|
||||
func_ptr retval = (func_ptr) NULL;
|
||||
char *load_error;
|
||||
#ifdef WIN32
|
||||
struct _stat stat_buf;
|
||||
#else
|
||||
struct stat stat_buf;
|
||||
#endif /* WIN32 */
|
||||
|
||||
/*
|
||||
* Do this because loading files may screw up the dynamic function
|
||||
* manager otherwise.
|
||||
*/
|
||||
procedureId_save = -1;
|
||||
|
||||
/*
|
||||
* Scan the list of loaded FILES to see if the function
|
||||
* has been loaded.
|
||||
*/
|
||||
|
||||
if (filename != (char *) NULL) {
|
||||
for (file_scanner = file_list;
|
||||
file_scanner != (DynamicFileList *) NULL
|
||||
&& file_scanner->filename != (char *) NULL
|
||||
&& strcmp(filename, file_scanner->filename) != 0;
|
||||
file_scanner = file_scanner->next)
|
||||
;
|
||||
if (file_scanner == (DynamicFileList *) NULL) {
|
||||
if (stat(filename, &stat_buf) == -1) {
|
||||
elog(WARN, "stat failed on file %s", filename);
|
||||
}
|
||||
|
||||
for (file_scanner = file_list;
|
||||
file_scanner != (DynamicFileList *) NULL
|
||||
&& (NOT_EQUAL(stat_buf, *file_scanner));
|
||||
file_scanner = file_scanner->next)
|
||||
;
|
||||
/*
|
||||
* Same files - different paths (ie, symlink or link)
|
||||
*/
|
||||
if (file_scanner != (DynamicFileList *) NULL)
|
||||
(void) strcpy(file_scanner->filename, filename);
|
||||
|
||||
}
|
||||
} else {
|
||||
file_scanner = (DynamicFileList *) NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* File not loaded yet.
|
||||
*/
|
||||
|
||||
if (file_scanner == (DynamicFileList *) NULL) {
|
||||
if (file_list == (DynamicFileList *) NULL) {
|
||||
file_list = (DynamicFileList *)
|
||||
malloc(sizeof(DynamicFileList));
|
||||
file_scanner = file_list;
|
||||
} else {
|
||||
file_tail->next = (DynamicFileList *)
|
||||
malloc(sizeof(DynamicFileList));
|
||||
file_scanner = file_tail->next;
|
||||
}
|
||||
memset((char *) file_scanner, 0, sizeof(DynamicFileList));
|
||||
|
||||
(void) strcpy(file_scanner->filename, filename);
|
||||
#ifndef WIN32
|
||||
file_scanner->device = stat_buf.st_dev;
|
||||
file_scanner->inode = stat_buf.st_ino;
|
||||
#endif /* WIN32 */
|
||||
file_scanner->next = (DynamicFileList *) NULL;
|
||||
|
||||
file_scanner->handle = pg_dlopen(filename);
|
||||
if (file_scanner->handle == (void *)NULL) {
|
||||
load_error = pg_dlerror();
|
||||
if (file_scanner == file_list) {
|
||||
file_list = (DynamicFileList *) NULL;
|
||||
} else {
|
||||
file_tail->next = (DynamicFileList *) NULL;
|
||||
}
|
||||
|
||||
free((char *) file_scanner);
|
||||
elog(WARN, "Load of file %s failed: %s", filename, load_error);
|
||||
}
|
||||
|
||||
/*
|
||||
* Just load the file - we are done with that so return.
|
||||
*/
|
||||
file_tail = file_scanner;
|
||||
|
||||
if (funcname == (char *) NULL)
|
||||
return((func_ptr) NULL);
|
||||
}
|
||||
|
||||
retval = pg_dlsym(file_scanner->handle, funcname);
|
||||
|
||||
if (retval == (func_ptr) NULL) {
|
||||
elog(WARN, "Can't find function %s in file %s", funcname, filename);
|
||||
}
|
||||
|
||||
return(retval);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function loads files by the following:
|
||||
*
|
||||
* If the file is already loaded:
|
||||
* o Zero out that file's loaded space (so it doesn't screw up linking)
|
||||
* o Free all space associated with that file
|
||||
* o Free that file's descriptor.
|
||||
*
|
||||
* Now load the file by calling handle_load with a NULL argument as the
|
||||
* function.
|
||||
*/
|
||||
void
|
||||
load_file(char *filename)
|
||||
{
|
||||
DynamicFileList *file_scanner, *p;
|
||||
#ifdef WIN32
|
||||
struct _stat stat_buf;
|
||||
#else
|
||||
struct stat stat_buf;
|
||||
#endif /* WIN32 */
|
||||
|
||||
int done = 0;
|
||||
|
||||
if (stat(filename, &stat_buf) == -1) {
|
||||
elog(WARN, "stat failed on file %s", filename);
|
||||
}
|
||||
|
||||
if (file_list != (DynamicFileList *) NULL
|
||||
&& !NOT_EQUAL(stat_buf, *file_list)) {
|
||||
file_scanner = file_list;
|
||||
file_list = file_list->next;
|
||||
pg_dlclose(file_scanner->handle);
|
||||
free((char *) file_scanner);
|
||||
} else if (file_list != (DynamicFileList *) NULL) {
|
||||
file_scanner = file_list;
|
||||
while (!done) {
|
||||
if (file_scanner->next == (DynamicFileList *) NULL) {
|
||||
done = 1;
|
||||
} else if (!NOT_EQUAL(stat_buf, *(file_scanner->next))) {
|
||||
done = 1;
|
||||
} else {
|
||||
file_scanner = file_scanner->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (file_scanner->next != (DynamicFileList *) NULL) {
|
||||
p = file_scanner->next;
|
||||
file_scanner->next = file_scanner->next->next;
|
||||
pg_dlclose(file_scanner->handle);
|
||||
free((char *)p);
|
||||
}
|
||||
}
|
||||
handle_load(filename, (char *) NULL);
|
||||
}
|
||||
254
src/backend/utils/fmgr/fmgr.c
Normal file
254
src/backend/utils/fmgr/fmgr.c
Normal file
@@ -0,0 +1,254 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* fmgr.c--
|
||||
* Interface routines for the table-driven function manager.
|
||||
*
|
||||
* Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/fmgr/fmgr.c,v 1.1.1.1 1996/07/09 06:22:08 scrappy Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
/* these 2 files are generated by Gen_fmgrtab.sh; contain the declarations */
|
||||
#include "fmgr.h"
|
||||
#include "utils/fmgrtab.h"
|
||||
|
||||
#include "nodes/pg_list.h"
|
||||
#include "catalog/pg_proc.h"
|
||||
#include "catalog/pg_language.h"
|
||||
#include "utils/syscache.h"
|
||||
#include "nodes/params.h"
|
||||
|
||||
#include "utils/elog.h"
|
||||
|
||||
|
||||
char *
|
||||
fmgr_c(func_ptr user_fn,
|
||||
Oid func_id,
|
||||
int n_arguments,
|
||||
FmgrValues *values,
|
||||
bool *isNull)
|
||||
{
|
||||
char *returnValue = (char *) NULL;
|
||||
|
||||
|
||||
if (user_fn == (func_ptr) NULL) {
|
||||
/*
|
||||
* a NULL func_ptr denotes untrusted function (in postgres 4.2).
|
||||
* Untrusted functions have very limited use and is clumsy. We
|
||||
* just get rid of it.
|
||||
*/
|
||||
elog(WARN, "internal error: untrusted function not supported.");
|
||||
}
|
||||
|
||||
switch (n_arguments) {
|
||||
case 0:
|
||||
returnValue = (*user_fn)();
|
||||
break;
|
||||
case 1:
|
||||
/* NullValue() uses isNull to check if args[0] is NULL */
|
||||
returnValue = (*user_fn)(values->data[0], isNull);
|
||||
break;
|
||||
case 2:
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1]);
|
||||
break;
|
||||
case 3:
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1],
|
||||
values->data[2]);
|
||||
break;
|
||||
case 4:
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1],
|
||||
values->data[2], values->data[3]);
|
||||
break;
|
||||
case 5:
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1],
|
||||
values->data[2], values->data[3],
|
||||
values->data[4]);
|
||||
break;
|
||||
case 6:
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1],
|
||||
values->data[2], values->data[3],
|
||||
values->data[4], values->data[5]);
|
||||
break;
|
||||
case 7:
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1],
|
||||
values->data[2], values->data[3],
|
||||
values->data[4], values->data[5],
|
||||
values->data[6]);
|
||||
break;
|
||||
case 8:
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1],
|
||||
values->data[2], values->data[3],
|
||||
values->data[4], values->data[5],
|
||||
values->data[6], values->data[7]);
|
||||
break;
|
||||
case 9:
|
||||
/*
|
||||
* XXX Note that functions with >8 arguments can only be
|
||||
* called from inside the system, not from the user level,
|
||||
* since the catalogs only store 8 argument types for user
|
||||
* type-checking!
|
||||
*/
|
||||
returnValue = (*user_fn)(values->data[0], values->data[1],
|
||||
values->data[2], values->data[3],
|
||||
values->data[4], values->data[5],
|
||||
values->data[6], values->data[7],
|
||||
values->data[8]);
|
||||
break;
|
||||
default:
|
||||
elog(WARN, "fmgr_c: function %d: too many arguments (%d > %d)",
|
||||
func_id, n_arguments, MAXFMGRARGS);
|
||||
break;
|
||||
}
|
||||
return(returnValue);
|
||||
}
|
||||
|
||||
void
|
||||
fmgr_info(Oid procedureId, func_ptr *function, int *nargs)
|
||||
{
|
||||
func_ptr user_fn;
|
||||
FmgrCall *fcp;
|
||||
HeapTuple procedureTuple;
|
||||
FormData_pg_proc *procedureStruct;
|
||||
Oid language;
|
||||
|
||||
if (!(fcp = fmgr_isbuiltin(procedureId))) {
|
||||
procedureTuple = SearchSysCacheTuple(PROOID,
|
||||
ObjectIdGetDatum(procedureId),
|
||||
0,0,0);
|
||||
if (!HeapTupleIsValid(procedureTuple)) {
|
||||
elog(WARN, "fmgr_info: function %d: cache lookup failed\n",
|
||||
procedureId);
|
||||
}
|
||||
procedureStruct = (FormData_pg_proc *)
|
||||
GETSTRUCT(procedureTuple);
|
||||
if (!procedureStruct->proistrusted) {
|
||||
*function = (func_ptr) NULL;
|
||||
*nargs = procedureStruct->pronargs;
|
||||
return;
|
||||
}
|
||||
language = procedureStruct->prolang;
|
||||
switch (language) {
|
||||
case INTERNALlanguageId:
|
||||
user_fn = fmgr_lookupByName(procedureStruct->proname.data);
|
||||
if (!user_fn)
|
||||
elog(WARN, "fmgr_info: function %s: not in internal table",
|
||||
procedureStruct->proname.data);
|
||||
break;
|
||||
case ClanguageId:
|
||||
user_fn = fmgr_dynamic(procedureId, nargs);
|
||||
break;
|
||||
case SQLlanguageId:
|
||||
user_fn = (func_ptr) NULL;
|
||||
*nargs = procedureStruct->pronargs;
|
||||
break;
|
||||
default:
|
||||
elog(WARN, "fmgr_info: function %d: unknown language %d",
|
||||
procedureId, language);
|
||||
}
|
||||
} else {
|
||||
user_fn = fcp->func;
|
||||
*nargs = fcp->nargs;
|
||||
}
|
||||
*function = user_fn;
|
||||
}
|
||||
|
||||
/*
|
||||
* fmgr - return the value of a function call
|
||||
*
|
||||
* If the function is a system routine, it's compiled in, so call
|
||||
* it directly.
|
||||
*
|
||||
* Otherwise pass it to the the appropriate 'language' function caller.
|
||||
*
|
||||
* Returns the return value of the invoked function if succesful,
|
||||
* 0 if unsuccessful.
|
||||
*/
|
||||
char *
|
||||
fmgr(Oid procedureId, ... )
|
||||
{
|
||||
va_list pvar;
|
||||
register i;
|
||||
int pronargs;
|
||||
FmgrValues values;
|
||||
func_ptr user_fn;
|
||||
bool isNull = false;
|
||||
|
||||
va_start(pvar, procedureId);
|
||||
|
||||
fmgr_info(procedureId, &user_fn, &pronargs);
|
||||
|
||||
if (pronargs > MAXFMGRARGS) {
|
||||
elog(WARN, "fmgr: function %d: too many arguments (%d > %d)",
|
||||
procedureId, pronargs, MAXFMGRARGS);
|
||||
}
|
||||
for (i = 0; i < pronargs; ++i)
|
||||
values.data[i] = va_arg(pvar, char *);
|
||||
va_end(pvar);
|
||||
|
||||
/* XXX see WAY_COOL_ORTHOGONAL_FUNCTIONS */
|
||||
return(fmgr_c(user_fn, procedureId, pronargs, &values,
|
||||
&isNull));
|
||||
}
|
||||
|
||||
/*
|
||||
* This is just a version of fmgr() in which the hacker can prepend a C
|
||||
* function pointer. This routine is not normally called; generally,
|
||||
* if you have all of this information you're likely to just jump through
|
||||
* the pointer, but it's available for use with macros in fmgr.h if you
|
||||
* want this routine to do sanity-checking for you.
|
||||
*
|
||||
* func_ptr, func_id, n_arguments, args...
|
||||
*/
|
||||
char *
|
||||
fmgr_ptr(func_ptr user_fn, Oid func_id, ...)
|
||||
{
|
||||
va_list pvar;
|
||||
register i;
|
||||
int n_arguments;
|
||||
FmgrValues values;
|
||||
bool isNull = false;
|
||||
|
||||
va_start(pvar, func_id);
|
||||
n_arguments = va_arg(pvar, int);
|
||||
if (n_arguments > MAXFMGRARGS) {
|
||||
elog(WARN, "fmgr_ptr: function %d: too many arguments (%d > %d)",
|
||||
func_id, n_arguments, MAXFMGRARGS);
|
||||
}
|
||||
for (i = 0; i < n_arguments; ++i)
|
||||
values.data[i] = va_arg(pvar, char *);
|
||||
va_end(pvar);
|
||||
|
||||
/* XXX see WAY_COOL_ORTHOGONAL_FUNCTIONS */
|
||||
return(fmgr_c(user_fn, func_id, n_arguments, &values,
|
||||
&isNull));
|
||||
}
|
||||
|
||||
/*
|
||||
* This routine is not well thought out. When I get around to adding a
|
||||
* function pointer field to FuncIndexInfo, it will be replace by calls
|
||||
* to fmgr_c().
|
||||
*/
|
||||
char *
|
||||
fmgr_array_args(Oid procedureId, int nargs, char *args[], bool *isNull)
|
||||
{
|
||||
func_ptr user_fn;
|
||||
int true_arguments;
|
||||
|
||||
fmgr_info(procedureId, &user_fn, &true_arguments);
|
||||
|
||||
/* XXX see WAY_COOL_ORTHOGONAL_FUNCTIONS */
|
||||
return
|
||||
(fmgr_c(user_fn,
|
||||
procedureId,
|
||||
true_arguments,
|
||||
(FmgrValues*)args,
|
||||
isNull));
|
||||
}
|
||||
Reference in New Issue
Block a user