1 |
johnpye |
529 |
/* ASCEND modelling environment |
2 |
|
|
Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly, Kirk Abbott. |
3 |
|
|
Copyright (C) 2006 Carnegie Mellon University |
4 |
aw0a |
1 |
|
5 |
johnpye |
529 |
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2, or (at your option) |
8 |
|
|
any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 59 Temple Place - Suite 330, |
18 |
|
|
Boston, MA 02111-1307, USA. |
19 |
|
|
*//** |
20 |
johnpye |
697 |
@file |
21 |
johnpye |
62 |
Code to support dynamic and static loading of user packages. |
22 |
|
|
|
23 |
johnpye |
1162 |
'Packages' are bits of code that add functionality to ASCEND, like 'plugins' |
24 |
|
|
or 'addins' on other projects. In ASCEND we have a few that are pretty much |
25 |
|
|
essential -- the 'built in' packges. They are only packages in the sense |
26 |
|
|
that a 'register' function must be called to connect them correctly with |
27 |
|
|
the rest of the system. |
28 |
jpye |
1524 |
|
29 |
johnpye |
1162 |
Next there are the 'static' packages. These ones are linked into |
30 |
johnpye |
62 |
The default state is to have packages. As such it takes an explicit |
31 |
|
|
definition of NO_PACKAGES, if packages are not to be handled. |
32 |
|
|
An explicit definition of STATIC_PACKAGES or DYNAMIC_PACKAGES is also |
33 |
|
|
required. |
34 |
johnpye |
529 |
*//* |
35 |
|
|
by Kirk Abbott |
36 |
|
|
Created: July 4, 1994 |
37 |
johnpye |
697 |
Last in CVS: 1.14 ballan 1998/03/06 15:47:14 |
38 |
johnpye |
62 |
*/ |
39 |
johnpye |
529 |
|
40 |
aw0a |
1 |
#include <math.h> |
41 |
|
|
#include <ctype.h> /* was compiler/actype.h */ |
42 |
johnpye |
542 |
|
43 |
johnpye |
399 |
#include <utilities/ascConfig.h> |
44 |
johnpye |
542 |
#include <utilities/config.h> /* NEW */ |
45 |
|
|
|
46 |
jpye |
1519 |
#ifndef ASC_DEFAULT_ASCENDLIBRARY |
47 |
|
|
# error "Where is ASC_DEFAULT_ASCENDLIBRARY???" |
48 |
johnpye |
542 |
#endif |
49 |
|
|
|
50 |
|
|
#include <general/ospath.h> |
51 |
|
|
|
52 |
johnpye |
1210 |
|
53 |
johnpye |
399 |
#include <utilities/ascMalloc.h> |
54 |
johnpye |
700 |
#include <utilities/ascEnvVar.h> |
55 |
johnpye |
864 |
#include <compiler/importhandler.h> |
56 |
|
|
#include <utilities/ascPanic.h> |
57 |
johnpye |
399 |
#include <general/list.h> |
58 |
|
|
#include "symtab.h" |
59 |
johnpye |
1211 |
|
60 |
|
|
|
61 |
johnpye |
399 |
#include "functype.h" |
62 |
johnpye |
669 |
#include "expr_types.h" |
63 |
johnpye |
399 |
#include "extcall.h" |
64 |
|
|
#include "mathinst.h" |
65 |
|
|
#include "instance_enum.h" |
66 |
|
|
#include "instquery.h" |
67 |
|
|
#include "atomvalue.h" |
68 |
|
|
#include "find.h" |
69 |
johnpye |
908 |
#include "rel_blackbox.h" |
70 |
|
|
#include "vlist.h" |
71 |
johnpye |
399 |
#include "relation.h" |
72 |
|
|
#include "safe.h" |
73 |
|
|
#include "relation_util.h" |
74 |
|
|
#include "extfunc.h" |
75 |
|
|
#include <packages/sensitivity.h> |
76 |
|
|
#include <packages/ascFreeAllVars.h> |
77 |
johnpye |
1289 |
#include <packages/defaultall.h> |
78 |
johnpye |
399 |
#include "module.h" |
79 |
|
|
#include "packages.h" |
80 |
jpye |
1616 |
#include "defaultpaths.h" |
81 |
aw0a |
1 |
|
82 |
johnpye |
62 |
/* |
83 |
|
|
Initialise the slv data structures used when calling external fns |
84 |
|
|
*/ |
85 |
johnpye |
908 |
void Init_BBoxInterp(struct BBoxInterp *interp) |
86 |
aw0a |
1 |
{ |
87 |
johnpye |
908 |
if (interp){ |
88 |
|
|
interp->status = calc_all_ok; |
89 |
|
|
interp->user_data = NULL; |
90 |
|
|
interp->task = bb_none; |
91 |
aw0a |
1 |
} |
92 |
|
|
} |
93 |
|
|
|
94 |
johnpye |
62 |
/*--------------------------------------------- |
95 |
|
|
BUILT-IN PACKAGES... |
96 |
|
|
*/ |
97 |
|
|
|
98 |
|
|
/** |
99 |
jpye |
1519 |
Load builtin packages (those that are compiled into libascend) |
100 |
johnpye |
62 |
|
101 |
|
|
@return 0 if success, 1 if failure. |
102 |
|
|
*/ |
103 |
|
|
static |
104 |
johnpye |
1132 |
int Builtins_Init(void){ |
105 |
johnpye |
62 |
int result = 0; |
106 |
|
|
|
107 |
johnpye |
804 |
/* ERROR_REPORTER_DEBUG("Loading function asc_free_all_variables\n"); */ |
108 |
johnpye |
870 |
result = CreateUserFunctionMethod("asc_free_all_variables" |
109 |
|
|
,Asc_FreeAllVars |
110 |
|
|
,1 /* num of args */ |
111 |
|
|
,"Unset 'fixed' flag of all items of type 'solver_var'" /* help */ |
112 |
|
|
,NULL /* user_data */ |
113 |
johnpye |
912 |
,NULL /* destroy fn */ |
114 |
johnpye |
870 |
); |
115 |
johnpye |
1289 |
|
116 |
johnpye |
1317 |
/* ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_self"); */ |
117 |
jpye |
1372 |
result = CreateUserFunctionMethod("defaultself_visit_childatoms" |
118 |
|
|
,defaultself_visit_childatoms |
119 |
johnpye |
1289 |
,1 /* num of args */ |
120 |
jpye |
1372 |
,"Set local child atoms to their ATOMs' DEFAULT values; recurse into arrays." /* help */ |
121 |
johnpye |
1289 |
,NULL /* user_data */ |
122 |
|
|
,NULL /* destroy fn */ |
123 |
|
|
); |
124 |
johnpye |
1291 |
|
125 |
johnpye |
1317 |
/* ERROR_REPORTER_DEBUG("Registering EXTERNAL asc_default_all"); */ |
126 |
jpye |
1372 |
result = CreateUserFunctionMethod("defaultself_visit_submodels" |
127 |
|
|
,defaultself_visit_submodels |
128 |
johnpye |
1291 |
,1 /* num of args */ |
129 |
jpye |
1372 |
,"Call 'default_self' methods on any nested sub-models." /* help */ |
130 |
johnpye |
1291 |
,NULL /* user_data */ |
131 |
|
|
,NULL /* destroy fn */ |
132 |
|
|
); |
133 |
jpye |
1519 |
|
134 |
johnpye |
62 |
return result; |
135 |
|
|
} |
136 |
|
|
|
137 |
johnpye |
864 |
/* return 0 on success */ |
138 |
jpye |
1519 |
int package_load(CONST char *partialpath, CONST char *initfunc){ |
139 |
aw0a |
1 |
|
140 |
johnpye |
542 |
struct FilePath *fp1; |
141 |
|
|
int result; |
142 |
johnpye |
864 |
struct ImportHandler *handler=NULL; |
143 |
jpye |
1616 |
static char *default_solvers_path = NULL; |
144 |
|
|
static char *default_library_path = NULL; |
145 |
|
|
if(!default_solvers_path){ |
146 |
|
|
default_solvers_path = get_default_solvers_path(); |
147 |
|
|
CONSOLE_DEBUG("Default ASCENDSOLVERS set to '%s'", default_solvers_path); |
148 |
|
|
} |
149 |
|
|
if(!default_library_path){ |
150 |
|
|
default_library_path = get_default_library_path(); |
151 |
|
|
CONSOLE_DEBUG("Default ASCENDLIBRARY set to '%s'", default_library_path); |
152 |
|
|
} |
153 |
johnpye |
62 |
|
154 |
jpye |
1616 |
|
155 |
jpye |
1359 |
/* CONSOLE_DEBUG("Searching for external library '%s'",partialpath); */ |
156 |
johnpye |
62 |
|
157 |
johnpye |
864 |
importhandler_createlibrary(); |
158 |
|
|
|
159 |
jpye |
1519 |
/* search in the ASCENDSOLVERS directory/ies first */ |
160 |
johnpye |
864 |
fp1 = importhandler_findinpath( |
161 |
jpye |
1616 |
partialpath, default_solvers_path, ASC_ENV_SOLVERS,&handler |
162 |
johnpye |
864 |
); |
163 |
jpye |
1519 |
|
164 |
|
|
/* next, search in the ASCENDLIBRARY */ |
165 |
johnpye |
864 |
if(fp1==NULL){ |
166 |
jpye |
1519 |
fp1 = importhandler_findinpath( |
167 |
jpye |
1616 |
partialpath, default_library_path, ASC_ENV_LIBRARY,&handler |
168 |
jpye |
1519 |
); |
169 |
|
|
if(fp1==NULL){ |
170 |
|
|
CONSOLE_DEBUG("External library '%s' not found",partialpath); |
171 |
|
|
ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"External library '%s' not found.",partialpath); |
172 |
|
|
return 1; /* failure */ |
173 |
|
|
} |
174 |
johnpye |
542 |
} |
175 |
johnpye |
380 |
|
176 |
johnpye |
864 |
asc_assert(handler!=NULL); |
177 |
jpye |
1524 |
|
178 |
johnpye |
888 |
/* CONSOLE_DEBUG("About to import external library..."); */ |
179 |
jpye |
1519 |
|
180 |
johnpye |
864 |
/* note the import handler will deal with all the initfunc execution, etc etc */ |
181 |
|
|
result = (*(handler->importfn))(fp1,initfunc,partialpath); |
182 |
|
|
if(result){ |
183 |
jpye |
1512 |
//CONSOLE_DEBUG("Error %d when importing external library of type '%s'",result,handler->name); |
184 |
johnpye |
864 |
ERROR_REPORTER_HERE(ASC_PROG_ERROR,"Error importing external library '%s'",partialpath); |
185 |
johnpye |
542 |
ospath_free(fp1); |
186 |
|
|
return 1; |
187 |
|
|
} |
188 |
aw0a |
1 |
|
189 |
johnpye |
542 |
ospath_free(fp1); |
190 |
johnpye |
864 |
return 0; |
191 |
aw0a |
1 |
} |
192 |
|
|
|
193 |
johnpye |
62 |
/*--------------------------------------------- |
194 |
|
|
STATIC_PACKAGES code only... |
195 |
aw0a |
1 |
|
196 |
johnpye |
62 |
Declare the functions which we are expected to be able to call. |
197 |
|
|
*/ |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
This is a general purpose function that will load whatever user |
201 |
|
|
functions are required according to the compile-time settings. |
202 |
johnpye |
190 |
|
203 |
johnpye |
62 |
If NO_PACKAGES, nothing will be loaded. If DYNAMIC_PACKAGES, then |
204 |
|
|
just the builtin packages will be loaded. If STATIC_PACKAGES then |
205 |
|
|
builtin plus those called in 'StaticPackages_Init' will be loaded. |
206 |
|
|
*/ |
207 |
johnpye |
1162 |
void AddUserFunctions(void){ |
208 |
|
|
|
209 |
|
|
/* Builtins are always statically linked */ |
210 |
|
|
if (Builtins_Init()) { |
211 |
|
|
ERROR_REPORTER_NOLINE(ASC_PROG_WARNING |
212 |
|
|
,"Problem in Builtins_Init: Some user functions not created" |
213 |
|
|
); |
214 |
|
|
} |
215 |
aw0a |
1 |
} |
216 |
|
|
|