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