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