1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2006 Carnegie Mellon University |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
Boston, MA 02111-1307, USA. |
18 |
*//** |
19 |
@file |
20 |
Handle the Import Handler library, which is a hash table of additional |
21 |
handlers for external scripts in the IMPORT statement. |
22 |
*//* |
23 |
by John Pye |
24 |
Created Sept 26, 2006 |
25 |
*/ |
26 |
|
27 |
#include "importhandler.h" |
28 |
|
29 |
#include <utilities/config.h> |
30 |
#include <utilities/error.h> |
31 |
#include <utilities/ascDynaLoad.h> |
32 |
#include <utilities/ascPanic.h> |
33 |
#include <utilities/ascEnvVar.h> |
34 |
#include <general/table.h> |
35 |
|
36 |
#include <string.h> |
37 |
|
38 |
#define SEARCH_DEBUG |
39 |
|
40 |
/* #define IMPORTHANDLER_VERBOSE */ |
41 |
|
42 |
/* |
43 |
Maximum number of importhandlers possible in one session. Hard to imagine |
44 |
that you would want more than this. |
45 |
*/ |
46 |
#define IMPORTHANDLER_MAX 10 |
47 |
|
48 |
/** |
49 |
List of import handlers currently in effect. @TODO this shouldn't be a global, |
50 |
but unfortunately such globals are 'The ASCEND Way'. |
51 |
*/ |
52 |
struct ImportHandler **importhandler_library=NULL; |
53 |
|
54 |
/** |
55 |
Table of registered pointers for use in passing GUI data out to external scripts. |
56 |
*/ |
57 |
struct Table *importhandler_sharedpointers=NULL; |
58 |
|
59 |
ASC_DLLSPEC int importhandler_add(struct ImportHandler *handler){ |
60 |
int i; |
61 |
if(handler==NULL){ |
62 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Handler is NULL"); |
63 |
return 2; |
64 |
} |
65 |
if(importhandler_library == NULL){ |
66 |
importhandler_createlibrary(); |
67 |
} |
68 |
for(i=0; i< IMPORTHANDLER_MAX; ++i){ |
69 |
if(importhandler_library[i] == NULL)break; |
70 |
if(importhandler_library[i]->name == handler->name){ |
71 |
ERROR_REPORTER_HERE(ASC_USER_NOTE,"Handler already loaded"); |
72 |
return 0; |
73 |
} |
74 |
} |
75 |
if(i==IMPORTHANDLER_MAX){ |
76 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Too many import handlers register (IMPORTHANDLER_MAX=%d)",IMPORTHANDLER_MAX); |
77 |
return 1; |
78 |
} |
79 |
importhandler_library[i] = handler; |
80 |
ERROR_REPORTER_HERE(ASC_USER_NOTE,"New import hander '%s' added",handler->name); |
81 |
return 0; |
82 |
} |
83 |
|
84 |
/* Function to attempt import of an external script |
85 |
@param partialname Name of the external script (without extension), relative to PATH. |
86 |
@param defaultpath Default value of file search PATH. Is trumped by value of pathenvvar if present in environment. |
87 |
@param pathenvvar Environment variable containing the user's preferred file search path value. |
88 |
@return 0 on success |
89 |
*/ |
90 |
int importhandler_attemptimport(const char *partialname,const char *defaultpath, const char *pathenvvar){ |
91 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"%s not implemented",__FUNCTION__); |
92 |
return 1; |
93 |
} |
94 |
|
95 |
/*------------------------------------------------------------------------------ |
96 |
DEFAULT IMPORT HANDLER FOR DLL/SO FILES |
97 |
*/ |
98 |
|
99 |
/** |
100 |
Create a filename for an external library (DLL/SO) based on a |
101 |
partial filename. |
102 |
|
103 |
@param partialname The partial filename (eg 'mylib') |
104 |
@return Complete filename (eg 'libmylib.so' or 'mylib.dlll', etc) |
105 |
*/ |
106 |
char *importhandler_extlib_filename(const char *partialname){ |
107 |
char *buffer; |
108 |
buffer = ASC_NEW_ARRAY(char,PATH_MAX); |
109 |
|
110 |
#if defined(ASC_SHLIBSUFFIX) && defined(ASC_SHLIBPREFIX) |
111 |
/* |
112 |
this is the preferred operation: SCons reports what the local system |
113 |
uses as its shared library file extension. |
114 |
*/ |
115 |
snprintf(buffer,PATH_MAX,"%s%s%s",ASC_SHLIBPREFIX,partialname,ASC_SHLIBSUFFIX); |
116 |
#else |
117 |
#ifdef __GNUC__ |
118 |
# warning "You should be using Use ASC_SHLIBPREFIX and ASC_SHLIBSUFFIX!" |
119 |
#endif |
120 |
/** |
121 |
@DEPRECATED |
122 |
|
123 |
If we don't have ASC_SHLIB-SUFFIX and -PREFIX then we can do some |
124 |
system-specific stuff here, but it's not as general. |
125 |
*/ |
126 |
# ifdef __WIN32__ |
127 |
snprintf(buffer,PATH_MAX,"%s.dll",partialname); |
128 |
# elif defined(linux) |
129 |
snprintf(buffer,PATH_MAX,"lib%s.so",partialname); /* changed from .o to .so -- JP */ |
130 |
# elif defined(sun) || defined(solaris) |
131 |
snprintf(buffer,PATH_MAX,"%s.so.1.0",partialname); |
132 |
# elif defined(__hpux) |
133 |
snprintf(buffer,PATH_MAX,"%s.sl",partialname); |
134 |
# elif defined(_SGI_SOURCE) |
135 |
snprintf(buffer,PATH_MAX,"%s.so",partialname); |
136 |
# else |
137 |
# error "Unknown system type (please define ASC_SHLIBSUFFIX and ASC_SHLIBPREFIX)" |
138 |
# endif |
139 |
#endif |
140 |
|
141 |
return buffer; |
142 |
} |
143 |
|
144 |
/** |
145 |
Perform the actual importing of an external DLL/SO in to ASCEND. Can assume |
146 |
that the file exists and is readable. |
147 |
|
148 |
@param fp Location of DLL/SO file |
149 |
@param initfunc Name of registration function, preferably NULL (so that ASCEND automatically determines it) |
150 |
@param partialpath as specified in 'IMPORT' statement, for creation of auto_initfunc name |
151 |
@return 0 on success |
152 |
*/ |
153 |
int importhandler_extlib_import(const struct FilePath *fp,const char *initfunc,const char *partialpath){ |
154 |
|
155 |
struct FilePath *fp1; |
156 |
char *stem; |
157 |
char *path; |
158 |
char auto_initfunc[PATH_MAX]; |
159 |
int result; |
160 |
|
161 |
path = ospath_str(fp); |
162 |
if(path==NULL){ |
163 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"File path is NULL"); |
164 |
return 1; |
165 |
} |
166 |
|
167 |
if(initfunc==NULL){ |
168 |
fp1 = ospath_new(partialpath); |
169 |
stem = ospath_getbasefilename(fp1); |
170 |
strncpy(auto_initfunc,stem,PATH_MAX); |
171 |
ospath_free(fp1); |
172 |
ASC_FREE(stem); |
173 |
|
174 |
strncat(auto_initfunc,"_register",PATH_MAX-strlen(auto_initfunc)); |
175 |
/* CONSOLE_DEBUG("Created auto-initfunc name '%s'",auto_initfunc); */ |
176 |
result = Asc_DynamicLoad(path,auto_initfunc); |
177 |
}else{ |
178 |
result = Asc_DynamicLoad(path,initfunc); |
179 |
} |
180 |
|
181 |
if(result){ |
182 |
CONSOLE_DEBUG("FAILED TO IMPORT '%s' (error %d)",partialpath,result); |
183 |
}else{ |
184 |
if(initfunc==NULL){ |
185 |
CONSOLE_DEBUG("Successfully ran '%s' (automatically named) from dynamic package '%s'",auto_initfunc,path); |
186 |
}else{ |
187 |
CONSOLE_DEBUG("Successfully ran '%s' from dynamic package '%s'",initfunc,path); |
188 |
} |
189 |
} |
190 |
|
191 |
ASC_FREE(path); |
192 |
return result; |
193 |
} |
194 |
|
195 |
/*------------------------------------------------------------------------------ |
196 |
LIST-BASED FUNCTIONS related to IMPORT handler 'library' |
197 |
*/ |
198 |
|
199 |
int importhandler_createlibrary(){ |
200 |
int i; |
201 |
struct ImportHandler *extlib_handler; |
202 |
|
203 |
if(importhandler_library!=NULL){ |
204 |
/* ERROR_REPORTER_HERE(ASC_PROG_ERR,"Already created"); */ |
205 |
return 0; |
206 |
}; |
207 |
importhandler_library=ASC_NEW_ARRAY(struct ImportHandler *,IMPORTHANDLER_MAX); |
208 |
for(i=0; i < IMPORTHANDLER_MAX; ++i){ |
209 |
importhandler_library[i] = NULL; |
210 |
} |
211 |
/* CONSOLE_DEBUG("ImportHandler library created"); */ |
212 |
|
213 |
extlib_handler = ASC_NEW(struct ImportHandler); |
214 |
extlib_handler->name ="extlib"; |
215 |
extlib_handler->filenamefn = &importhandler_extlib_filename; |
216 |
extlib_handler->importfn = &importhandler_extlib_import; |
217 |
if(importhandler_add(extlib_handler)){ |
218 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Failed to create 'extlib' import handler"); |
219 |
return 1; |
220 |
} |
221 |
|
222 |
return 0; |
223 |
} |
224 |
|
225 |
int importhandler_remove(const char *name){ |
226 |
if(importhandler_library==NULL)return 2; |
227 |
/* CONSOLE_DEBUG("Removing importhandler '%s'", name); */ |
228 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"%s not implemented",__FUNCTION__); |
229 |
return 1; |
230 |
} |
231 |
|
232 |
struct ImportHandler *importhandler_lookup(const char *name){ |
233 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"%s not implemented",__FUNCTION__); |
234 |
return NULL; |
235 |
} |
236 |
|
237 |
/** @return 0 on success */ |
238 |
int importhandler_destroylibrary(){ |
239 |
int i; |
240 |
int err = 0; |
241 |
CONSOLE_DEBUG("Destroying importhandler library..."); |
242 |
importhandler_printlibrary(stderr); |
243 |
if(importhandler_library!=NULL){ |
244 |
for(i=IMPORTHANDLER_MAX - 1; i >= 0; --i){ |
245 |
if(importhandler_library[i]==NULL)continue; |
246 |
err = err | importhandler_remove(importhandler_library[i]->name); |
247 |
} |
248 |
} |
249 |
if(err)ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Failed to destroy importhandler library"); |
250 |
return err; |
251 |
} |
252 |
|
253 |
/** @return 0 on success */ |
254 |
int importhandler_printlibrary(FILE *fp){ |
255 |
int i; |
256 |
if(importhandler_library==NULL){ |
257 |
fprintf(fp,"# importhandler_printlibrary: empty\n"); |
258 |
return 0; |
259 |
}else{ |
260 |
fprintf(fp,"# importhandler_printlibrary: start\n"); |
261 |
for(i=0; i < IMPORTHANDLER_MAX && importhandler_library[i] != NULL; ++i){ |
262 |
fprintf(fp,"%s\n",importhandler_library[i]->name); |
263 |
} |
264 |
fprintf(fp,"# importhandler_printlibrary: end\n"); |
265 |
return 0; |
266 |
} |
267 |
} |
268 |
|
269 |
int importhandler_printhandler(FILE *fp, struct ImportHandler *handler){ |
270 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"%s not implemented",__FUNCTION__); |
271 |
CONSOLE_DEBUG("NOT IMPLEMENTED"); |
272 |
return 1; |
273 |
} |
274 |
|
275 |
/*------------------------------------------------------------------------------ |
276 |
PATH SEARCH ROUTINES |
277 |
*/ |
278 |
|
279 |
/** |
280 |
A little structure to help with searching for import files |
281 |
|
282 |
@see test_importsearch |
283 |
*/ |
284 |
struct ImportHandlerSearch{ |
285 |
char *partialname; /**< for example 'myext' */ |
286 |
struct FilePath *relativedir; /**< for example 'path/to' */ |
287 |
struct FilePath *foundpath; /**< the complete filepath located, for example '/home/john/path/to/libmyext.so' */ |
288 |
struct ImportHandler *handler; /**< pointer to the import handler identified for this file */ |
289 |
}; |
290 |
|
291 |
FilePathTestFn importhandler_search_test; |
292 |
|
293 |
/** |
294 |
A FilePath 'test' function for passing to the ospath_searchpath_iterate function. |
295 |
This test function will return a match when an importable file having the |
296 |
required name is present in the fully resolved path. |
297 |
|
298 |
@param path the search path component |
299 |
@param userdata will be an ImportHandlerSearch object |
300 |
@return 1 if found |
301 |
*/ |
302 |
int importhandler_search_test(struct FilePath *path, void *userdata){ |
303 |
/* user data = the relative path, plus a place |
304 |
to store the full path when found */ |
305 |
FILE *f; |
306 |
char *filename; |
307 |
#ifdef SEARCH_DEBUG |
308 |
char *fullpath; |
309 |
#endif |
310 |
struct ImportHandlerSearch *searchdata; |
311 |
struct FilePath *fp, *fp1, *fp2; |
312 |
int i; |
313 |
ospath_stat_t buf; |
314 |
|
315 |
searchdata = (struct ImportHandlerSearch *)userdata; |
316 |
|
317 |
#ifdef SEARCH_DEBUG |
318 |
char *pathcomponent; |
319 |
pathcomponent = ospath_str(path); //eg '/home/john' |
320 |
CONSOLE_DEBUG("In directory '%s'...",pathcomponent); |
321 |
ASC_FREE(pathcomponent); |
322 |
#endif |
323 |
|
324 |
asc_assert(importhandler_library!=NULL); |
325 |
|
326 |
for(i=0; i<IMPORTHANDLER_MAX && importhandler_library[i]!=NULL; ++i){ |
327 |
|
328 |
filename = (*(importhandler_library[i]->filenamefn))(searchdata->partialname); /* eg 'myext' -> 'libmyext.so' */ |
329 |
if(filename==NULL){ |
330 |
CONSOLE_DEBUG("Unable to create filename from partialname '%s'",searchdata->partialname); |
331 |
continue; |
332 |
} |
333 |
/* CONSOLE_DEBUG("Filename '%s'",filename); */ |
334 |
fp = ospath_new_noclean(filename); /* eg 'libmyext.so' */ |
335 |
ASC_FREE(filename); |
336 |
asc_assert(fp!=NULL); |
337 |
|
338 |
#ifdef SEARCH_DEBUG |
339 |
fullpath = ospath_str(searchdata->relativedir); |
340 |
CONSOLE_DEBUG("Relative dir is '%s'",fullpath); |
341 |
ASC_FREE(fullpath); |
342 |
#endif |
343 |
fp1 = ospath_concat(path,searchdata->relativedir); /* eg '/home/john/path/to' */ |
344 |
asc_assert(fp1!=NULL); |
345 |
|
346 |
|
347 |
#ifdef SEARCH_DEBUG |
348 |
fullpath = ospath_str(fp1); |
349 |
CONSOLE_DEBUG("Path is '%s'",fullpath); |
350 |
ASC_FREE(fullpath); |
351 |
|
352 |
fullpath = ospath_str(fp); |
353 |
CONSOLE_DEBUG("Filename is '%s'",fullpath); |
354 |
ASC_FREE(fullpath); |
355 |
#endif |
356 |
|
357 |
fp2 = ospath_concat(fp1,fp); /* eg '/home/john/path/to/libmyext.so' */ |
358 |
asc_assert(fp2!=NULL); |
359 |
ospath_free(fp1); |
360 |
ospath_free(fp); |
361 |
|
362 |
#ifdef SEARCH_DEBUG |
363 |
fullpath = ospath_str(fp2); |
364 |
CONSOLE_DEBUG("Checking for readable '%s'",fullpath); |
365 |
ASC_FREE(fullpath); |
366 |
#endif |
367 |
|
368 |
if(0==ospath_stat(fp2,&buf) && NULL!=(f = ospath_fopen(fp2,"r"))){ |
369 |
fclose(f); |
370 |
searchdata->foundpath = fp2; |
371 |
searchdata->handler = importhandler_library[i]; |
372 |
return 1; /* success */ |
373 |
} |
374 |
|
375 |
ospath_free(fp2); |
376 |
} |
377 |
return 0; /* failed */ |
378 |
} |
379 |
|
380 |
struct FilePath *importhandler_findinpath(const char *partialname |
381 |
, char *defaultpath, char *envv, struct ImportHandler **handler |
382 |
){ |
383 |
struct FilePath *fp, *fp1; /* relative path */ |
384 |
struct ImportHandlerSearch searchdata; |
385 |
char *path, *filename; |
386 |
struct FilePath **sp; |
387 |
int i; |
388 |
ospath_stat_t buf; |
389 |
FILE *f; |
390 |
|
391 |
fp1 = ospath_new_noclean(partialname); /* eg 'path/to/myext' */ |
392 |
if(fp1==NULL){ |
393 |
ERROR_REPORTER_HERE(ASC_USER_ERROR,"Invalid partial path '%s'",partialname); |
394 |
return NULL; |
395 |
} |
396 |
|
397 |
searchdata.partialname = ospath_getbasefilename(fp1); |
398 |
if(searchdata.partialname==NULL){ |
399 |
CONSOLE_DEBUG("Not a filename"); |
400 |
ospath_free(fp1); |
401 |
return NULL; |
402 |
} |
403 |
|
404 |
searchdata.relativedir = ospath_getdir(fp1); |
405 |
if(searchdata.relativedir ==NULL){ |
406 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"unable to retrieve file dir"); |
407 |
ospath_free(fp1); |
408 |
ASC_FREE(searchdata.partialname); |
409 |
return NULL; |
410 |
} |
411 |
ospath_free(fp1); |
412 |
|
413 |
searchdata.foundpath = NULL; |
414 |
searchdata.handler = NULL; |
415 |
|
416 |
/* first, attempt to open without searching in path */ |
417 |
|
418 |
for(i=0; i<IMPORTHANDLER_MAX && importhandler_library[i]!=NULL; ++i){ |
419 |
|
420 |
filename = (*(importhandler_library[i]->filenamefn))(searchdata.partialname); /* eg 'myext' -> 'libmyext.so' */ |
421 |
if(filename==NULL){ |
422 |
CONSOLE_DEBUG("Unable to create filename from partialname '%s'",searchdata.partialname); |
423 |
continue; |
424 |
} |
425 |
|
426 |
fp = ospath_new(filename); /* eg 'libmyext.so' */ |
427 |
ASC_FREE(filename); |
428 |
asc_assert(fp!=NULL); |
429 |
|
430 |
#ifdef SEARCH_DEBUG |
431 |
path = ospath_str(searchdata.relativedir); |
432 |
CONSOLE_DEBUG("Relative dir is '%s'",path); |
433 |
ASC_FREE(path); |
434 |
|
435 |
path = ospath_str(fp); |
436 |
CONSOLE_DEBUG("Filename is '%s'",path); |
437 |
ASC_FREE(path); |
438 |
#endif |
439 |
|
440 |
fp1 = ospath_concat(searchdata.relativedir,fp); /* eg '/home/john/path/to/libmyext.so' */ |
441 |
asc_assert(fp1!=NULL); |
442 |
ospath_free(fp); |
443 |
|
444 |
#ifdef SEARCH_DEBUG |
445 |
path = ospath_str(fp1); |
446 |
CONSOLE_DEBUG("Checking for readable '%s'",path); |
447 |
ASC_FREE(path); |
448 |
#endif |
449 |
|
450 |
if(0==ospath_stat(fp1,&buf) && NULL!=(f = ospath_fopen(fp1,"r"))){ |
451 |
CONSOLE_DEBUG("FOUND!"); |
452 |
fclose(f); |
453 |
ASC_FREE(searchdata.partialname); |
454 |
ospath_free(searchdata.relativedir); |
455 |
*handler = importhandler_library[i]; |
456 |
return fp1; |
457 |
} |
458 |
#ifdef SEARCH_DEBUG |
459 |
else{ |
460 |
CONSOLE_DEBUG("Not found"); |
461 |
} |
462 |
#endif |
463 |
|
464 |
ospath_free(fp1); |
465 |
} |
466 |
|
467 |
/*-----------------------*/ |
468 |
|
469 |
path=Asc_GetEnv(envv); |
470 |
if(path==NULL){ |
471 |
CONSOLE_DEBUG("ENV VAR NOT FOUND, FALLING BACK TO DEFAULT SEARCH PATH = '%s'",defaultpath); |
472 |
path=defaultpath; |
473 |
} |
474 |
|
475 |
/* CONSOLE_DEBUG("SEARCHPATH IS %s",path); */ |
476 |
sp = ospath_searchpath_new(path); |
477 |
|
478 |
if(NULL==ospath_searchpath_iterate(sp,&importhandler_search_test,&searchdata)){ |
479 |
ospath_free(searchdata.relativedir); |
480 |
ASC_FREE(searchdata.partialname); |
481 |
ospath_searchpath_free(sp); |
482 |
return NULL; |
483 |
} |
484 |
|
485 |
ospath_searchpath_free(sp); |
486 |
ASC_FREE(searchdata.partialname); |
487 |
ospath_free(searchdata.relativedir); |
488 |
*handler = searchdata.handler; |
489 |
return searchdata.foundpath; |
490 |
} |
491 |
|
492 |
/*------------------------------------------------------------------------------ |
493 |
SHARED POINTER TABLE |
494 |
*/ |
495 |
|
496 |
int importhandler_createsharedpointertable(){ |
497 |
if(importhandler_sharedpointers==NULL){ |
498 |
/* CONSOLE_DEBUG("CREATED SHARED POINTER TABLE"); */ |
499 |
importhandler_sharedpointers = CreateTable(31); |
500 |
} |
501 |
return 0; |
502 |
} |
503 |
|
504 |
int importhandler_setsharedpointer(const char *key, void *ptr){ |
505 |
importhandler_createsharedpointertable(); |
506 |
if(key==NULL){ |
507 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"key is NULL"); |
508 |
return 1; |
509 |
} |
510 |
|
511 |
/* woops! 'AddTableData' does *not* overwrite table entries! */ |
512 |
RemoveTableData(importhandler_sharedpointers,(char *)key); |
513 |
|
514 |
AddTableData(importhandler_sharedpointers,ptr,key); |
515 |
#ifdef IMPORTHANDLER_VERBOSE |
516 |
CONSOLE_DEBUG("Set shared pointer '%s' to %p",key, ptr); |
517 |
#endif |
518 |
asc_assert(importhandler_getsharedpointer(key)==ptr); |
519 |
return 0; |
520 |
} |
521 |
|
522 |
void *importhandler_getsharedpointer(const char *key){ |
523 |
importhandler_createsharedpointertable(); |
524 |
if(key==NULL){ |
525 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"key is NULL"); |
526 |
return NULL; |
527 |
} |
528 |
return LookupTableData(importhandler_sharedpointers,key); |
529 |
} |