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