dynwin32_xsb_i.h

00001 /* File:      dynwin32_xsb_i.h
00002 ** Author(s): Luis Castro
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1999, 2000
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: dynwin32_xsb_i.h,v 1.15 2005/11/29 00:02:16 tswift Exp $
00022 ** 
00023 */
00024 
00025 
00026 #include <stdio.h>
00027 #include <windows.h>
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 /* wind2unix.h must be included after sys/stat.h */
00031 #include "wind2unix.h"
00032 #include <errno.h>
00033 #include <string.h>
00034 
00035 #include "auxlry.h"
00036 #include "cell_xsb.h"
00037 #include "memory_xsb.h"
00038 #include "error_xsb.h"
00039 #include "inst_xsb.h"
00040 #include "psc_xsb.h"
00041 #include "string_xsb.h"
00042 #include "extensions_xsb.h"
00043 #include "xsb_config.h"
00044 
00045 #define BUFFEXTRA 1024
00046 
00047 extern char *xsb_config_file_gl;
00048 
00049 /*----------------------------------------------------------------------*/
00050 
00051 xsbBool dummy()
00052 {
00053     xsb_error("Trying to use an undefined foreign procedure");
00054     return FALSE;
00055 }
00056 
00057 /*----------------------------------------------------------------------*/
00058 
00059 // construct a path to config\bin\cfile_name.dll by removing "lib\xsb_configuration.P"
00060 // from xsb_config_file location and appending "bin\cfile_name.dll"
00061 static char *create_bin_dll_path(char *xsb_config_file_location, char *dll_file_name, int *dirlen){
00062   char *xsb_bin_dir;
00063   int char_count;
00064   char_count = strlen(xsb_config_file_location)-strlen("lib")-1-strlen("xsb_configuration.P");
00065   *dirlen = sizeof(char)*(char_count+strlen(dll_file_name)+5); // 5 stands for bin\\ + null
00066   xsb_bin_dir = mem_alloc(*dirlen,FOR_CODE_SPACE);
00067   strncpy(xsb_bin_dir, xsb_config_file_location,char_count);
00068   xsb_bin_dir[char_count]='\0';
00069   strcat(xsb_bin_dir, "bin");
00070   char_count += 3;
00071   xsb_bin_dir[char_count]=SLASH;
00072   xsb_bin_dir[char_count+1]='\0';
00073   strcat(xsb_bin_dir, dll_file_name);
00074   return xsb_bin_dir;
00075 }
00076 
00077 static byte *load_obj_dyn(char *pofilename, Psc cur_mod, char *ld_option)
00078 {
00079   char  *name;
00080 #ifdef XSB_DLL
00081   char tempname[128];
00082   int  tempsize;
00083 #endif
00084   Pair  search_ptr;
00085   char  sofilename[128];
00086   HMODULE handle;
00087   void  *funcep;
00088   char  *file_extension_ptr;
00089   xsbBool       dummy();
00090   char *basename_ptr;
00091   char *xsb_bin_dir;
00092   int dirlen;
00093   
00094   /* (1) create filename.so */
00095   
00096   strcpy(sofilename, pofilename);
00097 
00098   file_extension_ptr = xsb_strrstr(sofilename, XSB_OBJ_EXTENSION_STRING);
00099   /* replace the OBJ file suffix with the so suffix */
00100   strcpy(file_extension_ptr+1, "dll");
00101   
00102   /* (2) open the needed object */
00103   if (( handle = LoadLibrary(sofilename)) == 0 ) {
00104     // if DLL is not found in c file's path
00105     // look for it in bin path, if still not found
00106     // let OS find it
00107     basename_ptr = strrchr(sofilename, SLASH); // get \file.dll
00108     if(basename_ptr != NULL){
00109       basename_ptr = basename_ptr + 1;
00110       xsb_bin_dir = create_bin_dll_path(xsb_config_file_gl, basename_ptr,&dirlen);
00111       if(( handle = LoadLibrary(xsb_bin_dir)) == 0 ){
00112         if (( handle = LoadLibrary(basename_ptr)) == 0 ) {
00113           mem_dealloc(xsb_bin_dir,dirlen,FOR_CODE_SPACE);
00114           xsb_warn("Cannot load library %s or %s; error #%d",basename_ptr,sofilename,GetLastError());
00115           return 0;
00116         }
00117       }
00118       mem_dealloc(xsb_bin_dir,dirlen,FOR_CODE_SPACE);
00119     }
00120   }
00121   
00122   /* (3) find address of function and data objects
00123   **
00124   ** dyn_link_all(loc, cur_mod);
00125   */
00126   
00127   search_ptr = (Pair)get_data(cur_mod);
00128   
00129   while (search_ptr) {
00130     name = get_name(search_ptr->psc_ptr);
00131 #ifdef XSB_DLL
00132     tempname[0] = '_';
00133     /*    tempname[1] = '_'; */
00134     strcpy(tempname+1,name);
00135     tempsize=strlen(tempname);
00136     tempname[tempsize++] = '@';
00137     tempname[tempsize++] = '0';
00138     tempname[tempsize++] = '\0';
00139     name = tempname;
00140 #endif
00141     if (get_type(search_ptr->psc_ptr) == T_FORN) {
00142       if ((funcep = (int (*)) GetProcAddress(handle, name)) == NULL) {
00143         xsb_warn("Cannot find foreign procedure %s", name);
00144         set_forn(search_ptr->psc_ptr, (byte *)(dummy));
00145       } else { 
00146         set_forn(search_ptr->psc_ptr, (byte *)(funcep));
00147       }
00148       
00149     }
00150     search_ptr = search_ptr->next;
00151   }
00152   return (byte *)4;
00153 }
00154 
00155 
00156 
00157 

Generated on Wed Jul 26 13:30:40 2006 for XSB by  doxygen 1.4.5