/* * m_vpplug.c * * Manejo de Plug-ins (.dll en win32, .so en unix) para parametros * virtuales * spd@daphne.cps.unizar.es * Ultima modificacion: Sat Oct 4 23:12:26 GMT--1:00 1997 */ #include #include #ifndef WIN32 # include # include # if defined(DIRENT) # include typedef struct dirent dir_type; # else # include typedef struct direct dir_type; # endif # include /* chdir */ # include # include #else # include # include # include # include # ifdef MAXPATHLEN # undefine MAXPATHLEN # endif # define MAXPATHLEN _MAX_PATH # define getcwd _getcwd # define chdir _chdir #endif #include "vpplug.h" #include "m_vpplug.h" /* * Globales */ M_PLUG *vp_plug; unsigned vp_plug_idx; /* */ int m_vpplug_init(void); { char *vppath, *path, *argt, po; char cwd[MAXPATHLEN]; vp_plug_idx=0; vp_plug=NULL; if ((vppath=getenv(M_VPPATH))==NULL) return M_VPERR_NOPATH; if ((path = strdup(vppath))==NULL) return M_VPERR_NOMEM; if (getcwd(cwd, sizeof(cwd)) != NULL) { argt = strtok( path, PATH_LIM ); while(argt!=NULL) { if ( -1 != chdir( argt )) { #ifndef WIN32 DIR *dir; dir_type *sd; char *po; if ( NULL == (dir = opendir( "." ))) { perror("opendir"); continue; } else { while ( NULL != ( sd = readdir( dir ))) { po=strrchr(sd->d_name, '.'); if ( (po != NULL) && ( 0 == strcmp ( po, PLUG_EXT )) ) if ( 0!= m_vpplug_add(sd->d_name,-1)) fprintf(stderr, "Can't load %s", sd->d_name); } closedir( dir ); } #else struct _finddata_t c_file; long hFile; /* Find first file in current directory */ if( (hFile = _findfirst( "*"PLUG_EXT, &c_file )) == -1L ) continue; else { do { if ( !( c_file.attrib & _A_SUBDIR )) if ( 0!= m_vpplug_add(c_file.name,-1)) fprintf(stderr, "Can't load %s", c_file.name); } while( _findnext( hFile, &c_file ) == 0 ); _findclose( hFile ); } #endif } argt = strtok( NULL, PATH_LIM ); } chdir(cwd); } else perror("getcwd"); return M_VPERR_NOERR; } int m_vpplug_add(char *path, int which); { int result=M_VPERR_NOERR; #ifndef WIN32 void *hLib; #else HINSTANCE hLib; # define dlsym GetProcAddress #endif char **name; short *magic; VP_PTR fun; VP_INIT init; if (NULL==(hLib= #ifndef WIN32 dlopen(path, RTLD_NOW) #else LoadLibrary( path ) #endif )) return 1; if ( which >= vp_plug_idx ) return M_VPERR_BADPAR; if ( which < 0 ) which = vp_plug_idx; if (!( (NULL==(magic=(short *)dlsym(hLib, VPPLUGIN_MAGIC))) || ( *magic != VPPLUGIN_MAGIC_VALUE ) || (NULL==(name=(char **)dlsym(hLib, VPPLUGIN_NAME))) || (NULL==(fun=(VP_PTR)dlsym(hLib, VPPLUGIN_MAIN))) )) { if (( which == vp_plug_idx ) && ( vp_plug_idx % M_PLUG_ADD == 0)) vp_plug = realloc ( vp_plug, sizeof(M_PLUG)*M_PLUG_ADD )); if ( vp_plug != NULL ) { vp_plug[which].type=M_PLUG_T_VP; vp_plug[which].name=*name; vp_plug[which].function=fun; vp_plug[which].path=strdup(path); vp_plug[which].lib=hLib; if ( which == vp_plug_idx ) ++vp_plug_idx; } else result=M_VPERR_NOMEM; if (NULL!=(init=(VP_INIT)dlsym(hLib, VPPLUGIN_INIT))) init(); } return result; } int m_vpplug_free(int which); { VP_INIT flush; if (NULL!=(flush=(VP_INIT)dlsym(hLib, VPPLUGIN_FLUSH))) flush(); vp_plug[which].function=NULL; if (vp_plug[which].path) free(vp_plug[which].path); #ifndef WIN32 dlclose(vp_plug[which].lib); #else FreeLibrary(vp_plug[which].lib); #endif return M_VPERR_NOMEM; } int m_vpplug_reload(int which) { char path[MAXPATHLEN]; strcpy(path, vp_plug[which].path) m_vpplug_free(which); return m_vpplug_add( path, which ); }