1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// We have 4 different implementations here<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
// Windows, Unix, , HP-UX

#include "CubitDynamicLoader.hpp"

#include <vector>
#include <sys/stat.h>

#include "CubitMessage.hpp"
#include "CubitString.hpp"

static std::vector<CubitString> gSearchPaths;

void CubitDynamicLoader::add_search_path(const char* path)
{<--- The function 'add_search_path' is never used.
  gSearchPaths.push_back(CubitString(path));
}

static bool absolute_path(const char* path)
{
#ifdef _WIN32
  // either a '\' or a 'x:' drive letter means absolute path
  return path[0] == '\\' || (path[0] != '\0') ? (path[1] == ':') : 0;
#else
  // a '/' is always an absolute path
  return path[0] == '/';
#endif
}

CubitBoolean CubitDynamicLoader::library_exists(const char* library)
{
  struct stat buf;

  // if absolute path, test it directly
  if(absolute_path(library))
  {
    return stat(library, &buf) == 0 ? CUBIT_TRUE : CUBIT_FALSE;
  }
  else
  { 
    // try finding with our search paths
    for(int i=0; i<(int)gSearchPaths.size(); i++)
    {
      CubitString path = gSearchPaths[i] + CubitString("/") + library;
      if(stat(path.c_str(), &buf) == 0)
        return CUBIT_TRUE;
    }
  }
  
  // one more final attempt
  if (stat(library, &buf) == 0)
    return CUBIT_TRUE;

  return CUBIT_FALSE;
}

#ifdef _WIN32

CubitDynamicLoader::LibraryHandle CubitDynamicLoader::InvalidLibraryHandle = NULL;

CubitDynamicLoader::LibraryHandle CubitDynamicLoader::load_library(const char* lib)
{
  // disable message boxes about failure to find dlls
  //UINT old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
	UINT old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS);

  LibraryHandle handle = InvalidLibraryHandle;
  
  // if absolute path, test it directly
  if(absolute_path(lib))
  {
    handle = LoadLibraryW(CubitString::toUtf16(lib).c_str());
  }
  else
  { 
    // try finding with our search paths
    for(unsigned int i=0; i<gSearchPaths.size(); i++)
    {
      CubitString path = gSearchPaths[i]+CubitString("\\")+lib;
      handle = LoadLibraryW(CubitString::toUtf16(path.c_str()).c_str());
      if(handle != InvalidLibraryHandle)
      {
        SetErrorMode(old_error_mode);
        return handle;
      }
    }
  }
  
  // one more final attempt
  handle = LoadLibraryW(CubitString::toUtf16(lib).c_str());

  SetErrorMode(old_error_mode);

  return handle;
}

CubitString CubitDynamicLoader::get_error()
{
  LPWSTR lpMsgBuf = NULL;
  DWORD dw = GetLastError(); 

  FormatMessageW(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    dw,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    lpMsgBuf,
    0, NULL );

  CubitString error = CubitString::toUtf8(lpMsgBuf);
  LocalFree(lpMsgBuf);

  return error;
}

CubitStatus CubitDynamicLoader::unload_library(CubitDynamicLoader::LibraryHandle lib)
{
  return FreeLibrary(lib) == FALSE ? CUBIT_FAILURE : CUBIT_SUCCESS;
}

void* CubitDynamicLoader::get_symbol_address(CubitDynamicLoader::LibraryHandle lib, const char* symbol)
{
  return reinterpret_cast<void*>(GetProcAddress(lib, symbol));
}

const char* CubitDynamicLoader::library_prefix()
{
  return "";
}

const char* CubitDynamicLoader::library_extension()
{
  return ".dll";
}

#elif defined(__hpux)

CubitDynamicLoader::LibraryHandle CubitDynamicLoader::InvalidLibraryHandle = NULL;

CubitDynamicLoader::LibraryHandle CubitDynamicLoader::load_library(const char* lib)
{
  LibraryHandle handle = InvalidLibraryHandle;
  
  // if absolute path, test it directly
  if(absolute_path(lib))
  {
    handle = shl_load(lib, BIND_DEFERRED | DYNAMIC_PATH | BIND_NONFATAL, 0L);
  }
  else
  { 
    // try finding with our search paths
    for(int i=0; i<gSearchPaths.size(); i++)
    {
      CubitString path = gSearchPaths[i]+CubitString("/")+lib;
      handle = shl_load(path.c_str(), BIND_DEFERRED | DYNAMIC_PATH | BIND_NONFATAL, 0L);
      if(handle != InvalidLibraryHandle)
        return handle;
    }
  }
  
  // one more final attempt
  handle = shl_load(lib, BIND_DEFERRED | DYNAMIC_PATH | BIND_NONFATAL, 0L);
  return handle;
}

CubitString CubitDynamicLoader::get_error()
{
  return CubitString();
}

CubitStatus CubitDynamicLoader::unload_library(CubitDynamicLoader::LibraryHandle lib)
{
  return shl_unload(lib) == 0 ? CUBIT_SUCCESS : CUBIT_FAILURE;
}

void* CubitDynamicLoader::get_symbol_address(CubitDynamicLoader::LibraryHandle lib, const char* symbol)
{
  void* address;
  return shl_findsym(&lib, symbol, TYPE_PROCEDURE, &address) < 0 ? NULL : address;
}

const char* CubitDynamicLoader::library_prefix()
{
  return "lib";
}

const char* CubitDynamicLoader::library_extension()
{
  return ".sl";
}

#else

#include <dlfcn.h>

CubitDynamicLoader::LibraryHandle CubitDynamicLoader::InvalidLibraryHandle = NULL;

CubitDynamicLoader::LibraryHandle CubitDynamicLoader::load_library(const char* lib)
{
  // if absolute path, test it directly
  if(absolute_path(lib))
  {
    return dlopen(lib, RTLD_LAZY);
  }
  else
  { 
    // try finding with our search paths
    for(int i=0; i<(int)gSearchPaths.size(); i++)
    {
      CubitString path = gSearchPaths[i]+CubitString("/")+lib;
      LibraryHandle handle = dlopen(path.c_str(), RTLD_LAZY);
      if(handle != InvalidLibraryHandle)
        return handle;
    }
  }
  
  // one more final attempt
  return dlopen(lib, RTLD_LAZY );
}

CubitString CubitDynamicLoader::get_error()
{
  return dlerror();
}

CubitStatus CubitDynamicLoader::unload_library(CubitDynamicLoader::LibraryHandle lib)
{<--- The function 'unload_library' is never used.
  return dlclose(lib) == 0 ? CUBIT_SUCCESS : CUBIT_FAILURE;
}

void* CubitDynamicLoader::get_symbol_address(CubitDynamicLoader::LibraryHandle lib, const char* symbol)
{
  return dlsym(lib, symbol);
}

const char* CubitDynamicLoader::library_prefix()
{
  return "lib";
}

const char* CubitDynamicLoader::library_extension()
{
  return ".so";
}
#endif