1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: interpreter: LIB and help |
---|
6 | */ |
---|
7 | |
---|
8 | #include "kernel/mod2.h" |
---|
9 | |
---|
10 | #include "Singular/tok.h" |
---|
11 | #include "misc/options.h" |
---|
12 | #include "Singular/ipid.h" |
---|
13 | #include "polys/monomials/ring.h" |
---|
14 | #include "Singular/subexpr.h" |
---|
15 | #include "Singular/ipid.h" |
---|
16 | #include "Singular/ipshell.h" |
---|
17 | #include "Singular/fevoices.h" |
---|
18 | #include "Singular/lists.h" |
---|
19 | |
---|
20 | #include <ctype.h> |
---|
21 | |
---|
22 | #if SIZEOF_LONG == 8 |
---|
23 | #define SI_MAX_NEST 500 |
---|
24 | #elif defined(__CYGWIN__) |
---|
25 | #define SI_MAX_NEST 480 |
---|
26 | #else |
---|
27 | #define SI_MAX_NEST 1000 |
---|
28 | #endif |
---|
29 | |
---|
30 | #if defined(ix86Mac_darwin) || defined(x86_64Mac_darwin) || defined(ppcMac_darwin) |
---|
31 | # define MODULE_SUFFIX bundle |
---|
32 | #elif defined(__CYGWIN__) |
---|
33 | # define MODULE_SUFFIX dll |
---|
34 | #else |
---|
35 | # define MODULE_SUFFIX so |
---|
36 | #endif |
---|
37 | |
---|
38 | #define MODULE_SUFFIX_STRING EXPANDED_STRINGIFY(MODULE_SUFFIX) |
---|
39 | |
---|
40 | |
---|
41 | #ifdef HAVE_DYNAMIC_LOADING |
---|
42 | BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport); |
---|
43 | #endif |
---|
44 | |
---|
45 | #ifdef HAVE_LIBPARSER |
---|
46 | # include "libparse.h" |
---|
47 | #else /* HAVE_LIBPARSER */ |
---|
48 | procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname, |
---|
49 | const char *procname, int line, long pos, BOOLEAN pstatic=FALSE); |
---|
50 | #endif /* HAVE_LIBPARSER */ |
---|
51 | |
---|
52 | extern int iiArithAddCmd(const char *szName, short nAlias, short nTokval, |
---|
53 | short nToktype, short nPos); |
---|
54 | |
---|
55 | #include "Singular/mod_lib.h" |
---|
56 | |
---|
57 | #ifdef HAVE_LIBPARSER |
---|
58 | void yylprestart (FILE *input_file ); |
---|
59 | int current_pos(int i=0); |
---|
60 | extern int yylp_errno; |
---|
61 | extern int yylplineno; |
---|
62 | extern const char *yylp_errlist[]; |
---|
63 | void print_init(); |
---|
64 | libstackv library_stack; |
---|
65 | #endif |
---|
66 | |
---|
67 | //int IsCmd(char *n, int tok); |
---|
68 | char mytolower(char c); |
---|
69 | |
---|
70 | /*2 |
---|
71 | * return TRUE if the libray libname is already loaded |
---|
72 | */ |
---|
73 | BOOLEAN iiGetLibStatus(char *lib) |
---|
74 | { |
---|
75 | idhdl hl; |
---|
76 | |
---|
77 | char *plib = iiConvName(lib); |
---|
78 | hl = basePack->idroot->get(plib,0); |
---|
79 | omFree(plib); |
---|
80 | if((hl==NULL) ||(IDTYP(hl)!=PACKAGE_CMD)) |
---|
81 | { |
---|
82 | return FALSE; |
---|
83 | } |
---|
84 | return (strcmp(lib,IDPACKAGE(hl)->libname)==0); |
---|
85 | } |
---|
86 | |
---|
87 | /*2 |
---|
88 | * given a line 'proc[ ]+{name}[ \t]*' |
---|
89 | * return a pointer to name and set the end of '\0' |
---|
90 | * changes the input! |
---|
91 | * returns: e: pointer to 'end of name' |
---|
92 | * ct: changed char at the end of s |
---|
93 | */ |
---|
94 | char* iiProcName(char *buf, char & ct, char* &e) |
---|
95 | { |
---|
96 | char *s=buf+5; |
---|
97 | while (*s==' ') s++; |
---|
98 | e=s+1; |
---|
99 | while ((*e>' ') && (*e!='(')) e++; |
---|
100 | ct=*e; |
---|
101 | *e='\0'; |
---|
102 | return s; |
---|
103 | } |
---|
104 | |
---|
105 | /*2 |
---|
106 | * given a line with args, return the argstr |
---|
107 | */ |
---|
108 | char * iiProcArgs(char *e,BOOLEAN withParenth) |
---|
109 | { |
---|
110 | while ((*e==' ') || (*e=='\t') || (*e=='(')) e++; |
---|
111 | if (*e<' ') |
---|
112 | { |
---|
113 | if (withParenth) |
---|
114 | { |
---|
115 | // no argument list, allow list # |
---|
116 | return omStrDup("parameter list #;"); |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | // empty list |
---|
121 | return omStrDup(""); |
---|
122 | } |
---|
123 | } |
---|
124 | BOOLEAN in_args; |
---|
125 | BOOLEAN args_found; |
---|
126 | char *s; |
---|
127 | char *argstr=(char *)omAlloc(127); // see ../omalloc/omTables.inc |
---|
128 | int argstrlen=127; |
---|
129 | *argstr='\0'; |
---|
130 | int par=0; |
---|
131 | do |
---|
132 | { |
---|
133 | args_found=FALSE; |
---|
134 | s=e; // set s to the starting point of the arg |
---|
135 | // and search for the end |
---|
136 | // skip leading spaces: |
---|
137 | loop |
---|
138 | { |
---|
139 | if ((*s==' ')||(*s=='\t')) |
---|
140 | s++; |
---|
141 | else if ((*s=='\n')&&(*(s+1)==' ')) |
---|
142 | s+=2; |
---|
143 | else // start of new arg or \0 or ) |
---|
144 | break; |
---|
145 | } |
---|
146 | e=s; |
---|
147 | while ((*e!=',') |
---|
148 | &&((par!=0) || (*e!=')')) |
---|
149 | &&(*e!='\0')) |
---|
150 | { |
---|
151 | if (*e=='(') par++; |
---|
152 | else if (*e==')') par--; |
---|
153 | args_found=args_found || (*e>' '); |
---|
154 | e++; |
---|
155 | } |
---|
156 | in_args=(*e==','); |
---|
157 | if (args_found) |
---|
158 | { |
---|
159 | *e='\0'; |
---|
160 | // check for space: |
---|
161 | if ((int)strlen(argstr)+12 /* parameter + ;*/ +(int)strlen(s)>= argstrlen) |
---|
162 | { |
---|
163 | argstrlen*=2; |
---|
164 | char *a=(char *)omAlloc( argstrlen); |
---|
165 | strcpy(a,argstr); |
---|
166 | omFree((ADDRESS)argstr); |
---|
167 | argstr=a; |
---|
168 | } |
---|
169 | // copy the result to argstr |
---|
170 | if(strncmp(s,"alias ",6)!=0) |
---|
171 | { |
---|
172 | strcat(argstr,"parameter "); |
---|
173 | } |
---|
174 | strcat(argstr,s); |
---|
175 | strcat(argstr,"; "); |
---|
176 | e++; // e was pointing to ',' |
---|
177 | } |
---|
178 | } while (in_args); |
---|
179 | return argstr; |
---|
180 | } |
---|
181 | |
---|
182 | /*2 |
---|
183 | * locate `procname` in lib `libname` and find the part `part`: |
---|
184 | * part=0: help, between, but excluding the line "proc ..." and "{...": |
---|
185 | * => return |
---|
186 | * part=1: body, between "{ ..." and "}", including the 1. line, w/o "{" |
---|
187 | * => set pi->data.s.body, return NULL |
---|
188 | * part=2: example, between, but excluding the line "exapmle {..." and "}": |
---|
189 | * => return |
---|
190 | */ |
---|
191 | char* iiGetLibProcBuffer(procinfo *pi, int part ) |
---|
192 | { |
---|
193 | char buf[256], *s = NULL, *p; |
---|
194 | long procbuflen; |
---|
195 | |
---|
196 | FILE * fp = feFopen( pi->libname, "rb", NULL, TRUE ); |
---|
197 | if (fp==NULL) |
---|
198 | { |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | |
---|
202 | fseek(fp, pi->data.s.proc_start, SEEK_SET); |
---|
203 | if(part==0) |
---|
204 | { // load help string |
---|
205 | int i, offset=0; |
---|
206 | long head = pi->data.s.def_end - pi->data.s.proc_start; |
---|
207 | procbuflen = pi->data.s.help_end - pi->data.s.help_start; |
---|
208 | if (procbuflen<5) |
---|
209 | { |
---|
210 | fclose(fp); |
---|
211 | return NULL; // help part does not exist |
---|
212 | } |
---|
213 | //Print("Help=%ld-%ld=%d\n", pi->data.s.body_start, |
---|
214 | // pi->data.s.proc_start, procbuflen); |
---|
215 | s = (char *)omAlloc(procbuflen+head+3); |
---|
216 | myfread(s, head, 1, fp); |
---|
217 | s[head] = '\n'; |
---|
218 | fseek(fp, pi->data.s.help_start, SEEK_SET); |
---|
219 | myfread(s+head+1, procbuflen, 1, fp); |
---|
220 | fclose(fp); |
---|
221 | s[procbuflen+head+1] = '\n'; |
---|
222 | s[procbuflen+head+2] = '\0'; |
---|
223 | offset=0; |
---|
224 | for(i=0;i<=procbuflen+head+2; i++) |
---|
225 | { |
---|
226 | if(s[i]=='\\' && |
---|
227 | (s[i+1]=='"' || s[i+1]=='{' || s[i+1]=='}' || s[i+1]=='\\')) |
---|
228 | { |
---|
229 | i++; |
---|
230 | offset++; |
---|
231 | } |
---|
232 | if(offset>0) s[i-offset] = s[i]; |
---|
233 | } |
---|
234 | return(s); |
---|
235 | } |
---|
236 | else if(part==1) |
---|
237 | { // load proc part - must exist |
---|
238 | procbuflen = pi->data.s.def_end - pi->data.s.proc_start; |
---|
239 | char *ss=(char *)omAlloc(procbuflen+2); |
---|
240 | //fgets(buf, sizeof(buf), fp); |
---|
241 | myfread( ss, procbuflen, 1, fp); |
---|
242 | char ct; |
---|
243 | char *e; |
---|
244 | s=iiProcName(ss,ct,e); |
---|
245 | char *argstr=NULL; |
---|
246 | *e=ct; |
---|
247 | argstr=iiProcArgs(e,TRUE); |
---|
248 | |
---|
249 | assume(pi->data.s.body_end > pi->data.s.body_start); |
---|
250 | |
---|
251 | procbuflen = pi->data.s.body_end - pi->data.s.body_start; |
---|
252 | pi->data.s.body = (char *)omAlloc( strlen(argstr)+procbuflen+15+ |
---|
253 | strlen(pi->libname) ); |
---|
254 | //Print("Body=%ld-%ld=%d\n", pi->data.s.body_end, |
---|
255 | // pi->data.s.body_start, procbuflen); |
---|
256 | assume(pi->data.s.body != NULL); |
---|
257 | fseek(fp, pi->data.s.body_start, SEEK_SET); |
---|
258 | strcpy(pi->data.s.body,argstr); |
---|
259 | myfread( pi->data.s.body+strlen(argstr), procbuflen, 1, fp); |
---|
260 | fclose( fp ); |
---|
261 | procbuflen+=strlen(argstr); |
---|
262 | omFree(argstr); |
---|
263 | omFree(ss); |
---|
264 | pi->data.s.body[procbuflen] = '\0'; |
---|
265 | strcat( pi->data.s.body+procbuflen, "\n;return();\n\n" ); |
---|
266 | strcat( pi->data.s.body+procbuflen+13,pi->libname); |
---|
267 | s=(char *)strchr(pi->data.s.body,'{'); |
---|
268 | if (s!=NULL) *s=' '; |
---|
269 | return NULL; |
---|
270 | } |
---|
271 | else if(part==2) |
---|
272 | { // example |
---|
273 | if ( pi->data.s.example_lineno == 0) |
---|
274 | return NULL; // example part does not exist |
---|
275 | // load example |
---|
276 | fseek(fp, pi->data.s.example_start, SEEK_SET); |
---|
277 | /*char *dummy=*/ (void) fgets(buf, sizeof(buf), fp); // skip line with "example" |
---|
278 | procbuflen = pi->data.s.proc_end - pi->data.s.example_start - strlen(buf); |
---|
279 | //Print("Example=%ld-%ld=%d\n", pi->data.s.proc_end, |
---|
280 | // pi->data.s.example_start, procbuflen); |
---|
281 | s = (char *)omAlloc(procbuflen+14); |
---|
282 | myfread(s, procbuflen, 1, fp); |
---|
283 | s[procbuflen] = '\0'; |
---|
284 | strcat(s+procbuflen-3, "\n;return();\n\n" ); |
---|
285 | p=(char *)strchr(s,'{'); |
---|
286 | if (p!=NULL) *p=' '; |
---|
287 | return(s); |
---|
288 | } |
---|
289 | return NULL; |
---|
290 | } |
---|
291 | |
---|
292 | BOOLEAN iiAllStart(procinfov pi, char *p,feBufferTypes t, int l) |
---|
293 | { |
---|
294 | // see below: |
---|
295 | BITSET save1=si_opt_1; |
---|
296 | BITSET save2=si_opt_2; |
---|
297 | newBuffer( omStrDup(p /*pi->data.s.body*/), t /*BT_proc*/, |
---|
298 | pi, l ); |
---|
299 | BOOLEAN err=yyparse(); |
---|
300 | if (sLastPrinted.rtyp!=0) |
---|
301 | { |
---|
302 | sLastPrinted.CleanUp(); |
---|
303 | } |
---|
304 | // the access to optionStruct and verboseStruct do not work |
---|
305 | // on x86_64-Linux for pic-code |
---|
306 | if ((TEST_V_ALLWARN) && |
---|
307 | (t==BT_proc) && |
---|
308 | ((save1!=si_opt_1)||(save2!=si_opt_2)) && |
---|
309 | (pi->libname!=NULL) && (pi->libname[0]!='\0')) |
---|
310 | { |
---|
311 | if ((pi->libname!=NULL) && (pi->libname[0]!='\0')) |
---|
312 | Warn("option changed in proc %s from %s",pi->procname,pi->libname); |
---|
313 | else |
---|
314 | Warn("option changed in proc %s",pi->procname); |
---|
315 | int i; |
---|
316 | for (i=0; optionStruct[i].setval!=0; i++) |
---|
317 | { |
---|
318 | if ((optionStruct[i].setval & si_opt_1) |
---|
319 | && (!(optionStruct[i].setval & save1))) |
---|
320 | { |
---|
321 | Print(" +%s",optionStruct[i].name); |
---|
322 | } |
---|
323 | if (!(optionStruct[i].setval & si_opt_1) |
---|
324 | && ((optionStruct[i].setval & save1))) |
---|
325 | { |
---|
326 | Print(" -%s",optionStruct[i].name); |
---|
327 | } |
---|
328 | } |
---|
329 | for (i=0; verboseStruct[i].setval!=0; i++) |
---|
330 | { |
---|
331 | if ((verboseStruct[i].setval & si_opt_2) |
---|
332 | && (!(verboseStruct[i].setval & save2))) |
---|
333 | { |
---|
334 | Print(" +%s",verboseStruct[i].name); |
---|
335 | } |
---|
336 | if (!(verboseStruct[i].setval & si_opt_2) |
---|
337 | && ((verboseStruct[i].setval & save2))) |
---|
338 | { |
---|
339 | Print(" -%s",verboseStruct[i].name); |
---|
340 | } |
---|
341 | } |
---|
342 | PrintLn(); |
---|
343 | } |
---|
344 | return err; |
---|
345 | } |
---|
346 | /*2 |
---|
347 | * start a proc |
---|
348 | * parameters are built as exprlist |
---|
349 | * TODO:interrupt |
---|
350 | * return FALSE on success, TRUE if an error occurs |
---|
351 | */ |
---|
352 | BOOLEAN iiPStart(idhdl pn, leftv v) |
---|
353 | { |
---|
354 | procinfov pi=NULL; |
---|
355 | int old_echo=si_echo; |
---|
356 | BOOLEAN err=FALSE; |
---|
357 | char save_flags=0; |
---|
358 | |
---|
359 | /* init febase ======================================== */ |
---|
360 | /* we do not enter this case if filename != NULL !! */ |
---|
361 | if (pn!=NULL) |
---|
362 | { |
---|
363 | pi = IDPROC(pn); |
---|
364 | if(pi!=NULL) |
---|
365 | { |
---|
366 | save_flags=pi->trace_flag; |
---|
367 | if( pi->data.s.body==NULL ) |
---|
368 | { |
---|
369 | iiGetLibProcBuffer(pi); |
---|
370 | if (pi->data.s.body==NULL) return TRUE; |
---|
371 | } |
---|
372 | // omUpdateInfo(); |
---|
373 | // int m=om_Info.UsedBytes; |
---|
374 | // Print("proc %s, mem=%d\n",IDID(pn),m); |
---|
375 | } |
---|
376 | } |
---|
377 | else return TRUE; |
---|
378 | /* generate argument list ======================================*/ |
---|
379 | //iiCurrArgs should be NULL here, as the assignment for the parameters |
---|
380 | // of the prevouis call are already done befor calling another routine |
---|
381 | if (v!=NULL) |
---|
382 | { |
---|
383 | iiCurrArgs=(leftv)omAllocBin(sleftv_bin); |
---|
384 | memcpy(iiCurrArgs,v,sizeof(sleftv)); // keeps track of v->next etc. |
---|
385 | memset(v,0,sizeof(sleftv)); |
---|
386 | } |
---|
387 | else |
---|
388 | { |
---|
389 | iiCurrArgs=NULL; |
---|
390 | } |
---|
391 | iiCurrProc=pn; |
---|
392 | /* start interpreter ======================================*/ |
---|
393 | myynest++; |
---|
394 | if (myynest > SI_MAX_NEST) |
---|
395 | { |
---|
396 | WerrorS("nesting too deep"); |
---|
397 | err=TRUE; |
---|
398 | } |
---|
399 | else |
---|
400 | { |
---|
401 | err=iiAllStart(pi,pi->data.s.body,BT_proc,pi->data.s.body_lineno-(v!=NULL)); |
---|
402 | |
---|
403 | if (iiLocalRing[myynest-1] != currRing) |
---|
404 | { |
---|
405 | if (iiRETURNEXPR.RingDependend()) |
---|
406 | { |
---|
407 | //idhdl hn; |
---|
408 | const char *n; |
---|
409 | const char *o; |
---|
410 | idhdl nh=NULL, oh=NULL; |
---|
411 | if (iiLocalRing[myynest-1]!=NULL) |
---|
412 | oh=rFindHdl(iiLocalRing[myynest-1],NULL); |
---|
413 | if (oh!=NULL) o=oh->id; |
---|
414 | else o="none"; |
---|
415 | if (currRing!=NULL) |
---|
416 | nh=rFindHdl(currRing,NULL); |
---|
417 | if (nh!=NULL) n=nh->id; |
---|
418 | else n="none"; |
---|
419 | Werror("ring change during procedure call %s: %s -> %s (level %d)",pi->procname,o,n,myynest); |
---|
420 | iiRETURNEXPR.CleanUp(); |
---|
421 | err=TRUE; |
---|
422 | } |
---|
423 | currRing=iiLocalRing[myynest-1]; |
---|
424 | } |
---|
425 | if ((currRing==NULL) |
---|
426 | && (currRingHdl!=NULL)) |
---|
427 | currRing=IDRING(currRingHdl); |
---|
428 | else |
---|
429 | if ((currRing!=NULL) && |
---|
430 | ((currRingHdl==NULL)||(IDRING(currRingHdl)!=currRing) |
---|
431 | ||(IDLEV(currRingHdl)>=myynest-1))) |
---|
432 | { |
---|
433 | rSetHdl(rFindHdl(currRing,NULL)); |
---|
434 | iiLocalRing[myynest-1]=NULL; |
---|
435 | } |
---|
436 | //Print("kill locals for %s (level %d)\n",IDID(pn),myynest); |
---|
437 | killlocals(myynest); |
---|
438 | #ifndef SING_NDEBUG |
---|
439 | checkall(); |
---|
440 | #endif |
---|
441 | //Print("end kill locals for %s (%d)\n",IDID(pn),myynest); |
---|
442 | } |
---|
443 | myynest--; |
---|
444 | si_echo=old_echo; |
---|
445 | if (pi!=NULL) |
---|
446 | pi->trace_flag=save_flags; |
---|
447 | // omUpdateInfo(); |
---|
448 | // int m=om_Info.UsedBytes; |
---|
449 | // Print("exit %s, mem=%d\n",IDID(pn),m); |
---|
450 | return err; |
---|
451 | } |
---|
452 | |
---|
453 | ring *iiLocalRing; |
---|
454 | sleftv iiRETURNEXPR; |
---|
455 | int iiRETURNEXPR_len=0; |
---|
456 | |
---|
457 | #ifdef RDEBUG |
---|
458 | static void iiShowLevRings() |
---|
459 | { |
---|
460 | int i; |
---|
461 | for (i=0;i<=myynest;i++) |
---|
462 | { |
---|
463 | Print("lev %d:",i); |
---|
464 | if (iiLocalRing[i]==NULL) PrintS("NULL"); |
---|
465 | else Print("%lx",(long)iiLocalRing[i]); |
---|
466 | PrintLn(); |
---|
467 | } |
---|
468 | if (currRing==NULL) PrintS("curr:NULL\n"); |
---|
469 | else Print ("curr:%lx\n",(long)currRing); |
---|
470 | } |
---|
471 | #endif /* RDEBUG */ |
---|
472 | |
---|
473 | static void iiCheckNest() |
---|
474 | { |
---|
475 | if (myynest >= iiRETURNEXPR_len-1) |
---|
476 | { |
---|
477 | iiLocalRing=(ring *)omreallocSize(iiLocalRing, |
---|
478 | iiRETURNEXPR_len*sizeof(ring), |
---|
479 | (iiRETURNEXPR_len+16)*sizeof(ring)); |
---|
480 | memset(&(iiLocalRing[iiRETURNEXPR_len]),0,16*sizeof(ring)); |
---|
481 | iiRETURNEXPR_len+=16; |
---|
482 | } |
---|
483 | } |
---|
484 | BOOLEAN iiMake_proc(idhdl pn, package pack, leftv sl) |
---|
485 | { |
---|
486 | int err; |
---|
487 | procinfov pi = IDPROC(pn); |
---|
488 | if(pi->is_static && myynest==0) |
---|
489 | { |
---|
490 | Werror("'%s::%s()' is a local procedure and cannot be accessed by an user.", |
---|
491 | pi->libname, pi->procname); |
---|
492 | return TRUE; |
---|
493 | } |
---|
494 | iiCheckNest(); |
---|
495 | iiLocalRing[myynest]=currRing; |
---|
496 | //Print("currRing(%d):%s(%x) in %s\n",myynest,IDID(currRingHdl),currRing,IDID(pn)); |
---|
497 | iiRETURNEXPR.Init(); |
---|
498 | procstack->push(pi->procname); |
---|
499 | if ((traceit&TRACE_SHOW_PROC) |
---|
500 | || (pi->trace_flag&TRACE_SHOW_PROC)) |
---|
501 | { |
---|
502 | if (traceit&TRACE_SHOW_LINENO) PrintLn(); |
---|
503 | Print("entering%-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest); |
---|
504 | } |
---|
505 | #ifdef RDEBUG |
---|
506 | if (traceit&TRACE_SHOW_RINGS) iiShowLevRings(); |
---|
507 | #endif |
---|
508 | switch (pi->language) |
---|
509 | { |
---|
510 | default: |
---|
511 | case LANG_NONE: |
---|
512 | WerrorS("undefined proc"); |
---|
513 | err=TRUE; |
---|
514 | break; |
---|
515 | |
---|
516 | case LANG_SINGULAR: |
---|
517 | if ((pi->pack!=NULL)&&(currPack!=pi->pack)) |
---|
518 | { |
---|
519 | currPack=pi->pack; |
---|
520 | iiCheckPack(currPack); |
---|
521 | currPackHdl=packFindHdl(currPack); |
---|
522 | //Print("set pack=%s\n",IDID(currPackHdl)); |
---|
523 | } |
---|
524 | else if ((pack!=NULL)&&(currPack!=pack)) |
---|
525 | { |
---|
526 | currPack=pack; |
---|
527 | iiCheckPack(currPack); |
---|
528 | currPackHdl=packFindHdl(currPack); |
---|
529 | //Print("set pack=%s\n",IDID(currPackHdl)); |
---|
530 | } |
---|
531 | err=iiPStart(pn,sl); |
---|
532 | break; |
---|
533 | case LANG_C: |
---|
534 | leftv res = (leftv)omAlloc0Bin(sleftv_bin); |
---|
535 | err = (pi->data.o.function)(res, sl); |
---|
536 | memcpy(&iiRETURNEXPR,res,sizeof(iiRETURNEXPR)); |
---|
537 | omFreeBin((ADDRESS)res, sleftv_bin); |
---|
538 | break; |
---|
539 | } |
---|
540 | if ((traceit&TRACE_SHOW_PROC) |
---|
541 | || (pi->trace_flag&TRACE_SHOW_PROC)) |
---|
542 | { |
---|
543 | if (traceit&TRACE_SHOW_LINENO) PrintLn(); |
---|
544 | Print("leaving %-*.*s %s (level %d)\n",myynest*2,myynest*2," ",IDID(pn),myynest); |
---|
545 | } |
---|
546 | //const char *n="NULL"; |
---|
547 | //if (currRingHdl!=NULL) n=IDID(currRingHdl); |
---|
548 | //Print("currRing(%d):%s(%x) after %s\n",myynest,n,currRing,IDID(pn)); |
---|
549 | #ifdef RDEBUG |
---|
550 | if (traceit&TRACE_SHOW_RINGS) iiShowLevRings(); |
---|
551 | #endif |
---|
552 | if (err) |
---|
553 | { |
---|
554 | iiRETURNEXPR.CleanUp(); |
---|
555 | //iiRETURNEXPR.Init(); //done by CleanUp |
---|
556 | } |
---|
557 | if (iiCurrArgs!=NULL) |
---|
558 | { |
---|
559 | if (!err) Warn("too many arguments for %s",IDID(pn)); |
---|
560 | iiCurrArgs->CleanUp(); |
---|
561 | omFreeBin((ADDRESS)iiCurrArgs, sleftv_bin); |
---|
562 | iiCurrArgs=NULL; |
---|
563 | } |
---|
564 | procstack->pop(); |
---|
565 | if (err) |
---|
566 | return TRUE; |
---|
567 | return FALSE; |
---|
568 | } |
---|
569 | static void iiCallLibProcBegin() |
---|
570 | { |
---|
571 | idhdl tmp_ring=NULL; |
---|
572 | if (currRing!=NULL) |
---|
573 | { |
---|
574 | if (IDRING(currRingHdl)!=currRing) |
---|
575 | { |
---|
576 | // clean up things depending on currRingHdl: |
---|
577 | sLastPrinted.CleanUp(IDRING(currRingHdl)); |
---|
578 | sLastPrinted.Init(); |
---|
579 | // need to define a ring-hdl for currRingHdl |
---|
580 | tmp_ring=enterid(" tmpRing",myynest,RING_CMD,&IDROOT,FALSE); |
---|
581 | IDRING(tmp_ring)=currRing; |
---|
582 | currRing->ref++; |
---|
583 | rSetHdl(tmp_ring); |
---|
584 | } |
---|
585 | } |
---|
586 | } |
---|
587 | static void iiCallLibProcEnd(idhdl save_ringhdl, ring save_ring) |
---|
588 | { |
---|
589 | if ((currRing!=NULL) |
---|
590 | &&(currRing!=save_ring)) |
---|
591 | { |
---|
592 | currRing->ref--; |
---|
593 | idhdl hh=IDROOT; |
---|
594 | idhdl prev=NULL; |
---|
595 | while((hh!=currRingHdl) && (hh!=NULL)) { prev=hh; hh=hh->next; } |
---|
596 | if (hh!=NULL) |
---|
597 | { |
---|
598 | if (prev==NULL) IDROOT=hh->next; |
---|
599 | else prev->next=hh->next; |
---|
600 | omFree((ADDRESS)IDID(hh)); |
---|
601 | omFreeBin((ADDRESS)hh, idrec_bin); |
---|
602 | } |
---|
603 | else |
---|
604 | { |
---|
605 | WarnS("internal: lost ring in iiCallLib"); |
---|
606 | } |
---|
607 | } |
---|
608 | currRingHdl=save_ringhdl; |
---|
609 | currRing=save_ring; |
---|
610 | } |
---|
611 | |
---|
612 | void* iiCallLibProc1(const char*n, void *arg, int arg_type, BOOLEAN &err) |
---|
613 | { |
---|
614 | idhdl h=ggetid(n); |
---|
615 | if ((h==NULL) |
---|
616 | || (IDTYP(h)!=PROC_CMD)) |
---|
617 | { |
---|
618 | err=2; |
---|
619 | return NULL; |
---|
620 | } |
---|
621 | // ring handling |
---|
622 | idhdl save_ringhdl=currRingHdl; |
---|
623 | ring save_ring=currRing; |
---|
624 | iiCallLibProcBegin(); |
---|
625 | // argument: |
---|
626 | sleftv tmp; |
---|
627 | tmp.Init(); |
---|
628 | tmp.data=arg; |
---|
629 | tmp.rtyp=arg_type; |
---|
630 | // call proc |
---|
631 | err=iiMake_proc(h,currPack,&tmp); |
---|
632 | // clean up ring |
---|
633 | iiCallLibProcEnd(save_ringhdl,save_ring); |
---|
634 | // return |
---|
635 | if (err==FALSE) |
---|
636 | { |
---|
637 | void*r=iiRETURNEXPR.data; |
---|
638 | iiRETURNEXPR.data=NULL; |
---|
639 | iiRETURNEXPR.CleanUp(); |
---|
640 | return r; |
---|
641 | } |
---|
642 | return NULL; |
---|
643 | } |
---|
644 | /// args: NULL terminated arry of arguments |
---|
645 | /// arg_types: 0 terminated array of corresponding types |
---|
646 | void* iiCallLibProcM(const char*n, void **args, int* arg_types, BOOLEAN &err) |
---|
647 | { |
---|
648 | idhdl h=ggetid(n); |
---|
649 | if ((h==NULL) |
---|
650 | || (IDTYP(h)!=PROC_CMD)) |
---|
651 | { |
---|
652 | err=2; |
---|
653 | return NULL; |
---|
654 | } |
---|
655 | // ring handling |
---|
656 | idhdl save_ringhdl=currRingHdl; |
---|
657 | ring save_ring=currRing; |
---|
658 | iiCallLibProcBegin(); |
---|
659 | // argument: |
---|
660 | if (arg_types[0]!=0) |
---|
661 | { |
---|
662 | sleftv tmp; |
---|
663 | leftv tt=&tmp; |
---|
664 | int i=1; |
---|
665 | tmp.Init(); |
---|
666 | tmp.data=args[0]; |
---|
667 | tmp.rtyp=arg_types[0]; |
---|
668 | while(arg_types[i]!=0) |
---|
669 | { |
---|
670 | tt->next=(leftv)omAlloc0(sizeof(sleftv)); |
---|
671 | tt=tt->next; |
---|
672 | tt->rtyp=arg_types[i]; |
---|
673 | tt->data=args[i]; |
---|
674 | i++; |
---|
675 | } |
---|
676 | // call proc |
---|
677 | err=iiMake_proc(h,currPack,&tmp); |
---|
678 | } |
---|
679 | else |
---|
680 | // call proc |
---|
681 | err=iiMake_proc(h,currPack,NULL); |
---|
682 | // clean up ring |
---|
683 | iiCallLibProcEnd(save_ringhdl,save_ring); |
---|
684 | // return |
---|
685 | if (err==FALSE) |
---|
686 | { |
---|
687 | void*r=iiRETURNEXPR.data; |
---|
688 | iiRETURNEXPR.data=NULL; |
---|
689 | iiRETURNEXPR.CleanUp(); |
---|
690 | return r; |
---|
691 | } |
---|
692 | return NULL; |
---|
693 | } |
---|
694 | /*2 |
---|
695 | * start an example (as a proc), |
---|
696 | * destroys the string 'example' |
---|
697 | */ |
---|
698 | BOOLEAN iiEStart(char* example, procinfo *pi) |
---|
699 | { |
---|
700 | BOOLEAN err; |
---|
701 | int old_echo=si_echo; |
---|
702 | |
---|
703 | iiCheckNest(); |
---|
704 | procstack->push(example); |
---|
705 | iiLocalRing[myynest]=currRing; |
---|
706 | if (traceit&TRACE_SHOW_PROC) |
---|
707 | { |
---|
708 | if (traceit&TRACE_SHOW_LINENO) printf("\n"); |
---|
709 | printf("entering example (level %d)\n",myynest); |
---|
710 | } |
---|
711 | myynest++; |
---|
712 | |
---|
713 | err=iiAllStart(pi,example,BT_example,(pi != NULL ? pi->data.s.example_lineno: 0)); |
---|
714 | |
---|
715 | killlocals(myynest); |
---|
716 | myynest--; |
---|
717 | si_echo=old_echo; |
---|
718 | if (traceit&TRACE_SHOW_PROC) |
---|
719 | { |
---|
720 | if (traceit&TRACE_SHOW_LINENO) printf("\n"); |
---|
721 | printf("leaving -example- (level %d)\n",myynest); |
---|
722 | } |
---|
723 | if (iiLocalRing[myynest] != currRing) |
---|
724 | { |
---|
725 | if (iiLocalRing[myynest]!=NULL) |
---|
726 | { |
---|
727 | rSetHdl(rFindHdl(iiLocalRing[myynest],NULL)); |
---|
728 | iiLocalRing[myynest]=NULL; |
---|
729 | } |
---|
730 | else |
---|
731 | { |
---|
732 | currRingHdl=NULL; |
---|
733 | currRing=NULL; |
---|
734 | } |
---|
735 | } |
---|
736 | procstack->pop(); |
---|
737 | return err; |
---|
738 | } |
---|
739 | |
---|
740 | |
---|
741 | extern "C" |
---|
742 | { |
---|
743 | # define SI_GET_BUILTIN_MOD_INIT0(name) int SI_MOD_INIT0(name)(SModulFunctions*); |
---|
744 | SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT0) |
---|
745 | # undef SI_GET_BUILTIN_MOD_INIT0 |
---|
746 | }; |
---|
747 | |
---|
748 | |
---|
749 | SModulFunc_t |
---|
750 | iiGetBuiltinModInit(const char* libname) |
---|
751 | { |
---|
752 | # define SI_GET_BUILTIN_MOD_INIT(name) if (strcmp(libname, #name ".so") == 0){ return SI_MOD_INIT0(name); } |
---|
753 | SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT) |
---|
754 | # undef SI_GET_BUILTIN_MOD_INIT |
---|
755 | |
---|
756 | return NULL; |
---|
757 | } |
---|
758 | |
---|
759 | |
---|
760 | |
---|
761 | |
---|
762 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
763 | BOOLEAN iiTryLoadLib(leftv v, const char *id) |
---|
764 | { |
---|
765 | BOOLEAN LoadResult = TRUE; |
---|
766 | char libnamebuf[1024]; |
---|
767 | char *libname = (char *)omAlloc(strlen(id)+5); |
---|
768 | const char *suffix[] = { "", ".lib", ".so", ".sl", NULL }; |
---|
769 | int i = 0; |
---|
770 | // FILE *fp; |
---|
771 | // package pack; |
---|
772 | // idhdl packhdl; |
---|
773 | lib_types LT; |
---|
774 | for(i=0; suffix[i] != NULL; i++) |
---|
775 | { |
---|
776 | sprintf(libname, "%s%s", id, suffix[i]); |
---|
777 | *libname = mytolower(*libname); |
---|
778 | if((LT = type_of_LIB(libname, libnamebuf)) > LT_NOTFOUND) |
---|
779 | { |
---|
780 | char *s=omStrDup(libname); |
---|
781 | #ifdef HAVE_DYNAMIC_LOADING |
---|
782 | char libnamebuf[1024]; |
---|
783 | #endif |
---|
784 | |
---|
785 | if (LT==LT_SINGULAR) |
---|
786 | LoadResult = iiLibCmd(s, FALSE, FALSE,TRUE); |
---|
787 | #ifdef HAVE_DYNAMIC_LOADING |
---|
788 | else if ((LT==LT_ELF) || (LT==LT_HPUX)) |
---|
789 | LoadResult = load_modules(s,libnamebuf,FALSE); |
---|
790 | #endif |
---|
791 | else if (LT==LT_BUILTIN) |
---|
792 | { |
---|
793 | LoadResult=load_builtin(s,FALSE, iiGetBuiltinModInit(s)); |
---|
794 | } |
---|
795 | if(!LoadResult ) |
---|
796 | { |
---|
797 | v->name = iiConvName(libname); |
---|
798 | break; |
---|
799 | } |
---|
800 | } |
---|
801 | } |
---|
802 | omFree(libname); |
---|
803 | return LoadResult; |
---|
804 | } |
---|
805 | |
---|
806 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
807 | /* check, if library lib has already been loaded |
---|
808 | if yes, writes filename of lib into where and returns TRUE, |
---|
809 | no, returns FALSE |
---|
810 | */ |
---|
811 | BOOLEAN iiLocateLib(const char* lib, char* where) |
---|
812 | { |
---|
813 | char *plib = iiConvName(lib); |
---|
814 | idhdl pl = basePack->idroot->get(plib,0); |
---|
815 | if( (pl!=NULL) && (IDTYP(pl)==PACKAGE_CMD) && |
---|
816 | (IDPACKAGE(pl)->language == LANG_SINGULAR)) |
---|
817 | { |
---|
818 | strncpy(where,IDPACKAGE(pl)->libname,127); |
---|
819 | return TRUE; |
---|
820 | } |
---|
821 | else |
---|
822 | return FALSE;; |
---|
823 | } |
---|
824 | |
---|
825 | BOOLEAN iiLibCmd( char *newlib, BOOLEAN autoexport, BOOLEAN tellerror, BOOLEAN force ) |
---|
826 | { |
---|
827 | char libnamebuf[1024]; |
---|
828 | // procinfov pi; |
---|
829 | // idhdl h; |
---|
830 | idhdl pl; |
---|
831 | // idhdl hl; |
---|
832 | // long pos = 0L; |
---|
833 | char *plib = iiConvName(newlib); |
---|
834 | FILE * fp = feFopen( newlib, "r", libnamebuf, tellerror ); |
---|
835 | // int lines = 1; |
---|
836 | BOOLEAN LoadResult = TRUE; |
---|
837 | |
---|
838 | if (fp==NULL) |
---|
839 | { |
---|
840 | return TRUE; |
---|
841 | } |
---|
842 | pl = basePack->idroot->get(plib,0); |
---|
843 | if (pl==NULL) |
---|
844 | { |
---|
845 | pl = enterid( plib,0, PACKAGE_CMD, |
---|
846 | &(basePack->idroot), TRUE ); |
---|
847 | IDPACKAGE(pl)->language = LANG_SINGULAR; |
---|
848 | IDPACKAGE(pl)->libname=omStrDup(newlib); |
---|
849 | } |
---|
850 | else |
---|
851 | { |
---|
852 | if(IDTYP(pl)!=PACKAGE_CMD) |
---|
853 | { |
---|
854 | WarnS("not of type package."); |
---|
855 | fclose(fp); |
---|
856 | return TRUE; |
---|
857 | } |
---|
858 | if (!force) return FALSE; |
---|
859 | } |
---|
860 | LoadResult = iiLoadLIB(fp, libnamebuf, newlib, pl, autoexport, tellerror); |
---|
861 | omFree((ADDRESS)newlib); |
---|
862 | |
---|
863 | if(!LoadResult) IDPACKAGE(pl)->loaded = TRUE; |
---|
864 | omFree((ADDRESS)plib); |
---|
865 | |
---|
866 | return LoadResult; |
---|
867 | } |
---|
868 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
869 | static void iiCleanProcs(idhdl &root) |
---|
870 | { |
---|
871 | idhdl prev=NULL; |
---|
872 | loop |
---|
873 | { |
---|
874 | if (root==NULL) return; |
---|
875 | if (IDTYP(root)==PROC_CMD) |
---|
876 | { |
---|
877 | procinfo *pi=(procinfo*)IDDATA(root); |
---|
878 | if ((pi->language == LANG_SINGULAR) |
---|
879 | && (pi->data.s.body_start == 0L)) |
---|
880 | { |
---|
881 | // procinfo data incorrect: |
---|
882 | // - no proc body can start at the beginning of the file |
---|
883 | killhdl(root); |
---|
884 | if (prev==NULL) |
---|
885 | root=IDROOT; |
---|
886 | else |
---|
887 | { |
---|
888 | root=prev; |
---|
889 | prev=NULL; |
---|
890 | } |
---|
891 | continue; |
---|
892 | } |
---|
893 | } |
---|
894 | prev=root; |
---|
895 | root=IDNEXT(root); |
---|
896 | } |
---|
897 | } |
---|
898 | static void iiRunInit(package p) |
---|
899 | { |
---|
900 | idhdl h=p->idroot->get("mod_init",0); |
---|
901 | if (h==NULL) return; |
---|
902 | if (IDTYP(h)==PROC_CMD) |
---|
903 | { |
---|
904 | int save=yylineno; |
---|
905 | myynest++; |
---|
906 | // procinfo *pi=(procinfo*)IDDATA(h); |
---|
907 | //PrintS("mod_init found\n"); |
---|
908 | iiMake_proc(h,p,NULL); |
---|
909 | myynest--; |
---|
910 | yylineno=save; |
---|
911 | } |
---|
912 | } |
---|
913 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
914 | BOOLEAN iiLoadLIB(FILE *fp, const char *libnamebuf, const char*newlib, |
---|
915 | idhdl pl, BOOLEAN autoexport, BOOLEAN tellerror) |
---|
916 | { |
---|
917 | extern FILE *yylpin; |
---|
918 | libstackv ls_start = library_stack; |
---|
919 | lib_style_types lib_style; |
---|
920 | |
---|
921 | yylpin = fp; |
---|
922 | #if YYLPDEBUG > 1 |
---|
923 | print_init(); |
---|
924 | #endif |
---|
925 | extern int lpverbose; |
---|
926 | if (BVERBOSE(V_DEBUG_LIB)) lpverbose=1; |
---|
927 | else lpverbose=0; |
---|
928 | // yylplex sets also text_buffer |
---|
929 | if (text_buffer!=NULL) *text_buffer='\0'; |
---|
930 | yylplex(newlib, libnamebuf, &lib_style, pl, autoexport); |
---|
931 | if(yylp_errno) |
---|
932 | { |
---|
933 | Werror("Library %s: ERROR occurred: in line %d, %d.", newlib, yylplineno, |
---|
934 | current_pos(0)); |
---|
935 | if(yylp_errno==YYLP_BAD_CHAR) |
---|
936 | { |
---|
937 | Werror(yylp_errlist[yylp_errno], *text_buffer, yylplineno); |
---|
938 | omFree((ADDRESS)text_buffer); |
---|
939 | text_buffer=NULL; |
---|
940 | } |
---|
941 | else |
---|
942 | Werror(yylp_errlist[yylp_errno], yylplineno); |
---|
943 | WerrorS("Cannot load library,... aborting."); |
---|
944 | reinit_yylp(); |
---|
945 | fclose( yylpin ); |
---|
946 | iiCleanProcs(IDROOT); |
---|
947 | return TRUE; |
---|
948 | } |
---|
949 | if (BVERBOSE(V_LOAD_LIB)) |
---|
950 | Print( "// ** loaded %s %s\n", libnamebuf, text_buffer); |
---|
951 | if( (lib_style == OLD_LIBSTYLE) && (BVERBOSE(V_LOAD_LIB))) |
---|
952 | { |
---|
953 | Warn( "library %s has old format. This format is still accepted,", newlib); |
---|
954 | WarnS( "but for functionality you may wish to change to the new"); |
---|
955 | WarnS( "format. Please refer to the manual for further information."); |
---|
956 | } |
---|
957 | reinit_yylp(); |
---|
958 | fclose( yylpin ); |
---|
959 | fp = NULL; |
---|
960 | iiRunInit(IDPACKAGE(pl)); |
---|
961 | |
---|
962 | { |
---|
963 | libstackv ls; |
---|
964 | for(ls = library_stack; (ls != NULL) && (ls != ls_start); ) |
---|
965 | { |
---|
966 | if(ls->to_be_done) |
---|
967 | { |
---|
968 | ls->to_be_done=FALSE; |
---|
969 | iiLibCmd(ls->get(),autoexport,tellerror,FALSE); |
---|
970 | ls = ls->pop(newlib); |
---|
971 | } |
---|
972 | } |
---|
973 | #if 0 |
---|
974 | PrintS("--------------------\n"); |
---|
975 | for(ls = library_stack; ls != NULL; ls = ls->next) |
---|
976 | { |
---|
977 | Print("%s: LIB-stack:(%d), %s %s\n", newlib, ls->cnt, ls->get(), |
---|
978 | ls->to_be_done ? "not loaded" : "loaded"); |
---|
979 | } |
---|
980 | PrintS("--------------------\n"); |
---|
981 | #endif |
---|
982 | } |
---|
983 | |
---|
984 | if(fp != NULL) fclose(fp); |
---|
985 | return FALSE; |
---|
986 | } |
---|
987 | |
---|
988 | |
---|
989 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
990 | procinfo *iiInitSingularProcinfo(procinfov pi, const char *libname, |
---|
991 | const char *procname, int, long pos, BOOLEAN pstatic) |
---|
992 | { |
---|
993 | memset(pi,0,sizeof(*pi)); |
---|
994 | pi->libname = omStrDup(libname); |
---|
995 | pi->procname = omStrDup(procname); |
---|
996 | pi->language = LANG_SINGULAR; |
---|
997 | pi->ref = 1; |
---|
998 | pi->is_static = pstatic; |
---|
999 | pi->data.s.proc_start = pos; |
---|
1000 | return(pi); |
---|
1001 | } |
---|
1002 | |
---|
1003 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1004 | int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic, |
---|
1005 | BOOLEAN(*func)(leftv res, leftv v)) |
---|
1006 | { |
---|
1007 | procinfov pi; |
---|
1008 | idhdl h; |
---|
1009 | |
---|
1010 | #ifndef SING_NDEBUG |
---|
1011 | int dummy; |
---|
1012 | if (IsCmd(procname,dummy)) |
---|
1013 | { |
---|
1014 | Werror(">>%s< is a reserved name",procname); |
---|
1015 | return 0; |
---|
1016 | } |
---|
1017 | #endif |
---|
1018 | |
---|
1019 | h=IDROOT->get(procname,0); |
---|
1020 | if ((h!=NULL) |
---|
1021 | && (IDTYP(h)==PROC_CMD)) |
---|
1022 | { |
---|
1023 | pi = IDPROC(h); |
---|
1024 | if ((pi->language == LANG_SINGULAR) |
---|
1025 | &&(BVERBOSE(V_REDEFINE))) |
---|
1026 | Warn("extend `%s`",procname); |
---|
1027 | } |
---|
1028 | else |
---|
1029 | { |
---|
1030 | h = enterid(procname,0, PROC_CMD, &IDROOT, TRUE); |
---|
1031 | } |
---|
1032 | if ( h!= NULL ) |
---|
1033 | { |
---|
1034 | pi = IDPROC(h); |
---|
1035 | if((pi->language == LANG_SINGULAR) |
---|
1036 | ||(pi->language == LANG_NONE)) |
---|
1037 | { |
---|
1038 | omfree(pi->libname); |
---|
1039 | pi->libname = omStrDup(libname); |
---|
1040 | omfree(pi->procname); |
---|
1041 | pi->procname = omStrDup(procname); |
---|
1042 | pi->language = LANG_C; |
---|
1043 | pi->ref = 1; |
---|
1044 | pi->is_static = pstatic; |
---|
1045 | pi->data.o.function = func; |
---|
1046 | } |
---|
1047 | else if(pi->language == LANG_C) |
---|
1048 | { |
---|
1049 | if(pi->data.o.function == func) |
---|
1050 | { |
---|
1051 | pi->ref++; |
---|
1052 | } |
---|
1053 | else |
---|
1054 | { |
---|
1055 | omfree(pi->libname); |
---|
1056 | pi->libname = omStrDup(libname); |
---|
1057 | omfree(pi->procname); |
---|
1058 | pi->procname = omStrDup(procname); |
---|
1059 | pi->language = LANG_C; |
---|
1060 | pi->ref = 1; |
---|
1061 | pi->is_static = pstatic; |
---|
1062 | pi->data.o.function = func; |
---|
1063 | } |
---|
1064 | } |
---|
1065 | else |
---|
1066 | Warn("internal error: unknown procedure type %d",pi->language); |
---|
1067 | if (currPack->language==LANG_SINGULAR) currPack->language=LANG_MIX; |
---|
1068 | return(1); |
---|
1069 | } |
---|
1070 | else |
---|
1071 | { |
---|
1072 | WarnS("iiAddCproc: failed."); |
---|
1073 | } |
---|
1074 | return(0); |
---|
1075 | } |
---|
1076 | |
---|
1077 | int iiAddCprocTop(const char *libname, const char *procname, BOOLEAN pstatic, |
---|
1078 | BOOLEAN(*func)(leftv res, leftv v)) |
---|
1079 | { |
---|
1080 | int r=iiAddCproc(libname,procname,pstatic,func); |
---|
1081 | package s=currPack; |
---|
1082 | currPack=basePack; |
---|
1083 | if (r) r=iiAddCproc(libname,procname,pstatic,func); |
---|
1084 | currPack=s; |
---|
1085 | return r; |
---|
1086 | } |
---|
1087 | |
---|
1088 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1089 | #ifdef HAVE_DYNAMIC_LOADING |
---|
1090 | #include <map> |
---|
1091 | #include <string> |
---|
1092 | #include <pthread.h> |
---|
1093 | |
---|
1094 | STATIC_VAR std::map<std::string, void *> *dyn_modules; |
---|
1095 | |
---|
1096 | bool registered_dyn_module(char *fullname) { |
---|
1097 | if (dyn_modules == NULL) |
---|
1098 | return false; |
---|
1099 | std::string fname = fullname; |
---|
1100 | return !(dyn_modules->count(fname)); |
---|
1101 | } |
---|
1102 | |
---|
1103 | void register_dyn_module(char *fullname, void * handle) { |
---|
1104 | std::string fname = fullname; |
---|
1105 | if (dyn_modules == NULL) |
---|
1106 | dyn_modules = new std::map<std::string, void *>(); |
---|
1107 | dyn_modules->insert(std::pair<std::string, void *>(fname, handle)); |
---|
1108 | } |
---|
1109 | |
---|
1110 | void close_all_dyn_modules() { |
---|
1111 | for (std::map<std::string, void *>::iterator it = dyn_modules->begin(); |
---|
1112 | it != dyn_modules->end(); |
---|
1113 | it++) |
---|
1114 | { |
---|
1115 | dynl_close(it->second); |
---|
1116 | } |
---|
1117 | delete dyn_modules; |
---|
1118 | dyn_modules = NULL; |
---|
1119 | } |
---|
1120 | BOOLEAN load_modules_aux(const char *newlib, char *fullname, BOOLEAN autoexport) |
---|
1121 | { |
---|
1122 | #ifdef HAVE_STATIC |
---|
1123 | WerrorS("mod_init: static version can not load modules"); |
---|
1124 | return TRUE; |
---|
1125 | #else |
---|
1126 | /* |
---|
1127 | typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname, |
---|
1128 | BOOLEAN pstatic, |
---|
1129 | BOOLEAN(*func)(leftv res, leftv v))); |
---|
1130 | */ |
---|
1131 | SModulFunc_t fktn; |
---|
1132 | idhdl pl; |
---|
1133 | char *plib = iiConvName(newlib); |
---|
1134 | BOOLEAN RET=TRUE; |
---|
1135 | int token; |
---|
1136 | char FullName[256]; |
---|
1137 | |
---|
1138 | memset(FullName,0,256); |
---|
1139 | |
---|
1140 | if( *fullname != '/' && *fullname != '.' ) |
---|
1141 | sprintf(FullName, "./%s", newlib); |
---|
1142 | else strncpy(FullName, fullname,255); |
---|
1143 | |
---|
1144 | |
---|
1145 | if(IsCmd(plib, token)) |
---|
1146 | { |
---|
1147 | Werror("'%s' is resered identifier\n", plib); |
---|
1148 | goto load_modules_end; |
---|
1149 | } |
---|
1150 | pl = basePack->idroot->get(plib,0); /* packages only in top level |
---|
1151 | (see enterid) */ |
---|
1152 | if ((pl!=NULL) |
---|
1153 | &&(IDTYP(pl)==PACKAGE_CMD)) |
---|
1154 | { |
---|
1155 | if(IDPACKAGE(pl)->language==LANG_C) |
---|
1156 | { |
---|
1157 | if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as package", newlib); |
---|
1158 | omFree(plib); |
---|
1159 | return FALSE; |
---|
1160 | } |
---|
1161 | else if(IDPACKAGE(pl)->language==LANG_MIX) |
---|
1162 | { |
---|
1163 | if (BVERBOSE(V_LOAD_LIB)) Warn( "%s contain binary parts, cannot load", newlib); |
---|
1164 | omFree(plib); |
---|
1165 | return FALSE; |
---|
1166 | } |
---|
1167 | } |
---|
1168 | else |
---|
1169 | { |
---|
1170 | pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE ); |
---|
1171 | omFree(plib); /* enterid copied plib*/ |
---|
1172 | IDPACKAGE(pl)->libname=omStrDup(newlib); |
---|
1173 | } |
---|
1174 | IDPACKAGE(pl)->language = LANG_C; |
---|
1175 | if (registered_dyn_module(FullName)) |
---|
1176 | { |
---|
1177 | if (BVERBOSE(V_LOAD_LIB)) Warn( "%s already loaded as C library", fullname); |
---|
1178 | return FALSE; |
---|
1179 | } |
---|
1180 | if((IDPACKAGE(pl)->handle=dynl_open(FullName))==(void *)NULL) |
---|
1181 | { |
---|
1182 | Werror("dynl_open failed:%s", dynl_error()); |
---|
1183 | Werror("%s not found", newlib); |
---|
1184 | killhdl2(pl,&(basePack->idroot),NULL); // remove package |
---|
1185 | goto load_modules_end; |
---|
1186 | } |
---|
1187 | else |
---|
1188 | { |
---|
1189 | SModulFunctions sModulFunctions; |
---|
1190 | |
---|
1191 | package s=currPack; |
---|
1192 | currPack=IDPACKAGE(pl); |
---|
1193 | fktn = (SModulFunc_t)dynl_sym(IDPACKAGE(pl)->handle, "mod_init"); |
---|
1194 | if( fktn!= NULL) |
---|
1195 | { |
---|
1196 | sModulFunctions.iiArithAddCmd = iiArithAddCmd; |
---|
1197 | if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop; |
---|
1198 | else sModulFunctions.iiAddCproc = iiAddCproc; |
---|
1199 | int ver=(*fktn)(&sModulFunctions); |
---|
1200 | if (ver==MAX_TOK) |
---|
1201 | { |
---|
1202 | if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded %s\n", fullname); |
---|
1203 | } |
---|
1204 | else |
---|
1205 | { |
---|
1206 | Warn("loaded %s for a different version of Singular(expected MAX_TOK: %d, got %d)",fullname,MAX_TOK,ver); |
---|
1207 | } |
---|
1208 | currPack->loaded=1; |
---|
1209 | currPack=s; /* reset currPack to previous */ |
---|
1210 | register_dyn_module(fullname, IDPACKAGE(pl)->handle); |
---|
1211 | RET=FALSE; |
---|
1212 | } |
---|
1213 | else |
---|
1214 | { |
---|
1215 | Werror("mod_init not found:: %s\nThis is probably not a dynamic module for Singular!\n", dynl_error()); |
---|
1216 | errorreported=0; |
---|
1217 | if(IDPACKAGE(pl)->idroot==NULL) |
---|
1218 | killhdl2(pl,&(basePack->idroot),NULL); // remove package |
---|
1219 | } |
---|
1220 | } |
---|
1221 | |
---|
1222 | load_modules_end: |
---|
1223 | return RET; |
---|
1224 | #endif /*STATIC */ |
---|
1225 | } |
---|
1226 | |
---|
1227 | BOOLEAN load_modules(const char *newlib, char *fullname, BOOLEAN autoexport) { |
---|
1228 | GLOBAL_VAR static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
---|
1229 | pthread_mutex_lock(&mutex); |
---|
1230 | BOOLEAN r = load_modules_aux(newlib, fullname, autoexport); |
---|
1231 | pthread_mutex_unlock(&mutex); |
---|
1232 | return r; |
---|
1233 | } |
---|
1234 | #endif /* HAVE_DYNAMIC_LOADING */ |
---|
1235 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1236 | BOOLEAN load_builtin(const char *newlib, BOOLEAN autoexport, SModulFunc_t init) |
---|
1237 | { |
---|
1238 | int iiAddCproc(const char *libname, const char *procname, BOOLEAN pstatic, |
---|
1239 | BOOLEAN(*func)(leftv res, leftv v)); |
---|
1240 | /* |
---|
1241 | typedef int (*fktn_t)(int(*iiAddCproc)(const char *libname, const char *procname, |
---|
1242 | BOOLEAN pstatic, |
---|
1243 | BOOLEAN(*func)(leftv res, leftv v))); |
---|
1244 | */ |
---|
1245 | // SModulFunc_t fktn; |
---|
1246 | idhdl pl; |
---|
1247 | char *plib = iiConvName(newlib); |
---|
1248 | // BOOLEAN RET=TRUE; |
---|
1249 | // int token; |
---|
1250 | |
---|
1251 | pl = basePack->idroot->get(plib,0); // search PACKAGE only in Top |
---|
1252 | if ((pl!=NULL) |
---|
1253 | &&(IDTYP(pl)==PACKAGE_CMD)) |
---|
1254 | { |
---|
1255 | if(IDPACKAGE(pl)->language==LANG_C) |
---|
1256 | { |
---|
1257 | if (BVERBOSE(V_LOAD_LIB)) Warn( "(builtin) %s already loaded", newlib); |
---|
1258 | omFree(plib); |
---|
1259 | return FALSE; |
---|
1260 | } |
---|
1261 | } |
---|
1262 | else |
---|
1263 | { |
---|
1264 | pl = enterid( plib,0, PACKAGE_CMD, &IDROOT, TRUE ); |
---|
1265 | IDPACKAGE(pl)->libname=omStrDup(newlib); |
---|
1266 | } |
---|
1267 | IDPACKAGE(pl)->language = LANG_C; |
---|
1268 | |
---|
1269 | IDPACKAGE(pl)->handle=(void *)NULL; |
---|
1270 | SModulFunctions sModulFunctions; |
---|
1271 | |
---|
1272 | package s=currPack; |
---|
1273 | currPack=IDPACKAGE(pl); |
---|
1274 | if( init!= NULL) |
---|
1275 | { |
---|
1276 | sModulFunctions.iiArithAddCmd = iiArithAddCmd; |
---|
1277 | if (autoexport) sModulFunctions.iiAddCproc = iiAddCprocTop; |
---|
1278 | else sModulFunctions.iiAddCproc = iiAddCproc; |
---|
1279 | (*init)(&sModulFunctions); |
---|
1280 | } |
---|
1281 | if (BVERBOSE(V_LOAD_LIB)) Print( "// ** loaded (builtin) %s \n", newlib); |
---|
1282 | currPack->loaded=1; |
---|
1283 | currPack=s; |
---|
1284 | |
---|
1285 | return FALSE; |
---|
1286 | } |
---|
1287 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1288 | void module_help_main(const char *newlib,const char *help) |
---|
1289 | { |
---|
1290 | char *plib = iiConvName(newlib); |
---|
1291 | idhdl pl = basePack->idroot->get(plib,0); |
---|
1292 | if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD)) |
---|
1293 | Werror(">>%s<< is not a package (trying to add package help)",plib); |
---|
1294 | else |
---|
1295 | { |
---|
1296 | package s=currPack; |
---|
1297 | currPack=IDPACKAGE(pl); |
---|
1298 | idhdl h=enterid("info",0,STRING_CMD,&IDROOT,FALSE); |
---|
1299 | IDSTRING(h)=omStrDup(help); |
---|
1300 | currPack=s; |
---|
1301 | } |
---|
1302 | } |
---|
1303 | void module_help_proc(const char *newlib,const char *p, const char *help) |
---|
1304 | { |
---|
1305 | char *plib = iiConvName(newlib); |
---|
1306 | idhdl pl = basePack->idroot->get(plib,0); |
---|
1307 | if ((pl==NULL)||(IDTYP(pl)!=PACKAGE_CMD)) |
---|
1308 | Werror(">>%s<< is not a package(trying to add help for %s)",plib,p); |
---|
1309 | else |
---|
1310 | { |
---|
1311 | package s=currPack; |
---|
1312 | currPack=IDPACKAGE(pl); |
---|
1313 | char buff[256]; |
---|
1314 | buff[255]='\0'; |
---|
1315 | strncpy(buff,p,255); |
---|
1316 | strncat(buff,"_help",255-strlen(p)); |
---|
1317 | idhdl h=enterid(buff,0,STRING_CMD,&IDROOT,FALSE); |
---|
1318 | IDSTRING(h)=omStrDup(help); |
---|
1319 | currPack=s; |
---|
1320 | } |
---|
1321 | } |
---|
1322 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1323 | |
---|
1324 | #ifdef HAVE_DYNAMIC_LOADING |
---|
1325 | // loads a dynamic module from the binary path and returns a named function |
---|
1326 | // returns NULL, if something fails |
---|
1327 | void* binary_module_function(const char* newlib, const char* funcname) |
---|
1328 | { |
---|
1329 | void* result = NULL; |
---|
1330 | |
---|
1331 | const char* bin_dir = feGetResource('b'); |
---|
1332 | if (!bin_dir) { return NULL; } |
---|
1333 | |
---|
1334 | char path_name[MAXPATHLEN]; |
---|
1335 | sprintf(path_name, "%s%s%s.%s", bin_dir, DIR_SEPP, newlib, MODULE_SUFFIX_STRING); |
---|
1336 | |
---|
1337 | void* openlib = dynl_open(path_name); |
---|
1338 | if(!openlib) |
---|
1339 | { |
---|
1340 | Werror("dynl_open of %s failed:%s", path_name, dynl_error()); |
---|
1341 | return NULL; |
---|
1342 | } |
---|
1343 | result = dynl_sym(openlib, funcname); |
---|
1344 | if (!result) Werror("%s: %s\n", funcname, dynl_error()); |
---|
1345 | |
---|
1346 | return result; |
---|
1347 | } |
---|
1348 | #endif |
---|
1349 | |
---|
1350 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1351 | char mytoupper(char c) |
---|
1352 | { |
---|
1353 | if(c>=97 && c<=(97+26)) c-=32; |
---|
1354 | return(c); |
---|
1355 | } |
---|
1356 | |
---|
1357 | char mytolower(char c) |
---|
1358 | { |
---|
1359 | if(c>=65 && c<=(65+26)) c+=32; |
---|
1360 | return(c); |
---|
1361 | } |
---|
1362 | |
---|
1363 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1364 | //#if defined(WINNT) |
---|
1365 | //# define FS_SEP '\\' |
---|
1366 | //#else |
---|
1367 | //# define FS_SEP '/' |
---|
1368 | //#endif |
---|
1369 | |
---|
1370 | char *iiConvName(const char *libname) |
---|
1371 | { |
---|
1372 | char *tmpname = omStrDup(libname); |
---|
1373 | char *p = strrchr(tmpname, DIR_SEP); |
---|
1374 | char *r; |
---|
1375 | if(p==NULL) p = tmpname; else p++; |
---|
1376 | // p is now the start of the file name (without path) |
---|
1377 | r=p; |
---|
1378 | while(isalnum(*r)||(*r=='_')) r++; |
---|
1379 | // r point the the end of the main part of the filename |
---|
1380 | *r = '\0'; |
---|
1381 | r = omStrDup(p); |
---|
1382 | *r = mytoupper(*r); |
---|
1383 | // printf("iiConvName: '%s' '%s' => '%s'\n", libname, tmpname, r); |
---|
1384 | omFree((ADDRESS)tmpname); |
---|
1385 | |
---|
1386 | return(r); |
---|
1387 | } |
---|
1388 | |
---|
1389 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1390 | #if 0 /* debug only */ |
---|
1391 | void piShowProcList() |
---|
1392 | { |
---|
1393 | idhdl h; |
---|
1394 | procinfo *proc; |
---|
1395 | char *name; |
---|
1396 | |
---|
1397 | Print( "%-15s %20s %s,%s %s,%s %s,%s\n", "Library", "function", |
---|
1398 | "line", "start", "line", "body", "line", "example"); |
---|
1399 | for(h = IDROOT; h != NULL; h = IDNEXT(h)) |
---|
1400 | { |
---|
1401 | if(IDTYP(h) == PROC_CMD) |
---|
1402 | { |
---|
1403 | proc = IDPROC(h); |
---|
1404 | if(strcmp(proc->procname, IDID(h))!=0) |
---|
1405 | { |
---|
1406 | name = (char *)omAlloc(strlen(IDID(h))+strlen(proc->procname)+4); |
---|
1407 | sprintf(name, "%s -> %s", IDID(h), proc->procname); |
---|
1408 | Print( "%d %-15s %20s ", proc->is_static ? 1 : 0, proc->libname, name); |
---|
1409 | omFree((ADDRESS)name); |
---|
1410 | } |
---|
1411 | else |
---|
1412 | Print( "%d %-15s %20s ", proc->is_static ? 1 : 0, proc->libname, |
---|
1413 | proc->procname); |
---|
1414 | if(proc->language==LANG_SINGULAR) |
---|
1415 | Print("line %-5ld %4d,%-5ld %4d,%-5ld\n", |
---|
1416 | proc->data.s.proc_start, |
---|
1417 | proc->data.s.body_lineno, proc->data.s.body_start, |
---|
1418 | proc->data.s.example_lineno, proc->data.s.example_start); |
---|
1419 | else if(proc->language==LANG_C) |
---|
1420 | PrintS("type: object\n"); |
---|
1421 | } |
---|
1422 | } |
---|
1423 | } |
---|
1424 | #endif |
---|
1425 | |
---|
1426 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1427 | //char *iiLineNo(char *procname, int lineno) |
---|
1428 | //{ |
---|
1429 | // char buf[256]; |
---|
1430 | // idhdl pn = ggetid(procname); |
---|
1431 | // procinfo *pi = IDPROC(pn); |
---|
1432 | // |
---|
1433 | // sprintf(buf, "%s %3d\0", procname, lineno); |
---|
1434 | // //sprintf(buf, "%s::%s %3d\0", pi->libname, pi->procname, |
---|
1435 | // // lineno + pi->data.s.body_lineno); |
---|
1436 | // return(buf); |
---|
1437 | //} |
---|
1438 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|
1439 | #ifdef HAVE_LIBPARSER |
---|
1440 | void libstack::push(const char */*p*/, char *libn) |
---|
1441 | { |
---|
1442 | libstackv lp; |
---|
1443 | if( !iiGetLibStatus(libn)) |
---|
1444 | { |
---|
1445 | for(lp = this;lp!=NULL;lp=lp->next) |
---|
1446 | { |
---|
1447 | if(strcmp(lp->get(), libn)==0) break; |
---|
1448 | } |
---|
1449 | if(lp==NULL) |
---|
1450 | { |
---|
1451 | libstackv ls = (libstack *)omAlloc0Bin(libstack_bin); |
---|
1452 | ls->next = this; |
---|
1453 | ls->libname = omStrDup(libn); |
---|
1454 | ls->to_be_done = TRUE; |
---|
1455 | if(library_stack != NULL) ls->cnt = library_stack->cnt+1; else ls->cnt = 0; |
---|
1456 | library_stack = ls; |
---|
1457 | } |
---|
1458 | } |
---|
1459 | } |
---|
1460 | |
---|
1461 | libstackv libstack::pop(const char */*p*/) |
---|
1462 | { |
---|
1463 | libstackv ls = this; |
---|
1464 | //omFree((ADDRESS)ls->libname); |
---|
1465 | library_stack = ls->next; |
---|
1466 | omFreeBin((ADDRESS)ls, libstack_bin); |
---|
1467 | return(library_stack); |
---|
1468 | } |
---|
1469 | |
---|
1470 | #endif /* HAVE_LIBPARSER */ |
---|
1471 | /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ |
---|