/[ascend]/trunk/base/generic/compiler/packages.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/packages.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1162 - (show annotations) (download) (as text)
Wed Jan 17 04:34:59 2007 UTC (15 years, 10 months ago) by johnpye
File MIME type: text/x-csrc
File size: 7295 byte(s)
Fixed sensitivity module. It now runs as mostly external code, with just a few relics required by LSODE still in packages/sensitivity.c.
1 /* ASCEND modelling environment
2 Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly, Kirk Abbott.
3 Copyright (C) 2006 Carnegie Mellon University
4
5 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 @file
21 Code to support dynamic and static loading of user packages.
22
23 '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
29 Next there are the 'static' packages. These ones are linked into
30 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 *//*
35 by Kirk Abbott
36 Created: July 4, 1994
37 Last in CVS: 1.14 ballan 1998/03/06 15:47:14
38 */
39
40 #if !defined(DYNAMIC_PACKAGES) && !defined(STATIC_PACKAGES) && !defined(NO_PACKAGES)
41 # error "Package linking option not set!"
42 #endif
43
44 #include <math.h>
45 #include <ctype.h> /* was compiler/actype.h */
46
47 #include <utilities/ascConfig.h>
48 #include <utilities/config.h> /* NEW */
49
50 #ifndef ASC_DEFAULTPATH
51 # error "Where is ASC_DEFAULTPATH???"
52 #endif
53
54 #include <general/ospath.h>
55
56 #include "compiler.h"
57 #include <utilities/ascMalloc.h>
58 #include <utilities/ascEnvVar.h>
59 #include <compiler/importhandler.h>
60 #include <utilities/ascPanic.h>
61 #include <general/list.h>
62 #include "symtab.h"
63 #include "fractions.h"
64 #include "dimen.h"
65 #include "functype.h"
66 #include "expr_types.h"
67 #include "extcall.h"
68 #include "mathinst.h"
69 #include "instance_enum.h"
70 #include "instquery.h"
71 #include "atomvalue.h"
72 #include "find.h"
73 #include "relation_type.h"
74 #include "extfunc.h"
75 #include "rel_blackbox.h"
76 #include "vlist.h"
77 #include "relation.h"
78 #include "safe.h"
79 #include "relation_util.h"
80 #include "extfunc.h"
81 #include <packages/sensitivity.h>
82 #include <packages/ascFreeAllVars.h>
83 #include "module.h"
84 #include "packages.h"
85
86 /*
87 Initialise the slv data structures used when calling external fns
88 */
89 void Init_BBoxInterp(struct BBoxInterp *interp)
90 {
91 if (interp){
92 interp->status = calc_all_ok;
93 interp->user_data = NULL;
94 interp->task = bb_none;
95 }
96 }
97
98 /*---------------------------------------------
99 BUILT-IN PACKAGES...
100 */
101
102 /**
103 Load builtin packages, unless NO_PACKAGES.
104
105 @return 0 if success, 1 if failure.
106 */
107 static
108 int Builtins_Init(void){
109 int result = 0;
110
111 #ifdef NO_PACKAGES
112 ERROR_REPORTER_HERE(ASC_USER_WARNING,"Builtins_Init: DISABLED at compile-time");
113 #else
114 /* ERROR_REPORTER_DEBUG("Loading function asc_free_all_variables\n"); */
115 result = CreateUserFunctionMethod("asc_free_all_variables"
116 ,Asc_FreeAllVars
117 ,1 /* num of args */
118 ,"Unset 'fixed' flag of all items of type 'solver_var'" /* help */
119 ,NULL /* user_data */
120 ,NULL /* destroy fn */
121 );
122 #endif
123 return result;
124 }
125
126 /* return 0 on success */
127 int LoadArchiveLibrary(CONST char *partialpath, CONST char *initfunc){
128
129 #ifdef DYNAMIC_PACKAGES
130 struct FilePath *fp1;
131 int result;
132 struct ImportHandler *handler=NULL;
133
134 /**
135 @TODO
136 * modify SearchArchiveLibraryPath to use the ImportHandler array
137 in each directory in the path.
138 * when a file is found, return information about which ImportHandler
139 should be used to open it, then make the call.
140 */
141
142 CONSOLE_DEBUG("Searching for external library '%s'",partialpath);
143
144 importhandler_createlibrary();
145
146 fp1 = importhandler_findinpath(
147 partialpath, ASC_DEFAULTPATH, PATHENVIRONMENTVAR,&handler
148 );
149 if(fp1==NULL){
150 CONSOLE_DEBUG("External library '%s' not found",partialpath);
151 ERROR_REPORTER_NOLINE(ASC_USER_ERROR,"External library '%s' not found.",partialpath);
152 return 1; /* failure */
153 }
154
155 asc_assert(handler!=NULL);
156
157 /* CONSOLE_DEBUG("About to import external library..."); */
158 /* note the import handler will deal with all the initfunc execution, etc etc */
159 result = (*(handler->importfn))(fp1,initfunc,partialpath);
160 if(result){
161 CONSOLE_DEBUG("Error %d when importing external library of type '%s'",result,handler->name);
162 ERROR_REPORTER_HERE(ASC_PROG_ERROR,"Error importing external library '%s'",partialpath);
163 ospath_free(fp1);
164 return 1;
165 }
166
167 ospath_free(fp1);
168 return 0;
169 #else
170
171 (void)partialname; (void)initfunc;
172
173 # if defined(STATIC_PACKAGES)
174 ERROR_REPORTER_HERE(ASC_PROG_NOTE,"LoadArchiveLibrary disabled: STATIC_PACKAGES, no need to load dynamically.\n");
175 return 0;
176 # elif defined(NO_PACKAGES)
177 ERROR_REPORTER_HERE(ASC_PROG_ERROR,"LoadArchiveLibrary disabled: NO_PACKAGES");
178 return 1;
179 # else
180 # error "Invalid package linking flags"
181 # endif
182
183 #endif
184 }
185
186 /*---------------------------------------------
187 STATIC_PACKAGES code only...
188
189 Declare the functions which we are expected to be able to call.
190 */
191 #ifndef NO_PACKAGES
192 # ifdef STATIC_PACKAGES
193
194 #include <packages/kvalues.h>
195 #include <packages/bboxtest.h>
196 #include <packages/bisect.h>
197 #include <packages/sensitivity.h>
198
199 # endif
200 #endif
201
202 #ifdef STATIC_PACKAGES
203 /**
204 Load all statically-linked packages
205
206 @return 0 on success, >0 if any CreateUserFunction calls failed.
207 */
208 static int StaticPackages_Init(void){
209 int result = 0;
210
211 result += sensitivity_register();
212 result += kvalues_register();
213 result += bboxtest_register();
214
215 return result;
216 }
217 #endif
218
219 /**
220 This is a general purpose function that will load whatever user
221 functions are required according to the compile-time settings.
222
223 If NO_PACKAGES, nothing will be loaded. If DYNAMIC_PACKAGES, then
224 just the builtin packages will be loaded. If STATIC_PACKAGES then
225 builtin plus those called in 'StaticPackages_Init' will be loaded.
226 */
227 void AddUserFunctions(void){
228
229 CONSOLE_DEBUG("ADDING USER FUNCTIONS");
230
231 #ifdef NO_PACKAGES
232 # ifdef __GNUC__
233 # warning "NO_PACKAGES was defined so external packages are disabled"
234 # endif
235 ERROR_REPORTER_NOLINE(ASC_PROG_NOTE,"AddUserFunctions disabled at compile-time.");
236 #else
237
238 CONSOLE_DEBUG("ADDING BUILTINS");
239 /* Builtins are always statically linked */
240 if (Builtins_Init()) {
241 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING
242 ,"Problem in Builtins_Init: Some user functions not created"
243 );
244 }
245
246 # ifdef DYNAMIC_PACKAGES
247
248 CONSOLE_DEBUG("ADDING DYNAMIC PACKAGES...");
249 /* do nothing. WHY? */
250
251 # elif defined(STATIC_PACKAGES)
252 # ifdef __GNUC__
253 # warning "STATIC PACKAGES"
254 # endif
255
256 CONSOLE_DEBUG("ADDING STATIC PACKAGES FUNCTIONS...");
257 /*The following need to be reimplemented but are basically useful as is. */
258 if (StaticPackages_Init()) {
259 ERROR_REPORTER_NOLINE(ASC_PROG_WARNING
260 ,"Problem in StaticPackages_Init(): Some user functions not created"
261 );
262 }
263
264 # endif
265 #endif
266 }
267

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22