00001
00021 #include "mainInt.h"
00022 #include <stdio.h>
00023 #include <string.h>
00024
00025 #ifndef WIN32
00026 # include <sys/types.h>
00027 # include <dirent.h>
00028 # include <dlfcn.h>
00029 #endif
00030
00031 #define MAX_LIBS 256
00032 static void* libHandles[MAX_LIBS+1];
00033
00034 typedef void (*lib_init_end_func) (Abc_Frame_t * pAbc);
00035
00037
00039 void open_libs() {
00040 int curr_lib = 0;
00041
00042 #ifdef WIN32
00043
00044 #else
00045 DIR* dirp;
00046 struct dirent* dp;
00047 char *env, *init_p, *p;
00048 int done;
00049
00050 env = getenv ("ABC_LIB_PATH");
00051 if (env == NULL) {
00052
00053 init_p = malloc (2*sizeof(char));
00054 init_p[0]='.'; init_p[1] = 0;
00055 } else {
00056 init_p = malloc ((strlen(env)+1)*sizeof(char));
00057 strcpy (init_p, env);
00058 }
00059
00060
00061 done = 0;
00062 p = init_p;
00063 while (!done) {
00064 char *endp = strchr (p,':');
00065 if (endp == NULL) done = 1;
00066 else *endp = 0;
00067
00068 dirp = opendir(p);
00069 if (dirp == NULL) {
00070
00071 continue;
00072 }
00073
00074 while ((dp = readdir(dirp)) != NULL) {
00075 if ((strncmp("libabc_", dp->d_name, 7) == 0) &&
00076 (strcmp(".so", dp->d_name + strlen(dp->d_name) - 3) == 0)) {
00077
00078
00079 if (curr_lib >= MAX_LIBS) {
00080 printf("Warning: maximum number of ABC libraries (%d) exceeded. Not loading %s.\n",
00081 MAX_LIBS,
00082 dp->d_name);
00083 }
00084
00085
00086 else {
00087 char* szPrefixed = malloc((strlen(dp->d_name) + strlen(p) + 2) *
00088 sizeof(char));
00089 sprintf(szPrefixed, "%s/", p);
00090 strcat(szPrefixed, dp->d_name);
00091 libHandles[curr_lib] = dlopen(szPrefixed, RTLD_NOW | RTLD_LOCAL);
00092
00093
00094 if (libHandles[curr_lib] != 0) {
00095 printf("Loaded ABC library: %s (Abc library extension #%d)\n", szPrefixed, curr_lib);
00096 curr_lib++;
00097 } else {
00098 printf("Warning: failed to load ABC library %s:\n\t%s\n", szPrefixed, dlerror());
00099 }
00100
00101 free(szPrefixed);
00102 }
00103 }
00104 }
00105 closedir(dirp);
00106 p = endp+1;
00107 }
00108
00109 free(init_p);
00110 #endif
00111
00112
00113 libHandles[curr_lib] = 0;
00114 }
00115
00117
00119 void close_libs() {
00120 #ifdef WIN32
00121 printf("Warning: close_libs WIN32 not implemented.\n");
00122 #else
00123 int i;
00124 for (i = 0; libHandles[i] != 0; i++) {
00125 if (dlclose(libHandles[i]) != 0) {
00126 printf("Warning: failed to close library %d\n", i);
00127 }
00128 libHandles[i] = 0;
00129 }
00130 #endif
00131 }
00132
00134
00136 void* get_fnct_ptr(int lib_num, char* sym_name) {
00137 #ifdef WIN32
00138 printf("Warning: get_fnct_ptr WIN32 not implemented.\n");
00139 return 0;
00140 #else
00141 return dlsym(libHandles[lib_num], sym_name);
00142 #endif
00143 }
00144
00146
00148 void call_inits(Abc_Frame_t* pAbc) {
00149 int i;
00150 lib_init_end_func init_func;
00151 for (i = 0; libHandles[i] != 0; i++) {
00152 init_func = (lib_init_end_func) get_fnct_ptr(i, "abc_init");
00153 if (init_func == 0) {
00154 printf("Warning: Failed to initialize library %d.\n", i);
00155 } else {
00156 (*init_func)(pAbc);
00157 }
00158 }
00159 }
00160
00162
00164 void call_ends(Abc_Frame_t* pAbc) {
00165 int i;
00166 lib_init_end_func end_func;
00167 for (i = 0; libHandles[i] != 0; i++) {
00168 end_func = (lib_init_end_func) get_fnct_ptr(i, "abc_end");
00169 if (end_func == 0) {
00170 printf("Warning: Failed to end library %d.\n", i);
00171 } else {
00172 (*end_func)(pAbc);
00173 }
00174 }
00175 }
00176
00177 void Libs_Init(Abc_Frame_t * pAbc)
00178 {
00179 open_libs();
00180 call_inits(pAbc);
00181 }
00182
00183 void Libs_End(Abc_Frame_t * pAbc)
00184 {
00185 call_ends(pAbc);
00186
00187
00188
00189 }
00190