source: git/Singular/ipid.cc @ f4385cf

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