1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | |
---|
6 | /* |
---|
7 | * ABSTRACT: identfier handling |
---|
8 | */ |
---|
9 | |
---|
10 | #include <string.h> |
---|
11 | |
---|
12 | #include <Singular/mod2.h> |
---|
13 | #include <Singular/static.h> |
---|
14 | #include <omalloc.h> |
---|
15 | #include <Singular/tok.h> |
---|
16 | #include <kernel/options.h> |
---|
17 | #include <Singular/ipshell.h> |
---|
18 | #include <kernel/intvec.h> |
---|
19 | #include <kernel/febase.h> |
---|
20 | #include <kernel/numbers.h> |
---|
21 | #include <kernel/longrat.h> |
---|
22 | #include <kernel/polys.h> |
---|
23 | #include <kernel/ring.h> |
---|
24 | #include <kernel/ideals.h> |
---|
25 | #include <kernel/matpol.h> |
---|
26 | #include <Singular/lists.h> |
---|
27 | #include <Singular/attrib.h> |
---|
28 | #include <Singular/silink.h> |
---|
29 | #include <kernel/syz.h> |
---|
30 | #include <Singular/ipid.h> |
---|
31 | #include <Singular/Fan.h> |
---|
32 | #include <Singular/Cone.h> |
---|
33 | |
---|
34 | #ifdef HAVE_DYNAMIC_LOADING |
---|
35 | #include <kernel/mod_raw.h> |
---|
36 | #endif /* HAVE_DYNAMIC_LOADING */ |
---|
37 | |
---|
38 | omBin sip_command_bin = omGetSpecBin(sizeof(sip_command)); |
---|
39 | omBin sip_package_bin = omGetSpecBin(sizeof(sip_package)); |
---|
40 | //omBin ip_package_bin = omGetSpecBin(sizeof(ip_package)); |
---|
41 | omBin idrec_bin = omGetSpecBin(sizeof(idrec)); |
---|
42 | |
---|
43 | proclevel *procstack=NULL; |
---|
44 | #define TEST |
---|
45 | //idhdl idroot = NULL; |
---|
46 | |
---|
47 | idhdl currPackHdl = NULL; |
---|
48 | idhdl basePackHdl = NULL; |
---|
49 | package currPack =NULL; |
---|
50 | package basePack =NULL; |
---|
51 | idhdl currRingHdl = NULL; |
---|
52 | ring currRing = NULL; |
---|
53 | ideal currQuotient = NULL; |
---|
54 | const char* iiNoName="_"; |
---|
55 | |
---|
56 | void paCleanUp(package pack); |
---|
57 | |
---|
58 | /*0 implementation*/ |
---|
59 | |
---|
60 | int iiS2I(const char *s) |
---|
61 | { |
---|
62 | int i; |
---|
63 | i=s[0]; |
---|
64 | if (s[1]!='\0') |
---|
65 | { |
---|
66 | i=(i<<8)+s[1]; |
---|
67 | if (s[2]!='\0') |
---|
68 | { |
---|
69 | i=(i<<8)+s[2]; |
---|
70 | if (s[3]!='\0') |
---|
71 | { |
---|
72 | i=(i<<8)+s[3]; |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | return i; |
---|
77 | } |
---|
78 | |
---|
79 | idhdl idrec::get(const char * s, int lev) |
---|
80 | { |
---|
81 | assume(s!=NULL); |
---|
82 | assume((lev>=0) && (lev<=1000)); //not really, but if it isnt in that bounds.. |
---|
83 | idhdl h = this; |
---|
84 | idhdl found=NULL; |
---|
85 | int l; |
---|
86 | const char *id; |
---|
87 | int i=iiS2I(s); |
---|
88 | int less4=(i < (1<<24)); |
---|
89 | while (h!=NULL) |
---|
90 | { |
---|
91 | omCheckAddr((ADDRESS)IDID(h)); |
---|
92 | l=IDLEV(h); |
---|
93 | if ((l==0)||(l==lev)) |
---|
94 | { |
---|
95 | if (i==h->id_i) |
---|
96 | { |
---|
97 | id=IDID(h); |
---|
98 | if (less4 || (0 == strcmp(s+4,id+4))) |
---|
99 | { |
---|
100 | if (l==lev) return h; |
---|
101 | found=h; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | h = IDNEXT(h); |
---|
106 | } |
---|
107 | return found; |
---|
108 | } |
---|
109 | |
---|
110 | //idrec::~idrec() |
---|
111 | //{ |
---|
112 | // if (id!=NULL) |
---|
113 | // { |
---|
114 | // omFree((ADDRESS)id); |
---|
115 | // id=NULL; |
---|
116 | // } |
---|
117 | // /* much more !! */ |
---|
118 | //} |
---|
119 | |
---|
120 | idhdl idrec::set(const char * s, int lev, int t, BOOLEAN init) |
---|
121 | { |
---|
122 | //printf("define %s, %x, lev: %d, typ: %d\n", s,s,lev,t); |
---|
123 | idhdl h = (idrec *)omAlloc0Bin(idrec_bin); |
---|
124 | int len = 0; |
---|
125 | IDID(h) = s; |
---|
126 | IDTYP(h) = t; |
---|
127 | IDLEV(h) = lev; |
---|
128 | IDNEXT(h) = this; |
---|
129 | h->id_i=iiS2I(s); |
---|
130 | if (init) |
---|
131 | { |
---|
132 | switch (t) |
---|
133 | { |
---|
134 | //the type with init routines: |
---|
135 | case INTVEC_CMD: |
---|
136 | case INTMAT_CMD: |
---|
137 | IDINTVEC(h) = new intvec(); |
---|
138 | break; |
---|
139 | case NUMBER_CMD: |
---|
140 | IDNUMBER(h) = nInit(0); |
---|
141 | break; |
---|
142 | case BIGINT_CMD: |
---|
143 | IDNUMBER(h) = nlInit(0, NULL /* dummy for nlInit*/); |
---|
144 | break; |
---|
145 | case IDEAL_CMD: |
---|
146 | case MODUL_CMD: |
---|
147 | IDFLAG(h) = Sy_bit(FLAG_STD); |
---|
148 | case MATRIX_CMD: |
---|
149 | IDIDEAL(h) = idInit(1,1); |
---|
150 | break; |
---|
151 | case MAP_CMD: |
---|
152 | IDIDEAL(h) = idInit(1,1); |
---|
153 | IDMAP(h)->preimage = omStrDup(IDID(currRingHdl)); |
---|
154 | break; |
---|
155 | case STRING_CMD: |
---|
156 | len=1; |
---|
157 | break; |
---|
158 | case LIST_CMD: |
---|
159 | IDLIST(h)=(lists)omAllocBin(slists_bin); |
---|
160 | IDLIST(h)->Init(); |
---|
161 | break; |
---|
162 | case LINK_CMD: |
---|
163 | IDLINK(h)=(si_link) omAlloc0Bin(sip_link_bin); |
---|
164 | break; |
---|
165 | case RING_CMD: |
---|
166 | IDRING(h) = (ring) omAlloc0Bin(sip_sring_bin); |
---|
167 | break; |
---|
168 | case PACKAGE_CMD: |
---|
169 | IDPACKAGE(h) = (package) omAlloc0Bin(sip_package_bin); |
---|
170 | break; |
---|
171 | case PROC_CMD: |
---|
172 | IDPROC(h) = (procinfo*) omAlloc0Bin(procinfo_bin); |
---|
173 | break; |
---|
174 | //the types with the standard init: set the struct to zero |
---|
175 | case RESOLUTION_CMD: |
---|
176 | len=sizeof(ssyStrategy); |
---|
177 | break; |
---|
178 | //other types: without init (int,script,poly,def,package) |
---|
179 | } |
---|
180 | if (len!=0) |
---|
181 | { |
---|
182 | IDSTRING(h) = (char *)omAlloc0(len); |
---|
183 | } |
---|
184 | // additional settings:-------------------------------------- |
---|
185 | #if 0 |
---|
186 | // this leads to a memory leak |
---|
187 | if (t == QRING_CMD) |
---|
188 | { |
---|
189 | // IDRING(h)=rCopy(currRing); |
---|
190 | /* QRING_CMD is ring dep => currRing !=NULL */ |
---|
191 | } |
---|
192 | else |
---|
193 | #endif |
---|
194 | if (t == PROC_CMD) |
---|
195 | { |
---|
196 | IDPROC(h)->language=LANG_NONE; |
---|
197 | } |
---|
198 | else if (t == PACKAGE_CMD) |
---|
199 | { |
---|
200 | IDPACKAGE(h)->language=LANG_NONE; |
---|
201 | IDPACKAGE(h)->loaded = FALSE; |
---|
202 | } |
---|
203 | } |
---|
204 | // -------------------------------------------------------- |
---|
205 | return h; |
---|
206 | } |
---|
207 | |
---|
208 | char * idrec::String() |
---|
209 | { |
---|
210 | sleftv tmp; |
---|
211 | memset(&tmp,0,sizeof(sleftv)); |
---|
212 | tmp.rtyp=IDTYP(this); |
---|
213 | tmp.data=IDDATA(this); |
---|
214 | tmp.name=IDID(this); |
---|
215 | return tmp.String(); |
---|
216 | } |
---|
217 | |
---|
218 | //#define KAI |
---|
219 | idhdl enterid(const char * s, int lev, int t, idhdl* root, BOOLEAN init) |
---|
220 | { |
---|
221 | idhdl h; |
---|
222 | s=omStrDup(s); |
---|
223 | // is it already defined in root ? |
---|
224 | if ((h=(*root)->get(s,lev))!=NULL) |
---|
225 | { |
---|
226 | if (IDLEV(h)==lev) |
---|
227 | { |
---|
228 | if ((IDTYP(h) == t)||(t==DEF_CMD)) |
---|
229 | { |
---|
230 | if ((IDTYP(h)==PACKAGE_CMD) |
---|
231 | && (strcmp(s,"Top")==0)) |
---|
232 | { |
---|
233 | goto errlabel; |
---|
234 | } |
---|
235 | if (BVERBOSE(V_REDEFINE)) |
---|
236 | Warn("redefining %s **",s); |
---|
237 | if (s==IDID(h)) IDID(h)=NULL; |
---|
238 | killhdl2(h,root,currRing); |
---|
239 | } |
---|
240 | else |
---|
241 | goto errlabel; |
---|
242 | } |
---|
243 | } |
---|
244 | // is it already defined in idroot ? |
---|
245 | else if (*root != IDROOT) |
---|
246 | { |
---|
247 | if ((h=IDROOT->get(s,lev))!=NULL) |
---|
248 | { |
---|
249 | if (IDLEV(h)==lev) |
---|
250 | { |
---|
251 | if ((IDTYP(h) == t)||(t==DEF_CMD)) |
---|
252 | { |
---|
253 | if (BVERBOSE(V_REDEFINE)) |
---|
254 | Warn("redefining %s **",s); |
---|
255 | if (s==IDID(h)) IDID(h)=NULL; |
---|
256 | killhdl2(h,&IDROOT,NULL); |
---|
257 | } |
---|
258 | else |
---|
259 | goto errlabel; |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | // is it already defined in currRing->idroot ? |
---|
264 | else if ((currRing!=NULL)&&((*root) != currRing->idroot)) |
---|
265 | { |
---|
266 | if ((h=currRing->idroot->get(s,lev))!=NULL) |
---|
267 | { |
---|
268 | if (IDLEV(h)==lev) |
---|
269 | { |
---|
270 | if ((IDTYP(h) == t)||(t==DEF_CMD)) |
---|
271 | { |
---|
272 | if (BVERBOSE(V_REDEFINE)) |
---|
273 | Warn("redefining %s **",s); |
---|
274 | IDID(h)=NULL; |
---|
275 | killhdl2(h,&currRing->idroot,currRing); |
---|
276 | } |
---|
277 | else |
---|
278 | goto errlabel; |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | *root = (*root)->set(s, lev, t, init); |
---|
283 | #ifndef NDEBUG |
---|
284 | checkall(); |
---|
285 | #endif |
---|
286 | return *root; |
---|
287 | |
---|
288 | errlabel: |
---|
289 | //Werror("identifier `%s` in use(lev h=%d,typ=%d,t=%d, curr=%d)",s,IDLEV(h),IDTYP(h),t,lev); |
---|
290 | Werror("identifier `%s` in use",s); |
---|
291 | //listall(); |
---|
292 | omFree((ADDRESS)s); |
---|
293 | return NULL; |
---|
294 | } |
---|
295 | |
---|
296 | void killid(const char * id, idhdl * ih) |
---|
297 | { |
---|
298 | if (id!=NULL) |
---|
299 | { |
---|
300 | idhdl h = (*ih)->get(id,myynest); |
---|
301 | |
---|
302 | // id not found in global list, is it defined in current ring ? |
---|
303 | if (h==NULL) |
---|
304 | { |
---|
305 | if ((currRing!=NULL) && (*ih != (currRing->idroot))) |
---|
306 | { |
---|
307 | h = currRing->idroot->get(id,myynest); |
---|
308 | if (h!=NULL) |
---|
309 | { |
---|
310 | killhdl2(h,&(currRing->idroot),currRing); |
---|
311 | return; |
---|
312 | } |
---|
313 | } |
---|
314 | Werror("`%s` is not defined",id); |
---|
315 | return; |
---|
316 | } |
---|
317 | killhdl2(h,ih,currRing); |
---|
318 | } |
---|
319 | else |
---|
320 | Werror("kill what ?"); |
---|
321 | } |
---|
322 | |
---|
323 | void killhdl(idhdl h, package proot) |
---|
324 | { |
---|
325 | int t=IDTYP(h); |
---|
326 | if (((BEGIN_RING<t) && (t<END_RING) && (t!=QRING_CMD)) |
---|
327 | || ((t==LIST_CMD) && (lRingDependend((lists)IDDATA(h))))) |
---|
328 | killhdl2(h,&currRing->idroot,currRing); |
---|
329 | else |
---|
330 | { |
---|
331 | if(t==PACKAGE_CMD) |
---|
332 | { |
---|
333 | killhdl2(h,&(basePack->idroot),NULL); |
---|
334 | } |
---|
335 | else |
---|
336 | { |
---|
337 | idhdl s=proot->idroot; |
---|
338 | while ((s!=h) && (s!=NULL)) s=s->next; |
---|
339 | if (s!=NULL) |
---|
340 | killhdl2(h,&(proot->idroot),NULL); |
---|
341 | else if (basePack!=proot) |
---|
342 | { |
---|
343 | idhdl s=basePack->idroot; |
---|
344 | while ((s!=h) && (s!=NULL)) s=s->next; |
---|
345 | if (s!=NULL) |
---|
346 | killhdl2(h,&(basePack->idroot),currRing); |
---|
347 | else |
---|
348 | killhdl2(h,&(currRing->idroot),currRing); |
---|
349 | } |
---|
350 | } |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | void killhdl2(idhdl h, idhdl * ih, ring r) |
---|
355 | { |
---|
356 | //printf("kill %s, id %x, typ %d lev: %d\n",IDID(h),(int)IDID(h),IDTYP(h),IDLEV(h)); |
---|
357 | idhdl hh; |
---|
358 | |
---|
359 | if (h->attribute!=NULL) |
---|
360 | { |
---|
361 | at_KillAll(h,r); |
---|
362 | //h->attribute=NULL; |
---|
363 | } |
---|
364 | if ((IDTYP(h) == PACKAGE_CMD) && (strcmp(IDID(h),"Top")==0)) |
---|
365 | { |
---|
366 | WarnS("can not kill `Top`"); |
---|
367 | return; |
---|
368 | } |
---|
369 | // ring / qring -------------------------------------------------------- |
---|
370 | if ((IDTYP(h) == RING_CMD) || (IDTYP(h) == QRING_CMD)) |
---|
371 | { |
---|
372 | // any objects defined for this ring ? -> done by rKill |
---|
373 | |
---|
374 | rKill(h); |
---|
375 | } |
---|
376 | // package ------------------------------------------------------------- |
---|
377 | else if (IDTYP(h) == PACKAGE_CMD) |
---|
378 | { |
---|
379 | // any objects defined for this package ? |
---|
380 | if ((IDPACKAGE(h)->ref<=0) && (IDPACKAGE(h)->idroot!=NULL)) |
---|
381 | { |
---|
382 | if (currPack==IDPACKAGE(h)) |
---|
383 | { |
---|
384 | currPack=basePack; |
---|
385 | currPackHdl=NULL; |
---|
386 | } |
---|
387 | idhdl * hd = &IDRING(h)->idroot; |
---|
388 | idhdl hdh = IDNEXT(*hd); |
---|
389 | idhdl temp; |
---|
390 | while (hdh!=NULL) |
---|
391 | { |
---|
392 | temp = IDNEXT(hdh); |
---|
393 | killhdl2(hdh,&(IDPACKAGE(h)->idroot),NULL); |
---|
394 | hdh = temp; |
---|
395 | } |
---|
396 | killhdl2(*hd,hd,NULL); |
---|
397 | if (IDPACKAGE(h)->libname!=NULL) omFree((ADDRESS)(IDPACKAGE(h)->libname)); |
---|
398 | } |
---|
399 | paKill(IDPACKAGE(h)); |
---|
400 | if (currPackHdl==h) currPackHdl=packFindHdl(currPack); |
---|
401 | iiCheckPack(currPack); |
---|
402 | } |
---|
403 | // poly / vector ------------------------------------------------------- |
---|
404 | else if ((IDTYP(h) == POLY_CMD) || (IDTYP(h) == VECTOR_CMD)) |
---|
405 | { |
---|
406 | assume(r!=NULL); |
---|
407 | p_Delete(&IDPOLY(h),r); |
---|
408 | } |
---|
409 | // ideal / module/ matrix / map ---------------------------------------- |
---|
410 | else if ((IDTYP(h) == IDEAL_CMD) |
---|
411 | || (IDTYP(h) == MODUL_CMD) |
---|
412 | || (IDTYP(h) == MATRIX_CMD) |
---|
413 | || (IDTYP(h) == MAP_CMD)) |
---|
414 | { |
---|
415 | assume(r!=NULL); |
---|
416 | ideal iid = IDIDEAL(h); |
---|
417 | if (IDTYP(h) == MAP_CMD) |
---|
418 | { |
---|
419 | map im = IDMAP(h); |
---|
420 | omFree((ADDRESS)im->preimage); |
---|
421 | } |
---|
422 | id_Delete(&iid,r); |
---|
423 | } |
---|
424 | // string ------------------------------------------------------------- |
---|
425 | else if (IDTYP(h) == STRING_CMD) |
---|
426 | { |
---|
427 | omFree((ADDRESS)IDSTRING(h)); |
---|
428 | //IDSTRING(h)=NULL; |
---|
429 | } |
---|
430 | // proc --------------------------------------------------------------- |
---|
431 | else if (IDTYP(h) == PROC_CMD) |
---|
432 | { |
---|
433 | if (piKill(IDPROC(h))) return; |
---|
434 | } |
---|
435 | // number ------------------------------------------------------------- |
---|
436 | else if (IDTYP(h) == NUMBER_CMD) |
---|
437 | { |
---|
438 | assume(r!=NULL); |
---|
439 | n_Delete(&IDNUMBER(h),r); |
---|
440 | } |
---|
441 | // bigint ------------------------------------------------------------- |
---|
442 | else if (IDTYP(h) == BIGINT_CMD) |
---|
443 | { |
---|
444 | nlDelete(&IDNUMBER(h),NULL); |
---|
445 | } |
---|
446 | // intvec / intmat --------------------------------------------------- |
---|
447 | else if ((IDTYP(h) == INTVEC_CMD)||(IDTYP(h) == INTMAT_CMD)) |
---|
448 | { |
---|
449 | delete IDINTVEC(h); |
---|
450 | } |
---|
451 | // list ------------------------------------------------------------- |
---|
452 | else if (IDTYP(h)==LIST_CMD) |
---|
453 | { |
---|
454 | IDLIST(h)->Clean(r); |
---|
455 | //IDLIST(h)=NULL; |
---|
456 | } |
---|
457 | // link ------------------------------------------------------------- |
---|
458 | else if (IDTYP(h)==LINK_CMD) |
---|
459 | { |
---|
460 | slKill(IDLINK(h)); |
---|
461 | } |
---|
462 | else if(IDTYP(h)==RESOLUTION_CMD) |
---|
463 | { |
---|
464 | assume(r!=NULL); |
---|
465 | if (IDDATA(h)!=NULL) |
---|
466 | syKillComputation((syStrategy)IDDATA(h),r); |
---|
467 | } |
---|
468 | #ifdef HAVE_FANS |
---|
469 | // fan ------------------------------------------------------------- |
---|
470 | else if (IDTYP(h) == FAN_CMD) |
---|
471 | { |
---|
472 | Fan* fff = (Fan*)IDDATA(h); |
---|
473 | delete fff; |
---|
474 | IDDATA(h) = NULL; |
---|
475 | } |
---|
476 | // cone ------------------------------------------------------------ |
---|
477 | else if (IDTYP(h) == CONE_CMD) |
---|
478 | { |
---|
479 | Cone* ccc = (Cone*)IDDATA(h); |
---|
480 | delete ccc; |
---|
481 | IDDATA(h) = NULL; |
---|
482 | } |
---|
483 | #endif /* HAVE_FANS */ |
---|
484 | #ifdef TEST |
---|
485 | else if ((IDTYP(h)!= INT_CMD) |
---|
486 | &&(IDTYP(h)!=DEF_CMD) |
---|
487 | &&(IDTYP(h)!=ALIAS_CMD) |
---|
488 | &&(IDTYP(h)!=NONE)) |
---|
489 | Warn("unknown type to kill: %s(%d)",Tok2Cmdname(IDTYP(h)),IDTYP(h)); |
---|
490 | #endif |
---|
491 | |
---|
492 | // general ------------------------------------------------------------- |
---|
493 | // now dechain it and delete idrec |
---|
494 | #ifdef KAI |
---|
495 | if(h->next != NULL) |
---|
496 | Print("=======>%s(%x) -> %s<====\n", IDID(h), IDID(h), IDID(h->next)); |
---|
497 | else |
---|
498 | Print("=======>%s(%x)<====\n", IDID(h), IDID(h)); |
---|
499 | #endif |
---|
500 | |
---|
501 | if (IDID(h)!=NULL) // OB: ????? |
---|
502 | omFree((ADDRESS)IDID(h)); |
---|
503 | IDID(h)=NULL; |
---|
504 | IDDATA(h)=NULL; |
---|
505 | if (h == (*ih)) |
---|
506 | { |
---|
507 | // h is at the beginning of the list |
---|
508 | *ih = IDNEXT(h) /* ==*ih */; |
---|
509 | } |
---|
510 | else if (ih!=NULL) |
---|
511 | { |
---|
512 | // h is somethere in the list: |
---|
513 | hh = *ih; |
---|
514 | loop |
---|
515 | { |
---|
516 | if (hh==NULL) |
---|
517 | { |
---|
518 | PrintS(">>?<< not found for kill\n"); |
---|
519 | return; |
---|
520 | } |
---|
521 | idhdl hhh = IDNEXT(hh); |
---|
522 | if (hhh == h) |
---|
523 | { |
---|
524 | IDNEXT(hh) = IDNEXT(hhh); |
---|
525 | break; |
---|
526 | } |
---|
527 | hh = hhh; |
---|
528 | } |
---|
529 | } |
---|
530 | omFreeBin((ADDRESS)h, idrec_bin); |
---|
531 | } |
---|
532 | |
---|
533 | idhdl ggetid(const char *n, BOOLEAN local, idhdl *packhdl) |
---|
534 | { |
---|
535 | idhdl h = IDROOT->get(n,myynest); |
---|
536 | idhdl h2=NULL; |
---|
537 | *packhdl = NULL; |
---|
538 | if ((currRing!=NULL) && ((h==NULL)||(IDLEV(h)!=myynest))) |
---|
539 | { |
---|
540 | h2 = currRing->idroot->get(n,myynest); |
---|
541 | } |
---|
542 | if (h2==NULL) return h; |
---|
543 | return h2; |
---|
544 | } |
---|
545 | |
---|
546 | idhdl ggetid(const char *n) |
---|
547 | { |
---|
548 | idhdl h = IDROOT->get(n,myynest); |
---|
549 | if ((h!=NULL)&&(IDLEV(h)==myynest)) return h; |
---|
550 | idhdl h2=NULL; |
---|
551 | if (currRing!=NULL) |
---|
552 | { |
---|
553 | h2 = currRing->idroot->get(n,myynest); |
---|
554 | } |
---|
555 | if (h2!=NULL) return h2; |
---|
556 | if (h!=NULL) return h; |
---|
557 | if (basePack!=currPack) |
---|
558 | return basePack->idroot->get(n,myynest); |
---|
559 | return NULL; |
---|
560 | } |
---|
561 | |
---|
562 | void ipListFlag(idhdl h) |
---|
563 | { |
---|
564 | if (hasFlag(h,FLAG_STD)) PrintS(" (SB)"); |
---|
565 | #ifdef HAVE_PLURAL |
---|
566 | if (hasFlag(h,FLAG_TWOSTD)) PrintS(" (2SB)"); |
---|
567 | #endif |
---|
568 | } |
---|
569 | |
---|
570 | lists ipNameList(idhdl root) |
---|
571 | { |
---|
572 | idhdl h=root; |
---|
573 | /* compute the length */ |
---|
574 | int l=0; |
---|
575 | while (h!=NULL) { l++; h=IDNEXT(h); } |
---|
576 | /* allocate list */ |
---|
577 | lists L=(lists)omAllocBin(slists_bin); |
---|
578 | L->Init(l); |
---|
579 | /* copy names */ |
---|
580 | h=root; |
---|
581 | l=0; |
---|
582 | while (h!=NULL) |
---|
583 | { |
---|
584 | /* list is initialized with 0 => no need to clear anything */ |
---|
585 | L->m[l].rtyp=STRING_CMD; |
---|
586 | L->m[l].data=omStrDup(IDID(h)); |
---|
587 | l++; |
---|
588 | h=IDNEXT(h); |
---|
589 | } |
---|
590 | return L; |
---|
591 | } |
---|
592 | |
---|
593 | /* |
---|
594 | * move 'tomove' from root1 list to root2 list |
---|
595 | */ |
---|
596 | static int ipSwapId(idhdl tomove, idhdl &root1, idhdl &root2) |
---|
597 | { |
---|
598 | idhdl h; |
---|
599 | /* search 'tomove' in root2 : if found -> do nothing */ |
---|
600 | h=root2; |
---|
601 | while ((h!=NULL) && (h!=tomove)) h=IDNEXT(h); |
---|
602 | if (h!=NULL) return FALSE; /*okay */ |
---|
603 | /* search predecessor of h in root1, remove 'tomove' */ |
---|
604 | h=root1; |
---|
605 | if (tomove==h) |
---|
606 | { |
---|
607 | root1=IDNEXT(h); |
---|
608 | } |
---|
609 | else |
---|
610 | { |
---|
611 | while ((h!=NULL) && (IDNEXT(h)!=tomove)) h=IDNEXT(h); |
---|
612 | if (h==NULL) return TRUE; /* not in the list root1 -> do nothing */ |
---|
613 | IDNEXT(h)=IDNEXT(tomove); |
---|
614 | } |
---|
615 | /* add to root2 list */ |
---|
616 | IDNEXT(tomove)=root2; |
---|
617 | root2=tomove; |
---|
618 | return FALSE; |
---|
619 | } |
---|
620 | |
---|
621 | void ipMoveId(idhdl tomove) |
---|
622 | { |
---|
623 | if ((currRing!=NULL)&&(tomove!=NULL)) |
---|
624 | { |
---|
625 | if (((QRING_CMD!=IDTYP(tomove)) && RingDependend(IDTYP(tomove))) |
---|
626 | || ((IDTYP(tomove)==LIST_CMD) && (lRingDependend(IDLIST(tomove))))) |
---|
627 | { |
---|
628 | /*move 'tomove' to ring id's*/ |
---|
629 | if (ipSwapId(tomove,IDROOT,currRing->idroot)) |
---|
630 | ipSwapId(tomove,basePack->idroot,currRing->idroot); |
---|
631 | } |
---|
632 | else |
---|
633 | { |
---|
634 | /*move 'tomove' to global id's*/ |
---|
635 | ipSwapId(tomove,currRing->idroot,IDROOT); |
---|
636 | } |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | const char * piProcinfo(procinfov pi, const char *request) |
---|
641 | { |
---|
642 | if(pi == NULL) return "empty proc"; |
---|
643 | else if (strcmp(request, "libname") == 0) return pi->libname; |
---|
644 | else if (strcmp(request, "procname") == 0) return pi->procname; |
---|
645 | else if (strcmp(request, "type") == 0) |
---|
646 | { |
---|
647 | switch (pi->language) |
---|
648 | { |
---|
649 | case LANG_SINGULAR: return "singular"; break; |
---|
650 | case LANG_C: return "object"; break; |
---|
651 | case LANG_NONE: return "none"; break; |
---|
652 | default: return "unknow language"; |
---|
653 | } |
---|
654 | } |
---|
655 | else if (strcmp(request, "ref") == 0) |
---|
656 | { |
---|
657 | char p[8]; |
---|
658 | sprintf(p, "%d", pi->ref); |
---|
659 | return omStrDup(p); // MEMORY-LEAK |
---|
660 | } |
---|
661 | return "??"; |
---|
662 | } |
---|
663 | |
---|
664 | void piCleanUp(procinfov pi) |
---|
665 | { |
---|
666 | (pi->ref)--; |
---|
667 | if (pi->ref <= 0) |
---|
668 | { |
---|
669 | if (pi->libname != NULL) // OB: ???? |
---|
670 | omFree((ADDRESS)pi->libname); |
---|
671 | if (pi->procname != NULL) // OB: ???? |
---|
672 | omFree((ADDRESS)pi->procname); |
---|
673 | |
---|
674 | if( pi->language == LANG_SINGULAR) |
---|
675 | { |
---|
676 | if (pi->data.s.body != NULL) // OB: ???? |
---|
677 | omFree((ADDRESS)pi->data.s.body); |
---|
678 | } |
---|
679 | if( pi->language == LANG_C) |
---|
680 | { |
---|
681 | } |
---|
682 | memset((void *) pi, 0, sizeof(procinfo)); |
---|
683 | pi->language=LANG_NONE; |
---|
684 | } |
---|
685 | } |
---|
686 | |
---|
687 | BOOLEAN piKill(procinfov pi) |
---|
688 | { |
---|
689 | Voice *p=currentVoice; |
---|
690 | while (p!=NULL) |
---|
691 | { |
---|
692 | if (p->pi==pi && pi->ref <= 1) |
---|
693 | { |
---|
694 | Warn("`%s` in use, can not be killed",pi->procname); |
---|
695 | return TRUE; |
---|
696 | } |
---|
697 | p=p->next; |
---|
698 | } |
---|
699 | piCleanUp(pi); |
---|
700 | if (pi->ref <= 0) |
---|
701 | omFreeBin((ADDRESS)pi, procinfo_bin); |
---|
702 | return FALSE; |
---|
703 | } |
---|
704 | |
---|
705 | void paCleanUp(package pack) |
---|
706 | { |
---|
707 | (pack->ref)--; |
---|
708 | if (pack->ref < 0) |
---|
709 | { |
---|
710 | #ifndef HAVE_STATIC |
---|
711 | if( pack->language == LANG_C) |
---|
712 | { |
---|
713 | Print("//dlclose(%s)\n",pack->libname); |
---|
714 | #ifdef HAVE_DYNAMIC_LOADING |
---|
715 | dynl_close (pack->handle); |
---|
716 | #endif /* HAVE_DYNAMIC_LOADING */ |
---|
717 | } |
---|
718 | #endif /* HAVE_STATIC */ |
---|
719 | omfree((ADDRESS)pack->libname); |
---|
720 | memset((void *) pack, 0, sizeof(sip_package)); |
---|
721 | pack->language=LANG_NONE; |
---|
722 | } |
---|
723 | } |
---|
724 | |
---|
725 | char *idhdl2id(idhdl pck, idhdl h) |
---|
726 | { |
---|
727 | char *name = (char *)omAlloc(strlen(pck->id) + strlen(h->id) + 3); |
---|
728 | sprintf(name, "%s::%s", pck->id, h->id); |
---|
729 | return(name); |
---|
730 | } |
---|
731 | |
---|
732 | void iiname2hdl(const char *name, idhdl *pck, idhdl *h) |
---|
733 | { |
---|
734 | const char *q = strchr(name, ':'); |
---|
735 | char *p, *i; |
---|
736 | |
---|
737 | if(q==NULL) |
---|
738 | { |
---|
739 | p = omStrDup(""); |
---|
740 | i = (char *)omAlloc(strlen(name)+1); |
---|
741 | *i = '\0'; |
---|
742 | sscanf(name, "%s", i); |
---|
743 | } |
---|
744 | else { |
---|
745 | if( *(q+1) != ':') return; |
---|
746 | i = (char *)omAlloc(strlen(name)+1); |
---|
747 | *i = '\0'; |
---|
748 | if(name == q) |
---|
749 | { |
---|
750 | p = omStrDup(""); |
---|
751 | sscanf(name, "::%s", i); |
---|
752 | } |
---|
753 | else |
---|
754 | { |
---|
755 | p = (char *)omAlloc(strlen(name)+1); |
---|
756 | sscanf(name, "%[^:]::%s", p, i); |
---|
757 | } |
---|
758 | } |
---|
759 | //printf("Package: '%s'\n", p); |
---|
760 | //printf("Id Rec : '%s'\n", i); |
---|
761 | omFree(p); |
---|
762 | omFree(i); |
---|
763 | } |
---|
764 | |
---|
765 | #if 0 |
---|
766 | char *getnamelev() |
---|
767 | { |
---|
768 | char buf[256]; |
---|
769 | sprintf(buf, "(%s:%d)", namespaceroot->name,namespaceroot->lev); |
---|
770 | return(buf); |
---|
771 | } |
---|
772 | // warning: address of local variable `buf' returned |
---|
773 | #endif |
---|
774 | |
---|
775 | void proclevel::push(char *n) |
---|
776 | { |
---|
777 | //Print("push %s\n",n); |
---|
778 | proclevel *p=(proclevel*)omAlloc0(sizeof(proclevel)); |
---|
779 | p->cRing=currRing; |
---|
780 | p->cRingHdl=currRingHdl; |
---|
781 | p->name=n; |
---|
782 | p->cPackHdl=currPackHdl; |
---|
783 | p->cPack=currPack; |
---|
784 | p->next=this; |
---|
785 | procstack=p; |
---|
786 | } |
---|
787 | void proclevel::pop() |
---|
788 | { |
---|
789 | //Print("pop %s\n",name); |
---|
790 | //if (currRing!=::currRing) PrintS("currRing wrong\n");; |
---|
791 | //::currRing=this->currRing; |
---|
792 | //if (r==NULL) Print("set ring to NULL at lev %d(%s)\n",myynest,name); |
---|
793 | //::currRingHdl=this->currRingHdl; |
---|
794 | //if((::currRingHdl==NULL)||(IDRING(::currRingHdl)!=(::currRing))) |
---|
795 | // ::currRingHdl=rFindHdl(::currRing,NULL,NULL); |
---|
796 | //Print("restore pack=%s,1.obj=%s\n",IDID(currPackHdl),IDID(currPack->idroot)); |
---|
797 | currPackHdl=this->cPackHdl; |
---|
798 | currPack=this->cPack; |
---|
799 | iiCheckPack(currPack); |
---|
800 | proclevel *p=this; |
---|
801 | procstack=next; |
---|
802 | omFreeSize(p,sizeof(proclevel)); |
---|
803 | } |
---|
804 | |
---|
805 | idhdl packFindHdl(package r) |
---|
806 | { |
---|
807 | idhdl h=basePack->idroot; |
---|
808 | while (h!=NULL) |
---|
809 | { |
---|
810 | if ((IDTYP(h)==PACKAGE_CMD) |
---|
811 | && (IDPACKAGE(h)==r)) |
---|
812 | return h; |
---|
813 | h=IDNEXT(h); |
---|
814 | } |
---|
815 | return NULL; |
---|
816 | } |
---|