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